Added TextField option type

This commit is contained in:
thecozies 2024-08-07 09:18:06 -05:00 committed by Mr-Wiseguy
parent 7bfd5f9bc8
commit 33d3367761
3 changed files with 45 additions and 6 deletions

View file

@ -7,14 +7,15 @@
namespace recomp::config {
enum class ConfigOptionType {
Label,
Label, // todo
// Base types
Checkbox,
RadioTabs,
Dropdown,
Range,
Color,
Trigger,
Button, // todo
TextField,
// Group types
CheckboxGroup,
Group,
@ -28,7 +29,8 @@ NLOHMANN_JSON_SERIALIZE_ENUM(ConfigOptionType, {
{ConfigOptionType::Dropdown, "Dropdown"},
{ConfigOptionType::Range, "Range"},
{ConfigOptionType::Color, "Color"},
{ConfigOptionType::Trigger, "Trigger"},
{ConfigOptionType::Button, "Button"},
{ConfigOptionType::TextField, "TextField"},
{ConfigOptionType::CheckboxGroup, "CheckboxGroup"},
{ConfigOptionType::Group, "Group"}
});

View file

@ -73,6 +73,20 @@ void register_config_option(
config_registry.key_ref_map[this_key] = { config_group, json_path };
switch (type) {
case ConfigOptionType::TextField: {
std::string default_val = "";
if (j.find("default") != j.end()) {
default_val = get_string_in_json(j, "default");
}
if (j.find("maxlength") != j.end()) {
if (!j["maxlength"].is_number()) {
TODO_PARSE_ERROR(this_key + "/maxlength" , "number");
return;
}
}
set_config_store_value_and_default(this_key, default_val, default_val);
break;
}
case ConfigOptionType::Checkbox: {
bool default_val = false;
if (j.find("default") != j.end()) {

View file

@ -21,9 +21,9 @@ struct string_hash {
}
};
namespace recomp::config {
typedef std::variant<std::string, int> config_store_value;
typedef std::unordered_map<std::string, config_store_value, string_hash, std::equal_to<>> config_store_map;
namespace recomp::config {
using config_store_value = std::variant<std::monostate, std::string, int>;
using config_store_map = std::unordered_map<std::string, config_store_value, string_hash, std::equal_to<>>;
struct ConfigStore {
config_store_map map;
@ -34,6 +34,13 @@ namespace recomp::config {
extern ConfigStore config_store;
// Index of variant type config_store_value
enum class ConfigStoreValueType {
Null,
String,
Int
};
void set_config_store_value(std::string_view key, config_store_value value);
void set_config_store_default_value(std::string_view key, config_store_value value);
void set_config_store_value_and_default(std::string_view key, config_store_value value, config_store_value default_value);
@ -68,6 +75,22 @@ namespace recomp::config {
return get_config_store_default_value<T>(key);
}
};
// Get a value from the config store, if it doesn't exist then return the supplied value
template<typename T>
T get_config_store_value_with_default(std::string_view key, T default_value) {
std::lock_guard lock{ config_store.store_mutex };
auto it = config_store.map.find(key);
if (it != config_store.map.end()) {
if (std::holds_alternative<T>(it->second)) {
return std::get<T>(it->second);
} else {
throw std::runtime_error("Stored value is not of requested type");
}
} else {
return default_value;
}
};
};
#endif