diff options
author | HampusM <hampus@hampusmat.com> | 2022-06-02 12:17:23 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-13 17:57:00 +0200 |
commit | 87f55120f96d0f4f80b497dc9006d89df2dda125 (patch) | |
tree | 4aba3a77b268c9ff97b146e2a02626931d9a3122 /src/engine/user | |
parent | 62164c0abd9c5f563fe8fe57d51b5dc003816c49 (diff) |
refactor: have cursor controller inverting input positions
Diffstat (limited to 'src/engine/user')
-rw-r--r-- | src/engine/user/cursor.cpp | 27 | ||||
-rw-r--r-- | src/engine/user/cursor.hpp | 7 |
2 files changed, 30 insertions, 4 deletions
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 <cstdlib> #include <iostream> -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<Vector2::Value>(_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 <fmt/core.h> @@ -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; }; |