From 73f8d5c558ebc0820ede241e64a876ff1c5ccefb Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Thu, 12 Dec 2024 07:57:24 +0100 Subject: seperat ints converted to float --- src/crepe/api/Button.cpp | 3 +- src/crepe/api/Button.h | 12 +------ src/crepe/api/Config.h | 4 +-- src/crepe/system/InputSystem.cpp | 77 ++++++++++++++++++---------------------- src/crepe/system/InputSystem.h | 10 +++--- src/test/InputTest.cpp | 4 +-- 6 files changed, 44 insertions(+), 66 deletions(-) diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp index 76f74f0..305922c 100644 --- a/src/crepe/api/Button.cpp +++ b/src/crepe/api/Button.cpp @@ -3,9 +3,8 @@ namespace crepe { Button::Button(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, - const std::function & on_click, bool is_toggle) + const std::function & on_click) : UIObject(id, dimensions, offset), - is_toggle(is_toggle), on_click(on_click) {} } // namespace crepe diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h index 61b18d7..08f5dec 100644 --- a/src/crepe/api/Button.h +++ b/src/crepe/api/Button.h @@ -15,19 +15,11 @@ public: * \param id The unique ID of the game object associated with this button. * \param dimensions The width and height of the UIObject * \param offset The offset relative this GameObjects Transform - * \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, const vec2 & dimensions, const vec2 & offset, - const std::function & on_click, bool is_toggle = false); + const std::function & on_click); - /** - * \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 = false; // TODO: create separate toggle button class /** * \brief The callback function to be executed when the button is clicked. @@ -56,8 +48,6 @@ public: private: //! friend relation for is_pressed and hover variables friend class InputSystem; - //! Indicates whether the toggle button is pressed - bool is_pressed = false; //! Indicates whether the mouse is currently hovering over the button bool hover = false; diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index d2be3f3..324e639 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -89,8 +89,8 @@ public: //! Configuration for click tolerance. struct { //! The maximum number of pixels the mouse can move between MouseDown and MouseUp events to be considered a click. - int tolerance = 5; - } click_tolerance; + int click_tolerance = 5; + } input; }; } // namespace crepe diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index f5d8536..e76b1ec 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -2,7 +2,6 @@ #include "../api/Button.h" #include "../manager/ComponentManager.h" #include "../manager/EventManager.h" - #include "InputSystem.h" using namespace crepe; @@ -41,15 +40,15 @@ void InputSystem::update() { || event.event_type == SDLContext::EventType::MOUSEMOVE || event.event_type == SDLContext::EventType::MOUSEWHEEL) { - int adjusted_mouse_x = event.mouse_data.mouse_position.x + camera_origin.x; - int adjusted_mouse_y = event.mouse_data.mouse_position.y + camera_origin.y; - + ivec2 adjusted_mouse; + adjusted_mouse.x = event.mouse_data.mouse_position.x + camera_origin.x; + adjusted_mouse.y = event.mouse_data.mouse_position.y + camera_origin.y; // Check if the mouse is within the viewport bool mouse_in_viewport - = !(adjusted_mouse_x < camera_origin.x - || adjusted_mouse_x > camera_origin.x + current_cam.viewport_size.x - || adjusted_mouse_y < camera_origin.y - || adjusted_mouse_y > camera_origin.y + current_cam.viewport_size.y); + = !(adjusted_mouse.x < camera_origin.x + || adjusted_mouse.x > camera_origin.x + current_cam.viewport_size.x + || adjusted_mouse.y < camera_origin.y + || adjusted_mouse.y > camera_origin.y + current_cam.viewport_size.y); if (!mouse_in_viewport) continue; @@ -57,45 +56,44 @@ void InputSystem::update() { switch (event.event_type) { case SDLContext::EventType::MOUSEDOWN: event_mgr.queue_event({ - .mouse_pos = {adjusted_mouse_x, adjusted_mouse_y}, + .mouse_pos = {adjusted_mouse.x, adjusted_mouse.y}, .button = event.mouse_data.mouse_button, }); - this->last_mouse_down_position = {adjusted_mouse_x, adjusted_mouse_y}; + this->last_mouse_down_position = {adjusted_mouse.x, adjusted_mouse.y}; this->last_mouse_button = event.mouse_data.mouse_button; break; case SDLContext::EventType::MOUSEUP: { event_mgr.queue_event({ - .mouse_pos = {adjusted_mouse_x, adjusted_mouse_y}, + .mouse_pos = {adjusted_mouse.x, adjusted_mouse.y}, .button = event.mouse_data.mouse_button, }); - int delta_x = adjusted_mouse_x - this->last_mouse_down_position.x; - int delta_y = adjusted_mouse_y - this->last_mouse_down_position.y; + int delta_x = adjusted_mouse.x - this->last_mouse_down_position.x; + int delta_y = adjusted_mouse.y - this->last_mouse_down_position.y; if (this->last_mouse_button == event.mouse_data.mouse_button && std::abs(delta_x) <= click_tolerance && std::abs(delta_y) <= click_tolerance) { event_mgr.queue_event({ - .mouse_pos = {adjusted_mouse_x, adjusted_mouse_y}, + .mouse_pos = {adjusted_mouse.x, adjusted_mouse.y}, .button = event.mouse_data.mouse_button, }); - this->handle_click(event.mouse_data.mouse_button, adjusted_mouse_x, - adjusted_mouse_y); + this->handle_click(event.mouse_data.mouse_button, adjusted_mouse); } break; } case SDLContext::EventType::MOUSEMOVE: event_mgr.queue_event({ - .mouse_pos = {adjusted_mouse_x, adjusted_mouse_y}, + .mouse_pos = {adjusted_mouse.x, adjusted_mouse.y}, .mouse_delta = event.mouse_data.rel_mouse_move, }); - this->handle_move(event, adjusted_mouse_x, adjusted_mouse_y); + this->handle_move(event, adjusted_mouse); break; case SDLContext::EventType::MOUSEWHEEL: event_mgr.queue_event({ - .mouse_pos = {adjusted_mouse_x, adjusted_mouse_y}, + .mouse_pos = {adjusted_mouse.x, adjusted_mouse.y}, .scroll_direction = event.mouse_data.scroll_direction, .scroll_delta = event.mouse_data.scroll_delta, }); @@ -148,54 +146,57 @@ void InputSystem::update() { } void InputSystem::handle_move(const SDLContext::EventData & event_data, - const int adjusted_mouse_x, const int adjusted_mouse_y) { + const ivec2& mouse_pos) { ComponentManager & mgr = this->mediator.component_manager; RefVector