diff --git a/source/code/menudef_themes.cpp b/source/code/menudef_themes.cpp index 735b907..d5140f5 100644 --- a/source/code/menudef_themes.cpp +++ b/source/code/menudef_themes.cpp @@ -76,7 +76,13 @@ public: } }; -static int theme2edit = 0; +static Theme themeBackup; +static Theme themeToEdit; + +static void themesEditSwitchBackground() { + themeToEdit.background = ThemesGetNextBackground(themeToEdit.background.name); + globalData.theme = themeToEdit; +} static void themesEditSlot1() { ThemesMenu tem(globalData.screen, _("Edit custom theme 1"), true); @@ -85,9 +91,16 @@ static void themesEditSlot1() { // Theme not found return; } - theme2edit = theme_index; - globalData.theme = ThemesGet(theme2edit); + Button bSwitchBackground; + bSwitchBackground.setLabel(_("Switch background")); + bSwitchBackground.setAction(&themesEditSwitchBackground); + tem.addButton(&bSwitchBackground); + themeBackup = globalData.theme; + themeToEdit = ThemesGet(theme_index); + globalData.theme = themeToEdit; RunGameState(tem); + ThemesAddOrReplace(themeToEdit); + globalData.theme = themeBackup; } diff --git a/source/code/themes.cpp b/source/code/themes.cpp index d9dd7d1..4bd860c 100644 --- a/source/code/themes.cpp +++ b/source/code/themes.cpp @@ -135,7 +135,7 @@ static bool initialized = false; static size_t current_theme = 0; -static void ThemesFillMissingFields(Theme& theme) { +void ThemesFillMissingFields(Theme& theme) { if (theme.background.name.empty()) { //If the theme does not define a background then use the standard. theme.background.name = "standard"; @@ -268,3 +268,31 @@ size_t ThemeGetNumber(const std::string& name) { } return ret; } + +void ThemesAddOrReplace(const Theme& theme) { + ThemesInit(); + for (size_t i = 0; i < themes.size(); ++i) { + if (themes[i].theme_name == theme.theme_name) { + themes[i] = theme; + return; + } + } + themes.push_back(theme); +} + +BackGroundData ThemesGetNextBackground(const std::string& current) { + ThemesInit(); + BackGroundData ret = background_data["standard"]; + for (auto& pair : background_data) { + if (pair.first == current) { + auto it = background_data.find(current); + ++it; + if (it == background_data.end()) { + it = background_data.begin(); + } + ret = it->second; + break; + } + } + return ret; +} diff --git a/source/code/themes.hpp b/source/code/themes.hpp index d4a1667..fd04a03 100644 --- a/source/code/themes.hpp +++ b/source/code/themes.hpp @@ -61,6 +61,10 @@ struct ThemeFileData { std::vector themes; }; +void ThemesFillMissingFields(Theme& theme); + +void ThemesAddOrReplace(const Theme& theme); + /** * @brief returns a theme from a list * @return A copy of a theme @@ -85,3 +89,5 @@ void ThemesSaveCustomSlots(); * Returns 0 if it does not exist or is the standard theme. */ size_t ThemeGetNumber(const std::string& name); + +BackGroundData ThemesGetNextBackground(const std::string& current); \ No newline at end of file