-
Notifications
You must be signed in to change notification settings - Fork 2
/
sfml_scroll_box.cpp
89 lines (75 loc) · 2.47 KB
/
sfml_scroll_box.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
#include "sfml_scroll_box.h"
sfml_scroll_box::sfml_scroll_box(const double x, const double y,
const double height, const double width)
: m_x{x}, m_y{y}, m_height{height}, m_width{width}, m_scroll{0}
{
m_shape.setSize(sf::Vector2f(m_width,m_height));
m_shape.setPosition(sf::Vector2f(m_x,m_y));
m_shape.setFillColor(sf::Color(53,234,151,0));
m_shape.setOutlineThickness(5);
m_shape.setOutlineColor(sf::Color(43,189,121));
m_view = sf::View(sf::Vector2f(65, 55),
sf::Vector2f(230, 190));
}
void sfml_scroll_box::set_pos(int x, int y, sf::RenderWindow &window) {
m_x = x;
m_y = y;
m_shape.setPosition(window.mapPixelToCoords(sf::Vector2i(m_x, m_y)));
float x_pos = m_shape.getPosition().x / window.getSize().x;
float y_pos = m_shape.getPosition().y / window.getSize().y;
float x_per = m_shape.getSize().x / window.getSize().x;
float y_per = m_shape.getSize().y / window.getSize().y;
m_view.setViewport(sf::FloatRect(x_pos, y_pos, x_per, y_per));
//m_view.setCenter(0, 0);
}
bool sfml_scroll_box::is_clicked(sf::Vector2f pos) const {
double x = pos.x;
double y = pos.y;
return x > m_x && x < m_x + m_width &&
y > m_y && y < m_y + m_height;
}
void sfml_scroll_box::set_size(double width, double height) {
m_width = width;
m_height = height;
m_shape.setSize(sf::Vector2f(m_width, m_height));
}
void sfml_scroll_box::draw(sf::RenderWindow& window) {
const sf::View tmp_view = window.getView();
window.draw(m_shape);
window.setView(m_view);
for (sf::RectangleShape& r : m_rectangles) {
window.draw(r);
}
for (sf::Text& t : m_texts) {
window.draw(t);
}
window.setView(tmp_view);
}
void sfml_scroll_box::draw(sf::RenderWindow& window, std::vector<sfml_button> v) {
const sf::View tmp_view = window.getView();
window.draw(m_shape);
window.setView(m_view);
for (sfml_button &b : v) {
window.draw(b.get_shape());
window.draw(b.get_text());
}
window.setView(tmp_view);
}
void sfml_scroll_box::add_rectangle(sf::RectangleShape& r) {
m_rectangles.push_back(std::ref(r));
}
void sfml_scroll_box::add_text(sf::Text& t) {
m_texts.push_back(std::ref(t));
}
void sfml_scroll_box::scroll(sf::Event&
#if(SFML_VERSION_MINOR > 3)
event
#endif
) {
#if(SFML_VERSION_MINOR > 3)
if (event.type != sf::Event::MouseWheelScrolled) return;
m_scroll += event.mouseWheelScroll.delta * 18;
if (m_scroll > 0) m_scroll = 0;
#endif
//m_view.setViewport(m_shape.getGlobalBounds());
}