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"
26 #include "date_func.h"
27 #include "core/geometry_func.hpp"
29 #include "stringfilter_type.h"
32 #include "widgets/fios_widget.h"
34 #include "table/sprites.h"
35 #include "table/strings.h"
37 #include "safeguards.h"
39 LoadCheckData _load_check_data
; ///< Data loaded from save during SL_LOAD_CHECK.
41 static bool _fios_path_changed
;
42 static bool _savegame_sort_dirty
;
48 void LoadCheckData::Clear()
50 this->checkable
= false;
51 this->error
= INVALID_STRING_ID
;
52 free(this->error_data
);
53 this->error_data
= nullptr;
55 this->map_size_x
= this->map_size_y
= 256; // Default for old savegames which do not store mapsize.
56 this->current_date
= 0;
59 for (auto &pair
: this->companies
) {
64 GamelogFree(this->gamelog_action
, this->gamelog_actions
);
65 this->gamelog_action
= nullptr;
66 this->gamelog_actions
= 0;
68 ClearGRFConfigList(&this->grfconfig
);
71 /** Load game/scenario with optional content download */
72 static const NWidgetPart _nested_load_dialog_widgets
[] = {
73 NWidget(NWID_HORIZONTAL
),
74 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
75 NWidget(WWT_CAPTION
, COLOUR_GREY
, WID_SL_CAPTION
),
76 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
78 /* Current directory and free space */
79 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_SL_BACKGROUND
), SetFill(1, 0), SetResize(1, 0), EndContainer(),
81 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
82 /* Left side : filter box and available files */
83 NWidget(NWID_VERTICAL
),
84 /* Filter box with label */
85 NWidget(WWT_PANEL
, COLOUR_GREY
), SetFill(1, 1), SetResize(1, 1),
86 NWidget(NWID_HORIZONTAL
), SetPadding(WidgetDimensions::unscaled
.framerect
.top
, 0, WidgetDimensions::unscaled
.framerect
.bottom
, 0),
87 SetPIP(WidgetDimensions::unscaled
.frametext
.left
, WidgetDimensions::unscaled
.frametext
.right
, 0),
88 NWidget(WWT_TEXT
, COLOUR_GREY
), SetFill(0, 1), SetDataTip(STR_SAVELOAD_FILTER_TITLE
, STR_NULL
),
89 NWidget(WWT_EDITBOX
, COLOUR_GREY
, WID_SL_FILTER
), SetFill(1, 0), SetMinimalSize(50, 12), SetResize(1, 0),
90 SetDataTip(STR_LIST_FILTER_OSKTITLE
, STR_LIST_FILTER_TOOLTIP
),
94 NWidget(NWID_HORIZONTAL
),
95 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
96 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),
97 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),
99 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_SL_HOME_BUTTON
), SetMinimalSize(12, 12), SetDataTip(SPR_HOUSE_ICON
, STR_SAVELOAD_HOME_BUTTON
),
102 NWidget(NWID_HORIZONTAL
),
103 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_SL_FILE_BACKGROUND
),
104 NWidget(WWT_INSET
, COLOUR_GREY
, WID_SL_DRIVES_DIRECTORIES_LIST
), SetFill(1, 1), SetPadding(2, 2, 2, 2),
105 SetDataTip(0x0, STR_SAVELOAD_LIST_TOOLTIP
), SetResize(1, 10), SetScrollbar(WID_SL_SCROLLBAR
), EndContainer(),
107 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_SL_SCROLLBAR
),
109 /* Online Content button */
110 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_SL_CONTENT_DOWNLOAD_SEL
),
111 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SL_CONTENT_DOWNLOAD
), SetResize(1, 0),
112 SetDataTip(STR_INTRO_ONLINE_CONTENT
, STR_INTRO_TOOLTIP_ONLINE_CONTENT
),
116 /* Right side : game details */
117 NWidget(NWID_VERTICAL
),
118 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_SL_DETAILS
), SetResize(1, 1), SetFill(1, 1),
120 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),
121 NWidget(NWID_HORIZONTAL
),
122 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
123 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SL_NEWGRF_INFO
), SetDataTip(STR_INTRO_NEWGRF_SETTINGS
, STR_NULL
), SetFill(1, 0), SetResize(1, 0),
124 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SL_LOAD_BUTTON
), SetDataTip(STR_SAVELOAD_LOAD_BUTTON
, STR_SAVELOAD_LOAD_TOOLTIP
), SetFill(1, 0), SetResize(1, 0),
126 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
132 /** Load heightmap with content download */
133 static const NWidgetPart _nested_load_heightmap_dialog_widgets
[] = {
134 NWidget(NWID_HORIZONTAL
),
135 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
136 NWidget(WWT_CAPTION
, COLOUR_GREY
, WID_SL_CAPTION
),
137 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
139 /* Current directory and free space */
140 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_SL_BACKGROUND
), SetFill(1, 0), SetResize(1, 0), EndContainer(),
142 /* Filter box with label */
143 NWidget(WWT_PANEL
, COLOUR_GREY
), SetFill(1, 1), SetResize(1, 1),
144 NWidget(NWID_HORIZONTAL
), SetPadding(WidgetDimensions::unscaled
.framerect
.top
, 0, WidgetDimensions::unscaled
.framerect
.bottom
, 0),
145 SetPIP(WidgetDimensions::unscaled
.frametext
.left
, WidgetDimensions::unscaled
.frametext
.right
, 0),
146 NWidget(WWT_TEXT
, COLOUR_GREY
), SetFill(0, 1), SetDataTip(STR_SAVELOAD_FILTER_TITLE
, STR_NULL
),
147 NWidget(WWT_EDITBOX
, COLOUR_GREY
, WID_SL_FILTER
), SetFill(1, 0), SetMinimalSize(50, 12), SetResize(1, 0),
148 SetDataTip(STR_LIST_FILTER_OSKTITLE
, STR_LIST_FILTER_TOOLTIP
),
152 NWidget(NWID_HORIZONTAL
),
153 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
154 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),
155 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),
157 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_SL_HOME_BUTTON
), SetMinimalSize(12, 12), SetDataTip(SPR_HOUSE_ICON
, STR_SAVELOAD_HOME_BUTTON
),
160 NWidget(NWID_HORIZONTAL
),
161 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_SL_FILE_BACKGROUND
),
162 NWidget(WWT_INSET
, COLOUR_GREY
, WID_SL_DRIVES_DIRECTORIES_LIST
), SetFill(1, 1), SetPadding(2, 2, 2, 2),
163 SetDataTip(0x0, STR_SAVELOAD_LIST_TOOLTIP
), SetResize(1, 10), SetScrollbar(WID_SL_SCROLLBAR
), EndContainer(),
165 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_SL_SCROLLBAR
),
167 /* Online Content and Load button */
168 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
169 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SL_CONTENT_DOWNLOAD
), SetResize(1, 0), SetFill(1, 0),
170 SetDataTip(STR_INTRO_ONLINE_CONTENT
, STR_INTRO_TOOLTIP_ONLINE_CONTENT
),
171 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SL_LOAD_BUTTON
), SetResize(1, 0), SetFill(1, 0),
172 SetDataTip(STR_SAVELOAD_LOAD_BUTTON
, STR_SAVELOAD_LOAD_HEIGHTMAP_TOOLTIP
),
173 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
177 /** Save game/scenario */
178 static const NWidgetPart _nested_save_dialog_widgets
[] = {
179 NWidget(NWID_HORIZONTAL
),
180 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
181 NWidget(WWT_CAPTION
, COLOUR_GREY
, WID_SL_CAPTION
),
182 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
184 /* Current directory and free space */
185 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_SL_BACKGROUND
), SetFill(1, 0), SetResize(1, 0), 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), SetMinimalSize(50, 12), SetResize(1, 0),
195 SetDataTip(STR_LIST_FILTER_OSKTITLE
, STR_LIST_FILTER_TOOLTIP
),
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),
204 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_SL_HOME_BUTTON
), SetMinimalSize(12, 12), SetDataTip(SPR_HOUSE_ICON
, STR_SAVELOAD_HOME_BUTTON
),
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
), EndContainer(),
212 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_SL_SCROLLBAR
),
214 NWidget(WWT_PANEL
, COLOUR_GREY
),
215 NWidget(WWT_EDITBOX
, COLOUR_GREY
, WID_SL_SAVE_OSK_TITLE
), SetPadding(2, 2, 2, 2), SetFill(1, 0), SetResize(1, 0),
216 SetDataTip(STR_SAVELOAD_OSKTITLE
, STR_SAVELOAD_EDITBOX_TOOLTIP
),
218 /* Save/delete buttons */
219 NWidget(NWID_HORIZONTAL
),
220 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SL_DELETE_SELECTION
), SetDataTip(STR_SAVELOAD_DELETE_BUTTON
, STR_SAVELOAD_DELETE_TOOLTIP
), SetFill(1, 0), SetResize(1, 0),
221 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_SL_SAVE_GAME
), SetDataTip(STR_SAVELOAD_SAVE_BUTTON
, STR_SAVELOAD_SAVE_TOOLTIP
), SetFill(1, 0), SetResize(1, 0),
225 /* Right side : game details */
226 NWidget(NWID_VERTICAL
),
227 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_SL_DETAILS
), SetResize(1, 1), SetFill(1, 1), EndContainer(),
228 NWidget(NWID_HORIZONTAL
),
229 NWidget(WWT_PANEL
, COLOUR_GREY
), SetResize(1, 0), SetFill(1, 1), EndContainer(),
230 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
236 /** Text colours of #DetailedFileType fios entries in the window. */
237 static const TextColour _fios_colours
[] = {
238 TC_LIGHT_BROWN
, // DFT_OLD_GAME_FILE
239 TC_ORANGE
, // DFT_GAME_FILE
240 TC_YELLOW
, // DFT_HEIGHTMAP_BMP
241 TC_ORANGE
, // DFT_HEIGHTMAP_PNG
242 TC_LIGHT_BLUE
, // DFT_FIOS_DRIVE
243 TC_DARK_GREEN
, // DFT_FIOS_PARENT
244 TC_DARK_GREEN
, // DFT_FIOS_DIR
245 TC_ORANGE
, // DFT_FIOS_DIRECT
250 * Sort the collected list save games prior to displaying it in the save/load gui.
251 * @param[in,out] file_list List of save game files found in the directory.
253 static void SortSaveGameList(FileList
&file_list
)
255 size_t sort_start
= 0;
258 /* Directories are always above the files (FIOS_TYPE_DIR)
259 * Drives (A:\ (windows only) are always under the files (FIOS_TYPE_DRIVE)
260 * Only sort savegames/scenarios, not directories
262 for (const auto &item
: file_list
) {
264 case FIOS_TYPE_DIR
: sort_start
++; break;
265 case FIOS_TYPE_PARENT
: sort_start
++; break;
266 case FIOS_TYPE_DRIVE
: sort_end
++; break;
271 std::sort(file_list
.begin() + sort_start
, file_list
.end() - sort_end
);
274 struct SaveLoadWindow
: public Window
{
276 static const uint EDITBOX_MAX_SIZE
= 50;
278 QueryString filename_editbox
; ///< Filename editbox.
279 AbstractFileType abstract_filetype
; /// Type of file to select.
280 SaveLoadOperation fop
; ///< File operation to perform.
281 FileList fios_items
; ///< Save game list.
282 FiosItem o_dir
; ///< Original dir (home dir for this browser)
283 const FiosItem
*selected
; ///< Selected game in #fios_items, or \c nullptr.
284 const FiosItem
*highlighted
; ///< Item in fios_items highlighted by mouse pointer, or \c nullptr.
287 StringFilter string_filter
; ///< Filter for available games.
288 QueryString filter_editbox
; ///< Filter editbox;
289 std::vector
<bool> fios_items_shown
; ///< Map of the filtered out fios items
291 static void SaveGameConfirmationCallback(Window
*w
, bool confirmed
)
293 /* File name has already been written to _file_to_saveload */
294 if (confirmed
) _switch_mode
= SM_SAVE_GAME
;
297 static void SaveHeightmapConfirmationCallback(Window
*w
, bool confirmed
)
299 /* File name has already been written to _file_to_saveload */
300 if (confirmed
) _switch_mode
= SM_SAVE_HEIGHTMAP
;
305 /** Generate a default save filename. */
306 void GenerateFileName()
308 GenerateDefaultSaveName(this->filename_editbox
.text
.buf
, &this->filename_editbox
.text
.buf
[this->filename_editbox
.text
.max_bytes
- 1]);
309 this->filename_editbox
.text
.UpdateSize();
312 SaveLoadWindow(WindowDesc
*desc
, AbstractFileType abstract_filetype
, SaveLoadOperation fop
)
313 : Window(desc
), filename_editbox(64), abstract_filetype(abstract_filetype
), fop(fop
), filter_editbox(EDITBOX_MAX_SIZE
)
315 assert(this->fop
== SLO_SAVE
|| this->fop
== SLO_LOAD
);
317 /* For saving, construct an initial file name. */
318 if (this->fop
== SLO_SAVE
) {
319 switch (this->abstract_filetype
) {
321 this->GenerateFileName();
326 this->filename_editbox
.text
.Assign("UNNAMED");
333 this->querystrings
[WID_SL_SAVE_OSK_TITLE
] = &this->filename_editbox
;
334 this->filename_editbox
.ok_button
= WID_SL_SAVE_GAME
;
336 this->CreateNestedTree(true);
337 if (this->fop
== SLO_LOAD
&& this->abstract_filetype
== FT_SAVEGAME
) {
338 this->GetWidget
<NWidgetStacked
>(WID_SL_CONTENT_DOWNLOAD_SEL
)->SetDisplayedPlane(SZSP_HORIZONTAL
);
341 /* Select caption string of the window. */
342 StringID caption_string
;
343 switch (this->abstract_filetype
) {
345 caption_string
= (this->fop
== SLO_SAVE
) ? STR_SAVELOAD_SAVE_CAPTION
: STR_SAVELOAD_LOAD_CAPTION
;
349 caption_string
= (this->fop
== SLO_SAVE
) ? STR_SAVELOAD_SAVE_SCENARIO
: STR_SAVELOAD_LOAD_SCENARIO
;
353 caption_string
= (this->fop
== SLO_SAVE
) ? STR_SAVELOAD_SAVE_HEIGHTMAP
: STR_SAVELOAD_LOAD_HEIGHTMAP
;
359 this->GetWidget
<NWidgetCore
>(WID_SL_CAPTION
)->widget_data
= caption_string
;
361 this->vscroll
= this->GetScrollbar(WID_SL_SCROLLBAR
);
362 this->FinishInitNested(0);
364 this->LowerWidget(WID_SL_DRIVES_DIRECTORIES_LIST
);
365 this->querystrings
[WID_SL_FILTER
] = &this->filter_editbox
;
366 this->filter_editbox
.cancel_button
= QueryString::ACTION_CLEAR
;
368 /* pause is only used in single-player, non-editor mode, non-menu mode. It
369 * will be unpaused in the WE_DESTROY event handler. */
370 if (_game_mode
!= GM_MENU
&& !_networking
&& _game_mode
!= GM_EDITOR
) {
371 Command
<CMD_PAUSE
>::Post(PM_PAUSED_SAVELOAD
, true);
373 SetObjectToPlace(SPR_CURSOR_ZZZ
, PAL_NONE
, HT_NONE
, WC_MAIN_WINDOW
, 0);
375 this->OnInvalidateData(SLIWD_RESCAN_FILES
);
377 ResetObjectToPlace();
379 /* Select the initial directory. */
380 o_dir
.type
= FIOS_TYPE_DIRECT
;
382 switch (this->abstract_filetype
) {
384 dir
= FioFindDirectory(SAVE_DIR
);
388 dir
= FioFindDirectory(SCENARIO_DIR
);
392 dir
= FioFindDirectory(HEIGHTMAP_DIR
);
398 strecpy(o_dir
.name
, dir
.c_str(), lastof(o_dir
.name
));
402 /* Focus the edit box by default in the save window */
403 this->SetFocusedWidget(WID_SL_SAVE_OSK_TITLE
);
407 this->SetFocusedWidget(WID_SL_FILTER
);
411 void Close() override
413 /* pause is only used in single-player, non-editor mode, non menu mode */
414 if (!_networking
&& _game_mode
!= GM_EDITOR
&& _game_mode
!= GM_MENU
) {
415 Command
<CMD_PAUSE
>::Post(PM_PAUSED_SAVELOAD
, false);
417 this->Window::Close();
420 void DrawWidget(const Rect
&r
, int widget
) const override
423 case WID_SL_SORT_BYNAME
:
424 case WID_SL_SORT_BYDATE
:
425 if (((_savegame_sort_order
& SORT_BY_NAME
) != 0) == (widget
== WID_SL_SORT_BYNAME
)) {
426 this->DrawSortButtonState(widget
, _savegame_sort_order
& SORT_DESCENDING
? SBS_DOWN
: SBS_UP
);
430 case WID_SL_BACKGROUND
: {
431 static const char *path
= nullptr;
432 static StringID str
= STR_ERROR_UNABLE_TO_READ_DRIVE
;
433 static uint64 tot
= 0;
435 if (_fios_path_changed
) {
436 str
= FiosGetDescText(&path
, &tot
);
437 _fios_path_changed
= false;
440 Rect ir
= r
.Shrink(WidgetDimensions::scaled
.framerect
);
442 if (str
!= STR_ERROR_UNABLE_TO_READ_DRIVE
) SetDParam(0, tot
);
443 DrawString(ir
.left
, ir
.right
, ir
.top
+ FONT_HEIGHT_NORMAL
, str
);
444 DrawString(ir
.left
, ir
.right
, ir
.top
, path
, TC_BLACK
);
448 case WID_SL_DRIVES_DIRECTORIES_LIST
: {
449 const Rect br
= r
.Shrink(WidgetDimensions::scaled
.bevel
);
450 GfxFillRect(br
, PC_BLACK
);
452 Rect tr
= r
.Shrink(WidgetDimensions::scaled
.inset
).WithHeight(this->resize
.step_height
);
453 uint scroll_pos
= this->vscroll
->GetPosition();
454 for (uint row
= 0; row
< this->fios_items
.size() && tr
.top
< br
.bottom
; row
++) {
455 if (!this->fios_items_shown
[row
]) {
456 /* The current item is filtered out : we do not show it */
460 if (row
< scroll_pos
) continue;
461 const FiosItem
*item
= &this->fios_items
[row
];
463 if (item
== this->selected
) {
464 GfxFillRect(br
.left
, tr
.top
, br
.right
, tr
.bottom
, PC_DARK_BLUE
);
465 } else if (item
== this->highlighted
) {
466 GfxFillRect(br
.left
, tr
.top
, br
.right
, tr
.bottom
, PC_VERY_DARK_BLUE
);
468 DrawString(tr
, item
->title
, _fios_colours
[GetDetailedFileType(item
->type
)]);
469 tr
= tr
.Translate(0, this->resize
.step_height
);
475 this->DrawDetails(r
);
480 void DrawDetails(const Rect
&r
) const
483 int HEADER_HEIGHT
= FONT_HEIGHT_NORMAL
+ WidgetDimensions::scaled
.frametext
.Vertical();
485 Rect hr
= r
.WithHeight(HEADER_HEIGHT
).Shrink(WidgetDimensions::scaled
.frametext
);
486 Rect tr
= r
.Shrink(WidgetDimensions::scaled
.frametext
);
487 tr
.top
+= HEADER_HEIGHT
;
489 /* Create the nice grayish rectangle at the details top */
490 GfxFillRect(r
.WithHeight(HEADER_HEIGHT
).Shrink(WidgetDimensions::scaled
.bevel
.left
, WidgetDimensions::scaled
.bevel
.top
, WidgetDimensions::scaled
.bevel
.right
, 0), PC_GREY
);
491 DrawString(hr
.left
, hr
.right
, hr
.top
, STR_SAVELOAD_DETAIL_CAPTION
, TC_FROMSTRING
, SA_HOR_CENTER
);
493 if (this->selected
== nullptr) return;
496 tr
.bottom
-= FONT_HEIGHT_NORMAL
- 1;
497 if (tr
.top
> tr
.bottom
) return;
499 if (!_load_check_data
.checkable
) {
500 /* Old savegame, no information available */
501 DrawString(tr
, STR_SAVELOAD_DETAIL_NOT_AVAILABLE
);
502 tr
.top
+= FONT_HEIGHT_NORMAL
;
503 } else if (_load_check_data
.error
!= INVALID_STRING_ID
) {
504 /* Incompatible / broken savegame */
505 SetDParamStr(0, _load_check_data
.error_data
);
506 tr
.top
= DrawStringMultiLine(tr
, _load_check_data
.error
, TC_RED
);
509 SetDParam(0, _load_check_data
.map_size_x
);
510 SetDParam(1, _load_check_data
.map_size_y
);
511 DrawString(tr
, STR_NETWORK_SERVER_LIST_MAP_SIZE
);
512 tr
.top
+= FONT_HEIGHT_NORMAL
;
513 if (tr
.top
> tr
.bottom
) return;
516 byte landscape
= _load_check_data
.settings
.game_creation
.landscape
;
517 if (landscape
< NUM_LANDSCAPE
) {
518 SetDParam(0, STR_CLIMATE_TEMPERATE_LANDSCAPE
+ landscape
);
519 DrawString(tr
, STR_NETWORK_SERVER_LIST_LANDSCAPE
);
520 tr
.top
+= FONT_HEIGHT_NORMAL
;
523 tr
.top
+= WidgetDimensions::scaled
.vsep_normal
;
524 if (tr
.top
> tr
.bottom
) return;
526 /* Start date (if available) */
527 if (_load_check_data
.settings
.game_creation
.starting_year
!= 0) {
528 SetDParam(0, ConvertYMDToDate(_load_check_data
.settings
.game_creation
.starting_year
, 0, 1));
529 DrawString(tr
, STR_NETWORK_SERVER_LIST_START_DATE
);
530 tr
.top
+= FONT_HEIGHT_NORMAL
;
532 if (tr
.top
> tr
.bottom
) return;
534 /* Hide current date for scenarios */
535 if (this->abstract_filetype
!= FT_SCENARIO
) {
537 SetDParam(0, _load_check_data
.current_date
);
538 DrawString(tr
, STR_NETWORK_SERVER_LIST_CURRENT_DATE
);
539 tr
.top
+= FONT_HEIGHT_NORMAL
;
542 /* Hide the NewGRF stuff when saving. We also hide the button. */
543 if (this->fop
== SLO_LOAD
&& (this->abstract_filetype
== FT_SAVEGAME
|| this->abstract_filetype
== FT_SCENARIO
)) {
544 tr
.top
+= WidgetDimensions::scaled
.vsep_normal
;
545 if (tr
.top
> tr
.bottom
) return;
547 /* NewGrf compatibility */
548 SetDParam(0, _load_check_data
.grfconfig
== nullptr ? STR_NEWGRF_LIST_NONE
:
549 STR_NEWGRF_LIST_ALL_FOUND
+ _load_check_data
.grf_compatibility
);
550 DrawString(tr
, STR_SAVELOAD_DETAIL_GRFSTATUS
);
551 tr
.top
+= FONT_HEIGHT_NORMAL
;
553 if (tr
.top
> tr
.bottom
) return;
555 /* Hide the company stuff for scenarios */
556 if (this->abstract_filetype
!= FT_SCENARIO
) {
557 tr
.top
+= WidgetDimensions::scaled
.vsep_wide
;
558 if (tr
.top
> tr
.bottom
) return;
560 /* Companies / AIs */
561 for (auto &pair
: _load_check_data
.companies
) {
562 SetDParam(0, pair
.first
+ 1);
563 const CompanyProperties
&c
= *pair
.second
;
564 if (!c
.name
.empty()) {
565 SetDParam(1, STR_JUST_RAW_STRING
);
566 SetDParamStr(2, c
.name
);
568 SetDParam(1, c
.name_1
);
569 SetDParam(2, c
.name_2
);
571 DrawString(tr
, STR_SAVELOAD_DETAIL_COMPANY_INDEX
);
572 tr
.top
+= FONT_HEIGHT_NORMAL
;
573 if (tr
.top
> tr
.bottom
) break;
579 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
582 case WID_SL_BACKGROUND
:
583 size
->height
= 2 * FONT_HEIGHT_NORMAL
+ padding
.height
;
586 case WID_SL_DRIVES_DIRECTORIES_LIST
:
587 resize
->height
= FONT_HEIGHT_NORMAL
;
588 size
->height
= resize
->height
* 10 + padding
.height
;
590 case WID_SL_SORT_BYNAME
:
591 case WID_SL_SORT_BYDATE
: {
592 Dimension d
= GetStringBoundingBox(this->GetWidget
<NWidgetCore
>(widget
)->widget_data
);
593 d
.width
+= padding
.width
+ Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
594 d
.height
+= padding
.height
;
595 *size
= maxdim(*size
, d
);
601 void OnPaint() override
603 if (_savegame_sort_dirty
) {
604 _savegame_sort_dirty
= false;
605 SortSaveGameList(this->fios_items
);
606 this->OnInvalidateData(SLIWD_FILTER_CHANGES
);
612 void OnClick(Point pt
, int widget
, int click_count
) override
615 case WID_SL_SORT_BYNAME
: // Sort save names by name
616 _savegame_sort_order
= (_savegame_sort_order
== SORT_BY_NAME
) ?
617 SORT_BY_NAME
| SORT_DESCENDING
: SORT_BY_NAME
;
618 _savegame_sort_dirty
= true;
622 case WID_SL_SORT_BYDATE
: // Sort save names by date
623 _savegame_sort_order
= (_savegame_sort_order
== SORT_BY_DATE
) ?
624 SORT_BY_DATE
| SORT_DESCENDING
: SORT_BY_DATE
;
625 _savegame_sort_dirty
= true;
629 case WID_SL_HOME_BUTTON
: // OpenTTD 'button', jumps to OpenTTD directory
630 FiosBrowseTo(&o_dir
);
631 this->InvalidateData(SLIWD_RESCAN_FILES
);
634 case WID_SL_LOAD_BUTTON
: {
635 if (this->selected
== nullptr || _load_check_data
.HasErrors()) break;
637 const char *name
= FiosBrowseTo(this->selected
);
638 _file_to_saveload
.SetMode(this->selected
->type
);
639 _file_to_saveload
.SetName(name
);
640 _file_to_saveload
.SetTitle(this->selected
->title
);
642 if (this->abstract_filetype
== FT_HEIGHTMAP
) {
645 } else if (!_load_check_data
.HasNewGrfs() || _load_check_data
.grf_compatibility
!= GLC_NOT_FOUND
|| _settings_client
.gui
.UserIsAllowedToChangeNewGRFs()) {
646 _switch_mode
= (_game_mode
== GM_EDITOR
) ? SM_LOAD_SCENARIO
: SM_LOAD_GAME
;
647 ClearErrorMessages();
653 case WID_SL_NEWGRF_INFO
:
654 if (_load_check_data
.HasNewGrfs()) {
655 ShowNewGRFSettings(false, false, false, &_load_check_data
.grfconfig
);
659 case WID_SL_MISSING_NEWGRFS
:
660 if (!_network_available
) {
661 ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE
, INVALID_STRING_ID
, WL_ERROR
);
662 } else if (_load_check_data
.HasNewGrfs()) {
663 ShowMissingContentWindow(_load_check_data
.grfconfig
);
667 case WID_SL_DRIVES_DIRECTORIES_LIST
: { // Click the listbox
668 int y
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_SL_DRIVES_DIRECTORIES_LIST
, WidgetDimensions::scaled
.inset
.top
);
669 if (y
== INT_MAX
) return;
671 /* Get the corresponding non-filtered out item from the list */
674 if (!this->fios_items_shown
[i
]) y
++;
677 const FiosItem
*file
= &this->fios_items
[y
];
679 const char *name
= FiosBrowseTo(file
);
680 if (name
== nullptr) {
681 /* Changed directory, need refresh. */
682 this->InvalidateData(SLIWD_RESCAN_FILES
);
686 if (click_count
== 1) {
687 if (this->selected
!= file
) {
688 this->selected
= file
;
689 _load_check_data
.Clear();
691 if (GetDetailedFileType(file
->type
) == DFT_GAME_FILE
) {
692 /* Other detailed file types cannot be checked before. */
693 SaveOrLoad(name
, SLO_CHECK
, DFT_GAME_FILE
, NO_DIRECTORY
, false);
696 this->InvalidateData(SLIWD_SELECTION_CHANGES
);
698 if (this->fop
== SLO_SAVE
) {
699 /* Copy clicked name to editbox */
700 this->filename_editbox
.text
.Assign(file
->title
);
701 this->SetWidgetDirty(WID_SL_SAVE_OSK_TITLE
);
703 } else if (!_load_check_data
.HasErrors()) {
704 this->selected
= file
;
705 if (this->fop
== SLO_LOAD
) {
706 if (this->abstract_filetype
== FT_SAVEGAME
|| this->abstract_filetype
== FT_SCENARIO
) {
707 this->OnClick(pt
, WID_SL_LOAD_BUTTON
, 1);
709 assert(this->abstract_filetype
== FT_HEIGHTMAP
);
710 _file_to_saveload
.SetMode(file
->type
);
711 _file_to_saveload
.SetName(name
);
712 _file_to_saveload
.SetTitle(file
->title
);
722 case WID_SL_CONTENT_DOWNLOAD
:
723 if (!_network_available
) {
724 ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE
, INVALID_STRING_ID
, WL_ERROR
);
726 assert(this->fop
== SLO_LOAD
);
727 switch (this->abstract_filetype
) {
728 default: NOT_REACHED();
729 case FT_SCENARIO
: ShowNetworkContentListWindow(nullptr, CONTENT_TYPE_SCENARIO
); break;
730 case FT_HEIGHTMAP
: ShowNetworkContentListWindow(nullptr, CONTENT_TYPE_HEIGHTMAP
); break;
735 case WID_SL_DELETE_SELECTION
: // Delete
738 case WID_SL_SAVE_GAME
: // Save game
739 /* Note, this is also called via the OSK; and we need to lower the button. */
740 this->HandleButtonClick(WID_SL_SAVE_GAME
);
745 void OnMouseOver(Point pt
, int widget
) override
747 if (widget
== WID_SL_DRIVES_DIRECTORIES_LIST
) {
748 int y
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_SL_DRIVES_DIRECTORIES_LIST
, WidgetDimensions::scaled
.inset
.top
);
749 if (y
== INT_MAX
) return;
751 /* Get the corresponding non-filtered out item from the list */
754 if (!this->fios_items_shown
[i
]) y
++;
757 const FiosItem
*file
= &this->fios_items
[y
];
759 if (file
!= this->highlighted
) {
760 this->highlighted
= file
;
761 this->SetWidgetDirty(WID_SL_DRIVES_DIRECTORIES_LIST
);
763 } else if (this->highlighted
!= nullptr) {
764 this->highlighted
= nullptr;
765 this->SetWidgetDirty(WID_SL_DRIVES_DIRECTORIES_LIST
);
769 EventState
OnKeyPress(WChar key
, uint16 keycode
) override
771 if (keycode
== WKC_ESC
) {
776 return ES_NOT_HANDLED
;
779 void OnTimeout() override
781 /* Widgets WID_SL_DELETE_SELECTION and WID_SL_SAVE_GAME only exist when saving to a file. */
782 if (this->fop
!= SLO_SAVE
) return;
784 if (this->IsWidgetLowered(WID_SL_DELETE_SELECTION
)) { // Delete button clicked
785 if (!FiosDelete(this->filename_editbox
.text
.buf
)) {
786 ShowErrorMessage(STR_ERROR_UNABLE_TO_DELETE_FILE
, INVALID_STRING_ID
, WL_ERROR
);
788 this->InvalidateData(SLIWD_RESCAN_FILES
);
789 /* Reset file name to current date on successful delete */
790 if (this->abstract_filetype
== FT_SAVEGAME
) GenerateFileName();
792 } else if (this->IsWidgetLowered(WID_SL_SAVE_GAME
)) { // Save button clicked
793 if (this->abstract_filetype
== FT_SAVEGAME
|| this->abstract_filetype
== FT_SCENARIO
) {
794 _file_to_saveload
.name
= FiosMakeSavegameName(this->filename_editbox
.text
.buf
);
795 if (FioCheckFileExists(_file_to_saveload
.name
, Subdirectory::SAVE_DIR
)) {
796 ShowQuery(STR_SAVELOAD_OVERWRITE_TITLE
, STR_SAVELOAD_OVERWRITE_WARNING
, this, SaveLoadWindow::SaveGameConfirmationCallback
);
798 _switch_mode
= SM_SAVE_GAME
;
801 _file_to_saveload
.name
= FiosMakeHeightmapName(this->filename_editbox
.text
.buf
);
802 if (FioCheckFileExists(_file_to_saveload
.name
, Subdirectory::SAVE_DIR
)) {
803 ShowQuery(STR_SAVELOAD_OVERWRITE_TITLE
, STR_SAVELOAD_OVERWRITE_WARNING
, this, SaveLoadWindow::SaveHeightmapConfirmationCallback
);
805 _switch_mode
= SM_SAVE_HEIGHTMAP
;
809 /* In the editor set up the vehicle engines correctly (date might have changed) */
810 if (_game_mode
== GM_EDITOR
) StartupEngines();
814 void OnResize() override
816 this->vscroll
->SetCapacityFromWidget(this, WID_SL_DRIVES_DIRECTORIES_LIST
);
820 * Some data on this window has become invalid.
821 * @param data Information about the changed data.
822 * @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.
824 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
827 case SLIWD_RESCAN_FILES
:
829 this->selected
= nullptr;
830 _load_check_data
.Clear();
831 if (!gui_scope
) break;
833 _fios_path_changed
= true;
834 this->fios_items
.BuildFileList(this->abstract_filetype
, this->fop
);
835 this->vscroll
->SetCount((uint
)this->fios_items
.size());
836 this->selected
= nullptr;
837 _load_check_data
.Clear();
839 /* We reset the files filtered */
840 this->OnInvalidateData(SLIWD_FILTER_CHANGES
);
844 case SLIWD_SELECTION_CHANGES
:
845 /* Selection changes */
846 if (!gui_scope
) break;
848 if (this->fop
!= SLO_LOAD
) break;
850 switch (this->abstract_filetype
) {
852 this->SetWidgetDisabledState(WID_SL_LOAD_BUTTON
, this->selected
== nullptr || _load_check_data
.HasErrors());
857 bool disabled
= this->selected
== nullptr || _load_check_data
.HasErrors();
858 if (!_settings_client
.gui
.UserIsAllowedToChangeNewGRFs()) {
859 disabled
|= _load_check_data
.HasNewGrfs() && _load_check_data
.grf_compatibility
== GLC_NOT_FOUND
;
861 this->SetWidgetDisabledState(WID_SL_LOAD_BUTTON
, disabled
);
862 this->SetWidgetDisabledState(WID_SL_NEWGRF_INFO
, !_load_check_data
.HasNewGrfs());
863 this->SetWidgetDisabledState(WID_SL_MISSING_NEWGRFS
,
864 !_load_check_data
.HasNewGrfs() || _load_check_data
.grf_compatibility
== GLC_ALL_GOOD
);
873 case SLIWD_FILTER_CHANGES
:
875 this->fios_items_shown
.resize(this->fios_items
.size());
876 uint items_shown_count
= 0; ///< The number of items shown in the list
877 /* We pass through every fios item */
878 for (uint i
= 0; i
< this->fios_items
.size(); i
++) {
879 if (this->string_filter
.IsEmpty()) {
880 /* We don't filter anything out if the filter editbox is empty */
881 this->fios_items_shown
[i
] = true;
884 this->string_filter
.ResetState();
885 this->string_filter
.AddLine(this->fios_items
[i
].title
);
886 /* We set the vector to show this fios element as filtered depending on the result of the filter */
887 this->fios_items_shown
[i
] = this->string_filter
.GetState();
888 if (this->fios_items_shown
[i
]) items_shown_count
++;
890 if (&(this->fios_items
[i
]) == this->selected
&& !this->fios_items_shown
[i
]) {
891 /* The selected element has been filtered out */
892 this->selected
= nullptr;
893 this->OnInvalidateData(SLIWD_SELECTION_CHANGES
);
897 this->vscroll
->SetCount(items_shown_count
);
902 void OnEditboxChanged(int wid
) override
904 if (wid
== WID_SL_FILTER
) {
905 this->string_filter
.SetFilterTerm(this->filter_editbox
.text
.buf
);
906 this->InvalidateData(SLIWD_FILTER_CHANGES
);
911 /** Load game/scenario */
912 static WindowDesc
_load_dialog_desc(
913 WDP_CENTER
, "load_game", 500, 294,
914 WC_SAVELOAD
, WC_NONE
,
916 _nested_load_dialog_widgets
, lengthof(_nested_load_dialog_widgets
)
919 /** Load heightmap */
920 static WindowDesc
_load_heightmap_dialog_desc(
921 WDP_CENTER
, "load_heightmap", 257, 320,
922 WC_SAVELOAD
, WC_NONE
,
924 _nested_load_heightmap_dialog_widgets
, lengthof(_nested_load_heightmap_dialog_widgets
)
927 /** Save game/scenario */
928 static WindowDesc
_save_dialog_desc(
929 WDP_CENTER
, "save_game", 500, 294,
930 WC_SAVELOAD
, WC_NONE
,
932 _nested_save_dialog_widgets
, lengthof(_nested_save_dialog_widgets
)
936 * Launch save/load dialog in the given mode.
937 * @param abstract_filetype Kind of file to handle.
938 * @param fop File operation to perform (load or save).
940 void ShowSaveLoadDialog(AbstractFileType abstract_filetype
, SaveLoadOperation fop
)
942 CloseWindowById(WC_SAVELOAD
, 0);
945 if (fop
== SLO_SAVE
) {
946 sld
= &_save_dialog_desc
;
948 /* Dialogue for loading a file. */
949 sld
= (abstract_filetype
== FT_HEIGHTMAP
) ? &_load_heightmap_dialog_desc
: &_load_dialog_desc
;
952 new SaveLoadWindow(sld
, abstract_filetype
, fop
);