-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoveViewer.xaml.cs
316 lines (287 loc) · 12.1 KB
/
MoveViewer.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
using System;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using SrcChess2.Core;
namespace SrcChess2 {
/// <summary>
/// Move Item
/// </summary>
public class MoveItem {
/// <summary>Step</summary>
public string Step { get; set; }
/// <summary>Who did the move</summary>
public string Who { get; set; }
/// <summary>Move</summary>
public string Move { get; set; }
/// <summary>
/// Class Ctor
/// </summary>
/// <param name="step"> Move step</param>
/// <param name="who"> Who did the move</param>
/// <param name="move"> Move</param>
public MoveItem(string step, string who, string move) {
Step = step;
Who = who;
Move = move;
}
}
/// <summary>List of moves</summary>
public class MoveItemList : ObservableCollection<MoveItem> {}
/// <summary>
/// User interface displaying the list of moves
/// </summary>
public partial class MoveViewer : UserControl {
/// <summary>How the move are displayed: Move position (E2-E4) or PGN (e4)</summary>
public enum ViewerDisplayMode {
/// <summary>Display move using starting-ending position</summary>
MovePos,
/// <summary>Use PGN notation</summary>
Pgn
}
/// <summary>Argument for the NewMoveSelected event</summary>
public class NewMoveSelectedEventArg : System.ComponentModel.CancelEventArgs {
/// <summary>New selected index in the list</summary>
public int NewIndex { get; set; }
/// <summary>
/// Constructor
/// </summary>
/// <param name="newIndex"> New index</param>
public NewMoveSelectedEventArg(int newIndex) : base(false) => NewIndex = newIndex;
}
/// <summary>Called when a move has been selected by the control</summary>
public event EventHandler<NewMoveSelectedEventArg>? NewMoveSelected;
/// <summary>Chess board control associated with the move viewer</summary>
private ChessBoardControl? m_chessCtl;
/// <summary>Display Mode</summary>
private ViewerDisplayMode m_displayMode;
/// <summary>true to ignore change</summary>
private bool m_ignoreChg;
/// <summary>List of moves</summary>
public MoveItemList MoveList { get; }
/// <summary>
/// Class Ctor
/// </summary>
public MoveViewer() {
InitializeComponent();
m_displayMode = ViewerDisplayMode.MovePos;
m_ignoreChg = false;
MoveList = (MoveItemList)listViewMoveList.ItemsSource;
listViewMoveList.SelectionChanged += new SelectionChangedEventHandler(ListViewMoveList_SelectionChanged);
}
/// <summary>
/// Chess board control associated with move viewer
/// </summary>
public ChessBoardControl? ChessControl {
get => m_chessCtl;
set {
if (m_chessCtl != value) {
if (m_chessCtl != null) {
m_chessCtl.BoardReset -= ChessCtl_BoardReset;
m_chessCtl.NewMove -= ChessCtl_NewMove;
m_chessCtl.RedoPosChanged -= ChessCtl_RedoPosChanged;
}
m_chessCtl = value;
if (m_chessCtl != null) {
m_chessCtl.BoardReset += ChessCtl_BoardReset;
m_chessCtl.NewMove += ChessCtl_NewMove;
m_chessCtl.RedoPosChanged += ChessCtl_RedoPosChanged;
}
}
}
}
/// <summary>
/// Gets the description of a move
/// </summary>
/// <param name="move"> Move to describe</param>
/// <returns>
/// Move description
/// </returns>
private string GetMoveDesc(MoveExt move) {
string retVal;
if (m_displayMode == ViewerDisplayMode.MovePos) {
retVal = move.GetHumanPos();
} else {
retVal = PgnUtil.GetPgnMoveFromMove(m_chessCtl!.Board, move, includeEnding: false);
if ((move.Move.Type & Move.MoveType.MoveFromBook) == Move.MoveType.MoveFromBook) {
retVal = $"({retVal})";
}
}
return retVal;
}
/// <summary>
/// Redisplay all the moves using the current setting
/// </summary>
private void Redisplay() {
string[]? moveNames;
int moveCount;
MovePosStack movePosStack;
MoveExt move;
string moveTxt;
string moveIndex;
MoveItem moveItem;
ChessBoard chessBoard;
chessBoard = m_chessCtl!.Board;
if (chessBoard != null) {
movePosStack = chessBoard.MovePosStack;
moveCount = movePosStack.Count;
if (moveCount != 0) {
if (m_displayMode == ViewerDisplayMode.MovePos) {
moveNames = null;
} else {
moveNames = PgnUtil.GetPgnArrayFromMoveList(chessBoard);
}
for (int i = 0; i < moveCount; i++) {
move = movePosStack[i];
if (m_displayMode == ViewerDisplayMode.MovePos) {
moveTxt = move.GetHumanPos();
moveIndex = (i + 1).ToString();
} else {
moveTxt = moveNames![i];
moveIndex = (i / 2 + 1).ToString() + ((Char)('a' + (i & 1))).ToString();
}
moveItem = MoveList![i];
MoveList[i] = new MoveItem(moveIndex, moveItem.Who, moveTxt);
}
}
}
}
/// <summary>
/// Add the current move of the board
/// </summary>
private void AddCurrentMove() {
MoveItem moveItem;
string moveTxt;
string moveIndex;
int moveCount;
int itemCount;
int index;
MoveExt move;
ChessBoard.PlayerColor playerToMove;
ChessBoard chessBoard;
chessBoard = m_chessCtl!.Board;
m_ignoreChg = true;
move = chessBoard.MovePosStack.CurrentMove;
playerToMove = chessBoard.LastMovePlayer;
chessBoard.UndoMove();
moveCount = chessBoard.MovePosStack.Count;
itemCount = listViewMoveList.Items.Count;
while (itemCount >= moveCount) {
itemCount--;
MoveList.RemoveAt(itemCount);
}
moveTxt = GetMoveDesc(move);
chessBoard.RedoMove();
index = itemCount;
moveIndex = (m_displayMode == ViewerDisplayMode.MovePos) ? (index + 1).ToString() : (index / 2 + 1).ToString() + ((Char)('a' + (index & 1))).ToString();
moveItem = new MoveItem(moveIndex,
(playerToMove == ChessBoard.PlayerColor.Black) ? "Black" : "White",
moveTxt);
MoveList.Add(moveItem);
m_ignoreChg = false;
}
/// <summary>
/// Select the current move
/// </summary>
private void SelectCurrentMove() {
int index;
MoveItem moveItem;
ChessBoard chessBoard;
chessBoard = m_chessCtl!.Board;
m_ignoreChg = true;
index = chessBoard.MovePosStack.PositionInList;
if (index == -1) {
listViewMoveList.SelectedItem = null;
} else {
moveItem = (MoveItem)listViewMoveList.Items[index];
listViewMoveList.SelectedItem = moveItem;
listViewMoveList.ScrollIntoView(moveItem);
}
m_ignoreChg = false;
}
/// <summary>
/// Display Mode (Position or PGN)
/// </summary>
public ViewerDisplayMode DisplayMode {
get => m_displayMode;
set {
if (value != m_displayMode) {
m_displayMode = value;
Redisplay();
}
}
}
/// <summary>
/// Reset the control so it represents the specified chessboard
/// </summary>
private void Reset() {
int count;
ChessBoard chessBoard;
MoveList.Clear();
chessBoard = m_chessCtl!.Board;
count = chessBoard.MovePosStack.Count;
chessBoard.UndoAllMoves();
for (int i = 0; i < count; i++) {
chessBoard.RedoMove();
AddCurrentMove();
}
SelectCurrentMove();
}
/// <summary>
/// Trigger the NewMoveSelected argument
/// </summary>
/// <param name="e"> Event arguments</param>
protected void OnNewMoveSelected(NewMoveSelectedEventArg e) => NewMoveSelected?.Invoke(this, e);
/// <summary>
/// Triggered when the position has been changed
/// </summary>
/// <param name="sender"> Sender object</param>
/// <param name="e"> Event arguments</param>
private void ChessCtl_RedoPosChanged(object? sender, EventArgs e) => SelectCurrentMove();
/// <summary>
/// Triggered when a new move has been done
/// </summary>
/// <param name="sender"> Sender object</param>
/// <param name="e"> Event arguments</param>
private void ChessCtl_NewMove(object? sender, ChessBoardControl.NewMoveEventArgs e) {
AddCurrentMove();
SelectCurrentMove();
}
/// <summary>
/// Triggered when the board has been reset
/// </summary>
/// <param name="sender"> Sender object</param>
/// <param name="e"> Event arguments</param>
private void ChessCtl_BoardReset(object? sender, EventArgs e) => Reset();
/// <summary>
/// Called when the user select a move
/// </summary>
/// <param name="sender"> Sender object</param>
/// <param name="e"> Event arguments</param>
private void ListViewMoveList_SelectionChanged(object? sender, SelectionChangedEventArgs e) {
NewMoveSelectedEventArg evArg;
int curPos;
int newPos;
ChessBoard chessBoard;
if (!m_ignoreChg && !m_chessCtl!.IsBusy && !ChessBoardControl.IsSearchEngineBusy) {
m_ignoreChg = true;
chessBoard = m_chessCtl!.Board;
curPos = chessBoard.MovePosStack.PositionInList;
if (e.AddedItems.Count != 0) {
newPos = listViewMoveList.SelectedIndex;
if (newPos != curPos) {
evArg = new NewMoveSelectedEventArg(newPos);
OnNewMoveSelected(evArg);
if (evArg.Cancel) {
if (curPos == -1) {
listViewMoveList.SelectedItems.Clear();
} else {
listViewMoveList.SelectedIndex = curPos;
}
}
}
}
m_ignoreChg = false;
}
}
} // Class MoveViewer
} // Namespace