Changeset - r25027:9aa898fd565d
[Not reviewed]
master
0 1 0
sean - 3 years ago 2021-03-10 12:37:35
43609023+spnda@users.noreply.github.com
Codechange: Misleading function name for selecting refresh rate (#8836)

Co-authored-by: pnda <43609023+ThePNDA@users.noreply.github.com>
1 file changed with 2 insertions and 2 deletions:
0 comments (0 inline, 0 general)
src/settings_gui.cpp
Show inline comments
 
/*
 
 * This file is part of OpenTTD.
 
 * 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.
 
 * 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.
 
 * 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/>.
 
 */
 

	
 
/** @file settings_gui.cpp GUI for settings. */
 

	
 
#include "stdafx.h"
 
#include "currency.h"
 
#include "error.h"
 
#include "settings_gui.h"
 
#include "textbuf_gui.h"
 
#include "command_func.h"
 
#include "network/network.h"
 
#include "town.h"
 
#include "settings_internal.h"
 
#include "strings_func.h"
 
#include "window_func.h"
 
#include "string_func.h"
 
#include "widgets/dropdown_type.h"
 
#include "widgets/dropdown_func.h"
 
#include "highscore.h"
 
#include "base_media_base.h"
 
#include "company_base.h"
 
#include "company_func.h"
 
#include "viewport_func.h"
 
#include "core/geometry_func.hpp"
 
#include "ai/ai.hpp"
 
#include "blitter/factory.hpp"
 
#include "language.h"
 
#include "textfile_gui.h"
 
#include "stringfilter_type.h"
 
#include "querystring_gui.h"
 
#include "fontcache.h"
 
#include "zoom_func.h"
 
#include "video/video_driver.hpp"
 

	
 
#include <vector>
 
#include <iterator>
 

	
 
#include "safeguards.h"
 
#include "video/video_driver.hpp"
 

	
 

	
 
static const StringID _autosave_dropdown[] = {
 
	STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_OFF,
 
	STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_1_MONTH,
 
	STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_3_MONTHS,
 
	STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_6_MONTHS,
 
	STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_12_MONTHS,
 
	INVALID_STRING_ID,
 
};
 

	
 
static const StringID _gui_zoom_dropdown[] = {
 
	STR_GAME_OPTIONS_GUI_ZOOM_DROPDOWN_AUTO,
 
	STR_GAME_OPTIONS_GUI_ZOOM_DROPDOWN_NORMAL,
 
	STR_GAME_OPTIONS_GUI_ZOOM_DROPDOWN_2X_ZOOM,
 
	STR_GAME_OPTIONS_GUI_ZOOM_DROPDOWN_4X_ZOOM,
 
	INVALID_STRING_ID,
 
};
 

	
 
static const StringID _font_zoom_dropdown[] = {
 
	STR_GAME_OPTIONS_FONT_ZOOM_DROPDOWN_AUTO,
 
	STR_GAME_OPTIONS_FONT_ZOOM_DROPDOWN_NORMAL,
 
	STR_GAME_OPTIONS_FONT_ZOOM_DROPDOWN_2X_ZOOM,
 
	STR_GAME_OPTIONS_FONT_ZOOM_DROPDOWN_4X_ZOOM,
 
	INVALID_STRING_ID,
 
};
 

	
 
static Dimension _circle_size; ///< Dimension of the circle +/- icon. This is here as not all users are within the class of the settings window.
 

	
 
static const void *ResolveVariableAddress(const GameSettings *settings_ptr, const SettingDesc *sd);
 

	
 
/**
 
 * Get index of the current screen resolution.
 
 * @return Index of the current screen resolution if it is a known resolution, _resolutions.size() otherwise.
 
 */
 
static uint GetCurrentResolutionIndex()
 
{
 
	auto it = std::find(_resolutions.begin(), _resolutions.end(), Dimension(_screen.width, _screen.height));
 
	return std::distance(_resolutions.begin(), it);
 
}
 

	
 
static void ShowCustCurrency();
 

	
 
template <class T>
 
static DropDownList BuildSetDropDownList(int *selected_index, bool allow_selection)
 
{
 
	int n = T::GetNumSets();
 
	*selected_index = T::GetIndexOfUsedSet();
 

	
 
	DropDownList list;
 
	for (int i = 0; i < n; i++) {
 
		list.emplace_back(new DropDownListCharStringItem(T::GetSet(i)->name, i, !allow_selection && (*selected_index != i)));
 
	}
 

	
 
	return list;
 
}
 

	
 
DropDownList BuildMusicSetDropDownList(int *selected_index)
 
{
 
	return BuildSetDropDownList<BaseMusic>(selected_index, true);
 
}
 

	
 
/** Window for displaying the textfile of a BaseSet. */
 
template <class TBaseSet>
 
struct BaseSetTextfileWindow : public TextfileWindow {
 
	const TBaseSet* baseset; ///< View the textfile of this BaseSet.
 
	StringID content_type;   ///< STR_CONTENT_TYPE_xxx for title.
 

	
 
	BaseSetTextfileWindow(TextfileType file_type, const TBaseSet* baseset, StringID content_type) : TextfileWindow(file_type), baseset(baseset), content_type(content_type)
 
	{
 
		const char *textfile = this->baseset->GetTextfile(file_type);
 
		this->LoadTextfile(textfile, BASESET_DIR);
 
	}
 

	
 
	void SetStringParameters(int widget) const override
 
	{
 
		if (widget == WID_TF_CAPTION) {
 
			SetDParam(0, content_type);
 
			SetDParamStr(1, this->baseset->name.c_str());
 
		}
 
	}
 
};
 

	
 
/**
 
 * Open the BaseSet version of the textfile window.
 
 * @param file_type The type of textfile to display.
 
 * @param baseset The BaseSet to use.
 
 * @param content_type STR_CONTENT_TYPE_xxx for title.
 
 */
 
template <class TBaseSet>
 
void ShowBaseSetTextfileWindow(TextfileType file_type, const TBaseSet* baseset, StringID content_type)
 
{
 
	DeleteWindowById(WC_TEXTFILE, file_type);
 
	new BaseSetTextfileWindow<TBaseSet>(file_type, baseset, content_type);
 
}
 

	
 
std::set<int> _refresh_rates = { 30, 60, 75, 90, 100, 120, 144, 240 };
 

	
 
/**
 
 * Add the refresh rate from the config and the refresh rates from all the monitors to
 
 * our list of refresh rates shown in the GUI.
 
 */
 
static void AddRefreshRatesAndSelect()
 
static void AddCustomRefreshRates()
 
{
 
	/* Add the refresh rate as selected in the config. */
 
	_refresh_rates.insert(_settings_client.gui.refresh_rate);
 

	
 
	/* Add all the refresh rates of all monitors connected to the machine.  */
 
	std::vector<int> monitorRates = VideoDriver::GetInstance()->GetListOfMonitorRefreshRates();
 
	std::copy(monitorRates.begin(), monitorRates.end(), std::inserter(_refresh_rates, _refresh_rates.end()));
 
}
 

	
 
struct GameOptionsWindow : Window {
 
	GameSettings *opt;
 
	bool reload;
 

	
 
	GameOptionsWindow(WindowDesc *desc) : Window(desc)
 
	{
 
		this->opt = &GetGameSettings();
 
		this->reload = false;
 

	
 
		AddRefreshRatesAndSelect();
 
		AddCustomRefreshRates();
 

	
 
		this->InitNested(WN_GAME_OPTIONS_GAME_OPTIONS);
 
		this->OnInvalidateData(0);
 
	}
 

	
 
	~GameOptionsWindow()
 
	{
 
		DeleteWindowById(WC_CUSTOM_CURRENCY, 0);
 
		DeleteWindowByClass(WC_TEXTFILE);
 
		if (this->reload) _switch_mode = SM_MENU;
 
	}
 

	
 
	/**
 
	 * Build the dropdown list for a specific widget.
 
	 * @param widget         Widget to build list for
 
	 * @param selected_index Currently selected item
 
	 * @return the built dropdown list, or nullptr if the widget has no dropdown menu.
 
	 */
 
	DropDownList BuildDropDownList(int widget, int *selected_index) const
 
	{
 
		DropDownList list;
 
		switch (widget) {
 
			case WID_GO_CURRENCY_DROPDOWN: { // Setup currencies dropdown
 
				*selected_index = this->opt->locale.currency;
 
				StringID *items = BuildCurrencyDropdown();
 
				uint64 disabled = _game_mode == GM_MENU ? 0LL : ~GetMaskOfAllowedCurrencies();
 

	
 
				/* Add non-custom currencies; sorted naturally */
 
				for (uint i = 0; i < CURRENCY_END; items++, i++) {
 
					if (i == CURRENCY_CUSTOM) continue;
 
					list.emplace_back(new DropDownListStringItem(*items, i, HasBit(disabled, i)));
 
				}
 
				std::sort(list.begin(), list.end(), DropDownListStringItem::NatSortFunc);
 

	
 
				/* Append custom currency at the end */
 
				list.emplace_back(new DropDownListItem(-1, false)); // separator line
 
				list.emplace_back(new DropDownListStringItem(STR_GAME_OPTIONS_CURRENCY_CUSTOM, CURRENCY_CUSTOM, HasBit(disabled, CURRENCY_CUSTOM)));
 
				break;
 
			}
 

	
 
			case WID_GO_AUTOSAVE_DROPDOWN: { // Setup autosave dropdown
 
				*selected_index = _settings_client.gui.autosave;
 
				const StringID *items = _autosave_dropdown;
 
				for (uint i = 0; *items != INVALID_STRING_ID; items++, i++) {
 
					list.emplace_back(new DropDownListStringItem(*items, i, false));
 
				}
 
				break;
 
			}
 

	
 
			case WID_GO_LANG_DROPDOWN: { // Setup interface language dropdown
 
				for (uint i = 0; i < _languages.size(); i++) {
 
					auto item = new DropDownListParamStringItem(STR_JUST_RAW_STRING, i, false);
 
					if (&_languages[i] == _current_language) {
 
						*selected_index = i;
 
						item->SetParamStr(0, _languages[i].own_name);
 
					} else {
 
						/* Especially with sprite-fonts, not all localized
 
						 * names can be rendered. So instead, we use the
 
						 * international names for anything but the current
 
						 * selected language. This avoids showing a few ????
 
						 * entries in the dropdown list. */
 
						item->SetParamStr(0, _languages[i].name);
 
					}
 
					list.emplace_back(item);
 
				}
 
				std::sort(list.begin(), list.end(), DropDownListStringItem::NatSortFunc);
 
				break;
 
			}
 

	
 
			case WID_GO_RESOLUTION_DROPDOWN: // Setup resolution dropdown
 
				if (_resolutions.empty()) break;
 

	
 
				*selected_index = GetCurrentResolutionIndex();
 
				for (uint i = 0; i < _resolutions.size(); i++) {
 
					auto item = new DropDownListParamStringItem(STR_GAME_OPTIONS_RESOLUTION_ITEM, i, false);
 
					item->SetParam(0, _resolutions[i].width);
 
					item->SetParam(1, _resolutions[i].height);
 
					list.emplace_back(item);
 
				}
 
				break;
 

	
 
			case WID_GO_REFRESH_RATE_DROPDOWN: // Setup refresh rate dropdown
 
				for (auto it = _refresh_rates.begin(); it != _refresh_rates.end(); it++) {
 
					auto i = std::distance(_refresh_rates.begin(), it);
 
					if (*it == _settings_client.gui.refresh_rate) *selected_index = i;
 
					auto item = new DropDownListParamStringItem(STR_GAME_OPTIONS_REFRESH_RATE_ITEM, i, false);
 
					item->SetParam(0, *it);
 
					list.emplace_back(item);
 
				}
 
				break;
 

	
 
			case WID_GO_GUI_ZOOM_DROPDOWN: {
 
				*selected_index = _gui_zoom_cfg != ZOOM_LVL_CFG_AUTO ? ZOOM_LVL_OUT_4X - _gui_zoom + 1 : 0;
 
				const StringID *items = _gui_zoom_dropdown;
 
				for (int i = 0; *items != INVALID_STRING_ID; items++, i++) {
 
					list.emplace_back(new DropDownListStringItem(*items, i, i != 0 && _settings_client.gui.zoom_min > ZOOM_LVL_OUT_4X - i + 1));
 
				}
 
				break;
 
			}
 

	
 
			case WID_GO_FONT_ZOOM_DROPDOWN: {
 
				*selected_index = _font_zoom_cfg != ZOOM_LVL_CFG_AUTO ? ZOOM_LVL_OUT_4X - _font_zoom + 1 : 0;
 
				const StringID *items = _font_zoom_dropdown;
 
				for (int i = 0; *items != INVALID_STRING_ID; items++, i++) {
 
					list.emplace_back(new DropDownListStringItem(*items, i, false));
 
				}
 
				break;
 
			}
 

	
 
			case WID_GO_BASE_GRF_DROPDOWN:
 
				list = BuildSetDropDownList<BaseGraphics>(selected_index, (_game_mode == GM_MENU));
 
				break;
 

	
 
			case WID_GO_BASE_SFX_DROPDOWN:
 
				list = BuildSetDropDownList<BaseSounds>(selected_index, (_game_mode == GM_MENU));
 
				break;
 

	
 
			case WID_GO_BASE_MUSIC_DROPDOWN:
 
				list = BuildMusicSetDropDownList(selected_index);
 
				break;
 
		}
 

	
 
		return list;
 
	}
 

	
 
	void SetStringParameters(int widget) const override
 
	{
 
		switch (widget) {
 
			case WID_GO_CURRENCY_DROPDOWN:     SetDParam(0, _currency_specs[this->opt->locale.currency].name); break;
 
			case WID_GO_AUTOSAVE_DROPDOWN:     SetDParam(0, _autosave_dropdown[_settings_client.gui.autosave]); break;
 
			case WID_GO_LANG_DROPDOWN:         SetDParamStr(0, _current_language->own_name); break;
 
			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;
 
			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;
 
			case WID_GO_BASE_GRF_DROPDOWN:     SetDParamStr(0, BaseGraphics::GetUsedSet()->name.c_str()); break;
 
			case WID_GO_BASE_GRF_STATUS:       SetDParam(0, BaseGraphics::GetUsedSet()->GetNumInvalid()); break;
 
			case WID_GO_BASE_SFX_DROPDOWN:     SetDParamStr(0, BaseSounds::GetUsedSet()->name.c_str()); break;
 
			case WID_GO_BASE_MUSIC_DROPDOWN:   SetDParamStr(0, BaseMusic::GetUsedSet()->name.c_str()); break;
 
			case WID_GO_BASE_MUSIC_STATUS:     SetDParam(0, BaseMusic::GetUsedSet()->GetNumInvalid()); break;
 
			case WID_GO_REFRESH_RATE_DROPDOWN: SetDParam(0, _settings_client.gui.refresh_rate); break;
 
			case WID_GO_RESOLUTION_DROPDOWN: {
 
				auto current_resolution = GetCurrentResolutionIndex();
 

	
 
				if (current_resolution == _resolutions.size()) {
 
					SetDParam(0, STR_GAME_OPTIONS_RESOLUTION_OTHER);
 
				} else {
 
					SetDParam(0, STR_GAME_OPTIONS_RESOLUTION_ITEM);
 
					SetDParam(1, _resolutions[current_resolution].width);
 
					SetDParam(2, _resolutions[current_resolution].height);
 
				}
 
				break;
 
			}
 
		}
 
	}
 

	
 
	void DrawWidget(const Rect &r, int widget) const override
 
	{
 
		switch (widget) {
 
			case WID_GO_BASE_GRF_DESCRIPTION:
 
				SetDParamStr(0, BaseGraphics::GetUsedSet()->GetDescription(GetCurrentLanguageIsoCode()));
 
				DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, STR_BLACK_RAW_STRING);
 
				break;
 

	
 
			case WID_GO_BASE_SFX_DESCRIPTION:
 
				SetDParamStr(0, BaseSounds::GetUsedSet()->GetDescription(GetCurrentLanguageIsoCode()));
 
				DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, STR_BLACK_RAW_STRING);
 
				break;
 

	
 
			case WID_GO_BASE_MUSIC_DESCRIPTION:
 
				SetDParamStr(0, BaseMusic::GetUsedSet()->GetDescription(GetCurrentLanguageIsoCode()));
 
				DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, STR_BLACK_RAW_STRING);
 
				break;
 
		}
 
	}
 

	
 
	void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
 
	{
 
		switch (widget) {
 
			case WID_GO_BASE_GRF_DESCRIPTION:
 
				/* Find the biggest description for the default size. */
 
				for (int i = 0; i < BaseGraphics::GetNumSets(); i++) {
 
					SetDParamStr(0, BaseGraphics::GetSet(i)->GetDescription(GetCurrentLanguageIsoCode()));
 
					size->height = std::max(size->height, (uint)GetStringHeight(STR_BLACK_RAW_STRING, size->width));
 
				}
 
				break;
 

	
 
			case WID_GO_BASE_GRF_STATUS:
 
				/* Find the biggest description for the default size. */
 
				for (int i = 0; i < BaseGraphics::GetNumSets(); i++) {
 
					uint invalid_files = BaseGraphics::GetSet(i)->GetNumInvalid();
 
					if (invalid_files == 0) continue;
 

	
 
					SetDParam(0, invalid_files);
 
					*size = maxdim(*size, GetStringBoundingBox(STR_GAME_OPTIONS_BASE_GRF_STATUS));
 
				}
 
				break;
 

	
 
			case WID_GO_BASE_SFX_DESCRIPTION:
 
				/* Find the biggest description for the default size. */
 
				for (int i = 0; i < BaseSounds::GetNumSets(); i++) {
 
					SetDParamStr(0, BaseSounds::GetSet(i)->GetDescription(GetCurrentLanguageIsoCode()));
 
					size->height = std::max(size->height, (uint)GetStringHeight(STR_BLACK_RAW_STRING, size->width));
 
				}
 
				break;
 

	
 
			case WID_GO_BASE_MUSIC_DESCRIPTION:
 
				/* Find the biggest description for the default size. */
 
				for (int i = 0; i < BaseMusic::GetNumSets(); i++) {
 
					SetDParamStr(0, BaseMusic::GetSet(i)->GetDescription(GetCurrentLanguageIsoCode()));
 
					size->height = std::max(size->height, (uint)GetStringHeight(STR_BLACK_RAW_STRING, size->width));
 
				}
 
				break;
 

	
 
			case WID_GO_BASE_MUSIC_STATUS:
 
				/* Find the biggest description for the default size. */
 
				for (int i = 0; i < BaseMusic::GetNumSets(); i++) {
 
					uint invalid_files = BaseMusic::GetSet(i)->GetNumInvalid();
 
					if (invalid_files == 0) continue;
 

	
 
					SetDParam(0, invalid_files);
 
					*size = maxdim(*size, GetStringBoundingBox(STR_GAME_OPTIONS_BASE_MUSIC_STATUS));
 
				}
 
				break;
 

	
 
			default: {
 
				int selected;
 
				DropDownList list = this->BuildDropDownList(widget, &selected);
 
				if (!list.empty()) {
 
					/* Find the biggest item for the default size. */
 
					for (const auto &ddli : list) {
 
						Dimension string_dim;
 
						int width = ddli->Width();
 
						string_dim.width = width + padding.width;
 
						string_dim.height = ddli->Height(width) + padding.height;
 
						*size = maxdim(*size, string_dim);
 
					}
 
				}
 
			}
 
		}
 
	}
 

	
 
	void OnClick(Point pt, int widget, int click_count) override
 
	{
 
		if (widget >= WID_GO_BASE_GRF_TEXTFILE && widget < WID_GO_BASE_GRF_TEXTFILE + TFT_END) {
 
			if (BaseGraphics::GetUsedSet() == nullptr) return;
 

	
 
			ShowBaseSetTextfileWindow((TextfileType)(widget - WID_GO_BASE_GRF_TEXTFILE), BaseGraphics::GetUsedSet(), STR_CONTENT_TYPE_BASE_GRAPHICS);
 
			return;
 
		}
 
		if (widget >= WID_GO_BASE_SFX_TEXTFILE && widget < WID_GO_BASE_SFX_TEXTFILE + TFT_END) {
 
			if (BaseSounds::GetUsedSet() == nullptr) return;
 

	
 
			ShowBaseSetTextfileWindow((TextfileType)(widget - WID_GO_BASE_SFX_TEXTFILE), BaseSounds::GetUsedSet(), STR_CONTENT_TYPE_BASE_SOUNDS);
 
			return;
 
		}
 
		if (widget >= WID_GO_BASE_MUSIC_TEXTFILE && widget < WID_GO_BASE_MUSIC_TEXTFILE + TFT_END) {
 
			if (BaseMusic::GetUsedSet() == nullptr) return;
 

	
 
			ShowBaseSetTextfileWindow((TextfileType)(widget - WID_GO_BASE_MUSIC_TEXTFILE), BaseMusic::GetUsedSet(), STR_CONTENT_TYPE_BASE_MUSIC);
 
			return;
 
		}
 
		switch (widget) {
 
			case WID_GO_FULLSCREEN_BUTTON: // Click fullscreen on/off
 
				/* try to toggle full-screen on/off */
 
				if (!ToggleFullScreen(!_fullscreen)) {
 
					ShowErrorMessage(STR_ERROR_FULLSCREEN_FAILED, INVALID_STRING_ID, WL_ERROR);
 
				}
 
				this->SetWidgetLoweredState(WID_GO_FULLSCREEN_BUTTON, _fullscreen);
 
				this->SetDirty();
 
				break;
 

	
 
			case WID_GO_VIDEO_ACCEL_BUTTON:
 
				_video_hw_accel = !_video_hw_accel;
 
				ShowErrorMessage(STR_GAME_OPTIONS_VIDEO_ACCELERATION_RESTART, INVALID_STRING_ID, WL_INFO);
 
				this->SetWidgetLoweredState(WID_GO_VIDEO_ACCEL_BUTTON, _video_hw_accel);
 
				this->SetDirty();
 
				break;
 

	
 
			default: {
 
				int selected;
 
				DropDownList list = this->BuildDropDownList(widget, &selected);
 
				if (!list.empty()) {
 
					ShowDropDownList(this, std::move(list), selected, widget);
 
				} else {
 
					if (widget == WID_GO_RESOLUTION_DROPDOWN) ShowErrorMessage(STR_ERROR_RESOLUTION_LIST_FAILED, INVALID_STRING_ID, WL_ERROR);
 
				}
 
				break;
 
			}
 
		}
 
	}
 

	
 
	/**
 
	 * Set the base media set.
 
	 * @param index the index of the media set
 
	 * @tparam T class of media set
 
	 */
 
	template <class T>
 
	void SetMediaSet(int index)
 
	{
 
		if (_game_mode == GM_MENU) {
 
			auto name = T::GetSet(index)->name;
 

	
 
			T::ini_set = name;
 

	
 
			T::SetSet(name);
 
			this->reload = true;
 
			this->InvalidateData();
 
		}
 
	}
 

	
 
	void OnDropdownSelect(int widget, int index) override
 
	{
 
		switch (widget) {
 
			case WID_GO_CURRENCY_DROPDOWN: // Currency
 
				if (index == CURRENCY_CUSTOM) ShowCustCurrency();
 
				this->opt->locale.currency = index;
 
				ReInitAllWindows();
 
				break;
 

	
 
			case WID_GO_AUTOSAVE_DROPDOWN: // Autosave options
 
				_settings_client.gui.autosave = index;
 
				this->SetDirty();
 
				break;
 

	
 
			case WID_GO_LANG_DROPDOWN: // Change interface language
 
				ReadLanguagePack(&_languages[index]);
 
				DeleteWindowByClass(WC_QUERY_STRING);
 
				CheckForMissingGlyphs();
 
				ClearAllCachedNames();
 
				UpdateAllVirtCoords();
 
				CheckBlitter();
 
				ReInitAllWindows();
 
				break;
 

	
 
			case WID_GO_RESOLUTION_DROPDOWN: // Change resolution
 
				if ((uint)index < _resolutions.size() && ChangeResInGame(_resolutions[index].width, _resolutions[index].height)) {
 
					this->SetDirty();
 
				}
 
				break;
 

	
 
			case WID_GO_REFRESH_RATE_DROPDOWN: {
 
				_settings_client.gui.refresh_rate = *std::next(_refresh_rates.begin(), index);
 
				if (_settings_client.gui.refresh_rate > 60) {
 
					/* Show warning to the user that this refresh rate might not be suitable on
 
					 * larger maps with many NewGRFs and vehicles. */
 
					ShowErrorMessage(STR_GAME_OPTIONS_REFRESH_RATE_WARNING, INVALID_STRING_ID, WL_INFO);
 
				}
 
				break;
 
			}
 

	
 
			case WID_GO_GUI_ZOOM_DROPDOWN: {
 
				int8 new_zoom = index > 0 ? ZOOM_LVL_OUT_4X - index + 1 : ZOOM_LVL_CFG_AUTO;
 
				if (new_zoom != _gui_zoom_cfg) {
 
					GfxClearSpriteCache();
 
					_gui_zoom_cfg = new_zoom;
 
					UpdateGUIZoom();
 
					UpdateCursorSize();
 
					UpdateAllVirtCoords();
 
					FixTitleGameZoom();
 
					ReInitAllWindows();
 
				}
 
				break;
 
			}
 

	
 
			case WID_GO_FONT_ZOOM_DROPDOWN: {
 
				int8 new_zoom = index > 0 ? ZOOM_LVL_OUT_4X - index + 1 : ZOOM_LVL_CFG_AUTO;
 
				if (new_zoom != _font_zoom_cfg) {
 
					GfxClearSpriteCache();
 
					_font_zoom_cfg = new_zoom;
 
					UpdateGUIZoom();
 
					ClearFontCache();
 
					LoadStringWidthTable();
 
					UpdateAllVirtCoords();
 
				}
 
				break;
 
			}
 

	
 
			case WID_GO_BASE_GRF_DROPDOWN:
 
				this->SetMediaSet<BaseGraphics>(index);
 
				break;
 

	
 
			case WID_GO_BASE_SFX_DROPDOWN:
 
				this->SetMediaSet<BaseSounds>(index);
 
				break;
 

	
 
			case WID_GO_BASE_MUSIC_DROPDOWN:
 
				ChangeMusicSet(index);
 
				break;
 
		}
 
	}
 

	
 
	/**
 
	 * Some data on this window has become invalid.
 
	 * @param data Information about the changed data. @see GameOptionsInvalidationData
 
	 * @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.
 
	 */
 
	void OnInvalidateData(int data = 0, bool gui_scope = true) override
 
	{
 
		if (!gui_scope) return;
 
		this->SetWidgetLoweredState(WID_GO_FULLSCREEN_BUTTON, _fullscreen);
 
		this->SetWidgetLoweredState(WID_GO_VIDEO_ACCEL_BUTTON, _video_hw_accel);
 

	
 
		bool missing_files = BaseGraphics::GetUsedSet()->GetNumMissing() == 0;
 
		this->GetWidget<NWidgetCore>(WID_GO_BASE_GRF_STATUS)->SetDataTip(missing_files ? STR_EMPTY : STR_GAME_OPTIONS_BASE_GRF_STATUS, STR_NULL);
 

	
 
		for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
 
			this->SetWidgetDisabledState(WID_GO_BASE_GRF_TEXTFILE + tft, BaseGraphics::GetUsedSet() == nullptr || BaseGraphics::GetUsedSet()->GetTextfile(tft) == nullptr);
 
			this->SetWidgetDisabledState(WID_GO_BASE_SFX_TEXTFILE + tft, BaseSounds::GetUsedSet() == nullptr || BaseSounds::GetUsedSet()->GetTextfile(tft) == nullptr);
 
			this->SetWidgetDisabledState(WID_GO_BASE_MUSIC_TEXTFILE + tft, BaseMusic::GetUsedSet() == nullptr || BaseMusic::GetUsedSet()->GetTextfile(tft) == nullptr);
 
		}
 

	
 
		missing_files = BaseMusic::GetUsedSet()->GetNumInvalid() == 0;
 
		this->GetWidget<NWidgetCore>(WID_GO_BASE_MUSIC_STATUS)->SetDataTip(missing_files ? STR_EMPTY : STR_GAME_OPTIONS_BASE_MUSIC_STATUS, STR_NULL);
 
	}
 
};
 

	
 
static const NWidgetPart _nested_game_options_widgets[] = {
 
	NWidget(NWID_HORIZONTAL),
 
		NWidget(WWT_CLOSEBOX, COLOUR_GREY),
 
		NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
 
	EndContainer(),
 
	NWidget(WWT_PANEL, COLOUR_GREY, WID_GO_BACKGROUND), SetPIP(6, 6, 10),
 
		NWidget(NWID_HORIZONTAL), SetPIP(10, 10, 10),
 
			NWidget(NWID_VERTICAL), SetPIP(0, 6, 0),
 
				NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_AUTOSAVE_FRAME, STR_NULL),
 
					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),
 
				EndContainer(),
 
				NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_GUI_ZOOM_FRAME, STR_NULL),
 
					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),
 
				EndContainer(),
 
				NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_CURRENCY_UNITS_FRAME, STR_NULL),
 
					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),
 
				EndContainer(),
 
			EndContainer(),
 

	
 
			NWidget(NWID_VERTICAL), SetPIP(0, 6, 0),
 
				NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_LANGUAGE, STR_NULL),
 
					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),
 
				EndContainer(),
 
				NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_FONT_ZOOM, STR_NULL),
 
					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),
 
				EndContainer(),
 
			EndContainer(),
 
		EndContainer(),
 

	
 
		NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_GRAPHICS, STR_NULL), SetPadding(0, 10, 0, 10),
 
			NWidget(NWID_HORIZONTAL),
 
				NWidget(NWID_VERTICAL),
 
					NWidget(WWT_TEXT, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 0), SetDataTip(STR_GAME_OPTIONS_RESOLUTION, STR_NULL), SetPadding(0, 0, 2, 0),
 
					NWidget(WWT_TEXT, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 0), SetDataTip(STR_GAME_OPTIONS_REFRESH_RATE, STR_NULL), SetPadding(0, 0, 2, 0),
 
					NWidget(WWT_TEXT, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 0), SetDataTip(STR_GAME_OPTIONS_FULLSCREEN, STR_NULL), SetPadding(0, 0, 2, 0),
 
					NWidget(WWT_TEXT, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 0), SetDataTip(STR_GAME_OPTIONS_VIDEO_ACCELERATION, STR_NULL),
 
				EndContainer(),
 
				NWidget(NWID_VERTICAL),
 
					NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_RESOLUTION_DROPDOWN), SetMinimalSize(100, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_RESOLUTION_TOOLTIP), SetFill(1, 0), SetPadding(0, 0, 2, 0),
 
					NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_REFRESH_RATE_DROPDOWN), SetMinimalSize(100, 12), SetDataTip(STR_GAME_OPTIONS_REFRESH_RATE_ITEM, STR_GAME_OPTIONS_REFRESH_RATE_TOOLTIP), SetFill(1, 0), SetPadding(0, 0, 2, 0),
 
					NWidget(NWID_HORIZONTAL), SetPadding(0, 0, 2, 0),
 
						NWidget(NWID_SPACER), SetMinimalSize(1, 0), SetFill(1, 0),
 
						NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_GO_FULLSCREEN_BUTTON), SetMinimalSize(21, 9), SetDataTip(STR_EMPTY, STR_GAME_OPTIONS_FULLSCREEN_TOOLTIP),
 
					EndContainer(),
 
					NWidget(NWID_HORIZONTAL),
 
						NWidget(NWID_SPACER), SetMinimalSize(1, 0), SetFill(1, 0),
 
						NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_GO_VIDEO_ACCEL_BUTTON), SetMinimalSize(21, 9), SetDataTip(STR_EMPTY, STR_GAME_OPTIONS_VIDEO_ACCELERATION_TOOLTIP),
 
					EndContainer(),
 
				EndContainer(),
 
			EndContainer(),
 
		EndContainer(),
 

	
 
		NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_BASE_GRF, STR_NULL), SetPadding(0, 10, 0, 10),
 
			NWidget(NWID_HORIZONTAL), SetPIP(0, 30, 0),
 
				NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_BASE_GRF_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_RAW_STRING, STR_GAME_OPTIONS_BASE_GRF_TOOLTIP),
 
				NWidget(WWT_TEXT, COLOUR_GREY, WID_GO_BASE_GRF_STATUS), SetMinimalSize(150, 12), SetDataTip(STR_EMPTY, STR_NULL), SetFill(1, 0),
 
			EndContainer(),
 
			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),
 
			NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
 
				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),
 
				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),
 
				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),
 
			EndContainer(),
 
		EndContainer(),
 

	
 
		NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_BASE_SFX, STR_NULL), SetPadding(0, 10, 0, 10),
 
			NWidget(NWID_HORIZONTAL), SetPIP(0, 30, 0),
 
				NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_BASE_SFX_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_RAW_STRING, STR_GAME_OPTIONS_BASE_SFX_TOOLTIP),
 
				NWidget(NWID_SPACER), SetFill(1, 0),
 
			EndContainer(),
 
			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),
 
			NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
 
				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),
 
				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),
 
				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),
 
			EndContainer(),
 
		EndContainer(),
 

	
 
		NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_BASE_MUSIC, STR_NULL), SetPadding(0, 10, 0, 10),
 
			NWidget(NWID_HORIZONTAL), SetPIP(0, 30, 0),
 
				NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_BASE_MUSIC_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_RAW_STRING, STR_GAME_OPTIONS_BASE_MUSIC_TOOLTIP),
 
				NWidget(WWT_TEXT, COLOUR_GREY, WID_GO_BASE_MUSIC_STATUS), SetMinimalSize(150, 12), SetDataTip(STR_EMPTY, STR_NULL), SetFill(1, 0),
 
			EndContainer(),
 
			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),
 
			NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
 
				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),
 
				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),
 
				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),
 
			EndContainer(),
 
		EndContainer(),
 
	EndContainer(),
 
};
 

	
 
static WindowDesc _game_options_desc(
 
	WDP_CENTER, "settings_game", 0, 0,
 
	WC_GAME_OPTIONS, WC_NONE,
 
	0,
 
	_nested_game_options_widgets, lengthof(_nested_game_options_widgets)
 
);
 

	
 
/** Open the game options window. */
 
void ShowGameOptions()
 
{
 
	DeleteWindowByClass(WC_GAME_OPTIONS);
 
	new GameOptionsWindow(&_game_options_desc);
 
}
 

	
 
static int SETTING_HEIGHT = 11;    ///< Height of a single setting in the tree view in pixels
 
static const int LEVEL_WIDTH = 15; ///< Indenting width of a sub-page in pixels
 

	
 
/**
 
 * Flags for #SettingEntry
 
 * @note The #SEF_BUTTONS_MASK matches expectations of the formal parameter 'state' of #DrawArrowButtons
 
 */
 
enum SettingEntryFlags {
 
	SEF_LEFT_DEPRESSED  = 0x01, ///< Of a numeric setting entry, the left button is depressed
 
	SEF_RIGHT_DEPRESSED = 0x02, ///< Of a numeric setting entry, the right button is depressed
 
	SEF_BUTTONS_MASK = (SEF_LEFT_DEPRESSED | SEF_RIGHT_DEPRESSED), ///< Bit-mask for button flags
 

	
 
	SEF_LAST_FIELD = 0x04, ///< This entry is the last one in a (sub-)page
 
	SEF_FILTERED   = 0x08, ///< Entry is hidden by the string filter
 
};
 

	
 
/** How the list of advanced settings is filtered. */
 
enum RestrictionMode {
 
	RM_BASIC,                            ///< Display settings associated to the "basic" list.
 
	RM_ADVANCED,                         ///< Display settings associated to the "advanced" list.
 
	RM_ALL,                              ///< List all settings regardless of the default/newgame/... values.
 
	RM_CHANGED_AGAINST_DEFAULT,          ///< Show only settings which are different compared to default values.
 
	RM_CHANGED_AGAINST_NEW,              ///< Show only settings which are different compared to the user's new game setting values.
 
	RM_END,                              ///< End for iteration.
 
};
 
DECLARE_POSTFIX_INCREMENT(RestrictionMode)
 

	
 
/** Filter for settings list. */
 
struct SettingFilter {
 
	StringFilter string;     ///< Filter string.
 
	RestrictionMode min_cat; ///< Minimum category needed to display all filtered strings (#RM_BASIC, #RM_ADVANCED, or #RM_ALL).
 
	bool type_hides;         ///< Whether the type hides filtered strings.
 
	RestrictionMode mode;    ///< Filter based on category.
 
	SettingType type;        ///< Filter based on type.
 
};
 

	
 
/** Data structure describing a single setting in a tab */
 
struct BaseSettingEntry {
 
	byte flags; ///< Flags of the setting entry. @see SettingEntryFlags
 
	byte level; ///< Nesting level of this setting entry
 

	
 
	BaseSettingEntry() : flags(0), level(0) {}
 
	virtual ~BaseSettingEntry() {}
 

	
 
	virtual void Init(byte level = 0);
 
	virtual void FoldAll() {}
 
	virtual void UnFoldAll() {}
 

	
 
	/**
 
	 * Set whether this is the last visible entry of the parent node.
 
	 * @param last_field Value to set
 
	 */
 
	void SetLastField(bool last_field) { if (last_field) SETBITS(this->flags, SEF_LAST_FIELD); else CLRBITS(this->flags, SEF_LAST_FIELD); }
 

	
 
	virtual uint Length() const = 0;
 
	virtual void GetFoldingState(bool &all_folded, bool &all_unfolded) const {}
 
	virtual bool IsVisible(const BaseSettingEntry *item) const;
 
	virtual BaseSettingEntry *FindEntry(uint row, uint *cur_row);
 
	virtual uint GetMaxHelpHeight(int maxw) { return 0; }
 

	
 
	/**
 
	 * Check whether an entry is hidden due to filters
 
	 * @return true if hidden.
 
	 */
 
	bool IsFiltered() const { return (this->flags & SEF_FILTERED) != 0; }
 

	
 
	virtual bool UpdateFilterState(SettingFilter &filter, bool force_visible) = 0;
 

	
 
	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;
 

	
 
protected:
 
	virtual void DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const = 0;
 
};
 

	
 
/** Standard setting */
 
struct SettingEntry : BaseSettingEntry {
 
	const char *name;           ///< Name of the setting
 
	const SettingDesc *setting; ///< Setting description of the setting
 
	uint index;                 ///< Index of the setting in the settings table
 

	
 
	SettingEntry(const char *name);
 

	
 
	virtual void Init(byte level = 0);
 
	virtual uint Length() const;
 
	virtual uint GetMaxHelpHeight(int maxw);
 
	virtual bool UpdateFilterState(SettingFilter &filter, bool force_visible);
 

	
 
	void SetButtons(byte new_val);
 

	
 
	/**
 
	 * Get the help text of a single setting.
 
	 * @return The requested help text.
 
	 */
 
	inline StringID GetHelpText() const
 
	{
 
		return this->setting->desc.str_help;
 
	}
 

	
 
	void SetValueDParams(uint first_param, int32 value) const;
 

	
 
protected:
 
	virtual void DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const;
 

	
 
private:
 
	bool IsVisibleByRestrictionMode(RestrictionMode mode) const;
 
};
 

	
 
/** Containers for BaseSettingEntry */
 
struct SettingsContainer {
 
	typedef std::vector<BaseSettingEntry*> EntryVector;
 
	EntryVector entries; ///< Settings on this page
 

	
 
	template<typename T>
 
	T *Add(T *item)
 
	{
 
		this->entries.push_back(item);
 
		return item;
 
	}
 

	
 
	void Init(byte level = 0);
 
	void FoldAll();
 
	void UnFoldAll();
 

	
 
	uint Length() const;
 
	void GetFoldingState(bool &all_folded, bool &all_unfolded) const;
 
	bool IsVisible(const BaseSettingEntry *item) const;
 
	BaseSettingEntry *FindEntry(uint row, uint *cur_row);
 
	uint GetMaxHelpHeight(int maxw);
 

	
 
	bool UpdateFilterState(SettingFilter &filter, bool force_visible);
 

	
 
	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;
 
};
 

	
 
/** Data structure describing one page of settings in the settings window. */
 
struct SettingsPage : BaseSettingEntry, SettingsContainer {
 
	StringID title;     ///< Title of the sub-page
 
	bool folded;        ///< Sub-page is folded (not visible except for its title)
 

	
 
	SettingsPage(StringID title);
 

	
 
	virtual void Init(byte level = 0);
 
	virtual void FoldAll();
 
	virtual void UnFoldAll();
 

	
 
	virtual uint Length() const;
 
	virtual void GetFoldingState(bool &all_folded, bool &all_unfolded) const;
 
	virtual bool IsVisible(const BaseSettingEntry *item) const;
 
	virtual BaseSettingEntry *FindEntry(uint row, uint *cur_row);
 
	virtual uint GetMaxHelpHeight(int maxw) { return SettingsContainer::GetMaxHelpHeight(maxw); }
 

	
 
	virtual bool UpdateFilterState(SettingFilter &filter, bool force_visible);
 

	
 
	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;
 

	
 
protected:
 
	virtual void DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const;
 
};
 

	
 
/* == BaseSettingEntry methods == */
 

	
 
/**
 
 * Initialization of a setting entry
 
 * @param level      Page nesting level of this entry
 
 */
 
void BaseSettingEntry::Init(byte level)
 
{
 
	this->level = level;
 
}
 

	
 
/**
 
 * Check whether an entry is visible and not folded or filtered away.
 
 * Note: This does not consider the scrolling range; it might still require scrolling to make the setting really visible.
 
 * @param item Entry to search for.
 
 * @return true if entry is visible.
 
 */
 
bool BaseSettingEntry::IsVisible(const BaseSettingEntry *item) const
 
{
 
	if (this->IsFiltered()) return false;
 
	if (this == item) return true;
 
	return false;
 
}
 

	
 
/**
 
 * Find setting entry at row \a row_num
 
 * @param row_num Index of entry to return
 
 * @param cur_row Current row number
 
 * @return The requested setting entry or \c nullptr if it not found (folded or filtered)
 
 */
 
BaseSettingEntry *BaseSettingEntry::FindEntry(uint row_num, uint *cur_row)
 
{
 
	if (this->IsFiltered()) return nullptr;
 
	if (row_num == *cur_row) return this;
 
	(*cur_row)++;
 
	return nullptr;
 
}
 

	
 
/**
 
 * Draw a row in the settings panel.
 
 *
 
 * The scrollbar uses rows of the page, while the page data structure is a tree of #SettingsPage and #SettingEntry objects.
 
 * 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.
 
 * Then it enables drawing rows while traversing until \a max_row is reached, at which point drawing is terminated.
 
 *
 
 * The \a parent_last parameter ensures that the vertical lines at the left are
 
 * only drawn when another entry follows, that it prevents output like
 
 * \verbatim
 
 *  |-- setting
 
 *  |-- (-) - Title
 
 *  |    |-- setting
 
 *  |    |-- setting
 
 * \endverbatim
 
 * The left-most vertical line is not wanted. It is prevented by setting the
 
 * appropriate bit in the \a parent_last parameter.
 
 *
 
 * @param settings_ptr Pointer to current values of all settings
 
 * @param left         Left-most position in window/panel to start drawing \a first_row
 
 * @param right        Right-most x position to draw strings at.
 
 * @param y            Upper-most position in window/panel to start drawing \a first_row
 
 * @param first_row    First row number to draw
 
 * @param max_row      Row-number to stop drawing (the row-number of the row below the last row to draw)
 
 * @param selected     Selected entry by the user.
 
 * @param cur_row      Current row number (internal variable)
 
 * @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)
 
 * @return Row number of the next row to draw
 
 */
 
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
 
{
 
	if (this->IsFiltered()) return cur_row;
 
	if (cur_row >= max_row) return cur_row;
 

	
 
	bool rtl = _current_text_dir == TD_RTL;
 
	int offset = rtl ? -4 : 4;
 
	int level_width = rtl ? -LEVEL_WIDTH : LEVEL_WIDTH;
 

	
 
	int x = rtl ? right : left;
 
	if (cur_row >= first_row) {
 
		int colour = _colour_gradient[COLOUR_ORANGE][4];
 
		y += (cur_row - first_row) * SETTING_HEIGHT; // Compute correct y start position
 

	
 
		/* Draw vertical for parent nesting levels */
 
		for (uint lvl = 0; lvl < this->level; lvl++) {
 
			if (!HasBit(parent_last, lvl)) GfxDrawLine(x + offset, y, x + offset, y + SETTING_HEIGHT - 1, colour);
 
			x += level_width;
 
		}
 
		/* draw own |- prefix */
0 comments (0 inline, 0 general)