diff --git a/include/InputSystemComponent.h b/include/InputSystemComponent.h index f0be5d4..d0fab03 100644 --- a/include/InputSystemComponent.h +++ b/include/InputSystemComponent.h @@ -8,7 +8,8 @@ #include "InputComponent.h" #include "InputAction.h" -class InputSystemComponent : public InputComponent { +class InputSystemComponent : public InputComponent +{ public: InputSystemComponent() = default; ~InputSystemComponent() = default; @@ -33,7 +34,22 @@ 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); + + 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()), ...); + } + } private: InputComponent* m_inputComponent; diff --git a/src/InputSystemComponent.cpp b/src/InputSystemComponent.cpp index 0bab05f..9405e94 100644 --- a/src/InputSystemComponent.cpp +++ b/src/InputSystemComponent.cpp @@ -11,7 +11,7 @@ void InputSystemComponent::update() handleActions(); } -// alternative to template function +// alternative to bindAction template void InputSystemComponent::bindAction(const std::string& actionName, Key key, std::function callback) { if (m_actions.find(actionName) == m_actions.end()) @@ -24,7 +24,7 @@ void InputSystemComponent::bindAction(const std::string& actionName, Key key, st } -// alternative to template function +// alternative to bindAction template void InputSystemComponent::bindAction(const std::string& actionName, std::vector keys, std::function callback) { if (m_actions.find(actionName) == m_actions.end()) @@ -39,17 +39,17 @@ 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()); - } -} +// 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()); +// } +// } void InputSystemComponent::handleActions() {