-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathSaveLoadMenuGUI.cpp
More file actions
389 lines (318 loc) · 14.3 KB
/
Copy pathSaveLoadMenuGUI.cpp
File metadata and controls
389 lines (318 loc) · 14.3 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
#include "SaveLoadMenuGUI.h"
#include "ActivityMan.h"
#include "PresetMan.h"
#include "WindowMan.h"
#include "PauseMenuGUI.h"
#include "SettingsGUI.h"
#include "ModManagerGUI.h"
#include "GUI.h"
#include "AllegroScreen.h"
#include "GAScripted.h"
#include "GUIInputWrapper.h"
#include "GUICollectionBox.h"
#include "GUILabel.h"
#include "GUIButton.h"
#include "GUIListBox.h"
#include "GUITextBox.h"
#include "GUIComboBox.h"
#include <execution>
#ifdef SYSTEM_MINIZIP
#include <minizip/zip.h>
#include <minizip/unzip.h>
#else
#include "zip.h"
#include "unzip.h"
#endif
using namespace RTE;
SaveLoadMenuGUI::SaveLoadMenuGUI(AllegroScreen* guiScreen, GUIInputWrapper* guiInput, bool createForPauseMenu) {
m_GUIControlManager = std::make_unique<GUIControlManager>();
RTEAssert(m_GUIControlManager->Create(guiScreen, guiInput, "Base.rte/GUIs/Skins/Menus", "MainMenuSubMenuSkin.ini"), "Failed to create GUI Control Manager and load it from Base.rte/GUIs/Skins/Menus/MainMenuSubMenuSkin.ini");
m_GUIControlManager->Load("Base.rte/GUIs/SaveLoadMenuGUI.ini");
int rootBoxMaxWidth = g_WindowMan.FullyCoversAllDisplays() ? g_WindowMan.GetPrimaryWindowDisplayWidth() / g_WindowMan.GetResMultiplier() : g_WindowMan.GetResX();
GUICollectionBox* rootBox = dynamic_cast<GUICollectionBox*>(m_GUIControlManager->GetControl("root"));
rootBox->Resize(rootBoxMaxWidth, g_WindowMan.GetResY());
m_SaveGameMenuBox = dynamic_cast<GUICollectionBox*>(m_GUIControlManager->GetControl("CollectionBoxSaveGameMenu"));
m_SaveGameMenuBox->CenterInParent(true, true);
m_SaveGameMenuBox->SetPositionAbs(m_SaveGameMenuBox->GetXPos(), (rootBox->GetHeight() < 540) ? m_SaveGameMenuBox->GetYPos() - 15 : 140);
m_OrderByComboBox = dynamic_cast<GUIComboBox*>(m_GUIControlManager->GetControl("ComboOrderBy"));
m_OrderByComboBox->AddItem("Name");
m_OrderByComboBox->AddItem("Date");
m_OrderByComboBox->AddItem("Activity");
m_OrderByComboBox->SetSelectedIndex(1); // order by Date by default
m_BackToMainButton = dynamic_cast<GUIButton*>(m_GUIControlManager->GetControl("ButtonBackToMainMenu"));
if (createForPauseMenu) {
m_BackToMainButton->SetSize(120, 20);
m_BackToMainButton->SetText("Back to Pause Menu");
}
m_BackToMainButton->SetPositionAbs((rootBox->GetWidth() - m_BackToMainButton->GetWidth()) / 2, m_SaveGameMenuBox->GetYPos() + m_SaveGameMenuBox->GetHeight() + 10);
m_SaveGamesListBox = dynamic_cast<GUIListBox*>(m_GUIControlManager->GetControl("ListBoxSaveGames"));
m_SaveGamesListBox->SetFont(m_GUIControlManager->GetSkin()->GetFont("FontConsoleMonospace.png"));
m_SaveGamesListBox->SetMouseScrolling(true);
m_SaveGamesListBox->SetScrollBarThickness(15);
m_SaveGamesListBox->SetScrollBarPadding(2);
m_SaveGamesListBox->SetHighlightAsIfAlwaysFocused(true);
m_SaveGameName = dynamic_cast<GUITextBox*>(m_GUIControlManager->GetControl("SaveGameName"));
m_LoadButton = dynamic_cast<GUIButton*>(m_GUIControlManager->GetControl("ButtonLoad"));
m_CreateButton = dynamic_cast<GUIButton*>(m_GUIControlManager->GetControl("ButtonCreate"));
m_OverwriteButton = dynamic_cast<GUIButton*>(m_GUIControlManager->GetControl("ButtonOverwrite"));
m_DeleteButton = dynamic_cast<GUIButton*>(m_GUIControlManager->GetControl("ButtonDelete"));
m_DescriptionLabel = dynamic_cast<GUILabel*>(m_GUIControlManager->GetControl("DescriptionLabel"));
m_ConfirmationBox = dynamic_cast<GUICollectionBox*>(m_GUIControlManager->GetControl("ConfirmDialog"));
m_ConfirmationBox->CenterInParent(true, true);
m_ConfirmationLabel = dynamic_cast<GUILabel*>(m_GUIControlManager->GetControl("ConfirmLabel"));
m_ConfirmationButton = dynamic_cast<GUIButton*>(m_GUIControlManager->GetControl("ConfirmButton"));
m_CancelButton = dynamic_cast<GUIButton*>(m_GUIControlManager->GetControl("CancelButton"));
m_SaveGamesFetched = false;
m_WasSaving = false;
m_SavingBlinkTimer.SetRealTimeLimitS(1.5f);
SwitchToConfirmDialogMode(ConfirmDialogMode::None);
}
void SaveLoadMenuGUI::PopulateSaveGamesList() {
if (g_ActivityMan.IsCurrentlySaving() || m_SaveGamesFetched) {
return;
}
m_SaveGames.clear();
m_GUIControlManager->GetManager()->SetFocus(nullptr);
std::string saveFilePath = g_PresetMan.GetFullModulePath(c_UserScriptedSavesModuleName) + "/";
for (const auto& entry: std::filesystem::directory_iterator(saveFilePath)) {
if (entry.path().extension() == ".ccsave") {
SaveRecord record;
record.SavePath = entry.path();
record.SaveDate = entry.last_write_time();
m_SaveGames.push_back(record);
}
}
std::for_each(std::execution::par_unseq,
m_SaveGames.begin(), m_SaveGames.end(),
[](SaveRecord& record) {
// load zip sav file
std::string filePath = record.SavePath.string();
unzFile zippedSaveFile = unzOpen(filePath.c_str());
if (!zippedSaveFile) {
return;
}
// These need to use NULL instead of nullptr to compile on Linux/OSX?
if (unzLocateFile(zippedSaveFile, "Index.ini", NULL) == UNZ_END_OF_LIST_OF_FILE) {
unzClose(zippedSaveFile);
return;
}
unz_file_info info;
unzOpenCurrentFile(zippedSaveFile);
unzGetCurrentFileInfo(zippedSaveFile, &info, nullptr, 0, nullptr, 0, nullptr, 0);
char* buffer = (char*)malloc(info.uncompressed_size + 1);
if (!buffer) {
// If this ever hits I've lost all faith in modern OSes, but alas when one is writing C, one must dance along
RTEError::ShowMessageBox("Catastrophic failure! Failed to allocate memory for savegame");
unzClose(zippedSaveFile);
return;
}
unzReadCurrentFile(zippedSaveFile, buffer, info.uncompressed_size);
unzCloseCurrentFile(zippedSaveFile);
buffer[info.uncompressed_size] = 0; // need to null-terminate manually
Reader reader(std::make_unique<std::istringstream>(buffer), record.SavePath.string(), true, nullptr, false);
while (reader.NextProperty()) {
std::string propName = reader.ReadPropName();
if (propName == "ActivityName") {
reader >> record.Activity;
} else if (propName == "OriginalScenePresetName") {
reader >> record.Scene;
}
}
unzClose(zippedSaveFile);
free(buffer);
});
UpdateSaveGamesGUIList();
m_SaveGamesFetched = true;
}
void SaveLoadMenuGUI::UpdateSaveGamesGUIList() {
const std::string& currentOrder = m_OrderByComboBox->GetSelectedItem()->m_Name;
if (currentOrder == "Name") {
std::stable_sort(m_SaveGames.begin(), m_SaveGames.end(), [](const SaveRecord& lhs, const SaveRecord& rhs) { return lhs.SavePath.stem().string() < rhs.SavePath.stem().string(); });
} else if (currentOrder == "Date") {
std::stable_sort(m_SaveGames.begin(), m_SaveGames.end(), [](const SaveRecord& lhs, const SaveRecord& rhs) { return lhs.SaveDate > rhs.SaveDate; });
} else if (currentOrder == "Activity") {
std::stable_sort(m_SaveGames.begin(), m_SaveGames.end(), [](const SaveRecord& lhs, const SaveRecord& rhs) { return lhs.Activity < rhs.Activity; });
}
m_SaveGamesListBox->ClearList();
for (int i = 0; i < m_SaveGames.size(); i++) {
const SaveRecord& save = m_SaveGames[i];
std::stringstream saveNameText;
saveNameText << std::left << std::setfill(' ') << std::setw(32) << save.SavePath.stem().string();
// This is so much more fucking difficult than it has any right to be
#if defined(_MSC_VER) || __GNUC__ > 12
const auto saveFsTime = std::chrono::clock_cast<std::chrono::system_clock>(save.SaveDate);
const auto saveTime = std::chrono::system_clock::to_time_t(saveFsTime);
#else
// TODO - kill this monstrosity when we move to GCC13
// libc++ file_clock rep is wider than system_clock; duration_cast lands it in range first.
#if defined(_LIBCPP_VERSION)
auto saveFsTime = std::chrono::system_clock::time_point(
std::chrono::duration_cast<std::chrono::system_clock::duration>(save.SaveDate.time_since_epoch()));
#else
auto saveFsTime = std::chrono::system_clock::time_point(save.SaveDate.time_since_epoch());
#endif
#ifdef _WIN32
// Windows epoch time are the number of seconds since... 1601-01-01 00:00:00. Seriously.
saveFsTime -= std::chrono::seconds(11644473600LL);
#elif defined(__GLIBCXX__)
// libstdc++ file_clock epoch is 2174-01-01 00:00:00 for reasons
saveFsTime += std::chrono::seconds(6437664000LL);
#endif
const auto saveTime = std::chrono::system_clock::to_time_t(saveFsTime);
#endif
const auto saveTimeLocal = std::localtime(&saveTime);
std::stringstream saveDateTimeText;
saveDateTimeText << std::put_time(saveTimeLocal, "%Y-%m-%d %X");
m_SaveGamesListBox->AddItem(" " + saveNameText.str() + "" + save.Scene + " - " + save.Activity + "", saveDateTimeText.str() + " ", nullptr, nullptr, i);
}
m_SaveGamesListBox->ScrollToTop();
}
bool SaveLoadMenuGUI::LoadSave() {
bool success = g_ActivityMan.LoadAndLaunchGame(m_SaveGameName->GetText());
if (success) {
g_GUISound.ConfirmSound()->Play();
} else {
g_GUISound.UserErrorSound()->Play();
}
return success;
}
void SaveLoadMenuGUI::CreateSave() {
bool success = g_ActivityMan.SaveCurrentGame(m_SaveGameName->GetText());
if (success) {
g_GUISound.ConfirmSound()->Play();
} else {
g_GUISound.UserErrorSound()->Play();
}
m_SaveGamesFetched = false;
}
void SaveLoadMenuGUI::DeleteSave() {
std::string saveFilePath = g_PresetMan.GetFullModulePath(c_UserScriptedSavesModuleName) + "/" + m_SaveGameName->GetText() + ".ccsave";
std::filesystem::remove(saveFilePath);
g_GUISound.ConfirmSound()->Play();
m_SaveGamesFetched = false;
}
void SaveLoadMenuGUI::UpdateButtonEnabledStates() {
bool allowSave = g_ActivityMan.GetActivity() && g_ActivityMan.GetActivity()->GetAllowsUserSaving() && m_SaveGameName->GetText() != "";
int existingSaveItemIndex = -1;
for (int i = 0; i < m_SaveGamesListBox->GetItemList()->size(); ++i) {
SaveRecord& save = m_SaveGames[m_SaveGamesListBox->GetItem(i)->m_ExtraIndex];
if (save.SavePath.stem().string() == m_SaveGameName->GetText()) {
existingSaveItemIndex = i;
break;
}
}
// Select the item in the list - selecting -1 unselects all
m_SaveGamesListBox->SetSelectedIndex(existingSaveItemIndex);
bool saveExists = existingSaveItemIndex != -1;
bool allowCreate = allowSave && !saveExists;
m_CreateButton->SetVisible(allowCreate);
m_CreateButton->SetEnabled(allowCreate);
bool allowOverwrite = allowSave && saveExists;
m_OverwriteButton->SetVisible(allowOverwrite);
m_OverwriteButton->SetEnabled(allowOverwrite);
m_LoadButton->SetEnabled(saveExists);
m_DeleteButton->SetEnabled(saveExists);
m_DescriptionLabel->SetText("");
bool isSaving = g_ActivityMan.IsCurrentlySaving();
if (isSaving != m_WasSaving) {
m_SavingBlinkTimer.Reset();
}
if (g_ActivityMan.GetActivity()) {
if (isSaving) {
const char* saveText = "";
switch (m_SavingBlinkTimer.StepReal(500, 4)) {
case 0:
saveText = "Saving game, please wait ";
break;
case 1:
saveText = "Saving game, please wait. ";
break;
case 2:
saveText = "Saving game, please wait.. ";
break;
case 3:
saveText = "Saving game, please wait...";
break;
}
m_DescriptionLabel->SetText(saveText);
} else if (!m_SavingBlinkTimer.IsPastRealTimeLimit()) {
// Show "Saved!" for a little while after saving
m_DescriptionLabel->SetText("Game saved successfully!");
} else if (!g_ActivityMan.GetActivity()->GetAllowsUserSaving()) {
m_DescriptionLabel->SetText("The currently played activity does not allow saving.");
} else if (m_SaveGameName->GetText().empty()) {
m_DescriptionLabel->SetText("Enter a name for your savegame.");
}
}
m_WasSaving = isSaving;
}
void SaveLoadMenuGUI::SwitchToConfirmDialogMode(ConfirmDialogMode mode) {
m_ConfirmDialogMode = mode;
bool dialogOpen = m_ConfirmDialogMode != ConfirmDialogMode::None;
m_SaveGameMenuBox->SetEnabled(!dialogOpen);
m_ConfirmationBox->SetEnabled(dialogOpen);
m_ConfirmationBox->SetVisible(dialogOpen);
switch (m_ConfirmDialogMode) {
case ConfirmDialogMode::ConfirmOverwrite:
m_ConfirmationLabel->SetText("Are you sure you want to overwrite this savegame?");
break;
case ConfirmDialogMode::ConfirmDelete:
m_ConfirmationLabel->SetText("Are you sure you want to delete this savegame?");
break;
}
}
bool SaveLoadMenuGUI::HandleInputEvents(PauseMenuGUI* pauseMenu) {
PopulateSaveGamesList();
m_GUIControlManager->Update();
GUIEvent guiEvent;
while (m_GUIControlManager->GetEvent(&guiEvent)) {
if (guiEvent.GetType() == GUIEvent::Command) {
if (guiEvent.GetControl() == m_BackToMainButton) {
return true;
} else if (guiEvent.GetControl() == m_LoadButton) {
bool gameLoaded = LoadSave();
if (gameLoaded) {
return true;
}
} else if (guiEvent.GetControl() == m_CreateButton) {
CreateSave();
} else if (guiEvent.GetControl() == m_OverwriteButton) {
SwitchToConfirmDialogMode(ConfirmDialogMode::ConfirmOverwrite);
} else if (guiEvent.GetControl() == m_DeleteButton) {
SwitchToConfirmDialogMode(ConfirmDialogMode::ConfirmDelete);
} else if (guiEvent.GetControl() == m_ConfirmationButton) {
switch (m_ConfirmDialogMode) {
case ConfirmDialogMode::ConfirmOverwrite:
CreateSave();
break;
case ConfirmDialogMode::ConfirmDelete:
DeleteSave();
break;
}
SwitchToConfirmDialogMode(ConfirmDialogMode::None);
} else if (guiEvent.GetControl() == m_CancelButton) {
SwitchToConfirmDialogMode(ConfirmDialogMode::None);
}
} else if (guiEvent.GetType() == GUIEvent::Notification) {
if (guiEvent.GetMsg() == GUIButton::Focused && dynamic_cast<GUIButton*>(guiEvent.GetControl())) {
g_GUISound.SelectionChangeSound()->Play();
}
if (guiEvent.GetControl() == m_SaveGamesListBox && (guiEvent.GetMsg() == GUIListBox::Select && m_SaveGamesListBox->GetSelectedIndex() > -1)) {
const SaveRecord& record = m_SaveGames[m_SaveGamesListBox->GetSelected()->m_ExtraIndex];
m_SaveGameName->SetText(record.SavePath.stem().string());
}
if (guiEvent.GetControl() == m_OrderByComboBox && guiEvent.GetMsg() == GUIComboBox::Closed) {
UpdateSaveGamesGUIList();
}
}
}
UpdateButtonEnabledStates();
return false;
}
void SaveLoadMenuGUI::Refresh() {
m_SaveGamesFetched = false;
UpdateButtonEnabledStates();
}
void SaveLoadMenuGUI::Draw() const {
m_GUIControlManager->Draw();
}