-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.cpp
103 lines (92 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
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
94
95
96
97
98
99
100
101
102
103
#include <SFML/Graphics.hpp>
#include "button.hpp"
//constructor
Button::Button(sf::RenderWindow &win) : window(win)
{
}
//get bounding box around button
void Button::updateBounds()
{
bounds = getGlobalBounds();
}
//get if the mouse is above the button
bool Button::gethovered()
{
mouse_position = sf::Mouse::getPosition(window);
updateBounds();
if (bounds.contains(mouse_position.x, mouse_position.y))
{
hovered = true;
return true;
}
hovered = false;
return false;
}
//get if the button is currently pressed down
bool Button::getclicked()
{
if (gethovered())
{
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
clicked = true;
return true;
}
}
clicked = false;
return false;
}
bool Button::getfalling()
{
if (clicked)
{
if (!sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
return true;
}
}
clicked = false;
return false;
}
bool Button::getrising()
{
if (!clicked)
{
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
return true;
}
}
clicked = false;
return false;
}
// change to texture based on state of button
void Button::updatetexture()
{
getclicked();
if (clicked)
{
setTexture(clickedTexture);
}
else if (hovered)
{
setTexture(hoveredTexture);
}
else
{
setTexture(baseTexture);
}
}
//set texture for button states
void Button::setBaseTexture(sf::Texture texture)
{
baseTexture = texture;
}
void Button::setHoverTexture(sf::Texture texture)
{
hoveredTexture = texture;
}
void Button::setClickedTexture(sf::Texture texture)
{
clickedTexture = texture;
}