-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.cpp
More file actions
72 lines (61 loc) · 1.76 KB
/
button.cpp
File metadata and controls
72 lines (61 loc) · 1.76 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
#include "button.hpp"
//private functions
//Constructors
Button::Button(float x, float y, float width, float height, sf::Font* font, std::string text,
sf::Color idleColor,sf::Color hoverColor,sf::Color activeColor){
this->buttonState = BTN_IDLE;
this->shape.setPosition(sf::Vector2f(x,y));
this->shape.setSize(sf::Vector2f(width, height));
this->font = font;
this->text.setFont(*this->font);
this->text.setString(text);
this->text.setFillColor(sf::Color::White);
this->text.setCharacterSize(24);
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.1f) - (this->text.getGlobalBounds().height / 2.f)
);
this->idleColor = idleColor;
this->hoverColor = hoverColor;
this->activeColor = activeColor;
this->shape.setFillColor(this->idleColor);
}
Button::~Button(){
}
//Accessors
const bool Button::isPressed() const{
if(this->buttonState == BTN_ACTIVE)
return true;
return false;
}
//Functions
void Button::update(const sf::Vector2f 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);
break;
case BTN_HOVER:
this->shape.setFillColor(this->hoverColor);
break;
case BTN_ACTIVE:
this->shape.setFillColor(this->activeColor);
break;
default:
this->shape.setFillColor(sf::Color::Red);
break;
}
}
void Button::render(sf::RenderTarget* target){
target->draw(this->shape);
target->draw(this->text);
}