-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.cpp
66 lines (53 loc) · 1.73 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
#include "button.h"
Button::Button(float x, float y, float width,float height,sf::Font* font,std::string text,
sf::Color idleColor,sf::Color hoverColor, sf::Color pressedColor)
{
this->buttonState = BTN_IDLE;
this->jenson.setPosition(sf::Vector2f(x,y));
this->jenson.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(20);
this->text.setPosition(
this->jenson.getPosition().x+(jenson.getGlobalBounds().width/2.0f)-this->text.getGlobalBounds().width/2.0f,
this->jenson.getPosition().y+(jenson.getGlobalBounds().height/2.0f)-this->text.getGlobalBounds().height
);
this->idleColor=idleColor;
this->hoverColor=hoverColor;
this->pressedColor=pressedColor;
this->jenson.setFillColor(this->idleColor);
this->jenson.setOutlineThickness(2.0f);
this->jenson.setOutlineColor(sf::Color(212, 81, 25));
}
Button::~Button()
{
}
void Button::update(const sf::Vector2f mousePos)
{
this->buttonState=BTN_IDLE;
this->jenson.setFillColor(this->idleColor);
//hover
if(this->jenson.getGlobalBounds().contains(mousePos)){
this->buttonState=BTN_HOVER;
this->jenson.setFillColor(this->hoverColor);
//pressed
if(sf::Mouse::isButtonPressed(sf::Mouse::Left)){
this->buttonState=BTN_PRESSED;
this->jenson.setFillColor(this->pressedColor);
}
}
}
void Button::render(sf::RenderWindow& window)
{
window.draw(this->jenson);
window.draw(this->text);
}
bool Button::isPressed() const
{
if(this->buttonState==BTN_PRESSED){
return true;
}
return false;
}