From 1972939ebde39a3e90a50b021b2322d028f344de Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 20 Mar 2022 20:37:43 +0100 Subject: refactor: move updating status from the move cursor command --- src/game/cursor_listener.cpp | 18 ++++++++++++++++++ src/game/cursor_listener.hpp | 19 +++++++++++++++++++ src/game/game.cpp | 15 ++++++++++----- 3 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 src/game/cursor_listener.cpp create mode 100644 src/game/cursor_listener.hpp (limited to 'src/game') diff --git a/src/game/cursor_listener.cpp b/src/game/cursor_listener.cpp new file mode 100644 index 0000000..c13ce44 --- /dev/null +++ b/src/game/cursor_listener.cpp @@ -0,0 +1,18 @@ +#include "cursor_listener.hpp" + +#include "strings.hpp" + +#include +#include + +CursorListener::CursorListener(std::shared_ptr scene) noexcept + : _scene(std::move(scene)) +{ +} + +void CursorListener::update(const Vector2 &context) noexcept +{ + _scene->write_status(fmt::format(STATUS_BAR_COORDINATES, + fmt::arg("x", context.get_x()), + fmt::arg("y", context.get_y()))); +} diff --git a/src/game/cursor_listener.hpp b/src/game/cursor_listener.hpp new file mode 100644 index 0000000..c74bb3d --- /dev/null +++ b/src/game/cursor_listener.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include "interfaces/scene.hpp" +#include "interfaces/subscriber.hpp" + +#include "engine/data/vector2.hpp" + +#include + +class CursorListener : public ISubscriber +{ +public: + explicit CursorListener(std::shared_ptr scene) noexcept; + + void update(const Vector2 &context) noexcept override; + +private: + std::shared_ptr _scene; +}; diff --git a/src/game/game.cpp b/src/game/game.cpp index 5df3935..055e212 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -3,6 +3,7 @@ #include "commands/insert_cell.hpp" #include "commands/move_cursor.hpp" #include "commands/quit.hpp" +#include "game/cursor_listener.hpp" #include "strings.hpp" #include @@ -16,6 +17,10 @@ Game::Game(const std::shared_ptr &window, const std::shared_ptr void Game::on_start() noexcept { + auto cursor_listener = std::make_shared(_scene); + + _cursor_controller->subscribe(CursorEvent::POSITION_CHANGE, cursor_listener); + const auto window_size = _window->size(); const auto center_position = @@ -50,11 +55,11 @@ Game::get_input_config() const noexcept return {{'q', std::make_shared()}, {'i', std::make_shared(_cursor_controller, _scene)}, {'k', std::make_shared(Vector2::up(), _cursor_controller, - _scene, _window)}, + _window)}, {'j', std::make_shared(Vector2::down(), _cursor_controller, - _scene, _window)}, + _window)}, {'h', std::make_shared(Vector2::left(), _cursor_controller, - _scene, _window)}, - {'l', std::make_shared( - Vector2::right(), _cursor_controller, _scene, _window)}}; + _window)}, + {'l', std::make_shared(Vector2::right(), + _cursor_controller, _window)}}; } -- cgit v1.2.3-18-g5258