-
Notifications
You must be signed in to change notification settings - Fork 3
/
sfml-gui-slider.hpp
213 lines (179 loc) · 8.72 KB
/
sfml-gui-slider.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
#include <SFML/Graphics.hpp>
#include <string>
#include <algorithm>
#include <stdarg.h>
// T is type of value used by slider
// LEN is how many sliders you want
template <typename T, int LEN>
class Slider
{
public:
Slider(const sf::Color& guicolor, const sf::Color& textcolor, float slidersize, const sf::Font& font, const std::string& text, ...)
: guiColor(sf::Color(guicolor.r - 40.0f, guicolor.g - 40.0f, guicolor.b - 40.0f)),
sliderTitle(text, font, 13)
{
// Visual / Graphical initialization
for(int i = 0; i < LEN; i++)
{
sliderBackground[i].setSize(sf::Vector2f(slidersize, 15.0f));
sliderBackground[i].setFillColor(guiColor);
sliderBar[i].setSize(sf::Vector2f(0.0f, 10.0f));
sliderBar[i].setFillColor(sf::Color(guiColor.r + 55, guiColor.g + 55, guiColor.b + 55));
slider[i].setSize(sf::Vector2f(10.0f, 15.0f));
slider[i].setFillColor(guicolor);
// Other slider stuff initialization
sliderValue[i] = 0.0f;
}
// Minimum and maximum value initialization
va_list values;
va_start(values, text);
// Unfortunately this only supports int min / max
// so you can't have a minimum like 2.5
for(int i = 0; i < LEN; i++)
{
minimumValues[i] = va_arg(values, int);
}
for(int i = 0; i < LEN; i++)
{
maximumValues[i] = va_arg(values, int);
}
va_end(values);
// Text / Font initialization
sliderTitle.setFillColor(textcolor);
for(int i = 0; i < LEN; i++)
{
sliderTextValue[i].setString(std::to_string(minimumValues[i]));
sliderTextValue[i].setFont(font);
sliderTextValue[i].setCharacterSize(13);
sliderTextValue[i].setFillColor(textcolor);
}
}
~Slider()
{
}
void update(const sf::RectangleShape& GUI_PID, int slot, const sf::RenderWindow& window, T* value)
{
for(int i = 0; i < LEN; i++)
{
// Positioning / Resizing the slider
sliderBackground[i].setPosition((GUI_PID.getGlobalBounds().left + 10.0f) + ((sliderBackground[i].getSize().x + 10.0f) * i), GUI_PID.getGlobalBounds().top + slot * 20.0f);
if(oldSliderBackgroundPosition[i] != sliderBackground[i].getPosition()) // Checks if the position of sliderBackground has changed, so it doesn't need to update positions every frame
{
slider[i].setPosition(((sliderBackground[i].getSize().x - slider[i].getSize().x) * sliderValue[i]) + sliderBackground[i].getPosition().x, sliderBackground[i].getPosition().y);
sliderBar[i].setPosition(sliderBackground[i].getPosition().x + 3.0f, sliderBackground[i].getPosition().y + 2.0f);
sliderBar[i].setSize(sf::Vector2f(slider[i].getPosition().x - sliderBackground[i].getPosition().x, 10.0f));
sliderTextValue[i].setPosition(sliderBackground[i].getPosition().x + sliderBackground[i].getSize().x / 2, sliderBackground[i].getPosition().y);
oldSliderBackgroundPosition[i] = sliderBackground[i].getPosition();
}
}
sliderTitle.setPosition(sliderBackground[LEN - 1].getPosition().x + sliderBackground[LEN - 1].getSize().x + 10.0f, sliderBackground->getPosition().y);
if(sliderInitFlag)
{
for(int i = 0; i < LEN; i++)
{
updateValue(value);
}
sliderInitFlag = false;
}
// Input stuff
sf::FloatRect mousePos = {static_cast<sf::Vector2f>(sf::Mouse::getPosition(window)), sf::Vector2f(1, 1)};
if(window.hasFocus()) // Checks if window has focus so you don't move a slider while not focused
{
switch(currentSelected)
{
case SELECTED_THIS:
slider[currentSelectedSlider].setFillColor(sf::Color(guiColor.r + 100, guiColor.g + 50, guiColor.b + 50));
sliderValue[currentSelectedSlider] = (float) ((sf::Mouse::getPosition(window).x)
- sliderBackground[currentSelectedSlider].getPosition().x)
/ (float) sliderBackground[currentSelectedSlider].getSize().x;
// Swap out this clamp code if you want support for c++ versions older than C++17
sliderValue[currentSelectedSlider] = std::clamp(sliderValue[currentSelectedSlider], 0.0f, 1.0f); // Clamps sliderValue to 0 - 1
updateValue(value);
sliderBar[currentSelectedSlider].setSize(sf::Vector2f(slider[currentSelectedSlider].getPosition().x
- sliderBackground[currentSelectedSlider].getPosition().x, // X
10.0f)); // Y
slider[currentSelectedSlider].setPosition(((sliderBackground[currentSelectedSlider].getSize().x
- slider[currentSelectedSlider].getSize().x)
* sliderValue[currentSelectedSlider])
+ sliderBackground[currentSelectedSlider].getPosition().x, // X
sliderBackground[currentSelectedSlider].getPosition().y); // Y
if(!sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
currentSelected = SELECTED_NOTHING;
}
break;
case SELECTED_SOMETHING_ELSE:
// While selecting something else (e.g., moving a gui)
// it will wait for you to let go of lmb,
// so you dont move a slider while grabbing something else.
if(!sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
currentSelected = SELECTED_NOTHING;
}
break;
case SELECTED_NOTHING:
for(int i = 0; i < LEN; i++)
{
slider[i].setFillColor(sf::Color(guiColor.r + 40, guiColor.g + 40, guiColor.b + 40));
// If you haven't selected anything it checks for lmb
if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
if(mousePos.intersects(sliderBackground[i].getGlobalBounds()))
{
currentSelected = SELECTED_THIS;
currentSelectedSlider = i;
break;
} else
{
currentSelected = SELECTED_SOMETHING_ELSE;
}
}
}
break;
}
}
}
void draw(sf::RenderWindow& window, int slot, const sf::RectangleShape& GUI_PID, T* value)
{
update(GUI_PID, slot, window, value);
for(int i = 0; i < LEN; i++)
{
window.draw(sliderBackground[i]);
window.draw(sliderBar[i]);
window.draw(slider[i]);
window.draw(sliderTextValue[i]);
}
window.draw(sliderTitle);
}
private:
void updateValue(T* value)
{
for(int i = 0; i < LEN; i++)
{
int maximum = maximumValues[i] - minimumValues[i];
value[i] = maximum* sliderValue[i] + minimumValues[i];
std::stringstream ss;
ss << value[i];
sliderTextValue[i].setString(ss.str());
}
}
sf::RectangleShape sliderBackground[LEN]; // Background for slider
sf::RectangleShape sliderBar[LEN]; // Slider status bar
sf::RectangleShape slider[LEN]; // The slider you grab
sf::Color guiColor; // Color of the gui in use
sf::Text sliderTitle; // Text to right of the sliders
sf::Text sliderTextValue[LEN]; // Value of slider on the screen
sf::Vector2f oldSliderBackgroundPosition[LEN]; // Previous position of sliderBackground
float sliderValue[LEN]; // Current value of slider within a range of 0 - 1
int minimumValues[LEN];
int maximumValues[LEN];
bool sliderInitFlag = true;
enum Selected : unsigned char
{
SELECTED_THIS,
SELECTED_SOMETHING_ELSE,
SELECTED_NOTHING
};
Selected currentSelected = SELECTED_NOTHING;
unsigned char currentSelectedSlider = 0; // Which slider is currently being slid
};