Changeset - r28774:da9fc2859d5c
[Not reviewed]
master
0 4 0
Rubidium - 10 months ago 2024-02-08 20:45:20
rubidium@openttd.org
Feature: Fully user configurable number format and abbreviations
4 files changed with 37 insertions and 2 deletions:
0 comments (0 inline, 0 general)
src/settings_table.cpp
Show inline comments
 
@@ -41,24 +41,25 @@
 
#include "ai/ai_config.hpp"
 
#include "ai/ai.hpp"
 
#include "game/game_config.hpp"
 
#include "ship.h"
 
#include "smallmap_gui.h"
 
#include "roadveh.h"
 
#include "roadveh_cmd.h"
 
#include "vehicle_func.h"
 
#include "viewport_func.h"
 
#include "void_map.h"
 
#include "station_func.h"
 
#include "station_base.h"
 
#include "language.h"
 

	
 
#include "table/strings.h"
 
#include "table/settings.h"
 

	
 
#include "safeguards.h"
 

	
 
SettingTable _company_settings{ _company_settings_table };
 
SettingTable _currency_settings{ _currency_settings_table };
 
SettingTable _difficulty_settings{ _difficulty_settings_table };
 
SettingTable _multimedia_settings{ _multimedia_settings_table };
 
SettingTable _economy_settings{ _economy_settings_table };
 
SettingTable _game_settings{ _game_settings_table };
src/settings_type.h
Show inline comments
 
@@ -213,24 +213,26 @@ struct GUISettings {
 

	
 
	uint8_t  developer;                        ///< print non-fatal warnings in console (>= 1), copy debug output to console (== 2)
 
	bool   show_date_in_logs;                ///< whether to show dates in console logs
 
	bool   newgrf_developer_tools;           ///< activate NewGRF developer tools and allow modifying NewGRFs in an existing game
 
	bool   ai_developer_tools;               ///< activate AI/GS developer tools
 
	bool   scenario_developer;               ///< activate scenario developer: allow modifying NewGRFs in an existing game
 
	uint8_t  settings_restriction_mode;        ///< selected restriction mode in adv. settings GUI. @see RestrictionMode
 
	bool   newgrf_show_old_versions;         ///< whether to show old versions in the NewGRF list
 
	uint8_t  newgrf_default_palette;           ///< default palette to use for NewGRFs without action 14 palette information
 

	
 
	bool   scale_bevels;                     ///< bevels are scaled with GUI scale.
 

	
 
	std::string number_format; ///< formatting string for numbers (like "thousands" grouping)
 
	std::string number_abbreviations; ///< mapping to number formats for different powers of ten/thresholds
 
	std::string digit_decimal_separator; ///< decimal separator
 

	
 
	/**
 
	 * Returns true when the user has sufficient privileges to edit newgrfs on a running game
 
	 * @return whether the user has sufficient privileges to edit newgrfs in an existing game
 
	 */
 
	bool UserIsAllowedToChangeNewGRFs() const
 
	{
 
		return this->scenario_developer || this->newgrf_developer_tools;
 
	}
 
};
 

	
src/strings.cpp
Show inline comments
 
@@ -380,26 +380,39 @@ void SetDParamStr(size_t n, std::string 
 
	_global_string_params.SetParam(n, std::move(str));
 
}
 

	
 
static const char *GetDecimalSeparator()
 
{
 
	const char *decimal_separator = _settings_client.gui.digit_decimal_separator.c_str();
 
	if (StrEmpty(decimal_separator)) decimal_separator = _langpack.langpack->digit_decimal_separator;
 
	return decimal_separator;
 
}
 

	
 
void InitializeNumberFormats()
 
{
 
	ParseNumberFormatSeparators(_number_format_separators, _current_language->number_format);
 
	ParseNumberAbbreviations(_number_abbreviations, _current_language->number_abbreviations);
 
	bool loaded_number_format = false;
 
	if (!_settings_client.gui.number_format.empty()) {
 
		auto res = ParseNumberFormatSeparators(_number_format_separators, _settings_client.gui.number_format);
 
		if (res.has_value()) UserError("The setting 'number_format' under 'gui' is invalid: {}", *res);
 
		loaded_number_format = !res.has_value();
 
	}
 
	if (!loaded_number_format) ParseNumberFormatSeparators(_number_format_separators, _current_language->number_format);
 

	
 
	bool loaded_number_abbreviations = false;
 
	if (!_settings_client.gui.number_abbreviations.empty()) {
 
		auto res = ParseNumberAbbreviations(_number_abbreviations, _settings_client.gui.number_abbreviations);
 
		if (res.has_value()) UserError("The setting 'number_abbreviations' under 'gui' is invalid: {}", *res);
 
		loaded_number_abbreviations = !res.has_value();
 
	}
 
	if (!loaded_number_abbreviations) ParseNumberAbbreviations(_number_abbreviations, _current_language->number_abbreviations);
 
	_number_abbreviations.emplace_back(0, _number_format_separators);
 
}
 

	
 
/**
 
 * Format a number into a string.
 
 * @param builder The string builder to write to.
 
 * @param number The number to write down.
 
 * @param separators The separator to use between each of the digits.
 
 */
 
static void FormatNumber(StringBuilder &builder, int64_t number, const NumberFormatSeparators &separators)
 
{
 
	if (number < 0) {
src/table/settings/gui_settings.ini
Show inline comments
 
@@ -7,24 +7,25 @@
 
; GUI settings as stored in the main configuration file ("openttd.cfg").
 

	
 
[pre-amble]
 
static bool CanChangeTimetableMode(int32_t &new_value);
 
static void v_PositionMainToolbar(int32_t new_value);
 
static void v_PositionStatusbar(int32_t new_value);
 
static void RedrawSmallmap(int32_t new_value);
 
static void UpdateLinkgraphColours(int32_t new_value);
 
static void InvalidateCompanyLiveryWindow(int32_t new_value);
 
static void InvalidateNewGRFChangeWindows(int32_t new_value);
 
static void ZoomMinMaxChanged(int32_t new_value);
 
static void SpriteZoomMinChanged(int32_t new_value);
 
void InitializeNumberFormats();
 

	
 
static constexpr std::initializer_list<const char*> _osk_activation{"disabled", "double", "single", "immediately"};
 
static constexpr std::initializer_list<const char*> _savegame_date{"long", "short", "iso"};
 
static constexpr std::initializer_list<const char*> _right_click_close{"no", "yes", "except sticky"};
 

	
 
static const SettingVariant _gui_settings_table[] = {
 
[post-amble]
 
};
 
[templates]
 
SDTC_BOOL  =  SDTC_BOOL(              $var,        $flags, $def,                              $str, $strhelp, $strval, $pre_cb, $post_cb, $str_cb, $help_cb, $val_cb, $from, $to,        $cat, $extra, $startup),
 
SDTC_OMANY = SDTC_OMANY(              $var, $type, $flags, $def,             $max, $full,     $str, $strhelp, $strval, $pre_cb, $post_cb, $str_cb, $help_cb, $val_cb, $from, $to,        $cat, $extra, $startup),
 
SDTC_VAR   =   SDTC_VAR(              $var, $type, $flags, $def,       $min, $max, $interval, $str, $strhelp, $strval, $pre_cb, $post_cb, $str_cb, $help_cb, $val_cb, $from, $to,        $cat, $extra, $startup),
 
@@ -896,17 +897,35 @@ min      = 1
 
max      = 65535
 
cat      = SC_EXPERT
 

	
 
[SDTC_BOOL]
 
var      = gui.scale_bevels
 
flags    = SF_NOT_IN_SAVE | SF_NO_NETWORK_SYNC
 
def      = true
 
post_cb  = [](auto) { SetupWidgetDimensions(); ReInitAllWindows(true); }
 
cat      = SC_BASIC
 
startup  = true
 

	
 
[SDTC_SSTR]
 
var      = gui.number_format
 
flags    = SF_NOT_IN_SAVE | SF_NO_NETWORK_SYNC
 
type     = SLE_STRQ
 
def      = nullptr
 
pre_cb   = [](auto format) { NumberFormatSeparators separators; return !ParseNumberFormatSeparators(separators, format).has_value(); }
 
post_cb  = [](auto) { InitializeNumberFormats(); MarkWholeScreenDirty(); }
 
startup  = true
 

	
 
[SDTC_SSTR]
 
var      = gui.number_abbreviations
 
flags    = SF_NOT_IN_SAVE | SF_NO_NETWORK_SYNC
 
type     = SLE_STRQ
 
def      = nullptr
 
pre_cb   = [](auto format) { NumberAbbreviations abbreviations; return !ParseNumberAbbreviations(abbreviations, format).has_value(); }
 
post_cb  = [](auto) { InitializeNumberFormats(); MarkWholeScreenDirty(); }
 
startup  = true
 

	
 
[SDTC_SSTR]
 
var      = gui.digit_decimal_separator
 
flags    = SF_NOT_IN_SAVE | SF_NO_NETWORK_SYNC
 
type     = SLE_STRQ
 
def      = nullptr
 
post_cb  = [](auto) { MarkWholeScreenDirty(); }
0 comments (0 inline, 0 general)