From dea55572322ba12f51eb310a7f7a49b186594c33 Mon Sep 17 00:00:00 2001 From: ezveee Date: Thu, 16 Jan 2025 19:28:05 +0100 Subject: [PATCH] feat: added rebinding functionality to InputManager --- include/InputManager.h | 4 ++++ src/InputManager.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/include/InputManager.h b/include/InputManager.h index cea015e..b81ba0f 100644 --- a/include/InputManager.h +++ b/include/InputManager.h @@ -112,6 +112,10 @@ public: void setActiveContext(const std::string& context); std::string getActiveContext() const; + void rebindAction(const std::string& actionName, const std::vector& newBindings, const std::string& context); + void removeBindings(const std::string& actionName, const std::string& context); + std::vector getBindings(const std::string& actionName, const std::string& context) const; + private: std::map> actionsByContext; std::map keyMap; diff --git a/src/InputManager.cpp b/src/InputManager.cpp index 278c70a..37077f7 100644 --- a/src/InputManager.cpp +++ b/src/InputManager.cpp @@ -105,6 +105,47 @@ void InputManager::registerAction(const std::string& actionName, const std::vect std::cout << "Registered action: " << actionName << " in context: " << context << std::endl; } +void InputManager::rebindAction(const std::string& actionName, const std::vector& newBindings, const std::string& context) { + auto it = actionsByContext.find(context); + if (it != actionsByContext.end()) { + for (auto& action : it->second) { + if (action.name == actionName) { + action.bindings = newBindings; + std::cout << "Rebound action: " << actionName << " in context: " << context << " to new bindings.\n"; + return; + } + } + } + std::cout << "Action not found: " << actionName << " in context: " << context << std::endl; +} + +void InputManager::removeBindings(const std::string& actionName, const std::string& context) { + auto it = actionsByContext.find(context); + if (it != actionsByContext.end()) { + for (auto& action : it->second) { + if (action.name == actionName) { + action.bindings.clear(); + std::cout << "Removed bindings for action: " << actionName << " in context: " << context << std::endl; + return; + } + } + } + std::cout << "Action not found: " << actionName << " in context: " << context << std::endl; +} + +std::vector InputManager::getBindings(const std::string& actionName, const std::string& context) const { + auto it = actionsByContext.find(context); + if (it != actionsByContext.end()) { + for (const auto& action : it->second) { + if (action.name == actionName) { + return action.bindings; + } + } + } + std::cout << "Action not found: " << actionName << " in context: " << context << "\n"; + return {}; +} + void InputManager::setActiveContext(const std::string& context) { activeContext = context; std::cout << "Active context set to: " << activeContext << std::endl;