-
Notifications
You must be signed in to change notification settings - Fork 7
/
Button.cpp
84 lines (70 loc) · 2.59 KB
/
Button.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
/******************************************************************************
* @file Button.cpp
* @author Andrés Gavín Murillo, 716358
* @author Rubén Rodríguez Esteban, 737215
* @date Mayo 2020
* @coms Videojuegos - OutRun
******************************************************************************/
#include "Button.hpp"
using namespace std;
using namespace sf;
Button::Button() = default;
Button::Button(float x, float y, float width, float height, Font &f,
const string &text, Color idleColor, Color hoverColor, Color activeColor, int initialState,
float screenScale) {
shape.setPosition(Vector2f(x, y));
shape.setSize(Vector2f(width, height));
shape.setOutlineColor(Color::Black);
shape.setOutlineThickness(3.0f * screenScale);
textButton.setString(text);
textButton.setFont(f);
textButton.setFillColor(Color::Blue);
textButton.setCharacterSize(static_cast<unsigned int>(int(12.0f * screenScale)));
textButton.setPosition(
shape.getPosition().x + (shape.getGlobalBounds().width / 2.f) - textButton.getGlobalBounds().width / 2.f,
shape.getPosition().y + (shape.getGlobalBounds().height / 2.f) - textButton.getGlobalBounds().height / 2.f -
5
);
// Store the possible colors of the button
idleColorButton = idleColor;
hoverColorButton = hoverColor;
activeColorButton = activeColor;
// Check the initial state of the button
switch (initialState) {
case 0:
shape.setFillColor(idleColorButton);
break;
case 1:
shape.setFillColor(hoverColorButton);
break;
case 2:
shape.setFillColor(activeColorButton);
break;
default:
break;
}
}
void Button::setButtonState(button_states stateButton) {
switch (stateButton) {
case BUTTON_IDLE:
shape.setFillColor(idleColorButton);
break;
case BUTTON_HOVER:
shape.setFillColor(hoverColorButton);
break;
case BUTTON_PRESSED:
shape.setFillColor(activeColorButton);
}
}
void Button::render(RenderTexture *app) {
app->draw(shape);
app->draw(textButton);
}
void Button::setTextButton(const string &newString) {
textButton.setString(newString);
textButton.setPosition(
shape.getPosition().x + (shape.getGlobalBounds().width / 2.f) - textButton.getGlobalBounds().width / 2.f,
shape.getPosition().y + (shape.getGlobalBounds().height / 2.f) - textButton.getGlobalBounds().height / 2.f -
5
);
}