Fix #10023: Allow negative input in text fields when needed (#10112)
[openttd-github.git] / src / settings_gui.cpp
blobcc1c3515b8798eb6fa272a36d7a892a83e7fe3f0
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file settings_gui.cpp GUI for settings. */
10 #include "stdafx.h"
11 #include "currency.h"
12 #include "error.h"
13 #include "settings_gui.h"
14 #include "textbuf_gui.h"
15 #include "command_func.h"
16 #include "network/network.h"
17 #include "town.h"
18 #include "settings_internal.h"
19 #include "strings_func.h"
20 #include "window_func.h"
21 #include "string_func.h"
22 #include "widgets/dropdown_type.h"
23 #include "widgets/dropdown_func.h"
24 #include "widgets/slider_func.h"
25 #include "highscore.h"
26 #include "base_media_base.h"
27 #include "company_base.h"
28 #include "company_func.h"
29 #include "viewport_func.h"
30 #include "core/geometry_func.hpp"
31 #include "ai/ai.hpp"
32 #include "blitter/factory.hpp"
33 #include "language.h"
34 #include "textfile_gui.h"
35 #include "stringfilter_type.h"
36 #include "querystring_gui.h"
37 #include "fontcache.h"
38 #include "zoom_func.h"
39 #include "rev.h"
40 #include "video/video_driver.hpp"
41 #include "music/music_driver.hpp"
43 #include <vector>
44 #include <iterator>
46 #include "safeguards.h"
47 #include "video/video_driver.hpp"
50 static const StringID _autosave_dropdown[] = {
51 STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_OFF,
52 STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_1_MONTH,
53 STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_3_MONTHS,
54 STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_6_MONTHS,
55 STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_12_MONTHS,
56 INVALID_STRING_ID,
59 static const StringID _gui_zoom_dropdown[] = {
60 STR_GAME_OPTIONS_GUI_ZOOM_DROPDOWN_AUTO,
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 static const StringID _font_zoom_dropdown[] = {
68 STR_GAME_OPTIONS_FONT_ZOOM_DROPDOWN_AUTO,
69 STR_GAME_OPTIONS_FONT_ZOOM_DROPDOWN_NORMAL,
70 STR_GAME_OPTIONS_FONT_ZOOM_DROPDOWN_2X_ZOOM,
71 STR_GAME_OPTIONS_FONT_ZOOM_DROPDOWN_4X_ZOOM,
72 INVALID_STRING_ID,
75 static Dimension _circle_size; ///< Dimension of the circle +/- icon. This is here as not all users are within the class of the settings window.
77 static const void *ResolveObject(const GameSettings *settings_ptr, const IntSettingDesc *sd);
79 /**
80 * Get index of the current screen resolution.
81 * @return Index of the current screen resolution if it is a known resolution, _resolutions.size() otherwise.
83 static uint GetCurrentResolutionIndex()
85 auto it = std::find(_resolutions.begin(), _resolutions.end(), Dimension(_screen.width, _screen.height));
86 return std::distance(_resolutions.begin(), it);
89 static void ShowCustCurrency();
91 template <class T>
92 static DropDownList BuildSetDropDownList(int *selected_index, bool allow_selection)
94 int n = T::GetNumSets();
95 *selected_index = T::GetIndexOfUsedSet();
97 DropDownList list;
98 for (int i = 0; i < n; i++) {
99 list.emplace_back(new DropDownListCharStringItem(T::GetSet(i)->name, i, !allow_selection && (*selected_index != i)));
102 return list;
105 DropDownList BuildMusicSetDropDownList(int *selected_index)
107 return BuildSetDropDownList<BaseMusic>(selected_index, true);
110 /** Window for displaying the textfile of a BaseSet. */
111 template <class TBaseSet>
112 struct BaseSetTextfileWindow : public TextfileWindow {
113 const TBaseSet* baseset; ///< View the textfile of this BaseSet.
114 StringID content_type; ///< STR_CONTENT_TYPE_xxx for title.
116 BaseSetTextfileWindow(TextfileType file_type, const TBaseSet* baseset, StringID content_type) : TextfileWindow(file_type), baseset(baseset), content_type(content_type)
118 const char *textfile = this->baseset->GetTextfile(file_type);
119 this->LoadTextfile(textfile, BASESET_DIR);
122 void SetStringParameters(int widget) const override
124 if (widget == WID_TF_CAPTION) {
125 SetDParam(0, content_type);
126 SetDParamStr(1, this->baseset->name);
132 * Open the BaseSet version of the textfile window.
133 * @param file_type The type of textfile to display.
134 * @param baseset The BaseSet to use.
135 * @param content_type STR_CONTENT_TYPE_xxx for title.
137 template <class TBaseSet>
138 void ShowBaseSetTextfileWindow(TextfileType file_type, const TBaseSet* baseset, StringID content_type)
140 CloseWindowById(WC_TEXTFILE, file_type);
141 new BaseSetTextfileWindow<TBaseSet>(file_type, baseset, content_type);
144 std::set<int> _refresh_rates = { 30, 60, 75, 90, 100, 120, 144, 240 };
147 * Add the refresh rate from the config and the refresh rates from all the monitors to
148 * our list of refresh rates shown in the GUI.
150 static void AddCustomRefreshRates()
152 /* Add the refresh rate as selected in the config. */
153 _refresh_rates.insert(_settings_client.gui.refresh_rate);
155 /* Add all the refresh rates of all monitors connected to the machine. */
156 std::vector<int> monitorRates = VideoDriver::GetInstance()->GetListOfMonitorRefreshRates();
157 std::copy(monitorRates.begin(), monitorRates.end(), std::inserter(_refresh_rates, _refresh_rates.end()));
160 struct GameOptionsWindow : Window {
161 GameSettings *opt;
162 bool reload;
164 GameOptionsWindow(WindowDesc *desc) : Window(desc)
166 this->opt = &GetGameSettings();
167 this->reload = false;
169 AddCustomRefreshRates();
171 this->InitNested(WN_GAME_OPTIONS_GAME_OPTIONS);
172 this->OnInvalidateData(0);
175 void Close() override
177 CloseWindowById(WC_CUSTOM_CURRENCY, 0);
178 CloseWindowByClass(WC_TEXTFILE);
179 if (this->reload) _switch_mode = SM_MENU;
180 this->Window::Close();
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;
192 switch (widget) {
193 case WID_GO_CURRENCY_DROPDOWN: { // Setup currencies dropdown
194 *selected_index = this->opt->locale.currency;
195 StringID *items = BuildCurrencyDropdown();
196 uint64 disabled = _game_mode == GM_MENU ? 0LL : ~GetMaskOfAllowedCurrencies();
198 /* Add non-custom currencies; sorted naturally */
199 for (uint i = 0; i < CURRENCY_END; items++, i++) {
200 if (i == CURRENCY_CUSTOM) continue;
201 list.emplace_back(new DropDownListStringItem(*items, i, HasBit(disabled, i)));
203 std::sort(list.begin(), list.end(), DropDownListStringItem::NatSortFunc);
205 /* Append custom currency at the end */
206 list.emplace_back(new DropDownListItem(-1, false)); // separator line
207 list.emplace_back(new DropDownListStringItem(STR_GAME_OPTIONS_CURRENCY_CUSTOM, CURRENCY_CUSTOM, HasBit(disabled, CURRENCY_CUSTOM)));
208 break;
211 case WID_GO_AUTOSAVE_DROPDOWN: { // Setup autosave dropdown
212 *selected_index = _settings_client.gui.autosave;
213 const StringID *items = _autosave_dropdown;
214 for (uint i = 0; *items != INVALID_STRING_ID; items++, i++) {
215 list.emplace_back(new DropDownListStringItem(*items, i, false));
217 break;
220 case WID_GO_LANG_DROPDOWN: { // Setup interface language dropdown
221 for (uint i = 0; i < _languages.size(); i++) {
222 bool hide_language = IsReleasedVersion() && !_languages[i].IsReasonablyFinished();
223 if (hide_language) continue;
224 bool hide_percentage = IsReleasedVersion() || _languages[i].missing < _settings_client.gui.missing_strings_threshold;
225 auto item = new DropDownListParamStringItem(hide_percentage ? STR_JUST_RAW_STRING : STR_GAME_OPTIONS_LANGUAGE_PERCENTAGE, i, false);
226 if (&_languages[i] == _current_language) {
227 *selected_index = i;
228 item->SetParamStr(0, _languages[i].own_name);
229 } else {
230 /* Especially with sprite-fonts, not all localized
231 * names can be rendered. So instead, we use the
232 * international names for anything but the current
233 * selected language. This avoids showing a few ????
234 * entries in the dropdown list. */
235 item->SetParamStr(0, _languages[i].name);
237 item->SetParam(1, (LANGUAGE_TOTAL_STRINGS - _languages[i].missing) * 100 / LANGUAGE_TOTAL_STRINGS);
238 list.emplace_back(item);
240 std::sort(list.begin(), list.end(), DropDownListStringItem::NatSortFunc);
241 break;
244 case WID_GO_RESOLUTION_DROPDOWN: // Setup resolution dropdown
245 if (_resolutions.empty()) break;
247 *selected_index = GetCurrentResolutionIndex();
248 for (uint i = 0; i < _resolutions.size(); i++) {
249 auto item = new DropDownListParamStringItem(STR_GAME_OPTIONS_RESOLUTION_ITEM, i, false);
250 item->SetParam(0, _resolutions[i].width);
251 item->SetParam(1, _resolutions[i].height);
252 list.emplace_back(item);
254 break;
256 case WID_GO_REFRESH_RATE_DROPDOWN: // Setup refresh rate dropdown
257 for (auto it = _refresh_rates.begin(); it != _refresh_rates.end(); it++) {
258 auto i = std::distance(_refresh_rates.begin(), it);
259 if (*it == _settings_client.gui.refresh_rate) *selected_index = i;
260 auto item = new DropDownListParamStringItem(STR_GAME_OPTIONS_REFRESH_RATE_ITEM, i, false);
261 item->SetParam(0, *it);
262 list.emplace_back(item);
264 break;
266 case WID_GO_GUI_ZOOM_DROPDOWN: {
267 *selected_index = _gui_zoom_cfg != ZOOM_LVL_CFG_AUTO ? ZOOM_LVL_OUT_4X - _gui_zoom + 1 : 0;
268 const StringID *items = _gui_zoom_dropdown;
269 for (int i = 0; *items != INVALID_STRING_ID; items++, i++) {
270 list.emplace_back(new DropDownListStringItem(*items, i, i != 0 && _settings_client.gui.zoom_min > ZOOM_LVL_OUT_4X - i + 1));
272 break;
275 case WID_GO_FONT_ZOOM_DROPDOWN: {
276 *selected_index = _font_zoom_cfg != ZOOM_LVL_CFG_AUTO ? ZOOM_LVL_OUT_4X - _font_zoom + 1 : 0;
277 const StringID *items = _font_zoom_dropdown;
278 for (int i = 0; *items != INVALID_STRING_ID; items++, i++) {
279 list.emplace_back(new DropDownListStringItem(*items, i, false));
281 break;
284 case WID_GO_BASE_GRF_DROPDOWN:
285 list = BuildSetDropDownList<BaseGraphics>(selected_index, (_game_mode == GM_MENU));
286 break;
288 case WID_GO_BASE_SFX_DROPDOWN:
289 list = BuildSetDropDownList<BaseSounds>(selected_index, (_game_mode == GM_MENU));
290 break;
292 case WID_GO_BASE_MUSIC_DROPDOWN:
293 list = BuildMusicSetDropDownList(selected_index);
294 break;
297 return list;
300 void SetStringParameters(int widget) const override
302 switch (widget) {
303 case WID_GO_CURRENCY_DROPDOWN: SetDParam(0, _currency_specs[this->opt->locale.currency].name); break;
304 case WID_GO_AUTOSAVE_DROPDOWN: SetDParam(0, _autosave_dropdown[_settings_client.gui.autosave]); break;
305 case WID_GO_LANG_DROPDOWN: SetDParamStr(0, _current_language->own_name); break;
306 case WID_GO_GUI_ZOOM_DROPDOWN: SetDParam(0, _gui_zoom_dropdown[_gui_zoom_cfg != ZOOM_LVL_CFG_AUTO ? ZOOM_LVL_OUT_4X - _gui_zoom_cfg + 1 : 0]); break;
307 case WID_GO_FONT_ZOOM_DROPDOWN: SetDParam(0, _font_zoom_dropdown[_font_zoom_cfg != ZOOM_LVL_CFG_AUTO ? ZOOM_LVL_OUT_4X - _font_zoom_cfg + 1 : 0]); break;
308 case WID_GO_BASE_GRF_DROPDOWN: SetDParamStr(0, BaseGraphics::GetUsedSet()->name); break;
309 case WID_GO_BASE_GRF_STATUS: SetDParam(0, BaseGraphics::GetUsedSet()->GetNumInvalid()); break;
310 case WID_GO_BASE_SFX_DROPDOWN: SetDParamStr(0, BaseSounds::GetUsedSet()->name); break;
311 case WID_GO_BASE_MUSIC_DROPDOWN: SetDParamStr(0, BaseMusic::GetUsedSet()->name); break;
312 case WID_GO_BASE_MUSIC_STATUS: SetDParam(0, BaseMusic::GetUsedSet()->GetNumInvalid()); break;
313 case WID_GO_VIDEO_DRIVER_INFO: SetDParamStr(0, VideoDriver::GetInstance()->GetInfoString()); break;
314 case WID_GO_REFRESH_RATE_DROPDOWN: SetDParam(0, _settings_client.gui.refresh_rate); break;
315 case WID_GO_RESOLUTION_DROPDOWN: {
316 auto current_resolution = GetCurrentResolutionIndex();
318 if (current_resolution == _resolutions.size()) {
319 SetDParam(0, STR_GAME_OPTIONS_RESOLUTION_OTHER);
320 } else {
321 SetDParam(0, STR_GAME_OPTIONS_RESOLUTION_ITEM);
322 SetDParam(1, _resolutions[current_resolution].width);
323 SetDParam(2, _resolutions[current_resolution].height);
325 break;
330 void DrawWidget(const Rect &r, int widget) const override
332 switch (widget) {
333 case WID_GO_BASE_GRF_DESCRIPTION:
334 SetDParamStr(0, BaseGraphics::GetUsedSet()->GetDescription(GetCurrentLanguageIsoCode()));
335 DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, STR_BLACK_RAW_STRING);
336 break;
338 case WID_GO_BASE_SFX_DESCRIPTION:
339 SetDParamStr(0, BaseSounds::GetUsedSet()->GetDescription(GetCurrentLanguageIsoCode()));
340 DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, STR_BLACK_RAW_STRING);
341 break;
343 case WID_GO_BASE_MUSIC_DESCRIPTION:
344 SetDParamStr(0, BaseMusic::GetUsedSet()->GetDescription(GetCurrentLanguageIsoCode()));
345 DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, STR_BLACK_RAW_STRING);
346 break;
348 case WID_GO_BASE_SFX_VOLUME:
349 DrawVolumeSliderWidget(r, _settings_client.music.effect_vol);
350 break;
352 case WID_GO_BASE_MUSIC_VOLUME:
353 DrawVolumeSliderWidget(r, _settings_client.music.music_vol);
354 break;
358 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
360 switch (widget) {
361 case WID_GO_BASE_GRF_DESCRIPTION:
362 /* Find the biggest description for the default size. */
363 for (int i = 0; i < BaseGraphics::GetNumSets(); i++) {
364 SetDParamStr(0, BaseGraphics::GetSet(i)->GetDescription(GetCurrentLanguageIsoCode()));
365 size->height = std::max(size->height, (uint)GetStringHeight(STR_BLACK_RAW_STRING, size->width));
367 break;
369 case WID_GO_BASE_GRF_STATUS:
370 /* Find the biggest description for the default size. */
371 for (int i = 0; i < BaseGraphics::GetNumSets(); i++) {
372 uint invalid_files = BaseGraphics::GetSet(i)->GetNumInvalid();
373 if (invalid_files == 0) continue;
375 SetDParam(0, invalid_files);
376 *size = maxdim(*size, GetStringBoundingBox(STR_GAME_OPTIONS_BASE_GRF_STATUS));
378 break;
380 case WID_GO_BASE_SFX_DESCRIPTION:
381 /* Find the biggest description for the default size. */
382 for (int i = 0; i < BaseSounds::GetNumSets(); i++) {
383 SetDParamStr(0, BaseSounds::GetSet(i)->GetDescription(GetCurrentLanguageIsoCode()));
384 size->height = std::max(size->height, (uint)GetStringHeight(STR_BLACK_RAW_STRING, size->width));
386 break;
388 case WID_GO_BASE_MUSIC_DESCRIPTION:
389 /* Find the biggest description for the default size. */
390 for (int i = 0; i < BaseMusic::GetNumSets(); i++) {
391 SetDParamStr(0, BaseMusic::GetSet(i)->GetDescription(GetCurrentLanguageIsoCode()));
392 size->height = std::max(size->height, (uint)GetStringHeight(STR_BLACK_RAW_STRING, size->width));
394 break;
396 case WID_GO_BASE_MUSIC_STATUS:
397 /* Find the biggest description for the default size. */
398 for (int i = 0; i < BaseMusic::GetNumSets(); i++) {
399 uint invalid_files = BaseMusic::GetSet(i)->GetNumInvalid();
400 if (invalid_files == 0) continue;
402 SetDParam(0, invalid_files);
403 *size = maxdim(*size, GetStringBoundingBox(STR_GAME_OPTIONS_BASE_MUSIC_STATUS));
405 break;
407 default: {
408 int selected;
409 DropDownList list = this->BuildDropDownList(widget, &selected);
410 if (!list.empty()) {
411 /* Find the biggest item for the default size. */
412 for (const auto &ddli : list) {
413 Dimension string_dim;
414 int width = ddli->Width();
415 string_dim.width = width + padding.width;
416 string_dim.height = ddli->Height(width) + padding.height;
417 *size = maxdim(*size, string_dim);
424 void OnClick(Point pt, int widget, int click_count) override
426 if (widget >= WID_GO_BASE_GRF_TEXTFILE && widget < WID_GO_BASE_GRF_TEXTFILE + TFT_END) {
427 if (BaseGraphics::GetUsedSet() == nullptr) return;
429 ShowBaseSetTextfileWindow((TextfileType)(widget - WID_GO_BASE_GRF_TEXTFILE), BaseGraphics::GetUsedSet(), STR_CONTENT_TYPE_BASE_GRAPHICS);
430 return;
432 if (widget >= WID_GO_BASE_SFX_TEXTFILE && widget < WID_GO_BASE_SFX_TEXTFILE + TFT_END) {
433 if (BaseSounds::GetUsedSet() == nullptr) return;
435 ShowBaseSetTextfileWindow((TextfileType)(widget - WID_GO_BASE_SFX_TEXTFILE), BaseSounds::GetUsedSet(), STR_CONTENT_TYPE_BASE_SOUNDS);
436 return;
438 if (widget >= WID_GO_BASE_MUSIC_TEXTFILE && widget < WID_GO_BASE_MUSIC_TEXTFILE + TFT_END) {
439 if (BaseMusic::GetUsedSet() == nullptr) return;
441 ShowBaseSetTextfileWindow((TextfileType)(widget - WID_GO_BASE_MUSIC_TEXTFILE), BaseMusic::GetUsedSet(), STR_CONTENT_TYPE_BASE_MUSIC);
442 return;
444 switch (widget) {
445 case WID_GO_FULLSCREEN_BUTTON: // Click fullscreen on/off
446 /* try to toggle full-screen on/off */
447 if (!ToggleFullScreen(!_fullscreen)) {
448 ShowErrorMessage(STR_ERROR_FULLSCREEN_FAILED, INVALID_STRING_ID, WL_ERROR);
450 this->SetWidgetLoweredState(WID_GO_FULLSCREEN_BUTTON, _fullscreen);
451 this->SetWidgetDirty(WID_GO_FULLSCREEN_BUTTON);
452 break;
454 case WID_GO_VIDEO_ACCEL_BUTTON:
455 _video_hw_accel = !_video_hw_accel;
456 ShowErrorMessage(STR_GAME_OPTIONS_VIDEO_ACCELERATION_RESTART, INVALID_STRING_ID, WL_INFO);
457 this->SetWidgetLoweredState(WID_GO_VIDEO_ACCEL_BUTTON, _video_hw_accel);
458 this->SetWidgetDirty(WID_GO_VIDEO_ACCEL_BUTTON);
459 #ifndef __APPLE__
460 this->SetWidgetDisabledState(WID_GO_VIDEO_VSYNC_BUTTON, !_video_hw_accel);
461 this->SetWidgetDirty(WID_GO_VIDEO_VSYNC_BUTTON);
462 #endif
463 break;
465 case WID_GO_VIDEO_VSYNC_BUTTON:
466 if (!_video_hw_accel) break;
468 _video_vsync = !_video_vsync;
469 VideoDriver::GetInstance()->ToggleVsync(_video_vsync);
471 this->SetWidgetLoweredState(WID_GO_VIDEO_VSYNC_BUTTON, _video_vsync);
472 this->SetWidgetDirty(WID_GO_VIDEO_VSYNC_BUTTON);
473 this->SetWidgetDisabledState(WID_GO_REFRESH_RATE_DROPDOWN, _video_vsync);
474 this->SetWidgetDirty(WID_GO_REFRESH_RATE_DROPDOWN);
475 break;
477 case WID_GO_BASE_SFX_VOLUME:
478 case WID_GO_BASE_MUSIC_VOLUME: {
479 byte &vol = (widget == WID_GO_BASE_MUSIC_VOLUME) ? _settings_client.music.music_vol : _settings_client.music.effect_vol;
480 if (ClickVolumeSliderWidget(this->GetWidget<NWidgetBase>(widget)->GetCurrentRect(), pt, vol)) {
481 if (widget == WID_GO_BASE_MUSIC_VOLUME) MusicDriver::GetInstance()->SetVolume(vol);
482 this->SetWidgetDirty(widget);
483 SetWindowClassesDirty(WC_MUSIC_WINDOW);
486 if (click_count > 0) this->mouse_capture_widget = widget;
487 break;
490 default: {
491 int selected;
492 DropDownList list = this->BuildDropDownList(widget, &selected);
493 if (!list.empty()) {
494 ShowDropDownList(this, std::move(list), selected, widget);
495 } else {
496 if (widget == WID_GO_RESOLUTION_DROPDOWN) ShowErrorMessage(STR_ERROR_RESOLUTION_LIST_FAILED, INVALID_STRING_ID, WL_ERROR);
498 break;
504 * Set the base media set.
505 * @param index the index of the media set
506 * @tparam T class of media set
508 template <class T>
509 void SetMediaSet(int index)
511 if (_game_mode == GM_MENU) {
512 auto name = T::GetSet(index)->name;
514 T::ini_set = name;
516 T::SetSet(name);
517 this->reload = true;
518 this->InvalidateData();
522 void OnDropdownSelect(int widget, int index) override
524 switch (widget) {
525 case WID_GO_CURRENCY_DROPDOWN: // Currency
526 if (index == CURRENCY_CUSTOM) ShowCustCurrency();
527 this->opt->locale.currency = index;
528 ReInitAllWindows(false);
529 break;
531 case WID_GO_AUTOSAVE_DROPDOWN: // Autosave options
532 _settings_client.gui.autosave = index;
533 this->SetDirty();
534 break;
536 case WID_GO_LANG_DROPDOWN: // Change interface language
537 ReadLanguagePack(&_languages[index]);
538 CloseWindowByClass(WC_QUERY_STRING);
539 CheckForMissingGlyphs();
540 ClearAllCachedNames();
541 UpdateAllVirtCoords();
542 CheckBlitter();
543 ReInitAllWindows(false);
544 break;
546 case WID_GO_RESOLUTION_DROPDOWN: // Change resolution
547 if ((uint)index < _resolutions.size() && ChangeResInGame(_resolutions[index].width, _resolutions[index].height)) {
548 this->SetDirty();
550 break;
552 case WID_GO_REFRESH_RATE_DROPDOWN: {
553 _settings_client.gui.refresh_rate = *std::next(_refresh_rates.begin(), index);
554 if (_settings_client.gui.refresh_rate > 60) {
555 /* Show warning to the user that this refresh rate might not be suitable on
556 * larger maps with many NewGRFs and vehicles. */
557 ShowErrorMessage(STR_GAME_OPTIONS_REFRESH_RATE_WARNING, INVALID_STRING_ID, WL_INFO);
559 break;
562 case WID_GO_GUI_ZOOM_DROPDOWN: {
563 int8 new_zoom = index > 0 ? ZOOM_LVL_OUT_4X - index + 1 : ZOOM_LVL_CFG_AUTO;
564 if (new_zoom != _gui_zoom_cfg) {
565 GfxClearSpriteCache();
566 _gui_zoom_cfg = new_zoom;
567 UpdateGUIZoom();
568 UpdateCursorSize();
569 UpdateAllVirtCoords();
570 FixTitleGameZoom();
571 ReInitAllWindows(true);
573 break;
576 case WID_GO_FONT_ZOOM_DROPDOWN: {
577 int8 new_zoom = index > 0 ? ZOOM_LVL_OUT_4X - index + 1 : ZOOM_LVL_CFG_AUTO;
578 if (new_zoom != _font_zoom_cfg) {
579 GfxClearSpriteCache();
580 _font_zoom_cfg = new_zoom;
581 UpdateGUIZoom();
582 ClearFontCache();
583 LoadStringWidthTable();
584 UpdateAllVirtCoords();
585 ReInitAllWindows(true);
587 break;
590 case WID_GO_BASE_GRF_DROPDOWN:
591 this->SetMediaSet<BaseGraphics>(index);
592 break;
594 case WID_GO_BASE_SFX_DROPDOWN:
595 this->SetMediaSet<BaseSounds>(index);
596 break;
598 case WID_GO_BASE_MUSIC_DROPDOWN:
599 ChangeMusicSet(index);
600 break;
605 * Some data on this window has become invalid.
606 * @param data Information about the changed data. @see GameOptionsInvalidationData
607 * @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.
609 void OnInvalidateData(int data = 0, bool gui_scope = true) override
611 if (!gui_scope) return;
612 this->SetWidgetLoweredState(WID_GO_FULLSCREEN_BUTTON, _fullscreen);
613 this->SetWidgetLoweredState(WID_GO_VIDEO_ACCEL_BUTTON, _video_hw_accel);
614 this->SetWidgetDisabledState(WID_GO_REFRESH_RATE_DROPDOWN, _video_vsync);
616 #ifndef __APPLE__
617 this->SetWidgetLoweredState(WID_GO_VIDEO_VSYNC_BUTTON, _video_vsync);
618 this->SetWidgetDisabledState(WID_GO_VIDEO_VSYNC_BUTTON, !_video_hw_accel);
619 #endif
621 bool missing_files = BaseGraphics::GetUsedSet()->GetNumMissing() == 0;
622 this->GetWidget<NWidgetCore>(WID_GO_BASE_GRF_STATUS)->SetDataTip(missing_files ? STR_EMPTY : STR_GAME_OPTIONS_BASE_GRF_STATUS, STR_NULL);
624 for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
625 this->SetWidgetDisabledState(WID_GO_BASE_GRF_TEXTFILE + tft, BaseGraphics::GetUsedSet() == nullptr || BaseGraphics::GetUsedSet()->GetTextfile(tft) == nullptr);
626 this->SetWidgetDisabledState(WID_GO_BASE_SFX_TEXTFILE + tft, BaseSounds::GetUsedSet() == nullptr || BaseSounds::GetUsedSet()->GetTextfile(tft) == nullptr);
627 this->SetWidgetDisabledState(WID_GO_BASE_MUSIC_TEXTFILE + tft, BaseMusic::GetUsedSet() == nullptr || BaseMusic::GetUsedSet()->GetTextfile(tft) == nullptr);
630 missing_files = BaseMusic::GetUsedSet()->GetNumInvalid() == 0;
631 this->GetWidget<NWidgetCore>(WID_GO_BASE_MUSIC_STATUS)->SetDataTip(missing_files ? STR_EMPTY : STR_GAME_OPTIONS_BASE_MUSIC_STATUS, STR_NULL);
635 static const NWidgetPart _nested_game_options_widgets[] = {
636 NWidget(NWID_HORIZONTAL),
637 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
638 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
639 EndContainer(),
640 NWidget(WWT_PANEL, COLOUR_GREY, WID_GO_BACKGROUND), SetPIP(6, 6, 10),
641 NWidget(NWID_HORIZONTAL), SetPIP(10, 10, 10),
642 NWidget(NWID_VERTICAL), SetPIP(0, 6, 0),
643 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_AUTOSAVE_FRAME, STR_NULL),
644 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),
645 EndContainer(),
646 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_GUI_ZOOM_FRAME, STR_NULL),
647 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),
648 EndContainer(),
649 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_CURRENCY_UNITS_FRAME, STR_NULL),
650 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),
651 EndContainer(),
652 EndContainer(),
654 NWidget(NWID_VERTICAL), SetPIP(0, 6, 0),
655 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_LANGUAGE, STR_NULL),
656 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),
657 EndContainer(),
658 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_FONT_ZOOM, STR_NULL),
659 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_FONT_ZOOM_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_FONT_ZOOM_DROPDOWN_TOOLTIP), SetFill(1, 0),
660 EndContainer(),
661 EndContainer(),
662 EndContainer(),
664 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_GRAPHICS, STR_NULL), SetPadding(0, 10, 0, 10),
665 NWidget(NWID_HORIZONTAL),
666 NWidget(NWID_VERTICAL), SetPIP(0, 2, 0),
667 NWidget(NWID_HORIZONTAL),
668 NWidget(WWT_TEXT, COLOUR_GREY), SetMinimalSize(0, 12),SetDataTip(STR_GAME_OPTIONS_RESOLUTION, STR_NULL),
669 NWidget(NWID_SPACER), SetMinimalSize(1, 0), SetFill(1, 0),
670 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_RESOLUTION_DROPDOWN), SetMinimalSize(200, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_RESOLUTION_TOOLTIP),
671 EndContainer(),
672 NWidget(NWID_HORIZONTAL),
673 NWidget(WWT_TEXT, COLOUR_GREY), SetMinimalSize(0, 12), SetDataTip(STR_GAME_OPTIONS_REFRESH_RATE, STR_NULL),
674 NWidget(NWID_SPACER), SetMinimalSize(1, 0), SetFill(1, 0),
675 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_REFRESH_RATE_DROPDOWN), SetMinimalSize(200, 12), SetDataTip(STR_GAME_OPTIONS_REFRESH_RATE_ITEM, STR_GAME_OPTIONS_REFRESH_RATE_TOOLTIP),
676 EndContainer(),
677 NWidget(NWID_HORIZONTAL),
678 NWidget(WWT_TEXT, COLOUR_GREY), SetMinimalSize(0, 12), SetDataTip(STR_GAME_OPTIONS_FULLSCREEN, STR_NULL),
679 NWidget(NWID_SPACER), SetMinimalSize(1, 0), SetFill(1, 0),
680 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_GO_FULLSCREEN_BUTTON), SetMinimalSize(21, 9), SetDataTip(STR_EMPTY, STR_GAME_OPTIONS_FULLSCREEN_TOOLTIP),
681 EndContainer(),
682 NWidget(NWID_HORIZONTAL),
683 NWidget(WWT_TEXT, COLOUR_GREY), SetMinimalSize(0, 12), SetDataTip(STR_GAME_OPTIONS_VIDEO_ACCELERATION, STR_NULL),
684 NWidget(NWID_SPACER), SetMinimalSize(1, 0), SetFill(1, 0),
685 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_GO_VIDEO_ACCEL_BUTTON), SetMinimalSize(21, 9), SetDataTip(STR_EMPTY, STR_GAME_OPTIONS_VIDEO_ACCELERATION_TOOLTIP),
686 EndContainer(),
687 #ifndef __APPLE__
688 NWidget(NWID_HORIZONTAL),
689 NWidget(WWT_TEXT, COLOUR_GREY), SetMinimalSize(0, 12), SetDataTip(STR_GAME_OPTIONS_VIDEO_VSYNC, STR_NULL),
690 NWidget(NWID_SPACER), SetMinimalSize(1, 0), SetFill(1, 0),
691 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_GO_VIDEO_VSYNC_BUTTON), SetMinimalSize(21, 9), SetDataTip(STR_EMPTY, STR_GAME_OPTIONS_VIDEO_VSYNC_TOOLTIP),
692 EndContainer(),
693 #endif
694 NWidget(NWID_HORIZONTAL),
695 NWidget(WWT_TEXT, COLOUR_GREY, WID_GO_VIDEO_DRIVER_INFO), SetMinimalSize(0, 12), SetFill(1, 0), SetDataTip(STR_GAME_OPTIONS_VIDEO_DRIVER_INFO, STR_NULL),
696 EndContainer(),
697 EndContainer(),
698 EndContainer(),
699 EndContainer(),
701 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_BASE_GRF, STR_NULL), SetPadding(0, 10, 0, 10),
702 NWidget(NWID_HORIZONTAL), SetPIP(0, 30, 0),
703 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_BASE_GRF_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_RAW_STRING, STR_GAME_OPTIONS_BASE_GRF_TOOLTIP),
704 NWidget(WWT_TEXT, COLOUR_GREY, WID_GO_BASE_GRF_STATUS), SetMinimalSize(150, 12), SetDataTip(STR_EMPTY, STR_NULL), SetFill(1, 0),
705 EndContainer(),
706 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),
707 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
708 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),
709 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),
710 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),
711 EndContainer(),
712 EndContainer(),
714 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_BASE_SFX, STR_NULL), SetPadding(0, 10, 0, 10),
715 NWidget(NWID_HORIZONTAL), SetPIP(0, 30, 7),
716 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_BASE_SFX_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_RAW_STRING, STR_GAME_OPTIONS_BASE_SFX_TOOLTIP),
717 NWidget(NWID_SPACER), SetMinimalSize(150, 12), SetFill(1, 0),
718 NWidget(WWT_EMPTY, COLOUR_GREY, WID_GO_BASE_SFX_VOLUME), SetMinimalSize(67, 12), SetFill(0, 0), SetDataTip(0x0, STR_MUSIC_TOOLTIP_DRAG_SLIDERS_TO_SET_MUSIC),
719 EndContainer(),
720 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),
721 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
722 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),
723 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),
724 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),
725 EndContainer(),
726 EndContainer(),
728 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_BASE_MUSIC, STR_NULL), SetPadding(0, 10, 0, 10),
729 NWidget(NWID_HORIZONTAL), SetPIP(0, 30, 7),
730 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_BASE_MUSIC_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_RAW_STRING, STR_GAME_OPTIONS_BASE_MUSIC_TOOLTIP),
731 NWidget(WWT_TEXT, COLOUR_GREY, WID_GO_BASE_MUSIC_STATUS), SetMinimalSize(150, 12), SetDataTip(STR_EMPTY, STR_NULL), SetFill(1, 0),
732 NWidget(WWT_EMPTY, COLOUR_GREY, WID_GO_BASE_MUSIC_VOLUME), SetMinimalSize(67, 12), SetFill(0, 0), SetDataTip(0x0, STR_MUSIC_TOOLTIP_DRAG_SLIDERS_TO_SET_MUSIC),
733 EndContainer(),
734 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),
735 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
736 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),
737 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),
738 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),
739 EndContainer(),
740 EndContainer(),
741 EndContainer(),
744 static WindowDesc _game_options_desc(
745 WDP_CENTER, "settings_game", 0, 0,
746 WC_GAME_OPTIONS, WC_NONE,
748 _nested_game_options_widgets, lengthof(_nested_game_options_widgets)
751 /** Open the game options window. */
752 void ShowGameOptions()
754 CloseWindowByClass(WC_GAME_OPTIONS);
755 new GameOptionsWindow(&_game_options_desc);
758 static int SETTING_HEIGHT = 11; ///< Height of a single setting in the tree view in pixels
759 static const int LEVEL_WIDTH = 15; ///< Indenting width of a sub-page in pixels
762 * Flags for #SettingEntry
763 * @note The #SEF_BUTTONS_MASK matches expectations of the formal parameter 'state' of #DrawArrowButtons
765 enum SettingEntryFlags {
766 SEF_LEFT_DEPRESSED = 0x01, ///< Of a numeric setting entry, the left button is depressed
767 SEF_RIGHT_DEPRESSED = 0x02, ///< Of a numeric setting entry, the right button is depressed
768 SEF_BUTTONS_MASK = (SEF_LEFT_DEPRESSED | SEF_RIGHT_DEPRESSED), ///< Bit-mask for button flags
770 SEF_LAST_FIELD = 0x04, ///< This entry is the last one in a (sub-)page
771 SEF_FILTERED = 0x08, ///< Entry is hidden by the string filter
774 /** How the list of advanced settings is filtered. */
775 enum RestrictionMode {
776 RM_BASIC, ///< Display settings associated to the "basic" list.
777 RM_ADVANCED, ///< Display settings associated to the "advanced" list.
778 RM_ALL, ///< List all settings regardless of the default/newgame/... values.
779 RM_CHANGED_AGAINST_DEFAULT, ///< Show only settings which are different compared to default values.
780 RM_CHANGED_AGAINST_NEW, ///< Show only settings which are different compared to the user's new game setting values.
781 RM_END, ///< End for iteration.
783 DECLARE_POSTFIX_INCREMENT(RestrictionMode)
785 /** Filter for settings list. */
786 struct SettingFilter {
787 StringFilter string; ///< Filter string.
788 RestrictionMode min_cat; ///< Minimum category needed to display all filtered strings (#RM_BASIC, #RM_ADVANCED, or #RM_ALL).
789 bool type_hides; ///< Whether the type hides filtered strings.
790 RestrictionMode mode; ///< Filter based on category.
791 SettingType type; ///< Filter based on type.
794 /** Data structure describing a single setting in a tab */
795 struct BaseSettingEntry {
796 byte flags; ///< Flags of the setting entry. @see SettingEntryFlags
797 byte level; ///< Nesting level of this setting entry
799 BaseSettingEntry() : flags(0), level(0) {}
800 virtual ~BaseSettingEntry() {}
802 virtual void Init(byte level = 0);
803 virtual void FoldAll() {}
804 virtual void UnFoldAll() {}
805 virtual void ResetAll() = 0;
808 * Set whether this is the last visible entry of the parent node.
809 * @param last_field Value to set
811 void SetLastField(bool last_field) { if (last_field) SETBITS(this->flags, SEF_LAST_FIELD); else CLRBITS(this->flags, SEF_LAST_FIELD); }
813 virtual uint Length() const = 0;
814 virtual void GetFoldingState(bool &all_folded, bool &all_unfolded) const {}
815 virtual bool IsVisible(const BaseSettingEntry *item) const;
816 virtual BaseSettingEntry *FindEntry(uint row, uint *cur_row);
817 virtual uint GetMaxHelpHeight(int maxw) { return 0; }
820 * Check whether an entry is hidden due to filters
821 * @return true if hidden.
823 bool IsFiltered() const { return (this->flags & SEF_FILTERED) != 0; }
825 virtual bool UpdateFilterState(SettingFilter &filter, bool force_visible) = 0;
827 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;
829 protected:
830 virtual void DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const = 0;
833 /** Standard setting */
834 struct SettingEntry : BaseSettingEntry {
835 const char *name; ///< Name of the setting
836 const IntSettingDesc *setting; ///< Setting description of the setting
838 SettingEntry(const char *name);
840 virtual void Init(byte level = 0);
841 virtual void ResetAll();
842 virtual uint Length() const;
843 virtual uint GetMaxHelpHeight(int maxw);
844 virtual bool UpdateFilterState(SettingFilter &filter, bool force_visible);
846 void SetButtons(byte new_val);
849 * Get the help text of a single setting.
850 * @return The requested help text.
852 inline StringID GetHelpText() const
854 return this->setting->str_help;
857 void SetValueDParams(uint first_param, int32 value) const;
859 protected:
860 virtual void DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const;
862 private:
863 bool IsVisibleByRestrictionMode(RestrictionMode mode) const;
866 /** Containers for BaseSettingEntry */
867 struct SettingsContainer {
868 typedef std::vector<BaseSettingEntry*> EntryVector;
869 EntryVector entries; ///< Settings on this page
871 template<typename T>
872 T *Add(T *item)
874 this->entries.push_back(item);
875 return item;
878 void Init(byte level = 0);
879 void ResetAll();
880 void FoldAll();
881 void UnFoldAll();
883 uint Length() const;
884 void GetFoldingState(bool &all_folded, bool &all_unfolded) const;
885 bool IsVisible(const BaseSettingEntry *item) const;
886 BaseSettingEntry *FindEntry(uint row, uint *cur_row);
887 uint GetMaxHelpHeight(int maxw);
889 bool UpdateFilterState(SettingFilter &filter, bool force_visible);
891 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;
894 /** Data structure describing one page of settings in the settings window. */
895 struct SettingsPage : BaseSettingEntry, SettingsContainer {
896 StringID title; ///< Title of the sub-page
897 bool folded; ///< Sub-page is folded (not visible except for its title)
899 SettingsPage(StringID title);
901 virtual void Init(byte level = 0);
902 virtual void ResetAll();
903 virtual void FoldAll();
904 virtual void UnFoldAll();
906 virtual uint Length() const;
907 virtual void GetFoldingState(bool &all_folded, bool &all_unfolded) const;
908 virtual bool IsVisible(const BaseSettingEntry *item) const;
909 virtual BaseSettingEntry *FindEntry(uint row, uint *cur_row);
910 virtual uint GetMaxHelpHeight(int maxw) { return SettingsContainer::GetMaxHelpHeight(maxw); }
912 virtual bool UpdateFilterState(SettingFilter &filter, bool force_visible);
914 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;
916 protected:
917 virtual void DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const;
920 /* == BaseSettingEntry methods == */
923 * Initialization of a setting entry
924 * @param level Page nesting level of this entry
926 void BaseSettingEntry::Init(byte level)
928 this->level = level;
932 * Check whether an entry is visible and not folded or filtered away.
933 * Note: This does not consider the scrolling range; it might still require scrolling to make the setting really visible.
934 * @param item Entry to search for.
935 * @return true if entry is visible.
937 bool BaseSettingEntry::IsVisible(const BaseSettingEntry *item) const
939 if (this->IsFiltered()) return false;
940 return this == item;
944 * Find setting entry at row \a row_num
945 * @param row_num Index of entry to return
946 * @param cur_row Current row number
947 * @return The requested setting entry or \c nullptr if it not found (folded or filtered)
949 BaseSettingEntry *BaseSettingEntry::FindEntry(uint row_num, uint *cur_row)
951 if (this->IsFiltered()) return nullptr;
952 if (row_num == *cur_row) return this;
953 (*cur_row)++;
954 return nullptr;
958 * Draw a row in the settings panel.
960 * The scrollbar uses rows of the page, while the page data structure is a tree of #SettingsPage and #SettingEntry objects.
961 * 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.
962 * Then it enables drawing rows while traversing until \a max_row is reached, at which point drawing is terminated.
964 * The \a parent_last parameter ensures that the vertical lines at the left are
965 * only drawn when another entry follows, that it prevents output like
966 * \verbatim
967 * |-- setting
968 * |-- (-) - Title
969 * | |-- setting
970 * | |-- setting
971 * \endverbatim
972 * The left-most vertical line is not wanted. It is prevented by setting the
973 * appropriate bit in the \a parent_last parameter.
975 * @param settings_ptr Pointer to current values of all settings
976 * @param left Left-most position in window/panel to start drawing \a first_row
977 * @param right Right-most x position to draw strings at.
978 * @param y Upper-most position in window/panel to start drawing \a first_row
979 * @param first_row First row number to draw
980 * @param max_row Row-number to stop drawing (the row-number of the row below the last row to draw)
981 * @param selected Selected entry by the user.
982 * @param cur_row Current row number (internal variable)
983 * @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)
984 * @return Row number of the next row to draw
986 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
988 if (this->IsFiltered()) return cur_row;
989 if (cur_row >= max_row) return cur_row;
991 bool rtl = _current_text_dir == TD_RTL;
992 int offset = rtl ? -4 : 4;
993 int level_width = rtl ? -LEVEL_WIDTH : LEVEL_WIDTH;
995 int x = rtl ? right : left;
996 if (cur_row >= first_row) {
997 int colour = _colour_gradient[COLOUR_ORANGE][4];
998 y += (cur_row - first_row) * SETTING_HEIGHT; // Compute correct y start position
1000 /* Draw vertical for parent nesting levels */
1001 for (uint lvl = 0; lvl < this->level; lvl++) {
1002 if (!HasBit(parent_last, lvl)) GfxDrawLine(x + offset, y, x + offset, y + SETTING_HEIGHT - 1, colour);
1003 x += level_width;
1005 /* draw own |- prefix */
1006 int halfway_y = y + SETTING_HEIGHT / 2;
1007 int bottom_y = (flags & SEF_LAST_FIELD) ? halfway_y : y + SETTING_HEIGHT - 1;
1008 GfxDrawLine(x + offset, y, x + offset, bottom_y, colour);
1009 /* Small horizontal line from the last vertical line */
1010 GfxDrawLine(x + offset, halfway_y, x + level_width - offset, halfway_y, colour);
1011 x += level_width;
1013 this->DrawSetting(settings_ptr, rtl ? left : x, rtl ? x : right, y, this == selected);
1015 cur_row++;
1017 return cur_row;
1020 /* == SettingEntry methods == */
1023 * Constructor for a single setting in the 'advanced settings' window
1024 * @param name Name of the setting in the setting table
1026 SettingEntry::SettingEntry(const char *name)
1028 this->name = name;
1029 this->setting = nullptr;
1033 * Initialization of a setting entry
1034 * @param level Page nesting level of this entry
1036 void SettingEntry::Init(byte level)
1038 BaseSettingEntry::Init(level);
1039 this->setting = GetSettingFromName(this->name)->AsIntSetting();
1042 /* Sets the given setting entry to its default value */
1043 void SettingEntry::ResetAll()
1045 SetSettingValue(this->setting, this->setting->def);
1049 * Set the button-depressed flags (#SEF_LEFT_DEPRESSED and #SEF_RIGHT_DEPRESSED) to a specified value
1050 * @param new_val New value for the button flags
1051 * @see SettingEntryFlags
1053 void SettingEntry::SetButtons(byte new_val)
1055 assert((new_val & ~SEF_BUTTONS_MASK) == 0); // Should not touch any flags outside the buttons
1056 this->flags = (this->flags & ~SEF_BUTTONS_MASK) | new_val;
1059 /** Return number of rows needed to display the (filtered) entry */
1060 uint SettingEntry::Length() const
1062 return this->IsFiltered() ? 0 : 1;
1066 * Get the biggest height of the help text(s), if the width is at least \a maxw. Help text gets wrapped if needed.
1067 * @param maxw Maximal width of a line help text.
1068 * @return Biggest height needed to display any help text of this node (and its descendants).
1070 uint SettingEntry::GetMaxHelpHeight(int maxw)
1072 return GetStringHeight(this->GetHelpText(), maxw);
1076 * Checks whether an entry shall be made visible based on the restriction mode.
1077 * @param mode The current status of the restriction drop down box.
1078 * @return true if the entry shall be visible.
1080 bool SettingEntry::IsVisibleByRestrictionMode(RestrictionMode mode) const
1082 /* There shall not be any restriction, i.e. all settings shall be visible. */
1083 if (mode == RM_ALL) return true;
1085 const IntSettingDesc *sd = this->setting;
1087 if (mode == RM_BASIC) return (this->setting->cat & SC_BASIC_LIST) != 0;
1088 if (mode == RM_ADVANCED) return (this->setting->cat & SC_ADVANCED_LIST) != 0;
1090 /* Read the current value. */
1091 const void *object = ResolveObject(&GetGameSettings(), sd);
1092 int64 current_value = sd->Read(object);
1093 int64 filter_value;
1095 if (mode == RM_CHANGED_AGAINST_DEFAULT) {
1096 /* This entry shall only be visible, if the value deviates from its default value. */
1098 /* Read the default value. */
1099 filter_value = sd->def;
1100 } else {
1101 assert(mode == RM_CHANGED_AGAINST_NEW);
1102 /* This entry shall only be visible, if the value deviates from
1103 * its value is used when starting a new game. */
1105 /* Make sure we're not comparing the new game settings against itself. */
1106 assert(&GetGameSettings() != &_settings_newgame);
1108 /* Read the new game's value. */
1109 filter_value = sd->Read(ResolveObject(&_settings_newgame, sd));
1112 return current_value != filter_value;
1116 * Update the filter state.
1117 * @param filter Filter
1118 * @param force_visible Whether to force all items visible, no matter what (due to filter text; not affected by restriction drop down box).
1119 * @return true if item remains visible
1121 bool SettingEntry::UpdateFilterState(SettingFilter &filter, bool force_visible)
1123 CLRBITS(this->flags, SEF_FILTERED);
1125 bool visible = true;
1127 const IntSettingDesc *sd = this->setting;
1128 if (!force_visible && !filter.string.IsEmpty()) {
1129 /* Process the search text filter for this item. */
1130 filter.string.ResetState();
1132 SetDParam(0, STR_EMPTY);
1133 filter.string.AddLine(sd->str);
1134 filter.string.AddLine(this->GetHelpText());
1136 visible = filter.string.GetState();
1139 if (visible) {
1140 if (filter.type != ST_ALL && sd->GetType() != filter.type) {
1141 filter.type_hides = true;
1142 visible = false;
1144 if (!this->IsVisibleByRestrictionMode(filter.mode)) {
1145 while (filter.min_cat < RM_ALL && (filter.min_cat == filter.mode || !this->IsVisibleByRestrictionMode(filter.min_cat))) filter.min_cat++;
1146 visible = false;
1150 if (!visible) SETBITS(this->flags, SEF_FILTERED);
1151 return visible;
1154 static const void *ResolveObject(const GameSettings *settings_ptr, const IntSettingDesc *sd)
1156 if ((sd->flags & SF_PER_COMPANY) != 0) {
1157 if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) {
1158 return &Company::Get(_local_company)->settings;
1160 return &_settings_client.company;
1162 return settings_ptr;
1166 * Set the DParams for drawing the value of a setting.
1167 * @param first_param First DParam to use
1168 * @param value Setting value to set params for.
1170 void SettingEntry::SetValueDParams(uint first_param, int32 value) const
1172 if (this->setting->IsBoolSetting()) {
1173 SetDParam(first_param++, value != 0 ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
1174 } else {
1175 if ((this->setting->flags & SF_GUI_DROPDOWN) != 0) {
1176 SetDParam(first_param++, this->setting->str_val - this->setting->min + value);
1177 } else if ((this->setting->flags & SF_GUI_NEGATIVE_IS_SPECIAL) != 0) {
1178 SetDParam(first_param++, this->setting->str_val + ((value >= 0) ? 1 : 0));
1179 value = abs(value);
1180 } else {
1181 SetDParam(first_param++, this->setting->str_val + ((value == 0 && (this->setting->flags & SF_GUI_0_IS_SPECIAL) != 0) ? 1 : 0));
1183 SetDParam(first_param++, value);
1188 * Function to draw setting value (button + text + current value)
1189 * @param settings_ptr Pointer to current values of all settings
1190 * @param left Left-most position in window/panel to start drawing
1191 * @param right Right-most position in window/panel to draw
1192 * @param y Upper-most position in window/panel to start drawing
1193 * @param highlight Highlight entry.
1195 void SettingEntry::DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const
1197 const IntSettingDesc *sd = this->setting;
1198 int state = this->flags & SEF_BUTTONS_MASK;
1200 bool rtl = _current_text_dir == TD_RTL;
1201 uint buttons_left = rtl ? right + 1 - SETTING_BUTTON_WIDTH : left;
1202 uint text_left = left + (rtl ? 0 : SETTING_BUTTON_WIDTH + 5);
1203 uint text_right = right - (rtl ? SETTING_BUTTON_WIDTH + 5 : 0);
1204 uint button_y = y + (SETTING_HEIGHT - SETTING_BUTTON_HEIGHT) / 2;
1206 /* We do not allow changes of some items when we are a client in a networkgame */
1207 bool editable = sd->IsEditable();
1209 SetDParam(0, highlight ? STR_ORANGE_STRING1_WHITE : STR_ORANGE_STRING1_LTBLUE);
1210 int32 value = sd->Read(ResolveObject(settings_ptr, sd));
1211 if (sd->IsBoolSetting()) {
1212 /* Draw checkbox for boolean-value either on/off */
1213 DrawBoolButton(buttons_left, button_y, value != 0, editable);
1214 } else if ((sd->flags & SF_GUI_DROPDOWN) != 0) {
1215 /* Draw [v] button for settings of an enum-type */
1216 DrawDropDownButton(buttons_left, button_y, COLOUR_YELLOW, state != 0, editable);
1217 } else {
1218 /* Draw [<][>] boxes for settings of an integer-type */
1219 DrawArrowButtons(buttons_left, button_y, COLOUR_YELLOW, state,
1220 editable && value != (sd->flags & SF_GUI_0_IS_SPECIAL ? 0 : sd->min), editable && (uint32)value != sd->max);
1222 this->SetValueDParams(1, value);
1223 DrawString(text_left, text_right, y + (SETTING_HEIGHT - FONT_HEIGHT_NORMAL) / 2, sd->str, highlight ? TC_WHITE : TC_LIGHT_BLUE);
1226 /* == SettingsContainer methods == */
1229 * Initialization of an entire setting page
1230 * @param level Nesting level of this page (internal variable, do not provide a value for it when calling)
1232 void SettingsContainer::Init(byte level)
1234 for (EntryVector::iterator it = this->entries.begin(); it != this->entries.end(); ++it) {
1235 (*it)->Init(level);
1239 /** Resets all settings to their default values */
1240 void SettingsContainer::ResetAll()
1242 for (auto settings_entry : this->entries) {
1243 settings_entry->ResetAll();
1247 /** Recursively close all folds of sub-pages */
1248 void SettingsContainer::FoldAll()
1250 for (EntryVector::iterator it = this->entries.begin(); it != this->entries.end(); ++it) {
1251 (*it)->FoldAll();
1255 /** Recursively open all folds of sub-pages */
1256 void SettingsContainer::UnFoldAll()
1258 for (EntryVector::iterator it = this->entries.begin(); it != this->entries.end(); ++it) {
1259 (*it)->UnFoldAll();
1264 * Recursively accumulate the folding state of the tree.
1265 * @param[in,out] all_folded Set to false, if one entry is not folded.
1266 * @param[in,out] all_unfolded Set to false, if one entry is folded.
1268 void SettingsContainer::GetFoldingState(bool &all_folded, bool &all_unfolded) const
1270 for (EntryVector::const_iterator it = this->entries.begin(); it != this->entries.end(); ++it) {
1271 (*it)->GetFoldingState(all_folded, all_unfolded);
1276 * Update the filter state.
1277 * @param filter Filter
1278 * @param force_visible Whether to force all items visible, no matter what
1279 * @return true if item remains visible
1281 bool SettingsContainer::UpdateFilterState(SettingFilter &filter, bool force_visible)
1283 bool visible = false;
1284 bool first_visible = true;
1285 for (EntryVector::reverse_iterator it = this->entries.rbegin(); it != this->entries.rend(); ++it) {
1286 visible |= (*it)->UpdateFilterState(filter, force_visible);
1287 (*it)->SetLastField(first_visible);
1288 if (visible && first_visible) first_visible = false;
1290 return visible;
1295 * Check whether an entry is visible and not folded or filtered away.
1296 * Note: This does not consider the scrolling range; it might still require scrolling to make the setting really visible.
1297 * @param item Entry to search for.
1298 * @return true if entry is visible.
1300 bool SettingsContainer::IsVisible(const BaseSettingEntry *item) const
1302 for (EntryVector::const_iterator it = this->entries.begin(); it != this->entries.end(); ++it) {
1303 if ((*it)->IsVisible(item)) return true;
1305 return false;
1308 /** Return number of rows needed to display the whole page */
1309 uint SettingsContainer::Length() const
1311 uint length = 0;
1312 for (EntryVector::const_iterator it = this->entries.begin(); it != this->entries.end(); ++it) {
1313 length += (*it)->Length();
1315 return length;
1319 * Find the setting entry at row number \a row_num
1320 * @param row_num Index of entry to return
1321 * @param cur_row Variable used for keeping track of the current row number. Should point to memory initialized to \c 0 when first called.
1322 * @return The requested setting entry or \c nullptr if it does not exist
1324 BaseSettingEntry *SettingsContainer::FindEntry(uint row_num, uint *cur_row)
1326 BaseSettingEntry *pe = nullptr;
1327 for (EntryVector::iterator it = this->entries.begin(); it != this->entries.end(); ++it) {
1328 pe = (*it)->FindEntry(row_num, cur_row);
1329 if (pe != nullptr) {
1330 break;
1333 return pe;
1337 * Get the biggest height of the help texts, if the width is at least \a maxw. Help text gets wrapped if needed.
1338 * @param maxw Maximal width of a line help text.
1339 * @return Biggest height needed to display any help text of this (sub-)tree.
1341 uint SettingsContainer::GetMaxHelpHeight(int maxw)
1343 uint biggest = 0;
1344 for (EntryVector::const_iterator it = this->entries.begin(); it != this->entries.end(); ++it) {
1345 biggest = std::max(biggest, (*it)->GetMaxHelpHeight(maxw));
1347 return biggest;
1352 * Draw a row in the settings panel.
1354 * @param settings_ptr Pointer to current values of all settings
1355 * @param left Left-most position in window/panel to start drawing \a first_row
1356 * @param right Right-most x position to draw strings at.
1357 * @param y Upper-most position in window/panel to start drawing \a first_row
1358 * @param first_row First row number to draw
1359 * @param max_row Row-number to stop drawing (the row-number of the row below the last row to draw)
1360 * @param selected Selected entry by the user.
1361 * @param cur_row Current row number (internal variable)
1362 * @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)
1363 * @return Row number of the next row to draw
1365 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
1367 for (EntryVector::const_iterator it = this->entries.begin(); it != this->entries.end(); ++it) {
1368 cur_row = (*it)->Draw(settings_ptr, left, right, y, first_row, max_row, selected, cur_row, parent_last);
1369 if (cur_row >= max_row) {
1370 break;
1373 return cur_row;
1376 /* == SettingsPage methods == */
1379 * Constructor for a sub-page in the 'advanced settings' window
1380 * @param title Title of the sub-page
1382 SettingsPage::SettingsPage(StringID title)
1384 this->title = title;
1385 this->folded = true;
1389 * Initialization of an entire setting page
1390 * @param level Nesting level of this page (internal variable, do not provide a value for it when calling)
1392 void SettingsPage::Init(byte level)
1394 BaseSettingEntry::Init(level);
1395 SettingsContainer::Init(level + 1);
1398 /** Resets all settings to their default values */
1399 void SettingsPage::ResetAll()
1401 for (auto settings_entry : this->entries) {
1402 settings_entry->ResetAll();
1406 /** Recursively close all (filtered) folds of sub-pages */
1407 void SettingsPage::FoldAll()
1409 if (this->IsFiltered()) return;
1410 this->folded = true;
1412 SettingsContainer::FoldAll();
1415 /** Recursively open all (filtered) folds of sub-pages */
1416 void SettingsPage::UnFoldAll()
1418 if (this->IsFiltered()) return;
1419 this->folded = false;
1421 SettingsContainer::UnFoldAll();
1425 * Recursively accumulate the folding state of the (filtered) tree.
1426 * @param[in,out] all_folded Set to false, if one entry is not folded.
1427 * @param[in,out] all_unfolded Set to false, if one entry is folded.
1429 void SettingsPage::GetFoldingState(bool &all_folded, bool &all_unfolded) const
1431 if (this->IsFiltered()) return;
1433 if (this->folded) {
1434 all_unfolded = false;
1435 } else {
1436 all_folded = false;
1439 SettingsContainer::GetFoldingState(all_folded, all_unfolded);
1443 * Update the filter state.
1444 * @param filter Filter
1445 * @param force_visible Whether to force all items visible, no matter what (due to filter text; not affected by restriction drop down box).
1446 * @return true if item remains visible
1448 bool SettingsPage::UpdateFilterState(SettingFilter &filter, bool force_visible)
1450 if (!force_visible && !filter.string.IsEmpty()) {
1451 filter.string.ResetState();
1452 filter.string.AddLine(this->title);
1453 force_visible = filter.string.GetState();
1456 bool visible = SettingsContainer::UpdateFilterState(filter, force_visible);
1457 if (visible) {
1458 CLRBITS(this->flags, SEF_FILTERED);
1459 } else {
1460 SETBITS(this->flags, SEF_FILTERED);
1462 return visible;
1466 * Check whether an entry is visible and not folded or filtered away.
1467 * Note: This does not consider the scrolling range; it might still require scrolling to make the setting really visible.
1468 * @param item Entry to search for.
1469 * @return true if entry is visible.
1471 bool SettingsPage::IsVisible(const BaseSettingEntry *item) const
1473 if (this->IsFiltered()) return false;
1474 if (this == item) return true;
1475 if (this->folded) return false;
1477 return SettingsContainer::IsVisible(item);
1480 /** Return number of rows needed to display the (filtered) entry */
1481 uint SettingsPage::Length() const
1483 if (this->IsFiltered()) return 0;
1484 if (this->folded) return 1; // Only displaying the title
1486 return 1 + SettingsContainer::Length();
1490 * Find setting entry at row \a row_num
1491 * @param row_num Index of entry to return
1492 * @param cur_row Current row number
1493 * @return The requested setting entry or \c nullptr if it not found (folded or filtered)
1495 BaseSettingEntry *SettingsPage::FindEntry(uint row_num, uint *cur_row)
1497 if (this->IsFiltered()) return nullptr;
1498 if (row_num == *cur_row) return this;
1499 (*cur_row)++;
1500 if (this->folded) return nullptr;
1502 return SettingsContainer::FindEntry(row_num, cur_row);
1506 * Draw a row in the settings panel.
1508 * @param settings_ptr Pointer to current values of all settings
1509 * @param left Left-most position in window/panel to start drawing \a first_row
1510 * @param right Right-most x position to draw strings at.
1511 * @param y Upper-most position in window/panel to start drawing \a first_row
1512 * @param first_row First row number to draw
1513 * @param max_row Row-number to stop drawing (the row-number of the row below the last row to draw)
1514 * @param selected Selected entry by the user.
1515 * @param cur_row Current row number (internal variable)
1516 * @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)
1517 * @return Row number of the next row to draw
1519 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
1521 if (this->IsFiltered()) return cur_row;
1522 if (cur_row >= max_row) return cur_row;
1524 cur_row = BaseSettingEntry::Draw(settings_ptr, left, right, y, first_row, max_row, selected, cur_row, parent_last);
1526 if (!this->folded) {
1527 if (this->flags & SEF_LAST_FIELD) {
1528 assert(this->level < 8 * sizeof(parent_last));
1529 SetBit(parent_last, this->level); // Add own last-field state
1532 cur_row = SettingsContainer::Draw(settings_ptr, left, right, y, first_row, max_row, selected, cur_row, parent_last);
1535 return cur_row;
1539 * Function to draw setting value (button + text + current value)
1540 * @param settings_ptr Pointer to current values of all settings
1541 * @param left Left-most position in window/panel to start drawing
1542 * @param right Right-most position in window/panel to draw
1543 * @param y Upper-most position in window/panel to start drawing
1544 * @param highlight Highlight entry.
1546 void SettingsPage::DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const
1548 bool rtl = _current_text_dir == TD_RTL;
1549 DrawSprite((this->folded ? SPR_CIRCLE_FOLDED : SPR_CIRCLE_UNFOLDED), PAL_NONE, rtl ? right - _circle_size.width : left, y + (SETTING_HEIGHT - _circle_size.height) / 2);
1550 DrawString(rtl ? left : left + _circle_size.width + 2, rtl ? right - _circle_size.width - 2 : right, y + (SETTING_HEIGHT - FONT_HEIGHT_NORMAL) / 2, this->title);
1553 /** Construct settings tree */
1554 static SettingsContainer &GetSettingsTree()
1556 static SettingsContainer *main = nullptr;
1558 if (main == nullptr)
1560 /* Build up the dynamic settings-array only once per OpenTTD session */
1561 main = new SettingsContainer();
1563 SettingsPage *localisation = main->Add(new SettingsPage(STR_CONFIG_SETTING_LOCALISATION));
1565 localisation->Add(new SettingEntry("locale.units_velocity"));
1566 localisation->Add(new SettingEntry("locale.units_power"));
1567 localisation->Add(new SettingEntry("locale.units_weight"));
1568 localisation->Add(new SettingEntry("locale.units_volume"));
1569 localisation->Add(new SettingEntry("locale.units_force"));
1570 localisation->Add(new SettingEntry("locale.units_height"));
1571 localisation->Add(new SettingEntry("gui.date_format_in_default_names"));
1574 SettingsPage *graphics = main->Add(new SettingsPage(STR_CONFIG_SETTING_GRAPHICS));
1576 graphics->Add(new SettingEntry("gui.zoom_min"));
1577 graphics->Add(new SettingEntry("gui.zoom_max"));
1578 graphics->Add(new SettingEntry("gui.sprite_zoom_min"));
1579 graphics->Add(new SettingEntry("gui.smallmap_land_colour"));
1580 graphics->Add(new SettingEntry("gui.linkgraph_colours"));
1581 graphics->Add(new SettingEntry("gui.graph_line_thickness"));
1584 SettingsPage *sound = main->Add(new SettingsPage(STR_CONFIG_SETTING_SOUND));
1586 sound->Add(new SettingEntry("sound.click_beep"));
1587 sound->Add(new SettingEntry("sound.confirm"));
1588 sound->Add(new SettingEntry("sound.news_ticker"));
1589 sound->Add(new SettingEntry("sound.news_full"));
1590 sound->Add(new SettingEntry("sound.new_year"));
1591 sound->Add(new SettingEntry("sound.disaster"));
1592 sound->Add(new SettingEntry("sound.vehicle"));
1593 sound->Add(new SettingEntry("sound.ambient"));
1596 SettingsPage *interface = main->Add(new SettingsPage(STR_CONFIG_SETTING_INTERFACE));
1598 SettingsPage *general = interface->Add(new SettingsPage(STR_CONFIG_SETTING_INTERFACE_GENERAL));
1600 general->Add(new SettingEntry("gui.osk_activation"));
1601 general->Add(new SettingEntry("gui.hover_delay_ms"));
1602 general->Add(new SettingEntry("gui.errmsg_duration"));
1603 general->Add(new SettingEntry("gui.window_snap_radius"));
1604 general->Add(new SettingEntry("gui.window_soft_limit"));
1605 general->Add(new SettingEntry("gui.right_mouse_wnd_close"));
1608 SettingsPage *viewports = interface->Add(new SettingsPage(STR_CONFIG_SETTING_INTERFACE_VIEWPORTS));
1610 viewports->Add(new SettingEntry("gui.auto_scrolling"));
1611 viewports->Add(new SettingEntry("gui.scroll_mode"));
1612 viewports->Add(new SettingEntry("gui.smooth_scroll"));
1613 /* While the horizontal scrollwheel scrolling is written as general code, only
1614 * the cocoa (OSX) driver generates input for it.
1615 * Since it's also able to completely disable the scrollwheel will we display it on all platforms anyway */
1616 viewports->Add(new SettingEntry("gui.scrollwheel_scrolling"));
1617 viewports->Add(new SettingEntry("gui.scrollwheel_multiplier"));
1618 #ifdef __APPLE__
1619 /* We might need to emulate a right mouse button on mac */
1620 viewports->Add(new SettingEntry("gui.right_mouse_btn_emulation"));
1621 #endif
1622 viewports->Add(new SettingEntry("gui.population_in_label"));
1623 viewports->Add(new SettingEntry("gui.liveries"));
1624 viewports->Add(new SettingEntry("construction.train_signal_side"));
1625 viewports->Add(new SettingEntry("gui.measure_tooltip"));
1626 viewports->Add(new SettingEntry("gui.loading_indicators"));
1627 viewports->Add(new SettingEntry("gui.show_track_reservation"));
1630 SettingsPage *construction = interface->Add(new SettingsPage(STR_CONFIG_SETTING_INTERFACE_CONSTRUCTION));
1632 construction->Add(new SettingEntry("gui.link_terraform_toolbar"));
1633 construction->Add(new SettingEntry("gui.persistent_buildingtools"));
1634 construction->Add(new SettingEntry("gui.quick_goto"));
1635 construction->Add(new SettingEntry("gui.default_rail_type"));
1638 interface->Add(new SettingEntry("gui.fast_forward_speed_limit"));
1639 interface->Add(new SettingEntry("gui.autosave"));
1640 interface->Add(new SettingEntry("gui.toolbar_pos"));
1641 interface->Add(new SettingEntry("gui.statusbar_pos"));
1642 interface->Add(new SettingEntry("gui.prefer_teamchat"));
1643 interface->Add(new SettingEntry("gui.advanced_vehicle_list"));
1644 interface->Add(new SettingEntry("gui.timetable_in_ticks"));
1645 interface->Add(new SettingEntry("gui.timetable_arrival_departure"));
1646 interface->Add(new SettingEntry("gui.show_newgrf_name"));
1649 SettingsPage *advisors = main->Add(new SettingsPage(STR_CONFIG_SETTING_ADVISORS));
1651 advisors->Add(new SettingEntry("gui.coloured_news_year"));
1652 advisors->Add(new SettingEntry("news_display.general"));
1653 advisors->Add(new SettingEntry("news_display.new_vehicles"));
1654 advisors->Add(new SettingEntry("news_display.accident"));
1655 advisors->Add(new SettingEntry("news_display.accident_other"));
1656 advisors->Add(new SettingEntry("news_display.company_info"));
1657 advisors->Add(new SettingEntry("news_display.acceptance"));
1658 advisors->Add(new SettingEntry("news_display.arrival_player"));
1659 advisors->Add(new SettingEntry("news_display.arrival_other"));
1660 advisors->Add(new SettingEntry("news_display.advice"));
1661 advisors->Add(new SettingEntry("gui.order_review_system"));
1662 advisors->Add(new SettingEntry("gui.vehicle_income_warn"));
1663 advisors->Add(new SettingEntry("gui.lost_vehicle_warn"));
1664 advisors->Add(new SettingEntry("gui.show_finances"));
1665 advisors->Add(new SettingEntry("news_display.economy"));
1666 advisors->Add(new SettingEntry("news_display.subsidies"));
1667 advisors->Add(new SettingEntry("news_display.open"));
1668 advisors->Add(new SettingEntry("news_display.close"));
1669 advisors->Add(new SettingEntry("news_display.production_player"));
1670 advisors->Add(new SettingEntry("news_display.production_other"));
1671 advisors->Add(new SettingEntry("news_display.production_nobody"));
1674 SettingsPage *company = main->Add(new SettingsPage(STR_CONFIG_SETTING_COMPANY));
1676 company->Add(new SettingEntry("gui.semaphore_build_before"));
1677 company->Add(new SettingEntry("gui.cycle_signal_types"));
1678 company->Add(new SettingEntry("gui.signal_gui_mode"));
1679 company->Add(new SettingEntry("gui.drag_signals_fixed_distance"));
1680 company->Add(new SettingEntry("gui.auto_remove_signals"));
1681 company->Add(new SettingEntry("gui.new_nonstop"));
1682 company->Add(new SettingEntry("gui.stop_location"));
1683 company->Add(new SettingEntry("gui.starting_colour"));
1684 company->Add(new SettingEntry("company.engine_renew"));
1685 company->Add(new SettingEntry("company.engine_renew_months"));
1686 company->Add(new SettingEntry("company.engine_renew_money"));
1687 company->Add(new SettingEntry("vehicle.servint_ispercent"));
1688 company->Add(new SettingEntry("vehicle.servint_trains"));
1689 company->Add(new SettingEntry("vehicle.servint_roadveh"));
1690 company->Add(new SettingEntry("vehicle.servint_ships"));
1691 company->Add(new SettingEntry("vehicle.servint_aircraft"));
1694 SettingsPage *accounting = main->Add(new SettingsPage(STR_CONFIG_SETTING_ACCOUNTING));
1696 accounting->Add(new SettingEntry("economy.inflation"));
1697 accounting->Add(new SettingEntry("difficulty.initial_interest"));
1698 accounting->Add(new SettingEntry("difficulty.max_loan"));
1699 accounting->Add(new SettingEntry("difficulty.subsidy_multiplier"));
1700 accounting->Add(new SettingEntry("difficulty.subsidy_duration"));
1701 accounting->Add(new SettingEntry("economy.feeder_payment_share"));
1702 accounting->Add(new SettingEntry("economy.infrastructure_maintenance"));
1703 accounting->Add(new SettingEntry("difficulty.vehicle_costs"));
1704 accounting->Add(new SettingEntry("difficulty.construction_cost"));
1707 SettingsPage *vehicles = main->Add(new SettingsPage(STR_CONFIG_SETTING_VEHICLES));
1709 SettingsPage *physics = vehicles->Add(new SettingsPage(STR_CONFIG_SETTING_VEHICLES_PHYSICS));
1711 physics->Add(new SettingEntry("vehicle.train_acceleration_model"));
1712 physics->Add(new SettingEntry("vehicle.train_slope_steepness"));
1713 physics->Add(new SettingEntry("vehicle.wagon_speed_limits"));
1714 physics->Add(new SettingEntry("vehicle.freight_trains"));
1715 physics->Add(new SettingEntry("vehicle.roadveh_acceleration_model"));
1716 physics->Add(new SettingEntry("vehicle.roadveh_slope_steepness"));
1717 physics->Add(new SettingEntry("vehicle.smoke_amount"));
1718 physics->Add(new SettingEntry("vehicle.plane_speed"));
1721 SettingsPage *routing = vehicles->Add(new SettingsPage(STR_CONFIG_SETTING_VEHICLES_ROUTING));
1723 routing->Add(new SettingEntry("pf.pathfinder_for_trains"));
1724 routing->Add(new SettingEntry("difficulty.line_reverse_mode"));
1725 routing->Add(new SettingEntry("pf.reverse_at_signals"));
1726 routing->Add(new SettingEntry("pf.forbid_90_deg"));
1727 routing->Add(new SettingEntry("pf.pathfinder_for_roadvehs"));
1728 routing->Add(new SettingEntry("pf.pathfinder_for_ships"));
1731 vehicles->Add(new SettingEntry("order.no_servicing_if_no_breakdowns"));
1732 vehicles->Add(new SettingEntry("order.serviceathelipad"));
1735 SettingsPage *limitations = main->Add(new SettingsPage(STR_CONFIG_SETTING_LIMITATIONS));
1737 limitations->Add(new SettingEntry("construction.command_pause_level"));
1738 limitations->Add(new SettingEntry("construction.autoslope"));
1739 limitations->Add(new SettingEntry("construction.extra_dynamite"));
1740 limitations->Add(new SettingEntry("construction.map_height_limit"));
1741 limitations->Add(new SettingEntry("construction.max_bridge_length"));
1742 limitations->Add(new SettingEntry("construction.max_bridge_height"));
1743 limitations->Add(new SettingEntry("construction.max_tunnel_length"));
1744 limitations->Add(new SettingEntry("station.never_expire_airports"));
1745 limitations->Add(new SettingEntry("vehicle.never_expire_vehicles"));
1746 limitations->Add(new SettingEntry("vehicle.max_trains"));
1747 limitations->Add(new SettingEntry("vehicle.max_roadveh"));
1748 limitations->Add(new SettingEntry("vehicle.max_aircraft"));
1749 limitations->Add(new SettingEntry("vehicle.max_ships"));
1750 limitations->Add(new SettingEntry("vehicle.max_train_length"));
1751 limitations->Add(new SettingEntry("station.station_spread"));
1752 limitations->Add(new SettingEntry("station.distant_join_stations"));
1753 limitations->Add(new SettingEntry("construction.road_stop_on_town_road"));
1754 limitations->Add(new SettingEntry("construction.road_stop_on_competitor_road"));
1755 limitations->Add(new SettingEntry("vehicle.disable_elrails"));
1758 SettingsPage *disasters = main->Add(new SettingsPage(STR_CONFIG_SETTING_ACCIDENTS));
1760 disasters->Add(new SettingEntry("difficulty.disasters"));
1761 disasters->Add(new SettingEntry("difficulty.economy"));
1762 disasters->Add(new SettingEntry("difficulty.vehicle_breakdowns"));
1763 disasters->Add(new SettingEntry("vehicle.plane_crashes"));
1766 SettingsPage *genworld = main->Add(new SettingsPage(STR_CONFIG_SETTING_GENWORLD));
1768 genworld->Add(new SettingEntry("game_creation.landscape"));
1769 genworld->Add(new SettingEntry("game_creation.land_generator"));
1770 genworld->Add(new SettingEntry("difficulty.terrain_type"));
1771 genworld->Add(new SettingEntry("game_creation.tgen_smoothness"));
1772 genworld->Add(new SettingEntry("game_creation.variety"));
1773 genworld->Add(new SettingEntry("game_creation.snow_coverage"));
1774 genworld->Add(new SettingEntry("game_creation.snow_line_height"));
1775 genworld->Add(new SettingEntry("game_creation.desert_coverage"));
1776 genworld->Add(new SettingEntry("game_creation.amount_of_rivers"));
1777 genworld->Add(new SettingEntry("game_creation.tree_placer"));
1778 genworld->Add(new SettingEntry("vehicle.road_side"));
1779 genworld->Add(new SettingEntry("economy.larger_towns"));
1780 genworld->Add(new SettingEntry("economy.initial_city_size"));
1781 genworld->Add(new SettingEntry("economy.town_layout"));
1782 genworld->Add(new SettingEntry("difficulty.industry_density"));
1783 genworld->Add(new SettingEntry("gui.pause_on_newgame"));
1784 genworld->Add(new SettingEntry("game_creation.ending_year"));
1787 SettingsPage *environment = main->Add(new SettingsPage(STR_CONFIG_SETTING_ENVIRONMENT));
1789 SettingsPage *authorities = environment->Add(new SettingsPage(STR_CONFIG_SETTING_ENVIRONMENT_AUTHORITIES));
1791 authorities->Add(new SettingEntry("difficulty.town_council_tolerance"));
1792 authorities->Add(new SettingEntry("economy.bribe"));
1793 authorities->Add(new SettingEntry("economy.exclusive_rights"));
1794 authorities->Add(new SettingEntry("economy.fund_roads"));
1795 authorities->Add(new SettingEntry("economy.fund_buildings"));
1796 authorities->Add(new SettingEntry("economy.station_noise_level"));
1799 SettingsPage *towns = environment->Add(new SettingsPage(STR_CONFIG_SETTING_ENVIRONMENT_TOWNS));
1801 towns->Add(new SettingEntry("economy.town_growth_rate"));
1802 towns->Add(new SettingEntry("economy.allow_town_roads"));
1803 towns->Add(new SettingEntry("economy.allow_town_level_crossings"));
1804 towns->Add(new SettingEntry("economy.found_town"));
1805 towns->Add(new SettingEntry("economy.town_cargogen_mode"));
1808 SettingsPage *industries = environment->Add(new SettingsPage(STR_CONFIG_SETTING_ENVIRONMENT_INDUSTRIES));
1810 industries->Add(new SettingEntry("construction.raw_industry_construction"));
1811 industries->Add(new SettingEntry("construction.industry_platform"));
1812 industries->Add(new SettingEntry("economy.multiple_industry_per_town"));
1813 industries->Add(new SettingEntry("game_creation.oil_refinery_limit"));
1814 industries->Add(new SettingEntry("economy.type"));
1815 industries->Add(new SettingEntry("station.serve_neutral_industries"));
1818 SettingsPage *cdist = environment->Add(new SettingsPage(STR_CONFIG_SETTING_ENVIRONMENT_CARGODIST));
1820 cdist->Add(new SettingEntry("linkgraph.recalc_time"));
1821 cdist->Add(new SettingEntry("linkgraph.recalc_interval"));
1822 cdist->Add(new SettingEntry("linkgraph.distribution_pax"));
1823 cdist->Add(new SettingEntry("linkgraph.distribution_mail"));
1824 cdist->Add(new SettingEntry("linkgraph.distribution_armoured"));
1825 cdist->Add(new SettingEntry("linkgraph.distribution_default"));
1826 cdist->Add(new SettingEntry("linkgraph.accuracy"));
1827 cdist->Add(new SettingEntry("linkgraph.demand_distance"));
1828 cdist->Add(new SettingEntry("linkgraph.demand_size"));
1829 cdist->Add(new SettingEntry("linkgraph.short_path_saturation"));
1832 environment->Add(new SettingEntry("station.modified_catchment"));
1833 environment->Add(new SettingEntry("construction.extra_tree_placement"));
1836 SettingsPage *ai = main->Add(new SettingsPage(STR_CONFIG_SETTING_AI));
1838 SettingsPage *npc = ai->Add(new SettingsPage(STR_CONFIG_SETTING_AI_NPC));
1840 npc->Add(new SettingEntry("script.settings_profile"));
1841 npc->Add(new SettingEntry("script.script_max_opcode_till_suspend"));
1842 npc->Add(new SettingEntry("script.script_max_memory_megabytes"));
1843 npc->Add(new SettingEntry("difficulty.competitor_speed"));
1844 npc->Add(new SettingEntry("ai.ai_in_multiplayer"));
1845 npc->Add(new SettingEntry("ai.ai_disable_veh_train"));
1846 npc->Add(new SettingEntry("ai.ai_disable_veh_roadveh"));
1847 npc->Add(new SettingEntry("ai.ai_disable_veh_aircraft"));
1848 npc->Add(new SettingEntry("ai.ai_disable_veh_ship"));
1851 ai->Add(new SettingEntry("economy.give_money"));
1852 ai->Add(new SettingEntry("economy.allow_shares"));
1853 ai->Add(new SettingEntry("economy.min_years_for_shares"));
1856 SettingsPage *network = main->Add(new SettingsPage(STR_CONFIG_SETTING_NETWORK));
1858 network->Add(new SettingEntry("network.use_relay_service"));
1861 main->Init();
1863 return *main;
1866 static const StringID _game_settings_restrict_dropdown[] = {
1867 STR_CONFIG_SETTING_RESTRICT_BASIC, // RM_BASIC
1868 STR_CONFIG_SETTING_RESTRICT_ADVANCED, // RM_ADVANCED
1869 STR_CONFIG_SETTING_RESTRICT_ALL, // RM_ALL
1870 STR_CONFIG_SETTING_RESTRICT_CHANGED_AGAINST_DEFAULT, // RM_CHANGED_AGAINST_DEFAULT
1871 STR_CONFIG_SETTING_RESTRICT_CHANGED_AGAINST_NEW, // RM_CHANGED_AGAINST_NEW
1873 static_assert(lengthof(_game_settings_restrict_dropdown) == RM_END);
1875 /** Warnings about hidden search results. */
1876 enum WarnHiddenResult {
1877 WHR_NONE, ///< Nothing was filtering matches away.
1878 WHR_CATEGORY, ///< Category setting filtered matches away.
1879 WHR_TYPE, ///< Type setting filtered matches away.
1880 WHR_CATEGORY_TYPE, ///< Both category and type settings filtered matches away.
1884 * Callback function for the reset all settings button
1885 * @param w Window which is calling this callback
1886 * @param confirmed boolean value, true when yes was clicked, false otherwise
1888 static void ResetAllSettingsConfirmationCallback(Window *w, bool confirmed)
1890 if (confirmed) {
1891 GetSettingsTree().ResetAll();
1892 GetSettingsTree().FoldAll();
1893 w->InvalidateData();
1897 /** Window to edit settings of the game. */
1898 struct GameSettingsWindow : Window {
1899 static const int SETTINGTREE_LEFT_OFFSET = 5; ///< Position of left edge of setting values
1900 static const int SETTINGTREE_RIGHT_OFFSET = 5; ///< Position of right edge of setting values
1901 static const int SETTINGTREE_TOP_OFFSET = 5; ///< Position of top edge of setting values
1902 static const int SETTINGTREE_BOTTOM_OFFSET = 5; ///< Position of bottom edge of setting values
1904 static GameSettings *settings_ptr; ///< Pointer to the game settings being displayed and modified.
1906 SettingEntry *valuewindow_entry; ///< If non-nullptr, pointer to setting for which a value-entering window has been opened.
1907 SettingEntry *clicked_entry; ///< If non-nullptr, pointer to a clicked numeric setting (with a depressed left or right button).
1908 SettingEntry *last_clicked; ///< If non-nullptr, pointer to the last clicked setting.
1909 SettingEntry *valuedropdown_entry; ///< If non-nullptr, pointer to the value for which a dropdown window is currently opened.
1910 bool closing_dropdown; ///< True, if the dropdown list is currently closing.
1912 SettingFilter filter; ///< Filter for the list.
1913 QueryString filter_editbox; ///< Filter editbox;
1914 bool manually_changed_folding; ///< Whether the user expanded/collapsed something manually.
1915 WarnHiddenResult warn_missing; ///< Whether and how to warn about missing search results.
1916 int warn_lines; ///< Number of lines used for warning about missing search results.
1918 Scrollbar *vscroll;
1920 GameSettingsWindow(WindowDesc *desc) : Window(desc), filter_editbox(50)
1922 this->warn_missing = WHR_NONE;
1923 this->warn_lines = 0;
1924 this->filter.mode = (RestrictionMode)_settings_client.gui.settings_restriction_mode;
1925 this->filter.min_cat = RM_ALL;
1926 this->filter.type = ST_ALL;
1927 this->filter.type_hides = false;
1928 this->settings_ptr = &GetGameSettings();
1930 _circle_size = maxdim(GetSpriteSize(SPR_CIRCLE_FOLDED), GetSpriteSize(SPR_CIRCLE_UNFOLDED));
1931 GetSettingsTree().FoldAll(); // Close all sub-pages
1933 this->valuewindow_entry = nullptr; // No setting entry for which a entry window is opened
1934 this->clicked_entry = nullptr; // No numeric setting buttons are depressed
1935 this->last_clicked = nullptr;
1936 this->valuedropdown_entry = nullptr;
1937 this->closing_dropdown = false;
1938 this->manually_changed_folding = false;
1940 this->CreateNestedTree();
1941 this->vscroll = this->GetScrollbar(WID_GS_SCROLLBAR);
1942 this->FinishInitNested(WN_GAME_OPTIONS_GAME_SETTINGS);
1944 this->querystrings[WID_GS_FILTER] = &this->filter_editbox;
1945 this->filter_editbox.cancel_button = QueryString::ACTION_CLEAR;
1946 this->SetFocusedWidget(WID_GS_FILTER);
1948 this->InvalidateData();
1951 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
1953 switch (widget) {
1954 case WID_GS_OPTIONSPANEL:
1955 resize->height = SETTING_HEIGHT = std::max({(int)_circle_size.height, SETTING_BUTTON_HEIGHT, FONT_HEIGHT_NORMAL}) + 1;
1956 resize->width = 1;
1958 size->height = 5 * resize->height + SETTINGTREE_TOP_OFFSET + SETTINGTREE_BOTTOM_OFFSET;
1959 break;
1961 case WID_GS_HELP_TEXT: {
1962 static const StringID setting_types[] = {
1963 STR_CONFIG_SETTING_TYPE_CLIENT,
1964 STR_CONFIG_SETTING_TYPE_COMPANY_MENU, STR_CONFIG_SETTING_TYPE_COMPANY_INGAME,
1965 STR_CONFIG_SETTING_TYPE_GAME_MENU, STR_CONFIG_SETTING_TYPE_GAME_INGAME,
1967 for (uint i = 0; i < lengthof(setting_types); i++) {
1968 SetDParam(0, setting_types[i]);
1969 size->width = std::max(size->width, GetStringBoundingBox(STR_CONFIG_SETTING_TYPE).width);
1971 size->height = 2 * FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL +
1972 std::max(size->height, GetSettingsTree().GetMaxHelpHeight(size->width));
1973 break;
1976 case WID_GS_RESTRICT_CATEGORY:
1977 case WID_GS_RESTRICT_TYPE:
1978 size->width = std::max(GetStringBoundingBox(STR_CONFIG_SETTING_RESTRICT_CATEGORY).width, GetStringBoundingBox(STR_CONFIG_SETTING_RESTRICT_TYPE).width);
1979 break;
1981 default:
1982 break;
1986 void OnPaint() override
1988 if (this->closing_dropdown) {
1989 this->closing_dropdown = false;
1990 assert(this->valuedropdown_entry != nullptr);
1991 this->valuedropdown_entry->SetButtons(0);
1992 this->valuedropdown_entry = nullptr;
1995 /* Reserve the correct number of lines for the 'some search results are hidden' notice in the central settings display panel. */
1996 const NWidgetBase *panel = this->GetWidget<NWidgetBase>(WID_GS_OPTIONSPANEL);
1997 StringID warn_str = STR_CONFIG_SETTING_CATEGORY_HIDES - 1 + this->warn_missing;
1998 int new_warn_lines;
1999 if (this->warn_missing == WHR_NONE) {
2000 new_warn_lines = 0;
2001 } else {
2002 SetDParam(0, _game_settings_restrict_dropdown[this->filter.min_cat]);
2003 new_warn_lines = GetStringLineCount(warn_str, panel->current_x);
2005 if (this->warn_lines != new_warn_lines) {
2006 this->vscroll->SetCount(this->vscroll->GetCount() - this->warn_lines + new_warn_lines);
2007 this->warn_lines = new_warn_lines;
2010 this->DrawWidgets();
2012 /* Draw the 'some search results are hidden' notice. */
2013 if (this->warn_missing != WHR_NONE) {
2014 const int left = panel->pos_x;
2015 const int right = left + panel->current_x - 1;
2016 const int top = panel->pos_y + WD_FRAMETEXT_TOP + (SETTING_HEIGHT - FONT_HEIGHT_NORMAL) * this->warn_lines / 2;
2017 SetDParam(0, _game_settings_restrict_dropdown[this->filter.min_cat]);
2018 if (this->warn_lines == 1) {
2019 /* If the warning fits at one line, center it. */
2020 DrawString(left + WD_FRAMETEXT_LEFT, right - WD_FRAMETEXT_RIGHT, top, warn_str, TC_FROMSTRING, SA_HOR_CENTER);
2021 } else {
2022 DrawStringMultiLine(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, top, INT32_MAX, warn_str, TC_FROMSTRING, SA_HOR_CENTER);
2027 void SetStringParameters(int widget) const override
2029 switch (widget) {
2030 case WID_GS_RESTRICT_DROPDOWN:
2031 SetDParam(0, _game_settings_restrict_dropdown[this->filter.mode]);
2032 break;
2034 case WID_GS_TYPE_DROPDOWN:
2035 switch (this->filter.type) {
2036 case ST_GAME: SetDParam(0, _game_mode == GM_MENU ? STR_CONFIG_SETTING_TYPE_DROPDOWN_GAME_MENU : STR_CONFIG_SETTING_TYPE_DROPDOWN_GAME_INGAME); break;
2037 case ST_COMPANY: SetDParam(0, _game_mode == GM_MENU ? STR_CONFIG_SETTING_TYPE_DROPDOWN_COMPANY_MENU : STR_CONFIG_SETTING_TYPE_DROPDOWN_COMPANY_INGAME); break;
2038 case ST_CLIENT: SetDParam(0, STR_CONFIG_SETTING_TYPE_DROPDOWN_CLIENT); break;
2039 default: SetDParam(0, STR_CONFIG_SETTING_TYPE_DROPDOWN_ALL); break;
2041 break;
2045 DropDownList BuildDropDownList(int widget) const
2047 DropDownList list;
2048 switch (widget) {
2049 case WID_GS_RESTRICT_DROPDOWN:
2050 for (int mode = 0; mode != RM_END; mode++) {
2051 /* If we are in adv. settings screen for the new game's settings,
2052 * we don't want to allow comparing with new game's settings. */
2053 bool disabled = mode == RM_CHANGED_AGAINST_NEW && settings_ptr == &_settings_newgame;
2055 list.emplace_back(new DropDownListStringItem(_game_settings_restrict_dropdown[mode], mode, disabled));
2057 break;
2059 case WID_GS_TYPE_DROPDOWN:
2060 list.emplace_back(new DropDownListStringItem(STR_CONFIG_SETTING_TYPE_DROPDOWN_ALL, ST_ALL, false));
2061 list.emplace_back(new DropDownListStringItem(_game_mode == GM_MENU ? STR_CONFIG_SETTING_TYPE_DROPDOWN_GAME_MENU : STR_CONFIG_SETTING_TYPE_DROPDOWN_GAME_INGAME, ST_GAME, false));
2062 list.emplace_back(new DropDownListStringItem(_game_mode == GM_MENU ? STR_CONFIG_SETTING_TYPE_DROPDOWN_COMPANY_MENU : STR_CONFIG_SETTING_TYPE_DROPDOWN_COMPANY_INGAME, ST_COMPANY, false));
2063 list.emplace_back(new DropDownListStringItem(STR_CONFIG_SETTING_TYPE_DROPDOWN_CLIENT, ST_CLIENT, false));
2064 break;
2066 return list;
2069 void DrawWidget(const Rect &r, int widget) const override
2071 switch (widget) {
2072 case WID_GS_OPTIONSPANEL: {
2073 int top_pos = r.top + SETTINGTREE_TOP_OFFSET + 1 + this->warn_lines * SETTING_HEIGHT;
2074 uint last_row = this->vscroll->GetPosition() + this->vscroll->GetCapacity() - this->warn_lines;
2075 int next_row = GetSettingsTree().Draw(settings_ptr, r.left + SETTINGTREE_LEFT_OFFSET, r.right - SETTINGTREE_RIGHT_OFFSET, top_pos,
2076 this->vscroll->GetPosition(), last_row, this->last_clicked);
2077 if (next_row == 0) DrawString(r.left + SETTINGTREE_LEFT_OFFSET, r.right - SETTINGTREE_RIGHT_OFFSET, top_pos, STR_CONFIG_SETTINGS_NONE);
2078 break;
2081 case WID_GS_HELP_TEXT:
2082 if (this->last_clicked != nullptr) {
2083 const IntSettingDesc *sd = this->last_clicked->setting;
2085 int y = r.top;
2086 switch (sd->GetType()) {
2087 case ST_COMPANY: SetDParam(0, _game_mode == GM_MENU ? STR_CONFIG_SETTING_TYPE_COMPANY_MENU : STR_CONFIG_SETTING_TYPE_COMPANY_INGAME); break;
2088 case ST_CLIENT: SetDParam(0, STR_CONFIG_SETTING_TYPE_CLIENT); break;
2089 case ST_GAME: SetDParam(0, _game_mode == GM_MENU ? STR_CONFIG_SETTING_TYPE_GAME_MENU : STR_CONFIG_SETTING_TYPE_GAME_INGAME); break;
2090 default: NOT_REACHED();
2092 DrawString(r.left, r.right, y, STR_CONFIG_SETTING_TYPE);
2093 y += FONT_HEIGHT_NORMAL;
2095 this->last_clicked->SetValueDParams(0, sd->def);
2096 DrawString(r.left, r.right, y, STR_CONFIG_SETTING_DEFAULT_VALUE);
2097 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
2099 DrawStringMultiLine(r.left, r.right, y, r.bottom, this->last_clicked->GetHelpText(), TC_WHITE);
2101 break;
2103 default:
2104 break;
2109 * Set the entry that should have its help text displayed, and mark the window dirty so it gets repainted.
2110 * @param pe Setting to display help text of, use \c nullptr to stop displaying help of the currently displayed setting.
2112 void SetDisplayedHelpText(SettingEntry *pe)
2114 if (this->last_clicked != pe) this->SetDirty();
2115 this->last_clicked = pe;
2118 void OnClick(Point pt, int widget, int click_count) override
2120 switch (widget) {
2121 case WID_GS_EXPAND_ALL:
2122 this->manually_changed_folding = true;
2123 GetSettingsTree().UnFoldAll();
2124 this->InvalidateData();
2125 break;
2127 case WID_GS_COLLAPSE_ALL:
2128 this->manually_changed_folding = true;
2129 GetSettingsTree().FoldAll();
2130 this->InvalidateData();
2131 break;
2133 case WID_GS_RESET_ALL:
2134 ShowQuery(
2135 STR_CONFIG_SETTING_RESET_ALL_CONFIRMATION_DIALOG_CAPTION,
2136 STR_CONFIG_SETTING_RESET_ALL_CONFIRMATION_DIALOG_TEXT,
2137 this,
2138 ResetAllSettingsConfirmationCallback
2140 break;
2142 case WID_GS_RESTRICT_DROPDOWN: {
2143 DropDownList list = this->BuildDropDownList(widget);
2144 if (!list.empty()) {
2145 ShowDropDownList(this, std::move(list), this->filter.mode, widget);
2147 break;
2150 case WID_GS_TYPE_DROPDOWN: {
2151 DropDownList list = this->BuildDropDownList(widget);
2152 if (!list.empty()) {
2153 ShowDropDownList(this, std::move(list), this->filter.type, widget);
2155 break;
2159 if (widget != WID_GS_OPTIONSPANEL) return;
2161 uint btn = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GS_OPTIONSPANEL, SETTINGTREE_TOP_OFFSET);
2162 if (btn == INT_MAX || (int)btn < this->warn_lines) return;
2163 btn -= this->warn_lines;
2165 uint cur_row = 0;
2166 BaseSettingEntry *clicked_entry = GetSettingsTree().FindEntry(btn, &cur_row);
2168 if (clicked_entry == nullptr) return; // Clicked below the last setting of the page
2170 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
2171 if (x < 0) return; // Clicked left of the entry
2173 SettingsPage *clicked_page = dynamic_cast<SettingsPage*>(clicked_entry);
2174 if (clicked_page != nullptr) {
2175 this->SetDisplayedHelpText(nullptr);
2176 clicked_page->folded = !clicked_page->folded; // Flip 'folded'-ness of the sub-page
2178 this->manually_changed_folding = true;
2180 this->InvalidateData();
2181 return;
2184 SettingEntry *pe = dynamic_cast<SettingEntry*>(clicked_entry);
2185 assert(pe != nullptr);
2186 const IntSettingDesc *sd = pe->setting;
2188 /* return if action is only active in network, or only settable by server */
2189 if (!sd->IsEditable()) {
2190 this->SetDisplayedHelpText(pe);
2191 return;
2194 int32 value = sd->Read(ResolveObject(settings_ptr, sd));
2196 /* clicked on the icon on the left side. Either scroller, bool on/off or dropdown */
2197 if (x < SETTING_BUTTON_WIDTH && (sd->flags & SF_GUI_DROPDOWN)) {
2198 this->SetDisplayedHelpText(pe);
2200 if (this->valuedropdown_entry == pe) {
2201 /* unclick the dropdown */
2202 HideDropDownMenu(this);
2203 this->closing_dropdown = false;
2204 this->valuedropdown_entry->SetButtons(0);
2205 this->valuedropdown_entry = nullptr;
2206 } else {
2207 if (this->valuedropdown_entry != nullptr) this->valuedropdown_entry->SetButtons(0);
2208 this->closing_dropdown = false;
2210 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_GS_OPTIONSPANEL);
2211 int rel_y = (pt.y - (int)wid->pos_y - SETTINGTREE_TOP_OFFSET) % wid->resize_y;
2213 Rect wi_rect;
2214 wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? SETTING_BUTTON_WIDTH - 1 - x : x);
2215 wi_rect.right = wi_rect.left + SETTING_BUTTON_WIDTH - 1;
2216 wi_rect.top = pt.y - rel_y + (SETTING_HEIGHT - SETTING_BUTTON_HEIGHT) / 2;
2217 wi_rect.bottom = wi_rect.top + SETTING_BUTTON_HEIGHT - 1;
2219 /* For dropdowns we also have to check the y position thoroughly, the mouse may not above the just opening dropdown */
2220 if (pt.y >= wi_rect.top && pt.y <= wi_rect.bottom) {
2221 this->valuedropdown_entry = pe;
2222 this->valuedropdown_entry->SetButtons(SEF_LEFT_DEPRESSED);
2224 DropDownList list;
2225 for (int i = sd->min; i <= (int)sd->max; i++) {
2226 list.emplace_back(new DropDownListStringItem(sd->str_val + i - sd->min, i, false));
2229 ShowDropDownListAt(this, std::move(list), value, -1, wi_rect, COLOUR_ORANGE, true);
2232 this->SetDirty();
2233 } else if (x < SETTING_BUTTON_WIDTH) {
2234 this->SetDisplayedHelpText(pe);
2235 int32 oldvalue = value;
2237 if (sd->IsBoolSetting()) {
2238 value ^= 1;
2239 } else {
2240 /* Add a dynamic step-size to the scroller. In a maximum of
2241 * 50-steps you should be able to get from min to max,
2242 * unless specified otherwise in the 'interval' variable
2243 * of the current setting. */
2244 uint32 step = (sd->interval == 0) ? ((sd->max - sd->min) / 50) : sd->interval;
2245 if (step == 0) step = 1;
2247 /* don't allow too fast scrolling */
2248 if ((this->flags & WF_TIMEOUT) && this->timeout_timer > 1) {
2249 _left_button_clicked = false;
2250 return;
2253 /* Increase or decrease the value and clamp it to extremes */
2254 if (x >= SETTING_BUTTON_WIDTH / 2) {
2255 value += step;
2256 if (sd->min < 0) {
2257 assert((int32)sd->max >= 0);
2258 if (value > (int32)sd->max) value = (int32)sd->max;
2259 } else {
2260 if ((uint32)value > sd->max) value = (int32)sd->max;
2262 if (value < sd->min) value = sd->min; // skip between "disabled" and minimum
2263 } else {
2264 value -= step;
2265 if (value < sd->min) value = (sd->flags & SF_GUI_0_IS_SPECIAL) ? 0 : sd->min;
2268 /* Set up scroller timeout for numeric values */
2269 if (value != oldvalue) {
2270 if (this->clicked_entry != nullptr) { // Release previous buttons if any
2271 this->clicked_entry->SetButtons(0);
2273 this->clicked_entry = pe;
2274 this->clicked_entry->SetButtons((x >= SETTING_BUTTON_WIDTH / 2) != (_current_text_dir == TD_RTL) ? SEF_RIGHT_DEPRESSED : SEF_LEFT_DEPRESSED);
2275 this->SetTimeout();
2276 _left_button_clicked = false;
2280 if (value != oldvalue) {
2281 SetSettingValue(sd, value);
2282 this->SetDirty();
2284 } else {
2285 /* Only open editbox if clicked for the second time, and only for types where it is sensible for. */
2286 if (this->last_clicked == pe && !sd->IsBoolSetting() && !(sd->flags & SF_GUI_DROPDOWN)) {
2287 int64 value64 = value;
2288 /* Show the correct currency-translated value */
2289 if (sd->flags & SF_GUI_CURRENCY) value64 *= _currency->rate;
2291 CharSetFilter charset_filter = CS_NUMERAL; //default, only numeric input allowed
2292 if (sd->min < 0) charset_filter = CS_NUMERAL_SIGNED; // special case, also allow '-' sign for negative input
2294 this->valuewindow_entry = pe;
2295 SetDParam(0, value64);
2296 /* Limit string length to 14 so that MAX_INT32 * max currency rate doesn't exceed MAX_INT64. */
2297 ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 15, this, charset_filter, QSF_ENABLE_DEFAULT);
2299 this->SetDisplayedHelpText(pe);
2303 void OnTimeout() override
2305 if (this->clicked_entry != nullptr) { // On timeout, release any depressed buttons
2306 this->clicked_entry->SetButtons(0);
2307 this->clicked_entry = nullptr;
2308 this->SetDirty();
2312 void OnQueryTextFinished(char *str) override
2314 /* The user pressed cancel */
2315 if (str == nullptr) return;
2317 assert(this->valuewindow_entry != nullptr);
2318 const IntSettingDesc *sd = this->valuewindow_entry->setting;
2320 int32 value;
2321 if (!StrEmpty(str)) {
2322 long long llvalue = atoll(str);
2324 /* Save the correct currency-translated value */
2325 if (sd->flags & SF_GUI_CURRENCY) llvalue /= _currency->rate;
2327 value = (int32)ClampToI32(llvalue);
2328 } else {
2329 value = sd->def;
2332 SetSettingValue(this->valuewindow_entry->setting, value);
2333 this->SetDirty();
2336 void OnDropdownSelect(int widget, int index) override
2338 switch (widget) {
2339 case WID_GS_RESTRICT_DROPDOWN:
2340 this->filter.mode = (RestrictionMode)index;
2341 if (this->filter.mode == RM_CHANGED_AGAINST_DEFAULT ||
2342 this->filter.mode == RM_CHANGED_AGAINST_NEW) {
2344 if (!this->manually_changed_folding) {
2345 /* Expand all when selecting 'changes'. Update the filter state first, in case it becomes less restrictive in some cases. */
2346 GetSettingsTree().UpdateFilterState(this->filter, false);
2347 GetSettingsTree().UnFoldAll();
2349 } else {
2350 /* Non-'changes' filter. Save as default. */
2351 _settings_client.gui.settings_restriction_mode = this->filter.mode;
2353 this->InvalidateData();
2354 break;
2356 case WID_GS_TYPE_DROPDOWN:
2357 this->filter.type = (SettingType)index;
2358 this->InvalidateData();
2359 break;
2361 default:
2362 if (widget < 0) {
2363 /* Deal with drop down boxes on the panel. */
2364 assert(this->valuedropdown_entry != nullptr);
2365 const IntSettingDesc *sd = this->valuedropdown_entry->setting;
2366 assert(sd->flags & SF_GUI_DROPDOWN);
2368 SetSettingValue(sd, index);
2369 this->SetDirty();
2371 break;
2375 void OnDropdownClose(Point pt, int widget, int index, bool instant_close) override
2377 if (widget >= 0) {
2378 /* Normally the default implementation of OnDropdownClose() takes care of
2379 * a few things. We want that behaviour here too, but only for
2380 * "normal" dropdown boxes. The special dropdown boxes added for every
2381 * setting that needs one can't have this call. */
2382 Window::OnDropdownClose(pt, widget, index, instant_close);
2383 } else {
2384 /* We cannot raise the dropdown button just yet. OnClick needs some hint, whether
2385 * the same dropdown button was clicked again, and then not open the dropdown again.
2386 * So, we only remember that it was closed, and process it on the next OnPaint, which is
2387 * after OnClick. */
2388 assert(this->valuedropdown_entry != nullptr);
2389 this->closing_dropdown = true;
2390 this->SetDirty();
2394 void OnInvalidateData(int data = 0, bool gui_scope = true) override
2396 if (!gui_scope) return;
2398 /* Update which settings are to be visible. */
2399 RestrictionMode min_level = (this->filter.mode <= RM_ALL) ? this->filter.mode : RM_BASIC;
2400 this->filter.min_cat = min_level;
2401 this->filter.type_hides = false;
2402 GetSettingsTree().UpdateFilterState(this->filter, false);
2404 if (this->filter.string.IsEmpty()) {
2405 this->warn_missing = WHR_NONE;
2406 } else if (min_level < this->filter.min_cat) {
2407 this->warn_missing = this->filter.type_hides ? WHR_CATEGORY_TYPE : WHR_CATEGORY;
2408 } else {
2409 this->warn_missing = this->filter.type_hides ? WHR_TYPE : WHR_NONE;
2411 this->vscroll->SetCount(GetSettingsTree().Length() + this->warn_lines);
2413 if (this->last_clicked != nullptr && !GetSettingsTree().IsVisible(this->last_clicked)) {
2414 this->SetDisplayedHelpText(nullptr);
2417 bool all_folded = true;
2418 bool all_unfolded = true;
2419 GetSettingsTree().GetFoldingState(all_folded, all_unfolded);
2420 this->SetWidgetDisabledState(WID_GS_EXPAND_ALL, all_unfolded);
2421 this->SetWidgetDisabledState(WID_GS_COLLAPSE_ALL, all_folded);
2424 void OnEditboxChanged(int wid) override
2426 if (wid == WID_GS_FILTER) {
2427 this->filter.string.SetFilterTerm(this->filter_editbox.text.buf);
2428 if (!this->filter.string.IsEmpty() && !this->manually_changed_folding) {
2429 /* User never expanded/collapsed single pages and entered a filter term.
2430 * Expand everything, to save weird expand clicks, */
2431 GetSettingsTree().UnFoldAll();
2433 this->InvalidateData();
2437 void OnResize() override
2439 this->vscroll->SetCapacityFromWidget(this, WID_GS_OPTIONSPANEL, SETTINGTREE_TOP_OFFSET + SETTINGTREE_BOTTOM_OFFSET);
2443 GameSettings *GameSettingsWindow::settings_ptr = nullptr;
2445 static const NWidgetPart _nested_settings_selection_widgets[] = {
2446 NWidget(NWID_HORIZONTAL),
2447 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
2448 NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_CONFIG_SETTING_TREE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2449 NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
2450 EndContainer(),
2451 NWidget(WWT_PANEL, COLOUR_MAUVE),
2452 NWidget(NWID_VERTICAL), SetPIP(WD_TEXTPANEL_TOP, WD_PAR_VSEP_NORMAL, WD_TEXTPANEL_BOTTOM),
2453 NWidget(NWID_HORIZONTAL), SetPIP(WD_FRAMETEXT_LEFT, WD_FRAMETEXT_RIGHT, WD_FRAMETEXT_RIGHT),
2454 NWidget(WWT_TEXT, COLOUR_MAUVE, WID_GS_RESTRICT_CATEGORY), SetDataTip(STR_CONFIG_SETTING_RESTRICT_CATEGORY, STR_NULL),
2455 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),
2456 EndContainer(),
2457 NWidget(NWID_HORIZONTAL), SetPIP(WD_FRAMETEXT_LEFT, WD_FRAMETEXT_RIGHT, WD_FRAMETEXT_RIGHT),
2458 NWidget(WWT_TEXT, COLOUR_MAUVE, WID_GS_RESTRICT_TYPE), SetDataTip(STR_CONFIG_SETTING_RESTRICT_TYPE, STR_NULL),
2459 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),
2460 EndContainer(),
2461 NWidget(NWID_HORIZONTAL), SetPIP(WD_FRAMETEXT_LEFT, WD_FRAMETEXT_RIGHT, WD_FRAMETEXT_RIGHT),
2462 NWidget(WWT_TEXT, COLOUR_MAUVE), SetFill(0, 1), SetDataTip(STR_CONFIG_SETTING_FILTER_TITLE, STR_NULL),
2463 NWidget(WWT_EDITBOX, COLOUR_MAUVE, WID_GS_FILTER), SetMinimalSize(50, 12), SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
2464 EndContainer(),
2465 EndContainer(),
2466 EndContainer(),
2467 NWidget(NWID_HORIZONTAL),
2468 NWidget(WWT_PANEL, COLOUR_MAUVE, WID_GS_OPTIONSPANEL), SetMinimalSize(400, 174), SetScrollbar(WID_GS_SCROLLBAR), EndContainer(),
2469 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_GS_SCROLLBAR),
2470 EndContainer(),
2471 NWidget(WWT_PANEL, COLOUR_MAUVE),
2472 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_GS_HELP_TEXT), SetMinimalSize(300, 25), SetFill(1, 1), SetResize(1, 0),
2473 SetPadding(WD_FRAMETEXT_TOP, WD_FRAMETEXT_RIGHT, WD_FRAMETEXT_BOTTOM, WD_FRAMETEXT_LEFT),
2474 EndContainer(),
2475 NWidget(NWID_HORIZONTAL),
2476 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_GS_EXPAND_ALL), SetDataTip(STR_CONFIG_SETTING_EXPAND_ALL, STR_NULL),
2477 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_GS_COLLAPSE_ALL), SetDataTip(STR_CONFIG_SETTING_COLLAPSE_ALL, STR_NULL),
2478 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_GS_RESET_ALL), SetDataTip(STR_CONFIG_SETTING_RESET_ALL, STR_NULL),
2479 NWidget(WWT_PANEL, COLOUR_MAUVE), SetFill(1, 0), SetResize(1, 0),
2480 EndContainer(),
2481 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
2482 EndContainer(),
2485 static WindowDesc _settings_selection_desc(
2486 WDP_CENTER, "settings", 510, 450,
2487 WC_GAME_OPTIONS, WC_NONE,
2489 _nested_settings_selection_widgets, lengthof(_nested_settings_selection_widgets)
2492 /** Open advanced settings window. */
2493 void ShowGameSettings()
2495 CloseWindowByClass(WC_GAME_OPTIONS);
2496 new GameSettingsWindow(&_settings_selection_desc);
2501 * Draw [<][>] boxes.
2502 * @param x the x position to draw
2503 * @param y the y position to draw
2504 * @param button_colour the colour of the button
2505 * @param state 0 = none clicked, 1 = first clicked, 2 = second clicked
2506 * @param clickable_left is the left button clickable?
2507 * @param clickable_right is the right button clickable?
2509 void DrawArrowButtons(int x, int y, Colours button_colour, byte state, bool clickable_left, bool clickable_right)
2511 int colour = _colour_gradient[button_colour][2];
2512 Dimension dim = NWidgetScrollbar::GetHorizontalDimension();
2514 DrawFrameRect(x, y, x + dim.width - 1, y + dim.height - 1, button_colour, (state == 1) ? FR_LOWERED : FR_NONE);
2515 DrawFrameRect(x + dim.width, y, x + dim.width + dim.width - 1, y + dim.height - 1, button_colour, (state == 2) ? FR_LOWERED : FR_NONE);
2516 DrawSprite(SPR_ARROW_LEFT, PAL_NONE, x + WD_IMGBTN_LEFT, y + WD_IMGBTN_TOP);
2517 DrawSprite(SPR_ARROW_RIGHT, PAL_NONE, x + WD_IMGBTN_LEFT + dim.width, y + WD_IMGBTN_TOP);
2519 /* Grey out the buttons that aren't clickable */
2520 bool rtl = _current_text_dir == TD_RTL;
2521 if (rtl ? !clickable_right : !clickable_left) {
2522 GfxFillRect(x + 1, y, x + dim.width - 1, y + dim.height - 2, colour, FILLRECT_CHECKER);
2524 if (rtl ? !clickable_left : !clickable_right) {
2525 GfxFillRect(x + dim.width + 1, y, x + dim.width + dim.width - 1, y + dim.height - 2, colour, FILLRECT_CHECKER);
2530 * Draw a dropdown button.
2531 * @param x the x position to draw
2532 * @param y the y position to draw
2533 * @param button_colour the colour of the button
2534 * @param state true = lowered
2535 * @param clickable is the button clickable?
2537 void DrawDropDownButton(int x, int y, Colours button_colour, bool state, bool clickable)
2539 int colour = _colour_gradient[button_colour][2];
2541 DrawFrameRect(x, y, x + SETTING_BUTTON_WIDTH - 1, y + SETTING_BUTTON_HEIGHT - 1, button_colour, state ? FR_LOWERED : FR_NONE);
2542 DrawSprite(SPR_ARROW_DOWN, PAL_NONE, x + (SETTING_BUTTON_WIDTH - NWidgetScrollbar::GetVerticalDimension().width) / 2 + state, y + 2 + state);
2544 if (!clickable) {
2545 GfxFillRect(x + 1, y, x + SETTING_BUTTON_WIDTH - 1, y + SETTING_BUTTON_HEIGHT - 2, colour, FILLRECT_CHECKER);
2550 * Draw a toggle button.
2551 * @param x the x position to draw
2552 * @param y the y position to draw
2553 * @param state true = lowered
2554 * @param clickable is the button clickable?
2556 void DrawBoolButton(int x, int y, bool state, bool clickable)
2558 static const Colours _bool_ctabs[2][2] = {{COLOUR_CREAM, COLOUR_RED}, {COLOUR_DARK_GREEN, COLOUR_GREEN}};
2559 DrawFrameRect(x, y, x + SETTING_BUTTON_WIDTH - 1, y + SETTING_BUTTON_HEIGHT - 1, _bool_ctabs[state][clickable], state ? FR_LOWERED : FR_NONE);
2562 struct CustomCurrencyWindow : Window {
2563 int query_widget;
2565 CustomCurrencyWindow(WindowDesc *desc) : Window(desc)
2567 this->InitNested();
2569 SetButtonState();
2572 void SetButtonState()
2574 this->SetWidgetDisabledState(WID_CC_RATE_DOWN, _custom_currency.rate == 1);
2575 this->SetWidgetDisabledState(WID_CC_RATE_UP, _custom_currency.rate == UINT16_MAX);
2576 this->SetWidgetDisabledState(WID_CC_YEAR_DOWN, _custom_currency.to_euro == CF_NOEURO);
2577 this->SetWidgetDisabledState(WID_CC_YEAR_UP, _custom_currency.to_euro == MAX_YEAR);
2580 void SetStringParameters(int widget) const override
2582 switch (widget) {
2583 case WID_CC_RATE: SetDParam(0, 1); SetDParam(1, 1); break;
2584 case WID_CC_SEPARATOR: SetDParamStr(0, _custom_currency.separator); break;
2585 case WID_CC_PREFIX: SetDParamStr(0, _custom_currency.prefix); break;
2586 case WID_CC_SUFFIX: SetDParamStr(0, _custom_currency.suffix); break;
2587 case WID_CC_YEAR:
2588 SetDParam(0, (_custom_currency.to_euro != CF_NOEURO) ? STR_CURRENCY_SWITCH_TO_EURO : STR_CURRENCY_SWITCH_TO_EURO_NEVER);
2589 SetDParam(1, _custom_currency.to_euro);
2590 break;
2592 case WID_CC_PREVIEW:
2593 SetDParam(0, 10000);
2594 break;
2598 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
2600 switch (widget) {
2601 /* Set the appropriate width for the edit 'buttons' */
2602 case WID_CC_SEPARATOR_EDIT:
2603 case WID_CC_PREFIX_EDIT:
2604 case WID_CC_SUFFIX_EDIT:
2605 size->width = this->GetWidget<NWidgetBase>(WID_CC_RATE_DOWN)->smallest_x + this->GetWidget<NWidgetBase>(WID_CC_RATE_UP)->smallest_x;
2606 break;
2608 /* Make sure the window is wide enough for the widest exchange rate */
2609 case WID_CC_RATE:
2610 SetDParam(0, 1);
2611 SetDParam(1, INT32_MAX);
2612 *size = GetStringBoundingBox(STR_CURRENCY_EXCHANGE_RATE);
2613 break;
2617 void OnClick(Point pt, int widget, int click_count) override
2619 int line = 0;
2620 int len = 0;
2621 StringID str = 0;
2622 CharSetFilter afilter = CS_ALPHANUMERAL;
2624 switch (widget) {
2625 case WID_CC_RATE_DOWN:
2626 if (_custom_currency.rate > 1) _custom_currency.rate--;
2627 if (_custom_currency.rate == 1) this->DisableWidget(WID_CC_RATE_DOWN);
2628 this->EnableWidget(WID_CC_RATE_UP);
2629 break;
2631 case WID_CC_RATE_UP:
2632 if (_custom_currency.rate < UINT16_MAX) _custom_currency.rate++;
2633 if (_custom_currency.rate == UINT16_MAX) this->DisableWidget(WID_CC_RATE_UP);
2634 this->EnableWidget(WID_CC_RATE_DOWN);
2635 break;
2637 case WID_CC_RATE:
2638 SetDParam(0, _custom_currency.rate);
2639 str = STR_JUST_INT;
2640 len = 5;
2641 line = WID_CC_RATE;
2642 afilter = CS_NUMERAL;
2643 break;
2645 case WID_CC_SEPARATOR_EDIT:
2646 case WID_CC_SEPARATOR:
2647 SetDParamStr(0, _custom_currency.separator);
2648 str = STR_JUST_RAW_STRING;
2649 len = 7;
2650 line = WID_CC_SEPARATOR;
2651 break;
2653 case WID_CC_PREFIX_EDIT:
2654 case WID_CC_PREFIX:
2655 SetDParamStr(0, _custom_currency.prefix);
2656 str = STR_JUST_RAW_STRING;
2657 len = 15;
2658 line = WID_CC_PREFIX;
2659 break;
2661 case WID_CC_SUFFIX_EDIT:
2662 case WID_CC_SUFFIX:
2663 SetDParamStr(0, _custom_currency.suffix);
2664 str = STR_JUST_RAW_STRING;
2665 len = 15;
2666 line = WID_CC_SUFFIX;
2667 break;
2669 case WID_CC_YEAR_DOWN:
2670 _custom_currency.to_euro = (_custom_currency.to_euro <= 2000) ? CF_NOEURO : _custom_currency.to_euro - 1;
2671 if (_custom_currency.to_euro == CF_NOEURO) this->DisableWidget(WID_CC_YEAR_DOWN);
2672 this->EnableWidget(WID_CC_YEAR_UP);
2673 break;
2675 case WID_CC_YEAR_UP:
2676 _custom_currency.to_euro = Clamp(_custom_currency.to_euro + 1, 2000, MAX_YEAR);
2677 if (_custom_currency.to_euro == MAX_YEAR) this->DisableWidget(WID_CC_YEAR_UP);
2678 this->EnableWidget(WID_CC_YEAR_DOWN);
2679 break;
2681 case WID_CC_YEAR:
2682 SetDParam(0, _custom_currency.to_euro);
2683 str = STR_JUST_INT;
2684 len = 7;
2685 line = WID_CC_YEAR;
2686 afilter = CS_NUMERAL;
2687 break;
2690 if (len != 0) {
2691 this->query_widget = line;
2692 ShowQueryString(str, STR_CURRENCY_CHANGE_PARAMETER, len + 1, this, afilter, QSF_NONE);
2695 this->SetTimeout();
2696 this->SetDirty();
2699 void OnQueryTextFinished(char *str) override
2701 if (str == nullptr) return;
2703 switch (this->query_widget) {
2704 case WID_CC_RATE:
2705 _custom_currency.rate = Clamp(atoi(str), 1, UINT16_MAX);
2706 break;
2708 case WID_CC_SEPARATOR: // Thousands separator
2709 _custom_currency.separator = str;
2710 break;
2712 case WID_CC_PREFIX:
2713 _custom_currency.prefix = str;
2714 break;
2716 case WID_CC_SUFFIX:
2717 _custom_currency.suffix = str;
2718 break;
2720 case WID_CC_YEAR: { // Year to switch to euro
2721 int val = atoi(str);
2723 _custom_currency.to_euro = (val < 2000 ? CF_NOEURO : std::min(val, MAX_YEAR));
2724 break;
2727 MarkWholeScreenDirty();
2728 SetButtonState();
2731 void OnTimeout() override
2733 this->SetDirty();
2737 static const NWidgetPart _nested_cust_currency_widgets[] = {
2738 NWidget(NWID_HORIZONTAL),
2739 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
2740 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_CURRENCY_WINDOW, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2741 EndContainer(),
2742 NWidget(WWT_PANEL, COLOUR_GREY),
2743 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(7, 3, 0),
2744 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
2745 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_CC_RATE_DOWN), SetDataTip(AWV_DECREASE, STR_CURRENCY_DECREASE_EXCHANGE_RATE_TOOLTIP),
2746 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_CC_RATE_UP), SetDataTip(AWV_INCREASE, STR_CURRENCY_INCREASE_EXCHANGE_RATE_TOOLTIP),
2747 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
2748 NWidget(WWT_TEXT, COLOUR_BLUE, WID_CC_RATE), SetDataTip(STR_CURRENCY_EXCHANGE_RATE, STR_CURRENCY_SET_EXCHANGE_RATE_TOOLTIP), SetFill(1, 0),
2749 EndContainer(),
2750 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
2751 NWidget(WWT_PUSHBTN, COLOUR_DARK_BLUE, WID_CC_SEPARATOR_EDIT), SetDataTip(0x0, STR_CURRENCY_SET_CUSTOM_CURRENCY_SEPARATOR_TOOLTIP), SetFill(0, 1),
2752 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
2753 NWidget(WWT_TEXT, COLOUR_BLUE, WID_CC_SEPARATOR), SetDataTip(STR_CURRENCY_SEPARATOR, STR_CURRENCY_SET_CUSTOM_CURRENCY_SEPARATOR_TOOLTIP), SetFill(1, 0),
2754 EndContainer(),
2755 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
2756 NWidget(WWT_PUSHBTN, COLOUR_DARK_BLUE, WID_CC_PREFIX_EDIT), SetDataTip(0x0, STR_CURRENCY_SET_CUSTOM_CURRENCY_PREFIX_TOOLTIP), SetFill(0, 1),
2757 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
2758 NWidget(WWT_TEXT, COLOUR_BLUE, WID_CC_PREFIX), SetDataTip(STR_CURRENCY_PREFIX, STR_CURRENCY_SET_CUSTOM_CURRENCY_PREFIX_TOOLTIP), SetFill(1, 0),
2759 EndContainer(),
2760 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
2761 NWidget(WWT_PUSHBTN, COLOUR_DARK_BLUE, WID_CC_SUFFIX_EDIT), SetDataTip(0x0, STR_CURRENCY_SET_CUSTOM_CURRENCY_SUFFIX_TOOLTIP), SetFill(0, 1),
2762 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
2763 NWidget(WWT_TEXT, COLOUR_BLUE, WID_CC_SUFFIX), SetDataTip(STR_CURRENCY_SUFFIX, STR_CURRENCY_SET_CUSTOM_CURRENCY_SUFFIX_TOOLTIP), SetFill(1, 0),
2764 EndContainer(),
2765 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
2766 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_CC_YEAR_DOWN), SetDataTip(AWV_DECREASE, STR_CURRENCY_DECREASE_CUSTOM_CURRENCY_TO_EURO_TOOLTIP),
2767 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_CC_YEAR_UP), SetDataTip(AWV_INCREASE, STR_CURRENCY_INCREASE_CUSTOM_CURRENCY_TO_EURO_TOOLTIP),
2768 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
2769 NWidget(WWT_TEXT, COLOUR_BLUE, WID_CC_YEAR), SetDataTip(STR_JUST_STRING, STR_CURRENCY_SET_CUSTOM_CURRENCY_TO_EURO_TOOLTIP), SetFill(1, 0),
2770 EndContainer(),
2771 EndContainer(),
2772 NWidget(WWT_LABEL, COLOUR_BLUE, WID_CC_PREVIEW),
2773 SetDataTip(STR_CURRENCY_PREVIEW, STR_CURRENCY_CUSTOM_CURRENCY_PREVIEW_TOOLTIP), SetPadding(15, 1, 18, 2),
2774 EndContainer(),
2777 static WindowDesc _cust_currency_desc(
2778 WDP_CENTER, nullptr, 0, 0,
2779 WC_CUSTOM_CURRENCY, WC_NONE,
2781 _nested_cust_currency_widgets, lengthof(_nested_cust_currency_widgets)
2784 /** Open custom currency window. */
2785 static void ShowCustCurrency()
2787 CloseWindowById(WC_CUSTOM_CURRENCY, 0);
2788 new CustomCurrencyWindow(&_cust_currency_desc);