aboutsummaryrefslogtreecommitdiff
path: root/src/engine/user
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/user')
-rw-r--r--src/engine/user/input.cpp26
-rw-r--r--src/engine/user/input.hpp13
2 files changed, 23 insertions, 16 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));
+}
diff --git a/src/engine/user/input.hpp b/src/engine/user/input.hpp
index 3dd7fa9..d2aa447 100644
--- a/src/engine/user/input.hpp
+++ b/src/engine/user/input.hpp
@@ -1,9 +1,11 @@
#pragma once
#include "DI/auto_wirable.hpp"
+#include "interfaces/command.hpp"
#include "interfaces/input.hpp"
#include "interfaces/observable.hpp"
+#include <limits>
#include <memory>
#include <termios.h>
#include <unordered_map>
@@ -16,16 +18,21 @@ public:
void listen() const noexcept override;
- void attach(const char &event, Callback callback) noexcept override;
+ void attach(const char &key,
+ const std::shared_ptr<ICommand> &command) noexcept override;
- void notify(const char &event) const noexcept override;
+ void notify(const char &key) const noexcept override;
void enter_raw_mode() noexcept override;
void leave_raw_mode() noexcept override;
private:
- std::unordered_map<char, std::vector<Callback>> _key_observers;
+ std::array<std::vector<std::shared_ptr<ICommand>>, CHAR_MAX> _key_commands;
std::shared_ptr<termios> _original_termios = nullptr;
+
+ using _KeyCommandsSizeType = decltype(_key_commands)::size_type;
+
+ static _KeyCommandsSizeType _key_as_index(const char &key) noexcept;
};