Mario: added Yes/No dialog
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
zv0n 2022-07-22 12:23:16 +02:00
parent 2a2467ccc2
commit fa60a0f163
5 changed files with 237 additions and 6 deletions

View File

@ -90,6 +90,7 @@ target_sources(editor
PRIVATE editor_visitor.cpp PRIVATE editor_visitor.cpp
PRIVATE scenes/editor_main.cpp PRIVATE scenes/editor_main.cpp
PRIVATE scenes/editor_main_menu.cpp PRIVATE scenes/editor_main_menu.cpp
PRIVATE scenes/yes_no_scene.cpp
) )
target_compile_definitions(editor PUBLIC EDITOR) target_compile_definitions(editor PUBLIC EDITOR)

View File

@ -94,9 +94,13 @@ int main() {
SDL_framerateDelay(&gFPS); SDL_framerateDelay(&gFPS);
SDL_PumpEvents(); SDL_PumpEvents();
std::lock_guard<std::mutex> lock(render_mutex); std::lock_guard<std::mutex> lock(render_mutex);
for(auto &scene : game_scenes) { for (size_t i = 0; i < game_scenes.size(); i++) {
scene.additionalRender(scene.scene); game_scenes[i].additionalRender(game_scenes[i].scene);
scene.scene->renderScene(false); // additional renderer can remove scene from game_scenes, better
// check
if (i < game_scenes.size()) {
game_scenes[i].scene->renderScene(false);
}
} }
renderer->presentRenderer(); renderer->presentRenderer();
frames++; frames++;

View File

@ -1025,9 +1025,17 @@ void populateWorldType(
scene->addObject(tool_text); scene->addObject(tool_text);
} }
void dialogFinishedTest(bool result) {
global_test_text_text = result ? "YEAH, BOI!" : "Nah, boi";
setFlag(TEXT_UPDATE_FLAG);
}
void testButtonFunc(void * /*UNUSED*/, Button * /*UNUSED*/) { void testButtonFunc(void * /*UNUSED*/, Button * /*UNUSED*/) {
setFlag(TEXT_INPUT_FLAG); game_scenes.push_back(
SDL_StartTextInput(); createYesNoScene(game_scenes.back().scene->getRendererShared(), "Yeah?",
dialogFinishedTest));
/* setFlag(TEXT_INPUT_FLAG);
SDL_StartTextInput();*/
} }
void openMapEditor(std::shared_ptr<SDLPP::Scene> &scene, void openMapEditor(std::shared_ptr<SDLPP::Scene> &scene,
@ -1312,7 +1320,7 @@ createEditorMainScene(std::shared_ptr<SDLPP::Renderer> &renderer) {
left_character_input.other_button = global_vars.buttons.back(); left_character_input.other_button = global_vars.buttons.back();
global_vars.buttons.emplace_back(std::make_shared<Button>( global_vars.buttons.emplace_back(std::make_shared<Button>(
0, 0, 0.2, 0.2, renderer, "Write Button", default_button_theme, 0, 0, 0.2, 0.2, renderer, "Test Button", default_button_theme,
testButtonFunc, nullptr)); testButtonFunc, nullptr));
global_vars.buttons.back()->setPermanent(); global_vars.buttons.back()->setPermanent();
global_vars.buttons.back()->setButtonIndex(global_vars.buttons.size() - 1); global_vars.buttons.back()->setButtonIndex(global_vars.buttons.size() - 1);

View File

@ -22,5 +22,9 @@ SceneStruct
createEditorMainMenuScene(std::shared_ptr<SDLPP::Renderer> &renderer); createEditorMainMenuScene(std::shared_ptr<SDLPP::Renderer> &renderer);
SceneStruct SceneStruct
createEditorFileChoiceScene(std::shared_ptr<SDLPP::Renderer> &renderer); createEditorFileChoiceScene(std::shared_ptr<SDLPP::Renderer> &renderer);
// TODO move to a shared header
SceneStruct createYesNoScene(std::shared_ptr<SDLPP::Renderer> renderer,
const std::string &text,
std::function<void(bool)> finalizer);
#endif #endif

View File

@ -0,0 +1,214 @@
#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_yesno_dialog = false;
bool __quit_scenes_yesno_dialog = false;
bool __started_yesno_dialog = false;
uint64_t __cur_button_index_yesno_dialog = -1;
uint64_t __cur_button_index_yesno_dialog_down = -1;
std::vector<std::shared_ptr<Button>> __buttons_yesno_dialog{};
std::shared_ptr<SDLPP::RectangleRender> __mouse_yesno_dialog{};
std::function<void(bool)> __yesno_dialog_finalizer;
void __quitYesNoDialog(bool result = false) {
__yesno_dialog_finalizer(result);
__quit_scenes_yesno_dialog = true;
}
void __updateSelectedButton_YesNoDialog(uint64_t new_index) {
if (new_index != __cur_button_index_yesno_dialog &&
new_index != (uint64_t)-1) {
__buttons_yesno_dialog[new_index]->setHighlight();
if (__cur_button_index_yesno_dialog != (uint64_t)-1) {
__buttons_yesno_dialog[__cur_button_index_yesno_dialog]
->unsetHighlight();
}
__cur_button_index_yesno_dialog = new_index;
}
}
void __handleKeyUp_YesNoDialog(SDL_Keycode key, SDLPP::Scene & /*UNUSED*/) {
switch (key) {
case SDLK_ESCAPE:
__quitYesNoDialog();
break;
case SDLK_LEFT:
case SDLK_a:
__updateSelectedButton_YesNoDialog(0);
break;
case SDLK_RIGHT:
case SDLK_d:
__updateSelectedButton_YesNoDialog(1);
break;
case SDLK_RETURN:
__quitYesNoDialog(__cur_button_index_yesno_dialog == 0);
break;
// TODO return chooses selected button
default:
break;
}
}
void __returnYesCallback_YesNoDialog(void * /*UNUSED*/, Button * /*UNUSED*/) {
__quitYesNoDialog(true);
}
void __returnNoCallback_YesNoDialog(void * /*UNUSED*/, Button * /*UNUSED*/) {
__quitYesNoDialog(false);
}
std::shared_ptr<SDLPP::Scene>
createSceneYesNoDialog(std::shared_ptr<SDLPP::Renderer> &renderer,
const std::string &text) {
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_yesno_dialog =
std::make_shared<SDLPP::RectangleRender>(0.01, 0.01, 0, 0, renderer);
__mouse_yesno_dialog->setMinWidth(1);
__mouse_yesno_dialog->setMinHeight(1);
__mouse_yesno_dialog->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
__mouse_yesno_dialog->setId(EDITOR_MOUSE_ID);
__mouse_yesno_dialog->setColiderColor("#00FF00");
__mouse_yesno_dialog->addCollision(SDLPP::RectColider({ 0, 0 }, { 1, 1 }));
scene->addObject(__mouse_yesno_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_yesno_dialog.emplace_back(std::make_shared<Button>(
0.1, 0.5, 0.4, 0.2, renderer, "YES", default_button_theme,
__returnYesCallback_YesNoDialog, nullptr));
__buttons_yesno_dialog.back()->setAlignment(SDLPP::OBJ_CENTER,
SDLPP::OBJ_CENTER);
__buttons_yesno_dialog.back()->setPermanent();
__buttons_yesno_dialog.back()->setButtonIndex(
__buttons_yesno_dialog.size() - 1);
__buttons_yesno_dialog.emplace_back(std::make_shared<Button>(
0.55, 0.5, 0.4, 0.2, renderer, "NO", default_button_theme,
__returnNoCallback_YesNoDialog, nullptr));
__buttons_yesno_dialog.back()->setAlignment(SDLPP::OBJ_CENTER,
SDLPP::OBJ_CENTER);
__buttons_yesno_dialog.back()->setPermanent();
__buttons_yesno_dialog.back()->setButtonIndex(
__buttons_yesno_dialog.size() - 1);
for (auto &button : __buttons_yesno_dialog) {
scene->addObject(button);
}
auto font_config = std::make_shared<SDLPP::FontConfiguration>(
g_text_config->getFont(), "#000000", "#282828", 0.05);
auto dialog_text = std::make_shared<SDLPP::TextRenderer>(
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->setPermanent();
scene->addObject(dialog_text);
return scene;
}
void __resetGlobals_YesNoDialog() {
__update_scenes_yesno_dialog = false;
__quit_scenes_yesno_dialog = false;
__started_yesno_dialog = false;
__cur_button_index_yesno_dialog = -1;
__cur_button_index_yesno_dialog_down = -1;
__buttons_yesno_dialog.clear();
__mouse_yesno_dialog.reset();
}
void __additionalRender_YesNoDialog(
std::shared_ptr<SDLPP::Scene> & /*UNUSED*/) {
if (__update_scenes_yesno_dialog) {
for (auto &_scene : game_scenes) {
_scene.scene->updateSizeAndPosition();
}
if (__started_yesno_dialog) {
__update_scenes_yesno_dialog = false;
} else {
__started_yesno_dialog = true;
}
}
if (__quit_scenes_yesno_dialog) {
game_scenes.pop_back();
__resetGlobals_YesNoDialog();
}
}
void __getMousePositionFlags_YesNoDialog(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);
__updateSelectedButton_YesNoDialog(visitor.getCurButton());
}
void __pollEvents_YesNoDialog(std::shared_ptr<SDLPP::Scene> &scene) {
SDL_Event event;
while (SDLPP::getSDLEvent(event)) {
switch (event.type) {
case SDL_QUIT:
__quitYesNoDialog();
break;
case SDL_KEYUP:
__handleKeyUp_YesNoDialog(event.key.keysym.sym, *scene);
break;
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
__update_scenes_yesno_dialog = true;
}
break;
case SDL_MOUSEMOTION:
__getMousePositionFlags_YesNoDialog(*scene);
break;
case SDL_MOUSEBUTTONUP:
if (__cur_button_index_yesno_dialog_down ==
__cur_button_index_yesno_dialog &&
__cur_button_index_yesno_dialog != (uint64_t)-1) {
__buttons_yesno_dialog[__cur_button_index_yesno_dialog]
->performFunction();
}
break;
case SDL_MOUSEBUTTONDOWN:
// store current mouse flags in previous mouse flags
__cur_button_index_yesno_dialog_down =
__cur_button_index_yesno_dialog;
break;
default:
break;
}
}
}
SceneStruct createYesNoScene(std::shared_ptr<SDLPP::Renderer> renderer,
const std::string &text,
std::function<void(bool)> finalizer) {
__yesno_dialog_finalizer = finalizer;
SceneStruct ret{};
ret.scene = createSceneYesNoDialog(renderer, text);
ret.additionalRender = __additionalRender_YesNoDialog;
ret.doInput = __pollEvents_YesNoDialog;
__update_scenes_yesno_dialog = true;
__updateSelectedButton_YesNoDialog(1);
return ret;
}