0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 15:53:42 +00:00

Implemented template function for binding actions

This commit is contained in:
Sara Varga 2024-06-23 13:56:02 +02:00
parent cefa1c2e29
commit de74ca1307
2 changed files with 25 additions and 10 deletions

View File

@ -16,7 +16,22 @@ public:
void init() override; void init() override;
void update() override; void update() override;
void bindAction(const std::string& actionName, Key key, std::function<void()> callback); // void bindAction(const std::string& actionName, Key key, std::function<void()> callback);
template<typename... Keys>
void bindAction(const std::string& actionName, std::function<void()> callback, Keys... keys)
{
static_assert((std::is_same<Keys, Key>::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); void unbindAction(const std::string& actionName, Key key);
private: private:

View File

@ -11,17 +11,17 @@ void InputSystemComponent::update()
handleActions(); handleActions();
} }
void InputSystemComponent::bindAction(const std::string& actionName, Key key, std::function<void()> callback) // void InputSystemComponent::bindAction(const std::string& actionName, Key key, std::function<void()> callback)
{ // {
if (m_actions.find(actionName) == m_actions.end()) // if (m_actions.find(actionName) == m_actions.end())
{ // {
m_actions[actionName] = InputAction(actionName, callback); // m_actions[actionName] = InputAction(actionName, callback);
} // }
m_actions[actionName].keys.push_back(key); // m_actions[actionName].keys.push_back(key);
m_keyToActionsMap[key].push_back(m_actions[actionName]); // m_keyToActionsMap[key].push_back(m_actions[actionName]);
} // }
void InputSystemComponent::unbindAction(const std::string& actionName, Key key) void InputSystemComponent::unbindAction(const std::string& actionName, Key key)
{ {