diff options
author | HampusM <hampus@hampusmat.com> | 2022-03-08 16:40:27 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-13 17:56:55 +0200 |
commit | 3359b00178357cb68d14e807c6deef3182532307 (patch) | |
tree | 1be93f2ff15baddd14dcbd7fee50fd2cf8f2fcac | |
parent | 0e781f07bda4a73e89eb59b9765aa9f609647932 (diff) |
refactor: make cursor controller track cursor pos itself
-rw-r--r-- | src/engine/user/cursor.cpp | 54 | ||||
-rw-r--r-- | src/engine/user/cursor.hpp | 13 |
2 files changed, 49 insertions, 18 deletions
diff --git a/src/engine/user/cursor.cpp b/src/engine/user/cursor.cpp index 391a91a..39fef23 100644 --- a/src/engine/user/cursor.cpp +++ b/src/engine/user/cursor.cpp @@ -5,33 +5,46 @@ #include <cstdlib> #include <iostream> -void CursorController::move(const Vector2 &direction, const uint32_t &amount) +CursorController::CursorController() : _position(_request_position()) {} + +void CursorController::move(const Vector2 &direction, const uint32_t &amount) noexcept { auto format = direction_format_map.at(direction); fmt::print(fmt::runtime(format.data()), fmt::arg("esc", ESC), fmt::arg("amount", amount)); std::cout.flush(); -} -void CursorController::move_to(const Vector2 &pos) -{ - fmt::print(MOVE_CURSOR_TO, fmt::arg("esc", ESC), fmt::arg("row", pos.get_y()), - fmt::arg("column", pos.get_x())); - std::cout.flush(); + if (direction == Vector2::up()) + { + _position.set_y(_position.get_y() + static_cast<int>(amount)); + } + else if (direction == Vector2::down()) + { + _position.set_y(_position.get_y() - static_cast<int>(amount)); + } + else if (direction == Vector2::left()) + { + _position.set_x(_position.get_x() - static_cast<int>(amount)); + } + else if (direction == Vector2::right()) + { + _position.set_x(_position.get_x() + static_cast<int>(amount)); + } } -Vector2 CursorController::where() +void CursorController::move_to(const Vector2 &position) noexcept { - fmt::print(REQUEST_CURSOR_POSITION, fmt::arg("esc", ESC)); + fmt::print(MOVE_CURSOR_TO, fmt::arg("esc", ESC), fmt::arg("row", position.get_y()), + fmt::arg("column", position.get_x())); std::cout.flush(); - Vector2Options vector2_options = {}; - - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) - scanf("\033[%u;%uR", &vector2_options.y, &vector2_options.x); + _position = position; +} - return Vector2(vector2_options); +Vector2 CursorController::where() const noexcept +{ + return _position; } void CursorController::hide() @@ -45,3 +58,16 @@ void CursorController::show() fmt::print(CURSOR_VISIBLE, fmt::arg("esc", ESC)); std::cout.flush(); } + +Vector2 CursorController::_request_position() noexcept +{ + fmt::print(REQUEST_CURSOR_POSITION, fmt::arg("esc", ESC)); + std::cout.flush(); + + Vector2Options vector2_options = {}; + + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) + scanf("\033[%u;%uR", &vector2_options.y, &vector2_options.x); + + return Vector2(vector2_options); +} diff --git a/src/engine/user/cursor.hpp b/src/engine/user/cursor.hpp index 84117c1..50dd028 100644 --- a/src/engine/user/cursor.hpp +++ b/src/engine/user/cursor.hpp @@ -31,15 +31,20 @@ const std::unordered_map<Vector2, std::string_view, Vector2Hasher> direction_for class CursorController : public AutoWirable<CursorController, CursorController> { public: - CursorController() = default; + CursorController(); - static void move(const Vector2 &direction, const uint32_t &amount); + void move(const Vector2 &direction, const uint32_t &amount) noexcept; - static void move_to(const Vector2 &pos); + void move_to(const Vector2 &position) noexcept; static void hide(); static void show(); - [[nodiscard]] static Vector2 where(); + [[nodiscard]] Vector2 where() const noexcept; + +private: + Vector2 _position; + + [[nodiscard]] static Vector2 _request_position() noexcept; }; |