aboutsummaryrefslogtreecommitdiff
path: root/src/engine/user/input.cpp
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-03-20 13:17:16 +0100
committerHampusM <hampus@hampusmat.com>2022-06-13 17:56:56 +0200
commit78f5d8cf644383adf20933ad64c160c07c2c54fb (patch)
tree2a348d90f8f150665c379bf2f3741731999dd679 /src/engine/user/input.cpp
parentb6061eff99907495de282f611e8389e6468a1fb0 (diff)
refactor: improve input configuring structure
Diffstat (limited to 'src/engine/user/input.cpp')
-rw-r--r--src/engine/user/input.cpp26
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));
+}