From 7070f22d86057eafca3b82321d4146958c14a33e Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Wed, 27 Nov 2024 14:25:32 +0100 Subject: pr feedback --- src/crepe/api/Button.cpp | 7 ++++-- src/crepe/api/Button.h | 52 +++++++++++++++++++++++++++++----------- src/crepe/api/Event.h | 7 ++++-- src/crepe/api/UiObject.cpp | 2 +- src/crepe/api/UiObject.h | 2 +- src/crepe/facade/SDLContext.h | 14 ++++------- src/crepe/system/InputSystem.cpp | 46 +++++++++++++++++------------------ src/crepe/system/InputSystem.h | 7 ++++-- src/test/inputTest.cpp | 45 +++++++++++++++++++--------------- 9 files changed, 109 insertions(+), 73 deletions(-) (limited to 'src') diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp index 547c0fc..077a5e7 100644 --- a/src/crepe/api/Button.cpp +++ b/src/crepe/api/Button.cpp @@ -1,5 +1,8 @@ #include "Button.h" -using namespace crepe; +namespace crepe { -Button::Button(game_object_id_t id) : UiObject(id) {} +Button::Button(game_object_id_t id, int width, int height, bool is_toggle, std::function on_click) + : 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 0056238..df6f1e0 100644 --- a/src/crepe/api/Button.h +++ b/src/crepe/api/Button.h @@ -1,7 +1,6 @@ #pragma once #include - #include "UiObject.h" namespace crepe { @@ -9,34 +8,59 @@ namespace crepe { /** * \class Button * \brief Represents a clickable UI button, derived from the UiObject class. + * + * This class provides functionality for a button in the UI, including toggle state, + * click handling, and mouse hover detection. A callback function can be provided to + * handle button clicks. */ class Button : public UiObject { public: /** - * \brief Constructs a Button with the specified game object ID. + * \brief Constructs a Button with the specified game object ID and dimensions. + * * \param id The unique ID of the game object associated with this button. + * \param width The width of the button. + * \param height The height of the button. + * \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); - - //! Indicates if the button is interactable (can be clicked). - bool interactable = true; + Button(game_object_id_t id, int width, int height, bool is_toggle = false, std::function on_click = nullptr); - //! Indicates if the button is a toggle button (can be pressed and released). - bool is_toggle = false; + /** + * \brief Indicates if the button is a toggle button (can be pressed and released). + * + * A toggle button allows for a pressed/released state, whereas a regular button + * typically only has an on-click state. + */ + bool is_toggle; - //! Indicates whether the button is currently pressed. - bool is_pressed = false; + /** + * \brief Indicates whether the button is currently pressed. + * + * This state is true when the button is actively pressed and false otherwise. + */ + bool is_pressed; - //! Indicates whether the mouse is currently hovering over the button. - bool hover = false; + /** + * \brief Indicates whether the mouse is currently hovering over the button. + * + * This is set to true when the mouse is over the button and false otherwise. + */ + bool hover; - //! The callback function to be executed when the button is clicked. + /** + * \brief The callback function to be executed when the button is clicked. + * + * This function is invoked whenever the button is clicked. It can be set to any + * function that matches the signature `void()`. Defaults to nullptr. + */ std::function on_click; public: /** * \brief Retrieves the maximum number of instances allowed for this button type. - * \return Always returns 1, as only a single instance is allowed. + * + * \return Always returns 1, as only a single instance of this type is allowed. */ virtual int get_instances_max() const override { return 1; } }; diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h index b13abc1..a7d5511 100644 --- a/src/crepe/api/Event.h +++ b/src/crepe/api/Event.h @@ -88,10 +88,12 @@ public: //! Y-coordinate of the mouse position at the time of the event. int mouse_y = 0; + // Relative movement in x - int rel_x; + int rel_x = 0; + // Relative movement in y - int rel_y; + int rel_y = 0; }; /** @@ -104,6 +106,7 @@ public: //! Y-coordinate of the mouse position at the time of the event. int scroll_y = 0; + //! scroll direction (-1 = down, 1 = up) int direction = 0; }; diff --git a/src/crepe/api/UiObject.cpp b/src/crepe/api/UiObject.cpp index 1c11fc3..987fc06 100644 --- a/src/crepe/api/UiObject.cpp +++ b/src/crepe/api/UiObject.cpp @@ -2,4 +2,4 @@ using namespace crepe; -UiObject::UiObject(game_object_id_t id) : Component(id){}; +UiObject::UiObject(game_object_id_t id,int width,int height) : Component(id),width(width),height(height){}; diff --git a/src/crepe/api/UiObject.h b/src/crepe/api/UiObject.h index 7bd1c2e..6b0323e 100644 --- a/src/crepe/api/UiObject.h +++ b/src/crepe/api/UiObject.h @@ -14,7 +14,7 @@ public: * \brief Constructs a UiObject with the specified game object ID. * \param id The unique ID of the game object associated with this UI object. */ - UiObject(game_object_id_t id); + UiObject(game_object_id_t id,int width,int height); //! The width of the UI object. int width = 0; diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 3e0b073..9f18728 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -11,20 +11,16 @@ #include #include -#include "../api/Event.h" -#include "../api/KeyCodes.h" -#include "../api/Sprite.h" -#include "../api/Transform.h" -#include "../api/Vector2.h" +#include "api/Event.h" +#include "api/KeyCodes.h" +#include "api/Sprite.h" +#include "api/Transform.h" #include "api/Camera.h" #include "types.h" namespace crepe { -// TODO: SDL_Keycode is defined in a header not distributed with crepe, which means this -// typedef is unusable when crepe is packaged. Wouter will fix this later. -//typedef SDL_Keycode CREPE_KEYCODES; class LoopManager; class InputSystem; /** @@ -93,7 +89,7 @@ private: * @param sdlKey The SDL key code to convert. * @return The corresponding `Keycode` value. */ - Keycode sdl_to_keycode(SDL_Keycode sdlKey); + Keycode sdl_to_keycode(SDL_Keycode sdl_key); /** * @brief Converts an SDL mouse button code to the custom MouseButton type. diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index 9690fec..590be8d 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -1,8 +1,8 @@ -#include "../api/Button.h" -#include "../api/EventManager.h" +#include "api/Button.h" +#include "api/EventManager.h" #include "ComponentManager.h" -#include "system/InputSystem.h" +#include "InputSystem.h" using namespace crepe; @@ -36,17 +36,15 @@ void InputSystem::update() { break; } case SDLContext::EventType::MOUSEUP: { - MouseReleaseEvent mouse_release_event = MouseReleaseEvent{ + event_mgr.queue_event(MouseReleaseEvent{ .mouse_x = event.mouse_position.first, .mouse_y = event.mouse_position.second, .button = event.mouse_button, - }; - event_mgr.queue_event(mouse_release_event); + }); - // Calculate deltas for click detection int delta_x = event.mouse_position.first - last_mouse_down_position.first; int delta_y = event.mouse_position.second - last_mouse_down_position.second; - + if (last_mouse_button == event.mouse_button && std::abs(delta_x) <= click_tolerance && std::abs(delta_y) <= click_tolerance) { @@ -96,10 +94,10 @@ void InputSystem::handle_move(const SDLContext::EventData & event_data) { = mgr.get_components_by_type(); for (Button & button : buttons) { - Transform * transform = find_transform_for_button(button, transforms); + OptionalRef transform = find_transform_for_button(button, transforms); if (!transform) continue; - if (button.interactable && is_mouse_inside_button(event_data, button, *transform)) { + if (button.active && is_mouse_inside_button(event_data, button, transform)) { button.hover = true; } else { button.hover = false; @@ -111,29 +109,31 @@ void InputSystem::handle_click(const SDLContext::EventData & event_data) { ComponentManager & mgr = this->component_manager; std::vector> buttons = mgr.get_components_by_type