From 570acb7e9258edcd6a06fff4dd2ce0ff12a3e873 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 28 Nov 2024 09:01:37 +0100 Subject: added on_enter and on_hover --- src/crepe/api/Button.cpp | 6 ++-- src/crepe/api/Button.h | 42 ++++++++++++++++++-------- src/crepe/api/Event.h | 4 +-- src/crepe/facade/SDLContext.h | 2 +- src/crepe/system/InputSystem.cpp | 64 +++++++++++++++++++++------------------- src/crepe/system/InputSystem.h | 10 ------- src/test/InputTest.cpp | 12 ++++---- 7 files changed, 75 insertions(+), 65 deletions(-) (limited to 'src') diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp index c0ff5a8..a27ff53 100644 --- a/src/crepe/api/Button.cpp +++ b/src/crepe/api/Button.cpp @@ -2,12 +2,10 @@ namespace crepe { -Button::Button(game_object_id_t id, int width, int height, bool is_toggle, - std::function on_click) +Button::Button(game_object_id_t id, int width, int height, std::function on_click, bool is_toggle + ) : UiObject(id, width, height), is_toggle(is_toggle), - is_pressed(false), - hover(false), on_click(on_click) {} } // namespace crepe diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h index 2fa94ae..1410529 100644 --- a/src/crepe/api/Button.h +++ b/src/crepe/api/Button.h @@ -24,8 +24,7 @@ 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, bool is_toggle = false, - std::function on_click = nullptr); + Button(game_object_id_t id, int width, int height, std::function on_click, bool is_toggle = false); /** * \brief Indicates if the button is a toggle button (can be pressed and released). @@ -33,29 +32,46 @@ public: * A toggle button allows for a pressed/released state, whereas a regular button * typically only has an on-click state. */ - bool is_toggle; + bool is_toggle = false; /** - * \brief Indicates whether the button is currently pressed. + * \brief The callback function to be executed when the button is clicked. * - * This state is true when the button is actively pressed and false otherwise. + * This function is invoked whenever the button is clicked. It can be set to any + * function that matches the signature `void()`. */ - bool is_pressed; + std::function on_click; + + /** + * \brief Callback function to be executed when the mouse enters the button's boundaries. + * + * This function is triggered when the mouse cursor moves over the button, allowing + * custom actions like visual effects, highlighting, or sound effects. + */ + std::function on_enter; /** - * \brief Indicates whether the mouse is currently hovering over the button. + * \brief Callback function to be executed when the mouse exits the button's boundaries. + * + * This function is triggered when the mouse cursor moves out of the button's area, + * allowing custom actions like resetting visual effects or playing exit-related effects. + */ + std::function on_exit; + private: + friend class InputSystem; + /** + * \brief Indicates whether the button is currently pressed. * - * This is set to true when the mouse is over the button and false otherwise. + * This state is true when the button is actively pressed and false otherwise. */ - bool hover; + bool is_pressed = false; /** - * \brief The callback function to be executed when the button is clicked. + * \brief Indicates whether the mouse is currently hovering over the button. * - * This function is invoked whenever the button is clicked. It can be set to any - * function that matches the signature `void()`. Defaults to nullptr. + * This is set to true when the mouse is over the button and false otherwise. */ - std::function on_click; + bool hover = false; public: /** diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h index a7d5511..91a30b5 100644 --- a/src/crepe/api/Event.h +++ b/src/crepe/api/Event.h @@ -89,10 +89,10 @@ public: //! Y-coordinate of the mouse position at the time of the event. int mouse_y = 0; - // Relative movement in x + // Movement since last event in x int rel_x = 0; - // Relative movement in y + // Movement since last event in y int rel_y = 0; }; diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 886dda8..546e3fe 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -86,7 +86,7 @@ private: * This method maps an SDL key code to the corresponding `Keycode` enum value, * which is used internally by the system to identify the keys. * - * @param sdlKey The SDL key code to convert. + * @param sdl_key The SDL key code to convert. * @return The corresponding `Keycode` value. */ Keycode sdl_to_keycode(SDL_Keycode sdl_key); diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index 070f804..4695620 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -80,50 +80,54 @@ void InputSystem::update() { } void InputSystem::handle_move(const SDLContext::EventData & event_data) { - ComponentManager & mgr = this->component_manager; - - RefVector