From 78f5d8cf644383adf20933ad64c160c07c2c54fb Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 20 Mar 2022 13:17:16 +0100 Subject: refactor: improve input configuring structure --- src/engine/engine.cpp | 37 +++++++------------------------------ src/engine/engine.hpp | 6 +++--- src/engine/user/input.cpp | 26 +++++++++++++------------- src/engine/user/input.hpp | 13 ++++++++++--- 4 files changed, 33 insertions(+), 49 deletions(-) (limited to 'src/engine') diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index afa5924..b2add9c 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -1,6 +1,5 @@ #include "engine.hpp" -#include "input_actions.hpp" #include "strings.hpp" #include "util/function.hpp" @@ -58,45 +57,23 @@ void CLIGameEngine::start() noexcept std::cout.flush(); })); - const std::unordered_map input_config = { - {'q', InputActions::exit_success}, - {'i', - [this, scene]() - { - const auto position = _cursor_controller->where(); - - std::cout.put('x'); - std::cout.flush(); - - _cursor_controller->move_to(position); - - auto matrix = scene->get_matrix(); - - matrix->set(position - Vector2({.x = 0U, .y = 1U}), "#"); - }}, - {'k', - InputActions::move_cursor(Vector2::up(), _cursor_controller, scene, _window)}, - {'j', - InputActions::move_cursor(Vector2::down(), _cursor_controller, scene, _window)}, - {'h', - InputActions::move_cursor(Vector2::left(), _cursor_controller, scene, _window)}, - {'l', InputActions::move_cursor(Vector2::right(), _cursor_controller, scene, - _window)}}; + auto game = _game_factory(); - _configure_input(input_config); + _configure_input(game->get_input_config(_window, scene, _cursor_controller)); _input_handler->listen(); - auto game = _game_factory(); - game->run(); } void CLIGameEngine::_configure_input( - const std::unordered_map &input_config) noexcept + const std::unordered_map> &input_config) noexcept { for (const auto &config_pair : input_config) { - _input_handler->attach(config_pair.first, config_pair.second); + auto key = config_pair.first; + auto command = config_pair.second; + + _input_handler->attach(key, command); } } diff --git a/src/engine/engine.hpp b/src/engine/engine.hpp index 42cc297..32afa56 100644 --- a/src/engine/engine.hpp +++ b/src/engine/engine.hpp @@ -1,11 +1,11 @@ #pragma once #include "DI/auto_wirable.hpp" +#include "interfaces/command.hpp" #include "interfaces/cursor.hpp" #include "interfaces/engine.hpp" #include "interfaces/game.hpp" #include "interfaces/input.hpp" -#include "interfaces/observable.hpp" #include "interfaces/scene.hpp" #include "interfaces/window.hpp" @@ -33,6 +33,6 @@ private: std::shared_ptr _cursor_controller; std::shared_ptr _window; - void - _configure_input(const std::unordered_map &input_config) noexcept; + void _configure_input( + const std::unordered_map> &input_config) noexcept; }; 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 &command) noexcept { - if (_key_observers.count(event) == 0) - { - _key_observers[event] = std::vector(); - } + 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(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 #include #include #include @@ -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 &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> _key_observers; + std::array>, CHAR_MAX> _key_commands; std::shared_ptr _original_termios = nullptr; + + using _KeyCommandsSizeType = decltype(_key_commands)::size_type; + + static _KeyCommandsSizeType _key_as_index(const char &key) noexcept; }; -- cgit v1.2.3-18-g5258