-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.cpp
More file actions
118 lines (101 loc) · 2.89 KB
/
button.cpp
File metadata and controls
118 lines (101 loc) · 2.89 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
#include "button.h"
Button::Button(float x, float y, float width, float height,
sf::Font* font, std::string text, unsigned character_size,
sf::Color text_base_color, sf::Color text_hover_color, sf::Color text_click_color,
sf::Color base_color, sf::Color hover_color, sf::Color click_color,
sf::Color outline_base_color, sf::Color outline_hover_color, sf::Color outline_click_color,
short unsigned id)
{
buttonState = BUTTON_BASE;
this->id = id;
shape.setPosition(sf::Vector2f(x, y));
shape.setSize(sf::Vector2f(width, height));
shape.setFillColor(base_color);
shape.setOutlineThickness(1.f);
shape.setOutlineColor(outline_base_color);
this->font = font;
this->text.setFont(*this->font);
this->text.setString(text);
this->text.setFillColor(text_base_color);
this->text.setCharacterSize(character_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 / 2.f) - this->text.getGlobalBounds().height / 2.f
);
this->textBaseColor = text_base_color;
this->textHoverColor = text_hover_color;
this->textClickColor = text_click_color;
this->baseColor = base_color;
this->hoverColor = hover_color;
this->clickColor = click_color;
this->outlineBaseColor = outline_base_color;
this->outlineHoverColor = outline_hover_color;
this->outlineClickColor = outline_click_color;
}
Button::~Button()
{
}
const bool Button::isPressed() const
{
if (buttonState == BUTTON_CLICK) {
return true;
}
return false;
}
const std::string Button::getText() const
{
return text.getString();
}
const short unsigned& Button::getId() const
{
return id;
}
void Button::setText(const std::string text)
{
this->text.setString(text);
}
void Button::setId(const short unsigned id)
{
this->id = id;
}
void Button::update(const sf::Vector2f &mousePos)
{
buttonState = BUTTON_BASE;
if (shape.getGlobalBounds().contains(mousePos)) {
buttonState = BUTTON_HOVER;
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
buttonState = BUTTON_CLICK;
}
}
switch (buttonState) {
case BUTTON_BASE:{
shape.setFillColor(baseColor);
text.setFillColor(textBaseColor);
shape.setOutlineColor(outlineBaseColor);
break;
}
case BUTTON_HOVER: {
shape.setFillColor(hoverColor);
text.setFillColor(textHoverColor);
shape.setOutlineColor(outlineHoverColor);
break;
}
case BUTTON_CLICK: {
shape.setFillColor(clickColor);
text.setFillColor(textClickColor);
shape.setOutlineColor(outlineClickColor);
break;
}
default: {
shape.setFillColor(sf::Color::Red);
text.setFillColor(sf::Color::White);
shape.setOutlineColor(sf::Color::Green);
break;
}
}
}
void Button::render(sf::RenderTarget* target)
{
target->draw(shape);
target->draw(text);
}