diff --git a/source/code/menudef_themes.cpp b/source/code/menudef_themes.cpp index a18c666..735b907 100644 --- a/source/code/menudef_themes.cpp +++ b/source/code/menudef_themes.cpp @@ -43,11 +43,13 @@ static void testMusic() { highbeatNext = !highbeatNext; //Toggle between standard and highbeat } + + class ThemesMenu : public Menu { -private: +public: std::shared_ptr game; sago::SagoTextField themeTitle; -public: + ThemesMenu(SDL_Renderer* screen, const std::string& title, bool submenu) : Menu(screen, title, submenu) { game = std::make_shared(globalData.xsize-450,100,&globalData.spriteHolder->GetDataHolder()); game->putSampleBlocks(); @@ -74,12 +76,31 @@ public: } }; +static int theme2edit = 0; + +static void themesEditSlot1() { + ThemesMenu tem(globalData.screen, _("Edit custom theme 1"), true); + size_t theme_index = ThemeGetNumber("custom_slot_1"); + if (theme_index == 0) { + // Theme not found + return; + } + theme2edit = theme_index; + globalData.theme = ThemesGet(theme2edit); + RunGameState(tem); +} + + void OpenThemesMenu() { ThemesMenu tm(globalData.screen, _("Themes"), true); Button bSwitchTheme; bSwitchTheme.setLabel(_("Switch theme")); bSwitchTheme.setAction(&switchTheme); tm.addButton(&bSwitchTheme); + Button bEditTheme1; + bEditTheme1.setLabel(_("Edit custom theme 1")); + bEditTheme1.setAction(&themesEditSlot1); + tm.addButton(&bEditTheme1); if (!globalData.NoSound) { Button bTestMusic; bTestMusic.setLabel(_("Test music")); diff --git a/source/code/themes.cpp b/source/code/themes.cpp index be82c95..d9dd7d1 100644 --- a/source/code/themes.cpp +++ b/source/code/themes.cpp @@ -35,6 +35,9 @@ https://www.blockattack.net using json = nlohmann::json; +const int NUMBER_OF_CUSTOM_SLOTS = 2; + + void to_json(json& j, const ImgScale& p) { switch (p) { case ImgScale::Stretch: @@ -237,7 +240,7 @@ void ThemesInit() { } } ThemesReadDataFromFile("custom_themes.json"); - for (int i=1; i <= 4; ++i) { + for (int i=1; i <= NUMBER_OF_CUSTOM_SLOTS; ++i) { ThemesAddCustomSlot(themes, i); } ThemesDumpData(); @@ -250,7 +253,18 @@ Theme ThemesGetNext() { return themes.at(current_theme); } -Theme ThemesGet(size_t theme_number) { +Theme& ThemesGet(size_t theme_number) { ThemesInit(); return themes.at(theme_number % themes.size()); } + +size_t ThemeGetNumber(const std::string& name) { + size_t ret = 0; // Default to 0 + ThemesInit(); + for (size_t i = 0; i < themes.size(); ++i) { + if (themes[i].theme_name == name) { + ret = i; + } + } + return ret; +} diff --git a/source/code/themes.hpp b/source/code/themes.hpp index 0c471f4..d4a1667 100644 --- a/source/code/themes.hpp +++ b/source/code/themes.hpp @@ -70,12 +70,18 @@ Theme ThemesGetNext(); /** * @brief getTheme returns a specific theme * @param theme_number - * @return + * @return A reference to the theme. The referance is valid until the next call to a "Themes*" function */ -Theme ThemesGet(size_t theme_number); +Theme& ThemesGet(size_t theme_number); /** * @brief Saves the themes starting with "custom_slot_" to "custom_themes.json" * */ void ThemesSaveCustomSlots(); + +/** + * Returns the index of a given file. + * Returns 0 if it does not exist or is the standard theme. +*/ +size_t ThemeGetNumber(const std::string& name);