forked from OpenXcom/OpenXcom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveState.cpp
More file actions
232 lines (214 loc) · 6.78 KB
/
Copy pathSaveState.cpp
File metadata and controls
232 lines (214 loc) · 6.78 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
/*
* Copyright 2010-2013 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 "SaveState.h"
#include "../Engine/Logger.h"
#include "../Engine/CrossPlatform.h"
#include "../Savegame/SavedGame.h"
#include "../Engine/Game.h"
#include "../Engine/Action.h"
#include "../Engine/Exception.h"
#include "../Engine/Language.h"
#include "../Engine/Palette.h"
#include "../Engine/Options.h"
#include "../Interface/TextList.h"
#include "../Interface/TextEdit.h"
#include "ErrorMessageState.h"
#include "DeleteGameState.h"
namespace OpenXcom
{
/**
* Initializes all the elements in the Save Game screen.
* @param game Pointer to the core game.
* @param origin Game section that originated this state.
*/
SaveState::SaveState(Game *game, OptionsOrigin origin) : SavedGameState(game, origin), _selected(L""), _previousSelectedRow(-1), _selectedRow(-1)
{
// Create objects
_edtSave = new TextEdit(168, 9, 0, 0);
add(_edtSave);
// Set up objects
_txtTitle->setText(tr("STR_SELECT_SAVE_POSITION"));
_lstSaves->onMousePress((ActionHandler)&SaveState::lstSavesPress);
_edtSave->setColor(Palette::blockOffset(8)+10);
_edtSave->setVisible(false);
_edtSave->onKeyboardPress((ActionHandler)&SaveState::edtSaveKeyPress);
}
/**
* Creates the Quick Save Game state.
* @param game Pointer to the core game.
* @param origin Game section that originated this state.
* @param showMsg True if need to show messages like "Loading game" or "Saving game".
*/
SaveState::SaveState(Game *game, OptionsOrigin origin, bool showMsg) : SavedGameState(game, origin, showMsg)
{
quickSave(L"autosave");
}
/**
*
*/
SaveState::~SaveState()
{
}
/**
* Updates the save game list with a current list
* of available savegames.
*/
void SaveState::updateList()
{
_lstSaves->clearList();
_lstSaves->addRow(1, tr("STR_NEW_SAVED_GAME").c_str());
SavedGame::getList(_lstSaves, _game->getLanguage());
}
/**
* Names the selected save.
* @param action Pointer to an action.
*/
void SaveState::lstSavesPress(Action *action)
{
if (action->getDetails()->button.button == SDL_BUTTON_LEFT)
{
_previousSelectedRow = _selectedRow;
_selectedRow = _lstSaves->getSelectedRow();
switch (_previousSelectedRow)
{
case -1: // first click on the savegame list
break;
case 0:
_lstSaves->setCellText(_previousSelectedRow , 0, tr("STR_NEW_SAVED_GAME"));
break;
default:
_lstSaves->setCellText(_previousSelectedRow , 0, _selected);
}
_selected = _lstSaves->getCellText(_lstSaves->getSelectedRow(), 0);
_lstSaves->setCellText(_lstSaves->getSelectedRow(), 0, L"");
if (_lstSaves->getSelectedRow() == 0)
{
_edtSave->setText(L"");
_selected = L"";
}
else
{
_edtSave->setText(_selected);
}
_edtSave->setX(_lstSaves->getColumnX(0));
_edtSave->setY(_lstSaves->getRowY(_selectedRow));
_edtSave->setVisible(true);
_edtSave->focus();
_lstSaves->setScrolling(false);
}
else if (action->getDetails()->button.button == SDL_BUTTON_RIGHT && _lstSaves->getSelectedRow())
{
_game->pushState(new DeleteGameState(_game, _origin, _lstSaves->getCellText(_lstSaves->getSelectedRow(), 0), this));
}
}
/**
* Saves the selected save.
* @param action Pointer to an action.
*/
void SaveState::edtSaveKeyPress(Action *action)
{
if (action->getDetails()->key.keysym.sym == SDLK_RETURN ||
action->getDetails()->key.keysym.sym == SDLK_KP_ENTER)
{
updateStatus("STR_SAVING_GAME");
try
{
#ifdef _WIN32
std::string selected = Language::wstrToCp(_selected);
std::string filename = Language::wstrToCp(_edtSave->getText());
#else
std::string selected = Language::wstrToUtf8(_selected);
std::string filename = Language::wstrToUtf8(_edtSave->getText());
#endif
_game->getSavedGame()->save(filename);
std::string oldName = Options::getUserFolder() + selected + ".sav";
std::string newName = Options::getUserFolder() + filename + ".sav";
if (_selectedRow > 0 && oldName != newName)
{
if (!CrossPlatform::deleteFile(oldName))
{
throw Exception("Failed to overwrite save");
}
}
_game->popState();
_game->popState();
}
catch (Exception &e)
{
_edtSave->setVisible(false);
Log(LOG_ERROR) << e.what();
std::wstringstream error;
error << tr("STR_SAVE_UNSUCCESSFUL") << L'\x02' << Language::utf8ToWstr(e.what());
if (_origin != OPT_BATTLESCAPE)
_game->pushState(new ErrorMessageState(_game, error.str(), Palette::blockOffset(8)+10, "BACK01.SCR", 6));
else
_game->pushState(new ErrorMessageState(_game, error.str(), Palette::blockOffset(0), "TAC00.SCR", -1));
}
catch (YAML::Exception &e)
{
_edtSave->setVisible(false);
Log(LOG_ERROR) << e.what();
std::wstringstream error;
error <<
tr("STR_SAVE_UNSUCCESSFUL") << L'\x02' << Language::utf8ToWstr(e.what());
if (_origin != OPT_BATTLESCAPE)
_game->pushState(new ErrorMessageState(_game, error.str(), Palette::blockOffset(8)+10, "BACK01.SCR", 6));
else
_game->pushState(new ErrorMessageState(_game, error.str(), Palette::blockOffset(0), "TAC00.SCR", -1));
}
}
}
/**
* Quick save game.
* @param filename16 name of file without ".sav"
*/
void SaveState::quickSave(const std::wstring &filename16)
{
if (_showMsg) updateStatus("STR_SAVING_GAME");
#ifdef _WIN32
std::string filename = Language::wstrToCp(filename16);
#else
std::string filename = Language::wstrToUtf8(filename16);
#endif
try
{
_game->getSavedGame()->save(filename);
}
catch (Exception &e)
{
Log(LOG_ERROR) << e.what();
std::wstringstream error;
error << tr("STR_SAVE_UNSUCCESSFUL") << L'\x02' << Language::utf8ToWstr(e.what());
if (_origin != OPT_BATTLESCAPE)
_game->pushState(new ErrorMessageState(_game, error.str(), Palette::blockOffset(8)+10, "BACK01.SCR", 6));
else
_game->pushState(new ErrorMessageState(_game, error.str(), Palette::blockOffset(0), "TAC00.SCR", -1));
}
catch (YAML::Exception &e)
{
Log(LOG_ERROR) << e.what();
std::wstringstream error;
error << tr("STR_SAVE_UNSUCCESSFUL") << L'\x02' << Language::utf8ToWstr(e.what());
if (_origin != OPT_BATTLESCAPE)
_game->pushState(new ErrorMessageState(_game, error.str(), Palette::blockOffset(8)+10, "BACK01.SCR", 6));
else
_game->pushState(new ErrorMessageState(_game, error.str(), Palette::blockOffset(0), "TAC00.SCR", -1));
}
}
}