diff options
-rw-r--r-- | src/crepe/api/Config.h | 6 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 29 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 36 | ||||
-rw-r--r-- | src/crepe/system/InputSystem.h | 5 | ||||
-rw-r--r-- | src/example/button.cpp | 1 | ||||
-rw-r--r-- | src/test/InputTest.cpp | 28 |
6 files changed, 71 insertions, 34 deletions
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 diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 722c165..1e8af41 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -78,8 +78,7 @@ SDLContext::~SDLContext() { SDL_Quit(); } -// Function that uses the shared table -Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) { +Keycode SDLContext::sdl_to_keycode(SDL_Scancode sdl_key) { static const std::array<Keycode, SDL_NUM_SCANCODES> LOOKUP_TABLE = [] { std::array<Keycode, SDL_NUM_SCANCODES> table{}; table.fill(Keycode::NONE); @@ -192,19 +191,23 @@ Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) { return LOOKUP_TABLE[sdl_key]; } std::array<bool, Keycode::NUM_KEYCODES> SDLContext::get_keyboard_state() { - // Array to hold the key states (true if pressed, false if not) - std::array<bool, SDL_NUM_SCANCODES> keyState; - - const Uint8 * current_state = SDL_GetKeyboardState(nullptr); - - for (int i = 0; i < SDL_NUM_SCANCODES; ++i) { - // Set true if the key is pressed, false if not - keyState[i] = current_state[i] != 0; - } - - return keyState; + // Array to hold the key states (true if pressed, false if not) + std::array<bool, Keycode::NUM_KEYCODES> 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<SDL_Scancode>(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<MouseButton, 5> MOUSE_BUTTON_LOOKUP_TABLE = [] { std::array<MouseButton, 5> table{}; diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index b65ba9c..5182dca 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -110,7 +110,19 @@ public: MouseData mouse_data; WindowData window_data; }; - + /** + * \brief Retrieves the current state of the keyboard. + * + * This method returns the state of all keys on the keyboard, represented as a + * `std::array` of boolean values. Each element of the array corresponds to a + * specific key defined in the `Keycode` enum, and the value indicates whether + * the key is currently pressed (true) or not pressed (false). + * + * \return A `std::array<bool, Keycode::NUM_KEYCODES>` representing the state of + * each key on the keyboard, where `true` means the key is pressed, and + * `false` means it is not pressed. + */ + std::array<bool, Keycode::NUM_KEYCODES> get_keyboard_state(); /** * \brief Gets the singleton instance of SDLContext. * \return Reference to the SDLContext instance. @@ -144,28 +156,16 @@ private: void handle_window_event(const SDL_WindowEvent & window_event, std::vector<SDLContext::EventData> & event_list); /** - * \brief Converts an SDL key code to the custom Keycode type. + * \brief Converts an SDL scan code to the custom Keycode type. * - * This method maps an SDL key code to the corresponding `Keycode` enum value, + * This method maps an SDL scan code to the corresponding `Keycode` enum value, * which is used internally by the system to identify the keys. * - * \param sdl_key The SDL key code to convert. + * \param sdl_key The SDL scan code to convert. * \return The corresponding `Keycode` value or `Keycode::NONE` if the key is unrecognized. */ - Keycode sdl_to_keycode(SDL_Keycode sdl_key); - /** - * \brief Retrieves the current state of the keyboard. - * - * This method returns the state of all keys on the keyboard, represented as a - * `std::array` of boolean values. Each element of the array corresponds to a - * specific key defined in the `Keycode` enum, and the value indicates whether - * the key is currently pressed (true) or not pressed (false). - * - * \return A `std::array<bool, Keycode::NUM_KEYCODES>` representing the state of - * each key on the keyboard, where `true` means the key is pressed, and - * `false` means it is not pressed. - */ - std::array<bool, Keycode::NUM_KEYCODES> get_keyboard_state(); + 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 87e86f8..1d6e79c 100644 --- a/src/crepe/system/InputSystem.h +++ b/src/crepe/system/InputSystem.h @@ -3,6 +3,7 @@ #include "../facade/SDLContext.h" #include "../types.h" #include "../util/OptionalRef.h" +#include "../api/Config.h" #include "System.h" @@ -37,8 +38,8 @@ private: //! Stores the last mouse button pressed. MouseButton last_mouse_button = MouseButton::NONE; - //! The maximum allowable distance between mouse down and mouse up to register as a click. - const int click_tolerance = 5; + //! The maximum allowable distance between mouse down and mouse up to register as a click. This can be changed using the Config. + int click_tolerance = Config::get_instance().click_tolerance.tolerance; /** * \brief Handles the mouse click event. diff --git a/src/example/button.cpp b/src/example/button.cpp index 00bdc28..f2e77f6 100644 --- a/src/example/button.cpp +++ b/src/example/button.cpp @@ -15,7 +15,6 @@ #include <crepe/system/InputSystem.h> #include <crepe/system/RenderSystem.h> #include <crepe/types.h> -#include <iostream> using namespace crepe; using namespace std; diff --git a/src/test/InputTest.cpp b/src/test/InputTest.cpp index 94549a0..e9dc645 100644 --- a/src/test/InputTest.cpp +++ b/src/test/InputTest.cpp @@ -331,3 +331,31 @@ TEST_F(InputTest, WindowMoveTest) { event_manager.dispatch_events(); 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; + key_down_event.key.keysym.sym = SDL_SCANCODE_A; + SDL_PushEvent(&key_down_event); + + // 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]); + + // 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]); +} |