From 45ab92c31f61fb1f355f63dcd61f3f83d4b4e40d Mon Sep 17 00:00:00 2001 From: zv0n Date: Sat, 24 Sep 2022 20:33:15 +0200 Subject: [PATCH] Editor: warn if map isn't saved when quitting --- mario/scenes/editor_main.cpp | 47 +++++++++++++++++++++++-------- mario/scenes/editor_main_menu.cpp | 33 ++++++++++++++-------- mario/scenes/editor_scenes.hpp | 3 +- mario/scenes/yes_no_scene.cpp | 2 +- 4 files changed, 59 insertions(+), 26 deletions(-) diff --git a/mario/scenes/editor_main.cpp b/mario/scenes/editor_main.cpp index 306eff2..6ac0c0c 100644 --- a/mario/scenes/editor_main.cpp +++ b/mario/scenes/editor_main.cpp @@ -15,6 +15,7 @@ #include #include #include "../filesystem.hpp" +#include "shared_scenes.hpp" #ifdef _WIN32 #include "../sdlpp/SDL2/SDL2_framerate.h" @@ -28,7 +29,8 @@ #define MAP_WIDTH 24 #define MAP_HEIGHT 16 -#define QUIT_FLAG 0x00000001 +bool quit_editor = false; + #define UPDATE_FLAG 0x00000002 #define MAP_LEFT_ENABLED_FLAG 0x00000004 #define MAP_RIGHT_ENABLED_FLAG 0x00000008 @@ -49,6 +51,7 @@ #define OVERWORLD_WIDTH 2 const std::string levelsDir = "levels"; +bool map_changed = false; struct ToolType { enum Value { @@ -148,9 +151,12 @@ void saveMapCallback(void * /*UNUSED*/, Button * /*UNUSED*/) { std::cout << "SAVING" << std::endl; saveMap(levelsDir + FSLib::dir_divisor + level_name_text + ".marmap", global_vars.objects); + map_changed = false; + game_scenes.pop_back(); } void loadMapDialogCallback(const std::string &level_name) { std::cout << "LOADING" << std::endl; + map_changed = false; level_name_text = level_name.substr(0, level_name.length() - 7); setFlag(LOAD_MAP_FLAG); setFlag(TEXT_UPDATE_FLAG); @@ -531,7 +537,6 @@ void handleKeyUp(SDL_Keycode key, SDLPP::Scene &scene) { switch (key) { case SDLK_ESCAPE: std::cout << "Eskape" << std::endl; - // setFlag(QUIT_FLAG); game_scenes.push_back(mainMenuScene); break; case SDLK_a: @@ -703,6 +708,7 @@ MapObject &getSelectedObject() { void placeTool(SDLPP::Scene &scene) { std::lock_guard lock(render_mutex); + map_changed = true; ToolVisitor visitor; visitor.setSourceType(global_vars.current_world_type); @@ -806,7 +812,7 @@ void pollEvents(std::shared_ptr &scene) { while (SDLPP::getSDLEvent(event)) { switch (event.type) { case SDL_QUIT: - setFlag(QUIT_FLAG); + quit_editor = true; break; case SDL_KEYUP: if (!getFlag(TEXT_INPUT_FLAG) && !controlOrCommandPressed()) { @@ -824,15 +830,18 @@ void pollEvents(std::shared_ptr &scene) { !level_name_text.empty()) { level_name_text.pop_back(); setFlag(TEXT_UPDATE_FLAG); - } else if (event.key.keysym.sym == SDLK_c && controlOrCommandPressed()) { + } else if (event.key.keysym.sym == SDLK_c && + controlOrCommandPressed()) { // handle copy SDL_SetClipboardText(level_name_text.c_str()); - } else if (event.key.keysym.sym == SDLK_v && controlOrCommandPressed()) { + } else if (event.key.keysym.sym == SDLK_v && + controlOrCommandPressed()) { // handle paste level_name_text += SDL_GetClipboardText(); setFlag(TEXT_UPDATE_FLAG); } - } else if (event.key.keysym.sym == SDLK_s && controlOrCommandPressed()) { + } else if (event.key.keysym.sym == SDLK_s && + controlOrCommandPressed()) { saveMapCallback(nullptr, nullptr); } break; @@ -1057,10 +1066,11 @@ void openMapEditor(std::shared_ptr &scene, } else { loadMap(scene, global_vars.mario, filename, global_vars.objects, true, MAP_WIDTH); - for(int i = 0; i < global_vars.objects.size(); i++) { - for(int j = 0; j < 16; j++) { - if(global_vars.objects[i][j].hasCharacter() && global_vars.objects[i][j].getCharacterId() == MARIO_ID) { - global_vars.mario_pos = {i,j}; + for (int i = 0; i < global_vars.objects.size(); i++) { + for (int j = 0; j < 16; j++) { + if (global_vars.objects[i][j].hasCharacter() && + global_vars.objects[i][j].getCharacterId() == MARIO_ID) { + global_vars.mario_pos = { i, j }; } } } @@ -1342,8 +1352,21 @@ createEditorMainScene(std::shared_ptr &renderer) { return scene; } +void quitEditor(bool actually_quit) { + g_quit = actually_quit; +} + void editorAdditionalRender(std::shared_ptr &scene) { - g_quit = getFlag(QUIT_FLAG); + if (quit_editor) { + if (map_changed) { + game_scenes.push_back(createYesNoScene( + scene->getRendererShared(), + "Map hasn't been saved. Actually quit?", quitEditor)); + quit_editor = false; + } else { + quitEditor(true); + } + } for (auto &button : global_vars.buttons) { button->update(); } @@ -1377,7 +1400,7 @@ SceneStruct createEditorScene(std::shared_ptr &renderer) { ret.additionalRender = editorAdditionalRender; editorScene = ret.scene; - mainMenuScene = createEditorMainMenuScene(renderer); + mainMenuScene = createEditorMainMenuScene(renderer, &quit_editor); // fileChoiceScene = createEditorFileChoiceScene(renderer); return ret; } diff --git a/mario/scenes/editor_main_menu.cpp b/mario/scenes/editor_main_menu.cpp index 584a436..3e0da1b 100644 --- a/mario/scenes/editor_main_menu.cpp +++ b/mario/scenes/editor_main_menu.cpp @@ -8,13 +8,19 @@ bool __update_scenes_main_menu = false; bool __quit_scenes_main_menu = false; bool __started_main_menu = false; +bool *__quit_flag = nullptr; uint64_t __cur_button_index_main_menu = -1; uint64_t __cur_button_index_main_menu_down = -1; std::vector> __buttons_main_menu{}; std::shared_ptr __mouse_main_menu{}; void quitMainMenu() { - g_quit = true; + game_scenes.pop_back(); + if (__quit_flag != nullptr) { + *__quit_flag = true; + } else { + g_quit = true; + } } void resumeMainMenu() { @@ -34,9 +40,10 @@ void loadFinished(const std::string level_name) { loadMapDialogCallback(level_name); } -void showLoadMenu(void */*UNUSED*/, Button */*UNUSED*/) { +void showLoadMenu(void * /*UNUSED*/, Button * /*UNUSED*/) { // TODO levels - auto loadMenu = createLoadScene(__buttons_main_menu.back()->getRenderer(), "levels", loadFinished); + auto loadMenu = createLoadScene(__buttons_main_menu.back()->getRenderer(), + "levels", loadFinished); game_scenes.push_back(loadMenu); } @@ -46,8 +53,7 @@ void __updateSelectedButton_MainMenu(uint64_t new_index) { __buttons_main_menu[new_index]->setHighlight(); if (__cur_button_index_main_menu != (uint64_t)-1) { - __buttons_main_menu[__cur_button_index_main_menu] - ->unsetHighlight(); + __buttons_main_menu[__cur_button_index_main_menu]->unsetHighlight(); } __cur_button_index_main_menu = new_index; } @@ -67,7 +73,7 @@ void handleKeyUpMainMenu(SDL_Keycode key, SDLPP::Scene & /*UNUSED*/) { break; case SDLK_DOWN: case SDLK_s: - if(__cur_button_index_main_menu == __buttons_main_menu.size() - 1) { + if (__cur_button_index_main_menu == __buttons_main_menu.size() - 1) { __updateSelectedButton_MainMenu(0); } else { __updateSelectedButton_MainMenu(__cur_button_index_main_menu + 1); @@ -75,14 +81,15 @@ void handleKeyUpMainMenu(SDL_Keycode key, SDLPP::Scene & /*UNUSED*/) { break; case SDLK_UP: case SDLK_w: - if(__cur_button_index_main_menu == 0) { + if (__cur_button_index_main_menu == 0) { __updateSelectedButton_MainMenu(__buttons_main_menu.size() - 1); } else { __updateSelectedButton_MainMenu(__cur_button_index_main_menu - 1); } break; case SDLK_RETURN: - if(__cur_button_index_main_menu >= 0 && __cur_button_index_main_menu < __buttons_main_menu.size()) { + if (__cur_button_index_main_menu >= 0 && + __cur_button_index_main_menu < __buttons_main_menu.size()) { __buttons_main_menu[__cur_button_index_main_menu] ->performFunction(); } @@ -129,9 +136,9 @@ createSceneMainMenu(std::shared_ptr &renderer) { SDLPP::OBJ_CENTER); __buttons_main_menu.back()->setPermanent(); __buttons_main_menu.back()->setButtonIndex(__buttons_main_menu.size() - 1); - __buttons_main_menu.emplace_back(std::make_shared