Editor: testing out text input
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
zvon 2022-07-21 20:08:16 +02:00
parent 4414ee6ff7
commit 3a80fab3dd

View File

@ -11,6 +11,7 @@
#include "../editor_visitor.hpp"
#include "../tool_box.hpp"
#include "../blocks/coineditorblock.hpp"
#include <SDL2/SDL_keycode.h>
#include <array>
#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<SDLPP::Scene> editorScene;
SceneStruct mainMenuScene;
//SceneStruct fileChoiceScene;
std::shared_ptr<SDLPP::TextRenderer> global_test_text{};
std::string global_test_text_text{};
void openMapEditor(std::shared_ptr<SDLPP::Scene> &scene, const std::string &filename);
void setFlag(uint64_t flag) {
@ -773,7 +779,29 @@ void pollEvents(std::shared_ptr<SDLPP::Scene> &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<SDLPP::Scene> &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<int*>(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<SDLPP::Scene> &scene, const std::string &filename) {
@ -1032,6 +1063,13 @@ void openMapEditor(std::shared_ptr<SDLPP::Scene> &scene, const std::string &file
createGrid(BLOCK_SIZE, 1 - (MAP_HEIGHT + 3) * BLOCK_SIZE, OVERWORLD_WIDTH,
2, scene);
global_test_text = std::make_shared<SDLPP::TextRenderer>(
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<SDLPP::Scene> createEditorMainScene(std::shared_ptr<SDLPP::Rende
toolMoveUpdateButtons(right_character_input.other_button.get(), global_vars.buttons.back().get(), global_vars.tool.cur_page_characters, global_vars.tool.max_page_characters, false);
left_character_input.other_button = global_vars.buttons.back();
global_vars.buttons.emplace_back(std::make_shared<Button>(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<SDLPP::Scene> &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<SDLPP::Renderer> &renderer) {