aboutsummaryrefslogtreecommitdiff
path: root/src/engine/user/cursor.cpp
blob: a372ca70812e20a06297ea9b86502f031456eda4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "cursor.hpp"

#include "engine/escape.hpp"

#include <cstdlib>
#include <iostream>

CursorController::CursorController() noexcept
	: _position({.x = 0, .y = 0}), _bounds({0, 0})
{
}

void CursorController::move(
	const Vector2 &direction,
	const std::uint32_t &amount,
	bool flush_cout) noexcept
{
	auto direction_format = direction_format_map.at(direction);

	fmt::print(
		fmt::runtime(direction_format.data()),
		fmt::arg("esc", ESC),
		fmt::arg("amount", amount));

	if (flush_cout)
	{
		std::cout.flush();
	}

	_position = _position.to_direction(direction, static_cast<Vector2::Value>(amount));
}

void CursorController::move_to(const Vector2 &position, bool flush_cout) 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));

	if (flush_cout)
	{
		std::cout.flush();
	}

	_position = position;
}

auto CursorController::where() const noexcept -> Vector2
{
	return _position;
}

void CursorController::update_position(const Vector2 &position) noexcept
{
	_position = position;
}

void CursorController::hide(bool flush_cout) noexcept
{
	fmt::print(CURSOR_INVISIBLE, fmt::arg("esc", ESC));

	if (flush_cout)
	{
		std::cout.flush();
	}
}

void CursorController::show(bool flush_cout) noexcept
{
	fmt::print(CURSOR_VISIBLE, fmt::arg("esc", ESC));

	if (flush_cout)
	{
		std::cout.flush();
	}
}

void CursorController::set_cursor_style(
	CursorStyle cursor_style,
	bool flush_cout) noexcept
{
	fmt::print(
		SET_CURSOR_STYLE,
		fmt::arg("esc", ESC),
		fmt::arg("style", static_cast<int>(cursor_style)));

	if (flush_cout)
	{
		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()});
}