Editor: warn if map isn't saved when quitting
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
zv0n 2022-09-24 20:33:15 +02:00
parent 31f00ed5d0
commit 45ab92c31f
4 changed files with 59 additions and 26 deletions

View File

@ -15,6 +15,7 @@
#include <SDL2/SDL_keycode.h> #include <SDL2/SDL_keycode.h>
#include <array> #include <array>
#include "../filesystem.hpp" #include "../filesystem.hpp"
#include "shared_scenes.hpp"
#ifdef _WIN32 #ifdef _WIN32
#include "../sdlpp/SDL2/SDL2_framerate.h" #include "../sdlpp/SDL2/SDL2_framerate.h"
@ -28,7 +29,8 @@
#define MAP_WIDTH 24 #define MAP_WIDTH 24
#define MAP_HEIGHT 16 #define MAP_HEIGHT 16
#define QUIT_FLAG 0x00000001 bool quit_editor = false;
#define UPDATE_FLAG 0x00000002 #define UPDATE_FLAG 0x00000002
#define MAP_LEFT_ENABLED_FLAG 0x00000004 #define MAP_LEFT_ENABLED_FLAG 0x00000004
#define MAP_RIGHT_ENABLED_FLAG 0x00000008 #define MAP_RIGHT_ENABLED_FLAG 0x00000008
@ -49,6 +51,7 @@
#define OVERWORLD_WIDTH 2 #define OVERWORLD_WIDTH 2
const std::string levelsDir = "levels"; const std::string levelsDir = "levels";
bool map_changed = false;
struct ToolType { struct ToolType {
enum Value { enum Value {
@ -148,9 +151,12 @@ void saveMapCallback(void * /*UNUSED*/, Button * /*UNUSED*/) {
std::cout << "SAVING" << std::endl; std::cout << "SAVING" << std::endl;
saveMap(levelsDir + FSLib::dir_divisor + level_name_text + ".marmap", saveMap(levelsDir + FSLib::dir_divisor + level_name_text + ".marmap",
global_vars.objects); global_vars.objects);
map_changed = false;
game_scenes.pop_back();
} }
void loadMapDialogCallback(const std::string &level_name) { void loadMapDialogCallback(const std::string &level_name) {
std::cout << "LOADING" << std::endl; std::cout << "LOADING" << std::endl;
map_changed = false;
level_name_text = level_name.substr(0, level_name.length() - 7); level_name_text = level_name.substr(0, level_name.length() - 7);
setFlag(LOAD_MAP_FLAG); setFlag(LOAD_MAP_FLAG);
setFlag(TEXT_UPDATE_FLAG); setFlag(TEXT_UPDATE_FLAG);
@ -531,7 +537,6 @@ void handleKeyUp(SDL_Keycode key, SDLPP::Scene &scene) {
switch (key) { switch (key) {
case SDLK_ESCAPE: case SDLK_ESCAPE:
std::cout << "Eskape" << std::endl; std::cout << "Eskape" << std::endl;
// setFlag(QUIT_FLAG);
game_scenes.push_back(mainMenuScene); game_scenes.push_back(mainMenuScene);
break; break;
case SDLK_a: case SDLK_a:
@ -703,6 +708,7 @@ MapObject &getSelectedObject() {
void placeTool(SDLPP::Scene &scene) { void placeTool(SDLPP::Scene &scene) {
std::lock_guard<std::mutex> lock(render_mutex); std::lock_guard<std::mutex> lock(render_mutex);
map_changed = true;
ToolVisitor visitor; ToolVisitor visitor;
visitor.setSourceType(global_vars.current_world_type); visitor.setSourceType(global_vars.current_world_type);
@ -806,7 +812,7 @@ void pollEvents(std::shared_ptr<SDLPP::Scene> &scene) {
while (SDLPP::getSDLEvent(event)) { while (SDLPP::getSDLEvent(event)) {
switch (event.type) { switch (event.type) {
case SDL_QUIT: case SDL_QUIT:
setFlag(QUIT_FLAG); quit_editor = true;
break; break;
case SDL_KEYUP: case SDL_KEYUP:
if (!getFlag(TEXT_INPUT_FLAG) && !controlOrCommandPressed()) { if (!getFlag(TEXT_INPUT_FLAG) && !controlOrCommandPressed()) {
@ -824,15 +830,18 @@ void pollEvents(std::shared_ptr<SDLPP::Scene> &scene) {
!level_name_text.empty()) { !level_name_text.empty()) {
level_name_text.pop_back(); level_name_text.pop_back();
setFlag(TEXT_UPDATE_FLAG); setFlag(TEXT_UPDATE_FLAG);
} else if (event.key.keysym.sym == SDLK_c && controlOrCommandPressed()) { } else if (event.key.keysym.sym == SDLK_c &&
controlOrCommandPressed()) {
// handle copy // handle copy
SDL_SetClipboardText(level_name_text.c_str()); 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 // handle paste
level_name_text += SDL_GetClipboardText(); level_name_text += SDL_GetClipboardText();
setFlag(TEXT_UPDATE_FLAG); 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); saveMapCallback(nullptr, nullptr);
} }
break; break;
@ -1059,7 +1068,8 @@ void openMapEditor(std::shared_ptr<SDLPP::Scene> &scene,
MAP_WIDTH); MAP_WIDTH);
for (int i = 0; i < global_vars.objects.size(); i++) { for (int i = 0; i < global_vars.objects.size(); i++) {
for (int j = 0; j < 16; j++) { for (int j = 0; j < 16; j++) {
if(global_vars.objects[i][j].hasCharacter() && global_vars.objects[i][j].getCharacterId() == MARIO_ID) { if (global_vars.objects[i][j].hasCharacter() &&
global_vars.objects[i][j].getCharacterId() == MARIO_ID) {
global_vars.mario_pos = { i, j }; global_vars.mario_pos = { i, j };
} }
} }
@ -1342,8 +1352,21 @@ createEditorMainScene(std::shared_ptr<SDLPP::Renderer> &renderer) {
return scene; return scene;
} }
void quitEditor(bool actually_quit) {
g_quit = actually_quit;
}
void editorAdditionalRender(std::shared_ptr<SDLPP::Scene> &scene) { void editorAdditionalRender(std::shared_ptr<SDLPP::Scene> &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) { for (auto &button : global_vars.buttons) {
button->update(); button->update();
} }
@ -1377,7 +1400,7 @@ SceneStruct createEditorScene(std::shared_ptr<SDLPP::Renderer> &renderer) {
ret.additionalRender = editorAdditionalRender; ret.additionalRender = editorAdditionalRender;
editorScene = ret.scene; editorScene = ret.scene;
mainMenuScene = createEditorMainMenuScene(renderer); mainMenuScene = createEditorMainMenuScene(renderer, &quit_editor);
// fileChoiceScene = createEditorFileChoiceScene(renderer); // fileChoiceScene = createEditorFileChoiceScene(renderer);
return ret; return ret;
} }

View File

@ -8,14 +8,20 @@
bool __update_scenes_main_menu = false; bool __update_scenes_main_menu = false;
bool __quit_scenes_main_menu = false; bool __quit_scenes_main_menu = false;
bool __started_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 = -1;
uint64_t __cur_button_index_main_menu_down = -1; uint64_t __cur_button_index_main_menu_down = -1;
std::vector<std::shared_ptr<Button>> __buttons_main_menu{}; std::vector<std::shared_ptr<Button>> __buttons_main_menu{};
std::shared_ptr<SDLPP::RectangleRender> __mouse_main_menu{}; std::shared_ptr<SDLPP::RectangleRender> __mouse_main_menu{};
void quitMainMenu() { void quitMainMenu() {
game_scenes.pop_back();
if (__quit_flag != nullptr) {
*__quit_flag = true;
} else {
g_quit = true; g_quit = true;
} }
}
void resumeMainMenu() { void resumeMainMenu() {
__quit_scenes_main_menu = true; __quit_scenes_main_menu = true;
@ -36,7 +42,8 @@ void loadFinished(const std::string level_name) {
void showLoadMenu(void * /*UNUSED*/, Button * /*UNUSED*/) { void showLoadMenu(void * /*UNUSED*/, Button * /*UNUSED*/) {
// TODO levels // 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); game_scenes.push_back(loadMenu);
} }
@ -46,8 +53,7 @@ void __updateSelectedButton_MainMenu(uint64_t new_index) {
__buttons_main_menu[new_index]->setHighlight(); __buttons_main_menu[new_index]->setHighlight();
if (__cur_button_index_main_menu != (uint64_t)-1) { if (__cur_button_index_main_menu != (uint64_t)-1) {
__buttons_main_menu[__cur_button_index_main_menu] __buttons_main_menu[__cur_button_index_main_menu]->unsetHighlight();
->unsetHighlight();
} }
__cur_button_index_main_menu = new_index; __cur_button_index_main_menu = new_index;
} }
@ -82,7 +88,8 @@ void handleKeyUpMainMenu(SDL_Keycode key, SDLPP::Scene & /*UNUSED*/) {
} }
break; break;
case SDLK_RETURN: 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] __buttons_main_menu[__cur_button_index_main_menu]
->performFunction(); ->performFunction();
} }
@ -129,9 +136,9 @@ createSceneMainMenu(std::shared_ptr<SDLPP::Renderer> &renderer) {
SDLPP::OBJ_CENTER); SDLPP::OBJ_CENTER);
__buttons_main_menu.back()->setPermanent(); __buttons_main_menu.back()->setPermanent();
__buttons_main_menu.back()->setButtonIndex(__buttons_main_menu.size() - 1); __buttons_main_menu.back()->setButtonIndex(__buttons_main_menu.size() - 1);
__buttons_main_menu.emplace_back(std::make_shared<Button>( __buttons_main_menu.emplace_back(
0.2, 0.4, 0.6, 0.1, renderer, "LOAD", default_button_theme, std::make_shared<Button>(0.2, 0.4, 0.6, 0.1, renderer, "LOAD",
showLoadMenu, nullptr)); default_button_theme, showLoadMenu, nullptr));
__buttons_main_menu.back()->setAlignment(SDLPP::OBJ_CENTER, __buttons_main_menu.back()->setAlignment(SDLPP::OBJ_CENTER,
SDLPP::OBJ_CENTER); SDLPP::OBJ_CENTER);
__buttons_main_menu.back()->setPermanent(); __buttons_main_menu.back()->setPermanent();
@ -221,12 +228,14 @@ void pollEventsMainMenu(std::shared_ptr<SDLPP::Scene> &scene) {
} }
SceneStruct SceneStruct
createEditorMainMenuScene(std::shared_ptr<SDLPP::Renderer> &renderer) { createEditorMainMenuScene(std::shared_ptr<SDLPP::Renderer> &renderer,
bool *quit_flag) {
SceneStruct ret{}; SceneStruct ret{};
ret.scene = createSceneMainMenu(renderer); ret.scene = createSceneMainMenu(renderer);
ret.additionalRender = additionalRenderMainMenu; ret.additionalRender = additionalRenderMainMenu;
ret.doInput = pollEventsMainMenu; ret.doInput = pollEventsMainMenu;
__update_scenes_main_menu = true; __update_scenes_main_menu = true;
__updateSelectedButton_MainMenu(0); __updateSelectedButton_MainMenu(0);
__quit_flag = quit_flag;
return ret; return ret;
} }

View File

@ -14,7 +14,8 @@ void loadMapDialogCallback(const std::string &level_name);
SceneStruct createEditorScene(std::shared_ptr<SDLPP::Renderer> &renderer); SceneStruct createEditorScene(std::shared_ptr<SDLPP::Renderer> &renderer);
SceneStruct SceneStruct
createEditorMainMenuScene(std::shared_ptr<SDLPP::Renderer> &renderer); createEditorMainMenuScene(std::shared_ptr<SDLPP::Renderer> &renderer,
bool *quit_flag);
SceneStruct SceneStruct
createEditorFileChoiceScene(std::shared_ptr<SDLPP::Renderer> &renderer); createEditorFileChoiceScene(std::shared_ptr<SDLPP::Renderer> &renderer);

View File

@ -114,7 +114,7 @@ createSceneYesNoDialog(std::shared_ptr<SDLPP::Renderer> &renderer,
scene->addObject(button); scene->addObject(button);
} }
auto font_config = std::make_shared<SDLPP::FontConfiguration>( auto font_config = std::make_shared<SDLPP::FontConfiguration>(
g_text_config->getFont(), "#000000", "#282828", 0.05); g_text_config->getFont(), "#000000", "#FFFFFFAA", 0.10);
auto dialog_text = std::make_shared<SDLPP::TextRenderer>( auto dialog_text = std::make_shared<SDLPP::TextRenderer>(
0.1, 0.15, 0.8, 0.3, renderer, text, font_config, SDLPP_TEXT_CENTER); 0.1, 0.15, 0.8, 0.3, renderer, text, font_config, SDLPP_TEXT_CENTER);
dialog_text->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER); dialog_text->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);