-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlobalUISession.cpp
468 lines (439 loc) · 13.8 KB
/
GlobalUISession.cpp
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include "GlobalUISession.h"
#include "LocalChessGui.h"
#include "CommandOptionDefs.h"
#include "ChessPlayer_LocalHuman.h"
#include "ChessPlayer_LocalEngine.h"
#include "Settings.h"
#include "GlobalStrings.h"
#include "KeyMapper.h"
#include "GameSessionInfo.h"
#include "StringUtils.h"
#include <QFile>
#include <QTextStream>
#include <QTextCodec>
GlobalUISession::GlobalUISession() :
localHuman_(0), localEngine_(0), localHuman1_(0), localHuman2_(0),
gameSession_(0), keyRemapIdx_(-1), menuType_(menuNone)
{
initialize();
//
initialPosition_ = ChessPosition::fromString(toStdString(g_settings.initialPositionFen()));
initialPosition_.setChess960(g_settings.isChess960());
//
QObject::connect(&g_settings, SIGNAL(playerNameChanged()), this, SLOT(playerNameChanged()), Qt::UniqueConnection);
QObject::connect(&g_settings, SIGNAL(engineChanged()), this, SLOT(engineChanged()), Qt::UniqueConnection);
QObject::connect(&g_settings, SIGNAL(ponderingChanged()), this, SLOT(ponderingChanged()), Qt::UniqueConnection);
QObject::connect(&g_settings, SIGNAL(localeChanged()), this, SLOT(localeChanged()), Qt::UniqueConnection);
QObject::connect(&g_localChessGui, SIGNAL(keyPressed(Qt::Key,Qt::KeyboardModifiers)), this, SLOT(keyPressed(Qt::Key, Qt::KeyboardModifiers)), Qt::UniqueConnection);
QObject::connect(&g_localChessGui, SIGNAL(isExiting()), this, SLOT(isExiting()));
}
GlobalUISession::~GlobalUISession()
{
finalize();
}
void GlobalUISession::initialize()
{
localHuman_ = new ChessPlayer_LocalHuman(g_settings.playerName());
localHuman1_ = new ChessPlayer_LocalHuman("P1"); // @@todo: ask player names via UI before game starts
localHuman2_ = new ChessPlayer_LocalHuman("P2");
localEngine_ = new ChessPlayer_LocalEngine(g_settings.engineInfo(), g_settings.currentEngineProfile());
check960Support();
}
void GlobalUISession::begin()
{
QObject::connect(&beginDelayTimer_, SIGNAL(timeout()),
this, SLOT(asyncBegin()));
//
beginDelayTimer_.setSingleShot(true);
beginDelayTimer_.start(10); // any ridiculous amount will do
}
void GlobalUISession::asyncBegin()
{
QObject::disconnect(&beginDelayTimer_, SIGNAL(timeout()),
this, SLOT(asyncBegin()));
//
if(g_keyMapper.fileNotFound())
{
beginKeyRemapping();
}
else if(!restoreLastGame())
{
preGame(); // begin by offering a game
}
}
void GlobalUISession::preGame()
{
// offer user a choice of game types to play
showNewGameMenu();
}
void GlobalUISession::playGame(GameType type,
const ChessClock& wclock,
const ChessClock& bclock)
{
clearLastGame();
//
GameSessionInfo sessionInfo;
//
sessionInfo.profile.type = type;
sessionInfo.profile.whiteClock = wclock;
sessionInfo.profile.blackClock = bclock;
sessionInfo.initialPosition = initialPosition_;
//
playGame(sessionInfo);
}
void GlobalUISession::playGame(const GameSessionInfo& sessionInfo)
{
assert(gameSession_==0);
//
// reset gui
g_localChessGui.reset(initialPosition_);
//
switch(sessionInfo.profile.type)
{
case gameComputerBlack:
g_localChessGui.flipBoard(false);
gameSession_ = new GameSession(localHuman_, localEngine_, sessionInfo);
break;
case gameComputerWhite:
g_localChessGui.flipBoard(true);
gameSession_ = new GameSession(localEngine_, localHuman_, sessionInfo);
break;
case gameTwoPlayers:
g_localChessGui.flipBoard(false);
gameSession_ = new GameSession(localHuman1_, localHuman2_, sessionInfo);
break;
case gameTwoEngines:
assert(false); // @@note: not supported yet
break;
}
//
if(gameSession_)
{
QObject::connect(gameSession_, SIGNAL(end(GameSessionEndReason, ChessGameResult, const QString&)), this,
SLOT(gameSessionEnded(GameSessionEndReason, ChessGameResult, const QString&)), Qt::UniqueConnection);
gameSession_->begin();
}
}
void GlobalUISession::postGame()
{
QObject::disconnect(gameSession_, SIGNAL(end(GameSessionEndReason, ChessGameResult, const QString&)), this,
SLOT(gameSessionEnded(GameSessionEndReason, ChessGameResult, const QString&)));
delete gameSession_; gameSession_ = 0;
preGame();
}
void GlobalUISession::finalize()
{
delete gameSession_; gameSession_ = 0;
delete localHuman_; localHuman_ = 0;
delete localEngine_; localEngine_ = 0;
delete localHuman1_; localHuman1_ = 0;
delete localHuman2_; localHuman2_ = 0;
}
void GlobalUISession::offerChoice(const CommandOptions& options)
{
QObject::connect(&g_localChessGui, SIGNAL(userChoice(int)), this, SLOT(userChoice(int)), Qt::UniqueConnection);
g_localChessGui.offerChoice(options);
}
void GlobalUISession::userChoice(int id)
{
QObject::disconnect(&g_localChessGui, SIGNAL(userChoice(int)));
//
switch(id)
{
case cmd_NewGame_PlayerWhite:
playGame(gameComputerBlack, g_settings.playerClock(), g_settings.engineClock());
break;
case cmd_NewGame_PlayerBlack:
playGame(gameComputerWhite, g_settings.engineClock(), g_settings.playerClock());
break;
case cmd_NewGame_TwoPlayers:
playGame(gameTwoPlayers, g_settings.playerClock(), g_settings.playerClock());
break;
//
case cmd_ExtMenu_Settings:
g_localChessGui.showSettingsDialog();
showNewGameMenu();
break;
case cmd_ExtMenu_FEN:
{
ChessPosition position;
if(g_localChessGui.getInitialPosition(g_msg("InputInitialFEN"), position))
{
initialPosition_ = position;
}
showNewGameMenu();
}
break;
case cmd_ExtMenu_Quit:
requestExit();
break;
//
case cmd_PostGame_Save:
saveGameToPGN();
postGame();
break;
case cmd_PostGame_Discard:
postGame();
break;
}
}
void GlobalUISession::gameSessionEnded(GameSessionEndReason reason,
ChessGameResult result, const QString &message)
{
if(reason==reasonGameFinished)
{
clearLastGame();
// offer to save/discard/analyze game
if(g_settings.autoSaveGames())
{
saveGameToPGN();
postGame();
}
else
{
offerChoice(g_commandOptionDefs.postGameOptions());
}
}
else
{
clearLastGame();
postGame();
}
}
void GlobalUISession::saveGameToPGN()
{
assert(gameSession_);
//
QFile pgnFile(g_settings.pgnFilePath());
//
if(pgnFile.open(QIODevice::Append | QIODevice::WriteOnly))
{
QTextStream pgn(&pgnFile);
// use UTF-8 for pgn file, however, it may display
// gibberish in some chess programs which expect it to be ANSI
// this only affects metadata, but not the game data itself
// at least we don't lose any information, and the file can
// be converted to another code page manually, if needed
pgn.setCodec(QTextCodec::codecForName("UTF-8"));
pgn << gameSession_->game().toPGN();
}
//
pgnFile.close();
}
void GlobalUISession::engineChanged()
{
if(localEngine_==0) return;
if(gameSession_!=0)
{
assert(false); // attempt to change engine while playing: not supported
return;
}
if(g_settings.engineInfo().name==localEngine_->name()
&& g_settings.currentEngineProfile()==localEngine_->profileName()) return;
delete localEngine_; localEngine_ = 0;
localEngine_ = new ChessPlayer_LocalEngine(g_settings.engineInfo(), g_settings.currentEngineProfile());
check960Support();
}
void GlobalUISession::playerNameChanged()
{
if(localHuman_==0) return;
if(gameSession_!=0)
{
assert(false); // attempt to change player name while playing: not supported
return;
}
if(g_settings.playerName()==localHuman_->name()) return;
delete localHuman_; localHuman_= 0;
localHuman_ = new ChessPlayer_LocalHuman(g_settings.playerName());
}
void GlobalUISession::ponderingChanged()
{
if(localEngine_==0) return;
if(gameSession_!=0)
{
assert(false); // attempt to change pondering setting while playing: not supported
return;
}
localEngine_->ponderingChanged();
}
void GlobalUISession::localeChanged()
{
if(gameSession_!=0) return;
//
preGame();
}
void GlobalUISession::beginKeyRemapping()
{
keyRemapIdx_ = 0;
g_keyMapper.clearMapping();
g_localChessGui.enableDefaultKeyProcessing(false);
QObject::connect(&g_localChessGui, SIGNAL(keyPressed(Qt::Key, Qt::KeyboardModifiers)), this,
SLOT(keyPressed(Qt::Key, Qt::KeyboardModifiers)), Qt::UniqueConnection);
nextKeyRemapPrompt();
}
void GlobalUISession::keyPressed(Qt::Key key, Qt::KeyboardModifiers modifiers)
{
if(keyRemapIdx_>=0)
{
g_keyMapper.addMappedKey(key, g_keyMapper.keyDefs()[keyRemapIdx_].qtKey);
++keyRemapIdx_;
nextKeyRemapPrompt();
}
else
{
switch(key)
{
case Qt::Key_Cancel:
case Qt::Key_Escape:
case Qt::Key_Menu:
if(gameSession_==0)
{
if(menuType_==menuNewGame)
{
showExtMenu();
}
else if(menuType_==menuExt)
{
showNewGameMenu();
}
}
break;
case Qt::Key_Home:
if(gameSession_==0)
{
if(modifiers & Qt::ShiftModifier)
{
// turn off chess 960 mode if it is on
if(g_settings.isChess960())
{
g_settings.setChess960(false);
initialPosition_ = cStandardInitialPosition;
g_localChessGui.updatePosition(initialPosition_);
if(localEngine_) localEngine_->setChess960(false);
g_settings.setInitialPositionFen(cStandardInitialFen.c_str());
}
}
else if(localEngine_ && localEngine_->info().supports960())
{
// prepare a new initial 960 position if current engine supports it
g_settings.setChess960(true);
initialPosition_ = ChessPosition::new960Position();
g_localChessGui.updatePosition(initialPosition_);
g_settings.setInitialPositionFen(initialPosition_.toString().c_str());
localEngine_->setChess960(true);
}
}
break;
case Qt::Key_T:
g_settings.setShowGameClock(!g_settings.showGameClock());
if(gameSession_)
{
g_localChessGui.updateShowClock();
}
break;
case Qt::Key_U:
g_settings.setShowCapturedPieces(!g_settings.showCapturedPieces());
g_localChessGui.updateShowCaptured();
break;
case Qt::Key_N:
if(modifiers & Qt::AltModifier)
{
// change notation
g_settings.setUseRussianNotation(!g_settings.useRussianNotation());
if(gameSession_)
{
if(g_settings.useRussianNotation())
g_localChessGui.updateMoveList(gameSession_->game().ruMoves());
else
g_localChessGui.updateMoveList(gameSession_->game().sanMoves());
}
}
break;
default:
break;
}
}
}
void GlobalUISession::showNewGameMenu()
{
menuType_ = menuNewGame;
if(g_settings.engineInfo().name.isEmpty())
{
g_commandOptionDefs.newGameOptions().disable(cmd_NewGame_PlayerWhite);
g_commandOptionDefs.newGameOptions().disable(cmd_NewGame_PlayerBlack);
}
else
{
g_commandOptionDefs.newGameOptions().enable(cmd_NewGame_PlayerWhite);
g_commandOptionDefs.newGameOptions().enable(cmd_NewGame_PlayerBlack);
}
offerChoice(g_commandOptionDefs.newGameOptions());
}
void GlobalUISession::showExtMenu()
{
menuType_ = menuExt;
offerChoice(g_commandOptionDefs.extMenuOptions());
}
void GlobalUISession::nextKeyRemapPrompt()
{
if(keyRemapIdx_==(int)g_keyMapper.keyDefs().size())
{
keyRemapIdx_ = -1;
// end of key remapping sequence,
// proceed to preGame
g_keyMapper.saveKeyMapping();
g_localChessGui.enableDefaultKeyProcessing(true);
g_localChessGui.reset(initialPosition_);
preGame();
}
else
{
g_localChessGui.showStaticMessage(g_msg("PressKeyFor").arg(
g_msg(g_keyMapper.keyDefs()[keyRemapIdx_].keyName)));
}
}
bool GlobalUISession::restoreLastGame()
{
GameSessionInfo sessionInfo;
if(!sessionInfo.loadFromFile(g_settings.lastGameFile())) return false;
playGame(sessionInfo);
return true;
}
void GlobalUISession::clearLastGame()
{
QDir dir(extractFolderPath(g_settings.lastGameFile()));
dir.remove(g_settings.lastGameFile());
}
void GlobalUISession::requestExit()
{
finalize();
g_localChessGui.exitProgram();
}
void GlobalUISession::isExiting()
{
if(gameSession_ && gameSession_->game().result()==resultNone
&& !gameSession_->game().moves().empty())
{
// save game (will be restored and continued on next program start)
gameSession_->sessionInfo().saveToFile(g_settings.lastGameFile());
}
}
void GlobalUISession::check960Support()
{
if(!localEngine_) return;
if(!localEngine_->info().supports960())
{
if(g_settings.isChess960())
{
g_settings.setChess960(false);
initialPosition_ = cStandardInitialPosition;
g_settings.setInitialPositionFen(cStandardInitialFen.c_str());
g_localChessGui.updatePosition(initialPosition_);
}
}
//
if(g_settings.isChess960())
{
localEngine_->setChess960(true);
}
}