From cefa1c2e2904c980816de59dad6bb4513f62c87c Mon Sep 17 00:00:00 2001 From: Sara Varga Date: Fri, 21 Jun 2024 22:47:36 +0200 Subject: [PATCH] added InputSystemComponent needs testing --- include/InputComponent.h | 1 + include/InputSystemComponent.h | 28 ++++++++++++++++++ src/InputComponent.cpp | 3 +- src/InputSystemComponent.cpp | 54 ++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 include/InputSystemComponent.h create mode 100644 src/InputSystemComponent.cpp diff --git a/include/InputComponent.h b/include/InputComponent.h index b017184..1f4af80 100644 --- a/include/InputComponent.h +++ b/include/InputComponent.h @@ -1,4 +1,5 @@ #pragma once + #include #include diff --git a/include/InputSystemComponent.h b/include/InputSystemComponent.h new file mode 100644 index 0000000..bb1a72d --- /dev/null +++ b/include/InputSystemComponent.h @@ -0,0 +1,28 @@ +#pragma once + +#include +#include +#include + +#include "Component.h" +#include "InputComponent.h" +#include "InputAction.h" + +class InputSystemComponent : public InputComponent { +public: + InputSystemComponent() = default; + ~InputSystemComponent() = default; + + void init() override; + void update() override; + + void bindAction(const std::string& actionName, Key key, std::function callback); + void unbindAction(const std::string& actionName, Key key); + +private: + InputComponent* m_inputComponent; + std::map> m_keyToActionsMap; + std::map m_actions; + + void handleActions(); +}; diff --git a/src/InputComponent.cpp b/src/InputComponent.cpp index 538ad93..efdf623 100644 --- a/src/InputComponent.cpp +++ b/src/InputComponent.cpp @@ -10,8 +10,7 @@ InputComponent::~InputComponent() = default; void InputComponent::init() { - // m_keyStates = SDL_GetKeyboardState(NULL); - // InitKeyMappings(); + // function intentionally left empty } void InputComponent::update() diff --git a/src/InputSystemComponent.cpp b/src/InputSystemComponent.cpp new file mode 100644 index 0000000..13b329f --- /dev/null +++ b/src/InputSystemComponent.cpp @@ -0,0 +1,54 @@ +#include "InputSystemComponent.h" + +void InputSystemComponent::init() +{ + InputComponent::init(); +} + +void InputSystemComponent::update() +{ + InputComponent::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); + } + + m_actions[actionName].keys.push_back(key); + m_keyToActionsMap[key].push_back(m_actions[actionName]); + +} + +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() +{ + for (auto& keyActionsPair : m_keyToActionsMap) + { + Key key = keyActionsPair.first; + if (isKeyDown(key)) + { + for (auto& action : keyActionsPair.second) + { + if (action.callback) + { + action.callback(); + } + } + } + } +} \ No newline at end of file