game/mario/scenes/text_scene.cpp
zv0n 9402f2afa2
Some checks reported errors
continuous-integration/drone/push Build encountered an error
Teleportation in editor
2022-11-19 22:53:24 +01:00

216 lines
7.9 KiB
C++

#include "SDL2/SDL_keycode.h"
#include "editor_scenes.hpp"
#include "../../sdlpp/sdlpp.hpp"
#include "../../sdlpp/sdlpp_mouse.hpp"
#include "../global_vars.hpp"
#include "../objectids.hpp"
#include "../editor_visitor.hpp"
bool __update_scenes_text_dialog = false;
bool __quit_scenes_text_dialog = false;
bool __started_text_dialog = false;
bool __update_text = false;
uint64_t __cur_button_index_text_dialog = -1;
uint64_t __cur_button_index_text_dialog_down = -1;
std::vector<std::shared_ptr<Button>> __buttons_text_dialog{};
std::shared_ptr<SDLPP::RectangleRender> __mouse_text_dialog{};
std::shared_ptr<SDLPP::TextRenderer> __level_name_object{};
std::string __level_name_text = "LEVEL NAME";
std::function<void(const std::string &)> __text_dialog_finalizer;
void __quitTextDialog() {
__quit_scenes_text_dialog = true;
}
void __quitCallback_TextDialog(void * /*UNUSED*/, Button * /*UNUSED*/) {
__quitTextDialog();
}
void __handleKeyUp_TextDialog(SDL_Keycode key, SDLPP::Scene & /*UNUSED*/) {
switch (key) {
case SDLK_ESCAPE:
__level_name_text = "";
// fallthrough
case SDLK_RETURN:
__quitTextDialog();
break;
default:
break;
}
}
std::shared_ptr<SDLPP::Scene>
createSceneTextDialog(std::shared_ptr<SDLPP::Renderer> &renderer) {
auto scene = std::make_shared<SDLPP::Scene>(renderer);
auto bg = std::make_shared<SDLPP::RectangleRender>(0, 0, 10, 10, renderer,
"#00000088", true);
bg->setPermanent();
bg->setId(1);
scene->addObject(bg);
__mouse_text_dialog =
std::make_shared<SDLPP::RectangleRender>(0.01, 0.01, 0, 0, renderer);
__mouse_text_dialog->setMinWidth(1);
__mouse_text_dialog->setMinHeight(1);
__mouse_text_dialog->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
__mouse_text_dialog->setId(EDITOR_MOUSE_ID);
__mouse_text_dialog->setColiderColor("#00FF00");
__mouse_text_dialog->addCollision(SDLPP::RectColider({ 0, 0 }, { 1, 1 }));
scene->addObject(__mouse_text_dialog);
ButtonConfig default_button_theme{};
default_button_theme.bg_color = "#FFFFFF88";
default_button_theme.bg_color_highlight = "#FFFFFFBB";
default_button_theme.bg_color_disabled = "#AAAAAA88";
default_button_theme.font_color = "#000000";
default_button_theme.font_color_highlight = "#000000";
default_button_theme.font_color_disabled = "#555555";
default_button_theme.font_outline_color = "#FFFFFF88";
default_button_theme.font_outline_color_highlight = "#FFFFFFAA";
default_button_theme.font_outline_color_disabled = "#787878";
default_button_theme.outline = 0.1;
// buttons
__buttons_text_dialog.emplace_back(std::make_shared<Button>(
0.3, 0.7, 0.4, 0.2, renderer, "OK", default_button_theme,
__quitCallback_TextDialog, nullptr));
__buttons_text_dialog.back()->setAlignment(SDLPP::OBJ_CENTER,
SDLPP::OBJ_CENTER);
__buttons_text_dialog.back()->setPermanent();
__buttons_text_dialog.back()->setButtonIndex(__buttons_text_dialog.size() - 1);
__buttons_text_dialog.back()->setHighlight();
for (auto &button : __buttons_text_dialog) {
scene->addObject(button);
}
auto font_config = std::make_shared<SDLPP::FontConfiguration>(
g_text_config->getFont(), "#000000", "#282828", 0.05);
// level name
__level_name_object = std::make_shared<SDLPP::TextRenderer>(
0.1, 0.4, 0.8, 0.2, renderer, __level_name_text,
font_config, SDLPP_TEXT_LEFT);
__level_name_object->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
__level_name_object->setPermanent();
__level_name_object->setColor("#FFFFFF88");
scene->addObject(__level_name_object);
return scene;
}
void __resetGlobals_TextDialog() {
__update_scenes_text_dialog = false;
__quit_scenes_text_dialog = false;
__started_text_dialog = false;
__cur_button_index_text_dialog = -1;
__cur_button_index_text_dialog_down = -1;
__buttons_text_dialog.clear();
__mouse_text_dialog.reset();
__level_name_object.reset();
__level_name_text = "LEVEL NAME";
}
void __additionalRender_TextDialog(std::shared_ptr<SDLPP::Scene> & /*UNUSED*/) {
if (__update_text) {
__level_name_object->changeText(__level_name_text);
__update_text = false;
__update_scenes_text_dialog = true;
}
if (__update_scenes_text_dialog) {
for (auto &_scene : game_scenes) {
_scene.scene->updateSizeAndPosition();
}
if (__started_text_dialog) {
__update_scenes_text_dialog = false;
} else {
__started_text_dialog = true;
}
}
if (__quit_scenes_text_dialog) {
SDL_StopTextInput();
if(!__level_name_text.empty() &&
(__level_name_text.size() < 7 || __level_name_text.substr(__level_name_text.size() - 7) != ".marmap")) {
__level_name_text += ".marmap";
}
__text_dialog_finalizer(__level_name_text);
game_scenes.pop_back();
__resetGlobals_TextDialog();
}
}
void __getMousePositionFlags_TextDialog(SDLPP::Scene &scene) {
auto mouse = scene.getObjects({ EDITOR_MOUSE_ID })[0];
// move mouse colider to mouse position
mouse->setPos(SDLPP::Mouse::getMousePositionDouble(
scene.getRenderer(), SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER));
MouseVisitor visitor;
scene.visitCollisions(*mouse, visitor);
__cur_button_index_text_dialog = visitor.getCurButton();
}
bool controlOrCommandPressed();
void __pollEvents_TextDialog(std::shared_ptr<SDLPP::Scene> &scene) {
SDL_Event event;
while (SDLPP::getSDLEvent(event)) {
switch (event.type) {
case SDL_QUIT:
__quitTextDialog();
break;
case SDL_KEYUP:
__handleKeyUp_TextDialog(event.key.keysym.sym, *scene);
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_BACKSPACE &&
!__level_name_text.empty()) {
__level_name_text.pop_back();
__update_text = true;
} 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()) {
// handle paste
__level_name_text += SDL_GetClipboardText();
__update_text = true;
}
break;
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
__update_scenes_text_dialog = true;
}
break;
case SDL_MOUSEMOTION:
__getMousePositionFlags_TextDialog(*scene);
break;
case SDL_MOUSEBUTTONUP:
if (__cur_button_index_text_dialog_down ==
__cur_button_index_text_dialog &&
__cur_button_index_text_dialog != (uint64_t)-1) {
__buttons_text_dialog[__cur_button_index_text_dialog]
->performFunction();
}
break;
case SDL_MOUSEBUTTONDOWN:
// store current mouse flags in previous mouse flags
__cur_button_index_text_dialog_down = __cur_button_index_text_dialog;
break;
case SDL_TEXTINPUT:
__level_name_text += event.text.text;
__update_text = true;
break;
default:
break;
}
}
}
SceneStruct createTextScene(std::shared_ptr<SDLPP::Renderer> renderer,
std::function<void(const std::string &)> finalizer) {
__text_dialog_finalizer = finalizer;
SceneStruct ret{};
ret.scene = createSceneTextDialog(renderer);
ret.additionalRender = __additionalRender_TextDialog;
ret.doInput = __pollEvents_TextDialog;
__update_scenes_text_dialog = true;
SDL_StartTextInput();
return ret;
}