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);
0 comments (0 inline, 0 general)