From a80477f2e7f4c18adcc6441828d17582aad2598f Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 9 Dec 2024 15:29:18 +0100 Subject: get_keyboard_state working --- src/crepe/api/Config.h | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/crepe/api/Config.h') diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index a9745c3..7b1a5ca 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -86,6 +86,12 @@ public: */ std::string root_pattern = ".crepe-root"; } asset; + //! 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; + }; } // namespace crepe -- cgit v1.2.3 From f6633670594e04baf77c14399c9f3bdaa49aecea Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 9 Dec 2024 15:29:51 +0100 Subject: make format --- src/crepe/api/Config.h | 1 - src/crepe/facade/SDLContext.cpp | 29 ++++++++++++++-------------- src/crepe/facade/SDLContext.h | 2 +- src/crepe/system/InputSystem.h | 2 +- src/test/InputTest.cpp | 42 ++++++++++++++++++++--------------------- 5 files changed, 37 insertions(+), 39 deletions(-) (limited to 'src/crepe/api/Config.h') diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 7b1a5ca..d2be3f3 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -91,7 +91,6 @@ public: //! The maximum number of pixels the mouse can move between MouseDown and MouseUp events to be considered a click. int tolerance = 5; } click_tolerance; - }; } // namespace crepe diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 1e8af41..4504b95 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -191,22 +191,21 @@ Keycode SDLContext::sdl_to_keycode(SDL_Scancode sdl_key) { return LOOKUP_TABLE[sdl_key]; } std::array SDLContext::get_keyboard_state() { - // Array to hold the key states (true if pressed, false if not) - std::array keyState{}; - SDL_PumpEvents(); - const Uint8 *current_state = SDL_GetKeyboardState(nullptr); - - for (int i = 0; i < SDL_NUM_SCANCODES; ++i) { - Keycode key = sdl_to_keycode(static_cast(i)); - - if (key != Keycode::NONE) { - keyState[key] = current_state[i] != 0; - } - } - - return keyState; -} + // Array to hold the key states (true if pressed, false if not) + std::array keyState{}; + SDL_PumpEvents(); + const Uint8 * current_state = SDL_GetKeyboardState(nullptr); + + for (int i = 0; i < SDL_NUM_SCANCODES; ++i) { + Keycode key = sdl_to_keycode(static_cast(i)); + + if (key != Keycode::NONE) { + keyState[key] = current_state[i] != 0; + } + } + return keyState; +} MouseButton SDLContext::sdl_to_mousebutton(Uint8 sdl_button) { static const std::array MOUSE_BUTTON_LOOKUP_TABLE = [] { diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 5182dca..053aa59 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -165,7 +165,7 @@ private: * \return The corresponding `Keycode` value or `Keycode::NONE` if the key is unrecognized. */ Keycode sdl_to_keycode(SDL_Scancode sdl_key); - + /** * \brief Converts an SDL mouse button code to the custom MouseButton type. * diff --git a/src/crepe/system/InputSystem.h b/src/crepe/system/InputSystem.h index 1d6e79c..5da8f32 100644 --- a/src/crepe/system/InputSystem.h +++ b/src/crepe/system/InputSystem.h @@ -1,9 +1,9 @@ #pragma once +#include "../api/Config.h" #include "../facade/SDLContext.h" #include "../types.h" #include "../util/OptionalRef.h" -#include "../api/Config.h" #include "System.h" diff --git a/src/test/InputTest.cpp b/src/test/InputTest.cpp index e9dc645..5150960 100644 --- a/src/test/InputTest.cpp +++ b/src/test/InputTest.cpp @@ -332,30 +332,30 @@ TEST_F(InputTest, WindowMoveTest) { EXPECT_TRUE(callback_triggered); } TEST_F(InputTest, KeyboardStateTest) { - SDLContext& sdl_context = this->mediator.sdl_context; - // Simulate pressing a key - SDL_Event key_down_event; - SDL_zero(key_down_event); - key_down_event.type = SDL_KEYDOWN; - key_down_event.key.keysym.scancode = SDL_SCANCODE_A; + SDLContext & sdl_context = this->mediator.sdl_context; + // Simulate pressing a key + SDL_Event key_down_event; + SDL_zero(key_down_event); + key_down_event.type = SDL_KEYDOWN; + key_down_event.key.keysym.scancode = SDL_SCANCODE_A; key_down_event.key.keysym.sym = SDL_SCANCODE_A; - SDL_PushEvent(&key_down_event); + SDL_PushEvent(&key_down_event); - // Check the keyboard state - auto keyboard_state = sdl_context.get_keyboard_state(); + // Check the keyboard state + auto keyboard_state = sdl_context.get_keyboard_state(); - // Verify the state of the 'A' key - EXPECT_TRUE(keyboard_state[Keycode::A]); + // Verify the state of the 'A' key + EXPECT_TRUE(keyboard_state[Keycode::A]); - // Simulate releasing a key - SDL_Event key_up_event; - SDL_zero(key_up_event); - key_up_event.type = SDL_KEYUP; - key_up_event.key.keysym.scancode = SDL_SCANCODE_A; // Simulate releasing 'A' key - SDL_PushEvent(&key_up_event); - // Check the keyboard state again - keyboard_state = sdl_context.get_keyboard_state(); + // Simulate releasing a key + SDL_Event key_up_event; + SDL_zero(key_up_event); + key_up_event.type = SDL_KEYUP; + key_up_event.key.keysym.scancode = SDL_SCANCODE_A; // Simulate releasing 'A' key + SDL_PushEvent(&key_up_event); + // Check the keyboard state again + keyboard_state = sdl_context.get_keyboard_state(); - // Verify the state of the 'A' key - EXPECT_FALSE(keyboard_state[Keycode::A]); + // Verify the state of the 'A' key + EXPECT_FALSE(keyboard_state[Keycode::A]); } -- cgit v1.2.3 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(-) (limited to 'src/crepe/api/Config.h') 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