Codechange: Store custom station layouts in a map instead of nested vectors. (#12898)
[openttd-github.git] / src / fios_gui.cpp
blob08824aa395c68192b835cbcbd25d8352e441b97b
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD 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, version 2.
4 * OpenTTD 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.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file fios_gui.cpp GUIs for loading/saving games, scenarios, heightmaps, ... */
10 #include "stdafx.h"
11 #include "saveload/saveload.h"
12 #include "error.h"
13 #include "gui.h"
14 #include "gfx_func.h"
15 #include "command_func.h"
16 #include "network/network.h"
17 #include "network/network_content.h"
18 #include "strings_func.h"
19 #include "fileio_func.h"
20 #include "fios.h"
21 #include "window_func.h"
22 #include "tilehighlight_func.h"
23 #include "querystring_gui.h"
24 #include "engine_func.h"
25 #include "landscape_type.h"
26 #include "timer/timer_game_calendar.h"
27 #include "core/geometry_func.hpp"
28 #include "gamelog.h"
29 #include "stringfilter_type.h"
30 #include "misc_cmd.h"
31 #include "gamelog_internal.h"
33 #include "widgets/fios_widget.h"
35 #include "table/sprites.h"
36 #include "table/strings.h"
38 #include "safeguards.h"
40 LoadCheckData _load_check_data; ///< Data loaded from save during SL_LOAD_CHECK.
42 static bool _fios_path_changed;
43 static bool _savegame_sort_dirty;
45 /**
46 * Reset read data.
48 void LoadCheckData::Clear()
50 this->checkable = false;
51 this->error = INVALID_STRING_ID;
52 this->error_msg.clear();
54 this->map_size_x = this->map_size_y = 256; // Default for old savegames which do not store mapsize.
55 this->current_date = 0;
56 this->settings = {};
58 companies.clear();
60 this->gamelog.Reset();
62 ClearGRFConfigList(&this->grfconfig);
65 /** Load game/scenario with optional content download */
66 static constexpr NWidgetPart _nested_load_dialog_widgets[] = {
67 NWidget(NWID_HORIZONTAL),
68 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
69 NWidget(WWT_CAPTION, COLOUR_GREY, WID_SL_CAPTION),
70 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
71 EndContainer(),
72 /* Current directory and free space */
73 NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_BACKGROUND), SetFill(1, 0), SetResize(1, 0),
74 EndContainer(),
76 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
77 /* Left side : filter box and available files */
78 NWidget(NWID_VERTICAL),
79 /* Filter box with label */
80 NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), SetResize(1, 1),
81 NWidget(NWID_HORIZONTAL), SetPadding(WidgetDimensions::unscaled.framerect.top, 0, WidgetDimensions::unscaled.framerect.bottom, 0),
82 SetPIP(WidgetDimensions::unscaled.frametext.left, WidgetDimensions::unscaled.frametext.right, 0),
83 NWidget(WWT_TEXT, COLOUR_GREY), SetFill(0, 1), SetDataTip(STR_SAVELOAD_FILTER_TITLE , STR_NULL),
84 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_SL_FILTER), SetFill(1, 0), SetResize(1, 0),
85 SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
86 EndContainer(),
87 EndContainer(),
88 /* Sort buttons */
89 NWidget(NWID_HORIZONTAL),
90 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
91 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_SORT_BYNAME), SetDataTip(STR_SORT_BY_CAPTION_NAME, STR_TOOLTIP_SORT_ORDER), SetFill(1, 0), SetResize(1, 0),
92 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_SORT_BYDATE), SetDataTip(STR_SORT_BY_CAPTION_DATE, STR_TOOLTIP_SORT_ORDER), SetFill(1, 0), SetResize(1, 0),
93 EndContainer(),
94 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SL_HOME_BUTTON), SetAspect(1), SetDataTip(SPR_HOUSE_ICON, STR_SAVELOAD_HOME_BUTTON),
95 EndContainer(),
96 /* Files */
97 NWidget(NWID_HORIZONTAL),
98 NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_FILE_BACKGROUND),
99 NWidget(WWT_INSET, COLOUR_GREY, WID_SL_DRIVES_DIRECTORIES_LIST), SetFill(1, 1), SetPadding(2, 2, 2, 2),
100 SetDataTip(0x0, STR_SAVELOAD_LIST_TOOLTIP), SetResize(1, 10), SetScrollbar(WID_SL_SCROLLBAR),
101 EndContainer(),
102 EndContainer(),
103 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SL_SCROLLBAR),
104 EndContainer(),
105 /* Online Content button */
106 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_SL_CONTENT_DOWNLOAD_SEL),
107 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_CONTENT_DOWNLOAD), SetResize(1, 0),
108 SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
109 EndContainer(),
110 EndContainer(),
112 /* Right side : game details */
113 NWidget(NWID_VERTICAL),
114 NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_DETAILS), SetResize(1, 1), SetFill(1, 1),
115 EndContainer(),
116 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_MISSING_NEWGRFS), SetDataTip(STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON, STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
117 NWidget(NWID_HORIZONTAL),
118 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
119 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_NEWGRF_INFO), SetDataTip(STR_INTRO_NEWGRF_SETTINGS, STR_NULL), SetFill(1, 0), SetResize(1, 0),
120 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_LOAD_BUTTON), SetDataTip(STR_SAVELOAD_LOAD_BUTTON, STR_SAVELOAD_LOAD_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
121 EndContainer(),
122 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
123 EndContainer(),
124 EndContainer(),
125 EndContainer(),
128 /** Load heightmap with content download */
129 static constexpr NWidgetPart _nested_load_heightmap_dialog_widgets[] = {
130 NWidget(NWID_HORIZONTAL),
131 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
132 NWidget(WWT_CAPTION, COLOUR_GREY, WID_SL_CAPTION),
133 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
134 EndContainer(),
135 /* Current directory and free space */
136 NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_BACKGROUND), SetFill(1, 0), SetResize(1, 0),
137 EndContainer(),
139 /* Filter box with label */
140 NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), SetResize(1, 1),
141 NWidget(NWID_HORIZONTAL), SetPadding(WidgetDimensions::unscaled.framerect.top, 0, WidgetDimensions::unscaled.framerect.bottom, 0),
142 SetPIP(WidgetDimensions::unscaled.frametext.left, WidgetDimensions::unscaled.frametext.right, 0),
143 NWidget(WWT_TEXT, COLOUR_GREY), SetFill(0, 1), SetDataTip(STR_SAVELOAD_FILTER_TITLE , STR_NULL),
144 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_SL_FILTER), SetFill(1, 0), SetResize(1, 0),
145 SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
146 EndContainer(),
147 EndContainer(),
148 /* Sort Buttons */
149 NWidget(NWID_HORIZONTAL),
150 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
151 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_SORT_BYNAME), SetDataTip(STR_SORT_BY_CAPTION_NAME, STR_TOOLTIP_SORT_ORDER), SetFill(1, 0), SetResize(1, 0),
152 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_SORT_BYDATE), SetDataTip(STR_SORT_BY_CAPTION_DATE, STR_TOOLTIP_SORT_ORDER), SetFill(1, 0), SetResize(1, 0),
153 EndContainer(),
154 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SL_HOME_BUTTON), SetAspect(1), SetDataTip(SPR_HOUSE_ICON, STR_SAVELOAD_HOME_BUTTON),
155 EndContainer(),
156 /* Files */
157 NWidget(NWID_HORIZONTAL),
158 NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_FILE_BACKGROUND),
159 NWidget(WWT_INSET, COLOUR_GREY, WID_SL_DRIVES_DIRECTORIES_LIST), SetFill(1, 1), SetPadding(2, 2, 2, 2),
160 SetDataTip(0x0, STR_SAVELOAD_LIST_TOOLTIP), SetResize(1, 10), SetScrollbar(WID_SL_SCROLLBAR),
161 EndContainer(),
162 EndContainer(),
163 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SL_SCROLLBAR),
164 EndContainer(),
165 /* Online Content and Load button */
166 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
167 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_CONTENT_DOWNLOAD), SetResize(1, 0), SetFill(1, 0),
168 SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
169 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_LOAD_BUTTON), SetResize(1, 0), SetFill(1, 0),
170 SetDataTip(STR_SAVELOAD_LOAD_BUTTON, STR_SAVELOAD_LOAD_HEIGHTMAP_TOOLTIP),
171 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
172 EndContainer(),
175 /** Save game/scenario */
176 static constexpr NWidgetPart _nested_save_dialog_widgets[] = {
177 NWidget(NWID_HORIZONTAL),
178 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
179 NWidget(WWT_CAPTION, COLOUR_GREY, WID_SL_CAPTION),
180 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
181 EndContainer(),
182 /* Current directory and free space */
183 NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_BACKGROUND), SetFill(1, 0), SetResize(1, 0),
184 EndContainer(),
186 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
187 /* Left side : filter box and available files */
188 NWidget(NWID_VERTICAL),
189 /* Filter box with label */
190 NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), SetResize(1, 1),
191 NWidget(NWID_HORIZONTAL), SetPadding(WidgetDimensions::unscaled.framerect.top, 0, WidgetDimensions::unscaled.framerect.bottom, 0),
192 SetPIP(WidgetDimensions::unscaled.frametext.left, WidgetDimensions::unscaled.frametext.right, 0),
193 NWidget(WWT_TEXT, COLOUR_GREY), SetFill(0, 1), SetDataTip(STR_SAVELOAD_FILTER_TITLE , STR_NULL),
194 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_SL_FILTER), SetFill(1, 0), SetResize(1, 0),
195 SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
196 EndContainer(),
197 EndContainer(),
198 /* Sort buttons */
199 NWidget(NWID_HORIZONTAL),
200 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
201 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_SORT_BYNAME), SetDataTip(STR_SORT_BY_CAPTION_NAME, STR_TOOLTIP_SORT_ORDER), SetFill(1, 0), SetResize(1, 0),
202 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_SORT_BYDATE), SetDataTip(STR_SORT_BY_CAPTION_DATE, STR_TOOLTIP_SORT_ORDER), SetFill(1, 0), SetResize(1, 0),
203 EndContainer(),
204 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SL_HOME_BUTTON), SetAspect(1), SetDataTip(SPR_HOUSE_ICON, STR_SAVELOAD_HOME_BUTTON),
205 EndContainer(),
206 /* Files */
207 NWidget(NWID_HORIZONTAL),
208 NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_FILE_BACKGROUND),
209 NWidget(WWT_INSET, COLOUR_GREY, WID_SL_DRIVES_DIRECTORIES_LIST), SetPadding(2, 2, 2, 2),
210 SetDataTip(0x0, STR_SAVELOAD_LIST_TOOLTIP), SetResize(1, 10), SetScrollbar(WID_SL_SCROLLBAR),
211 EndContainer(),
212 EndContainer(),
213 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SL_SCROLLBAR),
214 EndContainer(),
215 NWidget(WWT_PANEL, COLOUR_GREY),
216 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_SL_SAVE_OSK_TITLE), SetPadding(2, 2, 2, 2), SetFill(1, 0), SetResize(1, 0),
217 SetDataTip(STR_SAVELOAD_OSKTITLE, STR_SAVELOAD_EDITBOX_TOOLTIP),
218 EndContainer(),
219 /* Save/delete buttons */
220 NWidget(NWID_HORIZONTAL),
221 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_DELETE_SELECTION), SetDataTip(STR_SAVELOAD_DELETE_BUTTON, STR_SAVELOAD_DELETE_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
222 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_SAVE_GAME), SetDataTip(STR_SAVELOAD_SAVE_BUTTON, STR_SAVELOAD_SAVE_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
223 EndContainer(),
224 EndContainer(),
226 /* Right side : game details */
227 NWidget(NWID_VERTICAL),
228 NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_DETAILS), SetResize(1, 1), SetFill(1, 1),
229 EndContainer(),
230 NWidget(NWID_HORIZONTAL),
231 NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), SetFill(1, 1),
232 EndContainer(),
233 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
234 EndContainer(),
235 EndContainer(),
236 EndContainer(),
239 /** Text colours of #DetailedFileType fios entries in the window. */
240 static const TextColour _fios_colours[] = {
241 TC_LIGHT_BROWN, // DFT_OLD_GAME_FILE
242 TC_ORANGE, // DFT_GAME_FILE
243 TC_YELLOW, // DFT_HEIGHTMAP_BMP
244 TC_ORANGE, // DFT_HEIGHTMAP_PNG
245 TC_LIGHT_BLUE, // DFT_FIOS_DRIVE
246 TC_DARK_GREEN, // DFT_FIOS_PARENT
247 TC_DARK_GREEN, // DFT_FIOS_DIR
248 TC_ORANGE, // DFT_FIOS_DIRECT
250 /* This should align with the DetailedFileType enum defined in fileio_type.h */
251 static_assert(std::size(_fios_colours) == DFT_END);
254 * Sort the collected list save games prior to displaying it in the save/load gui.
255 * @param[in,out] file_list List of save game files found in the directory.
257 static void SortSaveGameList(FileList &file_list)
259 size_t sort_start = 0;
260 size_t sort_end = 0;
262 /* Directories are always above the files (FIOS_TYPE_DIR)
263 * Drives (A:\ (windows only) are always under the files (FIOS_TYPE_DRIVE)
264 * Only sort savegames/scenarios, not directories
266 for (const auto &item : file_list) {
267 switch (item.type) {
268 case FIOS_TYPE_DIR: sort_start++; break;
269 case FIOS_TYPE_PARENT: sort_start++; break;
270 case FIOS_TYPE_DRIVE: sort_end++; break;
271 default: break;
275 std::sort(file_list.begin() + sort_start, file_list.end() - sort_end);
278 struct SaveLoadWindow : public Window {
279 private:
280 static const uint EDITBOX_MAX_SIZE = 50;
282 QueryString filename_editbox; ///< Filename editbox.
283 AbstractFileType abstract_filetype; /// Type of file to select.
284 SaveLoadOperation fop; ///< File operation to perform.
285 FileList fios_items; ///< Save game list.
286 FiosItem o_dir; ///< Original dir (home dir for this browser)
287 const FiosItem *selected; ///< Selected game in #fios_items, or \c nullptr.
288 const FiosItem *highlighted; ///< Item in fios_items highlighted by mouse pointer, or \c nullptr.
289 Scrollbar *vscroll;
291 StringFilter string_filter; ///< Filter for available games.
292 QueryString filter_editbox; ///< Filter editbox;
293 std::vector<FiosItem *> display_list; ///< Filtered display list
295 static void SaveGameConfirmationCallback(Window *, bool confirmed)
297 /* File name has already been written to _file_to_saveload */
298 if (confirmed) _switch_mode = SM_SAVE_GAME;
301 static void SaveHeightmapConfirmationCallback(Window *, bool confirmed)
303 /* File name has already been written to _file_to_saveload */
304 if (confirmed) _switch_mode = SM_SAVE_HEIGHTMAP;
307 public:
309 /** Generate a default save filename. */
310 void GenerateFileName()
312 this->filename_editbox.text.Assign(GenerateDefaultSaveName());
315 SaveLoadWindow(WindowDesc &desc, AbstractFileType abstract_filetype, SaveLoadOperation fop)
316 : Window(desc), filename_editbox(64), abstract_filetype(abstract_filetype), fop(fop), filter_editbox(EDITBOX_MAX_SIZE)
318 assert(this->fop == SLO_SAVE || this->fop == SLO_LOAD);
320 /* For saving, construct an initial file name. */
321 if (this->fop == SLO_SAVE) {
322 switch (this->abstract_filetype) {
323 case FT_SAVEGAME:
324 this->GenerateFileName();
325 break;
327 case FT_SCENARIO:
328 case FT_HEIGHTMAP:
329 this->filename_editbox.text.Assign("UNNAMED");
330 break;
332 default:
333 NOT_REACHED();
336 this->querystrings[WID_SL_SAVE_OSK_TITLE] = &this->filename_editbox;
337 this->filename_editbox.ok_button = WID_SL_SAVE_GAME;
339 this->CreateNestedTree();
340 if (this->fop == SLO_LOAD && this->abstract_filetype == FT_SAVEGAME) {
341 this->GetWidget<NWidgetStacked>(WID_SL_CONTENT_DOWNLOAD_SEL)->SetDisplayedPlane(SZSP_HORIZONTAL);
344 /* Select caption string of the window. */
345 StringID caption_string;
346 switch (this->abstract_filetype) {
347 case FT_SAVEGAME:
348 caption_string = (this->fop == SLO_SAVE) ? STR_SAVELOAD_SAVE_CAPTION : STR_SAVELOAD_LOAD_CAPTION;
349 break;
351 case FT_SCENARIO:
352 caption_string = (this->fop == SLO_SAVE) ? STR_SAVELOAD_SAVE_SCENARIO : STR_SAVELOAD_LOAD_SCENARIO;
353 break;
355 case FT_HEIGHTMAP:
356 caption_string = (this->fop == SLO_SAVE) ? STR_SAVELOAD_SAVE_HEIGHTMAP : STR_SAVELOAD_LOAD_HEIGHTMAP;
357 break;
359 default:
360 NOT_REACHED();
362 this->GetWidget<NWidgetCore>(WID_SL_CAPTION)->widget_data = caption_string;
364 this->vscroll = this->GetScrollbar(WID_SL_SCROLLBAR);
365 this->FinishInitNested(0);
367 this->LowerWidget(WID_SL_DRIVES_DIRECTORIES_LIST);
368 this->querystrings[WID_SL_FILTER] = &this->filter_editbox;
369 this->filter_editbox.cancel_button = QueryString::ACTION_CLEAR;
371 /* pause is only used in single-player, non-editor mode, non-menu mode. It
372 * will be unpaused in the WE_DESTROY event handler. */
373 if (_game_mode != GM_MENU && !_networking && _game_mode != GM_EDITOR) {
374 Command<CMD_PAUSE>::Post(PM_PAUSED_SAVELOAD, true);
376 SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
378 this->OnInvalidateData(SLIWD_RESCAN_FILES);
380 ResetObjectToPlace();
382 /* Select the initial directory. */
383 o_dir.type = FIOS_TYPE_DIRECT;
384 switch (this->abstract_filetype) {
385 case FT_SAVEGAME:
386 o_dir.name = FioFindDirectory(SAVE_DIR);
387 break;
389 case FT_SCENARIO:
390 o_dir.name = FioFindDirectory(SCENARIO_DIR);
391 break;
393 case FT_HEIGHTMAP:
394 o_dir.name = FioFindDirectory(HEIGHTMAP_DIR);
395 break;
397 default:
398 o_dir.name = _personal_dir;
401 switch (this->fop) {
402 case SLO_SAVE:
403 /* Focus the edit box by default in the save window */
404 this->SetFocusedWidget(WID_SL_SAVE_OSK_TITLE);
405 break;
407 default:
408 this->SetFocusedWidget(WID_SL_FILTER);
412 void Close([[maybe_unused]] int data = 0) override
414 /* pause is only used in single-player, non-editor mode, non menu mode */
415 if (!_networking && _game_mode != GM_EDITOR && _game_mode != GM_MENU) {
416 Command<CMD_PAUSE>::Post(PM_PAUSED_SAVELOAD, false);
418 this->Window::Close();
421 void DrawWidget(const Rect &r, WidgetID widget) const override
423 switch (widget) {
424 case WID_SL_SORT_BYNAME:
425 case WID_SL_SORT_BYDATE:
426 if (((_savegame_sort_order & SORT_BY_NAME) != 0) == (widget == WID_SL_SORT_BYNAME)) {
427 this->DrawSortButtonState(widget, _savegame_sort_order & SORT_DESCENDING ? SBS_DOWN : SBS_UP);
429 break;
431 case WID_SL_BACKGROUND: {
432 static std::string path;
433 static std::optional<uint64_t> free_space = std::nullopt;
435 if (_fios_path_changed) {
436 path = FiosGetCurrentPath();
437 free_space = FiosGetDiskFreeSpace(path);
438 _fios_path_changed = false;
441 Rect ir = r.Shrink(WidgetDimensions::scaled.framerect);
443 if (free_space.has_value()) SetDParam(0, free_space.value());
444 DrawString(ir.left, ir.right, ir.top + GetCharacterHeight(FS_NORMAL), free_space.has_value() ? STR_SAVELOAD_BYTES_FREE : STR_ERROR_UNABLE_TO_READ_DRIVE);
445 DrawString(ir.left, ir.right, ir.top, path, TC_BLACK);
446 break;
449 case WID_SL_DRIVES_DIRECTORIES_LIST: {
450 const Rect br = r.Shrink(WidgetDimensions::scaled.bevel);
451 GfxFillRect(br, PC_BLACK);
453 Rect tr = r.Shrink(WidgetDimensions::scaled.inset).WithHeight(this->resize.step_height);
454 auto [first, last] = this->vscroll->GetVisibleRangeIterators(this->display_list);
455 for (auto it = first; it != last; ++it) {
456 const FiosItem *item = *it;
458 if (item == this->selected) {
459 GfxFillRect(br.left, tr.top, br.right, tr.bottom, PC_DARK_BLUE);
460 } else if (item == this->highlighted) {
461 GfxFillRect(br.left, tr.top, br.right, tr.bottom, PC_VERY_DARK_BLUE);
463 DrawString(tr, item->title, _fios_colours[GetDetailedFileType(item->type)]);
464 tr = tr.Translate(0, this->resize.step_height);
466 break;
469 case WID_SL_DETAILS:
470 this->DrawDetails(r);
471 break;
475 void DrawDetails(const Rect &r) const
477 /* Header panel */
478 int HEADER_HEIGHT = GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.frametext.Vertical();
480 Rect hr = r.WithHeight(HEADER_HEIGHT).Shrink(WidgetDimensions::scaled.frametext);
481 Rect tr = r.Shrink(WidgetDimensions::scaled.frametext);
482 tr.top += HEADER_HEIGHT;
484 /* Create the nice grayish rectangle at the details top */
485 GfxFillRect(r.WithHeight(HEADER_HEIGHT).Shrink(WidgetDimensions::scaled.bevel.left, WidgetDimensions::scaled.bevel.top, WidgetDimensions::scaled.bevel.right, 0), PC_GREY);
486 DrawString(hr.left, hr.right, hr.top, STR_SAVELOAD_DETAIL_CAPTION, TC_FROMSTRING, SA_HOR_CENTER);
488 if (this->selected == nullptr) return;
490 /* Details panel */
491 tr.bottom -= GetCharacterHeight(FS_NORMAL) - 1;
492 if (tr.top > tr.bottom) return;
494 if (!_load_check_data.checkable) {
495 /* Old savegame, no information available */
496 DrawString(tr, STR_SAVELOAD_DETAIL_NOT_AVAILABLE);
497 tr.top += GetCharacterHeight(FS_NORMAL);
498 } else if (_load_check_data.error != INVALID_STRING_ID) {
499 /* Incompatible / broken savegame */
500 SetDParamStr(0, _load_check_data.error_msg);
501 tr.top = DrawStringMultiLine(tr, _load_check_data.error, TC_RED);
502 } else {
503 /* Mapsize */
504 SetDParam(0, _load_check_data.map_size_x);
505 SetDParam(1, _load_check_data.map_size_y);
506 DrawString(tr, STR_NETWORK_SERVER_LIST_MAP_SIZE);
507 tr.top += GetCharacterHeight(FS_NORMAL);
508 if (tr.top > tr.bottom) return;
510 /* Climate */
511 uint8_t landscape = _load_check_data.settings.game_creation.landscape;
512 if (landscape < NUM_LANDSCAPE) {
513 SetDParam(0, STR_CLIMATE_TEMPERATE_LANDSCAPE + landscape);
514 DrawString(tr, STR_NETWORK_SERVER_LIST_LANDSCAPE);
515 tr.top += GetCharacterHeight(FS_NORMAL);
518 tr.top += WidgetDimensions::scaled.vsep_normal;
519 if (tr.top > tr.bottom) return;
521 /* Start date (if available) */
522 if (_load_check_data.settings.game_creation.starting_year != 0) {
523 SetDParam(0, TimerGameCalendar::ConvertYMDToDate(_load_check_data.settings.game_creation.starting_year, 0, 1));
524 DrawString(tr, STR_NETWORK_SERVER_LIST_START_DATE);
525 tr.top += GetCharacterHeight(FS_NORMAL);
527 if (tr.top > tr.bottom) return;
529 /* Hide current date for scenarios */
530 if (this->abstract_filetype != FT_SCENARIO) {
531 /* Current date */
532 SetDParam(0, _load_check_data.current_date);
533 DrawString(tr, STR_NETWORK_SERVER_LIST_CURRENT_DATE);
534 tr.top += GetCharacterHeight(FS_NORMAL);
537 /* Hide the NewGRF stuff when saving. We also hide the button. */
538 if (this->fop == SLO_LOAD && (this->abstract_filetype == FT_SAVEGAME || this->abstract_filetype == FT_SCENARIO)) {
539 tr.top += WidgetDimensions::scaled.vsep_normal;
540 if (tr.top > tr.bottom) return;
542 /* NewGrf compatibility */
543 SetDParam(0, _load_check_data.grfconfig == nullptr ? STR_NEWGRF_LIST_NONE :
544 STR_NEWGRF_LIST_ALL_FOUND + _load_check_data.grf_compatibility);
545 DrawString(tr, STR_SAVELOAD_DETAIL_GRFSTATUS);
546 tr.top += GetCharacterHeight(FS_NORMAL);
548 if (tr.top > tr.bottom) return;
550 /* Hide the company stuff for scenarios */
551 if (this->abstract_filetype != FT_SCENARIO) {
552 tr.top += WidgetDimensions::scaled.vsep_wide;
553 if (tr.top > tr.bottom) return;
555 /* Companies / AIs */
556 for (auto &pair : _load_check_data.companies) {
557 SetDParam(0, pair.first + 1);
558 const CompanyProperties &c = *pair.second;
559 if (!c.name.empty()) {
560 SetDParam(1, STR_JUST_RAW_STRING);
561 SetDParamStr(2, c.name);
562 } else {
563 SetDParam(1, c.name_1);
564 SetDParam(2, c.name_2);
566 DrawString(tr, STR_SAVELOAD_DETAIL_COMPANY_INDEX);
567 tr.top += GetCharacterHeight(FS_NORMAL);
568 if (tr.top > tr.bottom) break;
574 void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
576 switch (widget) {
577 case WID_SL_BACKGROUND:
578 size.height = 2 * GetCharacterHeight(FS_NORMAL) + padding.height;
579 break;
581 case WID_SL_DRIVES_DIRECTORIES_LIST:
582 resize.height = GetCharacterHeight(FS_NORMAL);
583 size.height = resize.height * 10 + padding.height;
584 break;
585 case WID_SL_SORT_BYNAME:
586 case WID_SL_SORT_BYDATE: {
587 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
588 d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
589 d.height += padding.height;
590 size = maxdim(size, d);
591 break;
596 void OnPaint() override
598 if (_savegame_sort_dirty) {
599 _savegame_sort_dirty = false;
600 SortSaveGameList(this->fios_items);
601 this->OnInvalidateData(SLIWD_FILTER_CHANGES);
604 this->DrawWidgets();
607 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
609 switch (widget) {
610 case WID_SL_SORT_BYNAME: // Sort save names by name
611 _savegame_sort_order = (_savegame_sort_order == SORT_BY_NAME) ?
612 SORT_BY_NAME | SORT_DESCENDING : SORT_BY_NAME;
613 _savegame_sort_dirty = true;
614 this->SetDirty();
615 break;
617 case WID_SL_SORT_BYDATE: // Sort save names by date
618 _savegame_sort_order = (_savegame_sort_order == SORT_BY_DATE) ?
619 SORT_BY_DATE | SORT_DESCENDING : SORT_BY_DATE;
620 _savegame_sort_dirty = true;
621 this->SetDirty();
622 break;
624 case WID_SL_HOME_BUTTON: // OpenTTD 'button', jumps to OpenTTD directory
625 FiosBrowseTo(&o_dir);
626 this->InvalidateData(SLIWD_RESCAN_FILES);
627 break;
629 case WID_SL_LOAD_BUTTON: {
630 if (this->selected == nullptr || _load_check_data.HasErrors()) break;
632 _file_to_saveload.Set(*this->selected);
634 if (this->abstract_filetype == FT_HEIGHTMAP) {
635 this->Close();
636 ShowHeightmapLoad();
637 } else if (!_load_check_data.HasNewGrfs() || _load_check_data.grf_compatibility != GLC_NOT_FOUND || _settings_client.gui.UserIsAllowedToChangeNewGRFs()) {
638 _switch_mode = (_game_mode == GM_EDITOR) ? SM_LOAD_SCENARIO : SM_LOAD_GAME;
639 ClearErrorMessages();
640 this->Close();
642 break;
645 case WID_SL_NEWGRF_INFO:
646 if (_load_check_data.HasNewGrfs()) {
647 ShowNewGRFSettings(false, false, false, &_load_check_data.grfconfig);
649 break;
651 case WID_SL_MISSING_NEWGRFS:
652 if (!_network_available) {
653 ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
654 } else if (_load_check_data.HasNewGrfs()) {
655 ShowMissingContentWindow(_load_check_data.grfconfig);
657 break;
659 case WID_SL_DRIVES_DIRECTORIES_LIST: { // Click the listbox
660 auto it = this->vscroll->GetScrolledItemFromWidget(this->display_list, pt.y, this, WID_SL_DRIVES_DIRECTORIES_LIST, WidgetDimensions::scaled.inset.top);
661 if (it == this->display_list.end()) return;
663 /* Get the corresponding non-filtered out item from the list */
664 const FiosItem *file = *it;
666 if (FiosBrowseTo(file)) {
667 /* Changed directory, need refresh. */
668 this->InvalidateData(SLIWD_RESCAN_FILES);
669 break;
672 if (click_count == 1) {
673 if (this->selected != file) {
674 this->selected = file;
675 _load_check_data.Clear();
677 if (GetDetailedFileType(file->type) == DFT_GAME_FILE) {
678 /* Other detailed file types cannot be checked before. */
679 SaveOrLoad(file->name, SLO_CHECK, DFT_GAME_FILE, NO_DIRECTORY, false);
682 this->InvalidateData(SLIWD_SELECTION_CHANGES);
684 if (this->fop == SLO_SAVE) {
685 /* Copy clicked name to editbox */
686 this->filename_editbox.text.Assign(file->title);
687 this->SetWidgetDirty(WID_SL_SAVE_OSK_TITLE);
689 } else if (!_load_check_data.HasErrors()) {
690 this->selected = file;
691 if (this->fop == SLO_LOAD) {
692 if (this->abstract_filetype == FT_SAVEGAME || this->abstract_filetype == FT_SCENARIO) {
693 this->OnClick(pt, WID_SL_LOAD_BUTTON, 1);
694 } else {
695 assert(this->abstract_filetype == FT_HEIGHTMAP);
696 _file_to_saveload.Set(*file);
698 this->Close();
699 ShowHeightmapLoad();
703 break;
706 case WID_SL_CONTENT_DOWNLOAD:
707 if (!_network_available) {
708 ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
709 } else {
710 assert(this->fop == SLO_LOAD);
711 switch (this->abstract_filetype) {
712 default: NOT_REACHED();
713 case FT_SCENARIO: ShowNetworkContentListWindow(nullptr, CONTENT_TYPE_SCENARIO); break;
714 case FT_HEIGHTMAP: ShowNetworkContentListWindow(nullptr, CONTENT_TYPE_HEIGHTMAP); break;
717 break;
719 case WID_SL_DELETE_SELECTION: // Delete
720 break;
722 case WID_SL_SAVE_GAME: // Save game
723 /* Note, this is also called via the OSK; and we need to lower the button. */
724 this->HandleButtonClick(WID_SL_SAVE_GAME);
725 break;
729 void OnMouseOver([[maybe_unused]] Point pt, WidgetID widget) override
731 if (widget == WID_SL_DRIVES_DIRECTORIES_LIST) {
732 auto it = this->vscroll->GetScrolledItemFromWidget(this->display_list, pt.y, this, WID_SL_DRIVES_DIRECTORIES_LIST, WidgetDimensions::scaled.inset.top);
733 if (it == this->display_list.end()) return;
735 /* Get the corresponding non-filtered out item from the list */
736 const FiosItem *file = *it;
738 if (file != this->highlighted) {
739 this->highlighted = file;
740 this->SetWidgetDirty(WID_SL_DRIVES_DIRECTORIES_LIST);
742 } else if (this->highlighted != nullptr) {
743 this->highlighted = nullptr;
744 this->SetWidgetDirty(WID_SL_DRIVES_DIRECTORIES_LIST);
748 EventState OnKeyPress([[maybe_unused]] char32_t key, uint16_t keycode) override
750 if (keycode == WKC_ESC) {
751 this->Close();
752 return ES_HANDLED;
755 return ES_NOT_HANDLED;
758 void OnTimeout() override
760 /* Widgets WID_SL_DELETE_SELECTION and WID_SL_SAVE_GAME only exist when saving to a file. */
761 if (this->fop != SLO_SAVE) return;
763 if (this->IsWidgetLowered(WID_SL_DELETE_SELECTION)) { // Delete button clicked
764 if (!FiosDelete(this->filename_editbox.text.buf)) {
765 ShowErrorMessage(STR_ERROR_UNABLE_TO_DELETE_FILE, INVALID_STRING_ID, WL_ERROR);
766 } else {
767 this->InvalidateData(SLIWD_RESCAN_FILES);
768 /* Reset file name to current date on successful delete */
769 if (this->abstract_filetype == FT_SAVEGAME) GenerateFileName();
771 } else if (this->IsWidgetLowered(WID_SL_SAVE_GAME)) { // Save button clicked
772 if (this->abstract_filetype == FT_SAVEGAME || this->abstract_filetype == FT_SCENARIO) {
773 _file_to_saveload.name = FiosMakeSavegameName(this->filename_editbox.text.buf);
774 if (FioCheckFileExists(_file_to_saveload.name, Subdirectory::SAVE_DIR)) {
775 ShowQuery(STR_SAVELOAD_OVERWRITE_TITLE, STR_SAVELOAD_OVERWRITE_WARNING, this, SaveLoadWindow::SaveGameConfirmationCallback);
776 } else {
777 _switch_mode = SM_SAVE_GAME;
779 } else {
780 _file_to_saveload.name = FiosMakeHeightmapName(this->filename_editbox.text.buf);
781 if (FioCheckFileExists(_file_to_saveload.name, Subdirectory::SAVE_DIR)) {
782 ShowQuery(STR_SAVELOAD_OVERWRITE_TITLE, STR_SAVELOAD_OVERWRITE_WARNING, this, SaveLoadWindow::SaveHeightmapConfirmationCallback);
783 } else {
784 _switch_mode = SM_SAVE_HEIGHTMAP;
788 /* In the editor set up the vehicle engines correctly (date might have changed) */
789 if (_game_mode == GM_EDITOR) StartupEngines();
793 void OnResize() override
795 this->vscroll->SetCapacityFromWidget(this, WID_SL_DRIVES_DIRECTORIES_LIST);
798 void BuildDisplayList()
800 /* Filter changes */
801 this->display_list.clear();
802 this->display_list.reserve(this->fios_items.size());
804 if (this->string_filter.IsEmpty()) {
805 /* We don't filter anything out if the filter editbox is empty */
806 for (auto &it : this->fios_items) {
807 this->display_list.push_back(&it);
809 } else {
810 for (auto &it : this->fios_items) {
811 this->string_filter.ResetState();
812 this->string_filter.AddLine(it.title);
813 /* We set the vector to show this fios element as filtered depending on the result of the filter */
814 if (this->string_filter.GetState()) {
815 this->display_list.push_back(&it);
816 } else if (&it == this->selected) {
817 /* The selected element has been filtered out */
818 this->selected = nullptr;
819 this->OnInvalidateData(SLIWD_SELECTION_CHANGES);
824 this->vscroll->SetCount(this->display_list.size());
828 * Some data on this window has become invalid.
829 * @param data Information about the changed data.
830 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
832 void OnInvalidateData(int data = 0, bool gui_scope = true) override
834 switch (data) {
835 case SLIWD_RESCAN_FILES:
836 /* Rescan files */
837 this->selected = nullptr;
838 _load_check_data.Clear();
839 if (!gui_scope) break;
841 _fios_path_changed = true;
842 this->fios_items.BuildFileList(this->abstract_filetype, this->fop, true);
843 this->selected = nullptr;
844 _load_check_data.Clear();
846 /* We reset the files filtered */
847 this->OnInvalidateData(SLIWD_FILTER_CHANGES);
849 [[fallthrough]];
851 case SLIWD_SELECTION_CHANGES:
852 /* Selection changes */
853 if (!gui_scope) break;
855 if (this->fop != SLO_LOAD) break;
857 switch (this->abstract_filetype) {
858 case FT_HEIGHTMAP:
859 this->SetWidgetDisabledState(WID_SL_LOAD_BUTTON, this->selected == nullptr || _load_check_data.HasErrors());
860 break;
862 case FT_SAVEGAME:
863 case FT_SCENARIO: {
864 bool disabled = this->selected == nullptr || _load_check_data.HasErrors();
865 if (!_settings_client.gui.UserIsAllowedToChangeNewGRFs()) {
866 disabled |= _load_check_data.HasNewGrfs() && _load_check_data.grf_compatibility == GLC_NOT_FOUND;
868 this->SetWidgetDisabledState(WID_SL_LOAD_BUTTON, disabled);
869 this->SetWidgetDisabledState(WID_SL_NEWGRF_INFO, !_load_check_data.HasNewGrfs());
870 this->SetWidgetDisabledState(WID_SL_MISSING_NEWGRFS,
871 !_load_check_data.HasNewGrfs() || _load_check_data.grf_compatibility == GLC_ALL_GOOD);
872 break;
875 default:
876 NOT_REACHED();
878 break;
880 case SLIWD_FILTER_CHANGES:
881 this->BuildDisplayList();
882 break;
886 void OnEditboxChanged(WidgetID wid) override
888 if (wid == WID_SL_FILTER) {
889 this->string_filter.SetFilterTerm(this->filter_editbox.text.buf);
890 this->InvalidateData(SLIWD_FILTER_CHANGES);
895 /** Load game/scenario */
896 static WindowDesc _load_dialog_desc(
897 WDP_CENTER, "load_game", 500, 294,
898 WC_SAVELOAD, WC_NONE,
900 _nested_load_dialog_widgets
903 /** Load heightmap */
904 static WindowDesc _load_heightmap_dialog_desc(
905 WDP_CENTER, "load_heightmap", 257, 320,
906 WC_SAVELOAD, WC_NONE,
908 _nested_load_heightmap_dialog_widgets
911 /** Save game/scenario */
912 static WindowDesc _save_dialog_desc(
913 WDP_CENTER, "save_game", 500, 294,
914 WC_SAVELOAD, WC_NONE,
916 _nested_save_dialog_widgets
920 * Launch save/load dialog in the given mode.
921 * @param abstract_filetype Kind of file to handle.
922 * @param fop File operation to perform (load or save).
924 void ShowSaveLoadDialog(AbstractFileType abstract_filetype, SaveLoadOperation fop)
926 CloseWindowById(WC_SAVELOAD, 0);
928 if (fop == SLO_SAVE) {
929 new SaveLoadWindow(_save_dialog_desc, abstract_filetype, fop);
930 } else {
931 /* Dialogue for loading a file. */
932 new SaveLoadWindow((abstract_filetype == FT_HEIGHTMAP) ? _load_heightmap_dialog_desc : _load_dialog_desc, abstract_filetype, fop);