From 31e29713de4bd243d2ba027d03a6c51114e74b10 Mon Sep 17 00:00:00 2001 From: ezveee Date: Thu, 16 Jan 2025 17:29:16 +0100 Subject: [PATCH] fix: fix issue with context switching by adding action grouping by context --- include/InputManager.h | 2 +- src/InputManager.cpp | 18 ++++++------------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/include/InputManager.h b/include/InputManager.h index 27a7503..3f360f4 100644 --- a/include/InputManager.h +++ b/include/InputManager.h @@ -114,7 +114,7 @@ public: std::string getActiveContext() const; private: - std::vector actions; + std::map> actionsByContext; std::map keyMap; std::string activeContext; diff --git a/src/InputManager.cpp b/src/InputManager.cpp index 9074306..278c70a 100644 --- a/src/InputManager.cpp +++ b/src/InputManager.cpp @@ -101,7 +101,8 @@ void InputManager::initKeyMap() { } void InputManager::registerAction(const std::string& actionName, const std::vector& keys, std::function callback, const std::string& context) { - actions.push_back({actionName, keys, callback, context}); + actionsByContext[context].push_back({actionName, keys, callback}); + std::cout << "Registered action: " << actionName << " in context: " << context << std::endl; } void InputManager::setActiveContext(const std::string& context) { @@ -124,19 +125,12 @@ void InputManager::processEvents() { void InputManager::handleEvent(const SDL_Event& event) { if (event.type != SDL_EVENT_KEY_DOWN) return; // TODO: add other events if necessary - for (const auto& action : actions) { - std::cout << "Processing action " << action.name - << " with context " << action.context - << " (Active context: " << activeContext << ")" << std::endl; - - if (action.context != activeContext) { - std::cout << "Skipping action: " << action.name << std::endl; - continue; - } + auto& contextActions = actionsByContext[activeContext]; + for (const auto& action : contextActions) { for (const auto& binding : action.bindings) { - if (event.key.scancode == keyMap[binding] && action.context == activeContext) { - std::cout << "Action triggered: " << action.name << " in context: " << action.context << std::endl; + if (event.key.scancode == keyMap[binding]) { + std::cout << "Action triggered: " << action.name << " in context: " << activeContext << std::endl; action.callback(); return; }