-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathControls.cpp
538 lines (452 loc) · 12.8 KB
/
Controls.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#include "Controls.h"
#include "RenderManager.h"
#pragma region Base Control
void CControl::SetPosition(int x, int y)
{
m_x = x;
m_y = y;
}
void CControl::SetSize(int w, int h)
{
m_iWidth = w;
m_iHeight = h;
}
void CControl::GetSize(int &w, int &h)
{
w = m_iWidth;
h = m_iHeight;
}
bool CControl::Flag(int f)
{
if (m_Flags & f)
return true;
else
return false;
}
POINT CControl::GetAbsolutePos()
{
POINT p;
RECT client = parent->GetClientArea();
if (parent)
{
p.x = m_x + client.left;
p.y = m_y + client.top + 29;
}
return p;
}
void CControl::SetFileId(std::string fid)
{
FileIdentifier = fid;
}
#pragma endregion Implementations of the Base control functions
#pragma region CheckBox
CCheckBox::CCheckBox()
{
Checked = false;
m_Flags = UIFlags::UI_Clickable | UIFlags::UI_Drawable | UIFlags::UI_SaveFile;
m_iWidth = 13;
m_iHeight = 13;
FileControlType = UIControlTypes::UIC_CheckBox;
}
void CCheckBox::SetState(bool s)
{
Checked = s;
}
bool CCheckBox::GetState()
{
return Checked;
}
void CCheckBox::Draw(bool hover)
{
POINT a = GetAbsolutePos();
Render::Outline(a.x, a.y, 13, 13, Color(129, 129, 129, 255));
if (hover)
{
if (Checked)
{
Render::Clear(a.x + 2, a.y + 2, 9, 9, Color(255, 0, 0, 255));
}
else
{
Render::Clear(a.x + 2, a.y + 2, 9, 9, Color(129, 129, 129, 255));
}
Render::Outline(a.x + 2, a.y + 2, 9, 9, Color(20, 20, 20, 80));
}
else if (Checked)
{
Render::Clear(a.x + 2, a.y + 2, 9, 9, Color(255, 0, 0, 255));
Render::Outline(a.x + 2, a.y + 2, 9, 9, Color(20, 20, 20, 80));
}
}
void CCheckBox::OnUpdate(){ m_iWidth = 13; m_iHeight = 13; }
void CCheckBox::OnClick()
{
Checked = !Checked;
}
#pragma endregion Implementations of the Check Box functions
#pragma region Label
CLabel::CLabel()
{
m_Flags = UIFlags::UI_Drawable;
Text = "Default";
FileIdentifier = "Default";
}
void CLabel::Draw(bool hover)
{
POINT a = GetAbsolutePos();
Render::Text(a.x, a.y, Color(0, 0, 0, 255), Render::Fonts::MenuBold, Text.c_str());
}
void CLabel::SetText(std::string text)
{
Text = text;
}
void CLabel::OnUpdate(){}
void CLabel::OnClick(){}
#pragma endregion Implementations of the Label functions
#pragma region GroupBox
CGroupBox::CGroupBox()
{
Items = 1;
m_Flags = UIFlags::UI_Drawable | UIFlags::UI_RenderFirst;
Text = "Default";
FileIdentifier = "Default";
}
void CGroupBox::Draw(bool hover)
{
POINT a = GetAbsolutePos();
RECT txtSize = Render::GetTextSize(Render::Fonts::MenuBold, Text.c_str());
Render::Clear(a.x+2, a.y+2, m_iWidth-4, m_iHeight-4, Color(245,245,245, 255));
Render::Text(a.x + 15, a.y - (txtSize.bottom / 2), Color(0, 0, 0, 255), Render::Fonts::MenuBold, Text.c_str());
Render::Line(a.x, a.y, a.x + 12, a.y, Color(129, 129, 129, 255));
Render::Line(a.x + 15 + txtSize.right + 5, a.y, a.x + m_iWidth, a.y, Color(129, 129, 129, 255));
Render::Line(a.x, a.y, a.x, a.y + m_iHeight, Color(129, 129, 129, 255));
Render::Line(a.x, a.y + m_iHeight, a.x + m_iWidth, a.y + m_iHeight, Color(129, 129, 129, 255));
Render::Line(a.x + m_iWidth, a.y, a.x + m_iWidth, a.y + m_iHeight, Color(129, 129, 129, 255));
}
void CGroupBox::SetText(std::string text)
{
Text = text;
}
void CGroupBox::PlaceLabledControl(std::string Label, CTab *Tab, CControl* control)
{
int x = m_x + 16;
int y = m_y + Items*24;
CLabel* label = new CLabel;
label->SetPosition(x, y);
label->SetText(Label);
Tab->RegisterControl(label);
x += m_iWidth / 2;
int cw, ch;
control->SetPosition(x, y);
control->GetSize(cw, ch);
control->SetSize((m_iWidth / 2) - 32, ch);
Tab->RegisterControl(control);
Items++;
}
void CGroupBox::OnUpdate(){}
void CGroupBox::OnClick(){}
#pragma endregion Implementations of the Group Box functions
#pragma region Sliders
CSlider::CSlider()
{
m_Flags = UIFlags::UI_Drawable | UIFlags::UI_Clickable | UIFlags::UI_SaveFile;
FileControlType = UIControlTypes::UIC_Slider;
}
void CSlider::Draw(bool hover)
{
POINT a = GetAbsolutePos();
Render::Clear(a.x, a.y + 5, m_iWidth, 2, Color(161, 161, 161, 255));
float Ratio = Value/(Max - Min);
float Location = Ratio*m_iWidth;
Render::Clear(a.x + Location, a.y+1, 4, 9, Color(255, 0, 0, 255));
Render::Outline(a.x + Location, a.y+1, 4, 9, Color(20, 20, 20, 80));
char buffer[24];
sprintf_s(buffer, "%.2f", Value);
RECT txtSize = Render::GetTextSize(Render::Fonts::MenuBold, buffer);
Render::Text(a.x + (m_iWidth / 2) - txtSize.right / 2, a.y + 10, Color(0, 0, 0, 255), Render::Fonts::MenuBold, buffer);
}
void CSlider::OnUpdate(){
POINT a = GetAbsolutePos();
m_iHeight = 11;
if (DoDrag)
{
if (GUI.GetKeyState(VK_LBUTTON))
{
POINT m = GUI.GetMouse();
float NewX;
float Ratio;
NewX = m.x - a.x - 1;
if (NewX < 0) NewX = 0;
if (NewX > m_iWidth) NewX = m_iWidth;
Ratio = NewX / float(m_iWidth);
Value = Min + (Max - Min)*Ratio;
}
else
{
DoDrag = false;
}
}
}
void CSlider::OnClick(){
POINT a = GetAbsolutePos();
RECT SliderRegion = { a.x, a.y, m_iWidth, 11 };
if (GUI.IsMouseInRegion(SliderRegion))
{
DoDrag = true;
}
}
float CSlider::GetValue()
{
return Value;
}
void CSlider::SetValue(float v)
{
Value = v;
}
void CSlider::SetBoundaries(float min, float max)
{
Min = min; Max = max;
}
#pragma endregion Implementations of the Slider functions
#pragma region KeyBinders
char* KeyStrings[254] = { nullptr, "Left Mouse", "Right Mouse", "Control+Break", "Middle Mouse", "Mouse 4", "Mouse 5",
nullptr, "Backspace", "TAB", nullptr, nullptr, nullptr, "ENTER", nullptr, nullptr, "SHIFT", "CTRL", "ALT", "PAUSE",
"CAPS LOCK", nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, "ESC", nullptr, nullptr, nullptr, nullptr, "SPACEBAR",
"PG UP", "PG DOWN", "END", "HOME", "Left", "Up", "Right", "Down", nullptr, "Print", nullptr, "Print Screen", "Insert",
"Delete", nullptr, "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
nullptr, "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
"Y", "Z", "Left Windows", "Right Windows", nullptr, nullptr, nullptr, "NUM 0", "NUM 1", "NUM 2", "NUM 3", "NUM 4", "NUM 5", "NUM 6",
"NUM 7", "NUM 8", "NUM 9", "*", "+", "_", "-", ".", "/", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12",
"F13", "F14", "F15", "F16", "F17", "F18", "F19", "F20", "F21", "F22", "F23", "F24", nullptr, nullptr, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, "NUM LOCK", "SCROLL LOCK", nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr, nullptr, "LSHIFT", "RSHIFT", "LCONTROL", "RCONTROL", "LMENU", "RMENU", nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, "Next Track", "Previous Track", "Stop", "Play/Pause", nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr, ";", "+", ",", "-", ".", "/?", "~", nullptr, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, "[{", "\\|", "}]", "'\"", nullptr, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
CKeyBind::CKeyBind()
{
m_Flags = UIFlags::UI_Drawable | UIFlags::UI_Clickable | UIFlags::UI_SaveFile;
FileControlType = UIControlTypes::UIC_KeyBind;
}
void CKeyBind::Draw(bool hover)
{
POINT a = GetAbsolutePos();
Render::Outline(a.x, a.y, m_iWidth, m_iHeight, Color(129, 129, 129, 255));
if (hover)
Render::Clear(a.x + 2, a.y + 2, m_iWidth - 4, m_iHeight - 4, Color(161, 161, 161, 255));
bool GoodKeyName = false;
char NameBuffer[128];
char* KeyName = "Not Bound";
if (IsGettingKey)
{
KeyName = "<Press A Key>";
}
else
{
if (Key >= 0)
{
KeyName = KeyStrings[Key];
if (KeyName)
{
GoodKeyName = true;
}
else
{
if (GetKeyNameText(Key << 16, NameBuffer, 127))
{
KeyName = NameBuffer;
GoodKeyName = true;
}
}
}
if (!GoodKeyName)
{
KeyName = "No Key Bound";
}
}
Render::Text(a.x + 2, a.y + 2, Color(0, 0, 0, 255), Render::Fonts::MenuBold, KeyName);
}
void CKeyBind::OnUpdate(){
m_iHeight = 16;
POINT a = GetAbsolutePos();
if (IsGettingKey)
{
for (int i = 0; i < 255; i++)
{
if (GUI.GetKeyPress(i))
{
if (i == VK_ESCAPE)
{
IsGettingKey = false;
Key = -1;
return;
}
Key = i;
IsGettingKey = false;
return;
}
}
}
}
void CKeyBind::OnClick(){
POINT a = GetAbsolutePos();
if (!IsGettingKey)
{
IsGettingKey = true;
}
}
int CKeyBind::GetKey()
{
return Key;
}
void CKeyBind::SetKey(int key)
{
Key = key;
}
#pragma endregion Implementations of the KeyBind Control functions
#pragma region Button
CButton::CButton()
{
m_iWidth = 177;
m_Flags = UIFlags::UI_Drawable | UIFlags::UI_Clickable;
Text = "Default";
CallBack = nullptr;
FileIdentifier = "Default";
}
void CButton::Draw(bool hover)
{
POINT a = GetAbsolutePos();
Render::Outline(a.x, a.y, m_iWidth, m_iHeight, Color(129, 129, 129, 255));
if (hover)
Render::GradientV(a.x + 2, a.y + 2, m_iWidth - 4, m_iHeight - 4, Color(192, 192, 192, 255), Color(234, 234, 234, 255));
else
Render::GradientV(a.x + 2, a.y + 2, m_iWidth - 4, m_iHeight - 4, Color(234, 234, 234, 255), Color(192, 192, 192, 255));
RECT TextSize = Render::GetTextSize(Render::Fonts::MenuBold, Text.c_str());
int TextX = a.x + (m_iWidth / 2) - (TextSize.left / 2);
int TextY = a.y + (m_iHeight / 2) - (TextSize.bottom / 2);
Render::Text(TextX, TextY, Color(0, 0, 0, 255), Render::Fonts::MenuBold, Text.c_str());
}
void CButton::SetText(std::string text)
{
Text = text;
}
void CButton::SetCallback(CButton::ButtonCallback_t callback)
{
CallBack = callback;
}
void CButton::OnUpdate()
{
m_iHeight = 26;
}
void CButton::OnClick()
{
if (CallBack)
CallBack();
}
#pragma endregion Implementations of the Button functions
#pragma region ComboBox
CComboBox::CComboBox()
{
m_Flags = UIFlags::UI_Drawable | UIFlags::UI_Clickable | UIFlags::UI_Focusable | UIFlags::UI_SaveFile;
FileControlType = UIControlTypes::UIC_ComboBox;
}
void CComboBox::Draw(bool hover)
{
POINT a = GetAbsolutePos();
RECT Region = { a.x, a.y, m_iWidth, 16 };
Render::Outline(a.x, a.y, m_iWidth, 16, Color(129, 129, 129, 255));
// Hover for the Top Box
if (GUI.IsMouseInRegion(Region))
Render::Clear(a.x + 2, a.y + 2, m_iWidth - 4, 12, Color(161, 161, 161, 255));
// If we have some items
if (Items.size() > 0)
{
// The current item
Render::Text(a.x + 2, a.y + 2, Color(0, 0, 0, 255), Render::Fonts::MenuBold, GetItem().c_str());
// If the drop down part is open
if (IsOpen)
{
Render::Clear(a.x, a.y + 17, m_iWidth, Items.size() * 16, Color(161, 161, 161, 255));
// Draw the items
for (int i = 0; i < Items.size(); i++)
{
RECT ItemRegion = { a.x, a.y + 17 + i * 16, m_iWidth, 16 };
// Hover
if (GUI.IsMouseInRegion(ItemRegion))
{
Render::Clear(a.x, a.y + 17 + i * 16, m_iWidth, 16, Color(207, 207, 207, 255));
}
Render::Text(a.x + 2, a.y + 19 + i * 16, Color(0, 0, 0, 255), Render::Fonts::MenuBold, Items[i].c_str());
}
}
}
}
void CComboBox::AddItem(std::string text)
{
Items.push_back(text);
SelectedIndex = 0;
}
void CComboBox::OnUpdate()
{
if (IsOpen)
{
m_iHeight = 16 + 16 * Items.size();
if (parent->GetFocus() != this)
IsOpen = false;
}
else
{
m_iHeight = 16;
}
}
void CComboBox::OnClick()
{
POINT a = GetAbsolutePos();
RECT Region = { a.x, a.y, m_iWidth, 16 };
if (IsOpen)
{
// If we clicked one of the items(Not in the top bar)
if (!GUI.IsMouseInRegion(Region))
{
// Draw the items
for (int i = 0; i < Items.size(); i++)
{
RECT ItemRegion = { a.x, a.y + 16 + i * 16, m_iWidth, 16 };
// Hover
if (GUI.IsMouseInRegion(ItemRegion))
{
SelectedIndex = i;
}
}
}
// Close the drop down
IsOpen = false;
}
else
{
IsOpen = true;
}
}
int CComboBox::GetIndex()
{
return SelectedIndex;
}
std::string CComboBox::GetItem()
{
if (SelectedIndex >= 0 && SelectedIndex < Items.size())
{
return Items[SelectedIndex];
}
return "Error";
}
void CComboBox::SelectIndex(int idx)
{
if (idx >= 0 && idx < Items.size())
{
SelectedIndex = idx;
}
}
#pragma endregion Implementations of the ComboBox functions