-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.cpp
More file actions
79 lines (60 loc) · 2.05 KB
/
Copy pathButton.cpp
File metadata and controls
79 lines (60 loc) · 2.05 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
#include "Button.h"
Button:: Button(float x, float y, float height, float width, std::string text, sf::Font* font,
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::Yellow);
this->text.setCharacterSize(30);
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->idleColor = idleColor;
this->hoverColor = hoverColor;
this->activeColor = activeColor;
this->shape.setFillColor(this->idleColor);
}
Button::~Button(){
}
// accessor
const bool Button::isPressed() const{
if (this->buttonState == BTN_PRESSED){ // button is pressed
return true;
}
return false;
}
void Button:: update(const sf::Vector2f& mousePos){
// update the boolean for hover and pressed
// 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_PRESSED;
}
}
switch(this->buttonState){
case BTN_IDLE:
this->shape.setFillColor(this->idleColor);
break;
case BTN_HOVER:
this->shape.setFillColor(this->hoverColor);
break;
case BTN_PRESSED:
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);
}