aboutsummaryrefslogtreecommitdiff
path: root/src/engine
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
parentb6061eff99907495de282f611e8389e6468a1fb0 (diff)
refactor: improve input configuring structure
Diffstat (limited to 'src/engine')
-rw-r--r--src/engine/engine.cpp37
-rw-r--r--src/engine/engine.hpp6
-rw-r--r--src/engine/user/input.cpp26
-rw-r--r--src/engine/user/input.hpp13
4 files changed, 33 insertions, 49 deletions
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<char, Callback> 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<char, Callback> &input_config) noexcept
+ const std::unordered_map<char, std::shared_ptr<ICommand>> &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<ICursorController> _cursor_controller;
std::shared_ptr<IWindow> _window;
- void
- _configure_input(const std::unordered_map<char, Callback> &input_config) noexcept;
+ void _configure_input(
+ const std::unordered_map<char, std::shared_ptr<ICommand>> &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<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;
};