-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.cpp
More file actions
465 lines (384 loc) · 15.1 KB
/
Button.cpp
File metadata and controls
465 lines (384 loc) · 15.1 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#include "stdafx.h"
#include "Button.h"
//Converts a percentage value to pixels relative to current resolution in the X axis.
//@param float percent The percentage value.
//@param sf::VideoMode& video_mode Current video mode of the window (resolution).
//@return float The calculated pixel value.
const float gui::percentIntoX(const float percent, const sf::VideoMode& video_mode)
{
return std::floor(static_cast<float>(video_mode.width) * (percent / 100.f));
}
//Converts a percentage value to pixels relative to current resolution in the Y axis.
//@param float percent The percentage value.
//@param sf::VideoMode& video_mode Current video mode of the window (resolution).
//@return float The calculated pixel value.
const float gui::percentIntoY(const float percent, const sf::VideoMode& video_mode)
{
return std::floor(static_cast<float>(video_mode.height) * (percent / 100.f));
}
//Calculates the character size for text using the current resolution and a constant.
//@param float percent The percentage value.
//@param sf::VideoMode& video_mode Current video mode of the window (resolution).
//@return unsigned The calculated character size value.
const unsigned gui::calculateCharSize(const float percent, const sf::VideoMode& video_mode)
{
if (video_mode.width > video_mode.height)
{
return static_cast<unsigned>(std::floor(static_cast<float>(video_mode.height) * (percent / 100.f)));
}
return static_cast<unsigned>(std::floor(static_cast<float>(video_mode.width) * (percent / 100.f)));
// return (this->stateData->gfxSettings->resolution.width + this->stateData->gfxSettings->resolution.height) / 100;
}
//private: Functions:
void gui::Button::initVariables()
{
this->fontSize = 20;
this->colorNumber = 0;
// this->colorMaxStages = 6;
this->stepOutline = 0.034f;
this->stepButton = 0.1f;
this->stepText = 0.034f;
this->coefficientOutline = 0.8f;
this->coefficientButton = 0.4f;
this->coefficientText = 0.8f;
this->buttonTimer = 0.f;
this->buttonTimerMax = 4.f;
}
//Constructors and Destructor:
gui::Button::Button
(
const float x, const float y, const float width, const float height,
sf::Font* font, const std::string& str, const uint32_t font_size,
sf::Color button_idle_color, sf::Color button_hover_color, sf::Color button_active_color,
sf::Color text_idle_color, sf::Color text_hover_color, sf::Color text_active_color,
sf::Color outline_idle_color,
sf::Color outline_hover_color,
sf::Color outline_active_color,
const uint32_t id
)
{
this->initVariables();
this->buttonState = gui::ButtonStates::Idle;
this->id = id;
this->shape.setPosition(sf::Vector2f(x, y));
this->shape.setSize(sf::Vector2f(width, height));
this->shape.setOutlineThickness(-1.f);
this->font = font;
this->text.setFont(*this->font);
this->text.setString(str);
this->text.setFillColor(sf::Color::White);
this->text.setCharacterSize(font_size);
this->text.setPosition
(
(this->shape.getPosition().x + (this->shape.getGlobalBounds().width / 2.f)) - (this->text.getGlobalBounds().width / 2.f),
this->shape.getPosition().y// + (this->shape.getGlobalBounds().height / 20.f))// - (this->text.getGlobalBounds().height / 2.f)
);
// Outline colors
this->outlineIdleColor = outline_idle_color;
this->outlineHoverColor = outline_hover_color;
this->outlineActiveColor = outline_active_color;
// Button colors
this->buttonIdleColor = button_idle_color;
this->buttonHoverColor = button_hover_color;
this->buttonActiveColor = button_active_color;
// Text colors
this->textIdleColor = text_idle_color;
this->textHoverColor = text_hover_color;
this->textActiveColor = text_active_color;
// Outline idle to hover color
this->idleToHoverColorOutline[0] = this->outlineIdleColor;
this->coefficientOutline += this->stepOutline;
for (unsigned short i = 1; i < this->colorMaxStages; i++)
{
this->idleToHoverColorOutline[i].r = static_cast<unsigned>(static_cast<float>(outline_hover_color.r) * this->coefficientOutline);
this->idleToHoverColorOutline[i].g = static_cast<unsigned>(static_cast<float>(outline_hover_color.g) * this->coefficientOutline);
this->idleToHoverColorOutline[i].b = static_cast<unsigned>(static_cast<float>(outline_hover_color.b) * this->coefficientOutline);
this->coefficientOutline += this->stepOutline;
}
//Button idle to hover color
this->idleToHoverColorButton[0] = this->buttonIdleColor;
this->coefficientButton += this->stepButton;
for (unsigned short i = 1; i < this->colorMaxStages; i++)
{
this->idleToHoverColorButton[i].r = static_cast<unsigned>(static_cast<float>(button_hover_color.r) * this->coefficientButton);
this->idleToHoverColorButton[i].g = static_cast<unsigned>(static_cast<float>(button_hover_color.g) * this->coefficientButton);
this->idleToHoverColorButton[i].b = static_cast<unsigned>(static_cast<float>(button_hover_color.b) * this->coefficientButton);
this->coefficientButton += this->stepButton;
}
//Text idle to hover color
this->idleToHoverColorText[0] = this->textIdleColor;
this->coefficientText += this->stepText;
for (unsigned short i = 1; i < this->colorMaxStages; i++)
{
this->idleToHoverColorText[i].r = static_cast<unsigned>(static_cast<float>(text_hover_color.r) * this->coefficientText);
this->idleToHoverColorText[i].g = static_cast<unsigned>(static_cast<float>(text_hover_color.g) * this->coefficientText);
this->idleToHoverColorText[i].b = static_cast<unsigned>(static_cast<float>(text_hover_color.b) * this->coefficientText);
this->coefficientText += this->stepText;
}
// Outline
this->shape.setOutlineColor(this->outlineIdleColor);
// Button
this->shape.setFillColor(this->buttonIdleColor);
// Text
this->text.setFillColor(this->textIdleColor);
}
gui::Button::Button
(
const float x, const float y, const float width, const float height,
sf::Font* font, const std::string& text, const uint32_t font_size,
sf::Color auto_outline_hover_color,
sf::Color auto_button_hover_color,
sf::Color auto_text_hover_color,
const uint32_t id
)
{
this->initVariables();
this->buttonState = gui::ButtonStates::Idle;
this->id = id;
this->shape.setPosition(sf::Vector2f(x, y));
this->shape.setSize(sf::Vector2f(width, height));
this->shape.setOutlineThickness(-1.f);
this->font = font;
this->text.setFont(*this->font);
this->text.setString(text);
this->text.setFillColor(sf::Color::White);
this->text.setCharacterSize(font_size);
this->text.setPosition
(
(this->shape.getPosition().x + (this->shape.getGlobalBounds().width / 2.f)) - (this->text.getGlobalBounds().width / 2.f),
(this->shape.getPosition().y + 1000.f)// + (this->shape.getGlobalBounds().height / 20.f))// - (this->text.getGlobalBounds().height / 2.f)
);
//Colors check:
//Button
if (auto_button_hover_color.r < 60 && auto_button_hover_color.g < 60 && auto_button_hover_color.b < 60)
{
auto_button_hover_color.r += 130;
auto_button_hover_color.g += 130;
auto_button_hover_color.b += 130;
}
//Text
// if (auto_text_hover_color.r < 60 && auto_text_hover_color.g < 60 && auto_text_hover_color.b < 60)
// {
// auto_text_hover_color.r += 160;
// auto_text_hover_color.g += 160;
// auto_text_hover_color.b += 160;
// }
// Outline colors:
//Idle color
this->outlineIdleColor.r = static_cast<uint32_t>(static_cast<float>(auto_outline_hover_color.r) * 0.7f);
this->outlineIdleColor.g = static_cast<uint32_t>(static_cast<float>(auto_outline_hover_color.g) * 0.7f);
this->outlineIdleColor.b = static_cast<uint32_t>(static_cast<float>(auto_outline_hover_color.b) * 0.7f);
this->outlineIdleColor.a = 200;
//Hover color
this->outlineHoverColor = auto_outline_hover_color;
this->outlineHoverColor.a = 255;
//Active color
this->outlineActiveColor.r = static_cast<uint32_t>(static_cast<float>(auto_outline_hover_color.r) * 0.4f);
this->outlineActiveColor.g = static_cast<uint32_t>(static_cast<float>(auto_outline_hover_color.g) * 0.4f);
this->outlineActiveColor.b = static_cast<uint32_t>(static_cast<float>(auto_outline_hover_color.b) * 0.4f);
this->outlineActiveColor.a = 200;
// Button colors:
//Idle color
this->buttonIdleColor.r = static_cast<uint32_t>(static_cast<float>(auto_button_hover_color.r) * 0.4f);
this->buttonIdleColor.g = static_cast<uint32_t>(static_cast<float>(auto_button_hover_color.g) * 0.4f);
this->buttonIdleColor.b = static_cast<uint32_t>(static_cast<float>(auto_button_hover_color.b) * 0.4f);
this->buttonIdleColor.a = 200;
//Hover color
this->buttonHoverColor = auto_button_hover_color;
this->buttonHoverColor.a = 255;
//Active color
this->buttonActiveColor.r = static_cast<uint32_t>(static_cast<float>(auto_button_hover_color.r) * 0.1f);
this->buttonActiveColor.g = static_cast<uint32_t>(static_cast<float>(auto_button_hover_color.g) * 0.1f);
this->buttonActiveColor.b = static_cast<uint32_t>(static_cast<float>(auto_button_hover_color.b) * 0.1f);
this->buttonActiveColor.a = 200;
//Text colors:
//Idle color
this->textIdleColor.r = static_cast<uint32_t>(static_cast<float>(auto_text_hover_color.r) * 0.7f);
this->textIdleColor.g = static_cast<uint32_t>(static_cast<float>(auto_text_hover_color.g) * 0.7f);
this->textIdleColor.b = static_cast<uint32_t>(static_cast<float>(auto_text_hover_color.b) * 0.7f);
this->textIdleColor.a = 255;
//Hover color
this->textHoverColor = auto_text_hover_color;
this->textHoverColor.a = 255;
//Active color
this->textActiveColor.r = static_cast<unsigned>(static_cast<float>(auto_text_hover_color.r) * 0.4f);
this->textActiveColor.g = static_cast<unsigned>(static_cast<float>(auto_text_hover_color.g) * 0.4f);
this->textActiveColor.b = static_cast<unsigned>(static_cast<float>(auto_text_hover_color.b) * 0.4f);
this->textActiveColor.a = 255;
// Outline idle to hover color
this->idleToHoverColorOutline[0] = this->outlineIdleColor;
this->coefficientOutline += this->stepOutline;
for (unsigned short i = 1; i < this->colorMaxStages; i++)
{
this->idleToHoverColorOutline[i].r = static_cast<unsigned>(static_cast<float>(auto_outline_hover_color.r) * this->coefficientOutline);
this->idleToHoverColorOutline[i].g = static_cast<unsigned>(static_cast<float>(auto_outline_hover_color.g) * this->coefficientOutline);
this->idleToHoverColorOutline[i].b = static_cast<unsigned>(static_cast<float>(auto_outline_hover_color.b) * this->coefficientOutline);
this->coefficientOutline += this->stepOutline;
}
// Button idle to hover color
this->idleToHoverColorButton[0] = this->buttonIdleColor;
this->coefficientButton += this->stepText;
for (unsigned short i = 1; i < this->colorMaxStages; i++)
{
this->idleToHoverColorButton[i].r = static_cast<unsigned>(static_cast<float>(auto_button_hover_color.r) * this->coefficientButton);
this->idleToHoverColorButton[i].g = static_cast<unsigned>(static_cast<float>(auto_button_hover_color.g) * this->coefficientButton);
this->idleToHoverColorButton[i].b = static_cast<unsigned>(static_cast<float>(auto_button_hover_color.b) * this->coefficientButton);
this->coefficientButton += this->stepButton;
}
// Text idle to hover color
this->idleToHoverColorText[0] = this->textIdleColor;
this->coefficientText += this->stepText;
for (unsigned short i = 1; i < this->colorMaxStages; i++)
{
this->idleToHoverColorText[i].r = static_cast<unsigned>(static_cast<float>(auto_text_hover_color.r) * this->coefficientText);
this->idleToHoverColorText[i].g = static_cast<unsigned>(static_cast<float>(auto_text_hover_color.g) * this->coefficientText);
this->idleToHoverColorText[i].b = static_cast<unsigned>(static_cast<float>(auto_text_hover_color.b) * this->coefficientText);
this->coefficientText += this->stepText;
}
// Outline
this->shape.setOutlineColor(this->outlineIdleColor);
// Button
this->shape.setFillColor(this->buttonIdleColor);
// Text
this->text.setFillColor(this->textIdleColor);
}
gui::Button::~Button()
{
}
// Accessors:
const bool gui::Button::isPressed() const
{
if (this->buttonState == gui::ButtonStates::Active)
{
return true;
}
return false;
}
const std::string gui::Button::getString() const
{
return this->text.getString();
}
const uint32_t& gui::Button::getId() const
{
return this->id;
}
// Modifiers:
void gui::Button::setString(const std::string text)
{
this->text.setString(text);
}
void gui::Button::setId(const uint32_t id)
{
this->id = id;
}
// Functions:
// Updates the booleans for hover and pressed.
void gui::Button::update(const sf::Vector2i& mouse_position, bool update_buttons, bool update_text)
{
// Hover
if (this->shape.getGlobalBounds().contains(sf::Vector2f(mouse_position)))
{
this->buttonState = gui::ButtonStates::Hover;
// Pressed
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
this->buttonState = gui::ButtonStates::Active;
}
}
else
{
// Idle
this->buttonState = gui::ButtonStates::Idle;
}
switch (this->buttonState)
{
case gui::ButtonStates::Idle:
if (update_buttons)
{
this->shape.setOutlineColor(this->currentColorOutline);
this->shape.setFillColor(this->currentColorButton);
}
else
{
this->shape.setOutlineColor(this->outlineIdleColor);
this->shape.setFillColor(this->buttonIdleColor);
}
if (update_text)
{
this->text.setFillColor(this->currentColorText);
}
else
{
this->text.setFillColor(this->textIdleColor);
}
break;
case gui::ButtonStates::Hover:
if (update_buttons)
{
this->shape.setOutlineColor(this->currentColorOutline);
this->shape.setFillColor(this->currentColorButton);
}
else
{
this->shape.setOutlineColor(this->outlineHoverColor);
this->shape.setFillColor(this->buttonHoverColor);
}
if (update_text)
{
this->text.setFillColor(this->currentColorText);
}
else
{
this->text.setFillColor(this->textHoverColor);
}
break;
case gui::ButtonStates::Active:
this->shape.setOutlineColor(this->outlineActiveColor);
this->shape.setFillColor(this->buttonActiveColor);
this->text.setFillColor(this->textActiveColor);
break;
default:
this->shape.setOutlineColor(sf::Color::Green);
this->shape.setFillColor(sf::Color::Red);
this->text.setFillColor(sf::Color::Blue);
break;
}
}
void gui::Button::updateButtonColor(const float& dt)
{
if (this->buttonState == gui::ButtonStates::Hover)
{
this->buttonTimer += 100.f * dt;
if (this->buttonTimer >= this->buttonTimerMax)
{
this->buttonTimer = 0.f;
this->currentColorOutline = this->idleToHoverColorOutline[static_cast<uint32_t>(this->colorNumber)];
this->currentColorButton = this->idleToHoverColorButton[static_cast<uint32_t>(this->colorNumber)];
this->currentColorText = this->idleToHoverColorText[static_cast<uint32_t>(this->colorNumber)];
if (static_cast<uint32_t>(this->colorNumber) < (this->colorMaxStages - 1))
{
++this->colorNumber;
}
}
}
else if (this->buttonState == gui::ButtonStates::Idle)
{
this->buttonTimer += 100.f * dt;
if (this->buttonTimer >= this->buttonTimerMax)
{
this->buttonTimer = 0.f;
this->currentColorOutline = this->idleToHoverColorOutline[static_cast<uint32_t>(this->colorNumber)];
this->currentColorButton = this->idleToHoverColorButton[static_cast<uint32_t>(this->colorNumber)];
this->currentColorText = this->idleToHoverColorText[static_cast<uint32_t>(this->colorNumber)];
if (this->colorNumber > 0)
{
--this->colorNumber;
}
}
}
}
void gui::Button::render(sf::RenderTarget* target)
{
target->draw(this->shape);
target->draw(this->text);
}