From a130d865dd75a15927360bb442043f32b54f6108 Mon Sep 17 00:00:00 2001 From: ezveee Date: Thu, 16 Jan 2025 17:18:24 +0100 Subject: [PATCH] feat: added context for input actions (bug: issue with context switching) --- include/InputManager.h | 7 ++++++- src/InputManager.cpp | 29 ++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/include/InputManager.h b/include/InputManager.h index 6aed753..27a7503 100644 --- a/include/InputManager.h +++ b/include/InputManager.h @@ -100,6 +100,7 @@ public: std::string name; std::vector bindings; std::function callback; + std::string context; }; InputManager(); @@ -107,11 +108,15 @@ public: void init(); // see if necessary void processEvents(); - void registerAction(const std::string& actionName, const std::vector& keys, std::function callback); + void registerAction(const std::string& actionName, const std::vector& keys, std::function callback, const std::string& context); + + void setActiveContext(const std::string& context); + std::string getActiveContext() const; private: std::vector actions; std::map keyMap; + std::string activeContext; void initKeyMap(); void handleEvent(const SDL_Event& event); diff --git a/src/InputManager.cpp b/src/InputManager.cpp index 4b4e0f1..9074306 100644 --- a/src/InputManager.cpp +++ b/src/InputManager.cpp @@ -1,7 +1,7 @@ #include "InputManager.h" #include -InputManager::InputManager() { +InputManager::InputManager() : activeContext("Default") { initKeyMap(); } @@ -100,8 +100,17 @@ void InputManager::initKeyMap() { }; } -void InputManager::registerAction(const std::string& actionName, const std::vector& keys, std::function callback) { - actions.push_back({actionName, keys, callback}); +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}); +} + +void InputManager::setActiveContext(const std::string& context) { + activeContext = context; + std::cout << "Active context set to: " << activeContext << std::endl; +} + +std::string InputManager::getActiveContext() const { + return activeContext; } void InputManager::processEvents() { @@ -116,10 +125,20 @@ 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; + } + for (const auto& binding : action.bindings) { - if (event.key.scancode == keyMap[binding]) { - std::cout << "Action triggered: " << action.name << std::endl; + if (event.key.scancode == keyMap[binding] && action.context == activeContext) { + std::cout << "Action triggered: " << action.name << " in context: " << action.context << std::endl; action.callback(); + return; } } }