Update readme.md
[openttd-joker.git] / src / settings_gui.cpp
blob007b839906088a7085530268fe77909047bd1df5
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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 */
10 /** @file settings_gui.cpp GUI for settings. */
12 #include "stdafx.h"
13 #include "currency.h"
14 #include "error.h"
15 #include "settings_gui.h"
16 #include "textbuf_gui.h"
17 #include "command_func.h"
18 #include "network/network.h"
19 #include "town.h"
20 #include "settings_internal.h"
21 #include "newgrf_townname.h"
22 #include "strings_func.h"
23 #include "window_func.h"
24 #include "string_func.h"
25 #include "widgets/dropdown_type.h"
26 #include "widgets/dropdown_func.h"
27 #include "highscore.h"
28 #include "base_media_base.h"
29 #include "company_base.h"
30 #include "company_func.h"
31 #include "viewport_func.h"
32 #include "core/geometry_func.hpp"
33 #include "ai/ai.hpp"
34 #include "blitter/factory.hpp"
35 #include "language.h"
36 #include "textfile_gui.h"
37 #include "stringfilter_type.h"
38 #include "querystring_gui.h"
40 #include <vector>
42 #include "safeguards.h"
45 static const StringID _driveside_dropdown[] = {
46 STR_GAME_OPTIONS_ROAD_VEHICLES_DROPDOWN_LEFT,
47 STR_GAME_OPTIONS_ROAD_VEHICLES_DROPDOWN_RIGHT,
48 INVALID_STRING_ID
51 static const StringID _autosave_dropdown[] = {
52 STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_OFF,
53 STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_1_MONTH,
54 STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_3_MONTHS,
55 STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_6_MONTHS,
56 STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_12_MONTHS,
57 INVALID_STRING_ID,
60 static const StringID _gui_zoom_dropdown[] = {
61 STR_GAME_OPTIONS_GUI_ZOOM_DROPDOWN_NORMAL,
62 STR_GAME_OPTIONS_GUI_ZOOM_DROPDOWN_2X_ZOOM,
63 STR_GAME_OPTIONS_GUI_ZOOM_DROPDOWN_4X_ZOOM,
64 INVALID_STRING_ID,
67 int _nb_orig_names = SPECSTR_TOWNNAME_LAST - SPECSTR_TOWNNAME_START + 1; ///< Number of original town names.
68 static StringID *_grf_names = nullptr; ///< Pointer to town names defined by NewGRFs.
69 static int _nb_grf_names = 0; ///< Number of town names defined by NewGRFs.
71 static Dimension _circle_size; ///< Dimension of the circle +/- icon. This is here as not all users are within the class of the settings window.
73 static const void *ResolveVariableAddress(const GameSettings *settings_ptr, const SettingDesc *sd);
75 /** Allocate memory for the NewGRF town names. */
76 void InitGRFTownGeneratorNames()
78 free(_grf_names);
79 _grf_names = GetGRFTownNameList();
80 _nb_grf_names = 0;
81 for (StringID *s = _grf_names; *s != INVALID_STRING_ID; s++) _nb_grf_names++;
84 /**
85 * Get a town name.
86 * @param town_name Number of the wanted town name.
87 * @return Name of the town as string ID.
89 static inline StringID TownName(int town_name)
91 if (town_name < _nb_orig_names) return STR_GAME_OPTIONS_TOWN_NAME_ORIGINAL_ENGLISH + town_name;
92 town_name -= _nb_orig_names;
93 if (town_name < _nb_grf_names) return _grf_names[town_name];
94 return STR_UNDEFINED;
97 /**
98 * Get index of the current screen resolution.
99 * @return Index of the current screen resolution if it is a known resolution, #_num_resolutions otherwise.
101 static int GetCurRes()
103 int i;
105 for (i = 0; i != _num_resolutions; i++) {
106 if ((int)_resolutions[i].width == _screen.width &&
107 (int)_resolutions[i].height == _screen.height) {
108 break;
111 return i;
114 static void ShowCustCurrency();
116 template <class T>
117 static DropDownList *BuiltSetDropDownList(int *selected_index)
119 int n = T::GetNumSets();
120 *selected_index = T::GetIndexOfUsedSet();
122 DropDownList *list = new DropDownList();
123 for (int i = 0; i < n; i++) {
124 *list->Append() = new DropDownListCharStringItem(T::GetSet(i)->name, i, (_game_mode == GM_MENU) ? false : (*selected_index != i));
127 return list;
130 /** Window for displaying the textfile of a BaseSet. */
131 template <class TBaseSet>
132 struct BaseSetTextfileWindow : public TextfileWindow {
133 const TBaseSet* baseset; ///< View the textfile of this BaseSet.
134 StringID content_type; ///< STR_CONTENT_TYPE_xxx for title.
136 BaseSetTextfileWindow(TextfileType file_type, const TBaseSet* baseset, StringID content_type) : TextfileWindow(file_type), baseset(baseset), content_type(content_type)
138 const char *textfile = this->baseset->GetTextfile(file_type);
139 this->LoadTextfile(textfile, BASESET_DIR);
142 /* virtual */ void SetStringParameters(int widget) const
144 if (widget == WID_TF_CAPTION) {
145 SetDParam(0, content_type);
146 SetDParamStr(1, this->baseset->name);
152 * Open the BaseSet version of the textfile window.
153 * @param file_type The type of textfile to display.
154 * @param baseset The BaseSet to use.
155 * @param content_type STR_CONTENT_TYPE_xxx for title.
157 template <class TBaseSet>
158 void ShowBaseSetTextfileWindow(TextfileType file_type, const TBaseSet* baseset, StringID content_type)
160 DeleteWindowByClass(WC_TEXTFILE);
161 new BaseSetTextfileWindow<TBaseSet>(file_type, baseset, content_type);
164 struct GameOptionsWindow : Window {
165 GameSettings *opt;
166 bool reload;
168 GameOptionsWindow(WindowDesc *desc) : Window(desc)
170 this->opt = &GetGameSettings();
171 this->reload = false;
173 this->InitNested(WN_GAME_OPTIONS_GAME_OPTIONS);
174 this->OnInvalidateData(0);
177 ~GameOptionsWindow()
179 DeleteWindowById(WC_CUSTOM_CURRENCY, 0);
180 if (this->reload) _switch_mode = SM_MENU;
184 * Build the dropdown list for a specific widget.
185 * @param widget Widget to build list for
186 * @param selected_index Currently selected item
187 * @return the built dropdown list, or nullptr if the widget has no dropdown menu.
189 DropDownList *BuildDropDownList(int widget, int *selected_index) const
191 DropDownList *list = nullptr;
192 switch (widget) {
193 case WID_GO_CURRENCY_DROPDOWN: { // Setup currencies dropdown
194 list = new DropDownList();
195 *selected_index = this->opt->locale.currency;
196 StringID *items = BuildCurrencyDropdown();
197 uint64 disabled = _game_mode == GM_MENU ? 0LL : ~GetMaskOfAllowedCurrencies();
199 /* Add non-custom currencies; sorted naturally */
200 for (uint i = 0; i < CURRENCY_END; items++, i++) {
201 if (i == CURRENCY_CUSTOM) continue;
202 *list->Append() = new DropDownListStringItem(*items, i, HasBit(disabled, i));
204 QSortT(list->Begin(), list->Length(), DropDownListStringItem::NatSortFunc);
206 /* Append custom currency at the end */
207 *list->Append() = new DropDownListItem(-1, false); // separator line
208 *list->Append() = new DropDownListStringItem(STR_GAME_OPTIONS_CURRENCY_CUSTOM, CURRENCY_CUSTOM, HasBit(disabled, CURRENCY_CUSTOM));
209 break;
212 case WID_GO_ROADSIDE_DROPDOWN: { // Setup road-side dropdown
213 list = new DropDownList();
214 *selected_index = this->opt->vehicle.road_side;
215 const StringID *items = _driveside_dropdown;
216 uint disabled = 0;
218 /* You can only change the drive side if you are in the menu or ingame with
219 * no vehicles present. In a networking game only the server can change it */
220 extern bool RoadVehiclesAreBuilt();
221 if ((_game_mode != GM_MENU && RoadVehiclesAreBuilt()) || (_networking && !_network_server)) {
222 disabled = ~(1 << this->opt->vehicle.road_side); // disable the other value
225 for (uint i = 0; *items != INVALID_STRING_ID; items++, i++) {
226 *list->Append() = new DropDownListStringItem(*items, i, HasBit(disabled, i));
228 break;
231 case WID_GO_TOWNNAME_DROPDOWN: { // Setup townname dropdown
232 list = new DropDownList();
233 *selected_index = this->opt->game_creation.town_name;
235 int enabled_item = (_game_mode == GM_MENU || Town::GetNumItems() == 0) ? -1 : *selected_index;
237 /* Add and sort newgrf townnames generators */
238 for (int i = 0; i < _nb_grf_names; i++) {
239 int result = _nb_orig_names + i;
240 *list->Append() = new DropDownListStringItem(_grf_names[i], result, enabled_item != result && enabled_item >= 0);
242 QSortT(list->Begin(), list->Length(), DropDownListStringItem::NatSortFunc);
244 int newgrf_size = list->Length();
245 /* Insert newgrf_names at the top of the list */
246 if (newgrf_size > 0) {
247 *list->Append() = new DropDownListItem(-1, false); // separator line
248 newgrf_size++;
251 /* Add and sort original townnames generators */
252 for (int i = 0; i < _nb_orig_names; i++) {
253 *list->Append() = new DropDownListStringItem(STR_GAME_OPTIONS_TOWN_NAME_ORIGINAL_ENGLISH + i, i, enabled_item != i && enabled_item >= 0);
255 QSortT(list->Begin() + newgrf_size, list->Length() - newgrf_size, DropDownListStringItem::NatSortFunc);
256 break;
259 case WID_GO_AUTOSAVE_DROPDOWN: { // Setup autosave dropdown
260 list = new DropDownList();
261 *selected_index = _settings_client.gui.autosave;
262 const StringID *items = _autosave_dropdown;
263 for (uint i = 0; *items != INVALID_STRING_ID; items++, i++) {
264 *list->Append() = new DropDownListStringItem(*items, i, false);
266 break;
269 case WID_GO_LANG_DROPDOWN: { // Setup interface language dropdown
270 list = new DropDownList();
271 for (uint i = 0; i < _languages.Length(); i++) {
272 if (&_languages[i] == _current_language) *selected_index = i;
273 *list->Append() = new DropDownListStringItem(SPECSTR_LANGUAGE_START + i, i, false);
275 QSortT(list->Begin(), list->Length(), DropDownListStringItem::NatSortFunc);
276 break;
279 case WID_GO_RESOLUTION_DROPDOWN: // Setup resolution dropdown
280 if (_num_resolutions == 0) break;
282 list = new DropDownList();
283 *selected_index = GetCurRes();
284 for (int i = 0; i < _num_resolutions; i++) {
285 *list->Append() = new DropDownListStringItem(SPECSTR_RESOLUTION_START + i, i, false);
287 break;
289 case WID_GO_GUI_ZOOM_DROPDOWN: {
290 list = new DropDownList();
291 *selected_index = ZOOM_LVL_OUT_4X - _gui_zoom;
292 const StringID *items = _gui_zoom_dropdown;
293 for (int i = 0; *items != INVALID_STRING_ID; items++, i++) {
294 *list->Append() = new DropDownListStringItem(*items, i, _settings_client.gui.zoom_min > ZOOM_LVL_OUT_4X - i);
296 break;
299 case WID_GO_BASE_GRF_DROPDOWN:
300 list = BuiltSetDropDownList<BaseGraphics>(selected_index);
301 break;
303 case WID_GO_BASE_SFX_DROPDOWN:
304 list = BuiltSetDropDownList<BaseSounds>(selected_index);
305 break;
307 case WID_GO_BASE_MUSIC_DROPDOWN:
308 list = BuiltSetDropDownList<BaseMusic>(selected_index);
309 break;
311 default:
312 return nullptr;
315 return list;
318 virtual void SetStringParameters(int widget) const
320 switch (widget) {
321 case WID_GO_CURRENCY_DROPDOWN: SetDParam(0, _currency_specs[this->opt->locale.currency].name); break;
322 case WID_GO_ROADSIDE_DROPDOWN: SetDParam(0, STR_GAME_OPTIONS_ROAD_VEHICLES_DROPDOWN_LEFT + this->opt->vehicle.road_side); break;
323 case WID_GO_TOWNNAME_DROPDOWN: SetDParam(0, TownName(this->opt->game_creation.town_name)); break;
324 case WID_GO_AUTOSAVE_DROPDOWN: SetDParam(0, _autosave_dropdown[_settings_client.gui.autosave]); break;
325 case WID_GO_LANG_DROPDOWN: SetDParamStr(0, _current_language->own_name); break;
326 case WID_GO_RESOLUTION_DROPDOWN: SetDParam(0, GetCurRes() == _num_resolutions ? STR_GAME_OPTIONS_RESOLUTION_OTHER : SPECSTR_RESOLUTION_START + GetCurRes()); break;
327 case WID_GO_GUI_ZOOM_DROPDOWN: SetDParam(0, _gui_zoom_dropdown[ZOOM_LVL_OUT_4X - _gui_zoom]); break;
328 case WID_GO_BASE_GRF_DROPDOWN: SetDParamStr(0, BaseGraphics::GetUsedSet()->name); break;
329 case WID_GO_BASE_GRF_STATUS: SetDParam(0, BaseGraphics::GetUsedSet()->GetNumInvalid()); break;
330 case WID_GO_BASE_SFX_DROPDOWN: SetDParamStr(0, BaseSounds::GetUsedSet()->name); break;
331 case WID_GO_BASE_MUSIC_DROPDOWN: SetDParamStr(0, BaseMusic::GetUsedSet()->name); break;
332 case WID_GO_BASE_MUSIC_STATUS: SetDParam(0, BaseMusic::GetUsedSet()->GetNumInvalid()); break;
336 virtual void DrawWidget(const Rect &r, int widget) const
338 switch (widget) {
339 case WID_GO_BASE_GRF_DESCRIPTION:
340 SetDParamStr(0, BaseGraphics::GetUsedSet()->GetDescription(GetCurrentLanguageIsoCode()));
341 DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, STR_BLACK_RAW_STRING);
342 break;
344 case WID_GO_BASE_SFX_DESCRIPTION:
345 SetDParamStr(0, BaseSounds::GetUsedSet()->GetDescription(GetCurrentLanguageIsoCode()));
346 DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, STR_BLACK_RAW_STRING);
347 break;
349 case WID_GO_BASE_MUSIC_DESCRIPTION:
350 SetDParamStr(0, BaseMusic::GetUsedSet()->GetDescription(GetCurrentLanguageIsoCode()));
351 DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, STR_BLACK_RAW_STRING);
352 break;
356 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
358 switch (widget) {
359 case WID_GO_BASE_GRF_DESCRIPTION:
360 /* Find the biggest description for the default size. */
361 for (int i = 0; i < BaseGraphics::GetNumSets(); i++) {
362 SetDParamStr(0, BaseGraphics::GetSet(i)->GetDescription(GetCurrentLanguageIsoCode()));
363 size->height = max(size->height, (uint)GetStringHeight(STR_BLACK_RAW_STRING, size->width));
365 break;
367 case WID_GO_BASE_GRF_STATUS:
368 /* Find the biggest description for the default size. */
369 for (int i = 0; i < BaseGraphics::GetNumSets(); i++) {
370 uint invalid_files = BaseGraphics::GetSet(i)->GetNumInvalid();
371 if (invalid_files == 0) continue;
373 SetDParam(0, invalid_files);
374 *size = maxdim(*size, GetStringBoundingBox(STR_GAME_OPTIONS_BASE_GRF_STATUS));
376 break;
378 case WID_GO_BASE_SFX_DESCRIPTION:
379 /* Find the biggest description for the default size. */
380 for (int i = 0; i < BaseSounds::GetNumSets(); i++) {
381 SetDParamStr(0, BaseSounds::GetSet(i)->GetDescription(GetCurrentLanguageIsoCode()));
382 size->height = max(size->height, (uint)GetStringHeight(STR_BLACK_RAW_STRING, size->width));
384 break;
386 case WID_GO_BASE_MUSIC_DESCRIPTION:
387 /* Find the biggest description for the default size. */
388 for (int i = 0; i < BaseMusic::GetNumSets(); i++) {
389 SetDParamStr(0, BaseMusic::GetSet(i)->GetDescription(GetCurrentLanguageIsoCode()));
390 size->height = max(size->height, (uint)GetStringHeight(STR_BLACK_RAW_STRING, size->width));
392 break;
394 case WID_GO_BASE_MUSIC_STATUS:
395 /* Find the biggest description for the default size. */
396 for (int i = 0; i < BaseMusic::GetNumSets(); i++) {
397 uint invalid_files = BaseMusic::GetSet(i)->GetNumInvalid();
398 if (invalid_files == 0) continue;
400 SetDParam(0, invalid_files);
401 *size = maxdim(*size, GetStringBoundingBox(STR_GAME_OPTIONS_BASE_MUSIC_STATUS));
403 break;
405 default: {
406 int selected;
407 DropDownList *list = this->BuildDropDownList(widget, &selected);
408 if (list != nullptr) {
409 /* Find the biggest item for the default size. */
410 for (const DropDownListItem * const *it = list->Begin(); it != list->End(); it++) {
411 Dimension string_dim;
412 int width = (*it)->Width();
413 string_dim.width = width + padding.width;
414 string_dim.height = (*it)->Height(width) + padding.height;
415 *size = maxdim(*size, string_dim);
417 delete list;
423 virtual void OnClick(Point pt, int widget, int click_count)
425 if (widget >= WID_GO_BASE_GRF_TEXTFILE && widget < WID_GO_BASE_GRF_TEXTFILE + TFT_END) {
426 if (BaseGraphics::GetUsedSet() == nullptr) return;
428 ShowBaseSetTextfileWindow((TextfileType)(widget - WID_GO_BASE_GRF_TEXTFILE), BaseGraphics::GetUsedSet(), STR_CONTENT_TYPE_BASE_GRAPHICS);
429 return;
431 if (widget >= WID_GO_BASE_SFX_TEXTFILE && widget < WID_GO_BASE_SFX_TEXTFILE + TFT_END) {
432 if (BaseSounds::GetUsedSet() == nullptr) return;
434 ShowBaseSetTextfileWindow((TextfileType)(widget - WID_GO_BASE_SFX_TEXTFILE), BaseSounds::GetUsedSet(), STR_CONTENT_TYPE_BASE_SOUNDS);
435 return;
437 if (widget >= WID_GO_BASE_MUSIC_TEXTFILE && widget < WID_GO_BASE_MUSIC_TEXTFILE + TFT_END) {
438 if (BaseMusic::GetUsedSet() == nullptr) return;
440 ShowBaseSetTextfileWindow((TextfileType)(widget - WID_GO_BASE_MUSIC_TEXTFILE), BaseMusic::GetUsedSet(), STR_CONTENT_TYPE_BASE_MUSIC);
441 return;
443 switch (widget) {
444 case WID_GO_FULLSCREEN_BUTTON: // Click fullscreen on/off
445 /* try to toggle full-screen on/off */
446 if (!ToggleFullScreen(!_fullscreen)) {
447 ShowErrorMessage(STR_ERROR_FULLSCREEN_FAILED, INVALID_STRING_ID, WL_ERROR);
449 this->SetWidgetLoweredState(WID_GO_FULLSCREEN_BUTTON, _fullscreen);
450 this->SetDirty();
451 break;
453 default: {
454 int selected;
455 DropDownList *list = this->BuildDropDownList(widget, &selected);
456 if (list != nullptr) {
457 ShowDropDownList(this, list, selected, widget);
458 } else {
459 if (widget == WID_GO_RESOLUTION_DROPDOWN) ShowErrorMessage(STR_ERROR_RESOLUTION_LIST_FAILED, INVALID_STRING_ID, WL_ERROR);
461 break;
467 * Set the base media set.
468 * @param index the index of the media set
469 * @tparam T class of media set
471 template <class T>
472 void SetMediaSet(int index)
474 if (_game_mode == GM_MENU) {
475 const char *name = T::GetSet(index)->name;
477 free(T::ini_set);
478 T::ini_set = stredup(name);
480 T::SetSet(name);
481 this->reload = true;
482 this->InvalidateData();
486 virtual void OnDropdownSelect(int widget, int index)
488 switch (widget) {
489 case WID_GO_CURRENCY_DROPDOWN: // Currency
490 if (index == CURRENCY_CUSTOM) ShowCustCurrency();
491 this->opt->locale.currency = index;
492 ReInitAllWindows();
493 break;
495 case WID_GO_ROADSIDE_DROPDOWN: // Road side
496 if (this->opt->vehicle.road_side != index) { // only change if setting changed
497 uint i;
498 if (GetSettingFromName("vehicle.road_side", &i) == nullptr) NOT_REACHED();
499 SetSettingValue(i, index);
500 MarkWholeScreenDirty();
502 break;
504 case WID_GO_TOWNNAME_DROPDOWN: // Town names
505 if (_game_mode == GM_MENU || Town::GetNumItems() == 0) {
506 this->opt->game_creation.town_name = index;
507 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_GAME_OPTIONS);
509 break;
511 case WID_GO_AUTOSAVE_DROPDOWN: // Autosave options
512 _settings_client.gui.autosave = index;
513 this->SetDirty();
514 break;
516 case WID_GO_LANG_DROPDOWN: // Change interface language
517 ReadLanguagePack(&_languages[index]);
518 DeleteWindowByClass(WC_QUERY_STRING);
519 CheckForMissingGlyphs();
520 UpdateAllVirtCoords();
521 ReInitAllWindows();
522 break;
524 case WID_GO_RESOLUTION_DROPDOWN: // Change resolution
525 if (index < _num_resolutions && ChangeResInGame(_resolutions[index].width, _resolutions[index].height)) {
526 this->SetDirty();
528 break;
530 case WID_GO_GUI_ZOOM_DROPDOWN:
531 extern void UpdateFontHeightCache();
532 GfxClearSpriteCache();
533 _gui_zoom = (ZoomLevel)(ZOOM_LVL_OUT_4X - index);
534 UpdateCursorSize();
535 UpdateFontHeightCache();
536 LoadStringWidthTable();
537 UpdateAllVirtCoords();
538 break;
540 case WID_GO_BASE_GRF_DROPDOWN:
541 this->SetMediaSet<BaseGraphics>(index);
542 break;
544 case WID_GO_BASE_SFX_DROPDOWN:
545 this->SetMediaSet<BaseSounds>(index);
546 break;
548 case WID_GO_BASE_MUSIC_DROPDOWN:
549 this->SetMediaSet<BaseMusic>(index);
550 break;
555 * Some data on this window has become invalid.
556 * @param data Information about the changed data. @see GameOptionsInvalidationData
557 * @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.
559 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
561 if (!gui_scope) return;
562 this->SetWidgetLoweredState(WID_GO_FULLSCREEN_BUTTON, _fullscreen);
564 bool missing_files = BaseGraphics::GetUsedSet()->GetNumMissing() == 0;
565 this->GetWidget<NWidgetCore>(WID_GO_BASE_GRF_STATUS)->SetDataTip(missing_files ? STR_EMPTY : STR_GAME_OPTIONS_BASE_GRF_STATUS, STR_NULL);
567 for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
568 this->SetWidgetDisabledState(WID_GO_BASE_GRF_TEXTFILE + tft, BaseGraphics::GetUsedSet() == nullptr || BaseGraphics::GetUsedSet()->GetTextfile(tft) == nullptr);
569 this->SetWidgetDisabledState(WID_GO_BASE_SFX_TEXTFILE + tft, BaseSounds::GetUsedSet() == nullptr || BaseSounds::GetUsedSet()->GetTextfile(tft) == nullptr);
570 this->SetWidgetDisabledState(WID_GO_BASE_MUSIC_TEXTFILE + tft, BaseMusic::GetUsedSet() == nullptr || BaseMusic::GetUsedSet()->GetTextfile(tft) == nullptr);
573 missing_files = BaseMusic::GetUsedSet()->GetNumInvalid() == 0;
574 this->GetWidget<NWidgetCore>(WID_GO_BASE_MUSIC_STATUS)->SetDataTip(missing_files ? STR_EMPTY : STR_GAME_OPTIONS_BASE_MUSIC_STATUS, STR_NULL);
578 static const NWidgetPart _nested_game_options_widgets[] = {
579 NWidget(NWID_HORIZONTAL),
580 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
581 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
582 EndContainer(),
583 NWidget(WWT_PANEL, COLOUR_GREY, WID_GO_BACKGROUND), SetPIP(6, 6, 10),
584 NWidget(NWID_HORIZONTAL), SetPIP(10, 10, 10),
585 NWidget(NWID_VERTICAL), SetPIP(0, 6, 0),
586 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_ROAD_VEHICLES_FRAME, STR_NULL),
587 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_ROADSIDE_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_ROAD_VEHICLES_DROPDOWN_TOOLTIP), SetFill(1, 0),
588 EndContainer(),
589 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_AUTOSAVE_FRAME, STR_NULL),
590 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_AUTOSAVE_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_TOOLTIP), SetFill(1, 0),
591 EndContainer(),
592 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_RESOLUTION, STR_NULL),
593 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_RESOLUTION_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_RESOLUTION_TOOLTIP), SetFill(1, 0), SetPadding(0, 0, 3, 0),
594 NWidget(NWID_HORIZONTAL),
595 NWidget(WWT_TEXT, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 0), SetDataTip(STR_GAME_OPTIONS_FULLSCREEN, STR_NULL),
596 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_GO_FULLSCREEN_BUTTON), SetMinimalSize(21, 9), SetDataTip(STR_EMPTY, STR_GAME_OPTIONS_FULLSCREEN_TOOLTIP),
597 EndContainer(),
598 EndContainer(),
599 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_GUI_ZOOM_FRAME, STR_NULL),
600 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_GUI_ZOOM_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_GUI_ZOOM_DROPDOWN_TOOLTIP), SetFill(1, 0),
601 EndContainer(),
602 EndContainer(),
604 NWidget(NWID_VERTICAL), SetPIP(0, 6, 0),
605 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_TOWN_NAMES_FRAME, STR_NULL),
606 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_TOWNNAME_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_TOWN_NAMES_DROPDOWN_TOOLTIP), SetFill(1, 0),
607 EndContainer(),
608 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_LANGUAGE, STR_NULL),
609 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_LANG_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_RAW_STRING, STR_GAME_OPTIONS_LANGUAGE_TOOLTIP), SetFill(1, 0),
610 EndContainer(),
611 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_CURRENCY_UNITS_FRAME, STR_NULL),
612 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_CURRENCY_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_CURRENCY_UNITS_DROPDOWN_TOOLTIP), SetFill(1, 0),
613 EndContainer(),
614 NWidget(NWID_SPACER), SetMinimalSize(0, 0), SetFill(0, 1),
615 EndContainer(),
616 EndContainer(),
618 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_BASE_GRF, STR_NULL), SetPadding(0, 10, 0, 10),
619 NWidget(NWID_HORIZONTAL), SetPIP(0, 30, 0),
620 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_BASE_GRF_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_RAW_STRING, STR_GAME_OPTIONS_BASE_GRF_TOOLTIP),
621 NWidget(WWT_TEXT, COLOUR_GREY, WID_GO_BASE_GRF_STATUS), SetMinimalSize(150, 12), SetDataTip(STR_EMPTY, STR_NULL), SetFill(1, 0),
622 EndContainer(),
623 NWidget(WWT_TEXT, COLOUR_GREY, WID_GO_BASE_GRF_DESCRIPTION), SetMinimalSize(330, 0), SetDataTip(STR_EMPTY, STR_GAME_OPTIONS_BASE_GRF_DESCRIPTION_TOOLTIP), SetFill(1, 0), SetPadding(6, 0, 6, 0),
624 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
625 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GO_BASE_GRF_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_README, STR_NULL),
626 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GO_BASE_GRF_TEXTFILE + TFT_CHANGELOG), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_CHANGELOG, STR_NULL),
627 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GO_BASE_GRF_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_LICENCE, STR_NULL),
628 EndContainer(),
629 EndContainer(),
631 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_BASE_SFX, STR_NULL), SetPadding(0, 10, 0, 10),
632 NWidget(NWID_HORIZONTAL), SetPIP(0, 30, 0),
633 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_BASE_SFX_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_RAW_STRING, STR_GAME_OPTIONS_BASE_SFX_TOOLTIP),
634 NWidget(NWID_SPACER), SetFill(1, 0),
635 EndContainer(),
636 NWidget(WWT_TEXT, COLOUR_GREY, WID_GO_BASE_SFX_DESCRIPTION), SetMinimalSize(330, 0), SetDataTip(STR_EMPTY, STR_GAME_OPTIONS_BASE_SFX_DESCRIPTION_TOOLTIP), SetFill(1, 0), SetPadding(6, 0, 6, 0),
637 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
638 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GO_BASE_SFX_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_README, STR_NULL),
639 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GO_BASE_SFX_TEXTFILE + TFT_CHANGELOG), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_CHANGELOG, STR_NULL),
640 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GO_BASE_SFX_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_LICENCE, STR_NULL),
641 EndContainer(),
642 EndContainer(),
644 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_BASE_MUSIC, STR_NULL), SetPadding(0, 10, 0, 10),
645 NWidget(NWID_HORIZONTAL), SetPIP(0, 30, 0),
646 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_BASE_MUSIC_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_RAW_STRING, STR_GAME_OPTIONS_BASE_MUSIC_TOOLTIP),
647 NWidget(WWT_TEXT, COLOUR_GREY, WID_GO_BASE_MUSIC_STATUS), SetMinimalSize(150, 12), SetDataTip(STR_EMPTY, STR_NULL), SetFill(1, 0),
648 EndContainer(),
649 NWidget(WWT_TEXT, COLOUR_GREY, WID_GO_BASE_MUSIC_DESCRIPTION), SetMinimalSize(330, 0), SetDataTip(STR_EMPTY, STR_GAME_OPTIONS_BASE_MUSIC_DESCRIPTION_TOOLTIP), SetFill(1, 0), SetPadding(6, 0, 6, 0),
650 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
651 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GO_BASE_MUSIC_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_README, STR_NULL),
652 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GO_BASE_MUSIC_TEXTFILE + TFT_CHANGELOG), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_CHANGELOG, STR_NULL),
653 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GO_BASE_MUSIC_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_LICENCE, STR_NULL),
654 EndContainer(),
655 EndContainer(),
656 EndContainer(),
659 static WindowDesc _game_options_desc(
660 WDP_CENTER, "settings_game", 0, 0,
661 WC_GAME_OPTIONS, WC_NONE,
663 _nested_game_options_widgets, lengthof(_nested_game_options_widgets)
666 /** Open the game options window. */
667 void ShowGameOptions()
669 DeleteWindowByClass(WC_GAME_OPTIONS);
670 new GameOptionsWindow(&_game_options_desc);
673 static int SETTING_HEIGHT = 11; ///< Height of a single setting in the tree view in pixels
674 static const int LEVEL_WIDTH = 15; ///< Indenting width of a sub-page in pixels
677 * Flags for #SettingEntry
678 * @note The #SEF_BUTTONS_MASK matches expectations of the formal parameter 'state' of #DrawArrowButtons
680 enum SettingEntryFlags {
681 SEF_LEFT_DEPRESSED = 0x01, ///< Of a numeric setting entry, the left button is depressed
682 SEF_RIGHT_DEPRESSED = 0x02, ///< Of a numeric setting entry, the right button is depressed
683 SEF_BUTTONS_MASK = (SEF_LEFT_DEPRESSED | SEF_RIGHT_DEPRESSED), ///< Bit-mask for button flags
685 SEF_LAST_FIELD = 0x04, ///< This entry is the last one in a (sub-)page
686 SEF_FILTERED = 0x08, ///< Entry is hidden by the string filter
689 /** How the list of advanced settings is filtered. */
690 enum RestrictionMode {
691 RM_BASIC, ///< Display settings associated to the "basic" list.
692 RM_ADVANCED, ///< Display settings associated to the "advanced" list.
693 RM_ALL, ///< List all settings regardless of the default/newgame/... values.
694 RM_CHANGED_AGAINST_DEFAULT, ///< Show only settings which are different compared to default values.
695 RM_CHANGED_AGAINST_NEW, ///< Show only settings which are different compared to the user's new game setting values.
696 RM_END, ///< End for iteration.
698 DECLARE_POSTFIX_INCREMENT(RestrictionMode)
700 /** Filter for settings list. */
701 struct SettingFilter {
702 StringFilter string; ///< Filter string.
703 RestrictionMode min_cat; ///< Minimum category needed to display all filtered strings (#RM_BASIC, #RM_ADVANCED, or #RM_ALL).
704 bool type_hides; ///< Whether the type hides filtered strings.
705 RestrictionMode mode; ///< Filter based on category.
706 SettingType type; ///< Filter based on type.
709 /** Data structure describing a single setting in a tab */
710 struct BaseSettingEntry {
711 byte flags; ///< Flags of the setting entry. @see SettingEntryFlags
712 byte level; ///< Nesting level of this setting entry
714 BaseSettingEntry() : flags(0), level(0) {}
715 virtual ~BaseSettingEntry() {}
717 virtual void Init(byte level = 0);
718 virtual void FoldAll() {}
719 virtual void UnFoldAll() {}
722 * Set whether this is the last visible entry of the parent node.
723 * @param last_field Value to set
725 void SetLastField(bool last_field) { if (last_field) SETBITS(this->flags, SEF_LAST_FIELD); else CLRBITS(this->flags, SEF_LAST_FIELD); }
727 virtual uint Length() const = 0;
728 virtual void GetFoldingState(bool &all_folded, bool &all_unfolded) const {}
729 virtual bool IsVisible(const BaseSettingEntry *item) const;
730 virtual BaseSettingEntry *FindEntry(uint row, uint *cur_row);
731 virtual uint GetMaxHelpHeight(int maxw) { return 0; }
734 * Check whether an entry is hidden due to filters
735 * @return true if hidden.
737 bool IsFiltered() const { return (this->flags & SEF_FILTERED) != 0; }
739 virtual bool UpdateFilterState(SettingFilter &filter, bool force_visible) = 0;
741 virtual uint Draw(GameSettings *settings_ptr, int left, int right, int y, uint first_row, uint max_row, BaseSettingEntry *selected, uint cur_row = 0, uint parent_last = 0) const;
743 protected:
744 virtual void DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const = 0;
747 /** Standard setting */
748 struct SettingEntry : BaseSettingEntry {
749 const char *name; ///< Name of the setting
750 const SettingDesc *setting; ///< Setting description of the setting
751 uint index; ///< Index of the setting in the settings table
753 SettingEntry(const char *name);
755 virtual void Init(byte level = 0);
756 virtual uint Length() const;
757 virtual uint GetMaxHelpHeight(int maxw);
758 virtual bool UpdateFilterState(SettingFilter &filter, bool force_visible);
760 void SetButtons(byte new_val);
763 * Get the help text of a single setting.
764 * @return The requested help text.
766 inline StringID GetHelpText() const
768 return this->setting->desc.str_help;
771 void SetValueDParams(uint first_param, int32 value) const;
773 protected:
774 virtual void DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const;
776 private:
777 bool IsVisibleByRestrictionMode(RestrictionMode mode) const;
780 /** Containers for BaseSettingEntry */
781 struct SettingsContainer {
782 typedef std::vector<BaseSettingEntry*> EntryVector;
783 EntryVector entries; ///< Settings on this page
785 template<typename T>
786 T *Add(T *item)
788 this->entries.push_back(item);
789 return item;
792 void Init(byte level = 0);
793 void FoldAll();
794 void UnFoldAll();
796 uint Length() const;
797 void GetFoldingState(bool &all_folded, bool &all_unfolded) const;
798 bool IsVisible(const BaseSettingEntry *item) const;
799 BaseSettingEntry *FindEntry(uint row, uint *cur_row);
800 uint GetMaxHelpHeight(int maxw);
802 bool UpdateFilterState(SettingFilter &filter, bool force_visible);
804 uint Draw(GameSettings *settings_ptr, int left, int right, int y, uint first_row, uint max_row, BaseSettingEntry *selected, uint cur_row = 0, uint parent_last = 0) const;
807 /** Data structure describing one page of settings in the settings window. */
808 struct SettingsPage : BaseSettingEntry, SettingsContainer {
809 StringID title; ///< Title of the sub-page
810 bool folded; ///< Sub-page is folded (not visible except for its title)
812 SettingsPage(StringID title);
814 virtual void Init(byte level = 0);
815 virtual void FoldAll();
816 virtual void UnFoldAll();
818 virtual uint Length() const;
819 virtual void GetFoldingState(bool &all_folded, bool &all_unfolded) const;
820 virtual bool IsVisible(const BaseSettingEntry *item) const;
821 virtual BaseSettingEntry *FindEntry(uint row, uint *cur_row);
822 virtual uint GetMaxHelpHeight(int maxw) { return SettingsContainer::GetMaxHelpHeight(maxw); }
824 virtual bool UpdateFilterState(SettingFilter &filter, bool force_visible);
826 virtual uint Draw(GameSettings *settings_ptr, int left, int right, int y, uint first_row, uint max_row, BaseSettingEntry *selected, uint cur_row = 0, uint parent_last = 0) const;
828 protected:
829 virtual void DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const;
832 /* == BaseSettingEntry methods == */
835 * Initialization of a setting entry
836 * @param level Page nesting level of this entry
838 void BaseSettingEntry::Init(byte level)
840 this->level = level;
844 * Check whether an entry is visible and not folded or filtered away.
845 * Note: This does not consider the scrolling range; it might still require scrolling to make the setting really visible.
846 * @param item Entry to search for.
847 * @return true if entry is visible.
849 bool BaseSettingEntry::IsVisible(const BaseSettingEntry *item) const
851 if (this->IsFiltered()) return false;
852 if (this == item) return true;
853 return false;
857 * Find setting entry at row \a row_num
858 * @param row_num Index of entry to return
859 * @param cur_row Current row number
860 * @return The requested setting entry or \c nullptr if it not found (folded or filtered)
862 BaseSettingEntry *BaseSettingEntry::FindEntry(uint row_num, uint *cur_row)
864 if (this->IsFiltered()) return nullptr;
865 if (row_num == *cur_row) return this;
866 (*cur_row)++;
867 return nullptr;
871 * Draw a row in the settings panel.
873 * The scrollbar uses rows of the page, while the page data structure is a tree of #SettingsPage and #SettingEntry objects.
874 * As a result, the drawing routing traverses the tree from top to bottom, counting rows in \a cur_row until it reaches \a first_row.
875 * Then it enables drawing rows while traversing until \a max_row is reached, at which point drawing is terminated.
877 * The \a parent_last parameter ensures that the vertical lines at the left are
878 * only drawn when another entry follows, that it prevents output like
879 * \verbatim
880 * |-- setting
881 * |-- (-) - Title
882 * | |-- setting
883 * | |-- setting
884 * \endverbatim
885 * The left-most vertical line is not wanted. It is prevented by setting the
886 * appropriate bit in the \a parent_last parameter.
888 * @param settings_ptr Pointer to current values of all settings
889 * @param left Left-most position in window/panel to start drawing \a first_row
890 * @param right Right-most x position to draw strings at.
891 * @param y Upper-most position in window/panel to start drawing \a first_row
892 * @param first_row First row number to draw
893 * @param max_row Row-number to stop drawing (the row-number of the row below the last row to draw)
894 * @param selected Selected entry by the user.
895 * @param cur_row Current row number (internal variable)
896 * @param parent_last Last-field booleans of parent page level (page level \e i sets bit \e i to 1 if it is its last field)
897 * @return Row number of the next row to draw
899 uint BaseSettingEntry::Draw(GameSettings *settings_ptr, int left, int right, int y, uint first_row, uint max_row, BaseSettingEntry *selected, uint cur_row, uint parent_last) const
901 if (this->IsFiltered()) return cur_row;
902 if (cur_row >= max_row) return cur_row;
904 bool rtl = _current_text_dir == TD_RTL;
905 int offset = rtl ? -4 : 4;
906 int level_width = rtl ? -LEVEL_WIDTH : LEVEL_WIDTH;
908 int x = rtl ? right : left;
909 if (cur_row >= first_row) {
910 int colour = _colour_gradient[COLOUR_ORANGE][4];
911 y += (cur_row - first_row) * SETTING_HEIGHT; // Compute correct y start position
913 /* Draw vertical for parent nesting levels */
914 for (uint lvl = 0; lvl < this->level; lvl++) {
915 if (!HasBit(parent_last, lvl)) GfxDrawLine(x + offset, y, x + offset, y + SETTING_HEIGHT - 1, colour);
916 x += level_width;
918 /* draw own |- prefix */
919 int halfway_y = y + SETTING_HEIGHT / 2;
920 int bottom_y = (flags & SEF_LAST_FIELD) ? halfway_y : y + SETTING_HEIGHT - 1;
921 GfxDrawLine(x + offset, y, x + offset, bottom_y, colour);
922 /* Small horizontal line from the last vertical line */
923 GfxDrawLine(x + offset, halfway_y, x + level_width - offset, halfway_y, colour);
924 x += level_width;
926 this->DrawSetting(settings_ptr, rtl ? left : x, rtl ? x : right, y, this == selected);
928 cur_row++;
930 return cur_row;
933 /* == SettingEntry methods == */
936 * Constructor for a single setting in the 'advanced settings' window
937 * @param name Name of the setting in the setting table
939 SettingEntry::SettingEntry(const char *name)
941 this->name = name;
942 this->setting = nullptr;
943 this->index = 0;
947 * Initialization of a setting entry
948 * @param level Page nesting level of this entry
950 void SettingEntry::Init(byte level)
952 BaseSettingEntry::Init(level);
953 this->setting = GetSettingFromName(this->name, &this->index);
954 assert(this->setting != nullptr);
958 * Set the button-depressed flags (#SEF_LEFT_DEPRESSED and #SEF_RIGHT_DEPRESSED) to a specified value
959 * @param new_val New value for the button flags
960 * @see SettingEntryFlags
962 void SettingEntry::SetButtons(byte new_val)
964 assert((new_val & ~SEF_BUTTONS_MASK) == 0); // Should not touch any flags outside the buttons
965 this->flags = (this->flags & ~SEF_BUTTONS_MASK) | new_val;
968 /** Return number of rows needed to display the (filtered) entry */
969 uint SettingEntry::Length() const
971 return this->IsFiltered() ? 0 : 1;
975 * Get the biggest height of the help text(s), if the width is at least \a maxw. Help text gets wrapped if needed.
976 * @param maxw Maximal width of a line help text.
977 * @return Biggest height needed to display any help text of this node (and its descendants).
979 uint SettingEntry::GetMaxHelpHeight(int maxw)
981 return GetStringHeight(this->GetHelpText(), maxw);
985 * Checks whether an entry shall be made visible based on the restriction mode.
986 * @param mode The current status of the restriction drop down box.
987 * @return true if the entry shall be visible.
989 bool SettingEntry::IsVisibleByRestrictionMode(RestrictionMode mode) const
991 /* There shall not be any restriction, i.e. all settings shall be visible. */
992 if (mode == RM_ALL) return true;
994 GameSettings *settings_ptr = &GetGameSettings();
995 const SettingDesc *sd = this->setting;
997 if (mode == RM_BASIC) return (this->setting->desc.cat & SC_BASIC_LIST) != 0;
998 if (mode == RM_ADVANCED) return (this->setting->desc.cat & SC_ADVANCED_LIST) != 0;
1000 /* Read the current value. */
1001 const void *var = ResolveVariableAddress(settings_ptr, sd);
1002 int64 current_value = ReadValue(var, sd->save.conv);
1004 int64 filter_value;
1006 if (mode == RM_CHANGED_AGAINST_DEFAULT) {
1007 /* This entry shall only be visible, if the value deviates from its default value. */
1009 /* Read the default value. */
1010 filter_value = ReadValue(&sd->desc.def, sd->save.conv);
1011 } else {
1012 assert(mode == RM_CHANGED_AGAINST_NEW);
1013 /* This entry shall only be visible, if the value deviates from
1014 * its value is used when starting a new game. */
1016 /* Make sure we're not comparing the new game settings against itself. */
1017 assert(settings_ptr != &_settings_newgame);
1019 /* Read the new game's value. */
1020 var = ResolveVariableAddress(&_settings_newgame, sd);
1021 filter_value = ReadValue(var, sd->save.conv);
1024 return current_value != filter_value;
1028 * Update the filter state.
1029 * @param filter Filter
1030 * @param force_visible Whether to force all items visible, no matter what (due to filter text; not affected by restriction drop down box).
1031 * @return true if item remains visible
1033 bool SettingEntry::UpdateFilterState(SettingFilter &filter, bool force_visible)
1035 CLRBITS(this->flags, SEF_FILTERED);
1037 bool visible = true;
1039 const SettingDesc *sd = this->setting;
1040 if (!force_visible && !filter.string.IsEmpty()) {
1041 /* Process the search text filter for this item. */
1042 filter.string.ResetState();
1044 const SettingDescBase *sdb = &sd->desc;
1046 SetDParam(0, STR_EMPTY);
1047 filter.string.AddLine(sdb->str);
1048 filter.string.AddLine(this->GetHelpText());
1050 visible = filter.string.GetState();
1053 if (visible) {
1054 if (filter.type != ST_ALL && sd->GetType() != filter.type) {
1055 filter.type_hides = true;
1056 visible = false;
1058 if (!this->IsVisibleByRestrictionMode(filter.mode)) {
1059 while (filter.min_cat < RM_ALL && (filter.min_cat == filter.mode || !this->IsVisibleByRestrictionMode(filter.min_cat))) filter.min_cat++;
1060 visible = false;
1064 if (!visible) SETBITS(this->flags, SEF_FILTERED);
1065 return visible;
1069 static const void *ResolveVariableAddress(const GameSettings *settings_ptr, const SettingDesc *sd)
1071 if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
1072 if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) {
1073 return GetVariableAddress(&Company::Get(_local_company)->settings, &sd->save);
1074 } else {
1075 return GetVariableAddress(&_settings_client.company, &sd->save);
1077 } else {
1078 return GetVariableAddress(settings_ptr, &sd->save);
1083 * Set the DParams for drawing the value of a setting.
1084 * @param first_param First DParam to use
1085 * @param value Setting value to set params for.
1087 void SettingEntry::SetValueDParams(uint first_param, int32 value) const
1089 const SettingDescBase *sdb = &this->setting->desc;
1090 if (sdb->cmd == SDT_BOOLX) {
1091 SetDParam(first_param++, value != 0 ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
1092 } else {
1093 if ((sdb->flags & SGF_MULTISTRING) != 0) {
1094 SetDParam(first_param++, sdb->str_val - sdb->min + value);
1095 } else if ((sdb->flags & SGF_DISPLAY_ABS) != 0) {
1096 SetDParam(first_param++, sdb->str_val + ((value >= 0) ? 1 : 0));
1097 value = abs(value);
1098 } else {
1099 SetDParam(first_param++, sdb->str_val + ((value == 0 && (sdb->flags & SGF_0ISDISABLED) != 0) ? 1 : 0));
1101 SetDParam(first_param++, value);
1106 * Function to draw setting value (button + text + current value)
1107 * @param settings_ptr Pointer to current values of all settings
1108 * @param left Left-most position in window/panel to start drawing
1109 * @param right Right-most position in window/panel to draw
1110 * @param y Upper-most position in window/panel to start drawing
1111 * @param highlight Highlight entry.
1113 void SettingEntry::DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const
1115 const SettingDesc *sd = this->setting;
1116 const SettingDescBase *sdb = &sd->desc;
1117 const void *var = ResolveVariableAddress(settings_ptr, sd);
1118 int state = this->flags & SEF_BUTTONS_MASK;
1120 bool rtl = _current_text_dir == TD_RTL;
1121 uint buttons_left = rtl ? right + 1 - SETTING_BUTTON_WIDTH : left;
1122 uint text_left = left + (rtl ? 0 : SETTING_BUTTON_WIDTH + 5);
1123 uint text_right = right - (rtl ? SETTING_BUTTON_WIDTH + 5 : 0);
1124 uint button_y = y + (SETTING_HEIGHT - SETTING_BUTTON_HEIGHT) / 2;
1126 /* We do not allow changes of some items when we are a client in a networkgame */
1127 bool editable = sd->IsEditable();
1129 SetDParam(0, highlight ? STR_ORANGE_STRING1_WHITE : STR_ORANGE_STRING1_LTBLUE);
1130 int32 value = (int32)ReadValue(var, sd->save.conv);
1131 if (sdb->cmd == SDT_BOOLX) {
1132 /* Draw checkbox for boolean-value either on/off */
1133 DrawBoolButton(buttons_left, button_y, value != 0, editable);
1134 } else if ((sdb->flags & SGF_MULTISTRING) != 0) {
1135 /* Draw [v] button for settings of an enum-type */
1136 DrawDropDownButton(buttons_left, button_y, COLOUR_YELLOW, state != 0, editable);
1137 } else {
1138 /* Draw [<][>] boxes for settings of an integer-type */
1139 DrawArrowButtons(buttons_left, button_y, COLOUR_YELLOW, state,
1140 editable && value != (sdb->flags & SGF_0ISDISABLED ? 0 : sdb->min), editable && (uint32)value != sdb->max);
1142 this->SetValueDParams(1, value);
1143 DrawString(text_left, text_right, y + (SETTING_HEIGHT - FONT_HEIGHT_NORMAL) / 2, sdb->str, highlight ? TC_WHITE : TC_LIGHT_BLUE);
1146 /* == SettingsContainer methods == */
1149 * Initialization of an entire setting page
1150 * @param level Nesting level of this page (internal variable, do not provide a value for it when calling)
1152 void SettingsContainer::Init(byte level)
1154 for (EntryVector::iterator it = this->entries.begin(); it != this->entries.end(); ++it) {
1155 (*it)->Init(level);
1159 /** Recursively close all folds of sub-pages */
1160 void SettingsContainer::FoldAll()
1162 for (EntryVector::iterator it = this->entries.begin(); it != this->entries.end(); ++it) {
1163 (*it)->FoldAll();
1167 /** Recursively open all folds of sub-pages */
1168 void SettingsContainer::UnFoldAll()
1170 for (EntryVector::iterator it = this->entries.begin(); it != this->entries.end(); ++it) {
1171 (*it)->UnFoldAll();
1176 * Recursively accumulate the folding state of the tree.
1177 * @param[in,out] all_folded Set to false, if one entry is not folded.
1178 * @param[in,out] all_unfolded Set to false, if one entry is folded.
1180 void SettingsContainer::GetFoldingState(bool &all_folded, bool &all_unfolded) const
1182 for (EntryVector::const_iterator it = this->entries.begin(); it != this->entries.end(); ++it) {
1183 (*it)->GetFoldingState(all_folded, all_unfolded);
1188 * Update the filter state.
1189 * @param filter Filter
1190 * @param force_visible Whether to force all items visible, no matter what
1191 * @return true if item remains visible
1193 bool SettingsContainer::UpdateFilterState(SettingFilter &filter, bool force_visible)
1195 bool visible = false;
1196 bool first_visible = true;
1197 for (EntryVector::reverse_iterator it = this->entries.rbegin(); it != this->entries.rend(); ++it) {
1198 visible |= (*it)->UpdateFilterState(filter, force_visible);
1199 (*it)->SetLastField(first_visible);
1200 if (visible && first_visible) first_visible = false;
1202 return visible;
1207 * Check whether an entry is visible and not folded or filtered away.
1208 * Note: This does not consider the scrolling range; it might still require scrolling to make the setting really visible.
1209 * @param item Entry to search for.
1210 * @return true if entry is visible.
1212 bool SettingsContainer::IsVisible(const BaseSettingEntry *item) const
1214 for (EntryVector::const_iterator it = this->entries.begin(); it != this->entries.end(); ++it) {
1215 if ((*it)->IsVisible(item)) return true;
1217 return false;
1220 /** Return number of rows needed to display the whole page */
1221 uint SettingsContainer::Length() const
1223 uint length = 0;
1224 for (EntryVector::const_iterator it = this->entries.begin(); it != this->entries.end(); ++it) {
1225 length += (*it)->Length();
1227 return length;
1231 * Find the setting entry at row number \a row_num
1232 * @param row_num Index of entry to return
1233 * @param cur_row Variable used for keeping track of the current row number. Should point to memory initialized to \c 0 when first called.
1234 * @return The requested setting entry or \c nullptr if it does not exist
1236 BaseSettingEntry *SettingsContainer::FindEntry(uint row_num, uint *cur_row)
1238 BaseSettingEntry *pe = nullptr;
1239 for (EntryVector::iterator it = this->entries.begin(); it != this->entries.end(); ++it) {
1240 pe = (*it)->FindEntry(row_num, cur_row);
1241 if (pe != nullptr) {
1242 break;
1245 return pe;
1249 * Get the biggest height of the help texts, if the width is at least \a maxw. Help text gets wrapped if needed.
1250 * @param maxw Maximal width of a line help text.
1251 * @return Biggest height needed to display any help text of this (sub-)tree.
1253 uint SettingsContainer::GetMaxHelpHeight(int maxw)
1255 uint biggest = 0;
1256 for (EntryVector::const_iterator it = this->entries.begin(); it != this->entries.end(); ++it) {
1257 biggest = max(biggest, (*it)->GetMaxHelpHeight(maxw));
1259 return biggest;
1264 * Draw a row in the settings panel.
1266 * @param settings_ptr Pointer to current values of all settings
1267 * @param left Left-most position in window/panel to start drawing \a first_row
1268 * @param right Right-most x position to draw strings at.
1269 * @param y Upper-most position in window/panel to start drawing \a first_row
1270 * @param first_row First row number to draw
1271 * @param max_row Row-number to stop drawing (the row-number of the row below the last row to draw)
1272 * @param selected Selected entry by the user.
1273 * @param cur_row Current row number (internal variable)
1274 * @param parent_last Last-field booleans of parent page level (page level \e i sets bit \e i to 1 if it is its last field)
1275 * @return Row number of the next row to draw
1277 uint SettingsContainer::Draw(GameSettings *settings_ptr, int left, int right, int y, uint first_row, uint max_row, BaseSettingEntry *selected, uint cur_row, uint parent_last) const
1279 for (EntryVector::const_iterator it = this->entries.begin(); it != this->entries.end(); ++it) {
1280 cur_row = (*it)->Draw(settings_ptr, left, right, y, first_row, max_row, selected, cur_row, parent_last);
1281 if (cur_row >= max_row) {
1282 break;
1285 return cur_row;
1288 /* == SettingsPage methods == */
1291 * Constructor for a sub-page in the 'advanced settings' window
1292 * @param title Title of the sub-page
1294 SettingsPage::SettingsPage(StringID title)
1296 this->title = title;
1297 this->folded = true;
1301 * Initialization of an entire setting page
1302 * @param level Nesting level of this page (internal variable, do not provide a value for it when calling)
1304 void SettingsPage::Init(byte level)
1306 BaseSettingEntry::Init(level);
1307 SettingsContainer::Init(level + 1);
1310 /** Recursively close all (filtered) folds of sub-pages */
1311 void SettingsPage::FoldAll()
1313 if (this->IsFiltered()) return;
1314 this->folded = true;
1316 SettingsContainer::FoldAll();
1319 /** Recursively open all (filtered) folds of sub-pages */
1320 void SettingsPage::UnFoldAll()
1322 if (this->IsFiltered()) return;
1323 this->folded = false;
1325 SettingsContainer::UnFoldAll();
1329 * Recursively accumulate the folding state of the (filtered) tree.
1330 * @param[in,out] all_folded Set to false, if one entry is not folded.
1331 * @param[in,out] all_unfolded Set to false, if one entry is folded.
1333 void SettingsPage::GetFoldingState(bool &all_folded, bool &all_unfolded) const
1335 if (this->IsFiltered()) return;
1337 if (this->folded) {
1338 all_unfolded = false;
1339 } else {
1340 all_folded = false;
1343 SettingsContainer::GetFoldingState(all_folded, all_unfolded);
1347 * Update the filter state.
1348 * @param filter Filter
1349 * @param force_visible Whether to force all items visible, no matter what (due to filter text; not affected by restriction drop down box).
1350 * @return true if item remains visible
1352 bool SettingsPage::UpdateFilterState(SettingFilter &filter, bool force_visible)
1354 if (!force_visible && !filter.string.IsEmpty()) {
1355 filter.string.ResetState();
1356 filter.string.AddLine(this->title);
1357 force_visible = filter.string.GetState();
1360 bool visible = SettingsContainer::UpdateFilterState(filter, force_visible);
1361 if (visible) {
1362 CLRBITS(this->flags, SEF_FILTERED);
1363 } else {
1364 SETBITS(this->flags, SEF_FILTERED);
1366 return visible;
1370 * Check whether an entry is visible and not folded or filtered away.
1371 * Note: This does not consider the scrolling range; it might still require scrolling to make the setting really visible.
1372 * @param item Entry to search for.
1373 * @return true if entry is visible.
1375 bool SettingsPage::IsVisible(const BaseSettingEntry *item) const
1377 if (this->IsFiltered()) return false;
1378 if (this == item) return true;
1379 if (this->folded) return false;
1381 return SettingsContainer::IsVisible(item);
1384 /** Return number of rows needed to display the (filtered) entry */
1385 uint SettingsPage::Length() const
1387 if (this->IsFiltered()) return 0;
1388 if (this->folded) return 1; // Only displaying the title
1390 return 1 + SettingsContainer::Length();
1394 * Find setting entry at row \a row_num
1395 * @param row_num Index of entry to return
1396 * @param cur_row Current row number
1397 * @return The requested setting entry or \c nullptr if it not found (folded or filtered)
1399 BaseSettingEntry *SettingsPage::FindEntry(uint row_num, uint *cur_row)
1401 if (this->IsFiltered()) return nullptr;
1402 if (row_num == *cur_row) return this;
1403 (*cur_row)++;
1404 if (this->folded) return nullptr;
1406 return SettingsContainer::FindEntry(row_num, cur_row);
1410 * Draw a row in the settings panel.
1412 * @param settings_ptr Pointer to current values of all settings
1413 * @param left Left-most position in window/panel to start drawing \a first_row
1414 * @param right Right-most x position to draw strings at.
1415 * @param y Upper-most position in window/panel to start drawing \a first_row
1416 * @param first_row First row number to draw
1417 * @param max_row Row-number to stop drawing (the row-number of the row below the last row to draw)
1418 * @param selected Selected entry by the user.
1419 * @param cur_row Current row number (internal variable)
1420 * @param parent_last Last-field booleans of parent page level (page level \e i sets bit \e i to 1 if it is its last field)
1421 * @return Row number of the next row to draw
1423 uint SettingsPage::Draw(GameSettings *settings_ptr, int left, int right, int y, uint first_row, uint max_row, BaseSettingEntry *selected, uint cur_row, uint parent_last) const
1425 if (this->IsFiltered()) return cur_row;
1426 if (cur_row >= max_row) return cur_row;
1428 cur_row = BaseSettingEntry::Draw(settings_ptr, left, right, y, first_row, max_row, selected, cur_row, parent_last);
1430 if (!this->folded) {
1431 if (this->flags & SEF_LAST_FIELD) {
1432 assert(this->level < 8 * sizeof(parent_last));
1433 SetBit(parent_last, this->level); // Add own last-field state
1436 cur_row = SettingsContainer::Draw(settings_ptr, left, right, y, first_row, max_row, selected, cur_row, parent_last);
1439 return cur_row;
1443 * Function to draw setting value (button + text + current value)
1444 * @param settings_ptr Pointer to current values of all settings
1445 * @param left Left-most position in window/panel to start drawing
1446 * @param right Right-most position in window/panel to draw
1447 * @param y Upper-most position in window/panel to start drawing
1448 * @param highlight Highlight entry.
1450 void SettingsPage::DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const
1452 bool rtl = _current_text_dir == TD_RTL;
1453 DrawSprite((this->folded ? SPR_CIRCLE_FOLDED : SPR_CIRCLE_UNFOLDED), PAL_NONE, rtl ? right - _circle_size.width : left, y + (SETTING_HEIGHT - _circle_size.height) / 2);
1454 DrawString(rtl ? left : left + _circle_size.width + 2, rtl ? right - _circle_size.width - 2 : right, y + (SETTING_HEIGHT - FONT_HEIGHT_NORMAL) / 2, this->title);
1457 /** Construct settings tree */
1458 static SettingsContainer &GetSettingsTree()
1460 static SettingsContainer *main = nullptr;
1462 if (main == nullptr)
1464 /* Build up the dynamic settings-array only once per OpenTTD session */
1465 main = new SettingsContainer();
1467 SettingsPage *localisation = main->Add(new SettingsPage(STR_CONFIG_SETTING_LOCALISATION));
1469 localisation->Add(new SettingEntry("locale.units_velocity"));
1470 localisation->Add(new SettingEntry("locale.units_power"));
1471 localisation->Add(new SettingEntry("locale.units_weight"));
1472 localisation->Add(new SettingEntry("locale.units_volume"));
1473 localisation->Add(new SettingEntry("locale.units_force"));
1474 localisation->Add(new SettingEntry("locale.units_height"));
1475 localisation->Add(new SettingEntry("gui.date_format_in_default_names"));
1478 SettingsPage *graphics = main->Add(new SettingsPage(STR_CONFIG_SETTING_GRAPHICS));
1480 graphics->Add(new SettingEntry("gui.zoom_min"));
1481 graphics->Add(new SettingEntry("gui.zoom_max"));
1482 graphics->Add(new SettingEntry("gui.smallmap_land_colour"));
1483 graphics->Add(new SettingEntry("gui.graph_line_thickness"));
1484 graphics->Add(new SettingEntry("gui.show_vehicle_route_steps"));
1485 graphics->Add(new SettingEntry("gui.show_vehicle_route"));
1486 graphics->Add(new SettingEntry("gui.dash_level_of_route_lines"));
1489 SettingsPage *sound = main->Add(new SettingsPage(STR_CONFIG_SETTING_SOUND));
1491 sound->Add(new SettingEntry("sound.click_beep"));
1492 sound->Add(new SettingEntry("sound.confirm"));
1493 sound->Add(new SettingEntry("sound.news_ticker"));
1494 sound->Add(new SettingEntry("sound.news_full"));
1495 sound->Add(new SettingEntry("sound.new_year"));
1496 sound->Add(new SettingEntry("sound.disaster"));
1497 sound->Add(new SettingEntry("sound.vehicle"));
1498 sound->Add(new SettingEntry("sound.ambient"));
1501 SettingsPage *interface = main->Add(new SettingsPage(STR_CONFIG_SETTING_INTERFACE));
1503 SettingsPage *general = interface->Add(new SettingsPage(STR_CONFIG_SETTING_INTERFACE_GENERAL));
1505 general->Add(new SettingEntry("gui.osk_activation"));
1506 general->Add(new SettingEntry("gui.hover_delay_ms"));
1507 general->Add(new SettingEntry("gui.errmsg_duration"));
1508 general->Add(new SettingEntry("gui.window_snap_radius"));
1509 general->Add(new SettingEntry("gui.window_soft_limit"));
1510 general->Add(new SettingEntry("gui.right_mouse_wnd_close"));
1513 SettingsPage *viewports = interface->Add(new SettingsPage(STR_CONFIG_SETTING_INTERFACE_VIEWPORTS));
1515 SettingsPage *viewport_map = interface->Add(new SettingsPage(STR_CONFIG_SETTING_VIEWPORT_MAP_OPTIONS));
1517 viewport_map->Add(new SettingEntry("gui.default_viewport_map_mode"));
1518 viewport_map->Add(new SettingEntry("gui.action_when_viewport_map_is_dblclicked"));
1519 viewport_map->Add(new SettingEntry("gui.viewport_map_scan_surroundings"));
1520 viewport_map->Add(new SettingEntry("gui.show_scrolling_viewport_on_map"));
1521 viewport_map->Add(new SettingEntry("gui.show_slopes_on_viewport_map"));
1522 viewport_map->Add(new SettingEntry("gui.show_bridges_on_map"));
1523 viewport_map->Add(new SettingEntry("gui.show_tunnels_on_map"));
1524 viewport_map->Add(new SettingEntry("gui.use_owner_colour_for_tunnelbridge"));
1527 viewports->Add(new SettingEntry("gui.auto_scrolling"));
1528 viewports->Add(new SettingEntry("gui.reverse_scroll"));
1529 viewports->Add(new SettingEntry("gui.smooth_scroll"));
1530 viewports->Add(new SettingEntry("gui.left_mouse_btn_scrolling"));
1531 /* While the horizontal scrollwheel scrolling is written as general code, only
1532 * the cocoa (OSX) driver generates input for it.
1533 * Since it's also able to completely disable the scrollwheel will we display it on all platforms anyway */
1534 viewports->Add(new SettingEntry("gui.scrollwheel_scrolling"));
1535 viewports->Add(new SettingEntry("gui.scrollwheel_multiplier"));
1536 #ifdef __APPLE__
1537 /* We might need to emulate a right mouse button on mac */
1538 viewports->Add(new SettingEntry("gui.right_mouse_btn_emulation"));
1539 #endif
1540 viewports->Add(new SettingEntry("gui.population_in_label"));
1541 viewports->Add(new SettingEntry("gui.liveries"));
1542 viewports->Add(new SettingEntry("construction.train_signal_side"));
1543 viewports->Add(new SettingEntry("gui.measure_tooltip"));
1544 viewports->Add(new SettingEntry("gui.loading_indicators"));
1545 viewports->Add(new SettingEntry("gui.show_track_reservation"));
1546 viewports->Add(new SettingEntry("gui.show_track_overgrowth"));
1549 SettingsPage *construction = interface->Add(new SettingsPage(STR_CONFIG_SETTING_INTERFACE_CONSTRUCTION));
1551 construction->Add(new SettingEntry("gui.link_terraform_toolbar"));
1552 construction->Add(new SettingEntry("gui.enable_signal_gui"));
1553 construction->Add(new SettingEntry("gui.persistent_buildingtools"));
1554 construction->Add(new SettingEntry("gui.quick_goto"));
1555 construction->Add(new SettingEntry("gui.default_rail_type"));
1556 construction->Add(new SettingEntry("gui.disable_unsuitable_building"));
1559 SettingsPage *departureboards = interface->Add(new SettingsPage(STR_CONFIG_SETTING_INTERFACE_DEPARTUREBOARDS));
1561 departureboards->Add(new SettingEntry("gui.max_departures"));
1562 departureboards->Add(new SettingEntry("gui.departure_calc_frequency"));
1563 departureboards->Add(new SettingEntry("gui.departure_show_vehicle"));
1564 departureboards->Add(new SettingEntry("gui.departure_show_group"));
1565 departureboards->Add(new SettingEntry("gui.departure_show_company"));
1566 departureboards->Add(new SettingEntry("gui.departure_show_vehicle_type"));
1567 departureboards->Add(new SettingEntry("gui.departure_show_vehicle_color"));
1568 departureboards->Add(new SettingEntry("gui.departure_larger_font"));
1569 departureboards->Add(new SettingEntry("gui.departure_always_scroll"));
1570 departureboards->Add(new SettingEntry("gui.departure_destination_type"));
1571 departureboards->Add(new SettingEntry("gui.departure_show_both"));
1572 departureboards->Add(new SettingEntry("gui.departure_only_passengers"));
1573 departureboards->Add(new SettingEntry("gui.departure_smart_terminus"));
1574 departureboards->Add(new SettingEntry("gui.departure_conditionals"));
1575 departureboards->Add(new SettingEntry("gui.departure_show_all_stops"));
1576 departureboards->Add(new SettingEntry("gui.departure_merge_identical"));
1579 interface->Add(new SettingEntry("gui.autosave"));
1580 interface->Add(new SettingEntry("gui.toolbar_pos"));
1581 interface->Add(new SettingEntry("gui.statusbar_pos"));
1582 interface->Add(new SettingEntry("gui.prefer_teamchat"));
1583 interface->Add(new SettingEntry("gui.advanced_vehicle_list"));
1584 interface->Add(new SettingEntry("gui.advanced_train_purchase_window"));
1585 interface->Add(new SettingEntry("gui.timetable_arrival_departure"));
1586 interface->Add(new SettingEntry("gui.ticks_per_minute"));
1587 interface->Add(new SettingEntry("gui.expenses_layout"));
1590 SettingsPage *advisors = main->Add(new SettingsPage(STR_CONFIG_SETTING_ADVISORS));
1592 advisors->Add(new SettingEntry("gui.coloured_news_year"));
1593 advisors->Add(new SettingEntry("news_display.general"));
1594 advisors->Add(new SettingEntry("news_display.new_vehicles"));
1595 advisors->Add(new SettingEntry("news_display.accident"));
1596 advisors->Add(new SettingEntry("news_display.company_info"));
1597 advisors->Add(new SettingEntry("news_display.acceptance"));
1598 advisors->Add(new SettingEntry("news_display.arrival_player"));
1599 advisors->Add(new SettingEntry("news_display.arrival_other"));
1600 advisors->Add(new SettingEntry("news_display.advice"));
1601 advisors->Add(new SettingEntry("gui.order_review_system"));
1602 advisors->Add(new SettingEntry("gui.vehicle_income_warn"));
1603 advisors->Add(new SettingEntry("gui.lost_vehicle_warn"));
1604 advisors->Add(new SettingEntry("gui.show_finances"));
1605 advisors->Add(new SettingEntry("news_display.economy"));
1606 advisors->Add(new SettingEntry("news_display.subsidies"));
1607 advisors->Add(new SettingEntry("news_display.open"));
1608 advisors->Add(new SettingEntry("news_display.close"));
1609 advisors->Add(new SettingEntry("news_display.production_player"));
1610 advisors->Add(new SettingEntry("news_display.production_other"));
1611 advisors->Add(new SettingEntry("news_display.production_nobody"));
1614 SettingsPage *company = main->Add(new SettingsPage(STR_CONFIG_SETTING_COMPANY));
1616 company->Add(new SettingEntry("gui.semaphore_build_before"));
1617 company->Add(new SettingEntry("gui.default_signal_type"));
1618 company->Add(new SettingEntry("gui.cycle_signal_types"));
1619 company->Add(new SettingEntry("gui.drag_signals_fixed_distance"));
1620 company->Add(new SettingEntry("construction.simulated_wormhole_signals"));
1621 company->Add(new SettingEntry("gui.new_nonstop"));
1622 company->Add(new SettingEntry("gui.stop_location"));
1623 company->Add(new SettingEntry("company.engine_renew"));
1624 company->Add(new SettingEntry("company.engine_renew_months"));
1625 company->Add(new SettingEntry("company.engine_renew_money"));
1626 company->Add(new SettingEntry("vehicle.servint_ispercent"));
1627 company->Add(new SettingEntry("vehicle.servint_trains"));
1628 company->Add(new SettingEntry("vehicle.servint_roadveh"));
1629 company->Add(new SettingEntry("vehicle.servint_ships"));
1630 company->Add(new SettingEntry("vehicle.servint_aircraft"));
1633 SettingsPage *accounting = main->Add(new SettingsPage(STR_CONFIG_SETTING_ACCOUNTING));
1635 accounting->Add(new SettingEntry("economy.inflation"));
1636 accounting->Add(new SettingEntry("difficulty.initial_interest"));
1637 accounting->Add(new SettingEntry("difficulty.max_loan"));
1638 accounting->Add(new SettingEntry("difficulty.subsidy_multiplier"));
1639 accounting->Add(new SettingEntry("economy.infrastructure_maintenance"));
1640 accounting->Add(new SettingEntry("difficulty.vehicle_costs"));
1641 accounting->Add(new SettingEntry("difficulty.construction_cost"));
1644 SettingsPage *vehicles = main->Add(new SettingsPage(STR_CONFIG_SETTING_VEHICLES));
1646 SettingsPage *physics = vehicles->Add(new SettingsPage(STR_CONFIG_SETTING_VEHICLES_PHYSICS));
1648 physics->Add(new SettingEntry("vehicle.train_acceleration_model"));
1649 physics->Add(new SettingEntry("vehicle.train_slope_steepness"));
1650 physics->Add(new SettingEntry("vehicle.wagon_speed_limits"));
1651 physics->Add(new SettingEntry("vehicle.train_speed_adaption"));
1652 physics->Add(new SettingEntry("vehicle.freight_trains"));
1653 physics->Add(new SettingEntry("vehicle.roadveh_acceleration_model"));
1654 physics->Add(new SettingEntry("vehicle.roadveh_slope_steepness"));
1655 physics->Add(new SettingEntry("vehicle.smoke_amount"));
1656 physics->Add(new SettingEntry("vehicle.plane_speed"));
1657 physics->Add(new SettingEntry("vehicle.slow_road_vehicles_in_curves"));
1660 SettingsPage *routing = vehicles->Add(new SettingsPage(STR_CONFIG_SETTING_VEHICLES_ROUTING));
1662 routing->Add(new SettingEntry("pf.pathfinder_for_trains"));
1663 routing->Add(new SettingEntry("difficulty.line_reverse_mode"));
1664 routing->Add(new SettingEntry("pf.reverse_at_signals"));
1665 routing->Add(new SettingEntry("pf.back_of_one_way_pbs_waiting_point"));
1666 routing->Add(new SettingEntry("pf.forbid_90_deg"));
1667 routing->Add(new SettingEntry("pf.pathfinder_for_roadvehs"));
1668 routing->Add(new SettingEntry("pf.pathfinder_for_ships"));
1671 vehicles->Add(new SettingEntry("gui.specific_group_name"));
1672 vehicles->Add(new SettingEntry("order.no_servicing_if_no_breakdowns"));
1673 vehicles->Add(new SettingEntry("order.serviceathelipad"));
1674 vehicles->Add(new SettingEntry("order.automatic_timetable_separation"));
1675 vehicles->Add(new SettingEntry("order.timetable_auto_travel_buffer"));
1676 vehicles->Add(new SettingEntry("order.timetable_auto_load_buffer"));
1677 vehicles->Add(new SettingEntry("order.timetable_auto_travel_rounding"));
1678 vehicles->Add(new SettingEntry("order.timetable_auto_load_rounding"));
1681 SettingsPage *limitations = main->Add(new SettingsPage(STR_CONFIG_SETTING_LIMITATIONS));
1683 limitations->Add(new SettingEntry("construction.command_pause_level"));
1684 limitations->Add(new SettingEntry("construction.autoslope"));
1685 limitations->Add(new SettingEntry("construction.extra_dynamite"));
1686 limitations->Add(new SettingEntry("construction.max_heightlevel"));
1687 limitations->Add(new SettingEntry("construction.max_bridge_length"));
1688 limitations->Add(new SettingEntry("construction.max_bridge_height"));
1689 limitations->Add(new SettingEntry("construction.max_tunnel_length"));
1690 limitations->Add(new SettingEntry("construction.chunnel"));
1691 limitations->Add(new SettingEntry("station.never_expire_airports"));
1692 limitations->Add(new SettingEntry("vehicle.never_expire_vehicles"));
1693 limitations->Add(new SettingEntry("vehicle.max_trains"));
1694 limitations->Add(new SettingEntry("vehicle.max_roadveh"));
1695 limitations->Add(new SettingEntry("vehicle.max_aircraft"));
1696 limitations->Add(new SettingEntry("vehicle.max_ships"));
1697 limitations->Add(new SettingEntry("vehicle.max_train_length"));
1698 limitations->Add(new SettingEntry("station.station_spread"));
1699 limitations->Add(new SettingEntry("station.distant_join_stations"));
1700 limitations->Add(new SettingEntry("construction.road_stop_on_town_road"));
1701 limitations->Add(new SettingEntry("construction.road_stop_on_competitor_road"));
1702 limitations->Add(new SettingEntry("vehicle.disable_elrails"));
1703 limitations->Add(new SettingEntry("construction.road_custom_bridge_heads"));
1706 SettingsPage *disasters = main->Add(new SettingsPage(STR_CONFIG_SETTING_ACCIDENTS));
1708 disasters->Add(new SettingEntry("difficulty.disasters"));
1709 disasters->Add(new SettingEntry("difficulty.economy"));
1710 disasters->Add(new SettingEntry("difficulty.vehicle_breakdowns"));
1711 disasters->Add(new SettingEntry("vehicle.plane_crashes"));
1714 SettingsPage *genworld = main->Add(new SettingsPage(STR_CONFIG_SETTING_GENWORLD));
1716 genworld->Add(new SettingEntry("game_creation.landscape"));
1717 genworld->Add(new SettingEntry("game_creation.land_generator"));
1718 genworld->Add(new SettingEntry("difficulty.terrain_type"));
1719 genworld->Add(new SettingEntry("game_creation.tgen_smoothness"));
1720 genworld->Add(new SettingEntry("game_creation.variety"));
1721 genworld->Add(new SettingEntry("game_creation.snow_line_height"));
1722 genworld->Add(new SettingEntry("game_creation.amount_of_rivers"));
1723 genworld->Add(new SettingEntry("game_creation.min_river_length"));
1724 genworld->Add(new SettingEntry("game_creation.tree_placer"));
1725 genworld->Add(new SettingEntry("game_creation.tree_line_height"));
1726 genworld->Add(new SettingEntry("construction.trees_around_snow_line_range"));
1727 genworld->Add(new SettingEntry("vehicle.road_side"));
1728 genworld->Add(new SettingEntry("economy.larger_towns"));
1729 genworld->Add(new SettingEntry("economy.initial_city_size"));
1730 genworld->Add(new SettingEntry("economy.town_cargo_factor"));
1731 genworld->Add(new SettingEntry("economy.town_layout"));
1732 genworld->Add(new SettingEntry("economy.town_min_distance"));
1733 genworld->Add(new SettingEntry("economy.max_town_heightlevel"));
1734 genworld->Add(new SettingEntry("game_creation.build_public_roads"));
1735 genworld->Add(new SettingEntry("difficulty.industry_density"));
1736 genworld->Add(new SettingEntry("gui.pause_on_newgame"));
1739 SettingsPage *environment = main->Add(new SettingsPage(STR_CONFIG_SETTING_ENVIRONMENT));
1741 SettingsPage *authorities = environment->Add(new SettingsPage(STR_CONFIG_SETTING_ENVIRONMENT_AUTHORITIES));
1743 authorities->Add(new SettingEntry("difficulty.town_council_tolerance"));
1744 authorities->Add(new SettingEntry("economy.bribe"));
1745 authorities->Add(new SettingEntry("economy.exclusive_rights"));
1746 authorities->Add(new SettingEntry("economy.fund_roads"));
1747 authorities->Add(new SettingEntry("economy.fund_buildings"));
1748 authorities->Add(new SettingEntry("economy.station_noise_level"));
1751 SettingsPage *towns = environment->Add(new SettingsPage(STR_CONFIG_SETTING_ENVIRONMENT_TOWNS));
1753 towns->Add(new SettingEntry("economy.town_growth_rate"));
1754 towns->Add(new SettingEntry("economy.allow_town_roads"));
1755 towns->Add(new SettingEntry("economy.allow_town_level_crossings"));
1756 towns->Add(new SettingEntry("economy.found_town"));
1759 SettingsPage *industries = environment->Add(new SettingsPage(STR_CONFIG_SETTING_ENVIRONMENT_INDUSTRIES));
1761 industries->Add(new SettingEntry("construction.raw_industry_construction"));
1762 industries->Add(new SettingEntry("construction.industry_platform"));
1763 industries->Add(new SettingEntry("economy.multiple_industry_per_town"));
1764 industries->Add(new SettingEntry("game_creation.oil_refinery_limit"));
1765 industries->Add(new SettingEntry("economy.smooth_economy"));
1768 SettingsPage *cdist = environment->Add(new SettingsPage(STR_CONFIG_SETTING_ENVIRONMENT_CARGODIST));
1770 cdist->Add(new SettingEntry("linkgraph.recalc_time"));
1771 cdist->Add(new SettingEntry("linkgraph.recalc_interval"));
1772 cdist->Add(new SettingEntry("linkgraph.distribution_pax"));
1773 cdist->Add(new SettingEntry("linkgraph.distribution_mail"));
1774 cdist->Add(new SettingEntry("linkgraph.distribution_armoured"));
1775 cdist->Add(new SettingEntry("linkgraph.distribution_default"));
1776 cdist->Add(new SettingEntry("linkgraph.accuracy"));
1777 cdist->Add(new SettingEntry("linkgraph.demand_distance"));
1778 cdist->Add(new SettingEntry("linkgraph.demand_size"));
1779 cdist->Add(new SettingEntry("linkgraph.short_path_saturation"));
1782 environment->Add(new SettingEntry("economy.daylength"));
1783 environment->Add(new SettingEntry("station.modified_catchment"));
1784 environment->Add(new SettingEntry("construction.extra_tree_placement"));
1785 environment->Add(new SettingEntry("construction.tree_growth_rate"));
1788 SettingsPage *ai = main->Add(new SettingsPage(STR_CONFIG_SETTING_AI));
1790 SettingsPage *npc = ai->Add(new SettingsPage(STR_CONFIG_SETTING_AI_NPC));
1792 npc->Add(new SettingEntry("script.settings_profile"));
1793 npc->Add(new SettingEntry("script.script_max_opcode_till_suspend"));
1794 npc->Add(new SettingEntry("difficulty.competitor_speed"));
1795 npc->Add(new SettingEntry("ai.ai_in_multiplayer"));
1796 npc->Add(new SettingEntry("ai.ai_disable_veh_train"));
1797 npc->Add(new SettingEntry("ai.ai_disable_veh_roadveh"));
1798 npc->Add(new SettingEntry("ai.ai_disable_veh_aircraft"));
1799 npc->Add(new SettingEntry("ai.ai_disable_veh_ship"));
1802 ai->Add(new SettingEntry("economy.give_money"));
1803 ai->Add(new SettingEntry("economy.allow_shares"));
1806 main->Init();
1808 return *main;
1811 static const StringID _game_settings_restrict_dropdown[] = {
1812 STR_CONFIG_SETTING_RESTRICT_BASIC, // RM_BASIC
1813 STR_CONFIG_SETTING_RESTRICT_ADVANCED, // RM_ADVANCED
1814 STR_CONFIG_SETTING_RESTRICT_ALL, // RM_ALL
1815 STR_CONFIG_SETTING_RESTRICT_CHANGED_AGAINST_DEFAULT, // RM_CHANGED_AGAINST_DEFAULT
1816 STR_CONFIG_SETTING_RESTRICT_CHANGED_AGAINST_NEW, // RM_CHANGED_AGAINST_NEW
1818 assert_compile(lengthof(_game_settings_restrict_dropdown) == RM_END);
1820 /** Warnings about hidden search results. */
1821 enum WarnHiddenResult {
1822 WHR_NONE, ///< Nothing was filtering matches away.
1823 WHR_CATEGORY, ///< Category setting filtered matches away.
1824 WHR_TYPE, ///< Type setting filtered matches away.
1825 WHR_CATEGORY_TYPE, ///< Both category and type settings filtered matches away.
1828 /** Window to edit settings of the game. */
1829 struct GameSettingsWindow : Window {
1830 static const int SETTINGTREE_LEFT_OFFSET = 5; ///< Position of left edge of setting values
1831 static const int SETTINGTREE_RIGHT_OFFSET = 5; ///< Position of right edge of setting values
1832 static const int SETTINGTREE_TOP_OFFSET = 5; ///< Position of top edge of setting values
1833 static const int SETTINGTREE_BOTTOM_OFFSET = 5; ///< Position of bottom edge of setting values
1835 static GameSettings *settings_ptr; ///< Pointer to the game settings being displayed and modified.
1837 SettingEntry *valuewindow_entry; ///< If non-nullptr, pointer to setting for which a value-entering window has been opened.
1838 SettingEntry *clicked_entry; ///< If non-nullptr, pointer to a clicked numeric setting (with a depressed left or right button).
1839 SettingEntry *last_clicked; ///< If non-nullptr, pointer to the last clicked setting.
1840 SettingEntry *valuedropdown_entry; ///< If non-nullptr, pointer to the value for which a dropdown window is currently opened.
1841 bool closing_dropdown; ///< True, if the dropdown list is currently closing.
1843 SettingFilter filter; ///< Filter for the list.
1844 QueryString filter_editbox; ///< Filter editbox;
1845 bool manually_changed_folding; ///< Whether the user expanded/collapsed something manually.
1846 WarnHiddenResult warn_missing; ///< Whether and how to warn about missing search results.
1847 int warn_lines; ///< Number of lines used for warning about missing search results.
1849 Scrollbar *vscroll;
1851 GameSettingsWindow(WindowDesc *desc) : Window(desc), filter_editbox(50)
1853 this->warn_missing = WHR_NONE;
1854 this->warn_lines = 0;
1855 this->filter.mode = (RestrictionMode)_settings_client.gui.settings_restriction_mode;
1856 this->filter.min_cat = RM_ALL;
1857 this->filter.type = ST_ALL;
1858 this->filter.type_hides = false;
1859 this->settings_ptr = &GetGameSettings();
1861 _circle_size = maxdim(GetSpriteSize(SPR_CIRCLE_FOLDED), GetSpriteSize(SPR_CIRCLE_UNFOLDED));
1862 GetSettingsTree().FoldAll(); // Close all sub-pages
1864 this->valuewindow_entry = nullptr; // No setting entry for which a entry window is opened
1865 this->clicked_entry = nullptr; // No numeric setting buttons are depressed
1866 this->last_clicked = nullptr;
1867 this->valuedropdown_entry = nullptr;
1868 this->closing_dropdown = false;
1869 this->manually_changed_folding = false;
1871 this->CreateNestedTree();
1872 this->vscroll = this->GetScrollbar(WID_GS_SCROLLBAR);
1873 this->FinishInitNested(WN_GAME_OPTIONS_GAME_SETTINGS);
1875 this->querystrings[WID_GS_FILTER] = &this->filter_editbox;
1876 this->filter_editbox.cancel_button = QueryString::ACTION_CLEAR;
1877 this->SetFocusedWidget(WID_GS_FILTER);
1879 this->InvalidateData();
1882 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1884 switch (widget) {
1885 case WID_GS_OPTIONSPANEL:
1886 resize->height = SETTING_HEIGHT = max(max<int>(_circle_size.height, SETTING_BUTTON_HEIGHT), FONT_HEIGHT_NORMAL) + 1;
1887 resize->width = 1;
1889 size->height = 5 * resize->height + SETTINGTREE_TOP_OFFSET + SETTINGTREE_BOTTOM_OFFSET;
1890 break;
1892 case WID_GS_HELP_TEXT: {
1893 static const StringID setting_types[] = {
1894 STR_CONFIG_SETTING_TYPE_CLIENT,
1895 STR_CONFIG_SETTING_TYPE_COMPANY_MENU, STR_CONFIG_SETTING_TYPE_COMPANY_INGAME,
1896 STR_CONFIG_SETTING_TYPE_GAME_MENU, STR_CONFIG_SETTING_TYPE_GAME_INGAME,
1898 for (uint i = 0; i < lengthof(setting_types); i++) {
1899 SetDParam(0, setting_types[i]);
1900 size->width = max(size->width, GetStringBoundingBox(STR_CONFIG_SETTING_TYPE).width);
1902 size->height = 2 * FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL +
1903 max(size->height, GetSettingsTree().GetMaxHelpHeight(size->width));
1904 break;
1907 case WID_GS_RESTRICT_CATEGORY:
1908 case WID_GS_RESTRICT_TYPE:
1909 size->width = max(GetStringBoundingBox(STR_CONFIG_SETTING_RESTRICT_CATEGORY).width, GetStringBoundingBox(STR_CONFIG_SETTING_RESTRICT_TYPE).width);
1910 break;
1912 default:
1913 break;
1917 virtual void OnPaint()
1919 if (this->closing_dropdown) {
1920 this->closing_dropdown = false;
1921 assert(this->valuedropdown_entry != nullptr);
1922 this->valuedropdown_entry->SetButtons(0);
1923 this->valuedropdown_entry = nullptr;
1926 /* Reserve the correct number of lines for the 'some search results are hidden' notice in the central settings display panel. */
1927 const NWidgetBase *panel = this->GetWidget<NWidgetBase>(WID_GS_OPTIONSPANEL);
1928 StringID warn_str = STR_CONFIG_SETTING_CATEGORY_HIDES - 1 + this->warn_missing;
1929 int new_warn_lines;
1930 if (this->warn_missing == WHR_NONE) {
1931 new_warn_lines = 0;
1932 } else {
1933 SetDParam(0, _game_settings_restrict_dropdown[this->filter.min_cat]);
1934 new_warn_lines = GetStringLineCount(warn_str, panel->current_x);
1936 if (this->warn_lines != new_warn_lines) {
1937 this->vscroll->SetCount(this->vscroll->GetCount() - this->warn_lines + new_warn_lines);
1938 this->warn_lines = new_warn_lines;
1941 this->DrawWidgets();
1943 /* Draw the 'some search results are hidden' notice. */
1944 if (this->warn_missing != WHR_NONE) {
1945 const int left = panel->pos_x;
1946 const int right = left + panel->current_x - 1;
1947 const int top = panel->pos_y + WD_FRAMETEXT_TOP + (SETTING_HEIGHT - FONT_HEIGHT_NORMAL) * this->warn_lines / 2;
1948 SetDParam(0, _game_settings_restrict_dropdown[this->filter.min_cat]);
1949 if (this->warn_lines == 1) {
1950 /* If the warning fits at one line, center it. */
1951 DrawString(left + WD_FRAMETEXT_LEFT, right - WD_FRAMETEXT_RIGHT, top, warn_str, TC_FROMSTRING, SA_HOR_CENTER);
1952 } else {
1953 DrawStringMultiLine(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, top, INT32_MAX, warn_str, TC_FROMSTRING, SA_HOR_CENTER);
1958 virtual void SetStringParameters(int widget) const
1960 switch (widget) {
1961 case WID_GS_RESTRICT_DROPDOWN:
1962 SetDParam(0, _game_settings_restrict_dropdown[this->filter.mode]);
1963 break;
1965 case WID_GS_TYPE_DROPDOWN:
1966 switch (this->filter.type) {
1967 case ST_GAME: SetDParam(0, _game_mode == GM_MENU ? STR_CONFIG_SETTING_TYPE_DROPDOWN_GAME_MENU : STR_CONFIG_SETTING_TYPE_DROPDOWN_GAME_INGAME); break;
1968 case ST_COMPANY: SetDParam(0, _game_mode == GM_MENU ? STR_CONFIG_SETTING_TYPE_DROPDOWN_COMPANY_MENU : STR_CONFIG_SETTING_TYPE_DROPDOWN_COMPANY_INGAME); break;
1969 case ST_CLIENT: SetDParam(0, STR_CONFIG_SETTING_TYPE_DROPDOWN_CLIENT); break;
1970 default: SetDParam(0, STR_CONFIG_SETTING_TYPE_DROPDOWN_ALL); break;
1972 break;
1976 DropDownList *BuildDropDownList(int widget) const
1978 DropDownList *list = nullptr;
1979 switch (widget) {
1980 case WID_GS_RESTRICT_DROPDOWN:
1981 list = new DropDownList();
1983 for (int mode = 0; mode != RM_END; mode++) {
1984 /* If we are in adv. settings screen for the new game's settings,
1985 * we don't want to allow comparing with new game's settings. */
1986 bool disabled = mode == RM_CHANGED_AGAINST_NEW && settings_ptr == &_settings_newgame;
1988 *list->Append() = new DropDownListStringItem(_game_settings_restrict_dropdown[mode], mode, disabled);
1990 break;
1992 case WID_GS_TYPE_DROPDOWN:
1993 list = new DropDownList();
1994 *list->Append() = new DropDownListStringItem(STR_CONFIG_SETTING_TYPE_DROPDOWN_ALL, ST_ALL, false);
1995 *list->Append() = new DropDownListStringItem(_game_mode == GM_MENU ? STR_CONFIG_SETTING_TYPE_DROPDOWN_GAME_MENU : STR_CONFIG_SETTING_TYPE_DROPDOWN_GAME_INGAME, ST_GAME, false);
1996 *list->Append() = new DropDownListStringItem(_game_mode == GM_MENU ? STR_CONFIG_SETTING_TYPE_DROPDOWN_COMPANY_MENU : STR_CONFIG_SETTING_TYPE_DROPDOWN_COMPANY_INGAME, ST_COMPANY, false);
1997 *list->Append() = new DropDownListStringItem(STR_CONFIG_SETTING_TYPE_DROPDOWN_CLIENT, ST_CLIENT, false);
1998 break;
2000 return list;
2003 virtual void DrawWidget(const Rect &r, int widget) const
2005 switch (widget) {
2006 case WID_GS_OPTIONSPANEL: {
2007 int top_pos = r.top + SETTINGTREE_TOP_OFFSET + 1 + this->warn_lines * SETTING_HEIGHT;
2008 uint last_row = this->vscroll->GetPosition() + this->vscroll->GetCapacity() - this->warn_lines;
2009 int next_row = GetSettingsTree().Draw(settings_ptr, r.left + SETTINGTREE_LEFT_OFFSET, r.right - SETTINGTREE_RIGHT_OFFSET, top_pos,
2010 this->vscroll->GetPosition(), last_row, this->last_clicked);
2011 if (next_row == 0) DrawString(r.left + SETTINGTREE_LEFT_OFFSET, r.right - SETTINGTREE_RIGHT_OFFSET, top_pos, STR_CONFIG_SETTINGS_NONE);
2012 break;
2015 case WID_GS_HELP_TEXT:
2016 if (this->last_clicked != nullptr) {
2017 const SettingDesc *sd = this->last_clicked->setting;
2019 int y = r.top;
2020 switch (sd->GetType()) {
2021 case ST_COMPANY: SetDParam(0, _game_mode == GM_MENU ? STR_CONFIG_SETTING_TYPE_COMPANY_MENU : STR_CONFIG_SETTING_TYPE_COMPANY_INGAME); break;
2022 case ST_CLIENT: SetDParam(0, STR_CONFIG_SETTING_TYPE_CLIENT); break;
2023 case ST_GAME: SetDParam(0, _game_mode == GM_MENU ? STR_CONFIG_SETTING_TYPE_GAME_MENU : STR_CONFIG_SETTING_TYPE_GAME_INGAME); break;
2024 default: NOT_REACHED();
2026 DrawString(r.left, r.right, y, STR_CONFIG_SETTING_TYPE);
2027 y += FONT_HEIGHT_NORMAL;
2029 int32 default_value = ReadValue(&sd->desc.def, sd->save.conv);
2030 this->last_clicked->SetValueDParams(0, default_value);
2031 DrawString(r.left, r.right, y, STR_CONFIG_SETTING_DEFAULT_VALUE);
2032 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
2034 DrawStringMultiLine(r.left, r.right, y, r.bottom, this->last_clicked->GetHelpText(), TC_WHITE);
2036 break;
2038 default:
2039 break;
2044 * Set the entry that should have its help text displayed, and mark the window dirty so it gets repainted.
2045 * @param pe Setting to display help text of, use \c nullptr to stop displaying help of the currently displayed setting.
2047 void SetDisplayedHelpText(SettingEntry *pe)
2049 if (this->last_clicked != pe) this->SetDirty();
2050 this->last_clicked = pe;
2053 virtual void OnClick(Point pt, int widget, int click_count)
2055 switch (widget) {
2056 case WID_GS_EXPAND_ALL:
2057 this->manually_changed_folding = true;
2058 GetSettingsTree().UnFoldAll();
2059 this->InvalidateData();
2060 break;
2062 case WID_GS_COLLAPSE_ALL:
2063 this->manually_changed_folding = true;
2064 GetSettingsTree().FoldAll();
2065 this->InvalidateData();
2066 break;
2068 case WID_GS_RESTRICT_DROPDOWN: {
2069 DropDownList *list = this->BuildDropDownList(widget);
2070 if (list != nullptr) {
2071 ShowDropDownList(this, list, this->filter.mode, widget);
2073 break;
2076 case WID_GS_TYPE_DROPDOWN: {
2077 DropDownList *list = this->BuildDropDownList(widget);
2078 if (list != nullptr) {
2079 ShowDropDownList(this, list, this->filter.type, widget);
2081 break;
2085 if (widget != WID_GS_OPTIONSPANEL) return;
2087 uint btn = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GS_OPTIONSPANEL, SETTINGTREE_TOP_OFFSET);
2088 if (btn == INT_MAX || (int)btn < this->warn_lines) return;
2089 btn -= this->warn_lines;
2091 uint cur_row = 0;
2092 BaseSettingEntry *clicked_entry = GetSettingsTree().FindEntry(btn, &cur_row);
2094 if (clicked_entry == nullptr) return; // Clicked below the last setting of the page
2096 int x = (_current_text_dir == TD_RTL ? this->width - 1 - pt.x : pt.x) - SETTINGTREE_LEFT_OFFSET - (clicked_entry->level + 1) * LEVEL_WIDTH; // Shift x coordinate
2097 if (x < 0) return; // Clicked left of the entry
2099 SettingsPage *clicked_page = dynamic_cast<SettingsPage*>(clicked_entry);
2100 if (clicked_page != nullptr) {
2101 this->SetDisplayedHelpText(nullptr);
2102 clicked_page->folded = !clicked_page->folded; // Flip 'folded'-ness of the sub-page
2104 this->manually_changed_folding = true;
2106 this->InvalidateData();
2107 return;
2110 SettingEntry *pe = dynamic_cast<SettingEntry*>(clicked_entry);
2111 assert(pe != nullptr);
2112 const SettingDesc *sd = pe->setting;
2114 /* return if action is only active in network, or only settable by server */
2115 if (!sd->IsEditable()) {
2116 this->SetDisplayedHelpText(pe);
2117 return;
2120 const void *var = ResolveVariableAddress(settings_ptr, sd);
2121 int32 value = (int32)ReadValue(var, sd->save.conv);
2123 /* clicked on the icon on the left side. Either scroller, bool on/off or dropdown */
2124 if (x < SETTING_BUTTON_WIDTH && (sd->desc.flags & SGF_MULTISTRING)) {
2125 const SettingDescBase *sdb = &sd->desc;
2126 this->SetDisplayedHelpText(pe);
2128 if (this->valuedropdown_entry == pe) {
2129 /* unclick the dropdown */
2130 HideDropDownMenu(this);
2131 this->closing_dropdown = false;
2132 this->valuedropdown_entry->SetButtons(0);
2133 this->valuedropdown_entry = nullptr;
2134 } else {
2135 if (this->valuedropdown_entry != nullptr) this->valuedropdown_entry->SetButtons(0);
2136 this->closing_dropdown = false;
2138 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_GS_OPTIONSPANEL);
2139 int rel_y = (pt.y - (int)wid->pos_y - SETTINGTREE_TOP_OFFSET) % wid->resize_y;
2141 Rect wi_rect;
2142 wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? SETTING_BUTTON_WIDTH - 1 - x : x);
2143 wi_rect.right = wi_rect.left + SETTING_BUTTON_WIDTH - 1;
2144 wi_rect.top = pt.y - rel_y + (SETTING_HEIGHT - SETTING_BUTTON_HEIGHT) / 2;
2145 wi_rect.bottom = wi_rect.top + SETTING_BUTTON_HEIGHT - 1;
2147 /* For dropdowns we also have to check the y position thoroughly, the mouse may not above the just opening dropdown */
2148 if (pt.y >= wi_rect.top && pt.y <= wi_rect.bottom) {
2149 this->valuedropdown_entry = pe;
2150 this->valuedropdown_entry->SetButtons(SEF_LEFT_DEPRESSED);
2152 DropDownList *list = new DropDownList();
2153 for (int i = sdb->min; i <= (int)sdb->max; i++) {
2154 *list->Append() = new DropDownListStringItem(sdb->str_val + i - sdb->min, i, false);
2157 ShowDropDownListAt(this, list, value, -1, wi_rect, COLOUR_ORANGE, true);
2160 this->SetDirty();
2161 } else if (x < SETTING_BUTTON_WIDTH) {
2162 this->SetDisplayedHelpText(pe);
2163 const SettingDescBase *sdb = &sd->desc;
2164 int32 oldvalue = value;
2166 switch (sdb->cmd) {
2167 case SDT_BOOLX: value ^= 1; break;
2168 case SDT_ONEOFMANY:
2169 case SDT_NUMX: {
2170 /* Add a dynamic step-size to the scroller. In a maximum of
2171 * 50-steps you should be able to get from min to max,
2172 * unless specified otherwise in the 'interval' variable
2173 * of the current setting. */
2174 uint32 step = (sdb->interval == 0) ? ((sdb->max - sdb->min) / 50) : sdb->interval;
2175 if (step == 0) step = 1;
2177 /* don't allow too fast scrolling */
2178 if ((this->flags & WF_TIMEOUT) && this->timeout_timer > 1) {
2179 _left_button_clicked = false;
2180 return;
2183 /* Increase or decrease the value and clamp it to extremes */
2184 if (x >= SETTING_BUTTON_WIDTH / 2) {
2185 value += step;
2186 if (sdb->min < 0) {
2187 assert((int32)sdb->max >= 0);
2188 if (value > (int32)sdb->max) value = (int32)sdb->max;
2189 } else {
2190 if ((uint32)value > sdb->max) value = (int32)sdb->max;
2192 if (value < sdb->min) value = sdb->min; // skip between "disabled" and minimum
2193 } else {
2194 value -= step;
2195 if (value < sdb->min) value = (sdb->flags & SGF_0ISDISABLED) ? 0 : sdb->min;
2198 /* Set up scroller timeout for numeric values */
2199 if (value != oldvalue) {
2200 if (this->clicked_entry != nullptr) { // Release previous buttons if any
2201 this->clicked_entry->SetButtons(0);
2203 this->clicked_entry = pe;
2204 this->clicked_entry->SetButtons((x >= SETTING_BUTTON_WIDTH / 2) != (_current_text_dir == TD_RTL) ? SEF_RIGHT_DEPRESSED : SEF_LEFT_DEPRESSED);
2205 this->SetTimeout();
2206 _left_button_clicked = false;
2208 break;
2211 default: NOT_REACHED();
2214 if (value != oldvalue) {
2215 if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
2216 SetCompanySetting(pe->index, value);
2217 } else {
2218 SetSettingValue(pe->index, value);
2220 this->SetDirty();
2222 } else {
2223 /* Only open editbox if clicked for the second time, and only for types where it is sensible for. */
2224 if (this->last_clicked == pe && sd->desc.cmd != SDT_BOOLX && !(sd->desc.flags & SGF_MULTISTRING)) {
2225 /* Show the correct currency-translated value */
2226 if (sd->desc.flags & SGF_CURRENCY) value *= _currency->rate;
2228 this->valuewindow_entry = pe;
2229 SetDParam(0, value);
2230 ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, this, CS_NUMERAL, QSF_ENABLE_DEFAULT);
2232 this->SetDisplayedHelpText(pe);
2236 virtual void OnTimeout()
2238 if (this->clicked_entry != nullptr) { // On timeout, release any depressed buttons
2239 this->clicked_entry->SetButtons(0);
2240 this->clicked_entry = nullptr;
2241 this->SetDirty();
2245 virtual void OnQueryTextFinished(char *str)
2247 /* The user pressed cancel */
2248 if (str == nullptr) return;
2250 assert(this->valuewindow_entry != nullptr);
2251 const SettingDesc *sd = this->valuewindow_entry->setting;
2253 int32 value;
2254 if (!StrEmpty(str)) {
2255 value = atoi(str);
2257 /* Save the correct currency-translated value */
2258 if (sd->desc.flags & SGF_CURRENCY) value /= _currency->rate;
2259 } else {
2260 value = (int32)(size_t)sd->desc.def;
2263 if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
2264 SetCompanySetting(this->valuewindow_entry->index, value);
2265 } else {
2266 SetSettingValue(this->valuewindow_entry->index, value);
2268 this->SetDirty();
2271 virtual void OnDropdownSelect(int widget, int index)
2273 switch (widget) {
2274 case WID_GS_RESTRICT_DROPDOWN:
2275 this->filter.mode = (RestrictionMode)index;
2276 if (this->filter.mode == RM_CHANGED_AGAINST_DEFAULT ||
2277 this->filter.mode == RM_CHANGED_AGAINST_NEW) {
2279 if (!this->manually_changed_folding) {
2280 /* Expand all when selecting 'changes'. Update the filter state first, in case it becomes less restrictive in some cases. */
2281 GetSettingsTree().UpdateFilterState(this->filter, false);
2282 GetSettingsTree().UnFoldAll();
2284 } else {
2285 /* Non-'changes' filter. Save as default. */
2286 _settings_client.gui.settings_restriction_mode = this->filter.mode;
2288 this->InvalidateData();
2289 break;
2291 case WID_GS_TYPE_DROPDOWN:
2292 this->filter.type = (SettingType)index;
2293 this->InvalidateData();
2294 break;
2296 default:
2297 if (widget < 0) {
2298 /* Deal with drop down boxes on the panel. */
2299 assert(this->valuedropdown_entry != nullptr);
2300 const SettingDesc *sd = this->valuedropdown_entry->setting;
2301 assert(sd->desc.flags & SGF_MULTISTRING);
2303 if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
2304 SetCompanySetting(this->valuedropdown_entry->index, index);
2305 } else {
2306 SetSettingValue(this->valuedropdown_entry->index, index);
2309 this->SetDirty();
2311 break;
2315 virtual void OnDropdownClose(Point pt, int widget, int index, bool instant_close)
2317 if (widget >= 0) {
2318 /* Normally the default implementation of OnDropdownClose() takes care of
2319 * a few things. We want that behaviour here too, but only for
2320 * "normal" dropdown boxes. The special dropdown boxes added for every
2321 * setting that needs one can't have this call. */
2322 Window::OnDropdownClose(pt, widget, index, instant_close);
2323 } else {
2324 /* We cannot raise the dropdown button just yet. OnClick needs some hint, whether
2325 * the same dropdown button was clicked again, and then not open the dropdown again.
2326 * So, we only remember that it was closed, and process it on the next OnPaint, which is
2327 * after OnClick. */
2328 assert(this->valuedropdown_entry != nullptr);
2329 this->closing_dropdown = true;
2330 this->SetDirty();
2334 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
2336 if (!gui_scope) return;
2338 /* Update which settings are to be visible. */
2339 RestrictionMode min_level = (this->filter.mode <= RM_ALL) ? this->filter.mode : RM_BASIC;
2340 this->filter.min_cat = min_level;
2341 this->filter.type_hides = false;
2342 GetSettingsTree().UpdateFilterState(this->filter, false);
2344 if (this->filter.string.IsEmpty()) {
2345 this->warn_missing = WHR_NONE;
2346 } else if (min_level < this->filter.min_cat) {
2347 this->warn_missing = this->filter.type_hides ? WHR_CATEGORY_TYPE : WHR_CATEGORY;
2348 } else {
2349 this->warn_missing = this->filter.type_hides ? WHR_TYPE : WHR_NONE;
2351 this->vscroll->SetCount(GetSettingsTree().Length() + this->warn_lines);
2353 if (this->last_clicked != nullptr && !GetSettingsTree().IsVisible(this->last_clicked)) {
2354 this->SetDisplayedHelpText(nullptr);
2357 bool all_folded = true;
2358 bool all_unfolded = true;
2359 GetSettingsTree().GetFoldingState(all_folded, all_unfolded);
2360 this->SetWidgetDisabledState(WID_GS_EXPAND_ALL, all_unfolded);
2361 this->SetWidgetDisabledState(WID_GS_COLLAPSE_ALL, all_folded);
2364 virtual void OnEditboxChanged(int wid)
2366 if (wid == WID_GS_FILTER) {
2367 this->filter.string.SetFilterTerm(this->filter_editbox.text.buf);
2368 if (!this->filter.string.IsEmpty() && !this->manually_changed_folding) {
2369 /* User never expanded/collapsed single pages and entered a filter term.
2370 * Expand everything, to save weird expand clicks, */
2371 GetSettingsTree().UnFoldAll();
2373 this->InvalidateData();
2377 virtual void OnResize()
2379 this->vscroll->SetCapacityFromWidget(this, WID_GS_OPTIONSPANEL, SETTINGTREE_TOP_OFFSET + SETTINGTREE_BOTTOM_OFFSET);
2383 GameSettings *GameSettingsWindow::settings_ptr = nullptr;
2385 static const NWidgetPart _nested_settings_selection_widgets[] = {
2386 NWidget(NWID_HORIZONTAL),
2387 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
2388 NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_CONFIG_SETTING_TREE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2389 NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
2390 EndContainer(),
2391 NWidget(WWT_PANEL, COLOUR_MAUVE),
2392 NWidget(NWID_VERTICAL), SetPIP(0, WD_PAR_VSEP_NORMAL, 0), SetPadding(WD_TEXTPANEL_TOP, 0, WD_TEXTPANEL_BOTTOM, 0),
2393 NWidget(NWID_HORIZONTAL), SetPIP(WD_FRAMETEXT_LEFT, WD_FRAMETEXT_RIGHT, WD_FRAMETEXT_RIGHT),
2394 NWidget(WWT_TEXT, COLOUR_MAUVE, WID_GS_RESTRICT_CATEGORY), SetDataTip(STR_CONFIG_SETTING_RESTRICT_CATEGORY, STR_NULL),
2395 NWidget(WWT_DROPDOWN, COLOUR_MAUVE, WID_GS_RESTRICT_DROPDOWN), SetMinimalSize(100, 12), SetDataTip(STR_BLACK_STRING, STR_CONFIG_SETTING_RESTRICT_DROPDOWN_HELPTEXT), SetFill(1, 0), SetResize(1, 0),
2396 EndContainer(),
2397 NWidget(NWID_HORIZONTAL), SetPIP(WD_FRAMETEXT_LEFT, WD_FRAMETEXT_RIGHT, WD_FRAMETEXT_RIGHT),
2398 NWidget(WWT_TEXT, COLOUR_MAUVE, WID_GS_RESTRICT_TYPE), SetDataTip(STR_CONFIG_SETTING_RESTRICT_TYPE, STR_NULL),
2399 NWidget(WWT_DROPDOWN, COLOUR_MAUVE, WID_GS_TYPE_DROPDOWN), SetMinimalSize(100, 12), SetDataTip(STR_BLACK_STRING, STR_CONFIG_SETTING_TYPE_DROPDOWN_HELPTEXT), SetFill(1, 0), SetResize(1, 0),
2400 EndContainer(),
2401 EndContainer(),
2402 NWidget(NWID_HORIZONTAL), SetPadding(0, 0, WD_TEXTPANEL_BOTTOM, 0),
2403 SetPIP(WD_FRAMETEXT_LEFT, WD_FRAMETEXT_RIGHT, WD_FRAMETEXT_RIGHT),
2404 NWidget(WWT_TEXT, COLOUR_MAUVE), SetFill(0, 1), SetDataTip(STR_CONFIG_SETTING_FILTER_TITLE, STR_NULL),
2405 NWidget(WWT_EDITBOX, COLOUR_MAUVE, WID_GS_FILTER), SetFill(1, 0), SetMinimalSize(50, 12), SetResize(1, 0),
2406 SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
2407 EndContainer(),
2408 EndContainer(),
2409 NWidget(NWID_HORIZONTAL),
2410 NWidget(WWT_PANEL, COLOUR_MAUVE, WID_GS_OPTIONSPANEL), SetMinimalSize(400, 174), SetScrollbar(WID_GS_SCROLLBAR), EndContainer(),
2411 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_GS_SCROLLBAR),
2412 EndContainer(),
2413 NWidget(WWT_PANEL, COLOUR_MAUVE), SetMinimalSize(400, 40),
2414 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_GS_HELP_TEXT), SetMinimalSize(300, 25), SetFill(1, 1), SetResize(1, 0),
2415 SetPadding(WD_FRAMETEXT_TOP, WD_FRAMETEXT_RIGHT, WD_FRAMETEXT_BOTTOM, WD_FRAMETEXT_LEFT),
2416 EndContainer(),
2417 NWidget(NWID_HORIZONTAL),
2418 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_GS_EXPAND_ALL), SetDataTip(STR_CONFIG_SETTING_EXPAND_ALL, STR_NULL),
2419 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_GS_COLLAPSE_ALL), SetDataTip(STR_CONFIG_SETTING_COLLAPSE_ALL, STR_NULL),
2420 NWidget(WWT_PANEL, COLOUR_MAUVE), SetFill(1, 0), SetResize(1, 0),
2421 EndContainer(),
2422 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
2423 EndContainer(),
2426 static WindowDesc _settings_selection_desc(
2427 WDP_CENTER, "settings", 510, 450,
2428 WC_GAME_OPTIONS, WC_NONE,
2430 _nested_settings_selection_widgets, lengthof(_nested_settings_selection_widgets)
2433 /** Open advanced settings window. */
2434 void ShowGameSettings()
2436 DeleteWindowByClass(WC_GAME_OPTIONS);
2437 new GameSettingsWindow(&_settings_selection_desc);
2442 * Draw [<][>] boxes.
2443 * @param x the x position to draw
2444 * @param y the y position to draw
2445 * @param button_colour the colour of the button
2446 * @param state 0 = none clicked, 1 = first clicked, 2 = second clicked
2447 * @param clickable_left is the left button clickable?
2448 * @param clickable_right is the right button clickable?
2450 void DrawArrowButtons(int x, int y, Colours button_colour, byte state, bool clickable_left, bool clickable_right)
2452 int colour = _colour_gradient[button_colour][2];
2453 Dimension dim = NWidgetScrollbar::GetHorizontalDimension();
2455 DrawFrameRect(x, y, x + dim.width - 1, y + dim.height - 1, button_colour, (state == 1) ? FR_LOWERED : FR_NONE);
2456 DrawFrameRect(x + dim.width, y, x + dim.width + dim.width - 1, y + dim.height - 1, button_colour, (state == 2) ? FR_LOWERED : FR_NONE);
2457 DrawSprite(SPR_ARROW_LEFT, PAL_NONE, x + WD_IMGBTN_LEFT, y + WD_IMGBTN_TOP);
2458 DrawSprite(SPR_ARROW_RIGHT, PAL_NONE, x + WD_IMGBTN_LEFT + dim.width, y + WD_IMGBTN_TOP);
2460 /* Grey out the buttons that aren't clickable */
2461 bool rtl = _current_text_dir == TD_RTL;
2462 if (rtl ? !clickable_right : !clickable_left) {
2463 GfxFillRect(x + 1, y, x + dim.width - 1, y + dim.height - 2, colour, FILLRECT_CHECKER);
2465 if (rtl ? !clickable_left : !clickable_right) {
2466 GfxFillRect(x + dim.width + 1, y, x + dim.width + dim.width - 1, y + dim.height - 2, colour, FILLRECT_CHECKER);
2471 * Draw a dropdown button.
2472 * @param x the x position to draw
2473 * @param y the y position to draw
2474 * @param button_colour the colour of the button
2475 * @param state true = lowered
2476 * @param clickable is the button clickable?
2478 void DrawDropDownButton(int x, int y, Colours button_colour, bool state, bool clickable)
2480 int colour = _colour_gradient[button_colour][2];
2482 DrawFrameRect(x, y, x + SETTING_BUTTON_WIDTH - 1, y + SETTING_BUTTON_HEIGHT - 1, button_colour, state ? FR_LOWERED : FR_NONE);
2483 DrawSprite(SPR_ARROW_DOWN, PAL_NONE, x + (SETTING_BUTTON_WIDTH - NWidgetScrollbar::GetVerticalDimension().width) / 2 + state, y + 2 + state);
2485 if (!clickable) {
2486 GfxFillRect(x + 1, y, x + SETTING_BUTTON_WIDTH - 1, y + SETTING_BUTTON_HEIGHT - 2, colour, FILLRECT_CHECKER);
2491 * Draw a toggle button.
2492 * @param x the x position to draw
2493 * @param y the y position to draw
2494 * @param state true = lowered
2495 * @param clickable is the button clickable?
2497 void DrawBoolButton(int x, int y, bool state, bool clickable)
2499 static const Colours _bool_ctabs[2][2] = {{COLOUR_CREAM, COLOUR_RED}, {COLOUR_DARK_GREEN, COLOUR_GREEN}};
2500 DrawFrameRect(x, y, x + SETTING_BUTTON_WIDTH - 1, y + SETTING_BUTTON_HEIGHT - 1, _bool_ctabs[state][clickable], state ? FR_LOWERED : FR_NONE);
2503 struct CustomCurrencyWindow : Window {
2504 int query_widget;
2506 CustomCurrencyWindow(WindowDesc *desc) : Window(desc)
2508 this->InitNested();
2510 SetButtonState();
2513 void SetButtonState()
2515 this->SetWidgetDisabledState(WID_CC_RATE_DOWN, _custom_currency.rate == 1);
2516 this->SetWidgetDisabledState(WID_CC_RATE_UP, _custom_currency.rate == UINT16_MAX);
2517 this->SetWidgetDisabledState(WID_CC_YEAR_DOWN, _custom_currency.to_euro == CF_NOEURO);
2518 this->SetWidgetDisabledState(WID_CC_YEAR_UP, _custom_currency.to_euro == MAX_YEAR);
2521 virtual void SetStringParameters(int widget) const
2523 switch (widget) {
2524 case WID_CC_RATE: SetDParam(0, 1); SetDParam(1, 1); break;
2525 case WID_CC_SEPARATOR: SetDParamStr(0, _custom_currency.separator); break;
2526 case WID_CC_PREFIX: SetDParamStr(0, _custom_currency.prefix); break;
2527 case WID_CC_SUFFIX: SetDParamStr(0, _custom_currency.suffix); break;
2528 case WID_CC_YEAR:
2529 SetDParam(0, (_custom_currency.to_euro != CF_NOEURO) ? STR_CURRENCY_SWITCH_TO_EURO : STR_CURRENCY_SWITCH_TO_EURO_NEVER);
2530 SetDParam(1, _custom_currency.to_euro);
2531 break;
2533 case WID_CC_PREVIEW:
2534 SetDParam(0, 10000);
2535 break;
2539 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
2541 switch (widget) {
2542 /* Set the appropriate width for the edit 'buttons' */
2543 case WID_CC_SEPARATOR_EDIT:
2544 case WID_CC_PREFIX_EDIT:
2545 case WID_CC_SUFFIX_EDIT:
2546 size->width = this->GetWidget<NWidgetBase>(WID_CC_RATE_DOWN)->smallest_x + this->GetWidget<NWidgetBase>(WID_CC_RATE_UP)->smallest_x;
2547 break;
2549 /* Make sure the window is wide enough for the widest exchange rate */
2550 case WID_CC_RATE:
2551 SetDParam(0, 1);
2552 SetDParam(1, INT32_MAX);
2553 *size = GetStringBoundingBox(STR_CURRENCY_EXCHANGE_RATE);
2554 break;
2558 virtual void OnClick(Point pt, int widget, int click_count)
2560 int line = 0;
2561 int len = 0;
2562 StringID str = 0;
2563 CharSetFilter afilter = CS_ALPHANUMERAL;
2565 switch (widget) {
2566 case WID_CC_RATE_DOWN:
2567 if (_custom_currency.rate > 1) _custom_currency.rate--;
2568 if (_custom_currency.rate == 1) this->DisableWidget(WID_CC_RATE_DOWN);
2569 this->EnableWidget(WID_CC_RATE_UP);
2570 break;
2572 case WID_CC_RATE_UP:
2573 if (_custom_currency.rate < UINT16_MAX) _custom_currency.rate++;
2574 if (_custom_currency.rate == UINT16_MAX) this->DisableWidget(WID_CC_RATE_UP);
2575 this->EnableWidget(WID_CC_RATE_DOWN);
2576 break;
2578 case WID_CC_RATE:
2579 SetDParam(0, _custom_currency.rate);
2580 str = STR_JUST_INT;
2581 len = 5;
2582 line = WID_CC_RATE;
2583 afilter = CS_NUMERAL;
2584 break;
2586 case WID_CC_SEPARATOR_EDIT:
2587 case WID_CC_SEPARATOR:
2588 SetDParamStr(0, _custom_currency.separator);
2589 str = STR_JUST_RAW_STRING;
2590 len = 1;
2591 line = WID_CC_SEPARATOR;
2592 break;
2594 case WID_CC_PREFIX_EDIT:
2595 case WID_CC_PREFIX:
2596 SetDParamStr(0, _custom_currency.prefix);
2597 str = STR_JUST_RAW_STRING;
2598 len = 12;
2599 line = WID_CC_PREFIX;
2600 break;
2602 case WID_CC_SUFFIX_EDIT:
2603 case WID_CC_SUFFIX:
2604 SetDParamStr(0, _custom_currency.suffix);
2605 str = STR_JUST_RAW_STRING;
2606 len = 12;
2607 line = WID_CC_SUFFIX;
2608 break;
2610 case WID_CC_YEAR_DOWN:
2611 _custom_currency.to_euro = (_custom_currency.to_euro <= 2000) ? CF_NOEURO : _custom_currency.to_euro - 1;
2612 if (_custom_currency.to_euro == CF_NOEURO) this->DisableWidget(WID_CC_YEAR_DOWN);
2613 this->EnableWidget(WID_CC_YEAR_UP);
2614 break;
2616 case WID_CC_YEAR_UP:
2617 _custom_currency.to_euro = Clamp(_custom_currency.to_euro + 1, 2000, MAX_YEAR);
2618 if (_custom_currency.to_euro == MAX_YEAR) this->DisableWidget(WID_CC_YEAR_UP);
2619 this->EnableWidget(WID_CC_YEAR_DOWN);
2620 break;
2622 case WID_CC_YEAR:
2623 SetDParam(0, _custom_currency.to_euro);
2624 str = STR_JUST_INT;
2625 len = 7;
2626 line = WID_CC_YEAR;
2627 afilter = CS_NUMERAL;
2628 break;
2631 if (len != 0) {
2632 this->query_widget = line;
2633 ShowQueryString(str, STR_CURRENCY_CHANGE_PARAMETER, len + 1, this, afilter, QSF_NONE);
2636 this->SetTimeout();
2637 this->SetDirty();
2640 virtual void OnQueryTextFinished(char *str)
2642 if (str == nullptr) return;
2644 switch (this->query_widget) {
2645 case WID_CC_RATE:
2646 _custom_currency.rate = Clamp(atoi(str), 1, UINT16_MAX);
2647 break;
2649 case WID_CC_SEPARATOR: // Thousands separator
2650 strecpy(_custom_currency.separator, str, lastof(_custom_currency.separator));
2651 break;
2653 case WID_CC_PREFIX:
2654 strecpy(_custom_currency.prefix, str, lastof(_custom_currency.prefix));
2655 break;
2657 case WID_CC_SUFFIX:
2658 strecpy(_custom_currency.suffix, str, lastof(_custom_currency.suffix));
2659 break;
2661 case WID_CC_YEAR: { // Year to switch to euro
2662 int val = atoi(str);
2664 _custom_currency.to_euro = (val < 2000 ? CF_NOEURO : min(val, MAX_YEAR));
2665 break;
2668 MarkWholeScreenDirty();
2669 SetButtonState();
2672 virtual void OnTimeout()
2674 this->SetDirty();
2678 static const NWidgetPart _nested_cust_currency_widgets[] = {
2679 NWidget(NWID_HORIZONTAL),
2680 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
2681 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_CURRENCY_WINDOW, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2682 EndContainer(),
2683 NWidget(WWT_PANEL, COLOUR_GREY),
2684 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(7, 3, 0),
2685 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
2686 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_CC_RATE_DOWN), SetDataTip(AWV_DECREASE, STR_CURRENCY_DECREASE_EXCHANGE_RATE_TOOLTIP),
2687 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_CC_RATE_UP), SetDataTip(AWV_INCREASE, STR_CURRENCY_INCREASE_EXCHANGE_RATE_TOOLTIP),
2688 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
2689 NWidget(WWT_TEXT, COLOUR_BLUE, WID_CC_RATE), SetDataTip(STR_CURRENCY_EXCHANGE_RATE, STR_CURRENCY_SET_EXCHANGE_RATE_TOOLTIP), SetFill(1, 0),
2690 EndContainer(),
2691 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
2692 NWidget(WWT_PUSHBTN, COLOUR_DARK_BLUE, WID_CC_SEPARATOR_EDIT), SetDataTip(0x0, STR_CURRENCY_SET_CUSTOM_CURRENCY_SEPARATOR_TOOLTIP), SetFill(0, 1),
2693 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
2694 NWidget(WWT_TEXT, COLOUR_BLUE, WID_CC_SEPARATOR), SetDataTip(STR_CURRENCY_SEPARATOR, STR_CURRENCY_SET_CUSTOM_CURRENCY_SEPARATOR_TOOLTIP), SetFill(1, 0),
2695 EndContainer(),
2696 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
2697 NWidget(WWT_PUSHBTN, COLOUR_DARK_BLUE, WID_CC_PREFIX_EDIT), SetDataTip(0x0, STR_CURRENCY_SET_CUSTOM_CURRENCY_PREFIX_TOOLTIP), SetFill(0, 1),
2698 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
2699 NWidget(WWT_TEXT, COLOUR_BLUE, WID_CC_PREFIX), SetDataTip(STR_CURRENCY_PREFIX, STR_CURRENCY_SET_CUSTOM_CURRENCY_PREFIX_TOOLTIP), SetFill(1, 0),
2700 EndContainer(),
2701 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
2702 NWidget(WWT_PUSHBTN, COLOUR_DARK_BLUE, WID_CC_SUFFIX_EDIT), SetDataTip(0x0, STR_CURRENCY_SET_CUSTOM_CURRENCY_SUFFIX_TOOLTIP), SetFill(0, 1),
2703 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
2704 NWidget(WWT_TEXT, COLOUR_BLUE, WID_CC_SUFFIX), SetDataTip(STR_CURRENCY_SUFFIX, STR_CURRENCY_SET_CUSTOM_CURRENCY_SUFFIX_TOOLTIP), SetFill(1, 0),
2705 EndContainer(),
2706 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
2707 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_CC_YEAR_DOWN), SetDataTip(AWV_DECREASE, STR_CURRENCY_DECREASE_CUSTOM_CURRENCY_TO_EURO_TOOLTIP),
2708 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_CC_YEAR_UP), SetDataTip(AWV_INCREASE, STR_CURRENCY_INCREASE_CUSTOM_CURRENCY_TO_EURO_TOOLTIP),
2709 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
2710 NWidget(WWT_TEXT, COLOUR_BLUE, WID_CC_YEAR), SetDataTip(STR_JUST_STRING, STR_CURRENCY_SET_CUSTOM_CURRENCY_TO_EURO_TOOLTIP), SetFill(1, 0),
2711 EndContainer(),
2712 EndContainer(),
2713 NWidget(WWT_LABEL, COLOUR_BLUE, WID_CC_PREVIEW),
2714 SetDataTip(STR_CURRENCY_PREVIEW, STR_CURRENCY_CUSTOM_CURRENCY_PREVIEW_TOOLTIP), SetPadding(15, 1, 18, 2),
2715 EndContainer(),
2718 static WindowDesc _cust_currency_desc(
2719 WDP_CENTER, nullptr, 0, 0,
2720 WC_CUSTOM_CURRENCY, WC_NONE,
2722 _nested_cust_currency_widgets, lengthof(_nested_cust_currency_widgets)
2725 /** Open custom currency window. */
2726 static void ShowCustCurrency()
2728 DeleteWindowById(WC_CUSTOM_CURRENCY, 0);
2729 new CustomCurrencyWindow(&_cust_currency_desc);