forked from alimb2/Ayyware-Full-Fixed-Updated-2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI.cpp
548 lines (474 loc) · 14.5 KB
/
GUI.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
539
540
541
542
543
544
545
546
547
548
/*
Syn's AyyWare Framework 2015
*/
#include "GUI.h"
#include "RenderManager.h"
#include "MetaInfo.h"
#include <algorithm>
#include "tinyxml2.h"
#include "Controls.h"
bool m_bIsOpen;
CGUI GUI;
CGUI::CGUI()
{
}
// Draws all windows
void CGUI::Draw()
{
bool ShouldDrawCursor = false;
for (auto window : Windows)
{
if (m_bIsOpen)
{
ShouldDrawCursor = true;
DrawWindow(window);
}
}
if (ShouldDrawCursor)
{
static Vertex_t MouseVt[3];
MouseVt[0].Init(Vector2D(Mouse.x, Mouse.y));
MouseVt[1].Init(Vector2D(Mouse.x + 16, Mouse.y));
MouseVt[2].Init(Vector2D(Mouse.x, Mouse.y + 16));
Render::PolygonOutline(3, MouseVt, Color(0, 0, 0, 0), Color(0, 0, 0, 0));
}
}
// Handle all input etc
void CGUI::Update()
{
//Key Array
std::copy(keys, keys + 255, oldKeys);
for (int x = 0; x < 255; x++)
{
//oldKeys[x] = oldKeys[x] & keys[x];
keys[x] = (GetAsyncKeyState(x));
}
// Mouse Location
POINT mp; GetCursorPos(&mp);
Mouse.x = mp.x; Mouse.y = mp.y;
RECT Screen = Render::GetViewport();
// Window Binds
for (auto& bind : WindowBinds)
{
if (GetKeyPress(bind.first))
{
bind.second->Toggle();
}
}
// Stop dragging
if (IsDraggingWindow && !GetKeyState(VK_LBUTTON))
{
IsDraggingWindow = false;
DraggingWindow = nullptr;
}
// If we are in the proccess of dragging a window
if (IsDraggingWindow && GetKeyState(VK_LBUTTON) && !GetKeyPress(VK_LBUTTON))
{
if (DraggingWindow)
{
DraggingWindow->m_x = Mouse.x - DragOffsetX;
DraggingWindow->m_y = Mouse.y - DragOffsetY;
}
}
// Process some windows
for (auto window : Windows)
{
if (m_bIsOpen)
{
// Used to tell the widget processing that there could be a click
bool bCheckWidgetClicks = false;
// If the user clicks inside the window
if (GetKeyPress(VK_LBUTTON))
{
if (IsMouseInRegion(window->m_x, window->m_y, window->m_x + window->m_iWidth, window->m_y + window->m_iHeight))
{
// Is it inside the client area?
if (IsMouseInRegion(window->GetClientArea()))
{
// User is selecting a new tab
if (IsMouseInRegion(window->GetTabArea()))
{
// Loose focus on the control
window->IsFocusingControl = false;
window->FocusedControl = nullptr;
int iTab = 0;
int TabCount = window->Tabs.size();
if (TabCount) // If there are some tabs
{
int TabSize = (window->m_iWidth - 4 - 12) / TabCount;
int Dist = Mouse.x - (window->m_x + 8);
while (Dist > TabSize)
{
if (Dist > TabSize)
{
iTab++;
Dist -= TabSize;
}
}
window->SelectedTab = window->Tabs[iTab];
}
}
else
bCheckWidgetClicks = true;
}
else
{
// Must be in the around the title or side of the window
// So we assume the user is trying to drag the window
IsDraggingWindow = true;
DraggingWindow = window;
DragOffsetX = Mouse.x - window->m_x;
DragOffsetY = Mouse.y - window->m_y;
// Loose focus on the control
window->IsFocusingControl = false;
window->FocusedControl = nullptr;
}
}
else
{
// Loose focus on the control
window->IsFocusingControl = false;
window->FocusedControl = nullptr;
}
}
// Controls
if (window->SelectedTab != nullptr)
{
// Focused widget
bool SkipWidget = false;
CControl* SkipMe = nullptr;
// this window is focusing on a widget??
if (window->IsFocusingControl)
{
if (window->FocusedControl != nullptr)
{
// We've processed it once, skip it later
SkipWidget = true;
SkipMe = window->FocusedControl;
POINT cAbs = window->FocusedControl->GetAbsolutePos();
RECT controlRect = { cAbs.x, cAbs.y, window->FocusedControl->m_iWidth, window->FocusedControl->m_iHeight };
window->FocusedControl->OnUpdate();
if (window->FocusedControl->Flag(UIFlags::UI_Clickable) && IsMouseInRegion(controlRect) && bCheckWidgetClicks)
{
window->FocusedControl->OnClick();
// If it gets clicked we loose focus
window->IsFocusingControl = false;
window->FocusedControl = nullptr;
bCheckWidgetClicks = false;
}
}
}
// Itterate over the rest of the control
for (auto control : window->SelectedTab->Controls)
{
if (control != nullptr)
{
if (SkipWidget && SkipMe == control)
continue;
POINT cAbs = control->GetAbsolutePos();
RECT controlRect = { cAbs.x, cAbs.y, control->m_iWidth, control->m_iHeight };
control->OnUpdate();
if (control->Flag(UIFlags::UI_Clickable) && IsMouseInRegion(controlRect) && bCheckWidgetClicks)
{
control->OnClick();
bCheckWidgetClicks = false;
// Change of focus
if (control->Flag(UIFlags::UI_Focusable))
{
window->IsFocusingControl = true;
window->FocusedControl = control;
}
else
{
window->IsFocusingControl = false;
window->FocusedControl = nullptr;
}
}
}
}
// We must have clicked whitespace
if (bCheckWidgetClicks)
{
// Loose focus on the control
window->IsFocusingControl = false;
window->FocusedControl = nullptr;
}
}
}
}
}
// Returns
bool CGUI::GetKeyPress(unsigned int key)
{
if (keys[key] == true && oldKeys[key] == false)
return true;
else
return false;
}
bool CGUI::GetKeyState(unsigned int key)
{
return keys[key];
}
bool CGUI::IsMouseInRegion(int x, int y, int x2, int y2)
{
if (Mouse.x > x && Mouse.y > y && Mouse.x < x2 && Mouse.y < y2)
return true;
else
return false;
}
bool CGUI::IsMouseInRegion(RECT region)
{
return IsMouseInRegion(region.left, region.top, region.left + region.right, region.top + region.bottom);
}
POINT CGUI::GetMouse()
{
return Mouse;
}
bool CGUI::DrawWindow(CWindow* window)
{
Render::Outline(window->m_x, window->m_y, window->m_iWidth, window->m_iHeight, Color(21, 21, 21, 80));
Render::GradientV(window->m_x + 2, window->m_y + 2, window->m_iWidth - 4, 26, Color(45, 40, 40, 255), Color(45, 45, 40, 255));
Render::Clear(window->m_x + 2, window->m_y + 2 + 26, window->m_iWidth - 4, window->m_iHeight - 4 - 26, Color(49, 42, 42, 255));
Render::Outline(window->m_x + 1, window->m_y + 1, window->m_iWidth - 2, window->m_iHeight - 2, Color(49, 42, 42, 255));
// Main Window
/*Render::Outline(window->m_x, window->m_y, window->m_iWidth, window->m_iHeight, Color(21, 21, 21, 80));
Render::GradientV(window->m_x + 2, window->m_y + 2, window->m_iWidth - 4, 26, Color(2, 78, 0, 255), Color(7, 255, 37, 255));
Render::Clear(window->m_x + 2, window->m_y + 2 + 26, window->m_iWidth - 4, window->m_iHeight - 4 - 26, Color(7, 255, 37, 255));
Render::Outline(window->m_x + 1, window->m_y + 1, window->m_iWidth - 2, window->m_iHeight - 2, Color(91, 242, 0, 255));*/
Render::Text(window->m_x + 8, window->m_y + 8, Color(255, 0, 0, 255), Render::Fonts::MenuBold, window->Title.c_str());
//Inner
//Render::Outline(window->m_x + 7, window->m_y + 1 + 26, window->m_iWidth - 4 - 10, window->m_iHeight - 2 - 6 - 26, Color(255, 255, 255, 80));
Render::Clear(window->m_x + 8, window->m_y + 1 + 27, window->m_iWidth - 4 - 12, window->m_iHeight - 2 - 8 - 26, Color(30, 30, 30, 255));
//Tab
Render::GradientV(window->m_x + 8, window->m_y + 1 + 27, window->m_iWidth - 4 - 12, 29, Color(49, 42, 42, 255), Color(49, 42, 42, 255));
int TabCount = window->Tabs.size();
if (TabCount) // If there are some tabs
{
int TabSize = (window->m_iWidth - 4 - 12) / TabCount;
for (int i = 0; i < TabCount; i++)
{
RECT TabArea = { window->m_x + 8 + (i*TabSize), window->m_y + 1 + 27, TabSize, 29 };
CTab *tab = window->Tabs[i];
if (window->SelectedTab == tab)
{
Render::GradientV(window->m_x + 8 + (i*TabSize), window->m_y + 1 + 27, TabSize, 29, Color(106, 106, 106, 255), Color(49, 42, 42, 255));
}
else if (IsMouseInRegion(TabArea))
{
Render::GradientV(window->m_x + 8 + (i*TabSize), window->m_y + 1 + 27, TabSize, 29, Color(106, 106, 106, 255), Color(80, 80, 80, 255));
}
RECT TextSize = Render::GetTextSize(Render::Fonts::MenuBold, tab->Title.c_str());
Render::Text(TabArea.left + (TabSize / 2) - (TextSize.right / 2), TabArea.top + 8, Color(255, 0, 0, 255), Render::Fonts::MenuBold, tab->Title.c_str());
Render::Clear(window->m_x + 8, window->m_y + 1 + 27, window->m_iWidth - 4 - 12, 2, Color(62, 55, 55, 255));
}
}
// Controls
if (window->SelectedTab != nullptr)
{
// Focused widget
bool SkipWidget = false;
CControl* SkipMe = nullptr;
// this window is focusing on a widget??
if (window->IsFocusingControl)
{
if (window->FocusedControl != nullptr)
{
// We need to draw it last, so skip it in the regular loop
SkipWidget = true;
SkipMe = window->FocusedControl;
}
}
// Itterate over all the other controls
for (auto control : window->SelectedTab->Controls)
{
if (SkipWidget && SkipMe == control)
continue;
if (control != nullptr && control->Flag(UIFlags::UI_Drawable))
{
POINT cAbs = control->GetAbsolutePos();
RECT controlRect = { cAbs.x, cAbs.y, control->m_iWidth, control->m_iHeight };
bool hover = false;
if (IsMouseInRegion(controlRect))
{
hover = true;
}
control->Draw(hover);
}
}
// Draw the skipped widget last
if (SkipWidget)
{
auto control = window->FocusedControl;
if (control != nullptr && control->Flag(UIFlags::UI_Drawable))
{
POINT cAbs = control->GetAbsolutePos();
RECT controlRect = { cAbs.x, cAbs.y, control->m_iWidth, control->m_iHeight };
bool hover = false;
if (IsMouseInRegion(controlRect))
{
hover = true;
}
control->Draw(hover);
}
}
}
return true;
}
void CGUI::RegisterWindow(CWindow* window)
{
Windows.push_back(window);
// Resorting to put groupboxes at the start
for (auto tab : window->Tabs)
{
for (auto control : tab->Controls)
{
if (control->Flag(UIFlags::UI_RenderFirst))
{
CControl * c = control;
tab->Controls.erase(std::remove(tab->Controls.begin(), tab->Controls.end(), control), tab->Controls.end());
tab->Controls.insert(tab->Controls.begin(), control);
}
}
}
}
void CGUI::BindWindow(unsigned char Key, CWindow* window)
{
if (window)
WindowBinds[Key] = window;
else
WindowBinds.erase(Key);
}
void CGUI::SaveWindowState(CWindow* window, std::string Filename)
{
// Create a whole new document and we'll just save over top of the old one
tinyxml2::XMLDocument Doc;
// Root Element is called "ayy"
tinyxml2::XMLElement *Root = Doc.NewElement("ayy");
Doc.LinkEndChild(Root);
Utilities::Log("Saving Window %s", window->Title.c_str());
// If the window has some tabs..
if (Root && window->Tabs.size() > 0)
{
for (auto Tab : window->Tabs)
{
// Add a new section for this tab
tinyxml2::XMLElement *TabElement = Doc.NewElement(Tab->Title.c_str());
Root->LinkEndChild(TabElement);
Utilities::Log("Saving Tab %s", Tab->Title.c_str());
// Now we itterate the controls this tab contains
if (TabElement && Tab->Controls.size() > 0)
{
for (auto Control : Tab->Controls)
{
// If the control is ok to be saved
if (Control && Control->Flag(UIFlags::UI_SaveFile) && Control->FileIdentifier.length() > 1 && Control->FileControlType)
{
// Create an element for the control
tinyxml2::XMLElement *ControlElement = Doc.NewElement(Control->FileIdentifier.c_str());
TabElement->LinkEndChild(ControlElement);
Utilities::Log("Saving control %s", Control->FileIdentifier.c_str());
if (!ControlElement)
{
Utilities::Log("Errorino :("); // s0 cute
return;
}
CCheckBox* cbx = nullptr;
CComboBox* cbo = nullptr;
CKeyBind* key = nullptr;
CSlider* sld = nullptr;
// Figure out what kind of control and data this is
switch (Control->FileControlType)
{
case UIControlTypes::UIC_CheckBox:
cbx = (CCheckBox*)Control;
ControlElement->SetText(cbx->GetState());
break;
case UIControlTypes::UIC_ComboBox:
cbo = (CComboBox*)Control;
ControlElement->SetText(cbo->GetIndex());
break;
case UIControlTypes::UIC_KeyBind:
key = (CKeyBind*)Control;
ControlElement->SetText(key->GetKey());
break;
case UIControlTypes::UIC_Slider:
sld = (CSlider*)Control;
ControlElement->SetText(sld->GetValue());
break;
}
}
}
}
}
}
//Save the file
if (Doc.SaveFile(Filename.c_str()) != tinyxml2::XML_NO_ERROR)
{
MessageBox(NULL, "Failed To Save Config File!", "AyyWare", MB_OK);
}
}
void CGUI::LoadWindowState(CWindow* window, std::string Filename)
{
// Lets load our meme
tinyxml2::XMLDocument Doc;
if (Doc.LoadFile(Filename.c_str()) == tinyxml2::XML_NO_ERROR)
{
tinyxml2::XMLElement *Root = Doc.RootElement();
// The root "ayy" element
if (Root)
{
// If the window has some tabs..
if (Root && window->Tabs.size() > 0)
{
for (auto Tab : window->Tabs)
{
// We find the corresponding element for this tab
tinyxml2::XMLElement *TabElement = Root->FirstChildElement(Tab->Title.c_str());
if (TabElement)
{
// Now we itterate the controls this tab contains
if (TabElement && Tab->Controls.size() > 0)
{
for (auto Control : Tab->Controls)
{
// If the control is ok to be saved
if (Control && Control->Flag(UIFlags::UI_SaveFile) && Control->FileIdentifier.length() > 1 && Control->FileControlType)
{
// Get the controls element
tinyxml2::XMLElement *ControlElement = TabElement->FirstChildElement(Control->FileIdentifier.c_str());
if (ControlElement)
{
CCheckBox* cbx = nullptr;
CComboBox* cbo = nullptr;
CKeyBind* key = nullptr;
CSlider* sld = nullptr;
// Figure out what kind of control and data this is
switch (Control->FileControlType)
{
case UIControlTypes::UIC_CheckBox:
cbx = (CCheckBox*)Control;
cbx->SetState(ControlElement->GetText()[0] == '1' ? true : false);
break;
case UIControlTypes::UIC_ComboBox:
cbo = (CComboBox*)Control;
cbo->SelectIndex(atoi(ControlElement->GetText()));
break;
case UIControlTypes::UIC_KeyBind:
key = (CKeyBind*)Control;
key->SetKey(atoi(ControlElement->GetText()));
break;
case UIControlTypes::UIC_Slider:
sld = (CSlider*)Control;
sld->SetValue(atof(ControlElement->GetText()));
break;
}
}
}
}
}
}
}
}
}
}
}