-
Notifications
You must be signed in to change notification settings - Fork 0
/
DropList.cpp
270 lines (237 loc) · 7.47 KB
/
DropList.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include "DropList.h"
DropList::DropList(StateManager* stateManager, ResID fontResID, std::string label, float x, float y, bool sharedFont) : stateManager(stateManager), fontResID(fontResID), label(label), x(x), y(y), sharedFont(sharedFont)
{
resManager = stateManager->GetResourceManager();
screen = stateManager->GetGpuTarget();
width = x + 300.0f;
height = y + 60.0f;
dlx = width;
dly = y;
dlw = width + 350.0f;
dlh = y + 60.0f;
btx = dlw - 60.0f;
bty = dly = y;
btw = dlw;
bth = dlh;
edgeLightColor = { 127, 127, 127, 255 };
edgeDarkColor = { 27, 27, 27, 255 };
fillColor = { 55, 55, 55, 255 };
labelColor = { 255, 255, 255, 255 };
listColor = { 255, 255, 255, 255 };
listSelectionColor = { 0, 0, 255, 255 };
click = false;
showList = false;
vListItemSelected = -1; // Not selected
// Mouse
mx = 0;
my = 0;
mouseHoverSelect = -1;
}
DropList::~DropList()
{
Free();
}
void DropList::Free()
{
vList.clear();
resManager = nullptr;
stateManager = nullptr;
screen = nullptr;
}
void DropList::Draw()
{
// DropList
GPU_Rectangle(screen, dlx, dly, dlw, dlh, edgeLightColor);
// Text
if (sharedFont)
{
resManager->GetSharedFontResource(fontResID)->SetColor(listColor.r, listColor.g, listColor.b, listColor.a);
if (vListItemSelected != -1)
resManager->GetSharedFontResource(fontResID)->DrawText(vList[vListItemSelected], dlx + 25.0f, dly + 30.0f);
}
else
{
resManager->GetFontResource(fontResID)->SetColor(listColor.r, listColor.g, listColor.b, listColor.a);
if (vListItemSelected != -1)
resManager->GetFontResource(fontResID)->DrawText(vList[vListItemSelected], dlx + 25.0f, dly + 30.0f);
}
// Drop Button
GPU_RectangleFilled(screen, btx, bty, btw, bth, fillColor);
GPU_Line(screen, btx, bty, btw, bty, edgeLightColor);
GPU_Line(screen, btx, bty, btx, bth, edgeLightColor);
GPU_Line(screen, btx, bth, btw, bth, edgeLightColor);
GPU_Line(screen, btw, bty, btw, bth, edgeLightColor);
if (showList)
{
GPU_Line(screen, btx + 1, bty + 1, btw - 1, bty + 1, edgeDarkColor);
GPU_Line(screen, btx + 1, bty + 1, btx + 1, bth - 1, edgeDarkColor);
GPU_Line(screen, btx + 1, bth - 1, btw - 1, bth - 1, edgeLightColor);
GPU_Line(screen, btw - 1, bty + 1, btw - 1, bth - 1, edgeLightColor);
GPU_TriFilled(screen, btx + 10.0f, bty + 15.0f, btw - 10.0f, bty + 15.0f, btx + (btw-btx)/2, bty + (bth-bty)*0.707f, { 224, 224, 224, 255 });
GPU_Tri(screen, btx + 10.0f, bty + 16.0f, btw - 10.0f, bty + 16.0f, btx + (btw - btx) / 2, bty + (bth - bty)*0.707f, { 0, 0, 0, 255 });
}
else
{
GPU_Line(screen, btx + 1, bty + 1, btw - 1, bty + 1, edgeLightColor);
GPU_Line(screen, btx + 1, bty + 1, btx + 1, bth - 1, edgeLightColor);
GPU_Line(screen, btx + 1, bth - 1, btw - 1, bth - 1, edgeDarkColor);
GPU_Line(screen, btw - 1, bty + 1, btw - 1, bth - 1, edgeDarkColor);
GPU_TriFilled(screen, btx + 10.0f, bty + 15.0f, btw - 10.0f, bty + 15.0f, btx + (btw - btx) / 2, bty + (bth - bty)*0.707f, { 191, 191, 191, 255 });
GPU_Tri(screen, btx + 10.0f, bty + 16.0f, btw - 10.0f, bty + 16.0f, btx + (btw - btx) / 2, bty + (bth - bty)*0.707f, { 0, 0, 0, 255 });
}
if (showList)
{
GPU_RectangleFilled(screen, dlx, dlh, dlw, dlh + 15.0f + vList.size()*45, edgeDarkColor);
GPU_Rectangle(screen, dlx, dlh, dlw, dlh + 15.0f + vList.size() * 45, edgeLightColor);
// Draw the mouse hover selection (blue bar)
mouseHoverSelect = -1;
if ((mx >= dlx) && (mx < dlw) && (my >= dlh) && (my < dlh + 15.0f + vList.size() * 45.0f + 35.0f))
{
for (int i = 0; i < vList.size(); i++)
{
if ((my >= dlh + 10.0f + i * 45.0f) && (my < dlh + 15.0f + i * 45.0f + 35.0f))
{
mouseHoverSelect = i;
GPU_RectangleFilled(screen, dlx + 1, dlh + 10.0f + i * 45.0f, dlw - 1, dlh + 15.0f + i * 45.0f + 35.0f, listSelectionColor);
break;
}
}
}
// Draw all the text in the list
for (int i = 0; i < vList.size(); i++)
{
if (sharedFont)
{
if (vListItemSelected == i)
resManager->GetSharedFontResource(fontResID)->SetColor(255, 255, 255, 255);
else
resManager->GetSharedFontResource(fontResID)->SetColor(171, 171, 171, 255);
resManager->GetSharedFontResource(fontResID)->DrawText(vList[i], dlx + 25.0f, dly + 90.0f + i*45.0f);
}
else
{
if (vListItemSelected == i)
resManager->GetFontResource(fontResID)->SetColor(255, 255, 255, 255);
else
resManager->GetFontResource(fontResID)->SetColor(171, 171, 171, 255);
resManager->GetFontResource(fontResID)->DrawText(vList[i], dlx + 25.0f, dly + 90.0f + i*45.0f);
}
}
}
// Label
if (sharedFont)
{
resManager->GetSharedFontResource(fontResID)->SetColor(labelColor.r, labelColor.g, labelColor.b, labelColor.a);
resManager->GetSharedFontResource(fontResID)->DrawText(label, x + 25.0f, y + 30.0f);
}
else
{
resManager->GetFontResource(fontResID)->SetColor(labelColor.r, labelColor.g, labelColor.b, labelColor.a);
resManager->GetFontResource(fontResID)->DrawText(label, x + 25.0f, y + 30.0f);
}
}
void DropList::Event(SDL_Event &e)
{
if (e.type == SDL_MOUSEMOTION)
{
// Keep mouse coords for internal use
mx = e.motion.x;
my = e.motion.y;
}
else if (e.type == SDL_MOUSEBUTTONDOWN)
{
if ((e.motion.x >= dlx) && (e.motion.x < dlw) && (e.motion.y >= dly) && (e.motion.y < dlh))
{
SetEvent(DropList::DropListEventState::Down);
}
}
else if (e.type == SDL_MOUSEBUTTONUP)
{
SetEvent(DropList::DropListEventState::Up);
// User clicked in the list
if (showList)
{
if ((mx >= dlx) && (mx < dlw) && (my >= dlh) && (my < dlh + 15.0f + vList.size() * 45.0f + 35.0f))
{
if (mouseHoverSelect != -1)
{
SetClick(true);
vListItemSelected = mouseHoverSelect;
showList = false;
}
}
}
// User clicked on the main box
if ((e.motion.x >= dlx) && (e.motion.x < dlw) && (e.motion.y >= dly) && (e.motion.y < dlh))
{
SetClick(true);
showList = !showList;
}
// If user clicks outside of the list when it is showing, then we need to hide the list.
if ((e.motion.x < dlx) || (e.motion.x > dlw) || (e.motion.y < dly) || (e.motion.y > dlh + 15.0f + vList.size() * 45))
showList = false;
}
else if ((e.motion.x >= dlx) && (e.motion.x < dlw) && (e.motion.y >= dly) && (e.motion.y < dlh))
{
}
else
{
SetEvent(DropList::DropListEventState::Up);
}
}
void DropList::SetEvent(DropListEventState dleState)
{
this->dropListEventState = dleState;
}
DropList::DropListEventState DropList::GetEvent()
{
return dropListEventState;
}
void DropList::SetClick(bool click)
{
this->click = click;
}
bool DropList::GetClick()
{
return click;
}
void DropList::SetShowList(bool show)
{
this->showList = show;
}
bool DropList::GetShowList()
{
return showList;
}
bool DropList::EscIsPressed()
{
if (showList)
{
showList = false;
return true; // The list was open
}
return false; // This list was not open, so it nothing for this UI-control to concern about.
}
void DropList::AddItem(std::string item)
{
vList.push_back(item);
}
void DropList::SetItemSelectedByText(std::string ItemText)
{
for (int i = 0; i < vList.size(); i++)
{
if (vList[i] == ItemText)
{
vListItemSelected = i;
break;
}
}
}
std::string DropList::GetItemSelected()
{
if (vListItemSelected == -1)
return "";
if (vList.size() == 0 || vList.size() < vListItemSelected)
return "";
return vList.at(vListItemSelected);
}