diff options
author | HampusM <hampus@hampusmat.com> | 2022-03-20 18:49:49 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-13 17:56:56 +0200 |
commit | e6644d6b235005de9ba1b9884472fa5f5d8d6074 (patch) | |
tree | b6d10d8e3ee8b092933baa55d35480d438bb6328 /src/engine/user/input.cpp | |
parent | 6f9d9fe1d1904ecc5ca52697ec2319a21008120f (diff) |
refactor: update event system terminology & structure
Diffstat (limited to 'src/engine/user/input.cpp')
-rw-r--r-- | src/engine/user/input.cpp | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/src/engine/user/input.cpp b/src/engine/user/input.cpp index f1685fd..5e6f517 100644 --- a/src/engine/user/input.cpp +++ b/src/engine/user/input.cpp @@ -8,25 +8,26 @@ void InputHandler::listen() const noexcept while (read(STDIN_FILENO, &character, 1) == 1) { - notify(character); + notify_subscribers(character, nullptr); } } -void InputHandler::attach(const char &key, - const std::shared_ptr<ICommand> &command) noexcept +void InputHandler::subscribe( + const Event &event, const std::shared_ptr<ISubscriber<Context>> &subscriber) noexcept { - auto key_index = _key_as_index(key); + auto event_index = _event_as_index(event); - _key_commands.at(key_index).push_back(command); + _subscribers.at(event_index).push_back(subscriber); } -void InputHandler::notify(const char &key) const noexcept +void InputHandler::notify_subscribers(const Event &event, + const Context &context) const noexcept { - auto key_index = _key_as_index(key); + auto event_index = _event_as_index(event); - for (const auto &command : _key_commands.at(key_index)) + for (const auto &subscriber : _subscribers.at(event_index)) { - command->execute(); + subscriber->update(context); } } @@ -58,7 +59,8 @@ void InputHandler::leave_raw_mode() noexcept _original_termios = nullptr; } -InputHandler::_KeyCommandsSizeType InputHandler::_key_as_index(const char &key) noexcept +InputHandler::_SubscribersSizeType +InputHandler::_event_as_index(const Event &event) noexcept { - return static_cast<_KeyCommandsSizeType>(static_cast<uint8_t>(key)); + return static_cast<_SubscribersSizeType>(static_cast<uint8_t>(event)); } |