forked from zxvnme/zgui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zgui.cc
978 lines (764 loc) · 37.1 KB
/
zgui.cc
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
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <cctype>
#include <Windows.h>
#include "zgui.hh"
// zgui by zxvnme (https://github.com/zxvnme)
// heres defines that are designed to be modified by your preferences.
// see zgui.hh for complete documentation.
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Color definition. Can be changed at any time just simply by editing this struct.
static struct {
zgui::color window_border_inner_fill{ 60, 60, 60, 255 };
zgui::color window_border_fill{ 40, 40, 40, 255 };
zgui::color window_border_color{ 10, 10, 10, 255 };
zgui::color window_background{ 40, 40, 40, 255 };
zgui::color control_outline{ 23, 23, 30, 255 };
zgui::color control_active_or_clicked{ 108, 92, 231, 255 };
zgui::color control_idle{ 62, 62, 72, 255 };
zgui::color color_groupbox_bg{ 50, 50, 50, 255 };
zgui::color color_text{ 203, 203, 203, 255 };
zgui::color color_text_dimmer{ 99, 110, 114, 255 };
zgui::color color_slider{ 108, 92, 231, 255 };
zgui::color color_combo_bg{ 108, 92, 231, 255 };
} global_colors;
static struct {
// Base position of first drawn control (px). DO NOT change if its necessary
zgui::vec2 base_pos{ 16, 23 };
// Spacing between items (px)
int item_spacing = 16;
// Key that will toggle menu visibility unless zgui_window_flags_always_open is set
int menu_toggle_key = VK_INSERT;
} global_config;
// Window definitions.
static struct gui_context_t {
zgui::gui_window_context_t window;
} context;
// "Proxy" functions stuff...
zgui::functions_t zgui::functions;
// Globals
static zgui::vec2 mouse_pos;
static zgui::vec2 previous_mouse_pos;
// Input handling stuff
static bool key_state[256];
static bool prev_key_state[256];
// Check for input polling.
static bool input_loop_started = false;
// Function for starting our input loop.
void zgui::poll_input(std::string_view window_name)
{
if (window_name.empty())
throw std::exception("No window from where input should be read from specified in function parameter.");
for (int i = 0; i < 256; i++) {
prev_key_state[i] = key_state[i];
key_state[i] = GetAsyncKeyState(i);
}
POINT p_mouse_pos;
GetCursorPos(&p_mouse_pos);
ScreenToClient(FindWindow(nullptr, window_name.data()), &p_mouse_pos);
previous_mouse_pos = mouse_pos;
mouse_pos = vec2{ static_cast<float>(p_mouse_pos.x), static_cast<float>(p_mouse_pos.y) };
if (!input_loop_started)
input_loop_started = true;
}
void zgui::poll_input(HWND hwnd)
{
if (!hwnd)
throw std::exception("No window from where input should be read from specified in function parameter.");
for (int i = 0; i < 256; i++) {
prev_key_state[i] = key_state[i];
key_state[i] = GetAsyncKeyState(i);
}
POINT p_mouse_pos;
GetCursorPos(&p_mouse_pos);
ScreenToClient(hwnd, &p_mouse_pos);
previous_mouse_pos = mouse_pos;
mouse_pos = vec2{ static_cast<float>(p_mouse_pos.x), static_cast<float>(p_mouse_pos.y) };
if (!input_loop_started)
input_loop_started = true;
}
// Input utilities.
constexpr bool key_pressed(const int key) noexcept
{
return key_state[key] && !prev_key_state[key];
}
constexpr bool key_down(const int key) noexcept
{
return key_state[key];
}
constexpr bool key_released(const int key) noexcept
{
return !key_state[key] && prev_key_state[key];
}
// Check if mouse is hovered over specified region.
bool mouse_in_region(const int x, const int y, const int w, const int h) noexcept
{
return mouse_pos.x > x && mouse_pos.y > y && mouse_pos.x < w + x && mouse_pos.y < h + y;
}
// Push cursor position to the stack
void zgui::push_cursor_pos(const vec2 pos) noexcept
{
context.window.cursor_pos.push(pos);
}
// Pop cursor position from the stack
zgui::vec2 zgui::pop_cursor_pos() noexcept
{
const vec2 pos = context.window.cursor_pos.top();
context.window.cursor_pos.pop();
return pos;
}
// Hashing util
std::vector<std::string> split_str(const char* str, const char separator) noexcept
{
std::vector<std::string> output;
std::string substring;
std::istringstream stream{ str };
while (std::getline(stream, substring, separator))
output.push_back(substring);
return output;
}
static constexpr auto hash(const char* str) noexcept
{
uint32_t hash = 2166136261;
for (; *str; str++)
hash = (hash ^ *str) * 16777619;
return hash;
}
// Names for each of VKs
constexpr std::string_view keys_list[]{
"Error", "Left Mouse", "Right Mouse", "Break", "Middle Mouse", "Mouse 4", "Mouse 5",
"Error", "Backspace", "TAB", "Error", "Error", "Error", "ENTER", "Error", "Error", "SHIFT",
"CTRL", "ALT","PAUSE","CAPS LOCK", "Error", "Error", "Error", "Error", "Error", "Error",
"Error", "Error", "Error", "Error", "Error", "SPACEBAR","PG UP", "PG DOWN", "END", "HOME", "Left",
"Up", "Right", "Down", "Error", "Print", "Error", "Print Screen", "Insert","Delete", "Error", "0", "1",
"2", "3", "4", "5", "6", "7", "8", "9", "Error", "Error", "Error", "Error", "Error", "Error",
"Error", "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", "Error", "Error", "Error", "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", "Error", "Error", "Error", "Error", "Error","Error", "Error", "Error",
"NUM LOCK", "SCROLL LOCK", "Error", "Error", "Error", "Error", "Error", "Error", "Error",
"Error", "Error","Error", "Error", "Error", "Error", "Error", "LSHIFT", "RSHIFT", "LCONTROL",
"RCONTROL", "LMENU", "RMENU", "Error","Error", "Error","Error", "Error", "Error", "Error",
"Error", "Error", "Error", "Next Track", "Previous Track", "Stop", "Play/Pause", "Error", "Error",
"Error", "Error", "Error", "Error", ";", "+", ",", "-", ".", "/?", "~", "Error", "Error",
"Error", "Error","Error", "Error", "Error", "Error", "Error", "Error", "Error",
"Error", "Error", "Error", "Error", "Error", "Error", "Error", "Error","Error",
"Error", "Error", "Error", "Error", "Error", "Error", "[{", "\\|", "}]", "'\"", "Error",
"Error", "Error", "Error","Error", "Error", "Error", "Error", "Error", "Error",
"Error", "Error", "Error", "Error", "Error", "Error", "Error", "Error", "Error",
"Error", "Error", "Error", "Error", "Error", "Error", "Error", "Error", "Error",
"Error", "Error"
};
struct key_code_info {
int vk;
char regular;
char shift;
};
static key_code_info special_characters[22] = {
{ 48, '0', ')' },
{ 49, '1', '!' },
{ 50, '2', '@' },
{ 51, '3', '#' },
{ 52, '4', '$' },
{ 53, '5', '%' },
{ 54, '6', '^' },
{ 55, '7', '&' },
{ 56, '8', '*' },
{ 57, '9', '(' },
{ 32, ' ', ' ' },
{ 192, '`', '~' },
{ 189, '-', '_' },
{ 187, '=', '+' },
{ 219, '[', '{' },
{ 220, '\\', '|' },
{ 221, ']', '}' },
{ 186, ';', ':' },
{ 222, '\'', '"' },
{ 188, ',', '<' },
{ 190, '.', '>' },
{ 191, '/', '?' }
};
bool zgui::begin_window(std::string_view title, const vec2 default_size, const unsigned long font, const int flags)
{
if (!input_loop_started)
throw std::exception("Input loop didnt start or didnt start properly.");;
context.window.font = font;
if (!(flags & zgui_window_flags_always_open))
{
if (key_pressed(global_config.menu_toggle_key))
context.window.opened = !context.window.opened;
}
else
context.window.opened = true;
if (const int prev_alpha = context.window.alpha; !(flags & zgui_window_flags_no_ontoggle_animation))
{
const int fade_factor = static_cast<int>(1.0f / 0.15f * functions.get_frametime() * 255);
context.window.alpha = std::clamp(context.window.alpha + (context.window.opened ? fade_factor : -fade_factor), 0, 255);
if (context.window.alpha != prev_alpha)
{
global_colors.window_border_inner_fill.a = context.window.alpha;
global_colors.window_border_fill.a = context.window.alpha;
global_colors.window_border_color.a = context.window.alpha;
global_colors.window_background.a = context.window.alpha;
global_colors.control_outline.a = context.window.alpha;
global_colors.control_active_or_clicked.a = context.window.alpha;
global_colors.control_idle.a = context.window.alpha;
global_colors.color_groupbox_bg.a = context.window.alpha;
global_colors.color_text.a = context.window.alpha;
global_colors.color_text_dimmer.a = context.window.alpha;
global_colors.color_slider.a = context.window.alpha;
}
}
if (context.window.opened || context.window.alpha > 0)
{
if (!(flags & zgui_window_flags_no_move))
{
if ((flags & zgui_window_flags_no_border ? mouse_in_region(context.window.position.x + 9, context.window.position.y + 14, context.window.size.x - 18, 14)
: mouse_in_region(context.window.position.x - 6, context.window.position.y - 10, context.window.size.x + 12, 16))
&& key_pressed(VK_LBUTTON) && !context.window.dragging)
{
context.window.dragging = true;
}
else if (key_down(VK_LBUTTON) && context.window.dragging)
{
const vec2 mouse_delta{ mouse_pos.x - previous_mouse_pos.x, mouse_pos.y - previous_mouse_pos.y };
const vec2 new_position{ context.window.position.x + mouse_delta.x, context.window.position.y + mouse_delta.y };
context.window.position = new_position;
}
else if (!key_down(VK_LBUTTON) && context.window.dragging)
{
context.window.dragging = false;
}
}
if (context.window.size.x < 1 && context.window.size.y < 1)
context.window.size = default_size;
if (!(flags & zgui_window_flags_no_border))
{
functions.draw_filled_rect(context.window.position.x - 6, context.window.position.y - 10, context.window.size.x + 12, context.window.size.y + 16, global_colors.window_border_inner_fill);
functions.draw_filled_rect(context.window.position.x - 5, context.window.position.y - 9, context.window.size.x + 10, context.window.size.y + 14, global_colors.window_border_color);
functions.draw_filled_rect(context.window.position.x - 4, context.window.position.y - 8, context.window.size.x + 8, context.window.size.y + 12, global_colors.window_border_fill);
functions.draw_filled_rect(context.window.position.x, context.window.position.y + 7, context.window.size.x, context.window.size.y - 7, global_colors.window_border_color);
functions.draw_filled_rect(context.window.position.x + 1, context.window.position.y + 8, context.window.size.x - 2, context.window.size.y - 9, global_colors.window_border_inner_fill);
functions.draw_filled_rect(context.window.position.x + 8, context.window.position.y + 15, context.window.size.x - 16, context.window.size.y - 23, global_colors.window_border_color);
}
if (!(flags & zgui_window_flags_no_titlebar))
functions.draw_text(context.window.position.x + context.window.size.x * 0.5, context.window.position.y + (context.window.size.y * 0.010) - 10, global_colors.color_text, context.window.font, true, title.data());
functions.draw_filled_rect(context.window.position.x + 9, context.window.position.y + 16, context.window.size.x - 18, context.window.size.y - 25, global_colors.window_background);
push_cursor_pos(global_config.base_pos);
}
return context.window.opened || context.window.alpha > 0;
}
void zgui::end_window() noexcept
{
while (!context.window.cursor_pos.empty())
{
context.window.cursor_pos.pop();
}
}
void zgui::begin_groupbox(std::string_view title, const vec2 size) noexcept
{
const vec2 cursor_pos = pop_cursor_pos();
const vec2 draw_pos{ context.window.position.x + cursor_pos.x, context.window.position.y + cursor_pos.y };
functions.draw_rect(draw_pos.x - 1, draw_pos.y - 1, size.x + 2, size.y + 2, global_colors.control_outline);
functions.draw_filled_rect(draw_pos.x, draw_pos.y, size.x, size.y, global_colors.color_groupbox_bg);
if (title.length() > 0)
functions.draw_text(draw_pos.x + 4, draw_pos.y - 8, global_colors.color_text, context.window.font, false, title.data());
context.window.next_cursor_pos = vec2{ cursor_pos.x, cursor_pos.y + size.y + 10 };
push_cursor_pos(vec2{ cursor_pos.x + 8, cursor_pos.y + 14 });
}
void zgui::end_groupbox() noexcept
{
push_cursor_pos(context.window.next_cursor_pos);
context.window.next_cursor_pos = { };
}
void zgui::checkbox(const char* id, bool& value) noexcept
{
std::vector<std::string> id_split = split_str(id, '#');
const int control_height = 8;
const int control_width = 8;
const vec2 cursor_pos = pop_cursor_pos();
const vec2 draw_pos{ context.window.position.x + cursor_pos.x, context.window.position.y + cursor_pos.y };
functions.draw_filled_rect(draw_pos.x, draw_pos.y, control_width, control_height, global_colors.control_outline);
functions.draw_filled_rect(draw_pos.x + 1, draw_pos.y + 1, control_width - 2, control_height - 2, value ? global_colors.control_active_or_clicked : global_colors.control_idle);
int text_wide, text_tall;
functions.get_text_size(context.window.font, id_split[0].c_str(), text_wide, text_tall);
const bool active = context.window.blocking == hash(id);
functions.draw_text(draw_pos.x + 14, draw_pos.y - 2, value ? global_colors.color_text : global_colors.color_text_dimmer, context.window.font, false, id_split[0].c_str());
if (const bool hovered = mouse_in_region(draw_pos.x, draw_pos.y, control_width + 6 + text_wide, control_height); !active && hovered && key_pressed(VK_LBUTTON))
{
context.window.blocking = hash(id);
}
else if (active && !key_down(VK_LBUTTON))
{
context.window.blocking = 0;
value = !value;
}
push_cursor_pos(vec2{ cursor_pos.x + 14 + text_wide + global_config.item_spacing, cursor_pos.y });
push_cursor_pos(vec2{ cursor_pos.x, cursor_pos.y + global_config.item_spacing });
}
void zgui::toggle_button(const char* id, const vec2 size, bool& value) noexcept
{
std::vector<std::string> id_split = split_str(id, '#');
const vec2 cursor_pos = pop_cursor_pos();
const vec2 draw_pos{ context.window.position.x + cursor_pos.x, context.window.position.y + cursor_pos.y };
functions.draw_filled_rect(draw_pos.x, draw_pos.y, size.x, size.y, global_colors.control_outline);
functions.draw_filled_rect(draw_pos.x + 1, draw_pos.y + 1, size.x - 2, size.y - 2, value ? global_colors.control_active_or_clicked : global_colors.control_idle);
int text_wide, text_tall;
functions.get_text_size(context.window.font, id_split[0].c_str(), text_wide, text_tall);
const bool active = context.window.blocking == hash(id);
functions.draw_text(draw_pos.x + size.x / 2 - text_wide / 2, draw_pos.y + size.y / 2 - text_tall / 2, global_colors.color_text, context.window.font, false, id_split[0].c_str());
push_cursor_pos(vec2{ cursor_pos.x + size.x + global_config.item_spacing, cursor_pos.y });
push_cursor_pos(vec2{ cursor_pos.x, cursor_pos.y + size.y / 2 + global_config.item_spacing });
if (const bool hovered = mouse_in_region(draw_pos.x, draw_pos.y, size.x, size.y); !active && hovered && key_pressed(VK_LBUTTON))
{
context.window.blocking = hash(id);
}
else if (active && !key_down(VK_LBUTTON))
{
context.window.blocking = 0;
value = !value;
}
}
bool zgui::button(const char* id, const vec2 size) noexcept
{
std::vector<std::string> id_split = split_str(id, '#');
const vec2 cursor_pos = pop_cursor_pos();
const vec2 draw_pos{ context.window.position.x + cursor_pos.x, context.window.position.y + cursor_pos.y };
const bool active = context.window.blocking == hash(id);
functions.draw_filled_rect(draw_pos.x, draw_pos.y, size.x, size.y, global_colors.control_outline);
functions.draw_filled_rect(draw_pos.x + 1, draw_pos.y + 1, size.x - 2, size.y - 2, active ? global_colors.control_active_or_clicked : global_colors.control_idle);
int text_wide, text_tall;
functions.get_text_size(context.window.font, id_split[0].c_str(), text_wide, text_tall);
functions.draw_text(draw_pos.x + size.x / 2 - text_wide / 2, draw_pos.y + size.y / 2 - text_tall / 2, global_colors.color_text, context.window.font, false, id_split[0].data());
push_cursor_pos(vec2{ cursor_pos.x + size.x + global_config.item_spacing, cursor_pos.y });
push_cursor_pos(vec2{ cursor_pos.x, cursor_pos.y + size.y / 2 + global_config.item_spacing });
bool result = false;
if (const bool hovered = mouse_in_region(draw_pos.x, draw_pos.y, size.x, size.y); !active && hovered && key_pressed(VK_LBUTTON))
{
context.window.blocking = hash(id);
}
else if (active && !key_down(VK_LBUTTON))
{
context.window.blocking = 0;
result = hovered;
}
return result;
}
void zgui::key_bind(const char* id, int& value) noexcept
{
std::vector<std::string> id_split = split_str(id, '#');
const int control_width = 80;
const int control_height = 20;
value = std::clamp(value, 0, 255);
const vec2 cursor_pos = pop_cursor_pos();
vec2 draw_pos{ context.window.position.x + cursor_pos.x + 14, context.window.position.y + cursor_pos.y };
const bool inlined = id_split[0].empty();
if (!inlined)
{
int text_wide, text_tall;
functions.get_text_size(context.window.font, id_split[0].c_str(), text_wide, text_tall);
functions.draw_text(draw_pos.x, draw_pos.y - 4, global_colors.color_text, context.window.font, false, id_split[0].c_str());
draw_pos.y += text_tall;
}
const bool active = context.window.blocking == hash(id);
functions.draw_filled_rect(draw_pos.x, draw_pos.y, control_width, control_height, global_colors.control_outline);
functions.draw_filled_rect(draw_pos.x + 1, draw_pos.y + 1, control_width - 2, control_height - 2, active ? global_colors.control_active_or_clicked : global_colors.control_idle);
functions.draw_text(draw_pos.x + 4, draw_pos.y + 4, global_colors.color_text, context.window.font, false, active ? "Press any key" : keys_list[value].data());
push_cursor_pos(vec2{ cursor_pos.x + control_width + global_config.item_spacing, cursor_pos.y });
push_cursor_pos(vec2{ cursor_pos.x, cursor_pos.y + control_height / 2 + global_config.item_spacing + (inlined ? 0 : 12) });
if (const bool hovered = mouse_in_region(draw_pos.x, draw_pos.y, control_width, control_height); hovered && key_pressed(VK_LBUTTON) && context.window.blocking == 0)
{
context.window.blocking = hash(id);
}
else if (active)
{
for (int i = 0; i < 256; i++)
{
if (key_pressed(i))
{
if (keys_list[i] != "Error")
value = i;
context.window.blocking = 0;
}
}
}
}
void zgui::text_input(const char* id, std::string& value, const int max_length, const int flags) noexcept
{
std::vector<std::string> id_split = split_str(id, '#');
const int control_width = 80;
const int control_height = 20;
const vec2 cursor_pos = pop_cursor_pos();
vec2 draw_pos{ context.window.position.x + cursor_pos.x + 14, context.window.position.y + cursor_pos.y };
const bool inlined = id_split[0].empty();
if (!inlined)
{
int text_wide, text_tall;
functions.get_text_size(context.window.font, id_split[0].c_str(), text_wide, text_tall);
functions.draw_text(draw_pos.x, draw_pos.y - 4, global_colors.color_text, context.window.font, false, id_split[0].c_str());
draw_pos.y += text_tall;
}
const bool active = context.window.blocking == hash(id);
const bool hovered = mouse_in_region(draw_pos.x, draw_pos.y, control_width, control_height);
functions.draw_filled_rect(draw_pos.x, draw_pos.y, control_width, control_height, global_colors.control_outline);
functions.draw_filled_rect(draw_pos.x + 1, draw_pos.y + 1, control_width - 2, control_height - 2, active ? global_colors.control_active_or_clicked : global_colors.control_idle);
functions.draw_text(draw_pos.x + 4, draw_pos.y + 4, global_colors.color_text, context.window.font, false, flags & zgui_text_input_flags_password ? std::string(value.length(), '*').c_str() : value.c_str());
push_cursor_pos(vec2{ cursor_pos.x + control_width + global_config.item_spacing, cursor_pos.y });
push_cursor_pos(vec2{ cursor_pos.x, cursor_pos.y + control_height / 2 + global_config.item_spacing + (inlined ? 0 : 12) });
if (hovered && key_pressed(VK_LBUTTON) && !active)
{
context.window.blocking = hash(id);
}
else if (active)
{
if (key_pressed(VK_ESCAPE) || key_pressed(VK_RETURN) || (!hovered && key_pressed(VK_LBUTTON)))
{
context.window.blocking = 0;
}
else if (key_pressed(VK_BACK) && !value.empty())
{
value.pop_back();
}
else if (value.length() < max_length)
{
for (int i = 32; i <= 222; i++)
{
if ((i > 32 && i < 48) || (i > 57 && i < 65) || (i > 90 && i < 186))
continue;
if (i > 57 && i <= 90)
{
if (key_pressed(i))
value += key_down(VK_SHIFT) ? static_cast<char>(i) : static_cast<char>(i + 32);
}
else
{
if (key_pressed(i))
{
for (int j = 0; j < sizeof(special_characters); j++)
{
if (special_characters[j].vk == i)
value += key_down(VK_SHIFT) ? special_characters[j].shift : special_characters[j].regular;
}
}
}
}
}
}
}
void zgui::slider_int(const char* id, const int min, const int max, int& value) noexcept
{
std::vector<std::string> id_split = split_str(id, '#');
const int control_width = 120;
const int control_height = 10;
const vec2 cursor_pos = pop_cursor_pos();
vec2 draw_pos{ context.window.position.x + cursor_pos.x + 14, context.window.position.y + cursor_pos.y };
const bool inlined = id_split[0].empty();
if (!inlined)
{
int text_wide, text_tall;
functions.get_text_size(context.window.font, id_split[0].c_str(), text_wide, text_tall);
functions.draw_text(draw_pos.x, draw_pos.y - 4, global_colors.color_text, context.window.font, false, id_split[0].c_str());
draw_pos.y += text_tall;
}
const int dynamic_width = (static_cast<float>(value) - min) / (max - min) * control_width - 2;
functions.draw_filled_rect(draw_pos.x, draw_pos.y, control_width, control_height, global_colors.control_outline);
functions.draw_filled_rect(draw_pos.x + 1, draw_pos.y + 1, control_width - 2, control_height - 2, global_colors.control_idle);
functions.draw_filled_rect(draw_pos.x + 1, draw_pos.y + 1, dynamic_width, 10 - 2, global_colors.color_slider);
int text_wide, text_tall;
std::string value_str = std::to_string(value);
functions.get_text_size(context.window.font, value_str.c_str(), text_wide, text_tall);
int text_x = dynamic_width - text_wide;
if (text_x < 0)
text_x = 0;
functions.draw_text(draw_pos.x + text_x, draw_pos.y, global_colors.color_text, context.window.font, false, value_str.c_str());
functions.draw_text(draw_pos.x - (control_height - 2), draw_pos.y - 2, global_colors.color_text_dimmer, context.window.font, false, "-");
functions.draw_text(draw_pos.x + (control_width + 4), draw_pos.y - 2, global_colors.color_text_dimmer, context.window.font, false, "+");
if (context.window.blocking == 0 && mouse_in_region(draw_pos.x - (control_height - 2), draw_pos.y, 8, 10) && key_pressed(VK_LBUTTON))
value = std::clamp(value - 1, min, max);
else if (context.window.blocking == 0 && mouse_in_region(draw_pos.x + control_width, draw_pos.y, 8, 10) && key_pressed(VK_LBUTTON))
value = std::clamp(value + 1, min, max);
if (const bool hovered = mouse_in_region(draw_pos.x, draw_pos.y, control_width, control_height); hovered && key_pressed(VK_LBUTTON) && context.window.blocking == 0)
{
context.window.blocking = hash(id);
}
else if (key_down(VK_LBUTTON) && context.window.blocking == hash(id))
{
float value_unmapped = std::clamp(mouse_pos.x - draw_pos.x, 0.0f, static_cast<float>(control_width));
int value_mapped = static_cast<int>(value_unmapped / control_width * (max - min) + min);
value = value_mapped;
}
else if (!key_down(VK_LBUTTON) && context.window.blocking == hash(id))
{
context.window.blocking = 0;
}
push_cursor_pos(vec2{ cursor_pos.x + control_width + 14 + global_config.item_spacing, cursor_pos.y });
push_cursor_pos(vec2{ cursor_pos.x, cursor_pos.y + control_height / 2 + global_config.item_spacing + (inlined ? 0 : 12) });
}
void zgui::slider_float(const char* id, const float min, const float max, float& value) noexcept
{
std::vector<std::string> id_split = split_str(id, '#');
const int control_width = 120;
const int control_height = 10;
const vec2 cursor_pos = pop_cursor_pos();
vec2 draw_pos{ context.window.position.x + cursor_pos.x + 14, context.window.position.y + cursor_pos.y };
const bool inlined = id_split[0].empty();
if (!inlined)
{
int text_wide, text_tall;
functions.get_text_size(context.window.font, id_split[0].c_str(), text_wide, text_tall);
functions.draw_text(draw_pos.x, draw_pos.y - 4, global_colors.color_text, context.window.font, false, id_split[0].c_str());
draw_pos.y += text_tall;
}
const float dynamic_width = (value - min) / (max - min) * static_cast<float>(control_width) - 2.f;
functions.draw_filled_rect(draw_pos.x, draw_pos.y, control_width, control_height, global_colors.control_outline);
functions.draw_filled_rect(draw_pos.x + 1, draw_pos.y + 1, control_width - 2, control_height - 2, global_colors.control_idle);
functions.draw_filled_rect(draw_pos.x + 1, draw_pos.y + 1, dynamic_width, 10 - 2, global_colors.color_slider);
int text_wide, text_tall;
std::stringstream ss;
ss << std::fixed << std::setprecision(2) << value;
std::string value_str = ss.str();
functions.get_text_size(context.window.font, value_str.c_str(), text_wide, text_tall);
int text_x = dynamic_width - text_wide;
if (text_x < 0)
text_x = 0;
functions.draw_text(draw_pos.x + text_x, draw_pos.y, global_colors.color_text, context.window.font, false, value_str.c_str());
functions.draw_text(draw_pos.x - (control_height - 2), draw_pos.y - 2, global_colors.color_text_dimmer, context.window.font, false, "-");
functions.draw_text(draw_pos.x + (control_width + 4), draw_pos.y - 2, global_colors.color_text_dimmer, context.window.font, false, "+");
if (context.window.blocking == 0 && mouse_in_region(draw_pos.x - (control_height - 2), draw_pos.y, 8, 10) && key_pressed(VK_LBUTTON))
value = std::clamp(value - 1, min, max);
else if (context.window.blocking == 0 && mouse_in_region(draw_pos.x + control_width, draw_pos.y, 8, 10) && key_pressed(VK_LBUTTON))
value = std::clamp(value + 1, min, max);
if (const bool hovered = mouse_in_region(draw_pos.x, draw_pos.y, control_width, control_height); hovered && key_pressed(VK_LBUTTON) && context.window.blocking == 0)
{
context.window.blocking = hash(id);
}
else if (key_down(VK_LBUTTON) && context.window.blocking == hash(id))
{
float value_unmapped = std::clamp(mouse_pos.x - draw_pos.x, 0.0f, static_cast<float>(control_width));
float value_mapped = static_cast<float>((value_unmapped / static_cast<float>(control_width)) * (max - min) + min);
value = value_mapped;
}
else if (!key_down(VK_LBUTTON) && context.window.blocking == hash(id))
{
context.window.blocking = 0;
}
push_cursor_pos(vec2{ cursor_pos.x + control_width + 14 + global_config.item_spacing, cursor_pos.y });
push_cursor_pos(vec2{ cursor_pos.x, cursor_pos.y + control_height / 2 + global_config.item_spacing + (inlined ? 0 : 12) });
}
void zgui::combobox(const char* id, std::vector<std::string>items, int& value) noexcept
{
std::vector<std::string> id_split = split_str(id, '#');
const int control_width = 70;
const int control_height = 20;
value = std::clamp(value, 0, static_cast<int>(items.size()) - 1);
const vec2 cursor_pos = pop_cursor_pos();
vec2 draw_pos{ context.window.position.x + cursor_pos.x + 14, context.window.position.y + cursor_pos.y };
const bool inlined = id_split[0].empty();
if (!inlined)
{
int text_wide, text_tall;
functions.get_text_size(context.window.font, id_split[0].c_str(), text_wide, text_tall);
functions.draw_text(draw_pos.x, draw_pos.y - 4, global_colors.color_text, context.window.font, false, id_split[0].c_str());
draw_pos.y += text_tall;
}
functions.draw_filled_rect(draw_pos.x, draw_pos.y, control_width, control_height, global_colors.control_outline);
functions.draw_filled_rect(draw_pos.x + 1, draw_pos.y + 1, control_width - 2, control_height - 2, global_colors.control_idle);
functions.draw_text(draw_pos.x + 4, draw_pos.y + 4, global_colors.color_text, context.window.font, false, items.at(value).c_str());
functions.draw_text(draw_pos.x + control_width - 10, draw_pos.y + 4, global_colors.color_text, context.window.font, false, "+");
if (context.window.blocking == hash(id))
{
push_cursor_pos(vec2{ cursor_pos.x + control_width + global_config.item_spacing, cursor_pos.y });
push_cursor_pos(vec2{ cursor_pos.x, cursor_pos.y + control_height / 2 + global_config.item_spacing + (inlined ? 0 : 12) + 20 * items.size() });
}
else
{
push_cursor_pos(vec2{ cursor_pos.x + control_width + global_config.item_spacing, cursor_pos.y });
push_cursor_pos(vec2{ cursor_pos.x, cursor_pos.y + control_height / 2 + global_config.item_spacing + (inlined ? 0 : 12) });
}
if (const bool hovered = mouse_in_region(draw_pos.x, draw_pos.y, control_width, control_height); hovered && key_pressed(VK_LBUTTON) && context.window.blocking == 0)
{
context.window.blocking = hash(id);
}
else if (context.window.blocking == hash(id))
{
for (int i = 1; i <= items.size(); i++)
{
bool hovered = mouse_in_region(draw_pos.x, draw_pos.y + (control_height - 1) * i, control_width, control_height);
if (hovered && key_pressed(VK_LBUTTON))
{
context.window.blocking = 0;
value = i - 1;
}
functions.draw_filled_rect(draw_pos.x, draw_pos.y + 19 * i, control_width, control_height, global_colors.control_outline);
functions.draw_filled_rect(draw_pos.x + 1, draw_pos.y + (19 * i) + 1, control_width - 2, control_height - 2, hovered ? global_colors.color_combo_bg : global_colors.control_idle);
functions.draw_text(draw_pos.x + 4, draw_pos.y + (control_height - 1) * i + 4, global_colors.color_text, context.window.font, false, items.at(i - 1).c_str());
}
}
}
void zgui::multi_combobox(const char* id, std::vector<multi_select_item> items) noexcept
{
std::vector<std::string> id_split = split_str(id, '#');
const int control_width = 100;
const int control_height = 20;
const vec2 cursor_pos = pop_cursor_pos();
vec2 draw_pos{ context.window.position.x + cursor_pos.x + 14, context.window.position.y + cursor_pos.y };
const bool inlined = id_split[0].empty();
if (!inlined)
{
int text_wide, text_tall;
functions.get_text_size(context.window.font, id_split[0].c_str(), text_wide, text_tall);
functions.draw_text(draw_pos.x, draw_pos.y - 4, global_colors.color_text, context.window.font, false, id_split[0].c_str());
draw_pos.y += text_tall;
}
functions.draw_filled_rect(draw_pos.x, draw_pos.y, control_width, control_height, global_colors.control_outline);
functions.draw_filled_rect(draw_pos.x + 1, draw_pos.y + 1, control_width - 2, control_height - 2, global_colors.control_idle);
functions.draw_text(draw_pos.x + control_width - 10, draw_pos.y + 4, global_colors.color_text, context.window.font, false, "+");
std::string value_str;
int text_wide, text_tall;
for (auto &item_t : items) {
if (*item_t.value) {
if (value_str.length() > 0)
value_str += ", ";
value_str += item_t.name;
}
}
functions.get_text_size(context.window.font, value_str.c_str(), text_wide, text_tall);
if (text_wide > control_width - 18)
{
value_str.resize(control_width / 10);
value_str += " ...";
}
if (!value_str.length())
value_str += "None";
functions.draw_text(draw_pos.x + 4, draw_pos.y + 4, global_colors.color_text, context.window.font, false, value_str.c_str());
if (context.window.blocking == hash(id))
{
push_cursor_pos(vec2{ cursor_pos.x + control_width + global_config.item_spacing, cursor_pos.y });
push_cursor_pos(vec2{ cursor_pos.x, cursor_pos.y + control_height / 2 + global_config.item_spacing + (inlined ? 0 : 12) + 20 * items.size() });
}
else
{
push_cursor_pos(vec2{ cursor_pos.x + control_width + global_config.item_spacing, cursor_pos.y });
push_cursor_pos(vec2{ cursor_pos.x, cursor_pos.y + control_height / 2 + global_config.item_spacing + (inlined ? 0 : 12) });
}
if (const bool hovered = mouse_in_region(draw_pos.x, draw_pos.y, control_width, control_height); hovered && key_pressed(VK_LBUTTON) && context.window.blocking == 0)
{
context.window.blocking = hash(id);
}
else if (context.window.blocking == hash(id))
{
for (int i = 1; i <= items.size(); i++)
{
bool hovered = mouse_in_region(draw_pos.x, draw_pos.y + (control_height - 1) * i, control_width, control_height);
if (hovered && key_pressed(VK_LBUTTON))
{
context.window.blocking = 0;
*items[i - 1].value = !*items[i - 1].value;
}
functions.draw_filled_rect(draw_pos.x, draw_pos.y + 19 * i, control_width, control_height, global_colors.control_outline);
functions.draw_filled_rect(draw_pos.x + 1, draw_pos.y + (19 * i) + 1, control_width - 2, control_height - 2, *items[i - 1].value || hovered ? global_colors.control_active_or_clicked : global_colors.control_idle);
functions.draw_text(draw_pos.x + 4, draw_pos.y + (control_height - 1) * i + 4, global_colors.color_text, context.window.font, false, items[i - 1].name.data());
}
}
}
void zgui::listbox(const char* id, std::vector<multi_select_item> items) noexcept
{
std::vector<std::string> id_split = split_str(id, '#');
const int control_width = 100;
const int control_height = 20;
const vec2 cursor_pos = pop_cursor_pos();
vec2 draw_pos{ context.window.position.x + cursor_pos.x, context.window.position.y + cursor_pos.y };
const bool inlined = id_split[0].empty();
if (!inlined)
{
int text_wide, text_tall;
functions.get_text_size(context.window.font, id_split[0].c_str(), text_wide, text_tall);
functions.draw_text(draw_pos.x, draw_pos.y - 4, global_colors.color_text, context.window.font, false, id_split[0].c_str());
draw_pos.y += text_tall;
}
functions.draw_filled_rect(draw_pos.x, draw_pos.y, control_width, control_height * items.size(), global_colors.control_outline);
functions.draw_filled_rect(draw_pos.x + 1, draw_pos.y + 1, control_width - 2, control_height * items.size() - 2, global_colors.control_idle);
for (int i = 1; i <= items.size(); i++)
{
const bool hovered = mouse_in_region(draw_pos.x, draw_pos.y + (control_height - 1) * (i - 1), control_width, control_height);
if (hovered && key_pressed(VK_LBUTTON))
{
context.window.blocking = 0;
*items[i - 1].value = !*items[i - 1].value;
}
functions.draw_text(draw_pos.x + 4, draw_pos.y + (control_height - 1) * (i - 1) + 4, *items[i - 1].value || hovered ? global_colors.control_active_or_clicked : global_colors.color_text, context.window.font, false, items[i - 1].name.data());
}
if (context.window.blocking == hash(id))
{
push_cursor_pos(vec2{ cursor_pos.x + control_width + global_config.item_spacing, cursor_pos.y });
push_cursor_pos(vec2{ cursor_pos.x, cursor_pos.y + control_height / 2 + global_config.item_spacing + (inlined ? 0 : 12) + 20 * items.size() });
}
else
{
push_cursor_pos(vec2{ cursor_pos.x + control_width + global_config.item_spacing, cursor_pos.y });
push_cursor_pos(vec2{ cursor_pos.x, cursor_pos.y + control_height / 2 + global_config.item_spacing + (inlined ? 0 : 12) });
}
if (const bool hovered = mouse_in_region(draw_pos.x, draw_pos.y, control_width, control_height); hovered && key_pressed(VK_LBUTTON) && context.window.blocking == 0)
{
context.window.blocking = hash(id);
}
}
bool zgui::clickable_text(const char* id) noexcept
{
std::vector<std::string> id_split = split_str(id, '#');
const vec2 cursor_pos = pop_cursor_pos();
const vec2 draw_pos{ context.window.position.x + cursor_pos.x, context.window.position.y + cursor_pos.y };
int text_width, text_tall;
functions.get_text_size(context.window.font, id_split[0].c_str(), text_width, text_tall);
const bool active = context.window.blocking == hash(id);
functions.draw_text(draw_pos.x, draw_pos.y, global_colors.color_text, context.window.font, false, id_split[0].c_str());
push_cursor_pos(vec2{ cursor_pos.x + text_width + global_config.item_spacing, cursor_pos.y });
push_cursor_pos(vec2{ cursor_pos.x, cursor_pos.y + text_tall / 2 + global_config.item_spacing });
bool result = false;
if (const bool hovered = mouse_in_region(draw_pos.x, draw_pos.y, text_width, text_tall); !active && hovered && key_pressed(VK_LBUTTON))
{
context.window.blocking = hash(id);
}
else if (active && !key_down(VK_LBUTTON))
{
context.window.blocking = 0;
result = hovered;
}
return result;
}
void zgui::text(const char* text) noexcept
{
const vec2 cursor_pos = pop_cursor_pos();
const vec2 draw_pos{ context.window.position.x + cursor_pos.x, context.window.position.y + cursor_pos.y };
int text_width, text_tall;
functions.get_text_size(context.window.font, text, text_width, text_tall);
functions.draw_text(draw_pos.x, draw_pos.y, global_colors.color_text, context.window.font, false, text);
push_cursor_pos(vec2{ cursor_pos.x + text_width + global_config.item_spacing, cursor_pos.y });
push_cursor_pos(vec2{ cursor_pos.x, cursor_pos.y + text_tall / 2 + global_config.item_spacing });
}
void zgui::dummy() noexcept
{
const vec2 cursor_pos = pop_cursor_pos();
push_cursor_pos(vec2{ cursor_pos.x, cursor_pos.y + global_config.item_spacing });
}
void zgui::next_column(const int pusher_x, const int pusher_y) noexcept
{
const vec2 cursor_pos = pop_cursor_pos();
vec2 new_cursor_pos{ cursor_pos.x + pusher_x, global_config.base_pos.y + pusher_y };
if (context.window.next_cursor_pos.y != 0)
new_cursor_pos.y += 14;
push_cursor_pos(new_cursor_pos);
}
void zgui::same_line(const float x_axis) noexcept
{
const vec2 cursor_pos = pop_cursor_pos();
if (x_axis != -1)
push_cursor_pos(vec2{ global_config.base_pos.x + x_axis, cursor_pos.x });
}
void zgui::backup_line() noexcept
{
const vec2 cursor_pos = pop_cursor_pos();
push_cursor_pos(vec2{ global_config.base_pos.x, cursor_pos.y });
}