From c988905add09cf8baf46dc61279528f6f39f7a1a Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 10 Mar 2022 20:16:14 +0100 Subject: feat: add status bar --- src/bootstrap.cpp | 8 +++++--- src/engine/engine.cpp | 14 +++++++++----- src/engine/graphics/scene.cpp | 31 ++++++++++++++++++++++++++----- src/engine/graphics/scene.hpp | 22 +++++++++++++++------- src/input_actions.cpp | 6 +++++- src/input_actions.hpp | 1 + src/interfaces/game.hpp | 3 --- src/interfaces/scene.hpp | 13 ++++++++----- 8 files changed, 69 insertions(+), 29 deletions(-) diff --git a/src/bootstrap.cpp b/src/bootstrap.cpp index 8aeba85..5ff71db 100644 --- a/src/bootstrap.cpp +++ b/src/bootstrap.cpp @@ -70,10 +70,12 @@ Container bootstrap() }); container.bind().to_factory(normalize_lambda( - [&container]() + [&container](const std::shared_ptr &cursor_controller, + const std::shared_ptr &window) { - return std::dynamic_pointer_cast(std::make_shared( - container.get>())); + return std::dynamic_pointer_cast( + std::make_shared(container.get>(), + cursor_controller, window)); })); return container; diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index 0227d5c..6050348 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -21,7 +21,7 @@ CLIGameEngine::CLIGameEngine(IGameFactory game_factory, ISceneFactory scene_fact void CLIGameEngine::start() noexcept { - auto scene = _scene_factory(); + auto scene = _scene_factory(_cursor_controller, _window); scene->enter(); _input_handler->enter_raw_mode(); @@ -43,10 +43,14 @@ void CLIGameEngine::start() noexcept const std::unordered_map input_config = { {'q', InputActions::exit_success}, - {'k', InputActions::move_cursor(Vector2::up(), _cursor_controller, _window)}, - {'j', InputActions::move_cursor(Vector2::down(), _cursor_controller, _window)}, - {'h', InputActions::move_cursor(Vector2::left(), _cursor_controller, _window)}, - {'l', InputActions::move_cursor(Vector2::right(), _cursor_controller, _window)}}; + {'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)}}; _configure_input(input_config); diff --git a/src/engine/graphics/scene.cpp b/src/engine/graphics/scene.cpp index a8e7b88..c4a398c 100644 --- a/src/engine/graphics/scene.cpp +++ b/src/engine/graphics/scene.cpp @@ -4,13 +4,19 @@ #include #include - -Scene::Scene(IMatrixFactory matrix_factory) - : _is_shown(false), _matrix_factory(matrix_factory) +#include + +Scene::Scene(IMatrixFactory matrix_factory, + std::shared_ptr cursor_controller, + std::shared_ptr window) noexcept + : _is_shown(false), + _matrix_factory(matrix_factory), + _cursor_controller(std::move(cursor_controller)), + _window(std::move(window)) { } -void Scene::enter() +void Scene::enter() noexcept { if (_is_shown) { @@ -23,7 +29,7 @@ void Scene::enter() _is_shown = true; } -void Scene::leave() +void Scene::leave() noexcept { if (!_is_shown) { @@ -35,3 +41,18 @@ void Scene::leave() _is_shown = false; } + +void Scene::write_status(const std::string_view &str) noexcept +{ + const auto previous_position = _cursor_controller->where(); + + const auto window_size = _window->size(); + + _cursor_controller->move_to( + Vector2({.x = 1, .y = static_cast(window_size.get_height())})); + + fmt::print(ERASE_ENTIRE_LINE, fmt::arg("esc", ESC)); + fmt::print(fmt::runtime(str.data())); + + _cursor_controller->move_to(previous_position); +} diff --git a/src/engine/graphics/scene.hpp b/src/engine/graphics/scene.hpp index c4c217f..3d05cd9 100644 --- a/src/engine/graphics/scene.hpp +++ b/src/engine/graphics/scene.hpp @@ -1,29 +1,37 @@ #pragma once -#include "DI/auto_wirable.hpp" +#include "interfaces/cursor.hpp" #include "interfaces/matrix.hpp" #include "interfaces/scene.hpp" +#include "interfaces/window.hpp" #include +#include #include constexpr fmt::string_view ENABLE_ALT_BUFFER = "{esc}[?1049h"; constexpr fmt::string_view DISABLE_ALT_BUFFER = "{esc}[?1049l"; -class Scene : public IScene, - public AutoWirable> +constexpr fmt::string_view ERASE_ENTIRE_LINE = "{esc}[2K"; + +class Scene : public IScene { public: - explicit Scene(IMatrixFactory matrix_factory); + explicit Scene(IMatrixFactory matrix_factory, + std::shared_ptr cursor_controller, + std::shared_ptr window) noexcept; - void enter() override; + void enter() noexcept override; - void leave() override; + void leave() noexcept override; - // void do_in_statusbar(const std::function &routine); + void write_status(const std::string_view &str) noexcept override; private: bool _is_shown; IMatrixFactory _matrix_factory; + + std::shared_ptr _cursor_controller; + std::shared_ptr _window; }; diff --git a/src/input_actions.cpp b/src/input_actions.cpp index 854dc62..5df6e5f 100644 --- a/src/input_actions.cpp +++ b/src/input_actions.cpp @@ -12,9 +12,10 @@ void exit_success() Callback move_cursor(const Vector2 &direction, const std::shared_ptr &cursor_controller, + const std::shared_ptr &scene, const std::shared_ptr &window) { - return [direction, cursor_controller, window]() + return [direction, cursor_controller, scene, window]() { constexpr int32_t amount = 1; @@ -29,6 +30,9 @@ Callback move_cursor(const Vector2 &direction, } cursor_controller->move_to(new_position); + + scene->write_status( + fmt::format("X: {} Y {}", new_position.get_x(), new_position.get_y())); }; } diff --git a/src/input_actions.hpp b/src/input_actions.hpp index 8283d78..224c280 100644 --- a/src/input_actions.hpp +++ b/src/input_actions.hpp @@ -14,6 +14,7 @@ namespace InputActions Callback move_cursor(const Vector2 &direction, const std::shared_ptr &cursor_controller, + const std::shared_ptr &scene, const std::shared_ptr &window); void exit_success(); diff --git a/src/interfaces/game.hpp b/src/interfaces/game.hpp index 338a8a3..3f9d735 100644 --- a/src/interfaces/game.hpp +++ b/src/interfaces/game.hpp @@ -1,8 +1,5 @@ #pragma once -#include "interfaces/input.hpp" -#include "interfaces/scene.hpp" - #include class IGame diff --git a/src/interfaces/scene.hpp b/src/interfaces/scene.hpp index ca494cb..47df5fa 100644 --- a/src/interfaces/scene.hpp +++ b/src/interfaces/scene.hpp @@ -5,16 +5,19 @@ #include "interfaces/window.hpp" #include -#include class IScene { public: - virtual ~IScene() = default; + virtual ~IScene() noexcept = default; - virtual void enter() = 0; + virtual void enter() noexcept = 0; - virtual void leave() = 0; + virtual void leave() noexcept = 0; + + virtual void write_status(const std::string_view &str) noexcept = 0; }; -using ISceneFactory = std::shared_ptr (*)(); +using ISceneFactory = std::shared_ptr (*)( + const std::shared_ptr &cursor_controller, + const std::shared_ptr &window); -- cgit v1.2.3-18-g5258