Codechange: make BoolSettingDesc its own sub class

pull/332/head
rubidium42 3 years ago committed by rubidium42
parent 72ec81325b
commit 860003458f

@ -423,23 +423,24 @@ size_t IntSettingDesc::ParseValue(const char *str) const
break;
}
case SDT_BOOLX: {
if (strcmp(str, "true") == 0 || strcmp(str, "on") == 0 || strcmp(str, "1") == 0) return true;
if (strcmp(str, "false") == 0 || strcmp(str, "off") == 0 || strcmp(str, "0") == 0) return false;
ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
msg.SetDParamStr(0, str);
msg.SetDParamStr(1, this->name);
_settings_error_list.push_back(msg);
break;
}
default: NOT_REACHED();
}
return this->def;
}
size_t BoolSettingDesc::ParseValue(const char *str) const
{
if (strcmp(str, "true") == 0 || strcmp(str, "on") == 0 || strcmp(str, "1") == 0) return true;
if (strcmp(str, "false") == 0 || strcmp(str, "off") == 0 || strcmp(str, "0") == 0) return false;
ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_VALUE);
msg.SetDParamStr(0, str);
msg.SetDParamStr(1, this->name);
_settings_error_list.push_back(msg);
return this->def;
}
/**
* Set the value of a setting and if needed clamp the value to the preset minimum and maximum.
* @param object The object the setting is to be saved in.
@ -663,7 +664,6 @@ void IntSettingDesc::FormatValue(char *buf, const char *last, const void *object
{
uint32 i = (uint32)ReadValue(GetVariableAddress(object, &this->save), this->save.conv);
switch (this->cmd) {
case SDT_BOOLX: strecpy(buf, (i != 0) ? "true" : "false", last); break;
case SDT_NUMX: seprintf(buf, last, IsSignedVarMemType(this->save.conv) ? "%d" : (this->save.conv & SLF_HEX) ? "%X" : "%u", i); break;
case SDT_ONEOFMANY: MakeOneOfMany(buf, last, this->many, i); break;
case SDT_MANYOFMANY: MakeManyOfMany(buf, last, this->many, i); break;
@ -671,6 +671,12 @@ void IntSettingDesc::FormatValue(char *buf, const char *last, const void *object
}
}
void BoolSettingDesc::FormatValue(char *buf, const char *last, const void *object) const
{
bool val = ReadValue(GetVariableAddress(object, &this->save), this->save.conv) != 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());

@ -129,7 +129,7 @@ struct SettingDesc {
virtual bool IsSameValue(const IniItem *item, void *object) const = 0;
};
/** Integer type, including boolean, settings. Only these are shown in the settings UI. */
/** Base integer type, including boolean, settings. Only these are shown in the settings UI. */
struct IntSettingDesc : SettingDesc {
IntSettingDesc(SaveLoad save, const char *name, SettingGuiFlag flags, SettingDescType cmd, bool startup, int32 def,
int32 min, uint32 max, int32 interval, StringID str, StringID str_help, StringID str_val,
@ -153,12 +153,23 @@ struct IntSettingDesc : SettingDesc {
void ChangeValue(const void *object, int32 newvalue) const;
void Write_ValidateSetting(const void *object, int32 value) const;
size_t ParseValue(const char *str) const;
virtual size_t ParseValue(const char *str) const;
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;
};
/** Boolean setting. */
struct BoolSettingDesc : IntSettingDesc {
BoolSettingDesc(SaveLoad save, const char *name, SettingGuiFlag flags, SettingDescType cmd, bool startup, bool def,
StringID str, StringID str_help, StringID str_val, SettingCategory cat, OnChange *proc) :
IntSettingDesc(save, name, flags, cmd, startup, def, 0, 1, 0, str, str_help, str_val, cat, proc) {}
virtual ~BoolSettingDesc() {}
size_t ParseValue(const char *str) const override;
void FormatValue(char *buf, const char *last, const void *object) const override;
};
/** String settings. */
struct StringSettingDesc : SettingDesc {
StringSettingDesc(SaveLoad save, const char *name, SettingGuiFlag flags, SettingDescType cmd, bool startup, const char *def,

@ -62,7 +62,7 @@ static size_t ConvertLandscape(const char *value);
NSD(Int, SLEG_GENERAL(SL_VAR, var, type | flags, 1, from, to, extra), name, guiflags, SDT_NUMX, startup, def, min, max, interval, str, strhelp, strval, cat, proc)
#define SDTG_BOOL(name, flags, guiflags, var, def, str, strhelp, strval, proc, from, to, cat, extra, startup)\
NSD(Int, SLEG_GENERAL(SL_VAR, var, SLE_BOOL | flags, 1, from, to, extra), name, guiflags, SDT_BOOLX, startup, def, 0, 1, 0, str, strhelp, strval, cat, proc)
NSD(Bool, SLEG_GENERAL(SL_VAR, var, SLE_BOOL | flags, 1, from, to, extra), name, guiflags, SDT_BOOLX, startup, def, str, strhelp, strval, cat, proc)
#define SDTG_LIST(name, type, flags, guiflags, var, def, length, from, to, cat, extra, startup)\
NSD(List, SLEG_GENERAL(SL_ARR, var, type | flags, length, from, to, extra), name, guiflags, SDT_INTLIST, startup, def)
@ -85,7 +85,7 @@ static size_t ConvertLandscape(const char *value);
NSD(Int, SLE_GENERAL(SL_VAR, base, var, type | flags, 1, from, to, extra), #var, guiflags, SDT_NUMX, startup, def, min, max, interval, str, strhelp, strval, cat, proc)
#define SDT_BOOL(base, var, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extra, startup)\
NSD(Int, SLE_GENERAL(SL_VAR, base, var, SLE_BOOL | flags, 1, from, to, extra), #var, guiflags, SDT_BOOLX, startup, def, 0, 1, 0, str, strhelp, strval, cat, proc)
NSD(Bool, SLE_GENERAL(SL_VAR, base, var, SLE_BOOL | flags, 1, from, to, extra), #var, guiflags, SDT_BOOLX, startup, def, str, strhelp, strval, cat, proc)
#define SDT_LIST(base, var, type, flags, guiflags, def, from, to, cat, extra, startup)\
NSD(List, SLE_GENERAL(SL_ARR, base, var, type | flags, lengthof(((base*)8)->var), from, to, extra), #var, guiflags, SDT_INTLIST, startup, def)

Loading…
Cancel
Save