-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathButton.cpp
More file actions
76 lines (58 loc) · 2.03 KB
/
Button.cpp
File metadata and controls
76 lines (58 loc) · 2.03 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
#include "Button.h"
Button::Button(float posX, float posY, float width, float height, unsigned int characterSize, std::string fontFile, std::string text,
sf::Color idleColor, sf::Color hoverColor, sf::Color activeColor, sf::Color textColor, sf::Color textHoverColor) {
this->buttonState = BTN_IDLE;
this->shape.setSize(sf::Vector2<float>(width, height));
this->shape.setOrigin(this->shape.getGlobalBounds().width / 2, this->shape.getGlobalBounds().height / 2);
this->shape.setPosition(sf::Vector2<float>(posX, posY));
buttonFont.loadFromFile(fontFile);
this->text.setFont(buttonFont);
this->text.setCharacterSize(characterSize);
this->text.setString(text);
this->text.setOrigin(this->text.getGlobalBounds().width / 2, this->text.getGlobalBounds().height / 2);
this->text.setFillColor(textColor);
this->text.setPosition(posX, posY);
this->idleColor = idleColor;
this->hoverColor = hoverColor;
this->activeColor = activeColor;
this->textColor = textColor;
this->textHoverColor = textHoverColor;
this->shape.setFillColor(this->idleColor);
}
const bool Button::isPressed() const {
if (this->buttonState == BTN_ACTIVE) {
return true;
}
return false;
}
void Button::update(const sf::Vector2<float> mousePos) {
//Idle
this->buttonState = BTN_IDLE;
//Hover
if (this->shape.getGlobalBounds().contains(mousePos)) {
this->buttonState = BTN_HOVER;
//Pressed
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
this->buttonState = BTN_ACTIVE;
}
}
switch (this->buttonState) {
case BTN_IDLE:
this->shape.setFillColor(this->idleColor);
this->text.setFillColor(textColor);
break;
case BTN_HOVER:
this->shape.setFillColor(this->hoverColor);
this->text.setFillColor(textHoverColor);
break;
case BTN_ACTIVE:
this->shape.setFillColor(this->activeColor);
break;
default:
this->shape.setFillColor(sf::Color::Red);
}
}
void Button::renderTo(sf::RenderWindow& window) {
window.draw(this->shape);
window.draw(this->text);
}