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

added alternative to unbindAction template function

This commit is contained in:
ezveee 2024-06-23 15:48:01 +02:00
parent cf1df70eee
commit 18c722d07d
2 changed files with 43 additions and 25 deletions

View File

@ -34,22 +34,23 @@ public:
// (m_keyToActionsMap[keys].push_back(m_actions[actionName]), ...); // (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> key);
template<typename... Keys> // template<typename... Keys>
void unbindAction(const std::string& actionName, Keys... keys) // void unbindAction(const std::string& actionName, Keys... keys)
{ // {
static_assert((std::is_same<Keys, Key>::value && ...), "A passed argument for 'Keys' is not of type 'Key'"); // static_assert((std::is_same<Keys, Key>::value && ...), "A passed argument for 'Keys' is not of type 'Key'");
auto actionIt = m_actions.find(actionName); // auto actionIt = m_actions.find(actionName);
if (actionIt != m_actions.end()) // if (actionIt != m_actions.end())
{ // {
auto& action = actionIt->second; // auto& action = actionIt->second;
(action.keys.erase(std::remove(action.keys.begin(), action.keys.end(), keys), action.keys.end()), ...); // (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(), // (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()), ...); // [&](const InputAction& a) { return a.actionName == actionName; }), m_keyToActionsMap[keys].end()), ...);
} // }
} // }
private: private:
InputComponent* m_inputComponent; InputComponent* m_inputComponent;

View File

@ -39,17 +39,34 @@ void InputSystemComponent::bindAction(const std::string& actionName, std::vector
} }
} }
// void InputSystemComponent::unbindAction(const std::string& actionName, Key key) // 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 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()); auto& action = actionIt->second;
// m_keyToActionsMap[key].erase(std::remove_if(m_keyToActionsMap[key].begin(), m_keyToActionsMap[key].end(), action.keys.erase(std::remove(action.keys.begin(), action.keys.end(), key), action.keys.end());
// [&](const InputAction& a) { return a.name == actionName; }), m_keyToActionsMap[key].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<Key>& 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() void InputSystemComponent::handleActions()
{ {