diff --git a/mario/scenes/editor_main.cpp b/mario/scenes/editor_main.cpp index 953fe75..d71b575 100644 --- a/mario/scenes/editor_main.cpp +++ b/mario/scenes/editor_main.cpp @@ -11,6 +11,7 @@ #include "../editor_visitor.hpp" #include "../tool_box.hpp" #include "../blocks/coineditorblock.hpp" +#include #include #ifdef _WIN32 @@ -36,6 +37,8 @@ #define CHARACTER_LEFT_ENABLED_FLAG 0x00000100 #define CHARACTER_RIGHT_ENABLED_FLAG 0x00000200 #define LOAD_MAP_FLAG 0x00000400 +#define TEXT_UPDATE_FLAG 0x00000800 +#define TEXT_INPUT_FLAG 0x00001000 #define TOOLS_WIDTH 4 #define CHARACTER_WIDTH 3 @@ -120,6 +123,9 @@ std::shared_ptr editorScene; SceneStruct mainMenuScene; //SceneStruct fileChoiceScene; +std::shared_ptr global_test_text{}; +std::string global_test_text_text{}; + void openMapEditor(std::shared_ptr &scene, const std::string &filename); void setFlag(uint64_t flag) { @@ -773,7 +779,29 @@ void pollEvents(std::shared_ptr &scene) { setFlag(QUIT_FLAG); break; case SDL_KEYUP: - handleKeyUp(event.key.keysym.sym, *scene); + if(!getFlag(TEXT_INPUT_FLAG)) { + handleKeyUp(event.key.keysym.sym, *scene); + } else { + if ( event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_RETURN ) { + SDL_StopTextInput(); + unsetFlag(TEXT_INPUT_FLAG); + } + } + break; + case SDL_KEYDOWN: + if(getFlag(TEXT_INPUT_FLAG)) { + if( event.key.keysym.sym == SDLK_BACKSPACE && !global_test_text_text.empty() ) { + global_test_text_text.pop_back(); + setFlag(TEXT_UPDATE_FLAG); + } else if( event.key.keysym.sym == SDLK_c && SDL_GetModState() & KMOD_CTRL ) { + // handle copy + SDL_SetClipboardText( global_test_text_text.c_str() ); + } else if( event.key.keysym.sym == SDLK_v && SDL_GetModState() & KMOD_CTRL ) { + // handle paste + global_test_text_text += SDL_GetClipboardText(); + setFlag(TEXT_UPDATE_FLAG); + } + } break; case SDL_WINDOWEVENT: if (event.window.event == SDL_WINDOWEVENT_RESIZED) { @@ -838,6 +866,10 @@ void pollEvents(std::shared_ptr &scene) { global_vars.current_tool->onScrollDown(); } break; + case SDL_TEXTINPUT: + global_test_text_text += event.text.text; + setFlag(TEXT_UPDATE_FLAG); + break; default: break; } @@ -966,10 +998,9 @@ void populateWorldType( scene->addObject(tool_text); } -void testButtonFunc(void *input, Button *caller) { - auto actual = static_cast(input); - std::cout << "NUM: " << *actual << std::endl; - caller->disable(); +void testButtonFunc(void */*UNUSED*/, Button */*UNUSED*/) { + setFlag(TEXT_INPUT_FLAG); + SDL_StartTextInput(); } void openMapEditor(std::shared_ptr &scene, const std::string &filename) { @@ -1032,6 +1063,13 @@ void openMapEditor(std::shared_ptr &scene, const std::string &file createGrid(BLOCK_SIZE, 1 - (MAP_HEIGHT + 3) * BLOCK_SIZE, OVERWORLD_WIDTH, 2, scene); + global_test_text = std::make_shared( + 0.3, 0.05, BLOCK_SIZE*4, BLOCK_SIZE*2, + renderer, "TEST TEXT", font_config, SDLPP_TEXT_CENTER); + global_test_text->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER); + global_test_text->setPermanent(); + scene->addObject(global_test_text); + global_vars.map.max_page = global_vars.objects.size() - MAP_WIDTH; // arrowInputs[1] - rightMapInput, arrowInputs[0] - leftMapInput toolMoveUpdateButtons(arrowInputs[1].other_button.get(), arrowInputs[0].other_button.get(), global_vars.map.cur_page, global_vars.map.max_page, true); @@ -1217,6 +1255,10 @@ std::shared_ptr createEditorMainScene(std::shared_ptr(0, 0, 0.2, 0.2, renderer, "Write Button", default_button_theme, testButtonFunc, nullptr)); + global_vars.buttons.back()->setPermanent(); + global_vars.buttons.back()->setButtonIndex(global_vars.buttons.size() - 1); + openMapEditor(scene, ""); return scene; @@ -1237,6 +1279,11 @@ void editorAdditionalRender(std::shared_ptr &scene) { openMapEditor(editorScene, "test_binary.bin"); unsetFlag(LOAD_MAP_FLAG); } + if (getFlag(TEXT_UPDATE_FLAG)) { + global_test_text->changeText(global_test_text_text); + unsetFlag(TEXT_UPDATE_FLAG); + setFlag(UPDATE_FLAG); + } } SceneStruct createEditorScene(std::shared_ptr &renderer) {