1
0
mirror of synced 2025-01-18 00:56:49 +01:00

Added better settings API that handles errors better

This fixes #161
This commit is contained in:
WerWolv 2021-02-16 23:42:35 +01:00
parent 096bdef25a
commit df06dd49c5
5 changed files with 60 additions and 41 deletions

View File

@ -51,6 +51,7 @@ namespace hex {
static std::vector<std::string> read(std::string_view unlocalizedCategory, std::string_view unlocalizedName, const std::vector<std::string>& defaultValue = { });
static std::map<std::string, std::vector<Entry>>& getEntries();
static std::optional<nlohmann::json> getSetting(std::string_view unlocalizedCategory, std::string_view unlocalizedName);
static nlohmann::json& getSettingsData();
};

View File

@ -109,6 +109,15 @@ namespace hex {
return SharedData::settingsEntries;
}
std::optional<nlohmann::json> ContentRegistry::Settings::getSetting(std::string_view unlocalizedCategory, std::string_view unlocalizedName) {
auto &settings = getSettingsData();
if (!settings.contains(unlocalizedCategory)) return { };
if (!settings[unlocalizedCategory.data()].contains(unlocalizedName)) return { };
return settings[unlocalizedCategory.data()][unlocalizedName.data()];
}
nlohmann::json& ContentRegistry::Settings::getSettingsData() {
return SharedData::settingsJson;
}

View File

@ -18,22 +18,24 @@ namespace hex {
}
View::subscribeEvent(Events::SettingsChanged, [](auto) {
int theme = ContentRegistry::Settings::getSettingsData()["hex.builtin.setting.interface"]["hex.builtin.setting.interface.color"];
auto theme = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.color");
switch (theme) {
default:
case 0: /* Dark theme */
imnodes::StyleColorsDark();
break;
case 1: /* Light theme */
imnodes::StyleColorsLight();
break;
case 2: /* Classic theme */
imnodes::StyleColorsClassic();
break;
if (theme.has_value()) {
switch (static_cast<int>(theme.value())) {
default:
case 0: /* Dark theme */
imnodes::StyleColorsDark();
break;
case 1: /* Light theme */
imnodes::StyleColorsLight();
break;
case 2: /* Classic theme */
imnodes::StyleColorsClassic();
break;
}
imnodes::GetStyle().flags = imnodes::StyleFlags(imnodes::StyleFlags_NodeOutline | imnodes::StyleFlags_GridLines);
}
imnodes::GetStyle().flags = imnodes::StyleFlags(imnodes::StyleFlags_NodeOutline | imnodes::StyleFlags_GridLines);
});
View::subscribeEvent(Events::FileLoaded, [this](auto) {

View File

@ -173,19 +173,21 @@ namespace hex {
{
View::subscribeEvent(Events::SettingsChanged, [this](auto) {
int theme = ContentRegistry::Settings::getSettingsData()["hex.builtin.setting.interface"]["hex.builtin.setting.interface.color"];
auto theme = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.color");
switch (theme) {
default:
case 0: /* Dark theme */
this->m_textEditor.SetPalette(TextEditor::GetDarkPalette());
break;
case 1: /* Light theme */
this->m_textEditor.SetPalette(TextEditor::GetLightPalette());
break;
case 2: /* Classic theme */
this->m_textEditor.SetPalette(TextEditor::GetRetroBluePalette());
break;
if (theme.has_value()) {
switch (static_cast<int>(theme.value())) {
default:
case 0: /* Dark theme */
this->m_textEditor.SetPalette(TextEditor::GetDarkPalette());
break;
case 1: /* Light theme */
this->m_textEditor.SetPalette(TextEditor::GetLightPalette());
break;
case 2: /* Classic theme */
this->m_textEditor.SetPalette(TextEditor::GetRetroBluePalette());
break;
}
}
});

View File

@ -55,25 +55,30 @@ namespace hex {
EventManager::subscribe(Events::SettingsChanged, this, [](auto) -> std::any {
{
int theme = ContentRegistry::Settings::getSettingsData()["hex.builtin.setting.interface"]["hex.builtin.setting.interface.color"];
switch (theme) {
default:
case 0: /* Dark theme */
ImGui::StyleColorsDark();
break;
case 1: /* Light theme */
ImGui::StyleColorsLight();
break;
case 2: /* Classic theme */
ImGui::StyleColorsClassic();
break;
auto theme = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.color");
if (theme.has_value()) {
switch (static_cast<int>(theme.value())) {
default:
case 0: /* Dark theme */
ImGui::StyleColorsDark();
break;
case 1: /* Light theme */
ImGui::StyleColorsLight();
break;
case 2: /* Classic theme */
ImGui::StyleColorsClassic();
break;
}
ImGui::GetStyle().Colors[ImGuiCol_DockingEmptyBg] = ImGui::GetStyle().Colors[ImGuiCol_WindowBg];
}
ImGui::GetStyle().Colors[ImGuiCol_DockingEmptyBg] = ImGui::GetStyle().Colors[ImGuiCol_WindowBg];
}
{
std::string language = ContentRegistry::Settings::getSettingsData()["hex.builtin.setting.interface"]["hex.builtin.setting.interface.language"];
LangEntry::loadLanguage(language);
auto language = ContentRegistry::Settings::getSetting("hex.builtin.setting.interface", "hex.builtin.setting.interface.language");
if (language.has_value())
LangEntry::loadLanguage(static_cast<std::string>(language.value()));
}
return { };