From 18c722d07d5e5395b9f8ef02665e062c41c59b07 Mon Sep 17 00:00:00 2001 From: ezveee Date: Sun, 23 Jun 2024 15:48:01 +0200 Subject: [PATCH] added alternative to unbindAction template function --- include/InputSystemComponent.h | 29 +++++++++++++------------ src/InputSystemComponent.cpp | 39 ++++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 25 deletions(-) diff --git a/include/InputSystemComponent.h b/include/InputSystemComponent.h index d0fab03..5da21b5 100644 --- a/include/InputSystemComponent.h +++ b/include/InputSystemComponent.h @@ -34,22 +34,23 @@ public: // (m_keyToActionsMap[keys].push_back(m_actions[actionName]), ...); // } - // void unbindAction(const std::string& actionName, Key key); + void unbindAction(const std::string& actionName, Key key); + void unbindAction(const std::string& actionName, std::vector key); - template - void unbindAction(const std::string& actionName, Keys... keys) - { - static_assert((std::is_same::value && ...), "A passed argument for 'Keys' is not of type 'Key'"); + // template + // void unbindAction(const std::string& actionName, Keys... keys) + // { + // static_assert((std::is_same::value && ...), "A passed argument for 'Keys' is not of type 'Key'"); - auto actionIt = m_actions.find(actionName); - if (actionIt != m_actions.end()) - { - auto& action = actionIt->second; - (action.keys.erase(std::remove(action.keys.begin(), action.keys.end(), keys), action.keys.end()), ...); - (m_keyToActionsMap[keys].erase(std::remove_if(m_keyToActionsMap[keys].begin(), m_keyToActionsMap[keys].end(), - [&](const InputAction& a) { return a.actionName == actionName; }), m_keyToActionsMap[keys].end()), ...); - } - } + // auto actionIt = m_actions.find(actionName); + // if (actionIt != m_actions.end()) + // { + // auto& action = actionIt->second; + // (action.keys.erase(std::remove(action.keys.begin(), action.keys.end(), keys), action.keys.end()), ...); + // (m_keyToActionsMap[keys].erase(std::remove_if(m_keyToActionsMap[keys].begin(), m_keyToActionsMap[keys].end(), + // [&](const InputAction& a) { return a.actionName == actionName; }), m_keyToActionsMap[keys].end()), ...); + // } + // } private: InputComponent* m_inputComponent; diff --git a/src/InputSystemComponent.cpp b/src/InputSystemComponent.cpp index 9405e94..6a78202 100644 --- a/src/InputSystemComponent.cpp +++ b/src/InputSystemComponent.cpp @@ -39,17 +39,34 @@ void InputSystemComponent::bindAction(const std::string& actionName, std::vector } } -// void InputSystemComponent::unbindAction(const std::string& actionName, Key key) -// { -// auto actionIt = m_actions.find(actionName); -// if (actionIt != m_actions.end()) -// { -// auto& action = actionIt->second; -// action.keys.erase(std::remove(action.keys.begin(), action.keys.end(), key), action.keys.end()); -// m_keyToActionsMap[key].erase(std::remove_if(m_keyToActionsMap[key].begin(), m_keyToActionsMap[key].end(), -// [&](const InputAction& a) { return a.name == actionName; }), m_keyToActionsMap[key].end()); -// } -// } +// alternative to unbindAction template +void InputSystemComponent::unbindAction(const std::string& actionName, Key key) +{ + auto actionIt = m_actions.find(actionName); + if (actionIt != m_actions.end()) + { + auto& action = actionIt->second; + action.keys.erase(std::remove(action.keys.begin(), action.keys.end(), key), action.keys.end()); + m_keyToActionsMap[key].erase(std::remove_if(m_keyToActionsMap[key].begin(), m_keyToActionsMap[key].end(), + [&](const InputAction& a) { return a.name == actionName; }), m_keyToActionsMap[key].end()); + } +} + +// alternative to unbindAction template +void InputSystemComponent::unbindAction(const std::string& actionName, const std::vector& keysToRemove) +{ + for (Key key : keysToRemove) + { + auto actionIt = m_actions.find(actionName); + if (actionIt != m_actions.end()) + { + auto& action = actionIt->second; + action.keys.erase(std::remove(action.keys.begin(), action.keys.end(), key), action.keys.end()); + m_keyToActionsMap[key].erase(std::remove_if(m_keyToActionsMap[key].begin(), m_keyToActionsMap[key].end(), + [&](const InputAction& a) { return a.actionName == actionName; }), m_keyToActionsMap[key].end()); + } + } +} void InputSystemComponent::handleActions() {