Refactor user input

This commit is contained in:
Jan Mrna 2025-10-05 20:45:09 +02:00 committed by Mrna
parent d924e7dbca
commit f5304d045d
2 changed files with 25 additions and 11 deletions

View File

@ -3,6 +3,7 @@
#include <map> #include <map>
#include <string> #include <string>
#include <vector> #include <vector>
#include <unordered_set>
#include "user_input.hpp" #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; bool key_down = event.type == SDL_EVENT_KEY_DOWN ? true : false;
SDL_KeyboardEvent kbd_event = event.key; SDL_KeyboardEvent kbd_event = event.key;
@ -81,18 +82,31 @@ void UserInput::GetActions_kbd(const SDL_Event& event)
} }
const std::vector<UserAction>& UserInput::GetActions() { const std::vector<UserAction>& UserInput::GetActions() {
m_Actions.clear();
SDL_Event event;
while (SDL_PollEvent(&event)) { static std::unordered_set<uint32_t> mouse_events = {
if (event.type == SDL_EVENT_KEY_DOWN SDL_EVENT_MOUSE_MOTION,
|| event.type == SDL_EVENT_KEY_UP) SDL_EVENT_MOUSE_BUTTON_DOWN,
SDL_EVENT_MOUSE_BUTTON_UP,
SDL_EVENT_MOUSE_WHEEL,
SDL_EVENT_MOUSE_ADDED,
SDL_EVENT_MOUSE_REMOVED,
};
static std::unordered_set<uint32_t> keyboard_events = {
SDL_EVENT_KEY_DOWN,
SDL_EVENT_KEY_UP,
};
SDL_Event event;
m_Actions.clear();
while (SDL_PollEvent(&event))
{ {
GetActions_kbd(event); if (keyboard_events.contains(event.type))
{
GetActions_keyboard(event);
} }
else if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN else if (mouse_events.contains(event.type))
|| event.type == SDL_EVENT_MOUSE_BUTTON_UP
|| event.type == SDL_EVENT_MOUSE_MOTION)
{ {
GetActions_mouse(event); GetActions_mouse(event);
} }

View File

@ -50,6 +50,6 @@ public:
private: private:
std::vector<UserAction> m_Actions; std::vector<UserAction> m_Actions;
void GetActions_kbd(const SDL_Event&); void GetActions_keyboard(const SDL_Event&);
void GetActions_mouse(const SDL_Event&); void GetActions_mouse(const SDL_Event&);
}; };