From b45adba858835ebc89ca1de6b4c4ca86e39c1fe5 Mon Sep 17 00:00:00 2001 From: Pk11 Date: Tue, 23 Mar 2021 18:00:04 -0500 Subject: [PATCH] Make theme use objects instead of arrays Also some minor tweaks to make things less redundant and safer --- include/utils/theme.hpp | 4 +- resources/Themes.json | 44 +++++++------- romfs/lang/en/app.json | 1 - source/utils/theme.cpp | 132 +++++++++++++++++++--------------------- 4 files changed, 88 insertions(+), 93 deletions(-) diff --git a/include/utils/theme.hpp b/include/utils/theme.hpp index 698d619..ec804b2 100644 --- a/include/utils/theme.hpp +++ b/include/utils/theme.hpp @@ -34,10 +34,10 @@ class Theme { public: Theme(const std::string &ThemeJSON = "sdmc:/3ds/Universal-Updater/Themes.json"); - void InitWithDefaultColors(const std::string &ThemePath = "sdmc:/3ds/Universal-Updater/Themes.json"); + nlohmann::json InitWithDefaultColors(const std::string &ThemePath = "sdmc:/3ds/Universal-Updater/Themes.json"); void LoadTheme(const std::string &ThemeName); std::vector ThemeNames(); - uint32_t GetThemeColor(const std::string &colorString, const uint32_t DefaultColor); + uint32_t GetThemeColor(const std::string &ThemeName, const std::string &Key, const uint32_t DefaultColor); uint32_t BarColor() const { return this->vBarColor; }; uint32_t BGColor() const { return this->vBGColor; }; diff --git a/resources/Themes.json b/resources/Themes.json index bc29b2f..b350257 100644 --- a/resources/Themes.json +++ b/resources/Themes.json @@ -1,23 +1,23 @@ { - "Default": [ - "#324962", - "#262C4D", - "#191E35", - "#FFFFFF", - "#324962", - "#191E35", - "#1C213A", - "#6C829B", - "#000000", - "#1C213A", - "#4D6580", - "#334B66", - "#191E35", - "#6C829B", - "#4D6580", - "#4D6580", - "#1C213A", - "#1C213A", - "#ADCCEF" - ] -} \ No newline at end of file + "Default": { + "BGColor": "#262C4D", + "BarColor": "#324962", + "BarOutline": "#191E35", + "BoxInside": "#1C213A", + "BoxSelected": "#6C829B", + "BoxUnselected": "#000000", + "DownListPrev": "#1C213A", + "EntryBar": "#324962", + "EntryOutline": "#191E35", + "MarkSelected": "#4D6580", + "MarkUnselected": "#1C213A", + "ProgressbarIn": "#4D6580", + "ProgressbarOut": "#1C213A", + "SearchBar": "#334B66", + "SearchBarOutline": "#191E35", + "SideBarIconColor": "#ADCCEF", + "SideBarSelected": "#6C829B", + "SideBarUnselected": "#4D6580", + "TextColor": "#FFFFFF" + } +} diff --git a/romfs/lang/en/app.json b/romfs/lang/en/app.json index 4f7308b..79e625e 100644 --- a/romfs/lang/en/app.json +++ b/romfs/lang/en/app.json @@ -127,7 +127,6 @@ "START_SELECT": "Press START to select the current folder", "STORE_INFO": "Store Info", "SYNTAX_ERROR": "Syntax Error!", - "THEME_DEFAULT": "Default", "TITLE": "Title", "TOP_STYLE": "Top Style", "UNISTORE_BG": "Use UniStore BG", diff --git a/source/utils/theme.cpp b/source/utils/theme.cpp index 405eb4e..12bbbfb 100644 --- a/source/utils/theme.cpp +++ b/source/utils/theme.cpp @@ -39,80 +39,70 @@ Theme::Theme(const std::string &ThemeJSON) { - /* Set Default colors. */ - this->vBarColor = C2D_Color32(50, 73, 98, 255); - this->vBGColor = C2D_Color32(38, 44, 77, 255); - this->vBarOutline = C2D_Color32(25, 30, 53, 255); - this->vTextColor = C2D_Color32(255, 255, 255, 255); - this->vEntryBar = C2D_Color32(50, 73, 98, 255); - this->vEntryOutline = C2D_Color32(25, 30, 53, 255); - this->vBoxInside = C2D_Color32(28, 33, 58, 255); - this->vBoxSelected = C2D_Color32(108, 130, 155, 255); - this->vBoxUnselected = C2D_Color32(0, 0, 0, 255); - this->vProgressbarOut = C2D_Color32(28, 33, 58, 255); - this->vProgressbarIn = C2D_Color32(77, 101, 128, 255); - this->vSearchBar = C2D_Color32(51, 75, 102, 255); - this->vSearchBarOutline = C2D_Color32(25, 30, 53, 255); - this->vSideBarSelected = C2D_Color32(108, 130, 155, 255); - this->vSideBarUnselected = C2D_Color32(77, 101, 128, 255); - this->vMarkSelected = C2D_Color32(77, 101, 128, 255); - this->vMarkUnselected = C2D_Color32(28, 33, 58, 255); - this->vDownListPrev = C2D_Color32(28, 33, 58, 255); - this->vSideBarIconColor = C2D_Color32(173, 204, 239, 255); - - if (access(ThemeJSON.c_str(), F_OK) != 0) this->InitWithDefaultColors(); - - FILE *file = fopen(ThemeJSON.c_str(), "r"); - this->json = nlohmann::json::parse(file, nullptr, false); - fclose(file); + FILE *file = fopen(ThemeJSON.c_str(), "rt"); + if(file) { + this->json = nlohmann::json::parse(file, nullptr, false); + fclose(file); + } + if(!file || this->json.is_discarded()) + this->json = this->InitWithDefaultColors(); this->Loaded = true; } -void Theme::InitWithDefaultColors(const std::string &ThemePath) { - const std::vector DefaultColors = { // Default Universal-Updater Theme. - "#324962", "#262C4D", "#191E35", "#FFFFFF", - "#324962", "#191E35", "#1C213A", "#6C829B", - "#000000", "#1C213A", "#4D6580", "#334B66", - "#191E35", "#6C829B", "#4D6580", "#4D6580", - "#1C213A", "#1C213A", "#ADCCEF" +nlohmann::json Theme::InitWithDefaultColors(const std::string &ThemePath) { + nlohmann::json JS = { + {"Default", { + {"BarColor", "#324962"}, + {"BGColor", "#262C4D"}, + {"BarOutline", "#191E35"}, + {"TextColor", "#FFFFFF"}, + {"EntryBar", "#324962"}, + {"EntryOutline", "#191E35"}, + {"BoxInside", "#1C213A"}, + {"BoxSelected", "#6C829B"}, + {"BoxUnselected", "#000000"}, + {"ProgressbarOut", "#1C213A"}, + {"ProgressbarIn", "#4D6580"}, + {"SearchBar", "#334B66"}, + {"SearchBarOutline", "#191E35"}, + {"SideBarSelected", "#6C829B"}, + {"SideBarUnselected", "#4D6580"}, + {"MarkSelected", "#4D6580"}, + {"MarkUnselected", "#1C213A"}, + {"DownListPrev", "#1C213A"}, + {"SideBarIconColor", "#ADCCEF"} + }} }; - nlohmann::json JS = { }; - JS["Default"] = DefaultColors; - FILE *out = fopen(ThemePath.c_str(), "w"); const std::string dump = JS.dump(1, '\t'); fwrite(dump.c_str(), 1, JS.dump(1, '\t').size(), out); fclose(out); + + return JS; } void Theme::LoadTheme(const std::string &ThemeName) { - if (this->Loaded && this->json.contains(ThemeName) && this->json[ThemeName].is_array()) { - if (this->json[ThemeName].size() == 19) { // 19 Colors array. - const std::vector Colors = this->json[ThemeName].get>(); - - this->vBarColor = this->GetThemeColor(Colors[0], C2D_Color32(50, 73, 98, 255)); - this->vBGColor = this->GetThemeColor(Colors[1], C2D_Color32(38, 44, 77, 255)); - this->vBarOutline = this->GetThemeColor(Colors[2], C2D_Color32(25, 30, 53, 255)); - this->vTextColor = this->GetThemeColor(Colors[3], C2D_Color32(255, 255, 255, 255)); - this->vEntryBar = this->GetThemeColor(Colors[4], C2D_Color32(50, 73, 98, 255)); - this->vEntryOutline = this->GetThemeColor(Colors[5], C2D_Color32(25, 30, 53, 255)); - this->vBoxInside = this->GetThemeColor(Colors[6], C2D_Color32(28, 33, 58, 255)); - this->vBoxSelected = this->GetThemeColor(Colors[7], C2D_Color32(108, 130, 155, 255)); - this->vBoxUnselected = this->GetThemeColor(Colors[8], C2D_Color32(0, 0, 0, 255)); - this->vProgressbarOut = this->GetThemeColor(Colors[9], C2D_Color32(28, 33, 58, 255)); - this->vProgressbarIn = this->GetThemeColor(Colors[10], C2D_Color32(77, 101, 128, 255)); - this->vSearchBar = this->GetThemeColor(Colors[11], C2D_Color32(51, 75, 102, 255)); - this->vSearchBarOutline = this->GetThemeColor(Colors[12], C2D_Color32(25, 30, 53, 255)); - this->vSideBarSelected = this->GetThemeColor(Colors[13], C2D_Color32(108, 130, 155, 255)); - this->vSideBarUnselected = this->GetThemeColor(Colors[14], C2D_Color32(77, 101, 128, 255)); - this->vMarkSelected = this->GetThemeColor(Colors[15], C2D_Color32(77, 101, 128, 255)); - this->vMarkUnselected = this->GetThemeColor(Colors[16], C2D_Color32(28, 33, 58, 255)); - this->vDownListPrev = this->GetThemeColor(Colors[17], C2D_Color32(28, 33, 58, 255)); - this->vSideBarIconColor = this->GetThemeColor(Colors[18], C2D_Color32(173, 204, 239, 255)); - } - } + this->vBarColor = this->GetThemeColor(ThemeName, "BarColor", C2D_Color32(50, 73, 98, 255)); + this->vBGColor = this->GetThemeColor(ThemeName, "BGColor", C2D_Color32(38, 44, 77, 255)); + this->vBarOutline = this->GetThemeColor(ThemeName, "BarOutline", C2D_Color32(25, 30, 53, 255)); + this->vTextColor = this->GetThemeColor(ThemeName, "TextColor", C2D_Color32(255, 255, 255, 255)); + this->vEntryBar = this->GetThemeColor(ThemeName, "EntryBar", C2D_Color32(50, 73, 98, 255)); + this->vEntryOutline = this->GetThemeColor(ThemeName, "EntryOutline", C2D_Color32(25, 30, 53, 255)); + this->vBoxInside = this->GetThemeColor(ThemeName, "BoxInside", C2D_Color32(28, 33, 58, 255)); + this->vBoxSelected = this->GetThemeColor(ThemeName, "BoxSelected", C2D_Color32(108, 130, 155, 255)); + this->vBoxUnselected = this->GetThemeColor(ThemeName, "BoxUnselected", C2D_Color32(0, 0, 0, 255)); + this->vProgressbarOut = this->GetThemeColor(ThemeName, "ProgressbarOut", C2D_Color32(28, 33, 58, 255)); + this->vProgressbarIn = this->GetThemeColor(ThemeName, "ProgressbarIn", C2D_Color32(77, 101, 128, 255)); + this->vSearchBar = this->GetThemeColor(ThemeName, "SearchBar", C2D_Color32(51, 75, 102, 255)); + this->vSearchBarOutline = this->GetThemeColor(ThemeName, "SearchBarOutline", C2D_Color32(25, 30, 53, 255)); + this->vSideBarSelected = this->GetThemeColor(ThemeName, "SideBarSelected", C2D_Color32(108, 130, 155, 255)); + this->vSideBarUnselected = this->GetThemeColor(ThemeName, "SideBarUnselected", C2D_Color32(77, 101, 128, 255)); + this->vMarkSelected = this->GetThemeColor(ThemeName, "MarkSelected", C2D_Color32(77, 101, 128, 255)); + this->vMarkUnselected = this->GetThemeColor(ThemeName, "MarkUnselected", C2D_Color32(28, 33, 58, 255)); + this->vDownListPrev = this->GetThemeColor(ThemeName, "DownListPrev", C2D_Color32(28, 33, 58, 255)); + this->vSideBarIconColor = this->GetThemeColor(ThemeName, "SideBarIconColor", C2D_Color32(173, 204, 239, 255)); } std::vector Theme::ThemeNames() { @@ -128,13 +118,19 @@ std::vector Theme::ThemeNames() { } -uint32_t Theme::GetThemeColor(const std::string &colorString, const uint32_t DefaultColor) { - if (colorString.length() < 7 || std::regex_search(colorString.substr(1), std::regex("[^0-9A-F]"))) { // invalid color. - return DefaultColor; +uint32_t Theme::GetThemeColor(const std::string &ThemeName, const std::string &Key, const uint32_t DefaultColor) { + if(this->json.contains(ThemeName) && this->json[ThemeName].is_object() && this->json[ThemeName].contains(Key) && this->json[ThemeName][Key].is_string()) { + const std::string &colorString = this->json[ThemeName][Key].get_ref(); + if (colorString.length() < 7 || std::regex_search(colorString.substr(1), std::regex("[^0-9A-F]"))) { // invalid color. + return DefaultColor; + } + + int r = std::stoi(colorString.substr(1, 2), nullptr, 16); + int g = std::stoi(colorString.substr(3, 2), nullptr, 16); + int b = std::stoi(colorString.substr(5, 2), nullptr, 16); + return RGBA8(r, g, b, 0xFF); + } - int r = std::stoi(colorString.substr(1, 2), nullptr, 16); - int g = std::stoi(colorString.substr(3, 2), nullptr, 16); - int b = std::stoi(colorString.substr(5, 2), nullptr, 16); - return RGBA8(r, g, b, 0xFF); + return DefaultColor; } \ No newline at end of file