From d924e7dbca777700a9c45170bf20f40533c079b9 Mon Sep 17 00:00:00 2001 From: Jan Mrna Date: Sun, 5 Oct 2025 20:17:24 +0200 Subject: [PATCH] Refactor mouse and keyboard events --- cpp/src/user_input.cpp | 87 +++++++++++++++++++++++++----------------- cpp/src/user_input.hpp | 3 ++ 2 files changed, 55 insertions(+), 35 deletions(-) diff --git a/cpp/src/user_input.cpp b/cpp/src/user_input.cpp index 59dd83b..c9cfc5f 100644 --- a/cpp/src/user_input.cpp +++ b/cpp/src/user_input.cpp @@ -19,21 +19,43 @@ UserInput::~UserInput() { LOG_DEBUG("."); }; std::expected UserInput::Init() { return {}; } -const std::vector &UserInput::GetActions() { - m_Actions.clear(); - SDL_Event event; - - static WindowPos mouse_pan_start; +void UserInput::GetActions_mouse(const SDL_Event& event) +{ static bool mouse_pan = false; - while (SDL_PollEvent(&event)) { - // TODO refactor mouse / kbd handling into separate functions - if (event.type == SDL_EVENT_KEY_DOWN || event.type == SDL_EVENT_KEY_UP) { + if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) { + SDL_MouseButtonEvent mouse_event = event.button; + MouseButton button = static_cast(mouse_event.button); + if (button == MouseButton::LEFT) { + LOG_DEBUG("Mouse down: ", mouse_event.x, ", ", mouse_event.y); + m_Actions.emplace_back(UserAction::Type::SET_MOVE_TARGET, + WindowPos{mouse_event.x, mouse_event.y}); + } else if (button == MouseButton::MIDDLE) { + mouse_pan = true; + } + } else if (event.type == SDL_EVENT_MOUSE_BUTTON_UP) { + SDL_MouseButtonEvent mouse_event = event.button; + MouseButton button = static_cast(mouse_event.button); + if (button == MouseButton::MIDDLE) { + mouse_pan = false; + } + } else if (event.type == SDL_EVENT_MOUSE_MOTION) { + SDL_MouseMotionEvent motion_event = event.motion; + if (mouse_pan) { + m_Actions.emplace_back(UserAction::Type::CAMERA_PAN, + WindowPos{motion_event.xrel, motion_event.yrel}); + + } + } +} + +void UserInput::GetActions_kbd(const SDL_Event& event) +{ bool key_down = event.type == SDL_EVENT_KEY_DOWN ? true : false; SDL_KeyboardEvent kbd_event = event.key; if (kbd_event.repeat) { // SDL repeats KEY_DOWN if key is held down, we ignore that - continue; + return; } LOG_DEBUG("Key '", static_cast(kbd_event.key), key_down ? "' down" : "' up"); @@ -41,8 +63,7 @@ const std::vector &UserInput::GetActions() { switch (kbd_event.key) { case 'q': m_Actions.emplace_back(UserAction::Type::EXIT); - // further processing of inputs is not needed - return m_Actions; + return; case '1': case '2': case '3': @@ -57,30 +78,26 @@ const std::vector &UserInput::GetActions() { LOG_INFO("Key '", static_cast(kbd_event.key), "' not mapped"); break; } - } else if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) { - SDL_MouseButtonEvent mouse_event = event.button; - MouseButton button = static_cast(mouse_event.button); - if (button == MouseButton::LEFT) { - LOG_DEBUG("Mouse down: ", mouse_event.x, ", ", mouse_event.y); - m_Actions.emplace_back(UserAction::Type::SET_MOVE_TARGET, - WindowPos{mouse_event.x, mouse_event.y}); - } else if (button == MouseButton::MIDDLE) { - mouse_pan = true; - } - } else if (event.type == SDL_EVENT_MOUSE_BUTTON_UP) { - SDL_MouseButtonEvent mouse_event = event.button; - MouseButton button = static_cast(mouse_event.button); - if (button == MouseButton::MIDDLE) { - mouse_pan = false; - } - } else if (event.type == SDL_EVENT_MOUSE_MOTION) { - SDL_MouseMotionEvent motion_event = event.motion; - if (mouse_pan) { - m_Actions.emplace_back(UserAction::Type::CAMERA_PAN, - WindowPos{motion_event.xrel, motion_event.yrel}); - - } - } else { +} + +const std::vector& UserInput::GetActions() { + m_Actions.clear(); + SDL_Event event; + + while (SDL_PollEvent(&event)) { + if (event.type == SDL_EVENT_KEY_DOWN + || event.type == SDL_EVENT_KEY_UP) + { + GetActions_kbd(event); + } + else if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN + || event.type == SDL_EVENT_MOUSE_BUTTON_UP + || event.type == SDL_EVENT_MOUSE_MOTION) + { + GetActions_mouse(event); + } + else + { // TODO uncomment, for now too much noise // LOG_WARNING("Action not processed"); } diff --git a/cpp/src/user_input.hpp b/cpp/src/user_input.hpp index 48a2457..8c6b2fc 100644 --- a/cpp/src/user_input.hpp +++ b/cpp/src/user_input.hpp @@ -49,4 +49,7 @@ public: private: std::vector m_Actions; + + void GetActions_kbd(const SDL_Event&); + void GetActions_mouse(const SDL_Event&); };