-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMenu.hpp
More file actions
1372 lines (1109 loc) · 29.7 KB
/
Menu.hpp
File metadata and controls
1372 lines (1109 loc) · 29.7 KB
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include "pch.h"
class ElementBase
{
public:
enum class EElementType : uint8_t
{
None,
Button,
Checkbox,
Child,
ColorPicker,
Combo,
Hotkey,
InputText,
Menu,
RadioButtonIcon,
HeaderGroup,
Body,
BodyGroup,
SliderFloat,
SliderInt,
SeperatorText,
Spacing,
Text,
};
enum class ESameLine : uint8_t
{
New, // Force element to be on a new line
Same, // Force element to be on the same line
Dynamic, // Potentially buggy, only use if you know what you are doing
};
typedef struct Style_t
{
bool bVisible : 1 = true;
bool bChildrenVisible : 1 = true;
ESameLine eSameLine : 2 = ESameLine::Dynamic;
float flOffset = 0.f;
float flSpacing = -1.f;
ImVec2 vec2Size = { 100.f, 0.f };
ImDrawFlags iFlags = 0;
} Style_t;
Style_t m_stStyle;
enum EPage : uint8_t // Cannot be an enum class without a shit ton of changes
{
Developer,
Style,
Settings,
Config
};
typedef struct Header_t
{
uint8_t m_iParentPageID;
std::vector<size_t> m_ullLocalizedNameHashes;
} Header_t;
protected:
// used to display a custom or unlocalized name for elements like buttons
bool m_bUnlocalizedName = false;
std::string m_sUnlocalizedName = "";
std::size_t m_ullLocalizedNameHash = 0;
// used for internal reference for removing elements or config handling
std::string m_sUnique = "INVALID_UNIQUE";
ElementBase* m_pParent = nullptr;
std::vector<ElementBase*> m_Children;
inline static uint8_t eCurrentPage = ElementBase::EPage::Developer;
inline static uint8_t eCurrentSubPage = 0;
inline void SameLine()
{
switch (m_stStyle.eSameLine)
{
// Force draw on the same line
case(ESameLine::Same):
ImGui::SameLine(m_stStyle.flOffset, m_stStyle.flSpacing);
break;
// Ask our parent if we should draw on the same line or not
case(ESameLine::Dynamic):
if (m_pParent != nullptr)
m_pParent->RequestSameLine(this);
break;
// Draw on a new line
case(ESameLine::New):
default:
break;
}
};
void ConfigSaveChildren(nlohmann::json& jsonParent) const
{
for (ElementBase* const pElement : m_Children)
pElement->ConfigSave(jsonParent);
};
void ConfigLoadChildren(nlohmann::json& jsonParent)
{
for (ElementBase* const pElement : m_Children)
pElement->ConfigLoad(jsonParent);
};
void RenderChildren()
{
if (!m_stStyle.bChildrenVisible)
return;
for (ElementBase* const pElement : m_Children)
pElement->Render();
};
public:
ElementBase() = default;
void AddElement(void* pElement)
{
static_cast<ElementBase*>(pElement)->m_pParent = this;
m_Children.push_back(static_cast<ElementBase*>(pElement));
};
void RemoveElement(std::string sUnique)
{
for (auto it = m_Children.begin(); it != m_Children.end(); ++it)
{
if ((*it)->m_sUnique != sUnique)
continue;
m_Children.erase(it);
break;
}
};
void LeaveParent()
{
if (!m_pParent)
return;
for (auto it = m_pParent->m_Children.begin(); it != m_pParent->m_Children.end(); ++it)
{
if (*it != this)
continue;
m_pParent->m_Children.erase(it);
break;
}
m_pParent = nullptr;
};
// Gets internal element name
inline const std::string GetUnique() const
{
return m_sUnique;
};
// Gets localized element name
inline const std::string GetName() const
{
if (m_bUnlocalizedName)
return m_sUnlocalizedName;
return Localization::Get(m_ullLocalizedNameHash);
};
// Sets that we want an unlocalized name and replaces the internal unlocalized name string
inline void SetName(std::string s)
{
m_bUnlocalizedName = true;
m_sUnlocalizedName = s;
};
// Sets that we want to use a localized name and overrides the used localized name
inline void SetName(size_t ullHash)
{
m_bUnlocalizedName = false;
m_ullLocalizedNameHash = ullHash;
};
// Sets if we want to use the localized name or not
inline void SetName(bool bUseUnlocalized = false)
{
m_bUnlocalizedName = bUseUnlocalized;
};
inline ElementBase* GetParent() const
{
return m_pParent;
};
inline const bool HasParent() const
{
return m_pParent != nullptr;
};
inline ElementBase* GetChild(const std::string& sUnique) const
{
for (auto& pChild : m_Children) {
if (!pChild)
continue;
if (pChild->GetUnique().compare(sUnique) == 0) {
return pChild;
}
}
return nullptr;
};
inline const bool HasChildren() const
{
return m_Children.size() > 0;
};
inline void SetVisible(bool vis)
{
m_stStyle.bVisible = vis;
};
inline bool IsVisible() const
{
return m_stStyle.bVisible;
};
inline void SetChildrenVisible(bool vis)
{
m_stStyle.bChildrenVisible = vis;
};
inline bool IsChildrenVisible() const
{
return m_stStyle.bChildrenVisible;
};
inline Style_t GetStyle() const
{
return m_stStyle;
};
inline void SetStyle(Style_t stStyle)
{
m_stStyle = stStyle;
};
virtual constexpr EElementType GetType() const
{
return EElementType::None;
};
// Will determine if elements should be placed on the same line or not
virtual void RequestSameLine(ElementBase* pChild)
{};
virtual void ConfigSave(nlohmann::json& jsonParent) const
{
if (!HasChildren())
return;
nlohmann::json& jsonEntry = jsonParent[m_sUnique.c_str()] = nlohmann::json();
ConfigSaveChildren(jsonEntry);
};
virtual void ConfigLoad(nlohmann::json& jsonParent)
{
if (!HasChildren() || !jsonParent.contains(m_sUnique.c_str()))
return;
ConfigLoadChildren(jsonParent[m_sUnique.c_str()]);
};
virtual void Render()
{
if (!m_stStyle.bVisible)
return;
RenderChildren();
};
};
template<typename T>
class ElementInput : public ElementBase
{
protected:
bool m_bOverride = false;
T m_ValueOverride{};
T m_Value{};
inline std::string ConvertToString() const
{
return std::to_string(m_Value);
};
inline T ConvertFromString(const std::string& str) const
{
return std::stoi(str);
};
public:
void ConfigSave(nlohmann::json& jsonParent) const override
{
nlohmann::json& jsonEntry = jsonParent[m_sUnique.c_str()] = nlohmann::json();
jsonEntry["Value"] = ConvertToString();
if (!HasChildren())
return;
jsonEntry["Children"] = nlohmann::json();
ConfigSaveChildren(jsonEntry["Children"]);
};
void ConfigLoad(nlohmann::json& jsonParent) override
{
if (!jsonParent.contains(m_sUnique.c_str()))
return;
nlohmann::json& jsonEntry = jsonParent[m_sUnique.c_str()];
if (jsonEntry.contains("Value"))
m_Value = ConvertFromString(jsonEntry["Value"].get<std::string>());
if (jsonEntry.contains("Children"))
ConfigLoadChildren(jsonEntry["Children"]);
};
inline T GetValue() const
{
return m_bOverride ? m_ValueOverride : m_Value;
};
inline void SetValue(const T& Value)
{
m_Value = Value;
};
inline T GetOverride() const
{
return m_ValueOverride;
};
inline const bool GetOverrideActive() const
{
return m_bOverride;
};
inline void SetOverride(const T& Value) const
{
m_bOverride = true;
m_ValueOverride = Value;
};
inline void SetOverride() const
{
m_bOverride = false;
};
};
template<>
inline std::string ElementInput<ImVec4>::ConvertToString() const
{
return std::to_string(ImGui::ColorConvertFloat4ToU32(m_Value));
};
template<>
inline std::string ElementInput<std::string>::ConvertToString() const
{
return m_Value;
};
template<>
inline ImVec4 ElementInput<ImVec4>::ConvertFromString(const std::string& str) const
{
return ImGui::ColorConvertU32ToFloat4(std::stoul(str));
};
template<>
inline std::string ElementInput<std::string>::ConvertFromString(const std::string& str) const
{
return str;
};
template<>
inline float ElementInput<float>::ConvertFromString(const std::string& str) const
{
return std::stof(str);
};
class Spacing : public ElementBase
{
public:
Spacing(std::string sUnique, size_t ullLocalizedNameHash, Style_t stStyle = {})
{
m_sUnique = sUnique;
m_ullLocalizedNameHash = ullLocalizedNameHash;
m_stStyle = stStyle;
};
Spacing(std::string sUnique, std::string sUnlocalizedName, Style_t stStyle = {})
{
m_sUnique = sUnique;
m_bUnlocalizedName = true;
m_sUnlocalizedName = sUnlocalizedName;
m_stStyle = stStyle;
};
constexpr EElementType GetType() const override
{
return EElementType::Spacing;
};
void Render() override
{
ImGui::Spacing();
}
};
class Menu : public ElementBase
{
protected:
uint8_t m_ucSameLinedElements = 1;
EElementType m_eLastSameLinedElement = EElementType::None;
ImVec2 m_vec2MinSize = { 0.f, 0.f };
ImVec2 m_vec2MaxSize = { 9999.f, 9999.f };
public:
Menu(std::string sUnique, size_t ullLocalizedNameHash, Style_t stStyle = {})
{
m_sUnique = sUnique;
m_ullLocalizedNameHash = ullLocalizedNameHash;
m_stStyle = stStyle;
m_vec2MinSize = m_stStyle.vec2Size * 0.75f;
m_vec2MaxSize = m_stStyle.vec2Size * 2.f;
};
Menu(std::string sUnique, std::string sUnlocalizedName, Style_t stStyle = {})
{
m_sUnique = sUnique;
m_bUnlocalizedName = true;
m_sUnlocalizedName = sUnlocalizedName;
m_stStyle = stStyle;
m_vec2MinSize = m_stStyle.vec2Size * 0.75f;
m_vec2MaxSize = m_stStyle.vec2Size * 2.f;
};
constexpr EElementType GetType() const override
{
return EElementType::Menu;
};
// Will determine if elements should be placed on the same line or not
void RequestSameLine(ElementBase* pChild) override
{
if (!pChild)
return;
EElementType eChildType = pChild->GetType();
// Using the same line for multiple different element types should be done manually
if (eChildType != m_eLastSameLinedElement)
{
m_ucSameLinedElements = 1;
m_eLastSameLinedElement = eChildType;
return;
}
switch (eChildType)
{
case(EElementType::Button):
case(EElementType::Child):
{
if (m_ucSameLinedElements >= 3)
{
m_ucSameLinedElements = 1;
break;
}
++m_ucSameLinedElements;
ImGui::SameLine(0.f, pChild->m_stStyle.flSpacing);
break;
}
default:
break;
}
};
void Render() override
{
m_ucSameLinedElements = 1;
m_eLastSameLinedElement = EElementType::None;
ImGuiStyle& style = ImGui::GetStyle();
static float SideBarWidth = 160;
float FooterHeight = ImGui::GetFrameHeight();
float HeaderHeight = ImGui::GetFrameHeight() + style.WindowPadding.y * 2;
ImGui::SetNextWindowSize(m_stStyle.vec2Size, ImGuiCond_Once);
ImGui::SetNextWindowPos(ImGui::GetIO().DisplaySize / 2 - m_stStyle.vec2Size / 2, ImGuiCond_Once);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
ImGui::Begin(GetName().c_str(), nullptr, ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoBackground);
ImGui::PopStyleVar(2);
// Renders
{
ImVec2 pos = ImGui::GetWindowPos();
ImVec2 size = ImGui::GetWindowSize();
ImDrawList* drawList = ImGui::GetWindowDrawList();
drawList->AddRectFilled(pos, pos + ImVec2(SideBarWidth, size.y), ImGui::GetColorU32(ImGuiCol_ChildBg), style.WindowRounding, ImDrawFlags_RoundCornersLeft);
drawList->AddRectFilled(pos + ImVec2(SideBarWidth, 0), pos + ImVec2(size.x, HeaderHeight), ImGui::GetColorU32(ImGuiCol_ChildBg), style.WindowRounding, ImDrawFlags_RoundCornersTopRight);
drawList->AddRectFilled(pos + ImVec2(SideBarWidth, HeaderHeight), pos + ImVec2(size.x, size.y - FooterHeight), ImGui::GetColorU32(ImGuiCol_WindowBg), style.WindowRounding, ImDrawFlags_RoundCornersNone);
drawList->AddRectFilled(pos + ImVec2(SideBarWidth, size.y - FooterHeight), pos + size, ImGui::GetColorU32(ImGuiCol_ChildBg), style.WindowRounding, ImDrawFlags_RoundCornersBottomRight);
if (style.WindowBorderSize > 0) {
drawList->AddLine(pos + ImVec2(SideBarWidth - style.WindowBorderSize, style.WindowBorderSize), pos + ImVec2(SideBarWidth - style.WindowBorderSize, size.y - style.WindowBorderSize), ImGui::GetColorU32(ImGuiCol_Border), style.WindowBorderSize);
drawList->AddLine(pos + ImVec2(SideBarWidth, HeaderHeight - style.WindowBorderSize), pos + ImVec2(size.x - style.WindowBorderSize, HeaderHeight - style.WindowBorderSize), ImGui::GetColorU32(ImGuiCol_Border), style.WindowBorderSize);
drawList->AddLine(pos + ImVec2(SideBarWidth, size.y - FooterHeight + style.WindowBorderSize), pos + ImVec2(size.x - style.WindowBorderSize, size.y - FooterHeight + style.WindowBorderSize), ImGui::GetColorU32(ImGuiCol_Border), style.WindowBorderSize);
drawList->AddRect(pos, pos + size, ImGui::GetColorU32(ImGuiCol_Border), style.WindowRounding);
}
drawList->AddText(pos + ImVec2(SideBarWidth + style.FramePadding.x, size.y - FooterHeight + style.FramePadding.y), ImGui::GetColorU32(ImGuiCol_TextDisabled), "OmegaWare.xyz");
drawList->AddText(pos + ImVec2(size.x - ImGui::CalcTextSize((std::string("v") + STR(FRAMEWORK_VERSION)).c_str()).x - style.FramePadding.x, size.y - FooterHeight + style.FramePadding.y), ImGui::GetColorU32(ImGuiCol_SliderGrab), (std::string("v") + STR(FRAMEWORK_VERSION)).c_str());
}
RenderChildren();
ImGui::End();
}
};
class Child : public ElementBase
{
protected:
ImGuiWindowFlags m_WindowFlags;
std::function<ImVec2()> m_Callback = nullptr;
std::function<void()> m_PushVarsCallback = nullptr;
std::function<void()> m_PopVarsCallback = nullptr;
public:
Child(std::string sUnique, size_t ullLocalizedNameHash, Style_t stStyle = {}, ImGuiWindowFlags WindowFlags = 0)
{
m_sUnique = sUnique;
m_ullLocalizedNameHash = ullLocalizedNameHash;
m_stStyle = stStyle;
m_WindowFlags = WindowFlags;
};
Child(std::string sUnique, std::string sUnlocalizedName, Style_t stStyle = {}, ImGuiWindowFlags WindowFlags = 0)
{
m_sUnique = sUnique;
m_bUnlocalizedName = true;
m_sUnlocalizedName = sUnlocalizedName;
m_stStyle = stStyle;
m_WindowFlags = WindowFlags;
};
constexpr EElementType GetType() const override
{
return EElementType::Child;
};
void Render() override
{
if (!m_stStyle.bVisible)
return;
SameLine();
if (m_Callback)
m_stStyle.vec2Size = m_Callback();
ImGui::BeginChild(GetName().c_str(), m_stStyle.vec2Size, m_stStyle.iFlags, m_WindowFlags);
if (m_PushVarsCallback)
m_PushVarsCallback();
RenderChildren();
if (m_PopVarsCallback)
m_PopVarsCallback();
ImGui::EndChild();
};
void SetCallback(std::function<ImVec2()> Callback)
{
m_Callback = Callback;
};
void SetPushVarsCallback(std::function<void()> Callback)
{
m_PushVarsCallback = Callback;
};
void SetPopVarsCallback(std::function<void()> Callback)
{
m_PopVarsCallback = Callback;
};
};
class Text : public ElementBase
{
protected:
public:
Text(std::string sUnique, size_t ullLocalizedNameHash, Style_t stStyle = {})
{
m_sUnique = sUnique;
m_ullLocalizedNameHash = ullLocalizedNameHash;
m_stStyle = stStyle;
};
Text(std::string sUnique, std::string sUnlocalizedName, Style_t stStyle = {})
{
m_sUnique = sUnique;
m_bUnlocalizedName = true;
m_sUnlocalizedName = sUnlocalizedName;
m_stStyle = stStyle;
};
constexpr EElementType GetType() const override
{
return EElementType::Text;
};
void Render() override
{
if (!m_stStyle.bVisible)
return;
SameLine();
ImGui::Text(GetName().c_str());
};
};
class Button : public ElementBase
{
protected:
std::function<void()> m_Callback = nullptr;
public:
Button(std::string sUnique, size_t ullLocalizedNameHash, Style_t stStyle = {})
{
m_sUnique = sUnique;
m_ullLocalizedNameHash = ullLocalizedNameHash;
m_stStyle = stStyle;
};
Button(std::string sUnique, std::string sUnlocalizedName, Style_t stStyle = {})
{
m_sUnique = sUnique;
m_bUnlocalizedName = true;
m_sUnlocalizedName = sUnlocalizedName;
m_stStyle = stStyle;
};
constexpr EElementType GetType() const override
{
return EElementType::Button;
};
void Render() override
{
if (!m_stStyle.bVisible)
return;
SameLine();
if (ImGui::Button(GetName().c_str()))
if (m_Callback)
m_Callback();
RenderChildren();
};
void SetCallback(std::function<void()> Callback)
{
m_Callback = Callback;
};
};
class Combo : public ElementBase
{
protected:
std::string m_sPreviewlabel = "PreviewNotSet";
std::function<void()> m_Callback;
public:
Combo(std::string sUnique, size_t ullLocalizedNameHash, Style_t stStyle = {})
{
m_sUnique = sUnique;
m_ullLocalizedNameHash = ullLocalizedNameHash;
m_stStyle = stStyle;
};
Combo(std::string sUnique, std::string sUnlocalizedName, Style_t stStyle = {})
{
m_sUnique = sUnique;
m_bUnlocalizedName = true;
m_sUnlocalizedName = sUnlocalizedName;
m_stStyle = stStyle;
};
constexpr EElementType GetType() const override
{
return EElementType::Combo;
};
void Render() override
{
if (!m_stStyle.bVisible)
return;
SameLine();
if (ImGui::BeginCombo(GetName().c_str(), m_sPreviewlabel.c_str(), m_stStyle.iFlags))
{
if (m_Callback)
m_Callback();
ImGui::EndCombo();
}
RenderChildren();
};
void SetCallback(std::function<void()> Callback)
{
m_Callback = Callback;
};
void SetPreviewLabel(std::string s)
{
m_sPreviewlabel = s;
};
};
class Checkbox : public ElementInput<bool>
{
protected:
public:
Checkbox(std::string sUnique, size_t ullLocalizedNameHash, Style_t stStyle = {})
{
m_sUnique = sUnique;
m_ullLocalizedNameHash = ullLocalizedNameHash;
m_stStyle = stStyle;
};
Checkbox(std::string sUnique, std::string sUnlocalizedName, Style_t stStyle = {})
{
m_sUnique = sUnique;
m_bUnlocalizedName = true;
m_sUnlocalizedName = sUnlocalizedName;
m_stStyle = stStyle;
};
constexpr EElementType GetType() const override
{
return EElementType::Checkbox;
};
void Render() override
{
if (!m_stStyle.bVisible)
return;
SameLine();
ImGui::Checkbox(GetName().c_str(), &m_Value);
RenderChildren();
};
};
class Hotkey : public ElementBase
{
public:
enum class EHotkeyMode : uint8_t
{
AlwaysOn,
Hold,
Toggle,
HoldOff,
};
protected:
ImGuiKey m_eKey = ImGuiKey_None;
EHotkeyMode m_eMode = EHotkeyMode::Hold;
bool m_bSetting = false;
bool m_bActive = false;
bool SetKey() noexcept
{
for (int i = ImGuiKey_NamedKey_BEGIN; i < ImGuiKey_NamedKey_END; ++i) {
ImGuiKey _key = static_cast<ImGuiKey>(i);
if (!ImGui::IsKeyPressed(_key))
continue;
m_eKey = _key;
return true;
}
return false;
}
public:
Hotkey(std::string sUnique, size_t ullLocalizedNameHash, Style_t stStyle = {})
{
m_sUnique = sUnique;
m_ullLocalizedNameHash = ullLocalizedNameHash;
m_stStyle = stStyle;
};
Hotkey(std::string sUnique, std::string sUnlocalizedName, Style_t stStyle = {})
{
m_sUnique = sUnique;
m_bUnlocalizedName = true;
m_sUnlocalizedName = sUnlocalizedName;
m_stStyle = stStyle;
};
constexpr EElementType GetType() const override
{
return EElementType::Hotkey;
};
void Render() override
{
if (!m_stStyle.bVisible)
return;
SameLine();
if (ImGui::BeginCombo(("##CMB" + GetName()).c_str(), "##", ImGuiComboFlags_NoPreview))
{
bool bSelected;
bSelected = m_eMode == EHotkeyMode::AlwaysOn;
if (ImGui::Selectable("Always On", bSelected))
m_eMode = EHotkeyMode::AlwaysOn;
if (bSelected)
ImGui::SetItemDefaultFocus();
bSelected = m_eMode == EHotkeyMode::Hold;
if (ImGui::Selectable("Hold", bSelected))
m_eMode = EHotkeyMode::Hold;
if (bSelected)
ImGui::SetItemDefaultFocus();
bSelected = m_eMode == EHotkeyMode::Toggle;
if (ImGui::Selectable("Toggle", bSelected))
m_eMode = EHotkeyMode::Toggle;
if (bSelected)
ImGui::SetItemDefaultFocus();
bSelected = m_eMode == EHotkeyMode::HoldOff;
if (ImGui::Selectable("Hold Off", bSelected))
m_eMode = EHotkeyMode::HoldOff;
if (bSelected)
ImGui::SetItemDefaultFocus();
ImGui::EndCombo();
}
ImGui::SameLine(0.f, 2.f);
const char* szLabel = GetName().c_str();
const auto id = ImGui::GetID(szLabel);
ImGui::PushID(szLabel);
std::string BtnName = (m_bSetting) ? "..." : ImGui::GetKeyName(m_eKey);
if (ImGui::GetActiveID() == id) {
ImGui::PushStyleColor(ImGuiCol_Button, ImGui::GetColorU32(ImGuiCol_ButtonActive));
ImGui::Button("...", m_stStyle.vec2Size);
ImGui::PopStyleColor();
ImGui::GetCurrentContext()->ActiveIdAllowOverlap = true;
if (!ImGui::IsItemHovered() && !ImGui::IsItemFocused() && SetKey())
{
ImGui::ClearActiveID();
m_bSetting = false;
}
}
else if (ImGui::Button(BtnName.c_str(), m_stStyle.vec2Size) || m_bSetting) {
ImGui::SetActiveID(id, ImGui::GetCurrentWindow());
m_bSetting = true;
}
ImGui::SameLine(0.f, 4.f);
ImGui::Text("%s", GetName().c_str());
ImGui::PopID();
RenderChildren();
};
void ConfigSave(nlohmann::json& jsonParent) const override
{
nlohmann::json& jsonEntry = jsonParent[m_sUnique.c_str()] = nlohmann::json();
unsigned int v = 0;
v |= static_cast<unsigned int>(m_eKey) << 2;
v |= static_cast<unsigned int>(m_eMode);
jsonEntry["Value"] = std::to_string(v);
if (!HasChildren())
return;
jsonEntry["Children"] = nlohmann::json();
ConfigSaveChildren(jsonEntry["Children"]);
};
void ConfigLoad(nlohmann::json& jsonParent) override
{
if (!jsonParent.contains(m_sUnique.c_str()))
return;
nlohmann::json& jsonEntry = jsonParent[m_sUnique.c_str()];
if (jsonEntry.contains("Value"))
{
unsigned int v = std::stoi(jsonEntry["Value"].get<std::string>());
m_eMode = static_cast<EHotkeyMode>(v & 0b11);
m_eKey = static_cast<ImGuiKey>(v >> 2);
}
if (jsonEntry.contains("Children"))
ConfigLoadChildren(jsonEntry["Children"]);
};
void Update()
{
if (m_bSetting && m_eMode != EHotkeyMode::AlwaysOn)
{
m_bActive = false;
return;
}
switch (m_eMode)
{
case (EHotkeyMode::AlwaysOn):
{
m_bActive = true;
return;
}
case (EHotkeyMode::Hold):
{
m_bActive = ImGui::IsKeyDown(m_eKey);
return;
}
case (EHotkeyMode::HoldOff):
{
m_bActive = !ImGui::IsKeyDown(m_eKey);
return;
}
default:
{
if (ImGui::IsKeyPressed(m_eKey, false))
m_bActive = !m_bActive;
return;
}
};
}
inline bool GetValue() const
{
return m_bActive;
};
};
class SliderFloat : public ElementInput<float>
{