diff options
-rw-r--r-- | src/crepe/api/Button.cpp | 0 | ||||
-rw-r--r-- | src/crepe/api/Button.h | 2 | ||||
-rw-r--r-- | src/crepe/api/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/crepe/api/Event.h | 12 | ||||
-rw-r--r-- | src/crepe/api/KeyCodes.h | 303 | ||||
-rw-r--r-- | src/crepe/api/UiObject.h | 5 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 222 | ||||
-rw-r--r-- | src/test/inputTest.cpp | 139 |
8 files changed, 505 insertions, 180 deletions
diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp deleted file mode 100644 index e69de29..0000000 --- a/src/crepe/api/Button.cpp +++ /dev/null diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h index 5035729..9b1341f 100644 --- a/src/crepe/api/Button.h +++ b/src/crepe/api/Button.h @@ -6,7 +6,7 @@ namespace crepe { class Button : public UiObject{ public: - ~Button(){}; + Button(); bool interactable = true; bool is_toggle = false; bool is_pressed = false; diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt index 50c51ed..fc27fa0 100644 --- a/src/crepe/api/CMakeLists.txt +++ b/src/crepe/api/CMakeLists.txt @@ -58,4 +58,6 @@ target_sources(crepe PUBLIC FILE_SET HEADERS FILES LoopManager.h LoopTimer.h Asset.h + Button.h + UiObject.h ) diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h index bac8701..46dd86b 100644 --- a/src/crepe/api/Event.h +++ b/src/crepe/api/Event.h @@ -95,6 +95,18 @@ public: }; /** + * \brief Event triggered when the mouse is moved. + */ +class MouseScrollEvent : public Event { +public: + //! X-coordinate of the mouse position at the time of the event. + int scroll_x = 0; + + //! Y-coordinate of the mouse position at the time of the event. + int scroll_y = 0; + int direction = 0; +}; +/** * \brief Event triggered during a collision between objects. */ class CollisionEvent : public Event {}; diff --git a/src/crepe/api/KeyCodes.h b/src/crepe/api/KeyCodes.h index 9e173e0..4b026c5 100644 --- a/src/crepe/api/KeyCodes.h +++ b/src/crepe/api/KeyCodes.h @@ -1,153 +1,154 @@ #pragma once +namespace crepe { + //! Enumeration for mouse button inputs, including standard and extended buttons. + enum class MouseButton { + NONE = 0, //!< No mouse button input. + LEFT_MOUSE = 1, //!< Left mouse button. + RIGHT_MOUSE = 2, //!< Right mouse button. + MIDDLE_MOUSE = 3, //!< Middle mouse button (scroll wheel press). + X1_MOUSE = 4, //!< First extended mouse button. + X2_MOUSE = 5, //!< Second extended mouse button. + SCROLL_UP = 6, //!< Scroll wheel upward movement. + SCROLL_DOWN = 7, //!< Scroll wheel downward movement. + }; -//! Enumeration for mouse button inputs, including standard and extended buttons. -enum class MouseButton { - NONE = 0, //!< No mouse button input. - LEFT_MOUSE = 1, //!< Left mouse button. - RIGHT_MOUSE = 2, //!< Right mouse button. - MIDDLE_MOUSE = 3, //!< Middle mouse button (scroll wheel press). - X1_MOUSE = 4, //!< First extended mouse button. - X2_MOUSE = 5, //!< Second extended mouse button. - SCROLL_UP = 6, //!< Scroll wheel upward movement. - SCROLL_DOWN = 7, //!< Scroll wheel downward movement. -}; - -//! Enumeration for keyboard key inputs, including printable characters, function keys, and keypad keys. -enum class Keycode { - NONE = 0, //!< No key input. - SPACE = 32, //!< Spacebar. - APOSTROPHE = 39, //!< Apostrophe ('). - COMMA = 44, //!< Comma (,). - MINUS = 45, //!< Minus (-). - PERIOD = 46, //!< Period (.). - SLASH = 47, //!< Slash (/). - D0 = 48, //!< Digit 0. - D1 = 49, //!< Digit 1. - D2 = 50, //!< Digit 2. - D3 = 51, //!< Digit 3. - D4 = 52, //!< Digit 4. - D5 = 53, //!< Digit 5. - D6 = 54, //!< Digit 6. - D7 = 55, //!< Digit 7. - D8 = 56, //!< Digit 8. - D9 = 57, //!< Digit 9. - SEMICOLON = 59, //!< Semicolon (;). - EQUAL = 61, //!< Equal sign (=). - A = 65, //!< Key 'A'. - B = 66, //!< Key 'B'. - C = 67, //!< Key 'C'. - D = 68, //!< Key 'D'. - E = 69, //!< Key 'E'. - F = 70, //!< Key 'F'. - G = 71, //!< Key 'G'. - H = 72, //!< Key 'H'. - I = 73, //!< Key 'I'. - J = 74, //!< Key 'J'. - K = 75, //!< Key 'K'. - L = 76, //!< Key 'L'. - M = 77, //!< Key 'M'. - N = 78, //!< Key 'N'. - O = 79, //!< Key 'O'. - P = 80, //!< Key 'P'. - Q = 81, //!< Key 'Q'. - R = 82, //!< Key 'R'. - S = 83, //!< Key 'S'. - T = 84, //!< Key 'T'. - U = 85, //!< Key 'U'. - V = 86, //!< Key 'V'. - W = 87, //!< Key 'W'. - X = 88, //!< Key 'X'. - Y = 89, //!< Key 'Y'. - Z = 90, //!< Key 'Z'. - LEFT_BRACKET = 91, //!< Left bracket ([). - BACKSLASH = 92, //!< Backslash (\). - RIGHT_BRACKET = 93, //!< Right bracket (]). - GRAVE_ACCENT = 96, //!< Grave accent (`). - WORLD1 = 161, //!< Non-US key #1. - WORLD2 = 162, //!< Non-US key #2. - ESCAPE = 256, //!< Escape key. - ENTER = 257, //!< Enter key. - TAB = 258, //!< Tab key. - BACKSPACE = 259, //!< Backspace key. - INSERT = 260, //!< Insert key. - DELETE = 261, //!< Delete key. - RIGHT = 262, //!< Right arrow key. - LEFT = 263, //!< Left arrow key. - DOWN = 264, //!< Down arrow key. - UP = 265, //!< Up arrow key. - PAGE_UP = 266, //!< Page Up key. - PAGE_DOWN = 267, //!< Page Down key. - HOME = 268, //!< Home key. - END = 269, //!< End key. - CAPS_LOCK = 280, //!< Caps Lock key. - SCROLL_LOCK = 281, //!< Scroll Lock key. - NUM_LOCK = 282, //!< Num Lock key. - PRINT_SCREEN = 283, //!< Print Screen key. - PAUSE = 284, //!< Pause key. - /** - * \name Function keys (F1-F25). - * \{ - */ - F1 = 290, - F2 = 291, - F3 = 292, - F4 = 293, - F5 = 294, - F6 = 295, - F7 = 296, - F8 = 297, - F9 = 298, - F10 = 299, - F11 = 300, - F12 = 301, - F13 = 302, - F14 = 303, - F15 = 304, - F16 = 305, - F17 = 306, - F18 = 307, - F19 = 308, - F20 = 309, - F21 = 310, - F22 = 311, - F23 = 312, - F24 = 313, - F25 = 314, - /// \} - /** - * \name Keypad digits and operators. - * \{ - */ - KP0 = 320, - KP1 = 321, - KP2 = 322, - KP3 = 323, - KP4 = 324, - KP5 = 325, - KP6 = 326, - KP7 = 327, - KP8 = 328, - KP9 = 329, - KP_DECIMAL = 330, - KP_DIVIDE = 331, - KP_MULTIPLY = 332, - KP_SUBTRACT = 333, - KP_ADD = 334, - KP_ENTER = 335, - KP_EQUAL = 336, - /// \} - /** - * \name Modifier keys. - * \{ - */ - LEFT_SHIFT = 340, - LEFT_CONTROL = 341, - LEFT_ALT = 342, - LEFT_SUPER = 343, - RIGHT_SHIFT = 344, - RIGHT_CONTROL = 345, - RIGHT_ALT = 346, - RIGHT_SUPER = 347, - /// \} - MENU = 348, //!< Menu key. -}; + //! Enumeration for keyboard key inputs, including printable characters, function keys, and keypad keys. + enum class Keycode { + NONE = 0, //!< No key input. + SPACE = 32, //!< Spacebar. + APOSTROPHE = 39, //!< Apostrophe ('). + COMMA = 44, //!< Comma (,). + MINUS = 45, //!< Minus (-). + PERIOD = 46, //!< Period (.). + SLASH = 47, //!< Slash (/). + D0 = 48, //!< Digit 0. + D1 = 49, //!< Digit 1. + D2 = 50, //!< Digit 2. + D3 = 51, //!< Digit 3. + D4 = 52, //!< Digit 4. + D5 = 53, //!< Digit 5. + D6 = 54, //!< Digit 6. + D7 = 55, //!< Digit 7. + D8 = 56, //!< Digit 8. + D9 = 57, //!< Digit 9. + SEMICOLON = 59, //!< Semicolon (;). + EQUAL = 61, //!< Equal sign (=). + A = 65, //!< Key 'A'. + B = 66, //!< Key 'B'. + C = 67, //!< Key 'C'. + D = 68, //!< Key 'D'. + E = 69, //!< Key 'E'. + F = 70, //!< Key 'F'. + G = 71, //!< Key 'G'. + H = 72, //!< Key 'H'. + I = 73, //!< Key 'I'. + J = 74, //!< Key 'J'. + K = 75, //!< Key 'K'. + L = 76, //!< Key 'L'. + M = 77, //!< Key 'M'. + N = 78, //!< Key 'N'. + O = 79, //!< Key 'O'. + P = 80, //!< Key 'P'. + Q = 81, //!< Key 'Q'. + R = 82, //!< Key 'R'. + S = 83, //!< Key 'S'. + T = 84, //!< Key 'T'. + U = 85, //!< Key 'U'. + V = 86, //!< Key 'V'. + W = 87, //!< Key 'W'. + X = 88, //!< Key 'X'. + Y = 89, //!< Key 'Y'. + Z = 90, //!< Key 'Z'. + LEFT_BRACKET = 91, //!< Left bracket ([). + BACKSLASH = 92, //!< Backslash (\). + RIGHT_BRACKET = 93, //!< Right bracket (]). + GRAVE_ACCENT = 96, //!< Grave accent (`). + WORLD1 = 161, //!< Non-US key #1. + WORLD2 = 162, //!< Non-US key #2. + ESCAPE = 256, //!< Escape key. + ENTER = 257, //!< Enter key. + TAB = 258, //!< Tab key. + BACKSPACE = 259, //!< Backspace key. + INSERT = 260, //!< Insert key. + DELETE = 261, //!< Delete key. + RIGHT = 262, //!< Right arrow key. + LEFT = 263, //!< Left arrow key. + DOWN = 264, //!< Down arrow key. + UP = 265, //!< Up arrow key. + PAGE_UP = 266, //!< Page Up key. + PAGE_DOWN = 267, //!< Page Down key. + HOME = 268, //!< Home key. + END = 269, //!< End key. + CAPS_LOCK = 280, //!< Caps Lock key. + SCROLL_LOCK = 281, //!< Scroll Lock key. + NUM_LOCK = 282, //!< Num Lock key. + PRINT_SCREEN = 283, //!< Print Screen key. + PAUSE = 284, //!< Pause key. + /** + * \name Function keys (F1-F25). + * \{ + */ + F1 = 290, + F2 = 291, + F3 = 292, + F4 = 293, + F5 = 294, + F6 = 295, + F7 = 296, + F8 = 297, + F9 = 298, + F10 = 299, + F11 = 300, + F12 = 301, + F13 = 302, + F14 = 303, + F15 = 304, + F16 = 305, + F17 = 306, + F18 = 307, + F19 = 308, + F20 = 309, + F21 = 310, + F22 = 311, + F23 = 312, + F24 = 313, + F25 = 314, + /// \} + /** + * \name Keypad digits and operators. + * \{ + */ + KP0 = 320, + KP1 = 321, + KP2 = 322, + KP3 = 323, + KP4 = 324, + KP5 = 325, + KP6 = 326, + KP7 = 327, + KP8 = 328, + KP9 = 329, + KP_DECIMAL = 330, + KP_DIVIDE = 331, + KP_MULTIPLY = 332, + KP_SUBTRACT = 333, + KP_ADD = 334, + KP_ENTER = 335, + KP_EQUAL = 336, + /// \} + /** + * \name Modifier keys. + * \{ + */ + LEFT_SHIFT = 340, + LEFT_CONTROL = 341, + LEFT_ALT = 342, + LEFT_SUPER = 343, + RIGHT_SHIFT = 344, + RIGHT_CONTROL = 345, + RIGHT_ALT = 346, + RIGHT_SUPER = 347, + /// \} + MENU = 348, //!< Menu key. + }; +} diff --git a/src/crepe/api/UiObject.h b/src/crepe/api/UiObject.h index f57f7ef..1d7be69 100644 --- a/src/crepe/api/UiObject.h +++ b/src/crepe/api/UiObject.h @@ -1,11 +1,12 @@ -#include <functional> +#pragma once #include "Component.h" + #include "api/EventHandler.h" namespace crepe { class UiObject : public Component{ public: - ~UiObject(){}; + UiObject(); int width = 0; int height = 0; public: diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index ff7c9f0..1ef222c 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -17,6 +17,8 @@ #include "../api/Sprite.h" #include "../api/Texture.h" #include "../api/Transform.h" +#include "../api/EventManager.h" +#include "../api/Vector2.h" #include "../util/Log.h" @@ -74,27 +76,209 @@ SDLContext::~SDLContext() { SDL_Quit(); } -void SDLContext::handle_events(bool & running) { - //TODO: wouter i need events - /* - SDL_Event event; - SDL_PollEvent(&event); - switch (event.type) { - case SDL_QUIT: - running = false; - break; - case SDL_KEYDOWN: - triggerEvent(KeyPressedEvent(getCustomKey(event.key.keysym.sym))); - break; - case SDL_MOUSEBUTTONDOWN: - int x, y; - SDL_GetMouseState(&x, &y); - triggerEvent(MousePressedEvent(x, y)); - break; - } - */ +void SDLContext::handle_events(bool &running) { + EventManager& event_manager = EventManager::get_instance(); + SDL_Event event; + + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_QUIT: + running = false; + event_manager.trigger_event(ShutDownEvent{}); + break; + case SDL_KEYDOWN: + event_manager.trigger_event<KeyPressEvent>(KeyPressEvent{ + .repeat = (!event.key.repeat == 0), + .key = this->sdl_to_keycode(event.key.keysym.scancode) + }); + break; + + case SDL_KEYUP: + event_manager.trigger_event<KeyReleaseEvent>(KeyReleaseEvent{ + .key = this->sdl_to_keycode(event.key.keysym.scancode), + }); + break; + + case SDL_MOUSEBUTTONDOWN: + { + int x, y; + SDL_GetMouseState(&x, &y); + event_manager.trigger_event<MousePressEvent>(MousePressEvent{ + .mouse_x = x, + .mouse_y = y, + .button = this->sdl_to_mousebutton(event.button.button) + }); + } + break; + + case SDL_MOUSEBUTTONUP: + { + int x, y; + SDL_GetMouseState(&x, &y); + event_manager.trigger_event<MouseReleaseEvent>(MouseReleaseEvent{ + .mouse_x = x, + .mouse_y = y, + .button = this->sdl_to_mousebutton(event.button.button) + }); + } + break; + + case SDL_MOUSEMOTION: + event_manager.trigger_event<MouseMoveEvent>(MouseMoveEvent{ + .mouse_x = event.motion.x, + .mouse_y = event.motion.y, + .rel_x = event.motion.xrel, + .rel_y = event.motion.yrel + }); + break; + + case SDL_MOUSEWHEEL: + event_manager.trigger_event<MouseScrollEvent>(MouseScrollEvent{ + .scroll_x = event.wheel.x, + .scroll_y = event.wheel.y, + .direction = (event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED ? -1 : 1) + }); + break; + } + } } + + +Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) { + static const std::array<Keycode, SDL_NUM_SCANCODES> LOOKUP_TABLE = [] { + std::array<Keycode, SDL_NUM_SCANCODES> table{}; + table.fill(Keycode::NONE); + + table[SDL_SCANCODE_SPACE] = Keycode::SPACE; + table[SDL_SCANCODE_APOSTROPHE] = Keycode::APOSTROPHE; + table[SDL_SCANCODE_COMMA] = Keycode::COMMA; + table[SDL_SCANCODE_MINUS] = Keycode::MINUS; + table[SDL_SCANCODE_PERIOD] = Keycode::PERIOD; + table[SDL_SCANCODE_SLASH] = Keycode::SLASH; + table[SDL_SCANCODE_0] = Keycode::D0; + table[SDL_SCANCODE_1] = Keycode::D1; + table[SDL_SCANCODE_2] = Keycode::D2; + table[SDL_SCANCODE_3] = Keycode::D3; + table[SDL_SCANCODE_4] = Keycode::D4; + table[SDL_SCANCODE_5] = Keycode::D5; + table[SDL_SCANCODE_6] = Keycode::D6; + table[SDL_SCANCODE_7] = Keycode::D7; + table[SDL_SCANCODE_8] = Keycode::D8; + table[SDL_SCANCODE_9] = Keycode::D9; + table[SDL_SCANCODE_SEMICOLON] = Keycode::SEMICOLON; + table[SDL_SCANCODE_EQUALS] = Keycode::EQUAL; + table[SDL_SCANCODE_A] = Keycode::A; + table[SDL_SCANCODE_B] = Keycode::B; + table[SDL_SCANCODE_C] = Keycode::C; + table[SDL_SCANCODE_D] = Keycode::D; + table[SDL_SCANCODE_E] = Keycode::E; + table[SDL_SCANCODE_F] = Keycode::F; + table[SDL_SCANCODE_G] = Keycode::G; + table[SDL_SCANCODE_H] = Keycode::H; + table[SDL_SCANCODE_I] = Keycode::I; + table[SDL_SCANCODE_J] = Keycode::J; + table[SDL_SCANCODE_K] = Keycode::K; + table[SDL_SCANCODE_L] = Keycode::L; + table[SDL_SCANCODE_M] = Keycode::M; + table[SDL_SCANCODE_N] = Keycode::N; + table[SDL_SCANCODE_O] = Keycode::O; + table[SDL_SCANCODE_P] = Keycode::P; + table[SDL_SCANCODE_Q] = Keycode::Q; + table[SDL_SCANCODE_R] = Keycode::R; + table[SDL_SCANCODE_S] = Keycode::S; + table[SDL_SCANCODE_T] = Keycode::T; + table[SDL_SCANCODE_U] = Keycode::U; + table[SDL_SCANCODE_V] = Keycode::V; + table[SDL_SCANCODE_W] = Keycode::W; + table[SDL_SCANCODE_X] = Keycode::X; + table[SDL_SCANCODE_Y] = Keycode::Y; + table[SDL_SCANCODE_Z] = Keycode::Z; + table[SDL_SCANCODE_LEFTBRACKET] = Keycode::LEFT_BRACKET; + table[SDL_SCANCODE_BACKSLASH] = Keycode::BACKSLASH; + table[SDL_SCANCODE_RIGHTBRACKET] = Keycode::RIGHT_BRACKET; + table[SDL_SCANCODE_GRAVE] = Keycode::GRAVE_ACCENT; + table[SDL_SCANCODE_ESCAPE] = Keycode::ESCAPE; + table[SDL_SCANCODE_RETURN] = Keycode::ENTER; + table[SDL_SCANCODE_TAB] = Keycode::TAB; + table[SDL_SCANCODE_BACKSPACE] = Keycode::BACKSPACE; + table[SDL_SCANCODE_INSERT] = Keycode::INSERT; + table[SDL_SCANCODE_DELETE] = Keycode::DELETE; + table[SDL_SCANCODE_RIGHT] = Keycode::RIGHT; + table[SDL_SCANCODE_LEFT] = Keycode::LEFT; + table[SDL_SCANCODE_DOWN] = Keycode::DOWN; + table[SDL_SCANCODE_UP] = Keycode::UP; + table[SDL_SCANCODE_PAGEUP] = Keycode::PAGE_UP; + table[SDL_SCANCODE_PAGEDOWN] = Keycode::PAGE_DOWN; + table[SDL_SCANCODE_HOME] = Keycode::HOME; + table[SDL_SCANCODE_END] = Keycode::END; + table[SDL_SCANCODE_CAPSLOCK] = Keycode::CAPS_LOCK; + table[SDL_SCANCODE_SCROLLLOCK] = Keycode::SCROLL_LOCK; + table[SDL_SCANCODE_NUMLOCKCLEAR] = Keycode::NUM_LOCK; + table[SDL_SCANCODE_PRINTSCREEN] = Keycode::PRINT_SCREEN; + table[SDL_SCANCODE_PAUSE] = Keycode::PAUSE; + table[SDL_SCANCODE_F1] = Keycode::F1; + table[SDL_SCANCODE_F2] = Keycode::F2; + table[SDL_SCANCODE_F3] = Keycode::F3; + table[SDL_SCANCODE_F4] = Keycode::F4; + table[SDL_SCANCODE_F5] = Keycode::F5; + table[SDL_SCANCODE_F6] = Keycode::F6; + table[SDL_SCANCODE_F7] = Keycode::F7; + table[SDL_SCANCODE_F8] = Keycode::F8; + table[SDL_SCANCODE_F9] = Keycode::F9; + table[SDL_SCANCODE_F10] = Keycode::F10; + table[SDL_SCANCODE_F11] = Keycode::F11; + table[SDL_SCANCODE_F12] = Keycode::F12; + table[SDL_SCANCODE_KP_0] = Keycode::KP0; + table[SDL_SCANCODE_KP_1] = Keycode::KP1; + table[SDL_SCANCODE_KP_2] = Keycode::KP2; + table[SDL_SCANCODE_KP_3] = Keycode::KP3; + table[SDL_SCANCODE_KP_4] = Keycode::KP4; + table[SDL_SCANCODE_KP_5] = Keycode::KP5; + table[SDL_SCANCODE_KP_6] = Keycode::KP6; + table[SDL_SCANCODE_KP_7] = Keycode::KP7; + table[SDL_SCANCODE_KP_8] = Keycode::KP8; + table[SDL_SCANCODE_KP_9] = Keycode::KP9; + table[SDL_SCANCODE_LSHIFT] = Keycode::LEFT_SHIFT; + table[SDL_SCANCODE_LCTRL] = Keycode::LEFT_CONTROL; + table[SDL_SCANCODE_LALT] = Keycode::LEFT_ALT; + table[SDL_SCANCODE_LGUI] = Keycode::LEFT_SUPER; + table[SDL_SCANCODE_RSHIFT] = Keycode::RIGHT_SHIFT; + table[SDL_SCANCODE_RCTRL] = Keycode::RIGHT_CONTROL; + table[SDL_SCANCODE_RALT] = Keycode::RIGHT_ALT; + table[SDL_SCANCODE_RGUI] = Keycode::RIGHT_SUPER; + table[SDL_SCANCODE_MENU] = Keycode::MENU; + + return table; + }(); + + if (sdl_key < 0 || sdl_key >= SDL_NUM_SCANCODES) { + return Keycode::NONE; + } + + return LOOKUP_TABLE[sdl_key]; +} +MouseButton SDLContext::sdl_to_mousebutton(Uint8 sdl_button) { + static const std::array<MouseButton, 8> MOUSE_BUTTON_LOOKUP_TABLE = [] { + std::array<MouseButton, 8> table{}; + table.fill(MouseButton::NONE); + + table[SDL_BUTTON_LEFT] = MouseButton::LEFT_MOUSE; + table[SDL_BUTTON_RIGHT] = MouseButton::RIGHT_MOUSE; + table[SDL_BUTTON_MIDDLE] = MouseButton::MIDDLE_MOUSE; + table[SDL_BUTTON_X1] = MouseButton::X1_MOUSE; + table[SDL_BUTTON_X2] = MouseButton::X2_MOUSE; + + return table; + }(); + + if (sdl_button >= MOUSE_BUTTON_LOOKUP_TABLE.size()) { + // Return NONE for invalid or unmapped button + return MouseButton::NONE; + } + + return MOUSE_BUTTON_LOOKUP_TABLE[sdl_button]; +} void SDLContext::clear_screen() { SDL_RenderClear(this->game_renderer.get()); } void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); } diff --git a/src/test/inputTest.cpp b/src/test/inputTest.cpp index 0f02410..6b820ee 100644 --- a/src/test/inputTest.cpp +++ b/src/test/inputTest.cpp @@ -1,10 +1,18 @@ +#include <gtest/gtest.h> +#define protected public #include <SDL2/SDL.h> #include <SDL2/SDL_keycode.h> #include "system/InputSystem.h" +#include <crepe/ComponentManager.h> #include "api/EventManager.h" #include "api/KeyCodes.h" #include <gmock/gmock.h> -#include <gtest/gtest.h> +#include <crepe/ComponentManager.h> +#include <crepe/api/GameObject.h> +#include <crepe/api/Metadata.h> +#include <crepe/api/Transform.h> +#include <crepe/api/Vector2.h> +#include <crepe/api/Button.h> using namespace std; using namespace std::chrono_literals; @@ -12,14 +20,17 @@ using namespace crepe; class InputTest : public ::testing::Test { public: -InputSystem input_system; +ComponentManager mgr{}; +InputSystem input_system{mgr}; + EventManager& event_manager = EventManager::get_instance(); protected: void SetUp() override { + event_manager.clear(); } void TearDown() override { - + event_manager.clear(); } void simulate_mouse_click(int mouse_x, int mouse_y, Uint8 mouse_button) { SDL_Event event; @@ -41,13 +52,127 @@ void simulate_mouse_click(int mouse_x, int mouse_y, Uint8 mouse_button) { }; -TEST_F(InputTest, KeyDown) { - +TEST_F(InputTest, MouseDown) { + bool mouse_triggered = false; + EventHandler<MousePressEvent> on_mouse_click = [&](const MousePressEvent& event) { + // Handle the mouse click event here + mouse_triggered = true; + EXPECT_EQ(event.mouse_x, 10); + EXPECT_EQ(event.mouse_y, 10); + EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE); + return false; + }; + event_manager.subscribe<MousePressEvent>(on_mouse_click); SDL_Event event; SDL_zero(event); event.type = SDL_MOUSEBUTTONDOWN; event.button.x = 10; event.button.y = 10; - event.button.button = mouse_bu; - SDL_PushEvent(&event); // Push event into the SDL event queue + event.button.button = SDL_BUTTON_LEFT; + SDL_PushEvent(&event); + EXPECT_TRUE(mouse_triggered); +} + +TEST_F(InputTest, MouseUp) { + EventHandler<MouseReleaseEvent> on_mouse_click = [&](const MouseReleaseEvent& event) { + // Handle the mouse click event here + EXPECT_EQ(event.mouse_x, 10); + EXPECT_EQ(event.mouse_y, 10); + EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE); + return false; + }; + event_manager.subscribe<MouseReleaseEvent>(on_mouse_click); + SDL_Event event; + SDL_zero(event); + event.type = SDL_MOUSEBUTTONUP; + event.button.x = 10; + event.button.y = 10; + event.button.button = SDL_BUTTON_LEFT; + SDL_PushEvent(&event); +} +TEST_F(InputTest, MouseUp) { + EventHandler<MouseReleaseEvent> on_mouse_click = [&](const MouseReleaseEvent& event) { + // Handle the mouse click event here + EXPECT_EQ(event.mouse_x, 10); + EXPECT_EQ(event.mouse_y, 10); + EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE); + return false; + }; + event_manager.subscribe<MouseReleaseEvent>(on_mouse_click); + SDL_Event event; + SDL_zero(event); + event.type = SDL_MOUSEBUTTONUP; + event.button.x = 10; + event.button.y = 10; + event.button.button = SDL_BUTTON_LEFT; + SDL_PushEvent(&event); +} +TEST_F(InputTest, KeyDown) { + EventHandler<KeyPressEvent> on_mouse_click = [&](const KeyPressEvent& event) { + // Handle the mouse click event here + EXPECT_EQ(event.key, Keycode::B); + EXPECT_EQ(event.repeat, true); + return false; + }; + event_manager.subscribe<KeyPressEvent>(on_mouse_click); + SDL_Event event; + SDL_zero(event); + event.type = SDL_KEYUP; + event.button.button = SDL_BUTTON_LEFT; + event.key.repeat = 1; + SDL_PushEvent(&event); +} +TEST_F(InputTest, KeyUp) { + EventHandler<KeyReleaseEvent> on_mouse_click = [&](const KeyReleaseEvent& event) { + // Handle the mouse click event here + EXPECT_EQ(event.key, Keycode::B); + return false; + }; + event_manager.subscribe<KeyReleaseEvent>(on_mouse_click); + SDL_Event event; + SDL_zero(event); + event.type = SDL_KEYUP; + event.button.button = SDL_BUTTON_LEFT; + SDL_PushEvent(&event); +} +TEST_F(InputTest, KeyClick) { + GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); + Button test_button; + bool button_clicked = false; + test_button.active = true; + test_button.width = 100; + test_button.height = 100; + std::function<void()> on_click = [&]() { + button_clicked = true; + }; + test_button.on_click = on_click; + test_button.is_pressed = false; + test_button.is_toggle = false; + obj.add_component<Button>(); + EventHandler<KeyReleaseEvent> on_mouse_click = [&](const KeyReleaseEvent& event) { + EXPECT_EQ(event.key, Keycode::B); + return false; + }; + event_manager.subscribe<KeyReleaseEvent>(on_mouse_click); + SDL_Event event; + SDL_zero(event); + event.type = SDL_KEYUP; + event.button.button = SDL_BUTTON_LEFT; + SDL_PushEvent(&event); +} +TEST_F(InputTest, testButton) { + GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); + Button test_button; + bool button_clicked = false; + test_button.active = true; + test_button.width = 100; + test_button.height = 100; + std::function<void()> on_click = [&]() { + button_clicked = true; + }; + test_button.on_click = on_click; + test_button.is_pressed = false; + test_button.is_toggle = false; + obj.add_component<Button>(); + this->simulate_mouse_click(10,10,MouseButton::LEFT_MOUSE); } |