-
Notifications
You must be signed in to change notification settings - Fork 13
/
vui.c
5784 lines (4976 loc) · 201 KB
/
vui.c
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
#ifndef VUI_H
#include "vui.h"
#endif
#include <stddef.h>
#include <stdarg.h>
#include <signal.h>
// ===========================================================================================
//
//
// Internal
//
//
// ===========================================================================================
#define _vui_ctrls_init_cap 4096
#define _VuiArenaAlctor_arena_size 8192
typedef struct {
void* next;
} _VuiArenaHeader;
typedef struct {
void* arenas_head;
void* arena;
uint32_t pos;
} _VuiArenaAlctor;
void _VuiArenaAlctor_init(_VuiArenaAlctor* alctor) {
*alctor = (_VuiArenaAlctor){0};
alctor->arenas_head = vui_mem_alloc(_vui.allocator, _VuiArenaAlctor_arena_size, 1);
memset(alctor->arenas_head, 0, _VuiArenaAlctor_arena_size);
alctor->arena = alctor->arenas_head;
}
void _VuiArenaAlctor_reset(_VuiArenaAlctor* alctor) {
alctor->arena = alctor->arenas_head;
alctor->pos = sizeof(_VuiArenaHeader);
memset(alctor->arena + alctor->pos, 0, _VuiArenaAlctor_arena_size - alctor->pos);
}
void* _VuiArenaAlctor_alloc(_VuiArenaAlctor* alctor, uintptr_t size, uintptr_t align) {
while (1) {
// an allocation gets the pointer by adding the position to the start of the buffer.
void* ptr = vui_ptr_add(alctor->arena, alctor->pos);
// rounds up the pointer so it aligned as requested.
ptr = vui_ptr_round_up_align(ptr, align);
uint32_t next_pos = vui_ptr_diff(ptr, alctor->arena) + size;
// checks to see if it fits in the linear buffer.
if (next_pos <= _VuiArenaAlctor_arena_size) {
// and just increments the position for the next allocation.
alctor->pos = next_pos;
return ptr;
} else {
// allocate a new arena
void* arena = vui_mem_alloc(_vui.allocator, _VuiArenaAlctor_arena_size, 1);
// add it to the arena link list
((_VuiArenaHeader*)alctor->arena)->next = arena;
// now make it the current arena and zero its memory
alctor->arena = arena;
alctor->pos = sizeof(_VuiArenaHeader);
memset(alctor->arena, 0, _VuiArenaAlctor_arena_size);
}
}
}
typedef struct {
float x;
float y;
float offset_x;
float offset_y;
float wheel_offset_x;
float wheel_offset_y;
VuiMouseButtons buttons_is_pressed;
VuiMouseButtons buttons_has_been_pressed;
VuiMouseButtons buttons_has_been_released;
} _VuiMouse;
typedef uint8_t _VuiInputBoxType;
enum {
_VuiInputBoxType_text,
_VuiInputBoxType_float,
_VuiInputBoxType_u32,
_VuiInputBoxType_s32,
};
#define _vui_input_box_cap 16
typedef struct VuiRenderLayer VuiRenderLayer;
struct VuiRenderLayer {
VuiStk(VuiRenderCmd) cmds;
VuiStk(VuiVertex) verts;
VuiStk(VuiVertexIdx) indices;
};
typedef struct {
VuiCtrlId root_ctrl_id;
VuiVec2 size;
VuiCtrlId focused_ctrl_id;
VuiStk(char) text;
VuiStk(VuiRenderLayer) render_layers;
VuiWindowRender render;
} _VuiWindow;
typedef uint8_t VuiCtrlAttrType;
enum {
VuiCtrlAttrType_float,
VuiCtrlAttrType_vec2,
VuiCtrlAttrType_align,
VuiCtrlAttrType_bool,
VuiCtrlAttrType_image_scale_mode,
};
typedef struct VuiCtrlAttrChange VuiCtrlAttrChange;
struct VuiCtrlAttrChange {
VuiCtrlAttrChange* prev;
VuiCtrlAttrValue value;
};
typedef uint32_t _VuiFlags;
enum {
_VuiFlags_out_of_memory = 0x1,
_VuiFlags_pixel_snapping = 0x2,
_VuiFlags_right_to_left = 0x4,
};
typedef struct _VuiImage _VuiImage;
struct _VuiImage {
VuiImage inner;
uint16_t counter;
};
typedef_VuiPool(_VuiImage);
typedef struct _VuiCtrl _VuiCtrl;
struct _VuiCtrl {
VuiCtrl inner;
uint16_t counter;
};
typedef_VuiPool(_VuiCtrl);
typedef struct {
VuiPositionTextFn position_text_fn;
void* position_text_userdata;
void* allocator;
_VuiWindow* windows;
uint16_t windows_count;
_VuiFlags flags;
VuiWindowId mouse_focused_window_id;
VuiWindowId focused_window_id;
VuiCtrlId mouse_scroll_focused_ctrl_id;
VuiCtrlId mouse_focused_ctrl_id;
VuiPool(_VuiCtrl) ctrl_pool;
VuiPool(_VuiImage) image_pool;
_VuiArenaAlctor frame_data_alctor;
struct {
_VuiMouse mouse;
VuiInputActions actions;
VuiBool is_mouse_over_ctrl;
struct {
char* string;
uint32_t string_len;
uint32_t string_cap;
uint32_t cursor_idx;
uint32_t cursor_max_last_unchanged_column_idx;
// signed offset from the cursor_idx.
// cursor_idx to cursor_idx + select_offset
// will be the selection range.
int32_t select_offset;
_VuiInputBoxType type;
VuiBool has_changed;
VuiBool is_multiline;
VuiBool has_cursor_moved;
VuiBool has_cursor_moved_last_frame;
} focused_text_box;
//
// non text box related data.
struct {
uint32_t string_len;
uint32_t edit_string_len;
char string[_vui_input_box_cap];
char edit_string[_vui_input_box_cap];
} input_box;
} input;
//
// the data that is used when building the UI tree.
// this is the first stage of the pipeline.
struct {
_VuiWindow* w;
uint32_t frame_idx;
float dt;
VuiCtrlId parent_ctrl_id;
VuiCtrlId sibling_prev_ctrl_id;
float fill_portion_width;
float fill_portion_height;
VuiCtrlAttrChange* ctrl_attr_change_list_heads[VuiCtrlAttr_COUNT];
VuiCtrlAttrs ctrl_attrs;
VuiVec2* mouse_scroll_focused_content_offset;
VuiVec2* mouse_scroll_focused_size;
VuiStk(VuiBool) disabled_stack;
VuiStk(VuiCtrlId) popover_ctrl_ids;
} build;
//
// the data that is used when rendering the UI tree.
// this is the third stage of the pipeline.
struct {
_VuiWindow* w;
VuiRect clip_rect;
uint32_t layer_idx;
VuiStk(VuiVec2) path_points;
} render;
} _Vui;
_Vui _vui = {0};
VuiCtrlAttrType VuiCtrlAttr_types[VuiCtrlAttr_COUNT] = {
[VuiCtrlAttr_width] = VuiCtrlAttrType_float,
[VuiCtrlAttr_width_min] = VuiCtrlAttrType_float,
[VuiCtrlAttr_width_max] = VuiCtrlAttrType_float,
[VuiCtrlAttr_height] = VuiCtrlAttrType_float,
[VuiCtrlAttr_height_min] = VuiCtrlAttrType_float,
[VuiCtrlAttr_height_max] = VuiCtrlAttrType_float,
[VuiCtrlAttr_offset] = VuiCtrlAttrType_vec2,
[VuiCtrlAttr_align] = VuiCtrlAttrType_align,
[VuiCtrlAttr_layout_spacing] = VuiCtrlAttrType_float,
[VuiCtrlAttr_layout_wrap_spacing] = VuiCtrlAttrType_float,
[VuiCtrlAttr_layout_wrap] = VuiCtrlAttrType_bool,
[VuiCtrlAttr_image_scale_mode] = VuiCtrlAttrType_image_scale_mode,
[VuiCtrlAttr_style_transition_time] = VuiCtrlAttrType_float,
};
uint16_t VuiCtrlAttr_offsets[VuiCtrlAttr_COUNT] = {
[VuiCtrlAttr_width] = offsetof(VuiCtrlAttrs, width),
[VuiCtrlAttr_width_min] = offsetof(VuiCtrlAttrs, width_min),
[VuiCtrlAttr_width_max] = offsetof(VuiCtrlAttrs, width_max),
[VuiCtrlAttr_height] = offsetof(VuiCtrlAttrs, height),
[VuiCtrlAttr_height_min] = offsetof(VuiCtrlAttrs, height_min),
[VuiCtrlAttr_height_max] = offsetof(VuiCtrlAttrs, height_max),
[VuiCtrlAttr_offset] = offsetof(VuiCtrlAttrs, offset),
[VuiCtrlAttr_align] = offsetof(VuiCtrlAttrs, align),
[VuiCtrlAttr_layout_spacing] = offsetof(VuiCtrlAttrs, layout_spacing),
[VuiCtrlAttr_layout_wrap_spacing] = offsetof(VuiCtrlAttrs, layout_wrap_spacing),
[VuiCtrlAttr_layout_wrap] = offsetof(VuiCtrlAttrs, layout_wrap),
[VuiCtrlAttr_image_scale_mode] = offsetof(VuiCtrlAttrs, image_scale_mode),
[VuiCtrlAttr_style_transition_time] = offsetof(VuiCtrlAttrs, style_transition_time),
};
char* VuiCtrlState_strings[VuiCtrlState_COUNT] = {
[VuiCtrlState_default] = "default",
[VuiCtrlState_focused] = "focused",
[VuiCtrlState_active] = "active",
[VuiCtrlState_disabled] = "disabled",
};
void vui_push_disabled(VuiBool enabled) {
VuiBool* t = VuiStk_push(&_vui.build.disabled_stack);
vui_ensure_alloc_ok(t);
*t = enabled;
}
void vui_pop_disabled() {
vui_assert(VuiStk_count(_vui.build.disabled_stack), "vui_pop_disabled has been called more than vui_push_disabled")
VuiStk_pop(_vui.build.disabled_stack);
}
void _vui_push_ctrl_attr(VuiCtrlAttr attr, VuiCtrlAttrValue value) {
//
// allocate a new change to store the old attribute.
VuiCtrlAttrChange* old_attr_change = vui_frame_data_alloc_elmt(VuiCtrlAttrChange);
old_attr_change->prev = _vui.build.ctrl_attr_change_list_heads[attr];
_vui.build.ctrl_attr_change_list_heads[attr] = old_attr_change;
void* ptr = vui_ptr_add(&_vui.build.ctrl_attrs, (uintptr_t)VuiCtrlAttr_offsets[attr]);
VuiCtrlAttrType type = VuiCtrlAttr_types[attr];
//
// store the orignal value in the old_attr_change (history entry)
// and then set the value in the global attributes structure
switch (type) {
case VuiCtrlAttrType_float:
old_attr_change->value.float_ = *(float*)ptr;
*(float*)ptr = value.float_;
break;
case VuiCtrlAttrType_vec2:
old_attr_change->value.vec2 = *(VuiVec2*)ptr;
*(VuiVec2*)ptr = value.vec2;
break;
case VuiCtrlAttrType_align:
old_attr_change->value.align = *(VuiAlign*)ptr;
*(VuiAlign*)ptr = value.align;
break;
case VuiCtrlAttrType_bool:
old_attr_change->value.float_ = *(VuiBool*)ptr;
*(VuiBool*)ptr = value.bool_;
break;
case VuiCtrlAttrType_image_scale_mode:
old_attr_change->value.float_ = *(VuiImageScaleMode*)ptr;
*(VuiImageScaleMode*)ptr = value.image_scale_mode;
break;
}
}
void _vui_pop_ctrl_attr(VuiCtrlAttr attr) {
VuiCtrlAttrChange* old_attr_change = _vui.build.ctrl_attr_change_list_heads[attr];
void* ptr = vui_ptr_add(&_vui.build.ctrl_attrs, (uintptr_t)VuiCtrlAttr_offsets[attr]);
VuiCtrlAttrType type = VuiCtrlAttr_types[attr];
//
// restore the orignal value from the old_attr_change (history entry)
// by storing the value in the global attributes structure
switch (type) {
case VuiCtrlAttrType_float:
*(float*)ptr = old_attr_change->value.float_;
break;
case VuiCtrlAttrType_vec2:
*(VuiVec2*)ptr = old_attr_change->value.vec2;
break;
case VuiCtrlAttrType_align:
*(VuiAlign*)ptr = old_attr_change->value.align;
break;
case VuiCtrlAttrType_bool:
*(VuiBool*)ptr = old_attr_change->value.bool_;
break;
case VuiCtrlAttrType_image_scale_mode:
*(VuiImageScaleMode*)ptr = old_attr_change->value.image_scale_mode;
break;
}
_vui.build.ctrl_attr_change_list_heads[attr] = old_attr_change->prev;
}
// ===========================================================================================
//
//
// memory allocation - element pool implementation
//
//
// ===========================================================================================
static inline void _VuiPool_assert_idx(_VuiPool* pool, uint32_t idx) {
vui_debug_assert(idx < pool->cap, "idx is out of the memory boundary of the pool. idx is '%u' but cap is '%u'", idx, pool->cap);
}
static inline VuiBool _VuiPool_is_allocated(_VuiPool* pool, uint32_t idx) {
_VuiPool_assert_idx(pool, idx);
uint8_t bit = 1 << (idx % 8);
return (((uint8_t*)pool->data)[idx / 8] & bit) == bit;
}
static inline void _VuiPool_set_allocated(_VuiPool* pool, uint32_t idx) {
_VuiPool_assert_idx(pool, idx);
uint8_t bit = 1 << (idx % 8);
((uint8_t*)pool->data)[idx / 8] |= bit;
}
static inline void _VuiPool_set_free(_VuiPool* pool, uint32_t idx) {
_VuiPool_assert_idx(pool, idx);
((uint8_t*)pool->data)[idx / 8] &= ~(1 << (idx % 8));
}
void _VuiPool_reset(_VuiPool* pool, uintptr_t elmt_size) {
//
// set all the bits to 0 so all elements are marked as free
uint8_t* is_alloced_bitset = pool->data;
memset(is_alloced_bitset, 0, pool->elmts_start_byte_idx);
//
// now go through and set up the link list, so every element points to the next.
void* elmts = vui_ptr_add(pool->data, pool->elmts_start_byte_idx);
uintptr_t cap = pool->cap;
for (uintptr_t i = 0; i < cap; i += 1) {
// + 2 instead of 1 because we use id's here and not indexes.
*(uintptr_t*)vui_ptr_add(elmts, i * elmt_size) = i + 2;
}
pool->count = 0;
pool->free_list_head_id = 1;
}
VuiBool _VuiPool_resize_cap(_VuiPool* pool, uint32_t new_cap, uintptr_t elmt_size, uintptr_t elmt_align) {
//
// resizing the pool is fine since we store id's everywhere.
//
uintptr_t bitset_size = pool->elmts_start_byte_idx;
uintptr_t new_bitset_size = (new_cap / 8) + 1 + elmt_align - 1;
uintptr_t new_cap_bytes = ((uintptr_t)new_cap * elmt_size) + new_bitset_size;
void* data = pool->data;
void* new_data = vui_mem_alloc(_vui.allocator, new_cap_bytes, alignof(uint8_t));
if (new_data == NULL) {
_vui.flags |= _VuiFlags_out_of_memory;
return vui_false;
}
//
// zero the new bits of the is_allocated_bitset.
memset(vui_ptr_add(new_data, bitset_size), 0, new_bitset_size - bitset_size);
// then zero all the new elements.
memset(vui_ptr_add(new_data, new_bitset_size + (uintptr_t)pool->cap * elmt_size), 0, ((uintptr_t)(new_cap - pool->cap) * elmt_size));
if (data) {
uintptr_t cap_bytes = ((uintptr_t)pool->cap * elmt_size) + bitset_size;
//
// copy the bitset over to the new buffer
memcpy(new_data, data, bitset_size);
//
// copy the elements to their new location.
void* elmts = vui_ptr_add(data, pool->elmts_start_byte_idx);
void* new_elmts = vui_ptr_add(new_data, new_bitset_size);
memcpy(new_elmts, elmts, (uintptr_t)pool->cap * elmt_size);
//
// deallocate the old buffer.
vui_mem_dealloc(_vui.allocator, data, cap_bytes, alignof(uint8_t));
}
//
// setup the free list, by visiting each element and store an
// index to the next element.
for (uint32_t i = pool->cap; i < new_cap; i += 1) {
*(uint32_t*)vui_ptr_add(vui_ptr_add(new_data, new_bitset_size), i * elmt_size) = i + 2;
}
pool->data = new_data;
pool->free_list_head_id = pool->cap + 1;
pool->cap = new_cap;
pool->elmts_start_byte_idx = new_bitset_size;
return vui_true;
}
VuiBool _VuiPool_reset_and_populate(_VuiPool* pool, void* elmts, uint32_t count, uintptr_t elmt_size, uintptr_t elmt_align) {
_VuiPool_reset(pool, elmt_size);
if (pool->cap < count) {
if (!_VuiPool_resize_cap(pool, vui_max(pool->cap ? pool->cap * 2 : 64, count), elmt_size, elmt_align))
return vui_false;
}
//
// set the elements to allocated.
// the last byte is set manually as only some of the bits will be on.
memset(pool->data, 0xff, count / 8);
uint32_t remaining_count = count % 8;
if (remaining_count) ((uint8_t*)pool->data)[count / 8] = (1 << remaining_count) - 1;
//
// copy the elements and set the values in the pool structure
memcpy(vui_ptr_add(pool->data, pool->elmts_start_byte_idx), elmts, (uintptr_t)count * elmt_size);
pool->count = count;
pool->free_list_head_id = count + 1;
return vui_true;
}
void _VuiPool_init(_VuiPool* pool, uint32_t cap, uintptr_t elmt_size, uintptr_t elmt_align) {
uintptr_t bitset_size = (cap / 8) + 1;
uintptr_t elmts_start_byte_idx = (uintptr_t)vui_ptr_round_up_align((void*)bitset_size, elmt_align);
uintptr_t cap_bytes = ((uintptr_t)cap * elmt_size) + elmts_start_byte_idx;
pool->data = vui_mem_alloc(_vui.allocator, cap_bytes, elmt_align);
memset(vui_ptr_add(pool->data, elmts_start_byte_idx), 0, (uintptr_t)cap * elmt_size);
pool->elmts_start_byte_idx = elmts_start_byte_idx;
pool->cap = cap;
_VuiPool_reset(pool, elmt_size);
pool->free_list_head_id = 1;
}
void _VuiPool_deinit(_VuiPool* pool, uintptr_t elmt_size, uintptr_t elmt_align) {
uintptr_t bitset_size = (pool->cap / 8) + 1;
uintptr_t elmts_start_byte_idx = (uintptr_t)vui_ptr_round_up_align((void*)bitset_size, elmt_align);
uintptr_t cap = ((uintptr_t)pool->cap * elmt_size) + elmts_start_byte_idx;
vui_mem_dealloc(_vui.allocator, pool->data, cap, elmt_align);
*pool = (_VuiPool){0};
}
void* _VuiPool_alloc(_VuiPool* pool, VuiPoolId* id_out, uintptr_t elmt_size, uintptr_t elmt_align) {
if (pool->count == pool->cap) {
if (!_VuiPool_resize_cap(pool, pool->cap ? pool->cap * 2 : 64, elmt_size, elmt_align)) {
return NULL;
}
}
//
// allocate an element and remove it from the free list
uintptr_t alloced_id = pool->free_list_head_id;
vui_debug_assert(!_VuiPool_is_allocated(pool, alloced_id - 1), "allocated element is in the free list of the pool");
_VuiPool_set_allocated(pool, alloced_id - 1);
uint32_t* alloced_elmt = (uint32_t*)vui_ptr_add(pool->data, (uintptr_t)pool->elmts_start_byte_idx + ((alloced_id - 1) * elmt_size));
pool->free_list_head_id = *alloced_elmt;
pool->count += 1;
*id_out = alloced_id;
return alloced_elmt;
}
void _VuiPool_assert_id(_VuiPool* pool, uint32_t elmt_id) {
vui_debug_assert(elmt_id, "elmt_id is null, cannot deallocate a null element");
vui_debug_assert(elmt_id <= pool->cap, "elmt_id is out of the memory boundary of the pool. idx is '%u' but cap is '%u'",
elmt_id - 1, pool->cap);
vui_debug_assert(_VuiPool_is_allocated(pool, elmt_id - 1), "cannot get pointer to a element that is not allocated");
}
void _VuiPool_dealloc(_VuiPool* pool, uint32_t elmt_id, uintptr_t elmt_size, uintptr_t elmt_align) {
_VuiPool_assert_id(pool, elmt_id);
uint32_t* elmt_next_free_id = &pool->free_list_head_id;
void* elmts = vui_ptr_add(pool->data, pool->elmts_start_byte_idx);
//
// the free list is stored in low to high order, to try to keep allocations near eachother.
// move up the free list until elmt_id is less than that node.
while (1) {
uint32_t nfi = *elmt_next_free_id;
if (nfi > elmt_id || nfi == pool->cap) break;
elmt_next_free_id = (uint32_t*)vui_ptr_add(elmts, ((uintptr_t)nfi - 1) * elmt_size);
}
_VuiPool_set_free(pool, elmt_id - 1);
// point to the next element in the list
*(uint32_t*)vui_ptr_add(pool->data, (uintptr_t)pool->elmts_start_byte_idx + ((uintptr_t)(elmt_id - 1) * elmt_size)) = *elmt_next_free_id;
// get the previous element to point to this newly deallocated block
*elmt_next_free_id = elmt_id;
pool->count -= 1;
}
void* _VuiPool_id_to_ptr(_VuiPool* pool, VuiPoolId elmt_id, uintptr_t elmt_size) {
_VuiPool_assert_id(pool, elmt_id);
return vui_ptr_add(pool->data, (uintptr_t)pool->elmts_start_byte_idx + ((uintptr_t)(elmt_id - 1) * elmt_size));
}
VuiPoolId _VuiPool_ptr_to_id(_VuiPool* pool, void* ptr, uintptr_t elmt_size) {
return (vui_ptr_diff(ptr, vui_ptr_add(pool->data, (uintptr_t)pool->elmts_start_byte_idx)) / elmt_size) + 1;
}
// ===========================================================================================
//
//
// Misc Helpers
//
//
// ===========================================================================================
noreturn void _vui_abort(const char* file, int line, const char* func, char* assert_test, char* message_fmt, ...) {
if (assert_test) {
fprintf(stderr, "assertion failed: %s\nmessage: ", assert_test);
} else {
fprintf(stderr, "abort reason: ");
}
va_list args;
va_start(args, message_fmt);
vfprintf(stderr, message_fmt, args);
va_end(args);
fprintf(stderr, "\nfile: %s:%d\n%s\n", file, line, func);
abort();
}
uint32_t vui_utf8_codepoint(const char* str, int32_t* out_codepoint) {
uint32_t bytes = 0;
if (0xf0 == (0xf8 & str[0])) {
// 4 byte utf8 codepoint
*out_codepoint = ((0x07 & str[0]) << 18) | ((0x3f & str[1]) << 12) |
((0x3f & str[2]) << 6) | (0x3f & str[3]);
bytes = 4;
} else if (0xe0 == (0xf0 & str[0])) {
// 3 byte utf8 codepoint
*out_codepoint =
((0x0f & str[0]) << 12) | ((0x3f & str[1]) << 6) | (0x3f & str[2]);
bytes = 3;
} else if (0xc0 == (0xe0 & str[0])) {
// 2 byte utf8 codepoint
*out_codepoint = ((0x1f & str[0]) << 6) | (0x3f & str[1]);
bytes = 2;
} else {
// 1 byte utf8 codepoint otherwise
*out_codepoint = str[0];
bytes = 1;
}
return bytes;
}
uint32_t vui_utf8_prev_char(const char* str, uint32_t idx) {
while (idx--) {
if (vui_utf8_is_codepoint_boundary(str[idx])) {
break;
}
}
if (idx == UINT32_MAX)
idx = 0;
return idx;
}
VuiBool vui_utf8_is_codepoint_boundary(char ch) {
// this is bit magic equivalent to: b < 128 || b >= 192
return ch >= -0x40;
}
VuiBool vui_utf8_is_word_delimiter(int32_t codept) {
// all of the control characters are delimiters
if (codept < 32) return vui_true;
char word_delimiters[] = " ,.-!?:;";
int32_t delimiter = 0;
uint32_t delimiter_i = 0;
while (delimiter_i < sizeof(word_delimiters)) {
delimiter_i += vui_utf8_codepoint(&word_delimiters[delimiter_i], &delimiter);
if (delimiter == codept) return vui_true;
}
return vui_false;
}
VuiBool vui_utf8_is_whitespace(int32_t codept) {
char ws_codepts[] = " \t";
int32_t ws = 0;
uint32_t ws_i = 0;
while (ws_i < sizeof(ws_codepts)) {
ws_i += vui_utf8_codepoint(&ws_codepts[ws_i], &ws);
if (ws == codept) return vui_true;
}
return vui_false;
}
VuiVec2 vui_cubic_bezier_curve_interp(VuiVec2 points[4], float progress) {
VuiVec2 tmp_buf[4];
memcpy(tmp_buf, points, sizeof(VuiVec2) * 4);
size_t number_of_points = 4;
while (number_of_points > 1) {
for (size_t i = 0; i < number_of_points - 1; ++i) {
tmp_buf[i].x = vui_lerp(tmp_buf[i + 1].x, tmp_buf[i].x, progress);
tmp_buf[i].y = vui_lerp(tmp_buf[i + 1].y, tmp_buf[i].y, progress);
}
number_of_points -= 1;
}
return tmp_buf[0];
}
// ===========================================================================================
//
//
// Stack - LIFO (inspired by stb stretchy buffer)
// will not work for a type with an alignment greater than alignof(void*)
//
//
// ===========================================================================================
void* _VuiStk_maybe_expand(void** stk_ptr, uint32_t elmts_count, uint32_t elmt_size) {
void* stk = *stk_ptr;
uint32_t new_count = elmts_count;
if (stk == NULL) goto RESIZE_CAP;
_VuiStkHeader* h = _VuiStk_header(stk);
new_count += h->count;
if (new_count > h->cap) {
RESIZE_CAP: {}
// if we have a capacity workout the new capacity by times by 2
// or using the new_count if it is greater the double our current capacity.
uint32_t new_cap = stk != NULL && h->cap ? h->cap * 2 : vui_stk_init_cap;
if (new_count > new_cap)
new_cap = new_count;
if (!_VuiStk_resize_cap(&stk, new_cap, elmt_size)) return NULL;
}
*stk_ptr = stk;
return stk;
}
void* _VuiStk_push_many(void** stk_ptr, uint32_t elmts_count, uint32_t elmt_size) {
void* stk = _VuiStk_maybe_expand(stk_ptr, elmts_count, elmt_size);
if (stk == NULL) return NULL;
_VuiStkHeader* h = _VuiStk_header(stk);
void* elmt = (void*)((char*)stk + (uintptr_t)elmt_size * (uintptr_t)h->count);
h->count += elmts_count;
return elmt;
}
void* _VuiStk_insert_many(void** stk_ptr, uint32_t idx, uint32_t elmts_count, uint32_t elmt_size) {
void* stk = _VuiStk_maybe_expand(stk_ptr, elmts_count, elmt_size);
if (stk == NULL) return NULL;
_VuiStkHeader* h = _VuiStk_header(stk);
void* elmt = (void*)((char*)stk + (uintptr_t)elmt_size * (uintptr_t)idx);
// shift the elements from idx to (idx + elmts_count), to the right
// to make room for the elements
memmove((char*)elmt + (uintptr_t)elmts_count * (uintptr_t)elmt_size, elmt, (uintptr_t)(h->count - idx) * (uintptr_t)elmt_size);
h->count += elmts_count;
return elmt;
}
VuiBool _VuiStk_resize_cap(void** stk_ptr, uint32_t new_cap, uint32_t elmt_size) {
void* stk = *stk_ptr;
if (new_cap == 0) new_cap = vui_stk_init_cap;
uint32_t cap = VuiStk_cap(stk);
if (new_cap == cap) return vui_true;
uintptr_t size = VuiStk_size(stk) + sizeof(_VuiStkHeader);
uintptr_t new_size = (uintptr_t)elmt_size * (uintptr_t)new_cap + sizeof(_VuiStkHeader);
_VuiStkHeader* ptr = vui_mem_realloc(_vui.allocator, stk ? _VuiStk_header(stk) : NULL, size, new_size, alignof(void*));
if (ptr == NULL) return vui_false;
ptr->cap = new_cap;
// if the stk was uninitialized set its size to 0
if (!stk) ptr->count = 0;
// set the new stack pointer.
*stk_ptr = (void*)((char*)ptr + sizeof(_VuiStkHeader));
return vui_true;
}
void _VuiStk_remove_range_shift(void* stk, uint32_t start_idx, uint32_t end_idx, uint32_t elmt_size) {
_VuiStkHeader* h = _VuiStk_header(stk);
vui_assert(start_idx <= h->count, "start idx '%u' must be less than the count of '%u'", start_idx, h->count);
vui_assert(end_idx <= h->count, "end idx '%u' must be less than or equal to count of '%u'", end_idx, h->count);
uintptr_t remove_count = end_idx - start_idx;
void* dst = ((char*)stk + (uintptr_t)start_idx * (uintptr_t)elmt_size);
if (end_idx < h->count) {
void* src = (char*)dst + (remove_count * (uintptr_t)elmt_size);
memmove(dst, src, ((uintptr_t)h->count - ((uintptr_t)start_idx + remove_count)) * (uintptr_t)elmt_size);
}
h->count -= remove_count;
}
// ===========================================================================================
//
//
// Common Types
//
//
// ===========================================================================================
VuiVec2 VuiRect_center(const VuiRect* r) {
return VuiVec2_add(r->left_top, VuiVec2_mul_scalar(VuiRect_size(*r), 0.5));
}
VuiRect VuiRect_clip(const VuiRect* a, const VuiRect* b) {
if (
a->left_top.x >= b->right_bottom.x || a->left_top.y >= b->right_bottom.y ||
b->left_top.x >= a->right_bottom.x || b->left_top.y >= a->right_bottom.y
) {
return VuiRect_zero;
}
return VuiRect_init_v2(VuiVec2_max(a->left_top, b->left_top), VuiVec2_min(a->right_bottom, b->right_bottom));
}
VuiVec2 VuiRect_clip_pt(const VuiRect* rect, VuiVec2 pt) {
return VuiVec2_clamp(pt, rect->left_top, rect->right_bottom);
}
VuiBool VuiRect_intersects(const VuiRect* a, const VuiRect* b) {
return a->left_top.x < b->right_bottom.x &&
b->left_top.x < a->right_bottom.x &&
a->left_top.y < b->right_bottom.y &&
b->left_top.y < a->right_bottom.y;
}
VuiBool VuiRect_intersects_pt(const VuiRect* r, VuiVec2 pt) {
return r->left_top.x <= pt.x && r->right_bottom.x >= pt.x &&
r->left_top.y <= pt.y && r->right_bottom.y >= pt.y;
}
// ===========================================================================================
//
//
// Input
//
//
// ===========================================================================================
void vui_input_set_mouse_pos(float x, float y) {
_vui.input.mouse.offset_x = x - _vui.input.mouse.x;
_vui.input.mouse.offset_y = y - _vui.input.mouse.y;
_vui.input.mouse.x = x;
_vui.input.mouse.y = y;
}
void vui_input_set_mouse_wheel_offset(float wheel_offset_x, float wheel_offset_y) {
_vui.input.mouse.wheel_offset_x = wheel_offset_x;
_vui.input.mouse.wheel_offset_y = wheel_offset_y;
}
void vui_input_set_mouse_button_pressed(VuiMouseButtons buttons) {
_vui.input.mouse.buttons_is_pressed |= buttons;
_vui.input.mouse.buttons_has_been_pressed |= buttons;
}
void vui_input_set_mouse_button_released(VuiMouseButtons buttons) {
_vui.input.mouse.buttons_is_pressed &= ~buttons;
_vui.input.mouse.buttons_has_been_released |= buttons;
}
void vui_input_add_actions(VuiInputActions actions) {
_vui.input.actions |= actions;
}
uint32_t _vui_string_remove_range_shift(char* string, uint32_t string_len, uint32_t start_idx, uint32_t end_idx) {
uint32_t remove_count = end_idx - start_idx;
if (end_idx < string_len) {
char* dst = string + start_idx;
void* src = string + end_idx;
memmove(dst, src, string_len - end_idx);
}
string_len -= end_idx - start_idx;
string[string_len] = '\0';
return string_len;
}
static void _vui_input_text_remove_selected() {
//
// get the selection range
uint32_t start_idx = _vui.input.focused_text_box.cursor_idx;
uint32_t end_idx = _vui.input.focused_text_box.cursor_idx + _vui.input.focused_text_box.select_offset;
if (_vui.input.focused_text_box.select_offset < 0) {
uint32_t tmp = start_idx;
start_idx = end_idx;
end_idx = tmp;
}
//
// remove the selected text from the string by doing a shift remove.
_vui.input.focused_text_box.string_len =
_vui_string_remove_range_shift(_vui.input.focused_text_box.string, _vui.input.focused_text_box.string_len, start_idx, end_idx);
_vui.input.focused_text_box.cursor_idx = start_idx;
_vui.input.focused_text_box.select_offset = 0;
_vui.input.focused_text_box.has_cursor_moved = vui_true;
}
static void _vui_input_text_insert(const char* string, uint32_t string_length) {
char* dst_string = _vui.input.focused_text_box.string;
uint32_t dst_idx = _vui.input.focused_text_box.cursor_idx;
uint32_t dst_idx_end = dst_idx + string_length;
//
// if the string will exceed the capacity then reduce the string length to allow for a null terminator at the end.
if (dst_idx_end >= _vui.input.focused_text_box.string_cap) {
uint32_t amount = (dst_idx_end - _vui.input.focused_text_box.string_cap) + 1;
string_length -= string_length < amount ? string_length : amount;
dst_idx_end = dst_idx + string_length;
}
if (string_length) {
//
// shift the characters to the right of the index over by string_length.
if (dst_idx < _vui.input.focused_text_box.string_len) {
void* src = dst_string + dst_idx;
memmove(src + string_length, src, _vui.input.focused_text_box.string_len - dst_idx);
}
//
// insert the string
memcpy(&_vui.input.focused_text_box.string[_vui.input.focused_text_box.cursor_idx], string, string_length);
_vui.input.focused_text_box.cursor_idx += string_length;
_vui.input.focused_text_box.string_len += string_length;
_vui.input.focused_text_box.string[_vui.input.focused_text_box.string_len] = '\0';
_vui.input.focused_text_box.has_cursor_moved = vui_true;
}
}
void vui_input_add_text(const char* string, uint32_t string_length) {
// ignore if a text box is not focused
if (_vui.input.focused_text_box.string == NULL) return;
_vui.input.focused_text_box.has_changed = vui_true;
if (_vui.input.focused_text_box.select_offset != 0) { // if we are selecting
_vui_input_text_remove_selected();
}
uint32_t codept_size = 0;
for (uint32_t i = 0; i < string_length; i += codept_size) {
int32_t codept = 0;
codept_size = vui_utf8_codepoint(&string[i], &codept);
if (_vui.input.focused_text_box.string_len + codept_size >= _vui.input.focused_text_box.string_cap)
break;
VuiBool copy = vui_false;
switch (_vui.input.focused_text_box.type) {
case _VuiInputBoxType_text: copy = !_vui.input.focused_text_box.is_multiline || codept != '\n'; break;
case _VuiInputBoxType_float:
if (codept == '.') {
// we have a full stop, only insert this after the first character
// and make sure its the only decimal place in the float string.
if (_vui.input.focused_text_box.cursor_idx > 0) {
VuiBool has_full_stop = vui_false;
for (uint32_t idx = 0; idx < _vui.input.focused_text_box.string_len; idx += 1) {
if (_vui.input.focused_text_box.string[idx] == '.') {
has_full_stop = vui_true;
break;
}
}
copy = !has_full_stop;
}
}
if (copy) break;
// fallthrough
case _VuiInputBoxType_s32:
if (codept == '-') {
copy = _vui.input.focused_text_box.string_len == 0 ||
(_vui.input.focused_text_box.cursor_idx == 0 && _vui.input.focused_text_box.string[0] != '-');
if (copy) break;
}
// fallthrough
case _VuiInputBoxType_u32:
copy = codept >= '0' && codept <= '9';
break;
}
//
// copy the byte if it is valid for this type of input box
if (copy) {
_vui_input_text_insert(&string[i], codept_size);
}
}
_vui.input.focused_text_box.string[_vui.input.focused_text_box.string_len] = '\0';
}
VuiVec2 vui_mouse_pos() {
return VuiVec2_init(_vui.input.mouse.x, _vui.input.mouse.y);
}
VuiBool vui_has_mouse_over_ctrl() {
return _vui.input.is_mouse_over_ctrl;
}
VuiBool vui_has_mouse_focused_ctrl() {
return _vui.mouse_focused_ctrl_id != 0;
}
VuiBool vui_has_mouse_scroll_focused_ctrl() {
return _vui.mouse_scroll_focused_ctrl_id != 0;
}
VuiBool vui_has_text_box_focused() {
return _vui.input.focused_text_box.string != NULL;
}
VuiBool vui_ctrl_is_mouse_focused(VuiCtrlId ctrl_id) {
return _vui.mouse_focused_ctrl_id == ctrl_id;
}
VuiBool vui_ctrl_is_focused(VuiCtrlId ctrl_id) {
return _vui.windows[_vui.focused_window_id].focused_ctrl_id == ctrl_id;
}
VuiBool vui_ctrl_is_mouse_scroll_focused(VuiCtrlId ctrl_id) {
return _vui.mouse_scroll_focused_ctrl_id == ctrl_id;
}
void vui_ctrl_set_focused(VuiCtrlId ctrl_id) {
_VuiWindow* w = &_vui.windows[_vui.focused_window_id];
//
// remove focus from the currently focused control.
if (w->focused_ctrl_id) {
VuiCtrl* focused_ctrl = vui_ctrl_get(w->focused_ctrl_id);
focused_ctrl->state_flags &= ~VuiCtrlStateFlags_focused;
}
//
// if a control was supplied, then set it to be focus.
if (ctrl_id) {
VuiCtrl* ctrl = vui_ctrl_get(ctrl_id);
ctrl->state_flags |= VuiCtrlStateFlags_focused;
}
w->focused_ctrl_id = ctrl_id;
if (_vui.input.focused_text_box.string) {
_vui.input.focused_text_box.string = NULL;
_vui.input.focused_text_box.string_len = 0;
_vui.input.focused_text_box.string_cap = 0;
}
}