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/user/cursor.cpp | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'src/engine/user/cursor.cpp') 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()}); +} + -- cgit v1.2.3-18-g5258