From e6644d6b235005de9ba1b9884472fa5f5d8d6074 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 20 Mar 2022 18:49:49 +0100 Subject: refactor: update event system terminology & structure --- src/engine/engine.cpp | 2 +- src/engine/user/input.cpp | 24 +++++++++++++----------- src/engine/user/input.hpp | 17 +++++++++-------- 3 files changed, 23 insertions(+), 20 deletions(-) (limited to 'src/engine') diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index ed81ed3..e463f28 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -49,6 +49,6 @@ void CLIGameEngine::_configure_input( auto key = config_pair.first; auto command = config_pair.second; - _input_handler->attach(key, command); + _input_handler->subscribe(key, command); } } 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 &command) noexcept +void InputHandler::subscribe( + const Event &event, const std::shared_ptr> &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(key)); + return static_cast<_SubscribersSizeType>(static_cast(event)); } diff --git a/src/engine/user/input.hpp b/src/engine/user/input.hpp index d2aa447..7a3c88e 100644 --- a/src/engine/user/input.hpp +++ b/src/engine/user/input.hpp @@ -1,9 +1,8 @@ #pragma once #include "DI/auto_wirable.hpp" -#include "interfaces/command.hpp" #include "interfaces/input.hpp" -#include "interfaces/observable.hpp" +#include "interfaces/subscriber.hpp" #include #include @@ -18,21 +17,23 @@ public: void listen() const noexcept override; - void attach(const char &key, - const std::shared_ptr &command) noexcept override; + void + subscribe(const Event &event, + const std::shared_ptr> &subscriber) noexcept override; - void notify(const char &key) const noexcept override; + void notify_subscribers(const Event &event, + const Context &context) const noexcept override; void enter_raw_mode() noexcept override; void leave_raw_mode() noexcept override; private: - std::array>, CHAR_MAX> _key_commands; + std::array>>, CHAR_MAX> _subscribers; std::shared_ptr _original_termios = nullptr; - using _KeyCommandsSizeType = decltype(_key_commands)::size_type; + using _SubscribersSizeType = decltype(_subscribers)::size_type; - static _KeyCommandsSizeType _key_as_index(const char &key) noexcept; + static _SubscribersSizeType _event_as_index(const Event &event) noexcept; }; -- cgit v1.2.3-18-g5258