-
Notifications
You must be signed in to change notification settings - Fork 0
/
Button.h
196 lines (170 loc) · 5.49 KB
/
Button.h
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#pragma once
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <string>
class Button
{
public:
sf::Vector2f position;
sf::Vector2f size;
sf::Color color;
sf::Color hover_color;
sf::Color clicked_color;
sf::Color text_color;
sf::Color text_hover_color;
sf::Color text_clicked_color;
sf::Color outline_color;
sf::Font font;
bool clicked = false;
bool hover = false;
bool active = false;
sf::RectangleShape shape;
std::string text_string;
int font_size;
bool interactable;
bool outlined;
bool italic;
Button(sf::Vector2f position, sf::Vector2f size, sf::Color color, sf::Color text_color, std::string text_string, int font_size, bool interactable, bool outlined, bool italic)
{
this->position = position;
this->size = size;
this->color = color;
this->text_color = text_color;
this->font.loadFromFile("resources/monospace.ttf");
this->text_string = text_string;
this->font_size = font_size;
this->interactable = interactable;
this->outlined = outlined;
this->italic = italic;
this->outline_color = sf::Color::White;
shape.setSize(size);
shape.setOrigin(size.x / 2, size.y / 2);
shape.setPosition(position);
shape.setFillColor(color);
//if fill color is white, set outline color to black
//else set outline color to white
if (color == sf::Color::White)
{
shape.setOutlineColor(sf::Color::Black);
}
else
{
shape.setOutlineColor(sf::Color::White);
}
if (outlined)
shape.setOutlineThickness(2);
else
shape.setOutlineThickness(0);
hover_color = sf::Color(color.r + 50 > 255 ? 255 : color.r + 50, color.g + 50 > 255 ? 255 : color.g + 50, color.b + 50 > 255 ? 255 : color.b + 50);
clicked_color = sf::Color(color.r + 100 > 255 ? 255 : color.r + 100, color.g + 100 > 255 ? 255 : color.g + 100, color.b + 100 > 255 ? 255 : color.b + 100);
text_hover_color = sf::Color(text_color.r + 50 > 255 ? 255 : text_color.r + 50, text_color.g + 50 > 255 ? 255 : text_color.g + 50, text_color.b + 50 > 255 ? 255 : text_color.b + 50);
text_clicked_color = sf::Color(text_color.r + 100 > 255 ? 255 : text_color.r + 100, text_color.g + 100 > 255 ? 255 : text_color.g + 100, text_color.b + 100 > 255 ? 255 : text_color.b + 100);
}
Button(sf::Vector2f position, sf::Vector2f size, sf::Color color, sf::Texture texture, bool interactable, bool outlined)
{
this->position = position;
this->size = size;
this->color = color;
this->text_color = sf::Color::White;
this->font.loadFromFile("resources/monospace.ttf");
this->text_string = " ";
this->font_size = 1;
this->interactable = interactable;
this->outlined = outlined;
this->italic = false;
this->outline_color = sf::Color::White;
shape.setSize(size);
shape.setOrigin(size.x / 2, size.y / 2);
shape.setPosition(position);
shape.setFillColor(color);
//if fill color is white, set outline color to black
//else set outline color to white
if (color == sf::Color::White)
{
shape.setOutlineColor(sf::Color::Black);
}
else
{
shape.setOutlineColor(sf::Color::White);
}
if (outlined)
shape.setOutlineThickness(2);
else
shape.setOutlineThickness(0);
shape.setTexture(&texture);
hover_color = sf::Color(color.r + 50 > 255 ? 255 : color.r + 50, color.g + 50 > 255 ? 255 : color.g + 50, color.b + 50 > 255 ? 255 : color.b + 50);
clicked_color = sf::Color(color.r + 100 > 255 ? 255 : color.r + 100, color.g + 100 > 255 ? 255 : color.g + 100, color.b + 100 > 255 ? 255 : color.b + 100);
text_hover_color = sf::Color(text_color.r + 50 > 255 ? 255 : text_color.r + 50, text_color.g + 50 > 255 ? 255 : text_color.g + 50, text_color.b + 50 > 255 ? 255 : text_color.b + 50);
text_clicked_color = sf::Color(text_color.r + 100 > 255 ? 255 : text_color.r + 100, text_color.g + 100 > 255 ? 255 : text_color.g + 100, text_color.b + 100 > 255 ? 255 : text_color.b + 100);
}
void set_position(sf::Vector2f position)
{
this->position = position;
shape.setPosition(position);
}
void update(sf::RenderWindow& window)
{
sf::Vector2i mousePos = sf::Mouse::getPosition(window);
sf::Vector2f mousePosF{ static_cast<float>(mousePos.x), static_cast<float>(mousePos.y) };
//bounds should take into account the absolute position of the button
sf::FloatRect bounds = sf::FloatRect(position.x-size.x/2, position.y-size.y/2, size.x, size.y);
clicked = false;
hover = false;
shape.setOutlineColor(outline_color);
if (bounds.contains(mousePosF))
{
hover = true;
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
clicked = true;
active = true;
}
}
else
{
active = false;
}
//if (!interactable) return;
if (clicked)
{
shape.setFillColor(clicked_color);
}
else if (hover)
{
shape.setFillColor(hover_color);
}
else
{
shape.setFillColor(color);
}
}
void draw(sf::RenderWindow& window)
{
sf::Text text = sf::Text{ text_string, font };
text.setCharacterSize(font_size);
sf::Vector2f center = sf::Vector2f(text.getGlobalBounds().width / 2.f, text.getGlobalBounds().height / 2.f);
sf::Vector2f localBounds = center + sf::Vector2f(text.getLocalBounds().left, text.getLocalBounds().top);
text.setOrigin(localBounds);
text.setPosition(position);
text.setStyle(sf::Text::Bold);
if (italic)
text.setStyle(sf::Text::Italic);
if (clicked)
{
text.setFillColor(text_clicked_color);
}
else if (hover)
{
text.setFillColor(text_hover_color);
}
else
{
text.setFillColor(text_color);
}
window.draw(shape);
window.draw(text);
}
};