From 2b3659c8c5dace0ff9ff9cb8b9421f7f3f890218 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Fri, 13 Dec 2024 09:15:56 +0100 Subject: added window events back to code and function cleanup --- src/crepe/api/Event.h | 10 +- src/crepe/api/KeyCodes.h | 2 - src/crepe/facade/SDLContext.cpp | 72 ++++++------ src/crepe/facade/SDLContext.h | 5 - src/crepe/system/InputSystem.cpp | 239 ++++++++++++++++++++------------------- src/crepe/system/InputSystem.h | 37 ++++-- src/test/InputTest.cpp | 52 +-------- 7 files changed, 196 insertions(+), 221 deletions(-) (limited to 'src') diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h index 66dd0cc..17ae809 100644 --- a/src/crepe/api/Event.h +++ b/src/crepe/api/Event.h @@ -40,7 +40,7 @@ public: class MousePressEvent : public Event { public: //! mouse position - ivec2 mouse_pos = {0, 0}; + vec2 mouse_pos = {0, 0}; //! The mouse button that was pressed. MouseButton button = MouseButton::NONE; @@ -52,7 +52,7 @@ public: class MouseClickEvent : public Event { public: //! mouse position - ivec2 mouse_pos = {0, 0}; + vec2 mouse_pos = {0, 0}; //! The mouse button that was clicked. MouseButton button = MouseButton::NONE; @@ -64,7 +64,7 @@ public: class MouseReleaseEvent : public Event { public: //! mouse position - ivec2 mouse_pos = {0, 0}; + vec2 mouse_pos = {0, 0}; //! The mouse button that was released. MouseButton button = MouseButton::NONE; @@ -76,7 +76,7 @@ public: class MouseMoveEvent : public Event { public: //! new mouse position - ivec2 mouse_pos = {0, 0}; + vec2 mouse_pos = {0, 0}; //! The change in mouse position relative to the last position (in pixels). ivec2 mouse_delta = {0, 0}; }; @@ -87,7 +87,7 @@ public: class MouseScrollEvent : public Event { public: //! mouse position when the scroll happened. - ivec2 mouse_pos = {0, 0}; + vec2 mouse_pos = {0, 0}; //! scroll direction (-1 = down, 1 = up) int scroll_direction = 0; //! scroll amount in y axis (from and away from the person). diff --git a/src/crepe/api/KeyCodes.h b/src/crepe/api/KeyCodes.h index fa0d17c..a9141e2 100644 --- a/src/crepe/api/KeyCodes.h +++ b/src/crepe/api/KeyCodes.h @@ -153,8 +153,6 @@ typedef enum { RIGHT_SUPER = 347, /// \} MENU = 348, //!< Menu key. - //! Not actually a key instead its the amount of keycodes there are for array indexing - NUM_KEYCODES = 512, } Keycode; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 6e47561..7c7d58e 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -1,4 +1,3 @@ -#include #include #include #include @@ -86,7 +85,6 @@ Keycode SDLContext::sdl_to_keycode(SDL_Scancode sdl_key) { } void SDLContext::update_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); @@ -284,8 +282,6 @@ std::vector SDLContext::get_events() { ivec2 mouse_pos; mouse_pos.x = (event.button.x - cam.bar_size.x) / cam.render_scale.x; mouse_pos.y = (event.button.y - cam.bar_size.y) / cam.render_scale.y; - std::cout << "SDL mousePos X: " << mouse_pos.x << std::endl; - std::cout << "SDL mousePos Y: " << mouse_pos.y << std::endl; switch (event.type) { case SDL_QUIT: event_list.push_back({.event_type = SDLContext::EventType::SHUTDOWN}); @@ -357,44 +353,52 @@ std::vector SDLContext::get_events() { }; event_list.push_back(transfer_event); } break; + case SDL_WINDOWEVENT: + handle_window_event(event.window, event_list); + break; } } return event_list; } -// Separate function for SDL_WINDOWEVENT subtypes void SDLContext::handle_window_event(const SDL_WindowEvent & window_event, std::vector & event_list) { - // switch (window_event.event) { - // case SDL_WINDOWEVENT_EXPOSED: - // event_list.push_back({SDLContext::EventType::WINDOW_EXPOSE, {}, {}, {}}); - // break; - // case SDL_WINDOWEVENT_RESIZED: - // event_list.push_back({SDLContext::EventType::WINDOW_RESIZE, - // {}, - // {}, - // {{}, {window_event.data1, window_event.data2}}}); - // break; - // case SDL_WINDOWEVENT_MOVED: - // event_list.push_back({SDLContext::EventType::WINDOW_MOVE, - // {}, - // {}, - // {{window_event.data1, window_event.data2}, {}}}); - // break; - // case SDL_WINDOWEVENT_MINIMIZED: - // event_list.push_back({SDLContext::EventType::WINDOW_MINIMIZE, {}, {}, {}}); - // break; - // case SDL_WINDOWEVENT_MAXIMIZED: - // event_list.push_back({SDLContext::EventType::WINDOW_MAXIMIZE, {}, {}, {}}); - // break; - // case SDL_WINDOWEVENT_FOCUS_GAINED: - // event_list.push_back({SDLContext::EventType::WINDOW_FOCUS_GAIN, {}, {}, {}}); - // break; - // case SDL_WINDOWEVENT_FOCUS_LOST: - // event_list.push_back({SDLContext::EventType::WINDOW_FOCUS_LOST, {}, {}, {}}); - // break; - // } + switch (window_event.event) { + case SDL_WINDOWEVENT_EXPOSED: + event_list.push_back(EventData{SDLContext::EventType::WINDOW_EXPOSE}); + break; + case SDL_WINDOWEVENT_RESIZED: { + EventData transfer_event; + transfer_event.event_type = SDLContext::EventType::WINDOW_RESIZE; + transfer_event.data.window_data = WindowData{ + .resize_dimension = {window_event.data1, window_event.data2} + }; + event_list.push_back(transfer_event); + break; + } + case SDL_WINDOWEVENT_MOVED: { + EventData transfer_event; + transfer_event.event_type = SDLContext::EventType::WINDOW_MOVE; + transfer_event.data.window_data = WindowData{ + .move_delta = {window_event.data1, window_event.data2} + }; + event_list.push_back(transfer_event); + break; + } + case SDL_WINDOWEVENT_MINIMIZED: + event_list.push_back(EventData{SDLContext::EventType::WINDOW_MINIMIZE}); + break; + case SDL_WINDOWEVENT_MAXIMIZED: + event_list.push_back(EventData{SDLContext::EventType::WINDOW_MAXIMIZE}); + break; + case SDL_WINDOWEVENT_FOCUS_GAINED: + event_list.push_back(EventData{SDLContext::EventType::WINDOW_FOCUS_GAIN}); + break; + case SDL_WINDOWEVENT_FOCUS_LOST: + event_list.push_back(EventData{SDLContext::EventType::WINDOW_FOCUS_LOST}); + break; + } } void SDLContext::set_color_texture(const Texture & texture, const Color & color) { diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index b10bcfe..e56d531 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -116,11 +116,6 @@ public: EventDataUnion() {} ~EventDataUnion() {} } data; - - // Helper functions - // bool isKeyEvent() const { return event_type == SDLContext::EventType::KEYDOWN || event_type == SDLContext::EventType::KEYUP; } - // bool isMouseEvent() const { return event_type == SDLContext::EventType::MOUSEDOWN || event_type == SDLContext::EventType::MOUSEUP || event_type == SDLContext::EventType::MOUSEMOVE; } - // bool isWindowEvent() const { return event_type == SDLContext::EventType::WINDOW_MINIMIZE || event_type == SDLContext::EventType::WINDOW_RESIZE; } }; /** * \brief Retrieves the current state of the keyboard. diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index 32538e8..bb454af 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -1,4 +1,3 @@ -#include #include "../api/Button.h" #include "../manager/ComponentManager.h" #include "../manager/EventManager.h" @@ -11,7 +10,7 @@ using namespace crepe; void InputSystem::update() { ComponentManager & mgr = this->mediator.component_manager; - EventManager & event_mgr = this->mediator.event_manager; + SDLContext & context = this->mediator.sdl_context; std::vector event_list = context.get_events(); RefVector