#include "cursor.hpp" #include "engine/escape.hpp" #include #include 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(); if (direction == Vector2::up()) { _position.set_y(_position.get_y() + static_cast(amount)); } else if (direction == Vector2::down()) { _position.set_y(_position.get_y() - static_cast(amount)); } else if (direction == Vector2::left()) { _position.set_x(_position.get_x() - static_cast(amount)); } else if (direction == Vector2::right()) { _position.set_x(_position.get_x() + static_cast(amount)); } } void CursorController::move_to(const Vector2 &position) noexcept { fmt::print(MOVE_CURSOR_TO, fmt::arg("esc", ESC), fmt::arg("row", position.get_y()), fmt::arg("column", position.get_x())); std::cout.flush(); _position = position; } Vector2 CursorController::where() const noexcept { return _position; } void CursorController::hide() { fmt::print(CURSOR_INVISIBLE, fmt::arg("esc", ESC)); std::cout.flush(); } 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); }