diff options
Diffstat (limited to 'src/engine/user/input.cpp')
-rw-r--r-- | src/engine/user/input.cpp | 26 |
1 files changed, 13 insertions, 13 deletions
diff --git a/src/engine/user/input.cpp b/src/engine/user/input.cpp index 62aef67..f1685fd 100644 --- a/src/engine/user/input.cpp +++ b/src/engine/user/input.cpp @@ -12,26 +12,21 @@ void InputHandler::listen() const noexcept } } -void InputHandler::attach(const char &event, Callback callback) noexcept +void InputHandler::attach(const char &key, + const std::shared_ptr<ICommand> &command) noexcept { - if (_key_observers.count(event) == 0) - { - _key_observers[event] = std::vector<Callback>(); - } + auto key_index = _key_as_index(key); - _key_observers[event].push_back(callback); + _key_commands.at(key_index).push_back(command); } -void InputHandler::notify(const char &event) const noexcept +void InputHandler::notify(const char &key) const noexcept { - if (_key_observers.count(event) == 0) - { - return; - } + auto key_index = _key_as_index(key); - for (const auto &observer : _key_observers.at(event)) + for (const auto &command : _key_commands.at(key_index)) { - observer(); + command->execute(); } } @@ -62,3 +57,8 @@ void InputHandler::leave_raw_mode() noexcept _original_termios = nullptr; } + +InputHandler::_KeyCommandsSizeType InputHandler::_key_as_index(const char &key) noexcept +{ + return static_cast<_KeyCommandsSizeType>(static_cast<uint8_t>(key)); +} |