-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scroller.cpp
131 lines (112 loc) · 3 KB
/
Scroller.cpp
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "includes/Scroller.hpp"
tedit::Scroller::Scroller::Scroller(const Direction& direction, const size_t& max_size)
: m_direction(direction),
m_max_size(max_size)
{
setFillColor(sf::Color(241, 241, 241));
}
void
tedit::Scroller::startScrolling(const sf::Event::MouseButtonEvent& event)
{
if (event.button == sf::Mouse::Left
&& m_shape.getGlobalBounds().contains(sf::Vector2f(event.x, event.y)))
{
m_hold = m_direction == Direction::Vertical ? event.y : event.x;
auto position = m_shape.getPosition();
m_release = m_direction == Direction::Vertical ? position.y : position.x;
}
}
void
tedit::Scroller::endScrolling(const sf::Event::MouseButtonEvent& event)
{
m_release = m_direction == Direction::Vertical ? event.y : event.x;
m_hold = std::nullopt;
}
std::optional<std::size_t>
tedit::Scroller::mouseScroll(const int mouseX, const int mouseY)
{
if (!m_hold) return std::nullopt;
if (m_direction == Direction::Vertical)
{
int diff = mouseY - m_hold.value();
setPosition(m_shape.getPosition().x, m_release + diff);
}
else
{
int diff = mouseX - m_hold.value();
setPosition(m_release + diff, m_shape.getPosition().y);
}
std::size_t total = m_max_size - getSize();
std::size_t scrolled = total - (total - getPosition());
return scrolled * 100 / total;
}
void
tedit::Scroller::scrollTo(const int prec)
{
std::size_t total = m_max_size - getSize();
int value = total * prec / 100;
if (m_direction == Direction::Vertical)
{
setPosition(m_shape.getPosition().x, value);
}
else
{
setPosition(value, m_shape.getPosition().y);
}
}
void
tedit::Scroller::draw(sf::RenderTarget& target, sf::RenderStates states)
const
{
target.draw(m_shape, states);
}
float
tedit::Scroller::getPosition()
const noexcept
{
return m_direction == Direction::Vertical
? m_shape.getPosition().y
: m_shape.getPosition().x;
}
void
tedit::Scroller::setPosition(const float x, const float y)
{
auto current_size = m_shape.getSize();
if (m_direction == Direction::Vertical)
{
m_shape.setPosition(x, std::clamp(y, 0.0f, m_max_size - current_size.y));
}
else if (m_direction == Direction::Horizontal)
{
m_shape.setPosition(std::clamp(x, 0.0f, m_max_size - current_size.x), y);
}
}
void
tedit::Scroller::setX(const float x)
{
m_shape.setPosition(x, m_shape.getPosition().y);
}
void
tedit::Scroller::setY(const float y)
{
m_shape.setPosition(m_shape.getPosition().x, y);
}
std::size_t
tedit::Scroller::getSize()
const noexcept
{
return m_direction == Direction::Vertical
? m_shape.getSize().y
: m_shape.getSize().x;
}
void
tedit::Scroller::setSize(const std::size_t& max_size, const std::size_t& width, const std::size_t& height)
{
m_max_size = max_size;
m_shape.setSize(sf::Vector2f(width, height));
}
void
tedit::Scroller::setFillColor(const sf::Color& color)
{
m_shape.setFillColor(color);
}