From f5304d045d0c6f136bc5dc686e96ed640c96e140 Mon Sep 17 00:00:00 2001 From: Jan Mrna Date: Sun, 5 Oct 2025 20:45:09 +0200 Subject: [PATCH] Refactor user input --- cpp/src/user_input.cpp | 34 ++++++++++++++++++++++++---------- cpp/src/user_input.hpp | 2 +- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/cpp/src/user_input.cpp b/cpp/src/user_input.cpp index c9cfc5f..9d65e6e 100644 --- a/cpp/src/user_input.cpp +++ b/cpp/src/user_input.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "user_input.hpp" @@ -49,7 +50,7 @@ void UserInput::GetActions_mouse(const SDL_Event& event) } } -void UserInput::GetActions_kbd(const SDL_Event& event) +void UserInput::GetActions_keyboard(const SDL_Event& event) { bool key_down = event.type == SDL_EVENT_KEY_DOWN ? true : false; SDL_KeyboardEvent kbd_event = event.key; @@ -81,18 +82,31 @@ void UserInput::GetActions_kbd(const SDL_Event& event) } const std::vector& UserInput::GetActions() { - m_Actions.clear(); - SDL_Event event; + + static std::unordered_set mouse_events = { + SDL_EVENT_MOUSE_MOTION, + SDL_EVENT_MOUSE_BUTTON_DOWN, + SDL_EVENT_MOUSE_BUTTON_UP, + SDL_EVENT_MOUSE_WHEEL, + SDL_EVENT_MOUSE_ADDED, + SDL_EVENT_MOUSE_REMOVED, + }; - while (SDL_PollEvent(&event)) { - if (event.type == SDL_EVENT_KEY_DOWN - || event.type == SDL_EVENT_KEY_UP) + static std::unordered_set keyboard_events = { + SDL_EVENT_KEY_DOWN, + SDL_EVENT_KEY_UP, + }; + + SDL_Event event; + m_Actions.clear(); + + while (SDL_PollEvent(&event)) + { + if (keyboard_events.contains(event.type)) { - GetActions_kbd(event); + GetActions_keyboard(event); } - else if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN - || event.type == SDL_EVENT_MOUSE_BUTTON_UP - || event.type == SDL_EVENT_MOUSE_MOTION) + else if (mouse_events.contains(event.type)) { GetActions_mouse(event); } diff --git a/cpp/src/user_input.hpp b/cpp/src/user_input.hpp index 8c6b2fc..8ed139b 100644 --- a/cpp/src/user_input.hpp +++ b/cpp/src/user_input.hpp @@ -50,6 +50,6 @@ public: private: std::vector m_Actions; - void GetActions_kbd(const SDL_Event&); + void GetActions_keyboard(const SDL_Event&); void GetActions_mouse(const SDL_Event&); };