-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.hpp
219 lines (188 loc) · 5.08 KB
/
Button.hpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#ifndef BUTTON_H
#define BUTTON_H
#include <SFML/Graphics.hpp>
using namespace sf;
class Button
{
private:
Shape *btnShape;
Color originalColor;
bool isPressed;
bool pressDarkening;
float darkenFactor;
public:
Button(Shape *buttonShape);
Button(Shape* buttonShape, Color fillColor);
void setPosition(Vector2f pos);
void setFillColor(Color color);
Color getFillColor();
void setPressDarkening(bool doesDarken);
void setDarkenFactor(float factor);
bool isButtonPressedDown(RenderWindow *window);
bool isButtonPressedUP(RenderWindow* window);
bool isButtonPressed(RenderWindow* window);
void drawButton(RenderWindow* window);
private:
void darkenButton();
void undarkenButton();
};
/// <summary>
/// Construct a new button object
/// </summary>
/// <param name="buttonShape">the shape of the button</param>
Button::Button(Shape* buttonShape)
{
btnShape = buttonShape;
originalColor = buttonShape->getFillColor();
isPressed = false;
pressDarkening = true;
darkenFactor = .75;
}
/// <summary>
/// Construct a new button object and give it the given fill color
/// </summary>
/// <param name="buttonShape">the shape of the button</param>
/// <param name="fillColor">the fill color of the button</param>
Button::Button(Shape* buttonShape, Color fillColor)
{
btnShape = buttonShape;
btnShape->setFillColor(fillColor);
originalColor = fillColor;
pressDarkening = true;
darkenFactor = .75;
}
/// <summary>
/// Set the position of the button
/// </summary>
/// <param name="pos">The new position of this button</param>
void Button::setPosition(Vector2f pos)
{
btnShape->setPosition(pos);
}
/// <summary>
/// Set the fill fillColor of this button
/// </summary>
/// <param name="fillColor">The new fill fillColor of the button</param>
void Button::setFillColor(Color color)
{
btnShape->setFillColor(color);
}
/// <summary>
/// Get the fill color of the button
/// </summary>
/// <returns>the fill color of the button</returns>
Color Button::getFillColor()
{
return btnShape->getFillColor();
}
/// <summary>
/// Set whether the button darkens when pressed
/// </summary>
/// <param name="doesDarken">whether the button darkens or not</param>
void Button::setPressDarkening(bool doesDarken)
{
pressDarkening = doesDarken;
}
/// <summary>
/// Set the factor of darkening
/// </summary>
/// <param name="factor">the factor of darkening</param>
void Button::setDarkenFactor(float factor)
{
darkenFactor = factor;
}
/// <summary>
/// darken the button
/// </summary>
void Button::darkenButton()
{
Color currColor = btnShape->getFillColor();
currColor.r *= darkenFactor;
currColor.g *= darkenFactor;
currColor.b *= darkenFactor;
btnShape->setFillColor(currColor);
}
/// <summary>
/// set button color back to its original color
/// </summary>
void Button::undarkenButton()
{
btnShape->setFillColor(originalColor);
}
/// <summary>
/// See if this button has been pressed down
/// </summary>
/// <param name="window">the window in which this button is in</param>
/// <returns>true if this button has been pressed down
/// and false otherwise</returns>
bool Button::isButtonPressedDown(RenderWindow* window)
{
Vector2i mousePos = Mouse::getPosition(*window);
Vector2f mouseWorldPos(mousePos);
if (Mouse::isButtonPressed(Mouse::Button::Left)
&& btnShape->getGlobalBounds().contains(mouseWorldPos)
&& !isPressed)
{
isPressed = true;
if(pressDarkening)
darkenButton();
return true;
}
isButtonPressed(window);
return false;
}
/// <summary>
/// See if this button has been released
/// </summary>
/// <param name="window">the window in which this button is in</param>
/// <returns>true if this button has being released
/// and false otherwise</returns>
bool Button::isButtonPressedUP(RenderWindow* window)
{
Vector2i mousePos = Mouse::getPosition(*window);
Vector2f mouseWorldPos(mousePos);
if (Mouse::isButtonPressed(Mouse::Button::Left)
&& btnShape->getGlobalBounds().contains(mouseWorldPos)
&& isPressed)
{
isPressed = false;
if(pressDarkening)
undarkenButton();
return true;
}
isButtonPressed(window);
return false;
}
/// <summary>
/// See if this button is being held down
/// </summary>
/// <param name="window">the window in which this button is in</param>
/// <returns>true if this button is being held down
/// and false otherwise</returns>
bool Button::isButtonPressed(RenderWindow* window)
{
Vector2i mousePos = Mouse::getPosition(*window);
Vector2f mouseWorldPos(mousePos);
if (Mouse::isButtonPressed(Mouse::Button::Left)
&& btnShape->getGlobalBounds().contains(mouseWorldPos))
{
if(!isPressed && pressDarkening)
darkenButton();
isPressed = true;
return true;
}
isPressed = false;
if(pressDarkening)
undarkenButton();
return false;
}
/// <summary>
/// Draw this button
/// </summary>
/// <param name="window">the window that this button should be
/// drawn to</param>
void Button::drawButton(RenderWindow* window)
{
window->draw(*btnShape);
}
#endif