forked from OpenXcom/OpenXcom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModListState.cpp
More file actions
522 lines (459 loc) · 14.1 KB
/
Copy pathModListState.cpp
File metadata and controls
522 lines (459 loc) · 14.1 KB
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
* Copyright 2010-2016 OpenXcom Developers.
*
* This file is part of OpenXcom.
*
* OpenXcom 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.
*
* OpenXcom 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 OpenXcom. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ModListState.h"
#include "ModConfirmExtendedState.h"
#include <climits>
#include <algorithm>
#include "../Engine/Game.h"
#include "../Mod/Mod.h"
#include "../Engine/LocalizedText.h"
#include "../Interface/Window.h"
#include "../Interface/TextList.h"
#include "../Interface/Text.h"
#include "../Interface/TextButton.h"
#include "../Interface/ComboBox.h"
#include "../Engine/Options.h"
#include "../Engine/Action.h"
#include "StartState.h"
namespace OpenXcom
{
/**
* Initializes all the elements in the Mod Options window.
* @param game Pointer to the core game.
*/
ModListState::ModListState() : _curMasterIdx(0)
{
// Create objects
_window = new Window(this, 320, 200, 0, 0);
_txtMaster = new Text(305, 9, 8, 8);
_cbxMasters = new ComboBox(this, 305, 16, 8, 18);
_lstMods = new TextList(288, 104, 8, 40);
_btnOk = new TextButton(100, 16, 8, 176);
_btnCancel = new TextButton(100, 16, 212, 176);
_txtTooltip = new Text(305, 25, 8, 148);
// Set palette
setInterface("modsMenu");
add(_window, "window", "modsMenu");
add(_txtMaster, "text", "modsMenu");
add(_lstMods, "optionLists", "modsMenu");
add(_btnOk, "button2", "modsMenu");
add(_btnCancel, "button2", "modsMenu");
add(_txtTooltip, "tooltip", "modsMenu");
add(_cbxMasters, "button1", "modsMenu");
centerAllSurfaces();
// how much room do we need for YES/NO
Text text = Text(100, 9, 0, 0);
text.initText(_game->getMod()->getFont("FONT_BIG"), _game->getMod()->getFont("FONT_SMALL"), _game->getLanguage());
text.setText(tr("STR_YES"));
int yes = text.getTextWidth();
text.setText(tr("STR_NO"));
int no = text.getTextWidth();
int rightcol = std::max(yes, no) + 2;
int arrowCol = 25;
int leftcol = _lstMods->getWidth() - (rightcol + arrowCol);
// Set up objects
_window->setBackground(_game->getMod()->getSurface("BACK01.SCR"));
_txtMaster->setText(tr("STR_BASE_GAME"));
// scan for masters
Options::refreshMods();
const std::map<std::string, ModInfo> &modInfos(Options::getModInfos());
std::vector<std::string> masterNames;
for (std::vector< std::pair<std::string, bool> >::const_iterator i = Options::mods.begin(); i != Options::mods.end(); ++i)
{
std::string modId = i->first;
const ModInfo *modInfo = &modInfos.at(modId);
if (!modInfo->isMaster())
{
continue;
}
if (i->second)
{
_curMasterId = modId;
}
else if (_curMasterId.empty())
{
++_curMasterIdx;
}
_masters.push_back(modInfo);
masterNames.push_back(modInfo->getName());
}
_cbxMasters->setOptions(masterNames);
_cbxMasters->setSelected(_curMasterIdx);
_cbxMasters->onChange((ActionHandler)&ModListState::cbxMasterChange);
_cbxMasters->onMouseIn((ActionHandler)&ModListState::txtTooltipIn);
_cbxMasters->onMouseOut((ActionHandler)&ModListState::txtTooltipOut);
_cbxMasters->onMouseOver((ActionHandler)&ModListState::cbxMasterHover);
_cbxMasters->onListMouseIn((ActionHandler)&ModListState::txtTooltipIn);
_cbxMasters->onListMouseOut((ActionHandler)&ModListState::txtTooltipOut);
_cbxMasters->onListMouseOver((ActionHandler)&ModListState::cbxMasterHover);
_lstMods->setArrowColumn(leftcol + 1, ARROW_VERTICAL);
_lstMods->setColumns(3, leftcol, arrowCol, rightcol);
_lstMods->setAlign(ALIGN_RIGHT, 1);
_lstMods->setSelectable(true);
_lstMods->setBackground(_window);
_lstMods->setWordWrap(true);
_lstMods->onMouseClick((ActionHandler)&ModListState::lstModsClick);
_lstMods->onLeftArrowClick((ActionHandler)&ModListState::lstModsLeftArrowClick);
_lstMods->onRightArrowClick((ActionHandler)&ModListState::lstModsRightArrowClick);
_lstMods->onMousePress((ActionHandler)&ModListState::lstModsMousePress);
_lstMods->onMouseIn((ActionHandler)&ModListState::txtTooltipIn);
_lstMods->onMouseOut((ActionHandler)&ModListState::txtTooltipOut);
_lstMods->onMouseOver((ActionHandler)&ModListState::lstModsHover);
lstModsRefresh(0);
_btnOk->setText(tr("STR_OK"));
_btnOk->onMouseClick((ActionHandler)&ModListState::btnOkClick);
_btnOk->onKeyboardPress((ActionHandler)&ModListState::btnOkClick, Options::keyOk);
_btnCancel->setText(tr("STR_CANCEL"));
_btnCancel->onMouseClick((ActionHandler)&ModListState::btnCancelClick);
_btnCancel->onKeyboardPress((ActionHandler)&ModListState::btnCancelClick, Options::keyCancel);
_txtTooltip->setWordWrap(true);
}
ModListState::~ModListState()
{
}
std::string ModListState::makeTooltip(const ModInfo &modInfo)
{
return tr("STR_MODS_TOOLTIP").arg(modInfo.getVersion()).arg(modInfo.getAuthor()).arg(modInfo.getDescription());
}
void ModListState::cbxMasterHover(Action *)
{
_txtTooltip->setText(makeTooltip(*_masters[_cbxMasters->getHoveredListIdx()]));
}
void ModListState::cbxMasterChange(Action *)
{
const ModInfo *masterModInfo = _masters[_cbxMasters->getSelected()];
// when changing a master mod, check if it requires OXCE
{
if (!masterModInfo->isEngineOk())
{
_game->pushState(new ModConfirmExtendedState(this, masterModInfo));
return;
}
}
changeMasterMod();
}
void ModListState::changeMasterMod()
{
std::string masterId = _masters[_cbxMasters->getSelected()]->getId();
for (std::vector< std::pair<std::string, bool> >::iterator i = Options::mods.begin(); i != Options::mods.end(); ++i)
{
if (masterId == i->first)
{
i->second = true;
}
else if (_curMasterId == i->first)
{
i->second = false;
}
}
Options::reload = true;
_curMasterIdx = _cbxMasters->getSelected();
_curMasterId = masterId;
lstModsRefresh(0);
}
void ModListState::revertMasterMod()
{
_cbxMasters->setSelected(_curMasterIdx);
}
void ModListState::lstModsRefresh(size_t scrollLoc)
{
_lstMods->clearList();
_mods.clear();
// only show mods that work with the current master
for (std::vector< std::pair<std::string, bool> >::const_iterator i = Options::mods.begin(); i != Options::mods.end(); ++i)
{
ModInfo modInfo = Options::getModInfo(i->first);
if (modInfo.isMaster() || !modInfo.canActivate(_curMasterId))
{
continue;
}
std::string modName = modInfo.getName();
_lstMods->addRow(3, modName.c_str(), "", (i->second ? tr("STR_YES").c_str() : tr("STR_NO").c_str()));
_mods.push_back(*i);
}
_lstMods->scrollTo(scrollLoc);
}
void ModListState::lstModsHover(Action *)
{
size_t selectedRow = _lstMods->getSelectedRow();
if ((unsigned int)-1 != selectedRow)
{
_txtTooltip->setText(makeTooltip(Options::getModInfos().at(_mods[selectedRow].first)));
}
}
void ModListState::lstModsClick(Action *action)
{
if (action->getAbsoluteXMouse() >= _lstMods->getArrowsLeftEdge() &&
action->getAbsoluteXMouse() <= _lstMods->getArrowsRightEdge())
{
// don't count an arrow click as a mod enable toggle
return;
}
std::pair<std::string, bool> &mod(_mods.at(_lstMods->getSelectedRow()));
// when activating a mod, check if it requires OXCE
if (!mod.second)
{
const ModInfo *modInfo = &Options::getModInfos().at(mod.first);
if (!modInfo->isEngineOk())
{
_game->pushState(new ModConfirmExtendedState(this, modInfo));
return;
}
}
// if deactivating, or if not OXCE mod
toggleMod();
}
void ModListState::toggleMod()
{
std::pair<std::string, bool> &mod(_mods.at(_lstMods->getSelectedRow()));
for (std::vector< std::pair<std::string, bool> >::iterator i = Options::mods.begin(); i != Options::mods.end(); ++i)
{
if (mod.first != i->first)
{
continue;
}
mod.second = ! mod.second;
i->second = mod.second;
_lstMods->setCellText(_lstMods->getSelectedRow(), 2, (mod.second ? tr("STR_YES") : tr("STR_NO")));
break;
}
Options::reload = true;
}
void ModListState::lstModsLeftArrowClick(Action *action)
{
unsigned int row = _lstMods->getSelectedRow();
if (row <= 0)
{
return;
}
if (action->getDetails()->button.button == SDL_BUTTON_LEFT)
{
moveModUp(action, row);
}
else if (action->getDetails()->button.button == SDL_BUTTON_RIGHT)
{
moveModUp(action, row, true);
}
}
static void _moveAbove(const std::pair<std::string, bool> &srcMod, const std::pair<std::string, bool> &destMod)
{
// insert copy of srcMod above destMod
for (std::vector< std::pair<std::string, bool> >::iterator i = Options::mods.begin(); i != Options::mods.end(); ++i)
{
if (destMod.first == i->first)
{
Options::mods.insert(i, srcMod);
break;
}
}
// remove old copy of srcMod in separate loop since the insert above invalidated the iterator
for (std::vector< std::pair<std::string, bool> >::reverse_iterator i = Options::mods.rbegin(); i != Options::mods.rend(); ++i)
{
if (srcMod.first == i->first)
{
Options::mods.erase(i.base() - 1);
break;
}
}
}
void ModListState::moveModUp(Action *action, unsigned int row, bool max)
{
if (max)
{
_moveAbove(_mods.at(row), _mods.at(0));
// don't change the scroll position
lstModsRefresh(_lstMods->getScroll());
}
else
{
// calculate target scroll pos
int curScrollPos = _lstMods->getScroll();
int targetScrollPos = 0;
for (size_t i = 0; i < row - 1; ++i)
{
targetScrollPos += _lstMods->getNumTextLines(i);
}
if (curScrollPos < targetScrollPos)
{
int ydiff = _lstMods->getTextHeight(row - 1);
SDL_WarpMouse(action->getLeftBlackBand() + action->getXMouse(),
action->getTopBlackBand() + action->getYMouse() - static_cast<Uint16>(ydiff * action->getYScale()));
}
else
{
int ydiff = _lstMods->getRowY(row) - _lstMods->getY();
SDL_WarpMouse(action->getLeftBlackBand() + action->getXMouse(),
action->getTopBlackBand() + action->getYMouse() - static_cast<Uint16>(ydiff * action->getYScale()));
_lstMods->scrollTo(targetScrollPos);
}
_moveAbove(_mods.at(row), _mods.at(row - 1));
lstModsRefresh(_lstMods->getScroll());
}
Options::reload = true;
}
void ModListState::lstModsRightArrowClick(Action *action)
{
unsigned int row = _lstMods->getSelectedRow();
size_t numMods = _mods.size();
if (0 >= numMods || INT_MAX < numMods || row >= numMods - 1)
{
return;
}
if (action->getDetails()->button.button == SDL_BUTTON_LEFT)
{
moveModDown(action, row);
}
else if (action->getDetails()->button.button == SDL_BUTTON_RIGHT)
{
moveModDown(action, row, true);
}
}
static void _moveBelow(const std::pair<std::string, bool> &srcMod, const std::pair<std::string, bool> &destMod)
{
// insert copy of srcMod below destMod
for (std::vector< std::pair<std::string, bool> >::reverse_iterator i = Options::mods.rbegin(); i != Options::mods.rend(); ++i)
{
if (destMod.first == i->first)
{
Options::mods.insert(i.base(), srcMod);
break;
}
}
// remove old copy of srcMod in separate loop since the insert above invalidated the iterator
for (std::vector< std::pair<std::string, bool> >::iterator i = Options::mods.begin(); i != Options::mods.end(); ++i)
{
if (srcMod.first == i->first)
{
Options::mods.erase(i);
break;
}
}
}
void ModListState::moveModDown(Action *action, unsigned int row, bool max)
{
if (max)
{
_moveBelow(_mods.at(row), _mods.back());
// don't change the scroll position
lstModsRefresh(_lstMods->getScroll());
}
else
{
// calculate target scroll pos
int curScrollPos = _lstMods->getScroll();
int targetScrollPos = 0;
for (size_t i = 0; i <= row + 1; ++i)
{
if (i == row)
{
// don't count the current row -- it will be moved down
continue;
}
targetScrollPos += _lstMods->getNumTextLines(i);
}
if (curScrollPos + (int)_lstMods->getVisibleRows() > targetScrollPos)
{
int ydiff = _lstMods->getTextHeight(row + 1);
SDL_WarpMouse(action->getLeftBlackBand() + action->getXMouse(),
action->getTopBlackBand() + action->getYMouse() + static_cast<Uint16>(ydiff * action->getYScale()));
}
else
{
int ydiff = _lstMods->getY() + _lstMods->getHeight() - (_lstMods->getRowY(row) + _lstMods->getTextHeight(row));
SDL_WarpMouse(action->getLeftBlackBand() + action->getXMouse(),
action->getTopBlackBand() + action->getYMouse() + static_cast<Uint16>(ydiff * action->getYScale()));
_lstMods->scrollTo(targetScrollPos - _lstMods->getVisibleRows() + 1);
}
_moveBelow(_mods.at(row), _mods.at(row + 1));
lstModsRefresh(_lstMods->getScroll());
}
Options::reload = true;
}
void ModListState::lstModsMousePress(Action *action)
{
if (Options::changeValueByMouseWheel == 0)
return;
unsigned int row = _lstMods->getSelectedRow();
size_t numMods = _mods.size();
if (action->getDetails()->button.button == SDL_BUTTON_WHEELUP &&
row > 0)
{
if (action->getAbsoluteXMouse() >= _lstMods->getArrowsLeftEdge() &&
action->getAbsoluteXMouse() <= _lstMods->getArrowsRightEdge())
{
moveModUp(action, row);
}
}
else if (action->getDetails()->button.button == SDL_BUTTON_WHEELDOWN &&
0 < numMods && INT_MAX >= numMods && row < numMods - 1)
{
if (action->getAbsoluteXMouse() >= _lstMods->getArrowsLeftEdge() &&
action->getAbsoluteXMouse() <= _lstMods->getArrowsRightEdge())
{
moveModDown(action, row);
}
}
}
/**
* Restarts game with new mods.
* @param action Pointer to an action.
*/
void ModListState::btnOkClick(Action *)
{
Options::save();
if (Options::reload)
{
_game->setState(new StartState);
}
else
{
_game->popState();
}
}
/**
* Ignores mod changes and returns to the previous screen.
* @param action Pointer to an action.
*/
void ModListState::btnCancelClick(Action *)
{
Options::reload = false;
Options::load();
_game->popState();
}
/**
* Shows a tooltip for the appropriate button.
* @param action Pointer to an action.
*/
void ModListState::txtTooltipIn(Action *action)
{
_currentTooltip = action->getSender()->getTooltip();
_txtTooltip->setText(tr(_currentTooltip));
}
/**
* Clears the tooltip text.
* @param action Pointer to an action.
*/
void ModListState::txtTooltipOut(Action *action)
{
if (_currentTooltip == action->getSender()->getTooltip())
{
_txtTooltip->setText("");
}
}
}