From 24025673304da1b17ed6d2d2ba6e8afb9f43b021 Mon Sep 17 00:00:00 2001 From: zv0n Date: Tue, 21 Jun 2022 14:52:36 +0200 Subject: [PATCH] Added generic button --- mario/CMakeLists.txt | 5 +++++ mario/editor.cpp | 17 ++++++++++++++++ mario/editor_visitor.cpp | 9 +++++++++ mario/editor_visitor.hpp | 6 ++++++ mario/gui/gui.cpp | 12 ++++++++++++ mario/gui/gui.hpp | 42 ++++++++++++++++++++++++++++++++++++++++ mario/maploader.cpp | 10 ++++++---- mario/objectids.hpp | 3 +++ 8 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 mario/gui/gui.cpp create mode 100644 mario/gui/gui.hpp diff --git a/mario/CMakeLists.txt b/mario/CMakeLists.txt index 53fcc49..06948d6 100644 --- a/mario/CMakeLists.txt +++ b/mario/CMakeLists.txt @@ -22,12 +22,15 @@ list(APPEND CommonFiles blocks/simpleblocks.cpp mario.cpp blocks.cpp + gui/gui.cpp ) list(APPEND SDLLibs sdlpp) if(WIN32) + list(APPEND CommonFiles + filesystem/windows/filesystem.cpp) list(APPEND CommonFiles ../sdlpp/SDL2/SDL2_framerate.c ../sdlpp/SDL2/SDL2_gfxPrimitives.c @@ -51,6 +54,8 @@ if(WIN32) SDL2_ttf_m ) else() + list(APPEND CommonFiles + filesystem/unix/filesystem.cpp) add_executable(mario) add_executable(editor) list(APPEND SDLLibs diff --git a/mario/editor.cpp b/mario/editor.cpp index 1fa3b83..bee0759 100644 --- a/mario/editor.cpp +++ b/mario/editor.cpp @@ -1,4 +1,5 @@ #include "../sdlpp/sdlpp.hpp" +#include "gui/gui.hpp" #include "sprites.hpp" #ifdef _WIN32 #include "../sdlpp/SDL2/SDL2_framerate.h" @@ -56,6 +57,7 @@ struct MouseInfo { SDLPP::Vec2D edit_box; SDLPP::Vec2D tool_box; ToolType::Value tool_type{}; + const Button *cur_button{}; }; struct MapInfo { @@ -491,6 +493,7 @@ void getMousePositionFlags(SDLPP::Scene &scene) { MouseVisitor visitor; scene.visitCollisions(*mouse, visitor); + global_vars.mouse.cur_button = visitor.getCurButton(); global_vars.mouse.cur_flags = visitor.getFlags(); // + 1 because the left map arrow is on position 0 global_vars.mouse.edit_box = @@ -556,6 +559,9 @@ void mouseUpAction(uint64_t flags, SDLPP::Scene &scene) { updateToolSelection(global_vars.tool.cur_page_characters - 1, ToolType::CHARACTER); } + if(MouseVisitor::button(flags)) { + global_vars.mouse.cur_button->performFunction(); + } } SDLPP::Vec2D getSelectedObjectIndexes() { @@ -948,12 +954,18 @@ void checkArrowsEnabled(uint64_t cur_page, uint64_t max_page, } } +void testButtonFunc(void *input) { + auto actual = static_cast(input); + std::cout << "NUM: " << *actual << std::endl; +} + #ifdef _WIN32 int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR szCmdLine, int nCmdShow) { #else int main() { #endif + int num = 0; SDLPP::init(); SDLPP::Window w("Mario editor!"); w.setResizable(true); @@ -1125,6 +1137,10 @@ int main() { updateToolSelection(0, ToolType::CHARACTER); setToolColor(); + auto button = createButton(0, 0, 0.2, 0.2, renderer, "CLICK ME", "#FFFFFF", "#000000", "#FF0088", testButtonFunc, &num); + button->setStatic(); + scene->addObject(button); + auto base = SDL_GetTicks(); int frames = 0; std::thread inputThread(doInput, scene); @@ -1146,6 +1162,7 @@ int main() { setFlag(UPDATE_FLAG); while (!getFlag(QUIT_FLAG)) { + num = (num + 1) % 256; SDL_framerateDelay(&gFPS); SDL_PumpEvents(); std::lock_guard lock(destruction_mutex); diff --git a/mario/editor_visitor.cpp b/mario/editor_visitor.cpp index ab4e1f4..3794e16 100644 --- a/mario/editor_visitor.cpp +++ b/mario/editor_visitor.cpp @@ -4,6 +4,7 @@ #include "objectids.hpp" #include "edit_box.hpp" #include "tool_box.hpp" +#include #define SELECTED_LEFT_MAP 0x00000001 #define SELECTED_RIGHT_MAP 0x00000002 @@ -15,6 +16,7 @@ #define SELECTED_LEFT_MOD 0x00000080 #define SELECTED_RIGHT_CHARACTER 0x00000100 #define SELECTED_LEFT_CHARACTER 0x00000200 +#define SELECTED_BUTTON 0x00000400 void MouseVisitor::visit(const SDLPP::RenderObject &obj) { auto id = obj.getId(); @@ -52,6 +54,10 @@ void MouseVisitor::visit(const SDLPP::RenderObject &obj) { tool_box_location = dynamic_cast(obj).getIndexes(); tool_box_type = dynamic_cast(obj).getType(); break; + case BUTTON_ID: + select_flags |= SELECTED_BUTTON; + cur_button = reinterpret_cast(&obj); + break; default: break; } @@ -88,6 +94,9 @@ bool MouseVisitor::moveCharactersLeft(uint64_t flags) { bool MouseVisitor::moveCharactersRight(uint64_t flags) { return flags & SELECTED_RIGHT_CHARACTER; } +bool MouseVisitor::button(uint64_t flags) { + return flags & SELECTED_BUTTON; +} void ToolVisitor::visit(const SDLPP::RenderObject &obj) { auto id = obj.getCollisions()[0]->getId(); diff --git a/mario/editor_visitor.hpp b/mario/editor_visitor.hpp index f4b2a90..06c7c09 100644 --- a/mario/editor_visitor.hpp +++ b/mario/editor_visitor.hpp @@ -4,6 +4,7 @@ #include "../sdlpp/sdlpp_visitor.hpp" #include "../sdlpp/sdlpp_geometry.hpp" #include "blocks.hpp" +#include "gui/gui.hpp" #include struct VisitorType { @@ -46,6 +47,9 @@ public: uint64_t getToolType() const { return tool_box_type; } + const Button *getCurButton() { + return cur_button; + } static bool moveMapLeft(uint64_t flags); static bool moveMapRight(uint64_t flags); @@ -55,6 +59,7 @@ public: static bool moveModsRight(uint64_t flags); static bool moveCharactersLeft(uint64_t flags); static bool moveCharactersRight(uint64_t flags); + static bool button(uint64_t flags); private: uint64_t select_flags = 0; @@ -64,6 +69,7 @@ private: SDLPP::Vec2D tool_box_location = { -1, -1 }; uint64_t _type{}; uint64_t tool_box_type = 0; + const Button *cur_button; }; class ToolVisitor : public SDLPP::Visitor { diff --git a/mario/gui/gui.cpp b/mario/gui/gui.cpp new file mode 100644 index 0000000..e2bbb8a --- /dev/null +++ b/mario/gui/gui.cpp @@ -0,0 +1,12 @@ +#include "gui.hpp" + +std::shared_ptr +createButton(double x, double y, double w, double h, + std::shared_ptr &r, const std::string &text, + const std::string &color, const std::string &outline_color, + const std::string &background_color, + std::function click_fun, void *input) { + auto button = std::make_shared