-
Notifications
You must be signed in to change notification settings - Fork 0
/
Slider.cpp
201 lines (172 loc) · 4.85 KB
/
Slider.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
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
#include "Slider.h"
Slider::Slider(StateManager* stateManager, ResID fontResID, std::string text, float x, float y, bool sharedFont) : stateManager(stateManager), fontResID(fontResID), text(text), x(x), y(y), sharedFont(sharedFont)
{
resManager = stateManager->GetResourceManager();
screen = stateManager->GetGpuTarget();
width = x + 300.0f;
height = y + 60.0f;
rx = width + 100.0f;
ry = y + 25.0f;
rw = rx + 300.0f;
rh = y + 35.0f;
bx = rx - 10.0f;
by = ry - 20.0f;
bw = bx + 20.0f;
bh = by + 50.0f;
edgeLightColor = { 127, 127, 127, 255 };
edgeDarkColor = { 27, 27, 27, 255 };
fillColor = { 55, 55, 55, 255 };
hoverColor = { 127, 127, 127, 255 };
textColor = { 255, 255, 255, 255 };
railColor = { 31, 31, 31, 255 };
valueColor = { 0, 0, 255, 255 };
click = false;
drag = false;
sliderValue = 0;
//sliderMin = 0;
//sliderMax = 100;
}
Slider::~Slider()
{
Free();
}
void Slider::Free()
{
resManager = nullptr;
stateManager = nullptr;
screen = nullptr;
}
void Slider::Draw()
{
// Slider Rail
GPU_RectangleFilled(screen, rx, ry, rw, rh, railColor);
GPU_RectangleFilled(screen, rx, ry, bx + 10.0f, rh, valueColor);
GPU_Line(screen, rx, ry, rw, ry, edgeLightColor);
GPU_Line(screen, rx, ry, rx, rh, edgeLightColor);
GPU_Line(screen, rx, rh, rw, rh, edgeDarkColor);
GPU_Line(screen, rw, ry, rw, rh, edgeDarkColor);
// Slider Button
if (GetEvent() == SliderEventState::Hover)
GPU_RectangleFilled(screen, bx, by, bw, bh, hoverColor);
else
GPU_RectangleFilled(screen, bx, by, bw, bh, fillColor);
GPU_Line(screen, bx, by, bw, by, edgeLightColor);
GPU_Line(screen, bx, by, bx, bh, edgeLightColor);
GPU_Line(screen, bx, bh, bw, bh, edgeDarkColor);
GPU_Line(screen, bw, by, bw, bh, edgeDarkColor);
//GPU_Rectangle(screen, x, y, width, height, { 255,0,0,255 });
//GPU_Rectangle(screen, x+1, y+1, width-1, height-1, { 0,0,255,255 });
// Value
if (sharedFont)
{
resManager->GetSharedFontResource(fontResID)->SetColor(textColor.r, textColor.g, textColor.b, textColor.a);
resManager->GetSharedFontResource(fontResID)->DrawText(std::to_string(sliderValue) + " %", rw + 25.0f, y + 30.0f);
}
else
{
resManager->GetFontResource(fontResID)->SetColor(textColor.r, textColor.g, textColor.b, textColor.a);
resManager->GetFontResource(fontResID)->DrawText(std::to_string(sliderValue) + " %", rw + 25.0f, y + 30.0f);
}
// Text
if (sharedFont)
{
resManager->GetSharedFontResource(fontResID)->SetColor(textColor.r, textColor.g, textColor.b, textColor.a);
resManager->GetSharedFontResource(fontResID)->DrawText(text, x + 25.0f, y + 30.0f);
}
else
{
resManager->GetFontResource(fontResID)->SetColor(textColor.r, textColor.g, textColor.b, textColor.a);
resManager->GetFontResource(fontResID)->DrawText(text, x + 25.0f, y + 30.0f);
}
}
void Slider::Event(SDL_Event &e)
{
if (e.type == SDL_MOUSEMOTION)
{
if (drag)
{
// Move the slider button (part 1/2)
bx = e.motion.x - 10.0f;
// Clamp it
if (bx < (rx - 10.0f))
bx = rx - 10.0f;
if (bx > (rw - 10.0f))
bx = rw - 10.0f;
// Move the slider button (part 2/2)
bw = bx + 20.0f;
// Update the slider value
sliderValue = (int)(100.0f*(bx + 10.0f) / (rw - rx) - (rw - rx));
}
}
else if (e.type == SDL_MOUSEBUTTONDOWN)
{
if ((e.motion.x >= bx) && (e.motion.x < bw) && (e.motion.y >= by) && (e.motion.y < bh))
{
SetEvent(Slider::SliderEventState::Down);
drag = true;
}
}
else if (e.type == SDL_MOUSEBUTTONUP)
{
SetEvent(Slider::SliderEventState::Up);
drag = false;
if ((e.motion.x >= bx) && (e.motion.x < bw) && (e.motion.y >= by) && (e.motion.y < bh))
{
SetClick(true);
}
}
else if ((e.motion.x >= bx) && (e.motion.x < bw) && (e.motion.y >= by) && (e.motion.y < bh))
{
if (GetEvent() != SliderEventState::Down)
{
SetEvent(Slider::SliderEventState::Hover);
}
}
else
{
SetEvent(Slider::SliderEventState::Up);
}
}
void Slider::SetEvent(SliderEventState seState)
{
this->sliderEventState = seState;
}
Slider::SliderEventState Slider::GetEvent()
{
return sliderEventState;
}
void Slider::SetClick(bool click)
{
this->click = click;
}
bool Slider::GetClick()
{
return click;
}
void Slider::SetValue(int value)
{
// Clamp it
if (value < 0)
value = 0;
if (value > 100)
value = 100;
/*
// Move the slider button (part 1/2)
bx = e.motion.x - 10.0f;
// Move the slider button (part 2/2)
bw = bx + 20.0f;
*/
//sliderValue = (int)(100.0f*(bx + 10.0f) / (rw - rx) - (rw - rx));
//
//((sliderValue + (rw - rx))*(rw - rx))/100.0f - 10.0f = bx ;
//
// Update the slider value
sliderValue = value;
// Update the slider bar
bx = ((sliderValue + (rw - rx))*(rw - rx)) / 100.0f - 10.0f;
bw = bx + 20.0f;
}
int Slider::GetValue()
{
return sliderValue;
}