From 87f55120f96d0f4f80b497dc9006d89df2dda125 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 2 Jun 2022 12:17:23 +0200 Subject: refactor: have cursor controller inverting input positions --- src/engine/data/vector2.hpp | 8 ++++---- src/engine/engine.cpp | 2 ++ src/engine/graphics/statusline.cpp | 6 +----- src/engine/user/cursor.cpp | 27 +++++++++++++++++++++++---- src/engine/user/cursor.hpp | 7 +++++++ src/interfaces/cursor.hpp | 3 +++ 6 files changed, 40 insertions(+), 13 deletions(-) diff --git a/src/engine/data/vector2.hpp b/src/engine/data/vector2.hpp index 15df88f..620b41c 100644 --- a/src/engine/data/vector2.hpp +++ b/src/engine/data/vector2.hpp @@ -44,19 +44,19 @@ public: auto operator==(const Vector2 &rhs) const noexcept -> bool; /** - * Returns Vector2({.x = 0, .y = -1}) + * Returns Vector2({.x = 0, .y = 1}) */ static constexpr auto up() noexcept -> Vector2 { - return Vector2({.x = 0, .y = -1}); + return Vector2({.x = 0, .y = 1}); } /** - * Returns Vector2({.x = 0, .y = 1}) + * Returns Vector2({.x = 0, .y = -1}) */ static constexpr auto down() noexcept -> Vector2 { - return Vector2({.x = 0, .y = 1}); + return Vector2({.x = 0, .y = -1}); } /** diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index c7605b5..4775fe4 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -24,6 +24,8 @@ void CLIGameEngine::start() noexcept scene->enter(); + _cursor_controller->set_bounds(scene->size()); + auto game = _game_factory(scene, _cursor_controller, _user_input_observer); game->on_start(); diff --git a/src/engine/graphics/statusline.cpp b/src/engine/graphics/statusline.cpp index a8d0f3b..bc83993 100644 --- a/src/engine/graphics/statusline.cpp +++ b/src/engine/graphics/statusline.cpp @@ -58,13 +58,9 @@ auto StatusLine::_move_to_statusline(int32_t x) noexcept -> Vector2 { const auto previous_position = _cursor_controller->where(); - const auto scene_size = _scene->size(); - _cursor_controller->hide(); - auto scene_height = static_cast(scene_size.get_height()); - - _cursor_controller->move_to(Vector2({.x = x, .y = scene_height})); + _cursor_controller->move_to(Vector2({.x = x, .y = 0})); return previous_position; } diff --git a/src/engine/user/cursor.cpp b/src/engine/user/cursor.cpp index 97e9104..70828c3 100644 --- a/src/engine/user/cursor.cpp +++ b/src/engine/user/cursor.cpp @@ -5,7 +5,10 @@ #include #include -CursorController::CursorController() noexcept : _position({.x = 0, .y = 0}) {} +CursorController::CursorController() noexcept + : _position({.x = 0, .y = 0}), _bounds({0, 0}) +{ +} void CursorController::move(const Vector2 &direction, const uint32_t &amount) noexcept { @@ -22,11 +25,13 @@ void CursorController::move(const Vector2 &direction, const uint32_t &amount) no void CursorController::move_to(const Vector2 &position) noexcept { + const auto inverted_pos = _invert_position_y(position); + fmt::print( MOVE_CURSOR_TO, fmt::arg("esc", ESC), - fmt::arg("row", position.get_y()), - fmt::arg("column", position.get_x())); + fmt::arg("row", inverted_pos.get_y()), + fmt::arg("column", inverted_pos.get_x())); std::cout.flush(); _position = position; @@ -47,7 +52,7 @@ void CursorController::ensure_position() noexcept // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) scanf("\033[%u;%uR", &vector2_options.y, &vector2_options.x); - _position = Vector2(vector2_options); + _position = _invert_position_y(Vector2(vector2_options)); } void CursorController::hide() noexcept @@ -61,3 +66,17 @@ void CursorController::show() noexcept fmt::print(CURSOR_VISIBLE, fmt::arg("esc", ESC)); std::cout.flush(); } + +void CursorController::set_bounds(const Bounds &bounds) noexcept +{ + _bounds = bounds; +} + +auto CursorController::_invert_position_y(const Vector2 &position) const noexcept + -> Vector2 +{ + const auto bounds_height = static_cast(_bounds.get_height()); + + return Vector2({.x = position.get_x(), .y = bounds_height - position.get_y()}); +} + diff --git a/src/engine/user/cursor.hpp b/src/engine/user/cursor.hpp index de8c3ae..199c86b 100644 --- a/src/engine/user/cursor.hpp +++ b/src/engine/user/cursor.hpp @@ -2,6 +2,7 @@ #include "interfaces/cursor.hpp" +#include "engine/data/bounds.hpp" #include "engine/data/vector2.hpp" #include @@ -48,6 +49,12 @@ public: void show() noexcept override; + void set_bounds(const Bounds &bounds) noexcept override; + private: Vector2 _position; + Bounds _bounds; + + [[nodiscard]] auto _invert_position_y(const Vector2 &position) const noexcept + -> Vector2; }; diff --git a/src/interfaces/cursor.hpp b/src/interfaces/cursor.hpp index 63aae41..377fe25 100644 --- a/src/interfaces/cursor.hpp +++ b/src/interfaces/cursor.hpp @@ -1,5 +1,6 @@ #pragma once +#include "engine/data/bounds.hpp" #include "engine/data/vector2.hpp" #include @@ -30,4 +31,6 @@ public: virtual void hide() noexcept = 0; virtual void show() noexcept = 0; + + virtual void set_bounds(const Bounds &bounds) noexcept = 0; }; -- cgit v1.2.3-18-g5258