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/>.
8 /** @file fios_gui.cpp GUIs for loading/saving games, scenarios, heightmaps, ... */
11 #include "saveload/saveload.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"
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"
27 #include "timer/timer_game_calendar.h"
28 #include "core/geometry_func.hpp"
30 #include "stringfilter_type.h"
32 #include "gamelog_internal.h"
34 #include "widgets/fios_widget.h"
36 #include "table/sprites.h"
37 #include "table/strings.h"
39 #include "safeguards.h"
41 LoadCheckData _load_check_data
; ///< Data loaded from save during SL_LOAD_CHECK.
43 static bool _fios_path_changed
;
44 static bool _savegame_sort_dirty
;
49 void LoadCheckData::Clear()
51 this->checkable
= false;
52 this->error
= INVALID_STRING_ID
;
53 this->error_msg
.clear();
55 this->map_size_x
= this->map_size_y
= 256; // Default for old savegames which do not store mapsize.
56 this->current_date
= 0;
61 this->gamelog
.Reset();
63 ClearGRFConfigList(&this->grfconfig
);
66 /** Load game/scenario with optional content download */
67 static constexpr NWidgetPart _nested_load_dialog_widgets
[] = {
68 NWidget(NWID_HORIZONTAL
),
69 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
70 NWidget(WWT_CAPTION
, COLOUR_GREY
, WID_SL_CAPTION
),
71 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
73 /* Current directory and free space */
74 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_SL_BACKGROUND
), SetFill(1, 0), SetResize(1, 0),
77 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
78 /* Left side : filter box and available files */
79 NWidget(NWID_VERTICAL
),
80 /* Filter box with label */
81 NWidget(WWT_PANEL
, COLOUR_GREY
), SetFill(1, 1), SetResize(1, 1),
82 NWidget(NWID_HORIZONTAL
), SetPadding(WidgetDimensions::unscaled
.framerect
.top
, 0, WidgetDimensions::unscaled
.framerect
.bottom
, 0),
83 SetPIP(WidgetDimensions::unscaled
.frametext
.left
, WidgetDimensions::unscaled
.frametext
.right
, 0),
84 NWidget(WWT_TEXT
, COLOUR_GREY
), SetFill(0, 1), SetDataTip(STR_SAVELOAD_FILTER_TITLE
, STR_NULL
),
85 NWidget(WWT_EDITBOX
, COLOUR_GREY
, WID_SL_FILTER
), SetFill(1, 0), SetResize(1, 0),
86 SetDataTip(STR_LIST_FILTER_OSKTITLE
, STR_LIST_FILTER_TOOLTIP
),
90 NWidget(NWID_HORIZONTAL
),
91 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
92 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),
93 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),
95 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_SL_HOME_BUTTON
), SetAspect(1), SetDataTip(SPR_HOUSE_ICON
, STR_SAVELOAD_HOME_BUTTON
),
98 NWidget(NWID_HORIZONTAL
),
99 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_SL_FILE_BACKGROUND
),
100 NWidget(WWT_INSET
, COLOUR_GREY
, WID_SL_DRIVES_DIRECTORIES_LIST
), SetFill(1, 1), SetPadding(2, 2, 2, 2),
101 SetDataTip(0x0, STR_SAVELOAD_LIST_TOOLTIP
), SetResize(1, 10), SetScrollbar(WID_SL_SCROLLBAR
),
104 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_SL_SCROLLBAR
),
106 /* Online Content button */
107 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_SL_CONTENT_DOWNLOAD_SEL
),
108 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SL_CONTENT_DOWNLOAD
), SetResize(1, 0),
109 SetDataTip(STR_INTRO_ONLINE_CONTENT
, STR_INTRO_TOOLTIP_ONLINE_CONTENT
),
113 /* Right side : game details */
114 NWidget(NWID_VERTICAL
),
115 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_SL_DETAILS
), SetResize(1, 1), SetFill(1, 1),
117 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),
118 NWidget(NWID_HORIZONTAL
),
119 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
120 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SL_NEWGRF_INFO
), SetDataTip(STR_INTRO_NEWGRF_SETTINGS
, STR_NULL
), SetFill(1, 0), SetResize(1, 0),
121 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SL_LOAD_BUTTON
), SetDataTip(STR_SAVELOAD_LOAD_BUTTON
, STR_SAVELOAD_LOAD_TOOLTIP
), SetFill(1, 0), SetResize(1, 0),
123 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
129 /** Load heightmap with content download */
130 static constexpr NWidgetPart _nested_load_heightmap_dialog_widgets
[] = {
131 NWidget(NWID_HORIZONTAL
),
132 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
133 NWidget(WWT_CAPTION
, COLOUR_GREY
, WID_SL_CAPTION
),
134 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
136 /* Current directory and free space */
137 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_SL_BACKGROUND
), SetFill(1, 0), SetResize(1, 0),
140 /* Filter box with label */
141 NWidget(WWT_PANEL
, COLOUR_GREY
), SetFill(1, 1), SetResize(1, 1),
142 NWidget(NWID_HORIZONTAL
), SetPadding(WidgetDimensions::unscaled
.framerect
.top
, 0, WidgetDimensions::unscaled
.framerect
.bottom
, 0),
143 SetPIP(WidgetDimensions::unscaled
.frametext
.left
, WidgetDimensions::unscaled
.frametext
.right
, 0),
144 NWidget(WWT_TEXT
, COLOUR_GREY
), SetFill(0, 1), SetDataTip(STR_SAVELOAD_FILTER_TITLE
, STR_NULL
),
145 NWidget(WWT_EDITBOX
, COLOUR_GREY
, WID_SL_FILTER
), SetFill(1, 0), SetResize(1, 0),
146 SetDataTip(STR_LIST_FILTER_OSKTITLE
, STR_LIST_FILTER_TOOLTIP
),
150 NWidget(NWID_HORIZONTAL
),
151 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
152 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),
153 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),
155 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_SL_HOME_BUTTON
), SetAspect(1), SetDataTip(SPR_HOUSE_ICON
, STR_SAVELOAD_HOME_BUTTON
),
158 NWidget(NWID_HORIZONTAL
),
159 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_SL_FILE_BACKGROUND
),
160 NWidget(WWT_INSET
, COLOUR_GREY
, WID_SL_DRIVES_DIRECTORIES_LIST
), SetFill(1, 1), SetPadding(2, 2, 2, 2),
161 SetDataTip(0x0, STR_SAVELOAD_LIST_TOOLTIP
), SetResize(1, 10), SetScrollbar(WID_SL_SCROLLBAR
),
164 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_SL_SCROLLBAR
),
166 /* Online Content and Load button */
167 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
168 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SL_CONTENT_DOWNLOAD
), SetResize(1, 0), SetFill(1, 0),
169 SetDataTip(STR_INTRO_ONLINE_CONTENT
, STR_INTRO_TOOLTIP_ONLINE_CONTENT
),
170 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SL_LOAD_BUTTON
), SetResize(1, 0), SetFill(1, 0),
171 SetDataTip(STR_SAVELOAD_LOAD_BUTTON
, STR_SAVELOAD_LOAD_HEIGHTMAP_TOOLTIP
),
172 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
176 /** Load town data */
177 static constexpr NWidgetPart _nested_load_town_data_dialog_widgets
[] = {
178 NWidget(NWID_HORIZONTAL
),
179 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
180 NWidget(WWT_CAPTION
, COLOUR_GREY
, WID_SL_CAPTION
),
181 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
183 /* Current directory and free space */
184 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_SL_BACKGROUND
), SetFill(1, 0), SetResize(1, 0), EndContainer(),
186 /* Filter box with label */
187 NWidget(WWT_PANEL
, COLOUR_GREY
), SetFill(1, 1), SetResize(1, 1),
188 NWidget(NWID_HORIZONTAL
), SetPadding(WidgetDimensions::unscaled
.framerect
.top
, 0, WidgetDimensions::unscaled
.framerect
.bottom
, 0),
189 SetPIP(WidgetDimensions::unscaled
.frametext
.left
, WidgetDimensions::unscaled
.frametext
.right
, 0),
190 NWidget(WWT_TEXT
, COLOUR_GREY
), SetFill(0, 1), SetDataTip(STR_SAVELOAD_FILTER_TITLE
, STR_NULL
),
191 NWidget(WWT_EDITBOX
, COLOUR_GREY
, WID_SL_FILTER
), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_LIST_FILTER_OSKTITLE
, STR_LIST_FILTER_TOOLTIP
),
195 NWidget(NWID_HORIZONTAL
),
196 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
197 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),
198 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),
200 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_SL_HOME_BUTTON
), SetAspect(1), SetDataTip(SPR_HOUSE_ICON
, STR_SAVELOAD_HOME_BUTTON
),
203 NWidget(NWID_HORIZONTAL
),
204 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_SL_FILE_BACKGROUND
),
205 NWidget(WWT_INSET
, COLOUR_GREY
, WID_SL_DRIVES_DIRECTORIES_LIST
), SetFill(1, 1), SetPadding(2, 2, 2, 2),
206 SetDataTip(0x0, STR_SAVELOAD_LIST_TOOLTIP
), SetResize(1, 10), SetScrollbar(WID_SL_SCROLLBAR
), EndContainer(),
208 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_SL_SCROLLBAR
),
211 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
212 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SL_LOAD_BUTTON
), SetResize(1, 0), SetFill(1, 0),
213 SetDataTip(STR_SAVELOAD_LOAD_BUTTON
, STR_SAVELOAD_LOAD_TOWN_DATA_TOOLTIP
),
214 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
218 /** Save game/scenario */
219 static constexpr NWidgetPart _nested_save_dialog_widgets
[] = {
220 NWidget(NWID_HORIZONTAL
),
221 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
222 NWidget(WWT_CAPTION
, COLOUR_GREY
, WID_SL_CAPTION
),
223 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
225 /* Current directory and free space */
226 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_SL_BACKGROUND
), SetFill(1, 0), SetResize(1, 0),
229 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
230 /* Left side : filter box and available files */
231 NWidget(NWID_VERTICAL
),
232 /* Filter box with label */
233 NWidget(WWT_PANEL
, COLOUR_GREY
), SetFill(1, 1), SetResize(1, 1),
234 NWidget(NWID_HORIZONTAL
), SetPadding(WidgetDimensions::unscaled
.framerect
.top
, 0, WidgetDimensions::unscaled
.framerect
.bottom
, 0),
235 SetPIP(WidgetDimensions::unscaled
.frametext
.left
, WidgetDimensions::unscaled
.frametext
.right
, 0),
236 NWidget(WWT_TEXT
, COLOUR_GREY
), SetFill(0, 1), SetDataTip(STR_SAVELOAD_FILTER_TITLE
, STR_NULL
),
237 NWidget(WWT_EDITBOX
, COLOUR_GREY
, WID_SL_FILTER
), SetFill(1, 0), SetResize(1, 0),
238 SetDataTip(STR_LIST_FILTER_OSKTITLE
, STR_LIST_FILTER_TOOLTIP
),
242 NWidget(NWID_HORIZONTAL
),
243 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
244 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),
245 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),
247 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_SL_HOME_BUTTON
), SetAspect(1), SetDataTip(SPR_HOUSE_ICON
, STR_SAVELOAD_HOME_BUTTON
),
250 NWidget(NWID_HORIZONTAL
),
251 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_SL_FILE_BACKGROUND
),
252 NWidget(WWT_INSET
, COLOUR_GREY
, WID_SL_DRIVES_DIRECTORIES_LIST
), SetPadding(2, 2, 2, 2),
253 SetDataTip(0x0, STR_SAVELOAD_LIST_TOOLTIP
), SetResize(1, 10), SetScrollbar(WID_SL_SCROLLBAR
),
256 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_SL_SCROLLBAR
),
258 NWidget(WWT_PANEL
, COLOUR_GREY
),
259 NWidget(WWT_EDITBOX
, COLOUR_GREY
, WID_SL_SAVE_OSK_TITLE
), SetPadding(2, 2, 2, 2), SetFill(1, 0), SetResize(1, 0),
260 SetDataTip(STR_SAVELOAD_OSKTITLE
, STR_SAVELOAD_EDITBOX_TOOLTIP
),
262 /* Save/delete buttons */
263 NWidget(NWID_HORIZONTAL
),
264 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SL_DELETE_SELECTION
), SetDataTip(STR_SAVELOAD_DELETE_BUTTON
, STR_SAVELOAD_DELETE_TOOLTIP
), SetFill(1, 0), SetResize(1, 0),
265 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SL_SAVE_GAME
), SetDataTip(STR_SAVELOAD_SAVE_BUTTON
, STR_SAVELOAD_SAVE_TOOLTIP
), SetFill(1, 0), SetResize(1, 0),
269 /* Right side : game details */
270 NWidget(NWID_VERTICAL
),
271 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_SL_DETAILS
), SetResize(1, 1), SetFill(1, 1),
273 NWidget(NWID_HORIZONTAL
),
274 NWidget(WWT_PANEL
, COLOUR_GREY
), SetResize(1, 0), SetFill(1, 1),
276 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
282 /** Text colours of #DetailedFileType fios entries in the window. */
283 static const TextColour _fios_colours
[] = {
284 TC_LIGHT_BROWN
, // DFT_OLD_GAME_FILE
285 TC_ORANGE
, // DFT_GAME_FILE
286 TC_YELLOW
, // DFT_HEIGHTMAP_BMP
287 TC_ORANGE
, // DFT_HEIGHTMAP_PNG
288 TC_LIGHT_BROWN
, // DFT_TOWN_DATA_JSON
289 TC_LIGHT_BLUE
, // DFT_FIOS_DRIVE
290 TC_DARK_GREEN
, // DFT_FIOS_PARENT
291 TC_DARK_GREEN
, // DFT_FIOS_DIR
292 TC_ORANGE
, // DFT_FIOS_DIRECT
294 /* This should align with the DetailedFileType enum defined in fileio_type.h */
295 static_assert(std::size(_fios_colours
) == DFT_END
);
298 * Sort the collected list save games prior to displaying it in the save/load gui.
299 * @param[in,out] file_list List of save game files found in the directory.
301 static void SortSaveGameList(FileList
&file_list
)
303 size_t sort_start
= 0;
306 /* Directories are always above the files (FIOS_TYPE_DIR)
307 * Drives (A:\ (windows only) are always under the files (FIOS_TYPE_DRIVE)
308 * Only sort savegames/scenarios, not directories
310 for (const auto &item
: file_list
) {
312 case FIOS_TYPE_DIR
: sort_start
++; break;
313 case FIOS_TYPE_PARENT
: sort_start
++; break;
314 case FIOS_TYPE_DRIVE
: sort_end
++; break;
319 std::sort(file_list
.begin() + sort_start
, file_list
.end() - sort_end
);
322 struct SaveLoadWindow
: public Window
{
324 static const uint EDITBOX_MAX_SIZE
= 50;
326 QueryString filename_editbox
; ///< Filename editbox.
327 AbstractFileType abstract_filetype
; /// Type of file to select.
328 SaveLoadOperation fop
; ///< File operation to perform.
329 FileList fios_items
; ///< Save game list.
330 FiosItem o_dir
; ///< Original dir (home dir for this browser)
331 const FiosItem
*selected
; ///< Selected game in #fios_items, or \c nullptr.
332 const FiosItem
*highlighted
; ///< Item in fios_items highlighted by mouse pointer, or \c nullptr.
335 StringFilter string_filter
; ///< Filter for available games.
336 QueryString filter_editbox
; ///< Filter editbox;
337 std::vector
<FiosItem
*> display_list
; ///< Filtered display list
339 static void SaveGameConfirmationCallback(Window
*, bool confirmed
)
341 /* File name has already been written to _file_to_saveload */
342 if (confirmed
) _switch_mode
= SM_SAVE_GAME
;
345 static void SaveHeightmapConfirmationCallback(Window
*, bool confirmed
)
347 /* File name has already been written to _file_to_saveload */
348 if (confirmed
) _switch_mode
= SM_SAVE_HEIGHTMAP
;
353 /** Generate a default save filename. */
354 void GenerateFileName()
356 this->filename_editbox
.text
.Assign(GenerateDefaultSaveName());
359 SaveLoadWindow(WindowDesc
&desc
, AbstractFileType abstract_filetype
, SaveLoadOperation fop
)
360 : Window(desc
), filename_editbox(64), abstract_filetype(abstract_filetype
), fop(fop
), filter_editbox(EDITBOX_MAX_SIZE
)
362 assert(this->fop
== SLO_SAVE
|| this->fop
== SLO_LOAD
);
364 /* For saving, construct an initial file name. */
365 if (this->fop
== SLO_SAVE
) {
366 switch (this->abstract_filetype
) {
368 this->GenerateFileName();
373 this->filename_editbox
.text
.Assign("UNNAMED");
377 /* It's not currently possible to save town data. */
381 this->querystrings
[WID_SL_SAVE_OSK_TITLE
] = &this->filename_editbox
;
382 this->filename_editbox
.ok_button
= WID_SL_SAVE_GAME
;
384 this->CreateNestedTree();
385 if (this->fop
== SLO_LOAD
&& this->abstract_filetype
== FT_SAVEGAME
) {
386 this->GetWidget
<NWidgetStacked
>(WID_SL_CONTENT_DOWNLOAD_SEL
)->SetDisplayedPlane(SZSP_HORIZONTAL
);
389 /* Select caption string of the window. */
390 StringID caption_string
;
391 switch (this->abstract_filetype
) {
393 caption_string
= (this->fop
== SLO_SAVE
) ? STR_SAVELOAD_SAVE_CAPTION
: STR_SAVELOAD_LOAD_CAPTION
;
397 caption_string
= (this->fop
== SLO_SAVE
) ? STR_SAVELOAD_SAVE_SCENARIO
: STR_SAVELOAD_LOAD_SCENARIO
;
401 caption_string
= (this->fop
== SLO_SAVE
) ? STR_SAVELOAD_SAVE_HEIGHTMAP
: STR_SAVELOAD_LOAD_HEIGHTMAP
;
405 caption_string
= STR_SAVELOAD_LOAD_TOWN_DATA
; // It's not currently possible to save town data.
411 this->GetWidget
<NWidgetCore
>(WID_SL_CAPTION
)->widget_data
= caption_string
;
413 this->vscroll
= this->GetScrollbar(WID_SL_SCROLLBAR
);
414 this->FinishInitNested(0);
416 this->LowerWidget(WID_SL_DRIVES_DIRECTORIES_LIST
);
417 this->querystrings
[WID_SL_FILTER
] = &this->filter_editbox
;
418 this->filter_editbox
.cancel_button
= QueryString::ACTION_CLEAR
;
420 /* pause is only used in single-player, non-editor mode, non-menu mode. It
421 * will be unpaused in the WE_DESTROY event handler. */
422 if (_game_mode
!= GM_MENU
&& !_networking
&& _game_mode
!= GM_EDITOR
) {
423 Command
<CMD_PAUSE
>::Post(PM_PAUSED_SAVELOAD
, true);
425 SetObjectToPlace(SPR_CURSOR_ZZZ
, PAL_NONE
, HT_NONE
, WC_MAIN_WINDOW
, 0);
427 this->OnInvalidateData(SLIWD_RESCAN_FILES
);
429 ResetObjectToPlace();
431 /* Select the initial directory. */
432 o_dir
.type
= FIOS_TYPE_DIRECT
;
433 switch (this->abstract_filetype
) {
435 o_dir
.name
= FioFindDirectory(SAVE_DIR
);
439 o_dir
.name
= FioFindDirectory(SCENARIO_DIR
);
444 o_dir
.name
= FioFindDirectory(HEIGHTMAP_DIR
);
448 o_dir
.name
= _personal_dir
;
453 /* Focus the edit box by default in the save window */
454 this->SetFocusedWidget(WID_SL_SAVE_OSK_TITLE
);
458 this->SetFocusedWidget(WID_SL_FILTER
);
462 void Close([[maybe_unused
]] int data
= 0) override
464 /* pause is only used in single-player, non-editor mode, non menu mode */
465 if (!_networking
&& _game_mode
!= GM_EDITOR
&& _game_mode
!= GM_MENU
) {
466 Command
<CMD_PAUSE
>::Post(PM_PAUSED_SAVELOAD
, false);
468 this->Window::Close();
471 void DrawWidget(const Rect
&r
, WidgetID widget
) const override
474 case WID_SL_SORT_BYNAME
:
475 case WID_SL_SORT_BYDATE
:
476 if (((_savegame_sort_order
& SORT_BY_NAME
) != 0) == (widget
== WID_SL_SORT_BYNAME
)) {
477 this->DrawSortButtonState(widget
, _savegame_sort_order
& SORT_DESCENDING
? SBS_DOWN
: SBS_UP
);
481 case WID_SL_BACKGROUND
: {
482 static std::string path
;
483 static std::optional
<uint64_t> free_space
= std::nullopt
;
485 if (_fios_path_changed
) {
486 path
= FiosGetCurrentPath();
487 free_space
= FiosGetDiskFreeSpace(path
);
488 _fios_path_changed
= false;
491 Rect ir
= r
.Shrink(WidgetDimensions::scaled
.framerect
);
493 if (free_space
.has_value()) SetDParam(0, free_space
.value());
494 DrawString(ir
.left
, ir
.right
, ir
.top
+ GetCharacterHeight(FS_NORMAL
), free_space
.has_value() ? STR_SAVELOAD_BYTES_FREE
: STR_ERROR_UNABLE_TO_READ_DRIVE
);
495 DrawString(ir
.left
, ir
.right
, ir
.top
, path
, TC_BLACK
);
499 case WID_SL_DRIVES_DIRECTORIES_LIST
: {
500 const Rect br
= r
.Shrink(WidgetDimensions::scaled
.bevel
);
501 GfxFillRect(br
, PC_BLACK
);
503 Rect tr
= r
.Shrink(WidgetDimensions::scaled
.inset
).WithHeight(this->resize
.step_height
);
504 auto [first
, last
] = this->vscroll
->GetVisibleRangeIterators(this->display_list
);
505 for (auto it
= first
; it
!= last
; ++it
) {
506 const FiosItem
*item
= *it
;
508 if (item
== this->selected
) {
509 GfxFillRect(br
.left
, tr
.top
, br
.right
, tr
.bottom
, PC_DARK_BLUE
);
510 } else if (item
== this->highlighted
) {
511 GfxFillRect(br
.left
, tr
.top
, br
.right
, tr
.bottom
, PC_VERY_DARK_BLUE
);
513 DrawString(tr
, item
->title
, _fios_colours
[GetDetailedFileType(item
->type
)]);
514 tr
= tr
.Translate(0, this->resize
.step_height
);
520 this->DrawDetails(r
);
525 void DrawDetails(const Rect
&r
) const
528 int HEADER_HEIGHT
= GetCharacterHeight(FS_NORMAL
) + WidgetDimensions::scaled
.frametext
.Vertical();
530 Rect hr
= r
.WithHeight(HEADER_HEIGHT
).Shrink(WidgetDimensions::scaled
.frametext
);
531 Rect tr
= r
.Shrink(WidgetDimensions::scaled
.frametext
);
532 tr
.top
+= HEADER_HEIGHT
;
534 /* Create the nice grayish rectangle at the details top */
535 GfxFillRect(r
.WithHeight(HEADER_HEIGHT
).Shrink(WidgetDimensions::scaled
.bevel
.left
, WidgetDimensions::scaled
.bevel
.top
, WidgetDimensions::scaled
.bevel
.right
, 0), PC_GREY
);
536 DrawString(hr
.left
, hr
.right
, hr
.top
, STR_SAVELOAD_DETAIL_CAPTION
, TC_FROMSTRING
, SA_HOR_CENTER
);
538 if (this->selected
== nullptr) return;
541 tr
.bottom
-= GetCharacterHeight(FS_NORMAL
) - 1;
542 if (tr
.top
> tr
.bottom
) return;
544 if (!_load_check_data
.checkable
) {
545 /* Old savegame, no information available */
546 DrawString(tr
, STR_SAVELOAD_DETAIL_NOT_AVAILABLE
);
547 tr
.top
+= GetCharacterHeight(FS_NORMAL
);
548 } else if (_load_check_data
.error
!= INVALID_STRING_ID
) {
549 /* Incompatible / broken savegame */
550 SetDParamStr(0, _load_check_data
.error_msg
);
551 tr
.top
= DrawStringMultiLine(tr
, _load_check_data
.error
, TC_RED
);
554 SetDParam(0, _load_check_data
.map_size_x
);
555 SetDParam(1, _load_check_data
.map_size_y
);
556 DrawString(tr
, STR_NETWORK_SERVER_LIST_MAP_SIZE
);
557 tr
.top
+= GetCharacterHeight(FS_NORMAL
);
558 if (tr
.top
> tr
.bottom
) return;
561 uint8_t landscape
= _load_check_data
.settings
.game_creation
.landscape
;
562 if (landscape
< NUM_LANDSCAPE
) {
563 SetDParam(0, STR_CLIMATE_TEMPERATE_LANDSCAPE
+ landscape
);
564 DrawString(tr
, STR_NETWORK_SERVER_LIST_LANDSCAPE
);
565 tr
.top
+= GetCharacterHeight(FS_NORMAL
);
568 tr
.top
+= WidgetDimensions::scaled
.vsep_normal
;
569 if (tr
.top
> tr
.bottom
) return;
571 /* Start date (if available) */
572 if (_load_check_data
.settings
.game_creation
.starting_year
!= 0) {
573 SetDParam(0, TimerGameCalendar::ConvertYMDToDate(_load_check_data
.settings
.game_creation
.starting_year
, 0, 1));
574 DrawString(tr
, STR_NETWORK_SERVER_LIST_START_DATE
);
575 tr
.top
+= GetCharacterHeight(FS_NORMAL
);
577 if (tr
.top
> tr
.bottom
) return;
579 /* Hide current date for scenarios */
580 if (this->abstract_filetype
!= FT_SCENARIO
) {
582 SetDParam(0, _load_check_data
.current_date
);
583 DrawString(tr
, STR_NETWORK_SERVER_LIST_CURRENT_DATE
);
584 tr
.top
+= GetCharacterHeight(FS_NORMAL
);
587 /* Hide the NewGRF stuff when saving. We also hide the button. */
588 if (this->fop
== SLO_LOAD
&& (this->abstract_filetype
== FT_SAVEGAME
|| this->abstract_filetype
== FT_SCENARIO
)) {
589 tr
.top
+= WidgetDimensions::scaled
.vsep_normal
;
590 if (tr
.top
> tr
.bottom
) return;
592 /* NewGrf compatibility */
593 SetDParam(0, _load_check_data
.grfconfig
== nullptr ? STR_NEWGRF_LIST_NONE
:
594 STR_NEWGRF_LIST_ALL_FOUND
+ _load_check_data
.grf_compatibility
);
595 DrawString(tr
, STR_SAVELOAD_DETAIL_GRFSTATUS
);
596 tr
.top
+= GetCharacterHeight(FS_NORMAL
);
598 if (tr
.top
> tr
.bottom
) return;
600 /* Hide the company stuff for scenarios */
601 if (this->abstract_filetype
!= FT_SCENARIO
) {
602 tr
.top
+= WidgetDimensions::scaled
.vsep_wide
;
603 if (tr
.top
> tr
.bottom
) return;
605 /* Companies / AIs */
606 for (auto &pair
: _load_check_data
.companies
) {
607 SetDParam(0, pair
.first
+ 1);
608 const CompanyProperties
&c
= *pair
.second
;
609 if (!c
.name
.empty()) {
610 SetDParam(1, STR_JUST_RAW_STRING
);
611 SetDParamStr(2, c
.name
);
613 SetDParam(1, c
.name_1
);
614 SetDParam(2, c
.name_2
);
616 DrawString(tr
, STR_SAVELOAD_DETAIL_COMPANY_INDEX
);
617 tr
.top
+= GetCharacterHeight(FS_NORMAL
);
618 if (tr
.top
> tr
.bottom
) break;
624 void UpdateWidgetSize(WidgetID widget
, Dimension
&size
, [[maybe_unused
]] const Dimension
&padding
, [[maybe_unused
]] Dimension
&fill
, [[maybe_unused
]] Dimension
&resize
) override
627 case WID_SL_BACKGROUND
:
628 size
.height
= 2 * GetCharacterHeight(FS_NORMAL
) + padding
.height
;
631 case WID_SL_DRIVES_DIRECTORIES_LIST
:
632 resize
.height
= GetCharacterHeight(FS_NORMAL
);
633 size
.height
= resize
.height
* 10 + padding
.height
;
635 case WID_SL_SORT_BYNAME
:
636 case WID_SL_SORT_BYDATE
: {
637 Dimension d
= GetStringBoundingBox(this->GetWidget
<NWidgetCore
>(widget
)->widget_data
);
638 d
.width
+= padding
.width
+ Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
639 d
.height
+= padding
.height
;
640 size
= maxdim(size
, d
);
646 void OnPaint() override
648 if (_savegame_sort_dirty
) {
649 _savegame_sort_dirty
= false;
650 SortSaveGameList(this->fios_items
);
651 this->OnInvalidateData(SLIWD_FILTER_CHANGES
);
657 void OnClick([[maybe_unused
]] Point pt
, WidgetID widget
, [[maybe_unused
]] int click_count
) override
660 case WID_SL_SORT_BYNAME
: // Sort save names by name
661 _savegame_sort_order
= (_savegame_sort_order
== SORT_BY_NAME
) ?
662 SORT_BY_NAME
| SORT_DESCENDING
: SORT_BY_NAME
;
663 _savegame_sort_dirty
= true;
667 case WID_SL_SORT_BYDATE
: // Sort save names by date
668 _savegame_sort_order
= (_savegame_sort_order
== SORT_BY_DATE
) ?
669 SORT_BY_DATE
| SORT_DESCENDING
: SORT_BY_DATE
;
670 _savegame_sort_dirty
= true;
674 case WID_SL_HOME_BUTTON
: // OpenTTD 'button', jumps to OpenTTD directory
675 FiosBrowseTo(&o_dir
);
676 this->InvalidateData(SLIWD_RESCAN_FILES
);
679 case WID_SL_LOAD_BUTTON
: {
680 if (this->selected
== nullptr || _load_check_data
.HasErrors()) break;
682 _file_to_saveload
.Set(*this->selected
);
684 if (this->abstract_filetype
== FT_HEIGHTMAP
) {
687 } else if (this->abstract_filetype
== FT_TOWN_DATA
) {
690 } else if (!_load_check_data
.HasNewGrfs() || _load_check_data
.grf_compatibility
!= GLC_NOT_FOUND
|| _settings_client
.gui
.UserIsAllowedToChangeNewGRFs()) {
691 _switch_mode
= (_game_mode
== GM_EDITOR
) ? SM_LOAD_SCENARIO
: SM_LOAD_GAME
;
692 ClearErrorMessages();
698 case WID_SL_NEWGRF_INFO
:
699 if (_load_check_data
.HasNewGrfs()) {
700 ShowNewGRFSettings(false, false, false, &_load_check_data
.grfconfig
);
704 case WID_SL_MISSING_NEWGRFS
:
705 if (!_network_available
) {
706 ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE
, INVALID_STRING_ID
, WL_ERROR
);
707 } else if (_load_check_data
.HasNewGrfs()) {
708 ShowMissingContentWindow(_load_check_data
.grfconfig
);
712 case WID_SL_DRIVES_DIRECTORIES_LIST
: { // Click the listbox
713 auto it
= this->vscroll
->GetScrolledItemFromWidget(this->display_list
, pt
.y
, this, WID_SL_DRIVES_DIRECTORIES_LIST
, WidgetDimensions::scaled
.inset
.top
);
714 if (it
== this->display_list
.end()) return;
716 /* Get the corresponding non-filtered out item from the list */
717 const FiosItem
*file
= *it
;
719 if (FiosBrowseTo(file
)) {
720 /* Changed directory, need refresh. */
721 this->InvalidateData(SLIWD_RESCAN_FILES
);
725 if (click_count
== 1) {
726 if (this->selected
!= file
) {
727 this->selected
= file
;
728 _load_check_data
.Clear();
730 if (GetDetailedFileType(file
->type
) == DFT_GAME_FILE
) {
731 /* Other detailed file types cannot be checked before. */
732 SaveOrLoad(file
->name
, SLO_CHECK
, DFT_GAME_FILE
, NO_DIRECTORY
, false);
735 this->InvalidateData(SLIWD_SELECTION_CHANGES
);
737 if (this->fop
== SLO_SAVE
) {
738 /* Copy clicked name to editbox */
739 this->filename_editbox
.text
.Assign(file
->title
);
740 this->SetWidgetDirty(WID_SL_SAVE_OSK_TITLE
);
742 } else if (!_load_check_data
.HasErrors()) {
743 this->selected
= file
;
744 if (this->fop
== SLO_LOAD
) {
745 if (this->abstract_filetype
== FT_SAVEGAME
|| this->abstract_filetype
== FT_SCENARIO
|| this->abstract_filetype
== FT_TOWN_DATA
) {
746 this->OnClick(pt
, WID_SL_LOAD_BUTTON
, 1);
748 assert(this->abstract_filetype
== FT_HEIGHTMAP
);
749 _file_to_saveload
.Set(*file
);
759 case WID_SL_CONTENT_DOWNLOAD
:
760 if (!_network_available
) {
761 ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE
, INVALID_STRING_ID
, WL_ERROR
);
763 assert(this->fop
== SLO_LOAD
);
764 switch (this->abstract_filetype
) {
765 default: NOT_REACHED();
766 case FT_SCENARIO
: ShowNetworkContentListWindow(nullptr, CONTENT_TYPE_SCENARIO
); break;
767 case FT_HEIGHTMAP
: ShowNetworkContentListWindow(nullptr, CONTENT_TYPE_HEIGHTMAP
); break;
772 case WID_SL_DELETE_SELECTION
: // Delete
775 case WID_SL_SAVE_GAME
: // Save game
776 /* Note, this is also called via the OSK; and we need to lower the button. */
777 this->HandleButtonClick(WID_SL_SAVE_GAME
);
782 void OnMouseOver([[maybe_unused
]] Point pt
, WidgetID widget
) override
784 if (widget
== WID_SL_DRIVES_DIRECTORIES_LIST
) {
785 auto it
= this->vscroll
->GetScrolledItemFromWidget(this->display_list
, pt
.y
, this, WID_SL_DRIVES_DIRECTORIES_LIST
, WidgetDimensions::scaled
.inset
.top
);
786 if (it
== this->display_list
.end()) return;
788 /* Get the corresponding non-filtered out item from the list */
789 const FiosItem
*file
= *it
;
791 if (file
!= this->highlighted
) {
792 this->highlighted
= file
;
793 this->SetWidgetDirty(WID_SL_DRIVES_DIRECTORIES_LIST
);
795 } else if (this->highlighted
!= nullptr) {
796 this->highlighted
= nullptr;
797 this->SetWidgetDirty(WID_SL_DRIVES_DIRECTORIES_LIST
);
801 EventState
OnKeyPress([[maybe_unused
]] char32_t key
, uint16_t keycode
) override
803 if (keycode
== WKC_ESC
) {
808 return ES_NOT_HANDLED
;
811 void OnTimeout() override
813 /* Widgets WID_SL_DELETE_SELECTION and WID_SL_SAVE_GAME only exist when saving to a file. */
814 if (this->fop
!= SLO_SAVE
) return;
816 if (this->IsWidgetLowered(WID_SL_DELETE_SELECTION
)) { // Delete button clicked
817 if (!FiosDelete(this->filename_editbox
.text
.buf
)) {
818 ShowErrorMessage(STR_ERROR_UNABLE_TO_DELETE_FILE
, INVALID_STRING_ID
, WL_ERROR
);
820 this->InvalidateData(SLIWD_RESCAN_FILES
);
821 /* Reset file name to current date on successful delete */
822 if (this->abstract_filetype
== FT_SAVEGAME
) GenerateFileName();
824 } else if (this->IsWidgetLowered(WID_SL_SAVE_GAME
)) { // Save button clicked
825 if (this->abstract_filetype
== FT_SAVEGAME
|| this->abstract_filetype
== FT_SCENARIO
) {
826 _file_to_saveload
.name
= FiosMakeSavegameName(this->filename_editbox
.text
.buf
);
827 if (FioCheckFileExists(_file_to_saveload
.name
, Subdirectory::SAVE_DIR
)) {
828 ShowQuery(STR_SAVELOAD_OVERWRITE_TITLE
, STR_SAVELOAD_OVERWRITE_WARNING
, this, SaveLoadWindow::SaveGameConfirmationCallback
);
830 _switch_mode
= SM_SAVE_GAME
;
833 _file_to_saveload
.name
= FiosMakeHeightmapName(this->filename_editbox
.text
.buf
);
834 if (FioCheckFileExists(_file_to_saveload
.name
, Subdirectory::SAVE_DIR
)) {
835 ShowQuery(STR_SAVELOAD_OVERWRITE_TITLE
, STR_SAVELOAD_OVERWRITE_WARNING
, this, SaveLoadWindow::SaveHeightmapConfirmationCallback
);
837 _switch_mode
= SM_SAVE_HEIGHTMAP
;
841 /* In the editor set up the vehicle engines correctly (date might have changed) */
842 if (_game_mode
== GM_EDITOR
) StartupEngines();
846 void OnResize() override
848 this->vscroll
->SetCapacityFromWidget(this, WID_SL_DRIVES_DIRECTORIES_LIST
);
851 void BuildDisplayList()
854 this->display_list
.clear();
855 this->display_list
.reserve(this->fios_items
.size());
857 if (this->string_filter
.IsEmpty()) {
858 /* We don't filter anything out if the filter editbox is empty */
859 for (auto &it
: this->fios_items
) {
860 this->display_list
.push_back(&it
);
863 for (auto &it
: this->fios_items
) {
864 this->string_filter
.ResetState();
865 this->string_filter
.AddLine(it
.title
);
866 /* We set the vector to show this fios element as filtered depending on the result of the filter */
867 if (this->string_filter
.GetState()) {
868 this->display_list
.push_back(&it
);
869 } else if (&it
== this->selected
) {
870 /* The selected element has been filtered out */
871 this->selected
= nullptr;
872 this->OnInvalidateData(SLIWD_SELECTION_CHANGES
);
877 this->vscroll
->SetCount(this->display_list
.size());
881 * Some data on this window has become invalid.
882 * @param data Information about the changed data.
883 * @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.
885 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
888 case SLIWD_RESCAN_FILES
:
890 this->selected
= nullptr;
891 _load_check_data
.Clear();
892 if (!gui_scope
) break;
894 _fios_path_changed
= true;
895 this->fios_items
.BuildFileList(this->abstract_filetype
, this->fop
, true);
896 this->selected
= nullptr;
897 _load_check_data
.Clear();
899 /* We reset the files filtered */
900 this->OnInvalidateData(SLIWD_FILTER_CHANGES
);
904 case SLIWD_SELECTION_CHANGES
:
905 /* Selection changes */
906 if (!gui_scope
) break;
908 if (this->fop
!= SLO_LOAD
) break;
910 switch (this->abstract_filetype
) {
913 this->SetWidgetDisabledState(WID_SL_LOAD_BUTTON
, this->selected
== nullptr || _load_check_data
.HasErrors());
918 bool disabled
= this->selected
== nullptr || _load_check_data
.HasErrors();
919 if (!_settings_client
.gui
.UserIsAllowedToChangeNewGRFs()) {
920 disabled
|= _load_check_data
.HasNewGrfs() && _load_check_data
.grf_compatibility
== GLC_NOT_FOUND
;
922 this->SetWidgetDisabledState(WID_SL_LOAD_BUTTON
, disabled
);
923 this->SetWidgetDisabledState(WID_SL_NEWGRF_INFO
, !_load_check_data
.HasNewGrfs());
924 this->SetWidgetDisabledState(WID_SL_MISSING_NEWGRFS
,
925 !_load_check_data
.HasNewGrfs() || _load_check_data
.grf_compatibility
== GLC_ALL_GOOD
);
934 case SLIWD_FILTER_CHANGES
:
935 this->BuildDisplayList();
940 void OnEditboxChanged(WidgetID wid
) override
942 if (wid
== WID_SL_FILTER
) {
943 this->string_filter
.SetFilterTerm(this->filter_editbox
.text
.buf
);
944 this->InvalidateData(SLIWD_FILTER_CHANGES
);
949 /** Load game/scenario */
950 static WindowDesc
_load_dialog_desc(
951 WDP_CENTER
, "load_game", 500, 294,
952 WC_SAVELOAD
, WC_NONE
,
954 _nested_load_dialog_widgets
957 /** Load heightmap */
958 static WindowDesc
_load_heightmap_dialog_desc(
959 WDP_CENTER
, "load_heightmap", 257, 320,
960 WC_SAVELOAD
, WC_NONE
,
962 _nested_load_heightmap_dialog_widgets
965 /** Load town data */
966 static WindowDesc
_load_town_data_dialog_desc(
967 WDP_CENTER
, "load_town_data", 257, 320,
968 WC_SAVELOAD
, WC_NONE
,
970 _nested_load_town_data_dialog_widgets
973 /** Save game/scenario */
974 static WindowDesc
_save_dialog_desc(
975 WDP_CENTER
, "save_game", 500, 294,
976 WC_SAVELOAD
, WC_NONE
,
978 _nested_save_dialog_widgets
982 * Launch save/load dialog in the given mode.
983 * @param abstract_filetype Kind of file to handle.
984 * @param fop File operation to perform (load or save).
986 void ShowSaveLoadDialog(AbstractFileType abstract_filetype
, SaveLoadOperation fop
)
988 CloseWindowById(WC_SAVELOAD
, 0);
990 if (fop
== SLO_SAVE
) {
991 new SaveLoadWindow(_save_dialog_desc
, abstract_filetype
, fop
);
993 /* Dialogue for loading a file. */
994 switch (abstract_filetype
) {
996 new SaveLoadWindow(_load_heightmap_dialog_desc
, abstract_filetype
, fop
);
1000 new SaveLoadWindow(_load_town_data_dialog_desc
, abstract_filetype
, fop
);
1004 new SaveLoadWindow(_load_dialog_desc
, abstract_filetype
, fop
);