#include "cursor.hpp" #include "engine/escape.hpp" #include #include CursorController::CursorController() noexcept : _position({.x = 0, .y = 0}), _bounds({0, 0}) { } void CursorController::move(const Vector2 &direction, const uint32_t &amount) noexcept { auto direction_format = direction_format_map.at(direction); fmt::print( fmt::runtime(direction_format.data()), fmt::arg("esc", ESC), fmt::arg("amount", amount)); std::cout.flush(); _position = _position.to_direction(direction, static_cast(amount)); } 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", inverted_pos.get_y()), fmt::arg("column", inverted_pos.get_x() + 1)); std::cout.flush(); _position = position; } auto CursorController::where() const noexcept -> Vector2 { return _position; } void CursorController::ensure_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); _position = _invert_position_y(Vector2(vector2_options)); } void CursorController::hide() noexcept { fmt::print(CURSOR_INVISIBLE, fmt::arg("esc", ESC)); std::cout.flush(); } 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()}); }