1 /*
2 * Copyright 2010-2014 OpenXcom Developers.
3 *
4 * This file is part of OpenXcom.
5 *
6 * OpenXcom is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * OpenXcom is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "ListGamesState.h"
20 #include <algorithm>
21 #include <utility>
22 #include "../Engine/Logger.h"
23 #include "../Savegame/SavedGame.h"
24 #include "../Engine/Game.h"
25 #include "../Engine/Action.h"
26 #include "../Engine/Exception.h"
27 #include "../Engine/Options.h"
28 #include "../Engine/CrossPlatform.h"
29 #include "../Engine/Screen.h"
30 #include "../Resource/ResourcePack.h"
31 #include "../Engine/Language.h"
32 #include "../Engine/Palette.h"
33 #include "../Interface/TextButton.h"
34 #include "../Interface/Window.h"
35 #include "../Interface/Text.h"
36 #include "../Interface/TextList.h"
37 #include "../Interface/ArrowButton.h"
38 #include "DeleteGameState.h"
39
40 namespace OpenXcom
41 {
42
43 struct compareSaveName : public std::binary_function<SaveInfo&, SaveInfo&, bool>
44 {
45 bool _reverse;
46
compareSaveNameOpenXcom::compareSaveName47 compareSaveName(bool reverse) : _reverse(reverse) {}
48
operator ()OpenXcom::compareSaveName49 bool operator()(const SaveInfo &a, const SaveInfo &b) const
50 {
51 if (a.reserved == b.reserved)
52 {
53 return CrossPlatform::naturalCompare(a.displayName, b.displayName);
54 }
55 else
56 {
57 return _reverse ? b.reserved : a.reserved;
58 }
59 }
60 };
61
62 struct compareSaveTimestamp : public std::binary_function<SaveInfo&, SaveInfo&, bool>
63 {
64 bool _reverse;
65
compareSaveTimestampOpenXcom::compareSaveTimestamp66 compareSaveTimestamp(bool reverse) : _reverse(reverse) {}
67
operator ()OpenXcom::compareSaveTimestamp68 bool operator()(const SaveInfo &a, const SaveInfo &b) const
69 {
70 if (a.reserved == b.reserved)
71 {
72 return a.timestamp < b.timestamp;
73 }
74 else
75 {
76 return _reverse ? b.reserved : a.reserved;
77 }
78 }
79 };
80
81 /**
82 * Initializes all the elements in the Saved Game screen.
83 * @param game Pointer to the core game.
84 * @param origin Game section that originated this state.
85 * @param firstValidRow First row containing saves.
86 * @param autoquick Show auto/quick saved games?
87 */
ListGamesState(Game * game,OptionsOrigin origin,int firstValidRow,bool autoquick)88 ListGamesState::ListGamesState(Game *game, OptionsOrigin origin, int firstValidRow, bool autoquick) : State(game), _origin(origin), _showMsg(true), _noUI(false), _firstValidRow(firstValidRow), _autoquick(autoquick), _sortable(true)
89 {
90 _screen = false;
91
92 // Create objects
93 _window = new Window(this, 320, 200, 0, 0, POPUP_BOTH);
94 _btnCancel = new TextButton(80, 16, 120, 172);
95 _txtTitle = new Text(310, 17, 5, 7);
96 _txtDelete = new Text(310, 9, 5, 23);
97 _txtName = new Text(150, 9, 16, 32);
98 _txtDate = new Text(110, 9, 204, 32);
99 _lstSaves = new TextList(288, 112, 8, 42);
100 _txtDetails = new Text(288, 16, 16, 156);
101 _sortName = new ArrowButton(ARROW_NONE, 11, 8, 16, 32);
102 _sortDate = new ArrowButton(ARROW_NONE, 11, 8, 204, 32);
103
104 // Set palette
105 if (_origin == OPT_BATTLESCAPE)
106 {
107 setPalette("PAL_BATTLESCAPE");
108 }
109 else
110 {
111 setPalette("PAL_GEOSCAPE", 6);
112 }
113
114 add(_window);
115 add(_btnCancel);
116 add(_txtTitle);
117 add(_txtDelete);
118 add(_txtName);
119 add(_txtDate);
120 add(_lstSaves);
121 add(_txtDetails);
122 add(_sortName);
123 add(_sortDate);
124
125 // Set up objects
126 _window->setColor(Palette::blockOffset(8)+5);
127 _window->setBackground(game->getResourcePack()->getSurface("BACK01.SCR"));
128
129 _btnCancel->setColor(Palette::blockOffset(8)+5);
130 _btnCancel->setText(tr("STR_CANCEL_UC"));
131 _btnCancel->onMouseClick((ActionHandler)&ListGamesState::btnCancelClick);
132 _btnCancel->onKeyboardPress((ActionHandler)&ListGamesState::btnCancelClick, Options::keyCancel);
133
134 _txtTitle->setColor(Palette::blockOffset(15)-1);
135 _txtTitle->setBig();
136 _txtTitle->setAlign(ALIGN_CENTER);
137
138 _txtDelete->setColor(Palette::blockOffset(15)-1);
139 _txtDelete->setAlign(ALIGN_CENTER);
140 _txtDelete->setText(tr("STR_RIGHT_CLICK_TO_DELETE"));
141
142 _txtName->setColor(Palette::blockOffset(15)-1);
143 _txtName->setText(tr("STR_NAME"));
144
145 _txtDate->setColor(Palette::blockOffset(15)-1);
146 _txtDate->setText(tr("STR_DATE"));
147
148 _lstSaves->setColor(Palette::blockOffset(8)+10);
149 _lstSaves->setArrowColor(Palette::blockOffset(8)+5);
150 _lstSaves->setColumns(3, 188, 60, 40);
151 _lstSaves->setSelectable(true);
152 _lstSaves->setBackground(_window);
153 _lstSaves->setMargin(8);
154 _lstSaves->onMouseOver((ActionHandler)&ListGamesState::lstSavesMouseOver);
155 _lstSaves->onMouseOut((ActionHandler)&ListGamesState::lstSavesMouseOut);
156 _lstSaves->onMousePress((ActionHandler)&ListGamesState::lstSavesPress);
157
158 _txtDetails->setColor(Palette::blockOffset(15)-1);
159 _txtDetails->setSecondaryColor(Palette::blockOffset(8)+10);
160 _txtDetails->setWordWrap(true);
161 _txtDetails->setText(tr("STR_DETAILS").arg(L""));
162
163 _sortName->setX(_sortName->getX() + _txtName->getTextWidth() + 5);
164 _sortName->setColor(Palette::blockOffset(15)-1);
165 _sortName->onMouseClick((ActionHandler)&ListGamesState::sortNameClick);
166
167 _sortDate->setX(_sortDate->getX() + _txtDate->getTextWidth() + 5);
168 _sortDate->setColor(Palette::blockOffset(15)-1);
169 _sortDate->onMouseClick((ActionHandler)&ListGamesState::sortDateClick);
170
171 updateArrows();
172 }
173
174 /**
175 *
176 */
~ListGamesState()177 ListGamesState::~ListGamesState()
178 {
179
180 }
181
182 /**
183 * Refreshes the saves list.
184 */
init()185 void ListGamesState::init()
186 {
187 State::init();
188
189 if (_origin == OPT_BATTLESCAPE)
190 {
191 applyBattlescapeTheme();
192 }
193
194 try
195 {
196 _saves = SavedGame::getList(_game->getLanguage(), _autoquick);
197 _lstSaves->clearList();
198 sortList(Options::saveOrder);
199 }
200 catch (Exception &e)
201 {
202 Log(LOG_ERROR) << e.what();
203 }
204 }
205
206 /**
207 * Updates the sorting arrows based
208 * on the current setting.
209 */
updateArrows()210 void ListGamesState::updateArrows()
211 {
212 _sortName->setShape(ARROW_NONE);
213 _sortDate->setShape(ARROW_NONE);
214 switch (Options::saveOrder)
215 {
216 case SORT_NAME_ASC:
217 _sortName->setShape(ARROW_SMALL_UP);
218 break;
219 case SORT_NAME_DESC:
220 _sortName->setShape(ARROW_SMALL_DOWN);
221 break;
222 case SORT_DATE_ASC:
223 _sortDate->setShape(ARROW_SMALL_UP);
224 break;
225 case SORT_DATE_DESC:
226 _sortDate->setShape(ARROW_SMALL_DOWN);
227 break;
228 }
229 }
230
231 /**
232 * Sorts the save game list.
233 * @param sort Order to sort the games in.
234 */
sortList(SaveSort sort)235 void ListGamesState::sortList(SaveSort sort)
236 {
237 switch (sort)
238 {
239 case SORT_NAME_ASC:
240 std::sort(_saves.begin(), _saves.end(), compareSaveName(false));
241 break;
242 case SORT_NAME_DESC:
243 std::sort(_saves.rbegin(), _saves.rend(), compareSaveName(true));
244 break;
245 case SORT_DATE_ASC:
246 std::sort(_saves.begin(), _saves.end(), compareSaveTimestamp(false));
247 break;
248 case SORT_DATE_DESC:
249 std::sort(_saves.rbegin(), _saves.rend(), compareSaveTimestamp(true));
250 break;
251 }
252 updateList();
253 }
254
255 /**
256 * Updates the save game list with the current list
257 * of available savegames.
258 */
updateList()259 void ListGamesState::updateList()
260 {
261 int row = 0;
262 for (std::vector<SaveInfo>::const_iterator i = _saves.begin(); i != _saves.end(); ++i)
263 {
264 _lstSaves->addRow(3, i->displayName.c_str(), i->isoDate.c_str(), i->isoTime.c_str());
265 if (i->reserved && _origin != OPT_BATTLESCAPE)
266 {
267 _lstSaves->setRowColor(row, Palette::blockOffset(8)+5);
268 }
269 row++;
270 }
271 }
272
273 /**
274 * Returns to the previous screen.
275 * @param action Pointer to an action.
276 */
btnCancelClick(Action *)277 void ListGamesState::btnCancelClick(Action *)
278 {
279 _game->popState();
280 }
281
282 /**
283 * Shows the details of the currently hovered save.
284 * @param action Pointer to an action.
285 */
lstSavesMouseOver(Action *)286 void ListGamesState::lstSavesMouseOver(Action *)
287 {
288 int sel = _lstSaves->getSelectedRow() - _firstValidRow;
289 std::wstring wstr;
290 if (sel >= 0 && sel < (int)_saves.size())
291 {
292 wstr = _saves[sel].details;
293 }
294 _txtDetails->setText(tr("STR_DETAILS").arg(wstr));
295 }
296
297 /**
298 * Clears the details.
299 * @param action Pointer to an action.
300 */
lstSavesMouseOut(Action *)301 void ListGamesState::lstSavesMouseOut(Action *)
302 {
303 _txtDetails->setText(tr("STR_DETAILS").arg(L""));
304 }
305
306 /**
307 * Deletes the selected save.
308 * @param action Pointer to an action.
309 */
lstSavesPress(Action * action)310 void ListGamesState::lstSavesPress(Action *action)
311 {
312 if (action->getDetails()->button.button == SDL_BUTTON_RIGHT && _lstSaves->getSelectedRow() >= _firstValidRow)
313 {
314 _game->pushState(new DeleteGameState(_game, _origin, _saves[_lstSaves->getSelectedRow() - _firstValidRow].fileName));
315 }
316 }
317
318 /**
319 * Sorts the saves by name.
320 * @param action Pointer to an action.
321 */
sortNameClick(Action *)322 void ListGamesState::sortNameClick(Action *)
323 {
324 if (_sortable)
325 {
326 if (Options::saveOrder == SORT_NAME_ASC)
327 {
328 Options::saveOrder = SORT_NAME_DESC;
329 }
330 else
331 {
332 Options::saveOrder = SORT_NAME_ASC;
333 }
334 updateArrows();
335 _lstSaves->clearList();
336 sortList(Options::saveOrder);
337 }
338 }
339
340 /**
341 * Sorts the saves by date.
342 * @param action Pointer to an action.
343 */
sortDateClick(Action *)344 void ListGamesState::sortDateClick(Action *)
345 {
346 if (_sortable)
347 {
348 if (Options::saveOrder == SORT_DATE_ASC)
349 {
350 Options::saveOrder = SORT_DATE_DESC;
351 }
352 else
353 {
354 Options::saveOrder = SORT_DATE_ASC;
355 }
356 updateArrows();
357 _lstSaves->clearList();
358 sortList(Options::saveOrder);
359 }
360 }
361
disableSort()362 void ListGamesState::disableSort()
363 {
364 _sortable = false;
365 }
366
367 }
368