config: use string_view, added method to get value pointer

This commit is contained in:
Hyper 2024-11-15 13:43:14 +00:00
parent 2024dc46ce
commit cd1f9742f5
4 changed files with 34 additions and 27 deletions

View file

@ -18,7 +18,7 @@ void Config::Load()
{ {
def->ReadValue(toml); def->ReadValue(toml);
#if _DEBUG #if _DEBUG
printf("%s\n", def->GetDefinition().c_str()); printf("%s (0x%llx)\n", def->GetDefinition().c_str(), def->GetValue());
#endif #endif
} }
} }

View file

@ -5,7 +5,7 @@
class Config class Config
{ {
public: public:
inline static std::vector<ConfigDefBase*> Definitions{}; inline static std::vector<IConfigDef*> Definitions{};
CONFIG_DEFINE_ENUM("System", ELanguage, Language, ELanguage::English); CONFIG_DEFINE_ENUM("System", ELanguage, Language, ELanguage::English);
CONFIG_DEFINE("System", bool, Hints, true); CONFIG_DEFINE("System", bool, Hints, true);

View file

@ -30,24 +30,23 @@
#define WINDOWPOS_CENTRED 0x2FFF0000 #define WINDOWPOS_CENTRED 0x2FFF0000
class ConfigDefBase class IConfigDef
{ {
public: public:
virtual ~ConfigDefBase() = default; virtual ~IConfigDef() = default;
virtual void ReadValue(toml::v3::ex::parse_result& toml) = 0; virtual void ReadValue(toml::v3::ex::parse_result& toml) = 0;
virtual void MakeDefault() = 0; virtual void MakeDefault() = 0;
virtual std::string GetSection() const = 0; virtual bool IsMenuOption() const = 0;
virtual std::string GetName() const = 0; virtual std::string_view GetSection() const = 0;
virtual std::string_view GetName() const = 0;
virtual void* GetValue() = 0;
virtual std::string GetDefinition(bool withSection = false) const = 0; virtual std::string GetDefinition(bool withSection = false) const = 0;
virtual std::string ToString() const = 0; virtual std::string ToString() const = 0;
}; };
template<typename T, bool isMenuOption = true> template<typename T, bool isMenuOption = true>
class ConfigDef : public ConfigDefBase class ConfigDef : public IConfigDef
{ {
protected:
bool m_isMenuOption{ isMenuOption };
public: public:
std::string Section{}; std::string Section{};
std::string Name{}; std::string Name{};
@ -123,16 +122,26 @@ public:
Value = DefaultValue; Value = DefaultValue;
} }
std::string GetSection() const override bool IsMenuOption() const override
{
return isMenuOption;
}
std::string_view GetSection() const override
{ {
return Section; return Section;
} }
std::string GetName() const override std::string_view GetName() const override
{ {
return Name; return Name;
} }
void* GetValue() override
{
return &Value;
}
std::string GetDefinition(bool withSection = false) const override std::string GetDefinition(bool withSection = false) const override
{ {
std::string result; std::string result;
@ -147,27 +156,25 @@ public:
std::string ToString() const override std::string ToString() const override
{ {
if constexpr (std::is_same<T, std::string>::value) std::string result = "\"N/A\"";
if constexpr (std::is_same_v<T, std::string>)
{ {
return std::format("\"{}\"", Value); result = std::format("\"{}\"", Value);
} }
else if constexpr (std::is_enum_v<T>) else if constexpr (std::is_enum_v<T>)
{ {
auto it = EnumTemplateReverse.find(Value); auto it = EnumTemplateReverse.find(Value);
if (it != EnumTemplateReverse.end()) if (it != EnumTemplateReverse.end())
{ result = std::format("\"{}\"", it->second);
return std::format("\"{}\"", it->second);
} }
else else
{ {
return "\"N/A\""; result = std::format("{}", Value);
}
}
else
{
return std::format("{}", Value);
} }
return result;
} }
ConfigDef& operator=(const ConfigDef& other) ConfigDef& operator=(const ConfigDef& other)

View file

@ -311,7 +311,7 @@ static void DrawConfigOptions()
for (auto& config : Config::Definitions) for (auto& config : Config::Definitions)
{ {
if (_stricmp(config->GetSection().c_str(), CATEGORIES[g_categoryIndex]) != 0) if (_stricmp(config->GetSection().data(), CATEGORIES[g_categoryIndex]) != 0)
continue; continue;
// Left side // Left side
@ -322,9 +322,9 @@ static void DrawConfigOptions()
drawList->AddRectFilledMultiColor(min, max, COLOR0, COLOR0, COLOR1, COLOR1); drawList->AddRectFilledMultiColor(min, max, COLOR0, COLOR0, COLOR1, COLOR1);
float size = Scale(26.0f); float size = Scale(26.0f);
auto textSize = g_seuratFont->CalcTextSizeA(size, FLT_MAX, 0.0f, config->GetName().c_str()); auto textSize = g_seuratFont->CalcTextSizeA(size, FLT_MAX, 0.0f, config->GetName().data());
drawList->AddText(g_seuratFont, size, { min.x + gridSize, min.y + (optionHeight - textSize.y) / 2.0f }, IM_COL32_WHITE, config->GetName().c_str()); drawList->AddText(g_seuratFont, size, { min.x + gridSize, min.y + (optionHeight - textSize.y) / 2.0f }, IM_COL32_WHITE, config->GetName().data());
// Right side // Right side
min = { max.x + (clipRectMax.x - max.x - valueWidth) / 2.0f, min.y + (optionHeight - valueHeight) / 2.0f }; min = { max.x + (clipRectMax.x - max.x - valueWidth) / 2.0f, min.y + (optionHeight - valueHeight) / 2.0f };
@ -342,7 +342,7 @@ static void DrawConfigOptions()
std::transform(valueText.begin(), valueText.end(), valueText.begin(), toupper); std::transform(valueText.begin(), valueText.end(), valueText.begin(), toupper);
size = Scale(20.0f); size = Scale(20.0f);
textSize = g_newRodinFont->CalcTextSizeA(size, FLT_MAX, 0.0f, valueText.c_str()); textSize = g_newRodinFont->CalcTextSizeA(size, FLT_MAX, 0.0f, valueText.data());
min.x += ((max.x - min.x) - textSize.x) / 2.0f; min.x += ((max.x - min.x) - textSize.x) / 2.0f;
min.y += ((max.y - min.y) - textSize.y) / 2.0f; min.y += ((max.y - min.y) - textSize.y) / 2.0f;
@ -359,7 +359,7 @@ static void DrawConfigOptions()
size, size,
min, min,
IM_COL32_WHITE, IM_COL32_WHITE,
valueText.c_str(), valueText.data(),
Scale(2), Scale(2),
IM_COL32_BLACK); IM_COL32_BLACK);