mirror of https://github.com/OpenTTD/OpenTTD.git
Codechange: add helper functions to read an int setting value
This commit is contained in:
parent
86c9ef8134
commit
8372c679e3
|
@ -313,13 +313,13 @@ char *OneOfManySettingDesc::FormatSingleValue(char *buf, const char *last, uint
|
|||
|
||||
void OneOfManySettingDesc::FormatValue(char *buf, const char *last, const void *object) const
|
||||
{
|
||||
uint id = (uint)ReadValue(GetVariableAddress(object, &this->save), this->save.conv);
|
||||
uint id = (uint)this->Read(object);
|
||||
this->FormatSingleValue(buf, last, id);
|
||||
}
|
||||
|
||||
void ManyOfManySettingDesc::FormatValue(char *buf, const char *last, const void *object) const
|
||||
{
|
||||
uint bitmask = (uint)ReadValue(GetVariableAddress(object, &this->save), this->save.conv);
|
||||
uint bitmask = (uint)this->Read(object);
|
||||
uint id = 0;
|
||||
bool first = true;
|
||||
FOR_EACH_SET_BIT(id, bitmask) {
|
||||
|
@ -448,6 +448,17 @@ void IntSettingDesc::Write_ValidateSetting(const void *object, int32 val) const
|
|||
WriteValue(ptr, this->save.conv, (int64)val);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the integer from the the actual setting.
|
||||
* @param object The object the setting is to be saved in.
|
||||
* @return The value of the saved integer.
|
||||
*/
|
||||
int32 IntSettingDesc::Read(const void *object) const
|
||||
{
|
||||
void *ptr = GetVariableAddress(object, &this->save);
|
||||
return (int32)ReadValue(ptr, this->save.conv);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the string value of a setting.
|
||||
* @param object The object the setting is to be saved in.
|
||||
|
@ -609,20 +620,20 @@ static void IniSaveSettings(IniFile *ini, const SettingTable &settings_table, co
|
|||
|
||||
void IntSettingDesc::FormatValue(char *buf, const char *last, const void *object) const
|
||||
{
|
||||
uint32 i = (uint32)ReadValue(GetVariableAddress(object, &this->save), this->save.conv);
|
||||
uint32 i = (uint32)this->Read(object);
|
||||
seprintf(buf, last, IsSignedVarMemType(this->save.conv) ? "%d" : (this->save.conv & SLF_HEX) ? "%X" : "%u", i);
|
||||
}
|
||||
|
||||
void BoolSettingDesc::FormatValue(char *buf, const char *last, const void *object) const
|
||||
{
|
||||
bool val = ReadValue(GetVariableAddress(object, &this->save), this->save.conv) != 0;
|
||||
bool val = this->Read(object) != 0;
|
||||
strecpy(buf, val ? "true" : "false", last);
|
||||
}
|
||||
|
||||
bool IntSettingDesc::IsSameValue(const IniItem *item, void *object) const
|
||||
{
|
||||
int64 item_value = this->ParseValue(item->value->c_str());
|
||||
int64 object_value = ReadValue(GetVariableAddress(object, &this->save), this->save.conv);
|
||||
int32 item_value = (int32)this->ParseValue(item->value->c_str());
|
||||
int32 object_value = this->Read(object);
|
||||
return item_value == object_value;
|
||||
}
|
||||
|
||||
|
@ -1814,18 +1825,16 @@ void DeleteGRFPresetFromConfig(const char *config_name)
|
|||
*/
|
||||
void IntSettingDesc::ChangeValue(const void *object, int32 newval) const
|
||||
{
|
||||
void *var = GetVariableAddress(object, &this->save);
|
||||
|
||||
int32 oldval = (int32)ReadValue(var, this->save.conv);
|
||||
int32 oldval = this->Read(object);
|
||||
|
||||
this->Write_ValidateSetting(object, newval);
|
||||
newval = (int32)ReadValue(var, this->save.conv);
|
||||
newval = this->Read(object);
|
||||
|
||||
if (oldval == newval) return;
|
||||
|
||||
if (this->proc != nullptr && !this->proc(newval)) {
|
||||
/* The change was not allowed, so revert. */
|
||||
WriteValue(var, this->save.conv, (int64)oldval);
|
||||
WriteValue(GetVariableAddress(object, &this->save), this->save.conv, (int64)oldval);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -1989,12 +1998,12 @@ void SetDefaultCompanySettings(CompanyID cid)
|
|||
*/
|
||||
void SyncCompanySettings()
|
||||
{
|
||||
const void *old_object = &Company::Get(_current_company)->settings;
|
||||
const void *new_object = &_settings_client.company;
|
||||
uint i = 0;
|
||||
for (auto &sd : _company_settings) {
|
||||
const void *old_var = GetVariableAddress(&Company::Get(_current_company)->settings, &sd->save);
|
||||
const void *new_var = GetVariableAddress(&_settings_client.company, &sd->save);
|
||||
uint32 old_value = (uint32)ReadValue(old_var, sd->save.conv);
|
||||
uint32 new_value = (uint32)ReadValue(new_var, sd->save.conv);
|
||||
uint32 old_value = (uint32)sd->AsIntSetting()->Read(new_object);
|
||||
uint32 new_value = (uint32)sd->AsIntSetting()->Read(old_object);
|
||||
if (old_value != new_value) NetworkSendCommand(0, i, new_value, CMD_CHANGE_COMPANY_SETTING, nullptr, nullptr, _local_company);
|
||||
i++;
|
||||
}
|
||||
|
@ -2179,7 +2188,10 @@ static void LoadSettings(const SettingTable &settings, void *object)
|
|||
void *ptr = GetVariableAddress(object, &osd->save);
|
||||
|
||||
if (!SlObjectMember(ptr, &osd->save)) continue;
|
||||
if (osd->IsIntSetting()) osd->AsIntSetting()->Write_ValidateSetting(object, ReadValue(ptr, osd->save.conv));
|
||||
if (osd->IsIntSetting()) {
|
||||
const IntSettingDesc *int_setting = osd->AsIntSetting();
|
||||
int_setting->Write_ValidateSetting(object, int_setting->Read(object));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -74,7 +74,7 @@ static const StringID _font_zoom_dropdown[] = {
|
|||
|
||||
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 IntSettingDesc *sd);
|
||||
static const void *ResolveObject(const GameSettings *settings_ptr, const IntSettingDesc *sd);
|
||||
|
||||
/**
|
||||
* Get index of the current screen resolution.
|
||||
|
@ -1075,16 +1075,14 @@ bool SettingEntry::IsVisibleByRestrictionMode(RestrictionMode mode) const
|
|||
/* There shall not be any restriction, i.e. all settings shall be visible. */
|
||||
if (mode == RM_ALL) return true;
|
||||
|
||||
GameSettings *settings_ptr = &GetGameSettings();
|
||||
const IntSettingDesc *sd = this->setting;
|
||||
|
||||
if (mode == RM_BASIC) return (this->setting->cat & SC_BASIC_LIST) != 0;
|
||||
if (mode == RM_ADVANCED) return (this->setting->cat & SC_ADVANCED_LIST) != 0;
|
||||
|
||||
/* Read the current value. */
|
||||
const void *var = ResolveVariableAddress(settings_ptr, sd);
|
||||
int64 current_value = ReadValue(var, sd->save.conv);
|
||||
|
||||
const void *object = ResolveObject(&GetGameSettings(), sd);
|
||||
int64 current_value = sd->Read(object);
|
||||
int64 filter_value;
|
||||
|
||||
if (mode == RM_CHANGED_AGAINST_DEFAULT) {
|
||||
|
@ -1098,11 +1096,10 @@ bool SettingEntry::IsVisibleByRestrictionMode(RestrictionMode mode) const
|
|||
* its value is used when starting a new game. */
|
||||
|
||||
/* Make sure we're not comparing the new game settings against itself. */
|
||||
assert(settings_ptr != &_settings_newgame);
|
||||
assert(&GetGameSettings() != &_settings_newgame);
|
||||
|
||||
/* Read the new game's value. */
|
||||
var = ResolveVariableAddress(&_settings_newgame, sd);
|
||||
filter_value = ReadValue(var, sd->save.conv);
|
||||
filter_value = sd->Read(ResolveObject(&_settings_newgame, sd));
|
||||
}
|
||||
|
||||
return current_value != filter_value;
|
||||
|
@ -1147,17 +1144,15 @@ bool SettingEntry::UpdateFilterState(SettingFilter &filter, bool force_visible)
|
|||
return visible;
|
||||
}
|
||||
|
||||
static const void *ResolveVariableAddress(const GameSettings *settings_ptr, const IntSettingDesc *sd)
|
||||
static const void *ResolveObject(const GameSettings *settings_ptr, const IntSettingDesc *sd)
|
||||
{
|
||||
if ((sd->flags & SGF_PER_COMPANY) != 0) {
|
||||
if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) {
|
||||
return GetVariableAddress(&Company::Get(_local_company)->settings, &sd->save);
|
||||
} else {
|
||||
return GetVariableAddress(&_settings_client.company, &sd->save);
|
||||
return &Company::Get(_local_company)->settings;
|
||||
}
|
||||
} else {
|
||||
return GetVariableAddress(settings_ptr, &sd->save);
|
||||
return &_settings_client.company;
|
||||
}
|
||||
return settings_ptr;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1193,7 +1188,6 @@ void SettingEntry::SetValueDParams(uint first_param, int32 value) const
|
|||
void SettingEntry::DrawSetting(GameSettings *settings_ptr, int left, int right, int y, bool highlight) const
|
||||
{
|
||||
const IntSettingDesc *sd = this->setting;
|
||||
const void *var = ResolveVariableAddress(settings_ptr, sd);
|
||||
int state = this->flags & SEF_BUTTONS_MASK;
|
||||
|
||||
bool rtl = _current_text_dir == TD_RTL;
|
||||
|
@ -1206,7 +1200,7 @@ void SettingEntry::DrawSetting(GameSettings *settings_ptr, int left, int right,
|
|||
bool editable = sd->IsEditable();
|
||||
|
||||
SetDParam(0, highlight ? STR_ORANGE_STRING1_WHITE : STR_ORANGE_STRING1_LTBLUE);
|
||||
int32 value = (int32)ReadValue(var, sd->save.conv);
|
||||
int32 value = sd->Read(ResolveObject(settings_ptr, sd));
|
||||
if (sd->IsBoolSetting()) {
|
||||
/* Draw checkbox for boolean-value either on/off */
|
||||
DrawBoolButton(buttons_left, button_y, value != 0, editable);
|
||||
|
@ -2184,8 +2178,7 @@ struct GameSettingsWindow : Window {
|
|||
return;
|
||||
}
|
||||
|
||||
const void *var = ResolveVariableAddress(settings_ptr, sd);
|
||||
int32 value = (int32)ReadValue(var, sd->save.conv);
|
||||
int32 value = sd->Read(ResolveObject(settings_ptr, sd));
|
||||
|
||||
/* clicked on the icon on the left side. Either scroller, bool on/off or dropdown */
|
||||
if (x < SETTING_BUTTON_WIDTH && (sd->flags & SGF_MULTISTRING)) {
|
||||
|
|
|
@ -156,6 +156,7 @@ struct IntSettingDesc : SettingDesc {
|
|||
void FormatValue(char *buf, const char *last, const void *object) const override;
|
||||
void ParseValue(const IniItem *item, void *object) const override;
|
||||
bool IsSameValue(const IniItem *item, void *object) const override;
|
||||
int32 Read(const void *object) const;
|
||||
};
|
||||
|
||||
/** Boolean setting. */
|
||||
|
|
Loading…
Reference in New Issue