summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrubidium42 <rubidium@openttd.org>2021-05-23 18:20:49 +0200
committerrubidium42 <rubidium42@users.noreply.github.com>2021-05-27 18:49:43 +0200
commit860003458fc17f02e44b50f98ef570aff824142a (patch)
tree0d89021118e70ba89e408908d289f31a18ad5667
parent72ec81325b201931525e5c5f79abc6eb1de8d7a8 (diff)
downloadopenttd-860003458fc17f02e44b50f98ef570aff824142a.tar.xz
Codechange: make BoolSettingDesc its own sub class
-rw-r--r--src/settings.cpp30
-rw-r--r--src/settings_internal.h15
-rw-r--r--src/table/settings.h.preamble4
3 files changed, 33 insertions, 16 deletions
diff --git a/src/settings.cpp b/src/settings.cpp
index 3a71c6d2e..f26e56686 100644
--- a/src/settings.cpp
+++ b/src/settings.cpp
@@ -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());
diff --git a/src/settings_internal.h b/src/settings_internal.h
index 5e653ec65..1174a1f63 100644
--- a/src/settings_internal.h
+++ b/src/settings_internal.h
@@ -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,
diff --git a/src/table/settings.h.preamble b/src/table/settings.h.preamble
index 6b4dc5e39..c6788f707 100644
--- a/src/table/settings.h.preamble
+++ b/src/table/settings.h.preamble
@@ -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)