From de74ca13077c3a889f3a8baaf43b76aee2fd3acd Mon Sep 17 00:00:00 2001 From: Sara Varga Date: Sun, 23 Jun 2024 13:56:02 +0200 Subject: [PATCH] Implemented template function for binding actions --- include/InputSystemComponent.h | 17 ++++++++++++++++- src/InputSystemComponent.cpp | 18 +++++++++--------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/include/InputSystemComponent.h b/include/InputSystemComponent.h index bb1a72d..9050f4e 100644 --- a/include/InputSystemComponent.h +++ b/include/InputSystemComponent.h @@ -16,7 +16,22 @@ public: void init() override; void update() override; - void bindAction(const std::string& actionName, Key key, std::function callback); + // void bindAction(const std::string& actionName, Key key, std::function callback); + + template + void bindAction(const std::string& actionName, std::function callback, Keys... keys) + { + static_assert((std::is_same::value && ...), "A passed argument for 'Keys' is not of type 'Key'"); + + if (m_actions.find(actionName) == m_actions.end()) + { + m_actions[actionName] = InputAction(actionName, callback); + } + + (m_actions[actionName].keys.push_back(keys), ...); + (m_keyToActionsMap[keys].push_back(m_actions[actionName]), ...); + } + void unbindAction(const std::string& actionName, Key key); private: diff --git a/src/InputSystemComponent.cpp b/src/InputSystemComponent.cpp index 13b329f..9bd66af 100644 --- a/src/InputSystemComponent.cpp +++ b/src/InputSystemComponent.cpp @@ -11,17 +11,17 @@ void InputSystemComponent::update() handleActions(); } -void InputSystemComponent::bindAction(const std::string& actionName, Key key, std::function callback) -{ - if (m_actions.find(actionName) == m_actions.end()) - { - m_actions[actionName] = InputAction(actionName, callback); - } +// void InputSystemComponent::bindAction(const std::string& actionName, Key key, std::function callback) +// { +// if (m_actions.find(actionName) == m_actions.end()) +// { +// m_actions[actionName] = InputAction(actionName, callback); +// } - m_actions[actionName].keys.push_back(key); - m_keyToActionsMap[key].push_back(m_actions[actionName]); +// m_actions[actionName].keys.push_back(key); +// m_keyToActionsMap[key].push_back(m_actions[actionName]); -} +// } void InputSystemComponent::unbindAction(const std::string& actionName, Key key) {