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
|
/*
* Copyright (C) 2002-2024 by the Widelands Development Team
*
* This program 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 2
* of the License, or (at your option) any later version.
*
* This program 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 this program; if not, see <https://www.gnu.org/licenses/>.
*
*/
#include "wui/load_or_save_game.h"
#include <memory>
#include "build_info.h"
#include "graphic/mouse_cursor.h"
#include "logic/filesystem_constants.h"
#include "ui_basic/messagebox.h"
LoadOrSaveGame::LoadOrSaveGame(UI::Panel* parent,
Widelands::Game& g,
FileType filetype,
UI::PanelStyle style,
UI::WindowStyle ws,
bool localize_autosave,
UI::Panel* table_parent,
UI::Panel* delete_button_parent)
: window_style_(ws),
table_box_(new UI::Box(table_parent != nullptr ? table_parent : parent,
style,
"table_box",
0,
0,
UI::Box::Vertical)),
filetype_(filetype),
// Savegame description
game_details_(
parent,
style,
filetype == FileType::kReplay ? GameDetails::Mode::kReplay : GameDetails::Mode::kSavegame),
delete_(new UI::Button(
delete_button_parent != nullptr ? delete_button_parent : game_details()->button_box(),
"delete",
0,
0,
0,
0,
style == UI::PanelStyle::kFsMenu ? UI::ButtonStyle::kFsMenuSecondary :
UI::ButtonStyle::kWuiSecondary,
_("Delete"))),
basedir_(filetype_ == FileType::kReplay ? kReplayDir : kSaveDir),
curdir_(basedir_),
game_(g) {
g_mouse_cursor->change_wait(true);
switch (filetype_) {
case FileType::kReplay:
table_ = new SavegameTableReplay(table_box_, style, localize_autosave);
savegame_deleter_.reset(new ReplayDeleter(parent, ws));
savegame_loader_.reset(new ReplayLoader(g));
break;
case FileType::kGameSinglePlayer:
table_ = new SavegameTableSinglePlayer(table_box_, style, localize_autosave);
savegame_deleter_.reset(new SavegameDeleter(parent, ws));
savegame_loader_.reset((new SinglePlayerLoader(g)));
break;
case FileType::kGameMultiPlayer:
table_ = new SavegameTableMultiplayer(table_box_, style, localize_autosave);
savegame_deleter_.reset(new SavegameDeleter(parent, ws));
savegame_loader_.reset(new MultiPlayerLoader(g));
break;
case FileType::kShowAll:
table_ = new SavegameTableMultiplayer(
table_box_, style, localize_autosave); // wrong? showAll = save window -> "accidental"
// same table as multiplayer
savegame_deleter_.reset(new SavegameDeleter(parent, ws));
savegame_loader_.reset(new EverythingLoader(g));
break;
default:
NEVER_HERE();
}
table_->set_column_compare(
0, [this](uint32_t a, uint32_t b) { return compare_save_time(a, b); });
table_->set_column_compare(table_->number_of_columns() - 1,
[this](uint32_t a, uint32_t b) { return compare_map_name(a, b); });
table_box_->add(table_, UI::Box::Resizing::kExpandBoth);
if (delete_button_parent == nullptr) {
game_details_.button_box()->add(delete_, UI::Box::Resizing::kAlign, UI::Align::kLeft);
}
delete_->set_enabled(false);
delete_->sigclicked.connect([this] { clicked_delete(); });
fill_table();
}
bool LoadOrSaveGame::selection_contains_directory() const {
const std::set<uint32_t>& selections = table_->selections();
return std::any_of(selections.begin(), selections.end(),
[this](const uint32_t index) { return get_savegame(index).is_directory(); });
}
const SavegameData& LoadOrSaveGame::get_savegame(uint32_t index) const {
return games_data_[(*table_)[index]];
}
const std::vector<SavegameData> LoadOrSaveGame::get_selected_savegames() const {
const std::set<uint32_t> selections = table_->selections();
std::vector<SavegameData> savegames(selections.size());
size_t i = 0;
for (const uint32_t index : selections) {
savegames.at(i) = get_savegame(index);
++i;
}
return savegames;
}
bool LoadOrSaveGame::compare_save_time(uint32_t rowa, uint32_t rowb) const {
return get_savegame(rowa).compare_save_time(get_savegame(rowb));
}
bool LoadOrSaveGame::compare_map_name(uint32_t rowa, uint32_t rowb) const {
return get_savegame(rowa).compare_map_name(get_savegame(rowb));
}
/**
* Check whether the given file is a replay that is potentially incompatible with the
* current Widelands version. If so, show a message box to ask the user how to proceed.
* @param sd File data to investigate.
* @return \c false if the user has aborted loading the incompatible replay; \c true if it is not
* a replay at all or the replay is compatible or the user wishes to ignore the incompatibility.
*/
bool LoadOrSaveGame::check_replay_compatibility(const SavegameData& sd) {
if (filetype_ != FileType::kReplay || sd.is_directory() || !sd.errormessage.empty() ||
(SDL_GetModState() & KMOD_CTRL) != 0 || sd.version == build_id()) {
return true;
}
UI::WLMessageBox w(&game_details_.get_topmost_forefather(), window_style_, _("Version Mismatch"),
_("This replay was created with a different Widelands version. It "
"might be compatible, but will more likely desync or even fail to "
"load.\n\nPlease do not report any bugs that occur while watching "
"this replay.\n\nDo you want to load the replay anyway?"),
UI::WLMessageBox::MBoxType::kOkCancel);
return w.run<UI::Panel::Returncodes>() == UI::Panel::Returncodes::kOk;
}
std::unique_ptr<SavegameData> LoadOrSaveGame::entry_selected() {
std::unique_ptr<SavegameData> result(new SavegameData());
set_tooltips_of_buttons(table_->selections().size());
const std::vector<SavegameData> savegames = get_selected_savegames();
game_details_.display(savegames);
if (!table_->selections().empty()) {
delete_->set_enabled(!selection_contains_directory());
result.reset(new SavegameData(savegames[0]));
} else {
delete_->set_enabled(false);
delete_->set_tooltip("");
}
return result;
}
void LoadOrSaveGame::set_tooltips_of_buttons(size_t nr_of_selected_items) const {
if (nr_of_selected_items == 1) {
delete_->set_tooltip(
filetype_ == FileType::kReplay ?
/** TRANSLATORS: Tooltip for the delete button. The user has selected 1 file */
_("Delete this replay") :
/** TRANSLATORS: Tooltip for the delete button. The user has selected 1 file */
_("Delete this game"));
} else if (nr_of_selected_items > 1) {
delete_->set_tooltip(filetype_ == FileType::kReplay ?
/** TRANSLATORS: Tooltip for the delete button. The user has
selected multiple files */
_("Delete these replays") :
/** TRANSLATORS: Tooltip for the delete button. The user has
selected multiple files */
_("Delete these games"));
} else {
delete_->set_tooltip("");
}
}
bool LoadOrSaveGame::has_selection() const {
return table_->has_selection();
}
void LoadOrSaveGame::clear_selections() {
table_->clear_selections();
game_details_.clear();
}
void LoadOrSaveGame::select_by_name(const std::string& name) {
table_->clear_selections();
for (uint32_t idx = 0; idx < table_->size(); ++idx) {
const SavegameData& gamedata = get_savegame(idx);
if (name == gamedata.filename) {
table_->select(idx);
return;
}
}
}
SavegameTable& LoadOrSaveGame::table() {
return *table_;
}
UI::Box* LoadOrSaveGame::table_box() {
return table_box_;
}
GameDetails* LoadOrSaveGame::game_details() {
return &game_details_;
}
void LoadOrSaveGame::select_item_and_scroll_to_it(std::set<uint32_t>& selections) {
if (table_->empty()) {
return;
}
const uint32_t selectme = *selections.begin();
table_->select(std::min(selectme, table_->size() - 1));
table_->scroll_to_item(table_->selection_index() + 1);
}
void LoadOrSaveGame::clicked_delete() {
if (!has_selection()) {
return;
}
const std::vector<SavegameData> selected = get_selected_savegames();
std::set<uint32_t> selections = table_->selections();
if (savegame_deleter_->delete_savegames(selected)) {
fill_table();
select_item_and_scroll_to_it(selections);
// Make sure that the game details are updated
entry_selected();
}
}
UI::Button* LoadOrSaveGame::delete_button() {
return delete_;
}
void LoadOrSaveGame::fill_table() {
clear_selections();
games_data_ = savegame_loader_->load_files(curdir_);
// If we are not in basedir we are in a sub-dir so we need to add parent dir
if (curdir_ != basedir_) {
games_data_.push_back(SavegameData::create_parent_dir(curdir_));
}
table_->fill(games_data_);
}
void LoadOrSaveGame::set_show_filenames(bool show_filenames) {
if (filetype_ != FileType::kReplay) {
return;
}
table_->set_show_filenames(show_filenames);
}
void LoadOrSaveGame::change_directory_to(const std::string& directory) {
curdir_ = directory;
fill_table();
}
|