-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtkmovelist.c
366 lines (314 loc) · 14.2 KB
/
gtkmovelist.c
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
* Copyright (C) 2005-2009 Jon Kinsey <jonkinsey@gmail.com>
* Copyright (C) 2006-2018 the AUTHORS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* $Id: gtkmovelist.c,v 1.44 2022/12/15 22:23:00 plm Exp $
*/
#include "config.h"
#include "gtklocdefs.h"
#include "gtkgame.h"
#include <string.h>
#include "format.h"
#include "gtkmovelistctrl.h"
#include "drawboard.h"
#define DETAIL_COLUMN_COUNT 11
#define MIN_COLUMN_COUNT 5
enum {
ML_COL_RANK = 0,
ML_COL_TYPE,
ML_COL_WIN,
ML_COL_GWIN,
ML_COL_BGWIN,
ML_COL_LOSS,
ML_COL_GLOSS,
ML_COL_BGLOSS,
ML_COL_EQUITY,
ML_COL_DIFF,
ML_COL_MOVE,
ML_COL_FGCOL,
ML_COL_DATA
};
extern void
MoveListCreate(hintdata * phd)
{
static const char *aszTitleDetails[] = {
N_("Rank"),
N_("analysisType|Type"),
N_("Win"),
N_("W g"),
N_("W bg"),
N_("Lose"),
N_("L g"),
N_("L bg"),
NULL,
N_("Diff."),
N_("Move")
};
unsigned int i;
int showWLTree = showMoveListDetail && !phd->fDetails;
/* Create list widget */
GtkListStore *store;
GtkTreeIter iter;
GtkTreeSelection *sel;
GtkWidget *view = gtk_tree_view_new();
int offset = (phd->fDetails) ? 0 : MIN_COLUMN_COUNT - DETAIL_COLUMN_COUNT;
if (showWLTree) {
GtkStyle *psDefault = gtk_widget_get_style(view);
GtkCellRenderer *renderer = custom_cell_renderer_movelist_new();
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), -1, _(aszTitleDetails[ML_COL_RANK]), renderer,
"movelist", 0, "rank", 1, NULL);
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE);
g_object_set(renderer, "cell-background-gdk", &psDefault->bg[GTK_STATE_NORMAL],
"cell-background-set", TRUE, NULL);
g_object_set_data(G_OBJECT(view), "hintdata", phd);
} else {
GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
g_object_set(renderer, "ypad", 0, NULL);
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), -1, _(aszTitleDetails[ML_COL_RANK]), renderer,
"text", ML_COL_RANK, "foreground", ML_COL_FGCOL + offset, NULL);
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), -1, Q_(aszTitleDetails[ML_COL_TYPE]), renderer,
"text", ML_COL_TYPE, "foreground", ML_COL_FGCOL + offset, NULL);
if (phd->fDetails) {
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), -1, _(aszTitleDetails[ML_COL_WIN]),
renderer, "text", ML_COL_WIN, "foreground",
ML_COL_FGCOL + offset, NULL);
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), -1, _(aszTitleDetails[ML_COL_GWIN]),
renderer, "text", ML_COL_GWIN, "foreground",
ML_COL_FGCOL + offset, NULL);
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), -1, _(aszTitleDetails[ML_COL_BGWIN]),
renderer, "text", ML_COL_BGWIN, "foreground",
ML_COL_FGCOL + offset, NULL);
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), -1, _(aszTitleDetails[ML_COL_LOSS]),
renderer, "text", ML_COL_LOSS, "foreground",
ML_COL_FGCOL + offset, NULL);
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), -1, _(aszTitleDetails[ML_COL_GLOSS]),
renderer, "text", ML_COL_GLOSS, "foreground",
ML_COL_FGCOL + offset, NULL);
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), -1, _(aszTitleDetails[ML_COL_BGLOSS]),
renderer, "text", ML_COL_BGLOSS, "foreground",
ML_COL_FGCOL + offset, NULL);
}
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), -1, aszTitleDetails[ML_COL_EQUITY],
renderer, "text", ML_COL_EQUITY + offset, "foreground",
ML_COL_FGCOL + offset, NULL);
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), -1, _(aszTitleDetails[ML_COL_DIFF]), renderer,
"text", ML_COL_DIFF + offset, "foreground", ML_COL_FGCOL + offset,
NULL);
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view), -1, Q_(aszTitleDetails[ML_COL_MOVE]), renderer,
"text", ML_COL_MOVE + offset, "foreground", ML_COL_FGCOL + offset,
NULL);
}
phd->pwMoves = view;
sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
gtk_tree_selection_set_mode(sel, GTK_SELECTION_MULTIPLE);
g_signal_connect(view, "row-activated", G_CALLBACK(HintDoubleClick), phd);
g_signal_connect(sel, "changed", G_CALLBACK(HintSelect), phd);
/* Add empty rows */
if (phd->fDetails)
store =
gtk_list_store_new(DETAIL_COLUMN_COUNT + 2, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
else {
if (showWLTree)
store = gtk_list_store_new(2, G_TYPE_POINTER, G_TYPE_INT);
else
store =
gtk_list_store_new(MIN_COLUMN_COUNT + 2, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
}
for (i = 0; i < phd->pml->cMoves; i++)
gtk_list_store_append(store, &iter);
gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store));
MoveListUpdate(phd);
}
float rBest;
GtkStyle *psHighlight = NULL;
extern void
MoveListRefreshSize(void)
{
custom_cell_renderer_invalidate_size();
if (pwMoveAnalysis) {
hintdata *phd = (hintdata *) g_object_get_data(G_OBJECT(pwMoveAnalysis), "user_data");
MoveListUpdate(phd);
}
}
/*
* Call UpdateMostList to update the movelist in the GTK hint window.
* For example, after new evaluations, rollouts or toggle of MWC/Equity.
*
*/
extern void
MoveListUpdate(const hintdata * phd)
{
unsigned int i, j, colNum;
char sz[32];
cubeinfo ci;
movelist *pml = phd->pml;
int col = phd->fDetails ? 8 : 2;
int showWLTree = showMoveListDetail && !phd->fDetails;
int offset = (phd->fDetails) ? 0 : MIN_COLUMN_COUNT - DETAIL_COLUMN_COUNT;
GtkTreeIter iter;
GtkListStore *store;
store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(phd->pwMoves)));
gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
if (!psHighlight) { /* Get highlight style first time in */
GtkStyle *psTemp;
GtkStyle *psMoves = gtk_widget_get_style(phd->pwMoves);
GetStyleFromRCFile(&psHighlight, "move-done", psMoves);
/* Use correct background colour when selected */
memcpy(&psHighlight->bg[GTK_STATE_SELECTED], &psMoves->bg[GTK_STATE_SELECTED], sizeof(GdkColor));
/* Also get colour to use for w/l stats in detail view */
GetStyleFromRCFile(&psTemp, "move-winlossfg", psMoves);
memcpy(&wlCol, &psTemp->fg[GTK_STATE_NORMAL], sizeof(GdkColor));
g_object_unref(psTemp);
}
/* This function should only be called when the game state matches
* the move list. */
g_assert(ms.fMove == 0 || ms.fMove == 1);
GetMatchStateCubeInfo(&ci, &ms);
rBest = pml->amMoves[0].rScore;
if (!showWLTree)
gtk_tree_view_column_set_title(gtk_tree_view_get_column(GTK_TREE_VIEW(phd->pwMoves), col),
(fOutputMWC && ms.nMatchTo) ? _("MWC") : _("Equity"));
for (i = 0; i < pml->cMoves; i++) {
float *ar = pml->amMoves[i].arEvalMove;
int rankKnown;
const char *highlight_sz;
if (showWLTree)
gtk_list_store_set(store, &iter, 0, pml->amMoves + i, -1);
else
gtk_list_store_set(store, &iter, ML_COL_DATA + offset, pml->amMoves + i, -1);
rankKnown = 1;
if (i && i == pml->cMoves - 1 && phd->piHighlight && i == *phd->piHighlight)
/* The move made is the last on the list. Some moves might
* have been deleted to fit this one in */
{
/* Lets count how many moves are possible to see if this is the last move */
movelist ml;
int dice[2];
memcpy(dice, ms.anDice, sizeof(dice));
if (!dice[0]) { /* If the dice have got lost, try to find them */
moverecord *pmr = (moverecord *) plLastMove->plNext->p;
if (pmr) {
dice[0] = pmr->anDice[0];
dice[1] = pmr->anDice[1];
}
}
GenerateMoves(&ml, msBoard(), dice[0], dice[1], FALSE);
if (i < ml.cMoves - 1)
rankKnown = 0;
}
highlight_sz = (phd->piHighlight && *phd->piHighlight == i) ? "*" : "";
if (rankKnown)
sprintf(sz, "%s%s%u", pml->amMoves[i].cmark ? "+" : "", highlight_sz, i + 1);
else
sprintf(sz, "%s%s??", pml->amMoves[i].cmark ? "+" : "", highlight_sz);
if (showWLTree) {
gtk_list_store_set(store, &iter, 1, rankKnown ? (int) i + 1 : -1, -1);
goto skipoldcode;
} else
gtk_list_store_set(store, &iter, ML_COL_RANK, sz, -1);
FormatEval(sz, &pml->amMoves[i].esMove);
gtk_list_store_set(store, &iter, ML_COL_TYPE, sz, -1);
/* gwc */
if (phd->fDetails) {
colNum = ML_COL_WIN;
for (j = 0; j < 5; j++) {
if (j == 3) {
gtk_list_store_set(store, &iter, colNum, OutputPercent(1.0f - ar[OUTPUT_WIN]), -1);
colNum++;
}
gtk_list_store_set(store, &iter, colNum, OutputPercent(ar[j]), -1);
colNum++;
}
}
/* cubeless equity */
gtk_list_store_set(store, &iter, ML_COL_EQUITY + offset, OutputEquity(pml->amMoves[i].rScore, &ci, TRUE), -1);
if (i != 0) {
gtk_list_store_set(store, &iter, ML_COL_DIFF + offset,
OutputEquityDiff(pml->amMoves[i].rScore, rBest, &ci), -1);
}
gtk_list_store_set(store, &iter, ML_COL_MOVE + offset, FormatMove(sz, msBoard(), pml->amMoves[i].anMove), -1);
/* highlight row */
if (phd->piHighlight && *phd->piHighlight == i) {
char buf[20];
sprintf(buf, "#%02x%02x%02x", psHighlight->fg[GTK_STATE_SELECTED].red / 256,
psHighlight->fg[GTK_STATE_SELECTED].green / 256, psHighlight->fg[GTK_STATE_SELECTED].blue / 256);
gtk_list_store_set(store, &iter, ML_COL_FGCOL + offset, buf, -1);
} else
gtk_list_store_set(store, &iter, ML_COL_FGCOL + offset, NULL, -1);
skipoldcode: /* Messy as 3 copies of code at moment... */
gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
}
}
extern GList *
MoveListGetSelectionList(const hintdata * phd)
{
GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(phd->pwMoves));
GtkTreeSelection *sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(phd->pwMoves));
return gtk_tree_selection_get_selected_rows(sel, &model);
}
/* gtk_tree_path_free() is not the right function type
* to be called directly from g_list_foreach() below
*/
static inline void
my_gtk_tree_path_free(gpointer data, gpointer UNUSED(user_data))
{
gtk_tree_path_free((GtkTreePath *) data);
}
extern void
MoveListFreeSelectionList(GList * pl)
{
g_list_foreach(pl, my_gtk_tree_path_free, NULL);
g_list_free(pl);
}
extern move *
MoveListGetMove(const hintdata * phd, GList * pl)
{
move *m;
int showWLTree = showMoveListDetail && !phd->fDetails;
int col, offset = (phd->fDetails) ? 0 : MIN_COLUMN_COUNT - DETAIL_COLUMN_COUNT;
GtkTreeIter iter;
GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(phd->pwMoves));
gboolean check = gtk_tree_model_get_iter(model, &iter, (GtkTreePath *) (pl->data));
g_assert(check == TRUE);
#if defined(G_DISABLE_ASSERT)
(void)check; /* silence warning about unused variable */
#endif
if (showWLTree)
col = 0;
else
col = ML_COL_DATA + offset;
gtk_tree_model_get(model, &iter, col, &m, -1);
return m;
}
extern void
MoveListShowToggledClicked(GtkWidget * UNUSED(pw), hintdata * phd)
{
int f = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(phd->pwShow));
if (f)
gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(phd->pwMoves)), GTK_SELECTION_SINGLE);
else
gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(phd->pwMoves)), GTK_SELECTION_MULTIPLE);
ShowMove(phd, f);
}
extern gint
MoveListClearSelection(GtkWidget * UNUSED(pw), GdkEventSelection * UNUSED(pes), hintdata * phd)
{
gtk_tree_selection_unselect_all(gtk_tree_view_get_selection(GTK_TREE_VIEW(phd->pwMoves)));
return TRUE;
}