-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.cpp
93 lines (81 loc) · 2.8 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
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
#include "button.hpp"
namespace loopline
{
Button::Button(sf::Vector2f const &size, sf::Vector2f const &pos)
: size(size), position(pos), button(size), textNormal(sf::Color::Red), textHover(sf::Color::Red), textClick(sf::Color::Blue), buttonNormal(sf::Color{255,255,255,128}), buttonHover(sf::Color{255,255,0,128}), buttonClick(sf::Color::Red)
{
button.setOrigin(0.5f * size);
button.setPosition(pos);
}
bool Button::isHovered(sf::Vector2f const &mousePos) const
{
return button.getGlobalBounds().contains({static_cast<float>(mousePos.x), static_cast<float>(mousePos.y)});
}
void Button::click() const
{
for(auto& func : clickFunctions)
{
func();
}
}
void Button::setButton(sf::Vector2f const &size, sf::Vector2f const &pos)
{
this->size = size;
this->position = pos;
button.setSize(size);
button.setOrigin(0.5f * size);
button.setPosition(pos);
setText(text);
}
void Button::setText(std::string const &text)
{
this->text = text;
}
bool Button::mouseUpdate(sf::Vector2f const &mousePos)
{
if(!active) return false;
auto oldState = state;
if(isHovered(mousePos) && sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
state = CLICK;
}
else if(isHovered(mousePos))
{
state = HOVER;
}
else
{
state = NORMAL;
}
switch(state)
{
case NORMAL:
button.setFillColor(buttonNormal);
buttonText.setFillColor(textNormal);
break;
case HOVER:
button.setFillColor(buttonHover);
buttonText.setFillColor(textHover);
break;
case CLICK:
button.setFillColor(buttonClick);
buttonText.setFillColor(textClick);
break;
}
if(state == CLICK && oldState != state)
{
click();
}
return isHovered(mousePos);
}
void Button::drawUI(sf::RenderWindow &window, sf::Font const &font) const
{
if(!active) return;
sf::Text drawText{text, font};
drawText.setOrigin(0.5f * sf::Vector2f{drawText.getLocalBounds().width, drawText.getLocalBounds().height} + sf::Vector2f{0.f, 10.f});
drawText.setFont(font);
drawText.setPosition(button.getPosition());
window.draw(button);
window.draw(drawText);
}
}