-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosgui.cpp
More file actions
4527 lines (4370 loc) · 245 KB
/
Copy pathosgui.cpp
File metadata and controls
4527 lines (4370 loc) · 245 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
// OSGui core implementation: layout, interaction, widgets, and draw data.
#include "osgui.h"
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <float.h>
#include <algorithm>
#include <limits>
#include <sstream>
#include <iomanip>
#include <locale>
namespace og {
// =====================================================================
// Globals
// =====================================================================
static thread_local Context* GOG = 0;
static void CleanupContextState(Context* context);
const char* GetVersion() { return OSGUI_VERSION; }
Context* GetCurrentContext() { return GOG; }
void SetCurrentContext(Context* context) { GOG = context; }
Context& GetContext() { return *GOG; }
IO& GetIO() { return GOG->io; }
Style& GetStyle() { return GOG->style; }
FontAtlas& GetFontAtlas() { return GOG->atlas; }
DrawData* GetDrawData() { return &GOG->draw_data; }
const std::vector<Event>& GetEvents() { return GOG->events; }
const FrameMetrics& GetFrameMetrics() { return GOG->metrics; }
U32 GetColorU32(int idx) {
if (!GOG || idx < 0 || idx >= Col_COUNT) return 0;
U32 color = GOG->style.colors[idx];
if (GOG->disabled_depth > 0) {
int alpha = (int)(((color >> 24) & 255) * GOG->style.disabled_alpha);
color = (color & 0x00FFFFFFu) | ((U32)alpha << 24);
}
return color;
}
bool DebugCheckVersionAndDataLayout(const char* version, size_t io_size, size_t style_size,
size_t draw_vert_size, size_t draw_idx_size) {
bool ok = version && strcmp(version, OSGUI_VERSION) == 0 && io_size == sizeof(IO) &&
style_size == sizeof(Style) && draw_vert_size == sizeof(DrawVert) &&
draw_idx_size == sizeof(DrawIdx);
if (!ok && GOG) {
GOG->last_error = "OSGui version or data-layout mismatch";
if (GOG->debug_log_callback)
GOG->debug_log_callback(GOG->last_error.c_str(), GOG->debug_log_user_data);
}
return ok;
}
// =====================================================================
// Window (persistent per-name state)
// =====================================================================
struct ChildScrollRegion {
Vec4 rect;
ID id;
int depth;
ChildScrollRegion(const Vec4& value_rect, ID value_id, int value_depth)
: rect(value_rect), id(value_id), depth(value_depth) {}
};
struct Window {
ID id;
std::string name;
Vec2 pos;
Vec2 size; // size used this frame
Vec2 size_full; // size when expanded
bool collapsed;
bool active_this_frame;
bool active_last_frame;
int focus_order;
DockSlot dock_slot;
int flags;
bool appeared_this_frame;
// layout cursor (absolute screen coords)
Vec2 cursor;
Vec2 cursor_start;
Vec2 cursor_prev_line;
Vec2 cursor_max;
float curr_line_height;
float prev_line_height;
float indent;
float content_w;
float scroll_y;
float scroll_max_y;
bool scrollbar_active;
std::vector<ChildScrollRegion> child_scroll_regions;
ID wheel_child_id;
bool wheel_child_consumed;
bool skip_items;
Window* popup_parent;
DrawList draw;
std::vector<ID> id_stack;
Window() : id(0), collapsed(false), active_this_frame(false), active_last_frame(false), focus_order(0), dock_slot(Dock_None),
flags(WindowFlags_None), appeared_this_frame(false),
curr_line_height(0), prev_line_height(0), indent(0), content_w(0),
scroll_y(0), scroll_max_y(0), scrollbar_active(false), wheel_child_id(0),
wheel_child_consumed(false), skip_items(false), popup_parent(0) {}
};
static bool WindowDescendsFrom(Window* window, Window* ancestor) {
if (!ancestor) return false;
for (Window* current = window; current; current = current->popup_parent)
if (current == ancestor) return true;
return false;
}
static bool WindowInModalTree(const Context& context, Window* window) {
return !context.modal_window || WindowDescendsFrom(window, context.modal_window);
}
static Window* WindowAtPoint(const Vec2& point);
static bool ItemContainsPoint(const Vec4& rect, const Vec2& point, Window* window);
struct GridState {
Window* window;
Vec2 outer_cursor;
Vec2 outer_cursor_start;
float outer_content_w;
float outer_indent;
int columns;
int column;
float gap;
float column_width;
float row_y;
float row_height;
};
static thread_local std::map<Context*, std::vector<GridState> > GGridStacks;
static thread_local std::map<Context*, DockSlot> GNextDockSlots;
static thread_local std::map<Context*, bool> GNextDockSets;
static std::vector<GridState>& GridStack() { return GGridStacks[GOG]; }
static DockSlot& NextDockSlot() { return GNextDockSlots[GOG]; }
static bool& NextDockSet() { return GNextDockSets[GOG]; }
// =====================================================================
// Small helpers
// =====================================================================
static inline float Min(float a, float b) { return a < b ? a : b; }
static inline float Max(float a, float b) { return a > b ? a : b; }
static inline float Clamp(float v, float lo, float hi) { return v < lo ? lo : (v > hi ? hi : v); }
static inline bool FiniteFloat(float v) { return v == v && v <= FLT_MAX && v >= -FLT_MAX; }
static const int WindowFlags_InternalModal = 1 << 29;
static inline Vec2 Add(const Vec2& a, const Vec2& b) { return Vec2(a.x + b.x, a.y + b.y); }
static inline bool PointIn(const Vec2& p, const Vec4& r) {
return p.x >= r.x && p.y >= r.y && p.x < r.z && p.y < r.w;
}
static inline int ColR(U32 c) { return (int)(c & 255); }
static inline int ColG(U32 c) { return (int)((c >> 8) & 255); }
static inline int ColB(U32 c) { return (int)((c >> 16) & 255); }
static inline int ColA(U32 c) { return (int)((c >> 24) & 255); }
static U32 ColorLerp(U32 a, U32 b, float t) {
t = Clamp(t, 0.0f, 1.0f);
return OG_COL32((int)(ColR(a) + (ColR(b) - ColR(a)) * t),
(int)(ColG(a) + (ColG(b) - ColG(a)) * t),
(int)(ColB(a) + (ColB(b) - ColB(a)) * t),
(int)(ColA(a) + (ColA(b) - ColA(a)) * t));
}
static U32 ColorWithAlpha(U32 c, int alpha) {
return (c & 0x00FFFFFFu) | ((U32)Clamp((float)alpha, 0.0f, 255.0f) << 24);
}
static ID HashStr(const char* s, const char* s_end, ID seed) {
ID h = seed ? seed : 1469598103934665603ULL;
if (!s_end) { while (*s) { h ^= (unsigned char)*s++; h *= 1099511628211ULL; } }
else { while (s < s_end) { h ^= (unsigned char)*s++; h *= 1099511628211ULL; } }
return h;
}
static ID HashLabelID(const char* label, ID seed) {
if (!label) return 0;
const char* stable = strstr(label, "###");
return HashStr(stable ? stable + 3 : label, 0, seed);
}
static unsigned int DecodeUTF8(const char*& p, const char* end) {
if (!p || (end && p >= end) || !*p) return 0;
const char* start=p;const unsigned char* s=(const unsigned char*)start;unsigned int cp=0,min_value=0;int bytes=1;
if(s[0]<0x80){cp=s[0];min_value=0;}
else if((s[0]&0xE0)==0xC0){cp=s[0]&0x1F;bytes=2;min_value=0x80;}
else if((s[0]&0xF0)==0xE0){cp=s[0]&0x0F;bytes=3;min_value=0x800;}
else if((s[0]&0xF8)==0xF0){cp=s[0]&0x07;bytes=4;min_value=0x10000;}
else{++p;return 0xFFFD;}
for (int i = 1; i < bytes; ++i) {
if ((end && start + i >= end) || !s[i] || (s[i] & 0xC0) != 0x80) { p=start+1; return 0xFFFD; }
cp = (cp << 6) | (s[i] & 0x3F);
}
if(cp<min_value||cp>0x10FFFF||(cp>=0xD800&&cp<=0xDFFF)){p=start+1;return 0xFFFD;}
p = start + bytes;
return cp;
}
static const Glyph* FindGlyph(unsigned int cp) {
FontAtlas& atlas = GOG->atlas;
std::map<unsigned int, Glyph>::const_iterator found = atlas.glyph_map.find(cp);
if (found != atlas.glyph_map.end()) return &found->second;
if (cp < 128 && atlas.glyph_valid[cp]) return &atlas.glyphs[cp];
found = atlas.glyph_map.find('?');
if (found != atlas.glyph_map.end()) return &found->second;
return atlas.glyph_valid[(unsigned int)'?'] ? &atlas.glyphs[(unsigned int)'?'] : 0;
}
// "text##suffix" hides the suffix but hashes the full label; "text###stable"
// also keeps identity stable when the visible prefix changes.
static const char* FindDisplayEnd(const char* label) {
const char* p = label;
while (*p) { if (p[0] == '#' && p[1] == '#') return p; p++; }
return p;
}
ID GetID(const char* label) {
if (!GOG || !label) return 0;
ID seed = 0;
if (GOG->cur_window && !GOG->cur_window->id_stack.empty())
seed = GOG->cur_window->id_stack.back();
return HashLabelID(label, seed);
}
void PushID(const char* value) {
if (!GOG || !GOG->cur_window) return;
Window* w = GOG->cur_window;
w->id_stack.push_back(HashStr(value ? value : "", 0, w->id_stack.back()));
}
void PushID(int value) {
char buffer[48];
snprintf(buffer, sizeof(buffer), "#%d", value);
PushID(buffer);
}
void PushID(const void* value) {
char buffer[64];
snprintf(buffer, sizeof(buffer), "#%p", value);
PushID(buffer);
}
void PopID() {
if (!GOG || !GOG->cur_window) return;
std::vector<ID>& stack = GOG->cur_window->id_stack;
if (stack.size() > 1) stack.pop_back();
else {
GOG->last_error = "PopID() called with an empty user ID stack";
if (GOG->debug_log_callback) GOG->debug_log_callback(GOG->last_error.c_str(), GOG->debug_log_user_data);
}
}
static float AnimateID(ID id, float target, float speed) {
Context& g = *GOG;
AnimationState& state = g.animations[id];
if (state.last_frame == 0) state.value = target;
state.target = target;
state.last_frame = g.frame_count;
if (g.io.config_reduced_motion || g.style.motion_scale <= 0.0f) {
state.value = state.target;
state.velocity = 0.0f;
return state.value;
}
if (speed <= 0.0f) speed = g.style.animation_speed;
speed /= Max(g.style.motion_scale, 0.01f);
float dt = Clamp(g.io.delta_time, 0.0f, 0.05f);
float blend = 1.0f - expf(-speed * dt);
state.value += (state.target - state.value) * blend;
if (fabsf(state.target - state.value) < 0.0005f) state.value = state.target;
return state.value;
}
float Animate(const char* key, float target, float speed) {
ID seed = (GOG->cur_window && !GOG->cur_window->id_stack.empty())
? GOG->cur_window->id_stack.back() : 0;
return AnimateID(HashStr(key, 0, seed), target, speed);
}
// =====================================================================
// Input queue and diagnostics
// =====================================================================
IO::IO()
: display_size(), framebuffer_scale(1.0f, 1.0f), dpi_scale(1.0f), delta_time(1.0f / 60.0f),
mouse_pos(-1.0f, -1.0f), mouse_wheel(0.0f), mouse_wheel_h(0.0f),
get_clipboard_text(0), set_clipboard_text(0), clipboard_user_data(0),
backend_platform_name(0), backend_renderer_name(0), backend_flags(BackendFlags_None),
app_focused(true), config_reduced_motion(false), framerate(60.0f),
want_capture_mouse(false), want_capture_keyboard(false), want_text_input(false) {
for (int i = 0; i < 5; ++i) mouse_down[i] = false;
for (int i = 0; i < Key_COUNT; ++i) key_down[i] = false;
}
void IO::AddMousePosEvent(float x, float y) {
mouse_pos = Vec2(x, y); InputEvent e; e.type = InputEvent_MousePos; e.x = x; e.y = y; input_events.push_back(e);
}
void IO::AddMouseButtonEvent(int button, bool value) {
if (button < 0 || button >= 5) return;
mouse_down[button] = value; InputEvent e; e.type = InputEvent_MouseButton; e.key_or_button = button; e.down = value;
e.x = mouse_pos.x; e.y = mouse_pos.y; input_events.push_back(e);
}
void IO::AddMouseWheelEvent(float horizontal, float vertical) {
mouse_wheel_h += horizontal; mouse_wheel += vertical; InputEvent e; e.type = InputEvent_MouseWheel; e.x = horizontal; e.y = vertical; input_events.push_back(e);
}
void IO::AddKeyEvent(Key key, bool value) {
int index = (int)key; if (index <= 0 || index >= Key_COUNT) return;
key_down[index] = value; InputEvent e; e.type = InputEvent_Key; e.key_or_button = index; e.down = value; input_events.push_back(e);
}
void IO::AddInputCharacter(unsigned int codepoint) {
if (codepoint == 0 || codepoint > 0x10FFFF || (codepoint >= 0xD800 && codepoint <= 0xDFFF)) return;
input_chars.push_back(codepoint); InputEvent e; e.type = InputEvent_Text; e.codepoint = codepoint; input_events.push_back(e);
}
void IO::AddFocusEvent(bool focused) {
app_focused = focused; InputEvent e; e.type = InputEvent_Focus; e.down = focused; input_events.push_back(e);
if (!focused) {
for (int i = 0; i < 5; ++i) mouse_down[i] = false;
for (int i = 0; i < Key_COUNT; ++i) key_down[i] = false;
}
}
void IO::ClearInputEvents() { input_events.clear(); }
FrameMetrics::FrameMetrics()
: frame_number(0), active_windows(0), items_submitted(0), clipped_items(0), id_conflicts(0),
draw_lists(0), draw_commands(0), vertices(0), indices(0), input_events(0), ui_events(0),
animation_states(0) {}
// =====================================================================
// DrawList
// =====================================================================
DrawCmd::DrawCmd()
: clip_rect(-8192, -8192, 8192, 8192), tex_id(0), idx_offset(0), vtx_offset(0),
elem_count(0), effect(DrawEffect_None), effect_amount(0.0f), callback(0), callback_data(0) {}
DrawList::DrawList():cur_tex(0),cur_effect(DrawEffect_None),cur_effect_amount(0.0f),white_uv(){}
void DrawList::Clear() {
vtx.clear(); idx.clear(); cmds.clear(); clip_stack.clear(); texture_stack.clear(); effect_stack.clear(); effect_amount_stack.clear();
cur_tex = 0;
cur_effect = DrawEffect_None;
cur_effect_amount = 0.0f;
}
DrawCmd& DrawList::CurCmd() {
if (cmds.empty()) {
DrawCmd c;
c.clip_rect = clip_stack.empty() ? Vec4(-8192, -8192, 8192, 8192) : clip_stack.back();
c.tex_id = cur_tex; c.idx_offset = (unsigned)idx.size(); c.elem_count = 0;
c.effect = cur_effect; c.effect_amount = cur_effect_amount;
cmds.push_back(c);
}
return cmds.back();
}
static void NewCmd(DrawList* dl) {
DrawCmd c;
c.clip_rect = dl->clip_stack.empty() ? Vec4(-8192, -8192, 8192, 8192) : dl->clip_stack.back();
c.tex_id = dl->cur_tex; c.idx_offset = (unsigned)dl->idx.size(); c.elem_count = 0;
c.effect = dl->cur_effect; c.effect_amount = dl->cur_effect_amount;
dl->cmds.push_back(c);
}
void DrawList::PushClipRect(const Vec4& r, bool intersect_with_current) {
Vec4 clipped = r;
if (intersect_with_current && !clip_stack.empty()) {
const Vec4& parent = clip_stack.back();
clipped.x = Max(clipped.x, parent.x); clipped.y = Max(clipped.y, parent.y);
clipped.z = Min(clipped.z, parent.z); clipped.w = Min(clipped.w, parent.w);
if (clipped.z < clipped.x) clipped.z = clipped.x;
if (clipped.w < clipped.y) clipped.w = clipped.y;
}
clip_stack.push_back(clipped); NewCmd(this);
}
void DrawList::PopClipRect() { if (!clip_stack.empty()) clip_stack.pop_back(); NewCmd(this); }
void DrawList::PrimReserve(int idx_count, int vtx_count) {
if (idx_count <= 0 || vtx_count < 0) return;
size_t required_indices = idx.size() + (size_t)idx_count;
size_t required_vertices = vtx.size() + (size_t)vtx_count;
if (required_indices > idx.capacity())
idx.reserve(std::max(required_indices, idx.capacity() + idx.capacity() / 2 + 256));
if (required_vertices > vtx.capacity())
vtx.reserve(std::max(required_vertices, vtx.capacity() + vtx.capacity() / 2 + 128));
CurCmd().elem_count += (unsigned int)idx_count;
}
void DrawList::PushEffect(int effect, float amount) {
effect_stack.push_back(cur_effect); effect_amount_stack.push_back(cur_effect_amount);
cur_effect = effect;
cur_effect_amount = amount;
NewCmd(this);
}
void DrawList::PopEffect() {
if (effect_stack.empty() || effect_amount_stack.empty()) return;
cur_effect = effect_stack.back(); effect_stack.pop_back();
cur_effect_amount = effect_amount_stack.back(); effect_amount_stack.pop_back();
NewCmd(this);
}
void DrawList::PushTexture(TextureID texture_id) {
texture_stack.push_back(cur_tex); cur_tex = texture_id; NewCmd(this);
}
void DrawList::PopTexture() {
if (texture_stack.empty()) return;
cur_tex = texture_stack.back(); texture_stack.pop_back(); NewCmd(this);
}
void DrawList::AddCallback(DrawCallback callback_value, void* data) {
if (!callback_value) return;
DrawCmd command;
command.clip_rect = clip_stack.empty() ? Vec4(-8192, -8192, 8192, 8192) : clip_stack.back();
command.tex_id = cur_tex; command.idx_offset = (unsigned)idx.size();
command.callback = callback_value; command.callback_data = data;
cmds.push_back(command); NewCmd(this);
}
static bool SameRect(const Vec4& a, const Vec4& b) {
return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
}
void DrawList::CompactCommands() {
std::vector<DrawCmd> compact;
compact.reserve(cmds.size());
for (size_t i = 0; i < cmds.size(); ++i) {
const DrawCmd& command = cmds[i];
if (!command.callback && command.elem_count == 0) continue;
if (!compact.empty() && !command.callback && !compact.back().callback &&
compact.back().tex_id == command.tex_id && compact.back().effect == command.effect &&
compact.back().vtx_offset == command.vtx_offset &&
compact.back().effect_amount == command.effect_amount && SameRect(compact.back().clip_rect, command.clip_rect) &&
compact.back().idx_offset + compact.back().elem_count == command.idx_offset) {
compact.back().elem_count += command.elem_count;
} else compact.push_back(command);
}
cmds.swap(compact);
}
void DrawList::AddRectFilled(const Vec2& a, const Vec2& b, U32 col) {
DrawIdx base = (DrawIdx)vtx.size();
PrimReserve(6, 4);
DrawVert v; v.col = col; v.uv = white_uv;
v.pos = Vec2(a.x, a.y); vtx.push_back(v);
v.pos = Vec2(b.x, a.y); vtx.push_back(v);
v.pos = Vec2(b.x, b.y); vtx.push_back(v);
v.pos = Vec2(a.x, b.y); vtx.push_back(v);
idx.push_back(base + 0); idx.push_back(base + 1); idx.push_back(base + 2);
idx.push_back(base + 0); idx.push_back(base + 2); idx.push_back(base + 3);
}
void DrawList::AddRectFilledRounded(const Vec2& a, const Vec2& b, U32 col, float radius) {
float w = b.x - a.x, h = b.y - a.y;
if (!FiniteFloat(w) || !FiniteFloat(h) || w <= 0.0f || h <= 0.0f) return;
float r = Min(radius, Min(w, h) * 0.5f);
if (r <= 1.0f) { AddRectFilled(a, b, col); return; }
// A single convex fan keeps translucent fills uniform. Composing strips and
// circles would blend overlapping pixels more than once at every corner.
int corner_segments = (int)(r * 0.35f) + 3;
if (corner_segments < 3) corner_segments = 3;
if (corner_segments > 12) corner_segments = 12;
const int perimeter_count = 4 * (corner_segments + 1);
DrawIdx base = (DrawIdx)vtx.size();
PrimReserve(perimeter_count * 3, perimeter_count + 1);
DrawVert vertex; vertex.col = col; vertex.uv = white_uv;
vertex.pos = Vec2((a.x + b.x) * 0.5f, (a.y + b.y) * 0.5f);
vtx.push_back(vertex);
const Vec2 centers[4] = {
Vec2(b.x - r, a.y + r), Vec2(b.x - r, b.y - r),
Vec2(a.x + r, b.y - r), Vec2(a.x + r, a.y + r)
};
const float half_pi = 1.57079632679f;
for (int corner = 0; corner < 4; ++corner) {
float start = -half_pi + corner * half_pi;
for (int segment = 0; segment <= corner_segments; ++segment) {
float angle = start + half_pi * (float)segment / (float)corner_segments;
vertex.pos = Vec2(centers[corner].x + cosf(angle) * r,
centers[corner].y + sinf(angle) * r);
vtx.push_back(vertex);
}
}
for (int point = 0; point < perimeter_count; ++point) {
idx.push_back(base);
idx.push_back((DrawIdx)(base + 1 + point));
idx.push_back((DrawIdx)(base + 1 + ((point + 1) % perimeter_count)));
}
}
void DrawList::AddRectFilledMultiColor(const Vec2& a, const Vec2& b,
U32 col_tl, U32 col_tr, U32 col_br, U32 col_bl) {
DrawIdx base = (DrawIdx)vtx.size();
PrimReserve(6, 4);
DrawVert v; v.uv = white_uv;
v.pos = Vec2(a.x, a.y); v.col = col_tl; vtx.push_back(v);
v.pos = Vec2(b.x, a.y); v.col = col_tr; vtx.push_back(v);
v.pos = Vec2(b.x, b.y); v.col = col_br; vtx.push_back(v);
v.pos = Vec2(a.x, b.y); v.col = col_bl; vtx.push_back(v);
idx.push_back(base + 0); idx.push_back(base + 1); idx.push_back(base + 2);
idx.push_back(base + 0); idx.push_back(base + 2); idx.push_back(base + 3);
}
void DrawList::AddShadowRect(const Vec2& a, const Vec2& b, U32 col, float radius, float spread) {
const int layers = 5;
for (int i = layers; i >= 1; --i) {
float t = (float)i / (float)layers;
float grow = spread * t;
int alpha = (int)(ColA(col) * (1.0f - t * 0.78f) / layers);
AddRectFilledRounded(Vec2(a.x - grow, a.y - grow), Vec2(b.x + grow, b.y + grow),
ColorWithAlpha(col, alpha), radius + grow);
}
}
void DrawList::AddBackdropBlur(const Vec2& a, const Vec2& b, U32 tint, float radius, float rounding) {
PushEffect(DrawEffect_BackdropBlur, radius);
AddRectFilledRounded(a, b, tint, rounding);
PopEffect();
}
void DrawList::AddQuad(const Vec2& a, const Vec2& b, const Vec2& c, const Vec2& d, U32 col) {
DrawIdx base = (DrawIdx)vtx.size();
PrimReserve(6, 4);
DrawVert v; v.col = col; v.uv = white_uv;
v.pos = a; vtx.push_back(v);
v.pos = b; vtx.push_back(v);
v.pos = c; vtx.push_back(v);
v.pos = d; vtx.push_back(v);
idx.push_back(base + 0); idx.push_back(base + 1); idx.push_back(base + 2);
idx.push_back(base + 0); idx.push_back(base + 2); idx.push_back(base + 3);
}
void DrawList::AddRect(const Vec2& a, const Vec2& b, U32 col, float th) {
AddRectFilled(Vec2(a.x, a.y), Vec2(b.x, a.y + th), col); // top
AddRectFilled(Vec2(a.x, b.y - th), Vec2(b.x, b.y), col); // bottom
AddRectFilled(Vec2(a.x, a.y), Vec2(a.x + th, b.y), col); // left
AddRectFilled(Vec2(b.x - th, a.y), Vec2(b.x, b.y), col); // right
}
void DrawList::AddLine(const Vec2& a, const Vec2& b, U32 col, float th) {
float dx = b.x - a.x, dy = b.y - a.y;
float len = sqrtf(dx * dx + dy * dy);
if (len < 1e-4f) return;
dx /= len; dy /= len;
float nx = -dy * th * 0.5f, ny = dx * th * 0.5f;
AddQuad(Vec2(a.x + nx, a.y + ny), Vec2(b.x + nx, b.y + ny),
Vec2(b.x - nx, b.y - ny), Vec2(a.x - nx, a.y - ny), col);
}
void DrawList::AddTriangleFilled(const Vec2& a, const Vec2& b, const Vec2& c, U32 col) {
DrawIdx base = (DrawIdx)vtx.size();
PrimReserve(3, 3);
DrawVert v; v.col = col; v.uv = white_uv;
v.pos = a; vtx.push_back(v);
v.pos = b; vtx.push_back(v);
v.pos = c; vtx.push_back(v);
idx.push_back(base + 0); idx.push_back(base + 1); idx.push_back(base + 2);
}
void DrawList::AddCircleFilled(const Vec2& c, float r, U32 col, int segs) {
if (r <= 0.0f || segs < 3) return;
for (int i = 0; i < segs; i++) {
float a0 = (float)i / segs * 6.2831853f;
float a1 = (float)(i + 1) / segs * 6.2831853f;
AddTriangleFilled(c, Vec2(c.x + cosf(a0) * r, c.y + sinf(a0) * r),
Vec2(c.x + cosf(a1) * r, c.y + sinf(a1) * r), col);
}
}
void DrawList::AddCircle(const Vec2& c, float r, U32 col, int segs, float thickness) {
if (r <= 0.0f || thickness <= 0.0f || segs < 3) return;
Vec2 previous(c.x + r, c.y);
for (int i = 1; i <= segs; ++i) {
float angle = (float)i / (float)segs * 6.2831853f;
Vec2 point(c.x + cosf(angle) * r, c.y + sinf(angle) * r);
AddLine(previous, point, col, thickness); previous = point;
}
}
void DrawList::AddBezierCubic(const Vec2& p1, const Vec2& p2, const Vec2& p3, const Vec2& p4,
U32 col, float thickness, int segments) {
if (thickness <= 0.0f) return;
if (segments <= 0) {
float length_hint = fabsf(p4.x - p1.x) + fabsf(p4.y - p1.y) +
fabsf(p2.x - p1.x) + fabsf(p3.x - p4.x);
segments = (int)Clamp(length_hint * 0.08f, 8.0f, 48.0f);
}
Vec2 previous = p1;
for (int i = 1; i <= segments; ++i) {
float t = (float)i / (float)segments, u = 1.0f - t;
Vec2 point(u*u*u*p1.x + 3*u*u*t*p2.x + 3*u*t*t*p3.x + t*t*t*p4.x,
u*u*u*p1.y + 3*u*u*t*p2.y + 3*u*t*t*p3.y + t*t*t*p4.y);
AddLine(previous, point, col, thickness); previous = point;
}
}
void DrawList::AddGlyph(float x0, float y0, float x1, float y1,
float u0, float v0, float u1, float v1, U32 col) {
DrawIdx base = (DrawIdx)vtx.size();
PrimReserve(6, 4);
DrawVert v; v.col = col;
v.pos = Vec2(x0, y0); v.uv = Vec2(u0, v0); vtx.push_back(v);
v.pos = Vec2(x1, y0); v.uv = Vec2(u1, v0); vtx.push_back(v);
v.pos = Vec2(x1, y1); v.uv = Vec2(u1, v1); vtx.push_back(v);
v.pos = Vec2(x0, y1); v.uv = Vec2(u0, v1); vtx.push_back(v);
idx.push_back(base + 0); idx.push_back(base + 1); idx.push_back(base + 2);
idx.push_back(base + 0); idx.push_back(base + 2); idx.push_back(base + 3);
}
void DrawList::AddText(const Vec2& pos, U32 col, const char* text, const char* text_end) {
FontAtlas& a = GOG->atlas;
float x = pos.x, y = pos.y;
for (const char* p = text; (text_end ? p < text_end : *p) && *p; ) {
unsigned int cp = DecodeUTF8(p, text_end);
if (cp == '\n') { x = pos.x; y += a.line_height; continue; }
if (cp < 32) continue;
const Glyph* glyph = FindGlyph(cp);
if (!glyph) continue;
if (cp != ' ')
AddGlyph(x + glyph->x0, y + glyph->y0, x + glyph->x1, y + glyph->y1,
glyph->u0, glyph->v0, glyph->u1, glyph->v1, col);
x += glyph->advance;
}
}
DrawDataSnapshot::DrawDataSnapshot(){data.display_pos=Vec2();data.display_size=Vec2();data.total_vtx=data.total_idx=0;}
DrawDataSnapshot::DrawDataSnapshot(const DrawDataSnapshot& other):owned_lists(other.owned_lists),data(other.data){data.lists.clear();for(size_t i=0;i<owned_lists.size();++i)data.lists.push_back(&owned_lists[i]);}
DrawDataSnapshot& DrawDataSnapshot::operator=(const DrawDataSnapshot& other){if(this!=&other){owned_lists=other.owned_lists;data=other.data;data.lists.clear();for(size_t i=0;i<owned_lists.size();++i)data.lists.push_back(&owned_lists[i]);}return *this;}
void DrawDataSnapshot::Clear() { owned_lists.clear(); data.lists.clear(); data.display_pos=Vec2();data.display_size=Vec2();data.total_vtx = data.total_idx = 0; }
void DrawDataSnapshot::Capture(const DrawData* source) {
Clear(); if (!source) return;
data.display_pos = source->display_pos; data.display_size = source->display_size;
data.total_vtx = source->total_vtx; data.total_idx = source->total_idx;
owned_lists.reserve(source->lists.size());
for (size_t i = 0; i < source->lists.size(); ++i) owned_lists.push_back(*source->lists[i]);
data.lists.reserve(owned_lists.size());
for (size_t i = 0; i < owned_lists.size(); ++i) data.lists.push_back(&owned_lists[i]);
}
FontAtlas::FontAtlas():pixels(0),width(0),height(0),line_height(16.0f),ascent(12.0f),tex_id(0),white_uv(){memset(glyphs,0,sizeof(glyphs));memset(glyph_valid,0,sizeof(glyph_valid));}
// =====================================================================
// Style
// =====================================================================
Style::Style() {
window_padding = Vec2(18, 16);
frame_padding = Vec2(10, 6);
item_spacing = Vec2(10, 9);
item_inner_spacing = Vec2(10, 6);
indent_spacing = 24.0f;
scrollbar_size = 10.0f;
grab_min_size = 14.0f;
window_title_height = 0.0f;
window_rounding = 10.0f;
frame_rounding = 6.0f;
shadow_size = 12.0f;
animation_speed = 14.0f;
disabled_alpha = 0.46f;
motion_scale = 1.0f;
colors[Col_Text] = OG_COL32(237, 240, 249, 255);
colors[Col_TextDisabled] = OG_COL32(139, 146, 170, 255);
colors[Col_WindowBg] = OG_COL32(22, 24, 36, 250);
colors[Col_TitleBg] = OG_COL32(27, 29, 43, 255);
colors[Col_TitleBgActive] = OG_COL32(31, 34, 50, 255);
colors[Col_MenuBarBg] = OG_COL32(8, 10, 18, 255);
colors[Col_Border] = OG_COL32(59, 63, 86, 210);
colors[Col_FrameBg] = OG_COL32(38, 41, 58, 255);
colors[Col_FrameBgHovered] = OG_COL32(49, 53, 74, 255);
colors[Col_FrameBgActive] = OG_COL32(58, 62, 86, 255);
colors[Col_Button] = OG_COL32(116, 92, 255, 255);
colors[Col_ButtonHovered] = OG_COL32(133, 113, 255, 255);
colors[Col_ButtonActive] = OG_COL32(96, 73, 235, 255);
colors[Col_Header] = OG_COL32(35, 38, 55, 255);
colors[Col_HeaderHovered] = OG_COL32(48, 51, 72, 255);
colors[Col_HeaderActive] = OG_COL32(56, 59, 82, 255);
colors[Col_CheckMark] = OG_COL32(114, 226, 204, 255);
colors[Col_SliderGrab] = OG_COL32(126, 105, 255, 255);
colors[Col_SliderGrabActive] = OG_COL32(139, 238, 218, 255);
colors[Col_Separator] = OG_COL32(110, 110, 128, 128);
colors[Col_ResizeGrip] = OG_COL32(66, 150, 250, 51);
colors[Col_ResizeGripHovered] = OG_COL32(66, 150, 250, 171);
colors[Col_ResizeGripActive] = OG_COL32(66, 150, 250, 242);
colors[Col_ScrollbarBg] = OG_COL32(5, 5, 5, 135);
colors[Col_ScrollbarGrab] = OG_COL32(79, 79, 79, 255);
colors[Col_PlotLines] = OG_COL32(114, 226, 204, 255);
colors[Col_PlotHistogram] = OG_COL32(126, 105, 255, 255);
colors[Col_WindowShadow] = OG_COL32(0, 0, 0, 150);
colors[Col_GradientStart] = OG_COL32(126, 105, 255, 255);
colors[Col_GradientEnd] = OG_COL32(91, 210, 231, 255);
colors[Col_CodeBg] = OG_COL32(12, 14, 24, 255);
colors[Col_Link] = OG_COL32(111, 205, 255, 255);
colors[Col_Success] = OG_COL32(114, 226, 204, 255);
colors[Col_Warning] = OG_COL32(247, 193, 96, 255);
colors[Col_NodeBg] = OG_COL32(30, 33, 49, 245);
colors[Col_NodeTitle] = OG_COL32(83, 67, 172, 255);
colors[Col_NodeGrid] = OG_COL32(74, 79, 105, 80);
colors[Col_NodeLink] = OG_COL32(114, 226, 204, 255);
colors[Col_Info] = OG_COL32(91, 188, 255, 255);
colors[Col_Error] = OG_COL32(238, 82, 106, 255);
colors[Col_FocusRing] = OG_COL32(153, 137, 255, 255);
colors[Col_Selection] = OG_COL32(116, 92, 255, 105);
colors[Col_ChildBg] = OG_COL32(15, 17, 27, 190);
colors[Col_DragDropTarget] = OG_COL32(247, 193, 96, 255);
colors[Col_ModalDim] = OG_COL32(3, 4, 9, 150);
}
Style GetBuiltinTheme(ThemePreset preset) {
Style s;
if (preset == Theme_Dark) return s;
if (preset == Theme_HighContrast) {
s.window_rounding = 3.0f; s.frame_rounding = 2.0f; s.shadow_size = 5.0f;
s.colors[Col_Text] = OG_COL32(255,255,255,255);
s.colors[Col_TextDisabled] = OG_COL32(198,198,210,255);
s.colors[Col_WindowBg] = OG_COL32(7,8,12,255);
s.colors[Col_TitleBg] = s.colors[Col_TitleBgActive] = OG_COL32(15,16,23,255);
s.colors[Col_FrameBg] = OG_COL32(28,30,38,255);
s.colors[Col_FrameBgHovered] = OG_COL32(48,51,63,255);
s.colors[Col_FrameBgActive] = OG_COL32(64,68,82,255);
s.colors[Col_Button] = OG_COL32(0,112,255,255);
s.colors[Col_ButtonHovered] = OG_COL32(42,142,255,255);
s.colors[Col_ButtonActive] = OG_COL32(0,83,210,255);
s.colors[Col_Border] = OG_COL32(235,237,245,245);
s.colors[Col_CheckMark] = OG_COL32(75,255,184,255);
s.colors[Col_FocusRing] = OG_COL32(255,225,64,255);
s.colors[Col_Selection] = OG_COL32(0,112,255,150);
return s;
}
s.colors[Col_Text] = OG_COL32(28, 31, 43, 255);
s.colors[Col_TextDisabled] = OG_COL32(104, 110, 130, 255);
s.colors[Col_WindowBg] = OG_COL32(244, 246, 252, 252);
s.colors[Col_TitleBg] = OG_COL32(233, 236, 246, 255);
s.colors[Col_TitleBgActive] = OG_COL32(239, 241, 250, 255);
s.colors[Col_MenuBarBg] = OG_COL32(224, 228, 240, 255);
s.colors[Col_Border] = OG_COL32(195, 201, 218, 220);
s.colors[Col_FrameBg] = OG_COL32(224, 228, 240, 255);
s.colors[Col_FrameBgHovered] = OG_COL32(211, 216, 233, 255);
s.colors[Col_FrameBgActive] = OG_COL32(199, 205, 225, 255);
s.colors[Col_Header] = OG_COL32(229, 232, 243, 255);
s.colors[Col_HeaderHovered] = OG_COL32(214, 218, 235, 255);
s.colors[Col_HeaderActive] = OG_COL32(202, 207, 228, 255);
s.colors[Col_Separator] = OG_COL32(183, 189, 207, 180);
s.colors[Col_ScrollbarBg] = OG_COL32(225, 228, 237, 180);
s.colors[Col_ScrollbarGrab] = OG_COL32(166, 173, 196, 255);
s.colors[Col_WindowShadow] = OG_COL32(39, 45, 70, 90);
s.colors[Col_CodeBg] = OG_COL32(224, 227, 238, 255);
s.colors[Col_NodeBg] = OG_COL32(250, 251, 255, 250);
s.colors[Col_NodeTitle] = OG_COL32(118, 92, 255, 255);
s.colors[Col_NodeGrid] = OG_COL32(148, 155, 180, 80);
s.colors[Col_NodeLink] = OG_COL32(43, 184, 159, 255);
s.colors[Col_ChildBg] = OG_COL32(235, 238, 247, 210);
s.colors[Col_ModalDim] = OG_COL32(34, 38, 55, 92);
return s;
}
static void ScaleStyleMetrics(Style& s, float scale) {
s.window_padding.x *= scale; s.window_padding.y *= scale;
s.frame_padding.x *= scale; s.frame_padding.y *= scale;
s.item_spacing.x *= scale; s.item_spacing.y *= scale;
s.item_inner_spacing.x *= scale; s.item_inner_spacing.y *= scale;
s.indent_spacing *= scale;
s.scrollbar_size *= scale;
s.grab_min_size *= scale;
s.window_rounding *= scale;
s.frame_rounding *= scale;
s.shadow_size *= scale;
}
void SetTheme(ThemePreset preset, float transition_seconds) {
Context& g = *GOG;
g.theme_from = g.style;
g.theme_target = GetBuiltinTheme(preset);
if (g.ui_scale != 1.0f) ScaleStyleMetrics(g.theme_target, g.ui_scale);
g.theme_elapsed = 0.0f;
g.theme_duration = (g.io.config_reduced_motion || g.style.motion_scale <= 0.0f)
? 0.0f : Max(transition_seconds * g.style.motion_scale, 0.0f);
g.theme_transitioning = g.theme_duration > 0.0f;
g.theme_preset = preset;
if (!g.theme_transitioning) g.style = g.theme_target;
}
bool IsThemeTransitioning() { return GOG && GOG->theme_transitioning; }
void SetUIScale(float scale) {
if (!GOG) return;
scale = Clamp(scale, 0.50f, 4.0f);
Context& g = *GOG;
if (fabsf(scale - g.ui_scale) < 0.001f) return;
float ratio = scale / g.ui_scale;
ScaleStyleMetrics(g.style, ratio);
ScaleStyleMetrics(g.theme_from, ratio);
ScaleStyleMetrics(g.theme_target, ratio);
for (size_t i = 0; i < g.windows.size(); ++i) {
g.windows[i]->pos.x *= ratio; g.windows[i]->pos.y *= ratio;
g.windows[i]->size_full.x *= ratio; g.windows[i]->size_full.y *= ratio;
}
g.ui_scale = scale;
}
float GetUIScale() { return GOG ? GOG->ui_scale : 1.0f; }
void SetReducedMotion(bool reduced) { if (GOG) GOG->io.config_reduced_motion = reduced; }
bool IsReducedMotion() { return GOG && GOG->io.config_reduced_motion; }
bool IsKeyDown(Key key) { return GOG && (int)key > 0 && (int)key < Key_COUNT && GOG->io.key_down[(int)key]; }
bool IsKeyPressed(Key key, bool repeat) {
if (!GOG || (int)key <= 0 || (int)key >= Key_COUNT) return false;
if (GOG->key_consumed[(int)key]) return false;
if (GOG->key_pressed[(int)key]) return true;
if (!repeat || !GOG->io.key_down[(int)key]) return false;
float held=GOG->key_down_duration[(int)key],previous=held-GOG->io.delta_time;
if(held<0.35f)return false;int current_tick=(int)((held-0.35f)/0.06f),previous_tick=previous<0.35f?-1:(int)((previous-0.35f)/0.06f);return current_tick!=previous_tick;
}
bool IsKeyPressed(int key) { return IsKeyPressed((Key)key, false); }
bool IsMouseDown(int button) { return GOG && button >= 0 && button < 5 && GOG->io.mouse_down[button]; }
bool IsMouseClicked(int button) { return GOG && button >= 0 && button < 5 && GOG->mouse_clicked[button]; }
bool IsMouseReleased(int button) { return GOG && button >= 0 && button < 5 && GOG->mouse_released[button]; }
Vec2 GetMousePos() { return GOG ? GOG->io.mouse_pos : Vec2(); }
Vec2 GetMouseDragDelta(int button, float lock_threshold) {
if (!GOG || button < 0 || button >= 5 || !GOG->io.mouse_down[button]) return Vec2();
Vec2 delta(GOG->io.mouse_pos.x - GOG->mouse_clicked_pos[button].x, GOG->io.mouse_pos.y - GOG->mouse_clicked_pos[button].y);
float threshold = lock_threshold < 0.0f ? 0.0f : lock_threshold;
return delta.x * delta.x + delta.y * delta.y >= threshold * threshold ? delta : Vec2();
}
void PushStyleColor(int color_index, U32 color) {
if (!GOG || color_index < 0 || color_index >= Col_COUNT) return;
GOG->style_color_stack.push_back(StyleColorBackup(color_index, GOG->style.colors[color_index]));
GOG->style.colors[color_index] = color;
}
void PopStyleColor(int count) {
if (!GOG) return;
while (count-- > 0 && !GOG->style_color_stack.empty()) {
StyleColorBackup backup = GOG->style_color_stack.back(); GOG->style_color_stack.pop_back();
GOG->style.colors[backup.index] = backup.value;
}
}
static float* StyleVarFloat(Style& style, StyleVar var) {
switch (var) {
case StyleVar_IndentSpacing: return &style.indent_spacing;
case StyleVar_ScrollbarSize: return &style.scrollbar_size;
case StyleVar_GrabMinSize: return &style.grab_min_size;
case StyleVar_WindowRounding: return &style.window_rounding;
case StyleVar_FrameRounding: return &style.frame_rounding;
case StyleVar_ShadowSize: return &style.shadow_size;
case StyleVar_AnimationSpeed: return &style.animation_speed;
case StyleVar_DisabledAlpha: return &style.disabled_alpha;
case StyleVar_MotionScale: return &style.motion_scale;
default: return 0;
}
}
static Vec2* StyleVarVec2(Style& style, StyleVar var) {
switch (var) {
case StyleVar_WindowPadding: return &style.window_padding;
case StyleVar_FramePadding: return &style.frame_padding;
case StyleVar_ItemSpacing: return &style.item_spacing;
case StyleVar_ItemInnerSpacing: return &style.item_inner_spacing;
default: return 0;
}
}
void PushStyleVar(StyleVar var, float value) {
if (!GOG) return; float* target = StyleVarFloat(GOG->style, var); if (!target) return;
StyleVarBackup backup; backup.index = var; backup.float_value = *target; backup.is_vec2 = false;
GOG->style_var_stack.push_back(backup); *target = value;
}
void PushStyleVar(StyleVar var, const Vec2& value) {
if (!GOG) return; Vec2* target = StyleVarVec2(GOG->style, var); if (!target) return;
StyleVarBackup backup; backup.index = var; backup.vec_value = *target; backup.is_vec2 = true;
GOG->style_var_stack.push_back(backup); *target = value;
}
void PopStyleVar(int count) {
if (!GOG) return;
while (count-- > 0 && !GOG->style_var_stack.empty()) {
StyleVarBackup backup = GOG->style_var_stack.back(); GOG->style_var_stack.pop_back();
if (backup.is_vec2) { Vec2* target = StyleVarVec2(GOG->style, backup.index); if (target) *target = backup.vec_value; }
else { float* target = StyleVarFloat(GOG->style, backup.index); if (target) *target = backup.float_value; }
}
}
void BeginDisabled(bool disabled) { if (GOG) { GOG->disabled_stack.push_back(disabled); if (disabled) ++GOG->disabled_depth; } }
void EndDisabled() {
if (!GOG || GOG->disabled_stack.empty()) return;
bool disabled = GOG->disabled_stack.back(); GOG->disabled_stack.pop_back();
if (disabled && GOG->disabled_depth > 0) --GOG->disabled_depth;
}
void PushItemWidth(float width) { if (GOG) GOG->item_width_stack.push_back(width); }
void PopItemWidth() { if (GOG && !GOG->item_width_stack.empty()) GOG->item_width_stack.pop_back(); }
void SetNextItemWidth(float width) { if (GOG) { GOG->next_item_width = width; GOG->next_item_width_set = true; } }
Context::Context() {
cur_window = hovered_window = moving_window = nav_window = modal_window = 0;
active_id = 0; text_active_id = 0; active_id_window = text_active_id_window = 0; hovered_id = 0;
frame_count = 0; focus_counter = 0; time = 0; framerate_acc = 60.0f;
focus_lost_this_frame = false;
for (int i = 0; i < 5; i++) {
mouse_down_prev[i] = mouse_clicked[i] = mouse_released[i] = false;
mouse_clicked_pos[i] = mouse_released_pos[i] = Vec2();
mouse_clicked_mods[i] = mouse_released_mods[i] = 0;
}
for (int i = 0; i < Key_COUNT; ++i) {
key_down_prev[i] = key_pressed[i] = key_consumed[i] = false;
key_pressed_mods[i] = 0;
key_down_duration[i] = -1.0f;
}
next_pos_set = next_size_set = next_constraints_set = false;
next_pos_cond = next_size_cond = Cond_Once;
next_size_min = Vec2(0, 0); next_size_max = Vec2(FLT_MAX, FLT_MAX);
atlas.pixels = 0; atlas.width=atlas.height=0;atlas.line_height=16.0f;atlas.ascent=12.0f;atlas.tex_id = 0;atlas.white_uv=Vec2();
memset(atlas.glyphs,0,sizeof(atlas.glyphs));memset(atlas.glyph_valid,0,sizeof(atlas.glyph_valid));
theme_from = style;
theme_target = style;
theme_elapsed = 0.0f;
theme_duration = 0.0f;
theme_transitioning = false;
theme_preset = Theme_Dark;
ui_scale = 1.0f;
nav_id = nav_activate_id = 0; nav_activate_key = Key_None;
disabled_depth = 0; next_item_width = 0.0f; next_item_width_set = false;
drag_drop_active = drag_drop_target = drag_drop_delivered = false;
debug_log_callback = 0; debug_log_user_data = 0;
}
StreamingSeries::StreamingSeries(int capacity)
: values_((size_t)Max((float)capacity, 1.0f)), head_(0), count_(0) {}
void StreamingSeries::Push(float value) {
if (!FiniteFloat(value)) return;
values_[(size_t)head_] = value;
head_ = (head_ + 1) % (int)values_.size();
if (count_ < (int)values_.size()) ++count_;
}
void StreamingSeries::Clear() { head_ = 0; count_ = 0; }
int StreamingSeries::Size() const { return count_; }
int StreamingSeries::Capacity() const { return (int)values_.size(); }
void StreamingSeries::GetOrdered(std::vector<float>& out) const {
out.resize((size_t)count_);
int start = (head_ - count_ + (int)values_.size()) % (int)values_.size();
for (int i = 0; i < count_; ++i)
out[(size_t)i] = values_[(size_t)((start + i) % (int)values_.size())];
}
// =====================================================================
// Context lifecycle
// =====================================================================
Context* CreateContext() {
Context* context = new Context();
SetCurrentContext(context);
return context;
}
void DestroyContext(Context* context) {
if (!context) context = GOG;
if (!context) return;
for (size_t i = 0; i < context->windows.size(); i++) delete context->windows[i];
CleanupContextState(context);
if (GOG == context) GOG = 0;
delete context;
}
static Window* FindWindow(const char* name) {
ID id = HashLabelID(name, 0);
for (size_t i = 0; i < GOG->windows.size(); i++)
if (GOG->windows[i]->id == id) return GOG->windows[i];
return 0;
}
static Window* CreateWindowObj(const char* name) {
Window* w = new Window();
w->name = name;
w->id = HashLabelID(name, 0);
// cascade newly created windows
int n = (int)GOG->windows.size();
w->pos = Vec2(40.0f + n * 28.0f, 40.0f + n * 28.0f);
w->size_full = Vec2(380, 420);
w->focus_order = ++GOG->focus_counter;
GOG->windows.push_back(w);
return w;
}
static void FocusWindow(Window* w) {
if (!(w->flags & WindowFlags_NoBringToFrontOnFocus)) w->focus_order = ++GOG->focus_counter;
GOG->nav_window = w;
}
// =====================================================================
// Frame lifecycle
// =====================================================================
enum InputModifierBits {
InputMod_Shift = 1 << 0,
InputMod_Ctrl = 1 << 1,
InputMod_Alt = 1 << 2
};
static int ModifierMask(const bool key_state[Key_COUNT]) {
int modifiers = 0;
if (key_state[Key_Shift]) modifiers |= InputMod_Shift;
if (key_state[Key_Ctrl]) modifiers |= InputMod_Ctrl;
if (key_state[Key_Alt]) modifiers |= InputMod_Alt;
return modifiers;
}
static void UpdateInputTransitions(Context& g) {
bool key_state[Key_COUNT], key_seen[Key_COUNT];
bool mouse_state[5], mouse_seen[5];
g.focus_lost_this_frame = false;
for (int key = 0; key < Key_COUNT; ++key) {
key_state[key] = g.key_down_prev[key]; key_seen[key] = false;
g.key_pressed[key] = false; g.key_consumed[key] = false; g.key_pressed_mods[key] = 0;
}
for (int button = 0; button < 5; ++button) {
mouse_state[button] = g.mouse_down_prev[button]; mouse_seen[button] = false;
g.mouse_clicked[button] = g.mouse_released[button] = false;
g.mouse_clicked_mods[button] = g.mouse_released_mods[button] = 0;
}
// Replay queued transitions so a complete down/up pulse between two frames
// remains observable even though IO::key_down/mouse_down hold only the final state.
for (size_t i = 0; i < g.io.input_events.size(); ++i) {
const InputEvent& event = g.io.input_events[i];
if (event.type == InputEvent_Key && event.key_or_button > 0 && event.key_or_button < Key_COUNT) {
int key = event.key_or_button; key_seen[key] = true;
bool was_down = key_state[key];
key_state[key] = event.down;
if (event.down && !was_down && !g.key_pressed[key]) {
g.key_pressed[key] = true;
g.key_pressed_mods[key] = ModifierMask(key_state);
}
} else if (event.type == InputEvent_MouseButton && event.key_or_button >= 0 && event.key_or_button < 5) {
int button = event.key_or_button; mouse_seen[button] = true;
if (event.down && !mouse_state[button]) {
g.mouse_clicked[button] = true;
g.mouse_clicked_pos[button] = Vec2(event.x, event.y);
g.mouse_clicked_mods[button] = ModifierMask(key_state);
}
if (!event.down && mouse_state[button]) {
g.mouse_released[button] = true;
g.mouse_released_pos[button] = Vec2(event.x, event.y);
g.mouse_released_mods[button] = ModifierMask(key_state);
}
mouse_state[button] = event.down;
} else if (event.type == InputEvent_Focus && !event.down) {
g.focus_lost_this_frame = true;
for (int key = 0; key < Key_COUNT; ++key) {
key_state[key] = false; key_seen[key] = true; g.key_pressed[key] = false;
}
for (int button = 0; button < 5; ++button) {
if (mouse_state[button]) {
g.mouse_released[button] = true;
g.mouse_released_pos[button] = g.io.mouse_pos;
g.mouse_released_mods[button] = ModifierMask(key_state);
}