diff options
-rw-r--r-- | src/crepe/api/Button.cpp | 4 | ||||
-rw-r--r-- | src/crepe/api/Button.h | 8 | ||||
-rw-r--r-- | src/crepe/system/InputSystem.cpp | 57 | ||||
-rw-r--r-- | src/test/InputTest.cpp | 8 |
4 files changed, 39 insertions, 38 deletions
diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp index a27ff53..d325014 100644 --- a/src/crepe/api/Button.cpp +++ b/src/crepe/api/Button.cpp @@ -2,8 +2,8 @@ namespace crepe { -Button::Button(game_object_id_t id, int width, int height, std::function<void()> on_click, bool is_toggle - ) +Button::Button(game_object_id_t id, int width, int height, std::function<void()> on_click, + bool is_toggle) : UiObject(id, width, height), is_toggle(is_toggle), on_click(on_click) {} diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h index 1410529..18f3def 100644 --- a/src/crepe/api/Button.h +++ b/src/crepe/api/Button.h @@ -24,7 +24,8 @@ public: * \param is_toggle Optional flag to indicate if the button is a toggle button. Defaults to false. * \param on_click callback function that will be invoked when the button is clicked. */ - Button(game_object_id_t id, int width, int height, std::function<void()> on_click, bool is_toggle = false); + Button(game_object_id_t id, int width, int height, std::function<void()> on_click, + bool is_toggle = false); /** * \brief Indicates if the button is a toggle button (can be pressed and released). @@ -41,7 +42,7 @@ public: * function that matches the signature `void()`. */ std::function<void()> on_click; - + /** * \brief Callback function to be executed when the mouse enters the button's boundaries. * @@ -57,7 +58,8 @@ public: * allowing custom actions like resetting visual effects or playing exit-related effects. */ std::function<void()> on_exit; - private: + +private: friend class InputSystem; /** * \brief Indicates whether the button is currently pressed. diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index 4695620..beeef87 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -80,46 +80,45 @@ void InputSystem::update() { } void InputSystem::handle_move(const SDLContext::EventData & event_data) { - ComponentManager & mgr = this->component_manager; + ComponentManager & mgr = this->component_manager; - RefVector<Button> buttons = mgr.get_components_by_type<Button>(); - + RefVector<Button> buttons = mgr.get_components_by_type<Button>(); - for (Button & button : buttons) { - RefVector<Transform> transform_vec = mgr.get_components_by_id<Transform>(button.game_object_id); + for (Button & button : buttons) { + RefVector<Transform> transform_vec + = mgr.get_components_by_id<Transform>(button.game_object_id); OptionalRef<Transform> transform(transform_vec.front().get()); - if (!transform) continue; - - bool was_hovering = button.hover; // Store previous hover state - - // Check if the mouse is inside the button - if (button.active && is_mouse_inside_button(event_data, button, transform)) { - button.hover = true; - - // Trigger the on_enter callback if the hover state just changed to true - if (!was_hovering && button.on_enter) { - button.on_enter(); - } - } else { - button.hover = false; - - // Trigger the on_exit callback if the hover state just changed to false - if (was_hovering && button.on_exit) { - button.on_exit(); - } - } - } -} + if (!transform) continue; + + bool was_hovering = button.hover; // Store previous hover state + // Check if the mouse is inside the button + if (button.active && is_mouse_inside_button(event_data, button, transform)) { + button.hover = true; + + // Trigger the on_enter callback if the hover state just changed to true + if (!was_hovering && button.on_enter) { + button.on_enter(); + } + } else { + button.hover = false; + + // Trigger the on_exit callback if the hover state just changed to false + if (was_hovering && button.on_exit) { + button.on_exit(); + } + } + } +} void InputSystem::handle_click(const SDLContext::EventData & event_data) { ComponentManager & mgr = this->component_manager; RefVector<Button> buttons = mgr.get_components_by_type<Button>(); - for (Button & button : buttons) { - RefVector<Transform> transform_vec = mgr.get_components_by_id<Transform>(button.game_object_id); + RefVector<Transform> transform_vec + = mgr.get_components_by_id<Transform>(button.game_object_id); OptionalRef<Transform> transform(transform_vec.front().get()); if (button.active && is_mouse_inside_button(event_data, button, transform)) { diff --git a/src/test/InputTest.cpp b/src/test/InputTest.cpp index 2467839..4f6077b 100644 --- a/src/test/InputTest.cpp +++ b/src/test/InputTest.cpp @@ -191,11 +191,11 @@ TEST_F(InputTest, testButtonClick) { GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); bool button_clicked = false; std::function<void()> on_click = [&]() { button_clicked = true; }; - auto & button = obj.add_component<Button>(100, 100,on_click,false); - + auto & button = obj.add_component<Button>(100, 100, on_click, false); + bool hover = false; button.active = true; - + button.is_pressed = false; button.is_toggle = false; this->simulate_mouse_click(101, 101, SDL_BUTTON_LEFT); @@ -213,7 +213,7 @@ TEST_F(InputTest, testButtonHover) { GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); bool button_clicked = false; std::function<void()> on_click = [&]() { button_clicked = true; }; - auto & button = obj.add_component<Button>(100, 100,on_click,false); + auto & button = obj.add_component<Button>(100, 100, on_click, false); button.active = true; button.width = 100; button.height = 100; |