-
Notifications
You must be signed in to change notification settings - Fork 12
/
vui.h
1620 lines (1426 loc) · 64.2 KB
/
vui.h
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
#define VUI_H
#include <stdint.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
// ===========================================================================================
//
//
// Configuration - compile time flags and constants
//
//
// ===========================================================================================
#define VUI_DEBUG_CTRL_LAYOUT 1
#define vui_debug_ctrl_layout_dump_file_path "/tmp/vui_ctrls"
#define vui_text_box_select_scroll_amount 4.0f
#ifndef VuiCtrlStyle_max_colors
#define VuiCtrlStyle_max_colors 2
#endif
#ifndef VuiCtrlStyle_max_sizes
#define VuiCtrlStyle_max_sizes 2
#endif
#ifndef VuiCtrlStyle_max_children
#define VuiCtrlStyle_max_children 3
#endif
//
// when making custom controls that have custom style entries.
// you can to extend the sizes, colors or children of the VuiCtrlStyle
// by defining the macros below.
// EG:
#if 0
#define inline_VuiCtrlStyleUserExtColors \
struct { \
VuiColor my_button_style_attribute; \
struct { \
VuiColor my_text_box_style_attribute_1; \
VuiColor my_text_box_style_attribute_2; \
} \
}
#endif
#ifndef inline_VuiCtrlStyleUserExtColors
#define inline_VuiCtrlStyleUserExtColors
#endif
#ifndef inline_VuiCtrlStyleUserExtSizes
#define inline_VuiCtrlStyleUserExtSizes
#endif
#ifndef inline_VuiCtrlStyleUserExtChildren
#define inline_VuiCtrlStyleUserExtChildren
#endif
#ifndef inline_VuiCtrlStyleUserExt
#define inline_VuiCtrlStyleUserExt
#endif
#ifndef VuiCtrlStyleUserExt_lerp
#define VuiCtrlStyleUserExt_lerp(dst, to, from, interp_ratio)
#endif
#ifndef inline_VuiCtrlAnimateAuxUserExt
#define inline_VuiCtrlAnimateAuxUserExt
#endif
//
// you can redefine these macros to supply your own custom memory allocation
#if !defined(vui_mem_alloc)
#include <stdlib.h> //malloc, etc.
#define vui_mem_alloc(allocator, size, align) malloc(size)
#define vui_mem_realloc(allocator, ptr, size, new_size, align) realloc(ptr, new_size)
#define vui_mem_dealloc(allocator, ptr, size, align) free(ptr)
#endif
#define vui_mem_alloc_array(T, allocator, count) vui_mem_alloc(allocator, sizeof(T) * (count), alignof(T))
#define vui_mem_realloc_array(T, allocator, ptr, count, new_count) vui_mem_realloc(allocator, ptr, sizeof(T) * (count), sizeof(T) * (new_count), alignof(T))
#define vui_mem_dealloc_array(T, allocator, ptr, count) vui_mem_dealloc(allocator, ptr, sizeof(T) * (count), alignof(T))
#ifndef noreturn
#define noreturn _Noreturn
#endif
#ifndef alignof
#define alignof _Alignof
#endif
// ===========================================================================================
//
//
// Misc Helpers
//
//
// ===========================================================================================
typedef uint8_t VuiBool;
#define vui_false 0
#define vui_true 1
#define vui_ensure(expr) if (!(expr)) return 0;
#define vui_epsilon 0.001
#define vui_approx_eq(a, b) (fabsf((a) - (b)) < vui_epsilon)
#define vui_ensure_alloc_ok(ptr, ...) if (ptr == NULL) { _vui.flags |= _VuiFlags_out_of_memory; return __VA_ARGS__; }
noreturn void _vui_abort(const char* file, int line, const char* func, char* assert_test, char* message_fmt, ...);
#define vui_abort(message_fmt, ...) \
_vui_abort(__FILE__, __LINE__, __func__, NULL, message_fmt, ##__VA_ARGS__);
#define vui_assert(cond, message_fmt, ...) \
if (!(cond)) _vui_abort(__FILE__, __LINE__, __func__, #cond, message_fmt, ##__VA_ARGS__);
#define vui_assert_loc(file, line, func, cond, message_fmt, ...) \
if (!(cond)) _vui_abort(file, line, func, #cond, message_fmt, ##__VA_ARGS__);
#if VUI_DEBUG_ASSERTIONS
#define vui_debug_assert vui_assert
#else
#define vui_debug_assert(cond, message_fmt, ...) (void)(cond)
#endif
#define vui_auto_len INFINITY
#define vui_fill_len (-INFINITY)
//
// use this macro to get a unique sibling identifier based on the line number.
// if the function is used to define a new control, then get the identifier to be based in as a parameter.
#define vui_sib_id __LINE__
static inline float vui_min(float a, float b) { return a < b ? a : b; }
static inline float vui_max(float a, float b) { return a > b ? a : b; }
static inline float vui_clamp(float v, float min, float max) { return v < min ? min : (v > max ? max : v); }
static inline float vui_lerp(float to, float from, float t) { return (to - from) * t + from; }
#define vui_is_power_of_two(v) ((v) != 0) && (((v) & ((v) - 1)) == 0)
static inline void* vui_ptr_round_up_align(void* ptr, uintptr_t align) {
vui_debug_assert(vui_is_power_of_two(align), "align must be a power of two but got: %zu", align);
return (void*)(((uintptr_t)ptr + (align - 1)) & ~(align - 1));
}
static inline void* vui_ptr_round_down_align(void* ptr, uintptr_t align) {
vui_debug_assert(vui_is_power_of_two(align), "align must be a power of two but got: %zu", align);
return (void*)((uintptr_t)ptr & ~(align - 1));
}
#define vui_ptr_add(ptr, by) (void*)((uintptr_t)(ptr) + (uintptr_t)(by))
#define vui_ptr_sub(ptr, by) (void*)((uintptr_t)(ptr) - (uintptr_t)(by))
#define vui_ptr_diff(to, from) ((char*)(to) - (char*)(from))
#define _vui_defer_loop(start_expr, end_expr) for (int _i_ = (start_expr, 0); _i_ < 1; _i_ += 1, end_expr)
uint32_t vui_utf8_codepoint(const char* str, int32_t* out_codepoint);
uint32_t vui_utf8_prev_char(const char* str, uint32_t idx);
VuiBool vui_utf8_is_codepoint_boundary(char ch);
VuiBool vui_utf8_is_word_delimiter(int32_t codept);
VuiBool vui_utf8_is_whitespace(int32_t codept);
typedef struct {
float x;
float y;
} VuiVec2;
VuiVec2 vui_cubic_bezier_curve_interp(VuiVec2 points[4], float progress);
// ===========================================================================================
//
//
// Stack - LIFO (inspired by stb stretchy buffer)
// will not work for a type with an alignment greater than alignof(void*)
//
//
// ===========================================================================================
#define VuiStk(T) T*
typedef struct _VuiStkHeader _VuiStkHeader;
struct _VuiStkHeader {
uint32_t cap;
uint32_t count;
};
//
// only get the typeof if c++ as c will implicitly cast to and from a void*
#ifdef __cplusplus
#define _vui_typeof(type) decltype(type)
#else
#define _vui_typeof(type) void*
#endif
//
// these are the functions that the macro versions call, refer to the comments of the macro.
VuiBool _VuiStk_resize_cap(void** stk_ptr, uint32_t new_cap, uint32_t elmt_size);
void* _VuiStk_push_many(void** stk_ptr, uint32_t elmts_count, uint32_t elmt_size);
void* _VuiStk_insert_many(void** stk_ptr, uint32_t idx, uint32_t elmts_count, uint32_t elmt_size);
void _VuiStk_remove_range_shift(void* stk, uint32_t start_idx, uint32_t end_idx, uint32_t elmt_size);
//
// the number of elements the stacks will initially be able to hold
#define vui_stk_init_cap 16
// get the header of the stack that sits directly before the pointer you have.
#define _VuiStk_header(stk) ((_VuiStkHeader*)((char*)(stk) - sizeof(_VuiStkHeader)))
// gets the size of all the elements in bytes, returns zero if stk is NULL
#define VuiStk_size(stk) ((stk) ? (uintptr_t)_VuiStk_header(stk)->count * sizeof(*(stk)) : 0)
// removes all the elements from the stack by setting the count to 0
#define VuiStk_clear(stk) ((stk) ? _VuiStk_header(stk)->count = 0 : 0)
// gets the number of elements in the stack, returns zero if stk is NULL
#define VuiStk_count(stk) ((stk) ? _VuiStk_header(stk)->count : 0)
// gets the capacity of the stack, returns zero if stk is NULL
#define VuiStk_cap(stk) ((stk) ? _VuiStk_header(stk)->cap : 0)
// returns the last element, you can also take the address of this
#define VuiStk_last(stk) ((stk)[_VuiStk_header(stk)->count - 1])
// returns true if the stack has reached its maximum capacity, if stk is null returns true
#define VuiStk_is_full(stk) (VuiStk_count(stk) == VuiStk_cap(stk))
#define VuiStk_pop(stk) (_VuiStk_header(stk)->count -= 1)
//
// reallocates the stack to hold @param(new_cap) number of elements.
//
// @param stk: the stack of type VuiStk(T)
//
// @param new_cap: the number of elements you want to be able to hold in the stack before you need to reallocate again
//
// @return: on allocation failure vui_false is returned, otherwise returns vui_true
//
// @example:
// VuiStk_resize_cap(&stk, 24);
//
#define VuiStk_resize_cap(stk_ptr, new_cap) _VuiStk_resize_cap((void**)stk_ptr, new_cap, sizeof(**(stk_ptr)))
// pushes an uninitialized element on the stack and returns a pointer to it.
// NULL is returned on allocation failure.
//
// @example:
// uin32_t* elmt = VuiStk_push(&stk, 24);
#define VuiStk_push(stk_ptr) (_vui_typeof(*(stk_ptr)))VuiStk_push_many(stk_ptr, 1)
// pushes @param(elmts_count) number of uninitialized elements on the stack and returns a pointer to the first one.
// NULL is returned on allocation failure.
#define VuiStk_push_many(stk_ptr, elmts_count) (_vui_typeof(*(stk_ptr)))_VuiStk_push_many((void**)stk_ptr, elmts_count, sizeof(**(stk_ptr)))
// inserts an uninitialized element into the stack at @param(idx) and returns a pointer to it.
// NULL is returned on allocation failure.
//
// @example:
// uin32_t* elmt = VuiStk_insert(&stk, 4, 24);
#define VuiStk_insert(stk_ptr, idx) (_vui_typeof(*(stk_ptr)))VuiStk_insert_many(stk_ptr, idx, 1)
// inserts @param(elmts_count) number of uninitialized elements on the stack and returns a pointer to the first one.
// NULL is returned on allocation failure.
#define VuiStk_insert_many(stk_ptr, idx, elmts_count) (_vui_typeof(*(stk_ptr)))_VuiStk_insert_many((void**)stk_ptr, idx, elmts_count, sizeof(**(stk_ptr)))
//
// remove an element at @param(idx) by shifting the ones to it's right to the left.
// this retains the order of elements but comes at a cost of copying all the elements to it's right.
//
#define VuiStk_remove_shift(stk, idx) VuiStk_remove_range_shift(stk, idx, (idx) + 1)
//
// removes a range of elements by shifting the ones to it's right to the left.
// this retains the order of elements but comes at a cost of copying all the elements to it's right.
//
// @param stk: the stack of type VuiStk(T)
//
// @param start_idx: the index of the first element you wish to remove
//
// @param end_idx: the index after the last element you wish to remove
// this must be greater than @param(start_idx)
//
// @example:
// VuiStk_remove_range_shift(stk, 12, 18);
//
#define VuiStk_remove_range_shift(stk, start_idx, end_idx) _VuiStk_remove_range_shift(stk, start_idx, end_idx, sizeof(*(stk)))
//
// deallocates the stack and returns NULL.
//
// @example:
// stk = VuiStk_deinit(stk);
//
#define VuiStk_deinit(stk) ((stk) ? vui_mem_dealloc(_vui.allocator, _VuiStk_header(stk), VuiStk_size(stk) + sizeof(_VuiStkHeader), alignof(void*)), NULL : NULL)
// ===========================================================================================
//
//
// Common Types
//
//
// ===========================================================================================
#define VuiVec2_init(x, y) (VuiVec2){ x, y }
#define VuiVec2_zero (VuiVec2){0}
#define VuiVec2_auto (VuiVec2){vui_auto_len, vui_auto_len}
#define VuiVec2_fill (VuiVec2){vui_fill_len, vui_fill_len}
static inline VuiVec2 VuiVec2_add(VuiVec2 a, VuiVec2 b) { return VuiVec2_init(a.x + b.x, a.y + b.y); }
static inline VuiVec2 VuiVec2_sub(VuiVec2 a, VuiVec2 b) { return VuiVec2_init(a.x - b.x, a.y - b.y); }
static inline VuiVec2 VuiVec2_mul(VuiVec2 a, VuiVec2 b) { return VuiVec2_init(a.x * b.x, a.y * b.y); }
static inline VuiVec2 VuiVec2_div(VuiVec2 a, VuiVec2 b) { return VuiVec2_init(a.x / b.x, a.y / b.y); }
static inline VuiVec2 VuiVec2_add_scalar(VuiVec2 v, float by) { return VuiVec2_init(v.x + by, v.y + by); }
static inline VuiVec2 VuiVec2_sub_scalar(VuiVec2 v, float by) { return VuiVec2_init(v.x - by, v.y - by); }
static inline VuiVec2 VuiVec2_mul_scalar(VuiVec2 v, float by) { return VuiVec2_init(v.x * by, v.y * by); }
static inline VuiVec2 VuiVec2_div_scalar(VuiVec2 v, float by) { return VuiVec2_init(v.x / by, v.y / by); }
static inline VuiVec2 VuiVec2_neg(VuiVec2 v) { return VuiVec2_init(-v.x, -v.y); }
static inline VuiVec2 VuiVec2_abs(VuiVec2 v) { return VuiVec2_init(fabsf(v.x), fabsf(v.y)); }
static inline float VuiVec2_len(VuiVec2 v) { return sqrtf((v.x * v.x) + (v.y * v.y)); }
static inline VuiVec2 VuiVec2_norm(VuiVec2 v) {
if (v.x == 0 && v.y == 0) return v;
float k = 1.0 / sqrtf((v.x * v.x) + (v.y * v.y));
return VuiVec2_init(v.x * k, v.y * k);
}
static inline float VuiVec2_dot(VuiVec2 a, VuiVec2 b) {
float p = 0.0;
p += a.x * b.x;
p += a.y * b.y;
return p;
}
static inline VuiVec2 VuiVec2_mul_cross_scalar(VuiVec2 v, float s) { return VuiVec2_init(v.y * s, v.x * s); }
static inline float VuiVec2_mul_cross_vec(VuiVec2 a, VuiVec2 b) { return (a.x * b.y) - (a.y * b.x); }
static inline VuiVec2 VuiVec2_perp_left(VuiVec2 v) { return VuiVec2_init(v.y, -v.x); }
static inline VuiVec2 VuiVec2_perp_right(VuiVec2 v) { return VuiVec2_init(-v.y, v.x); }
static inline VuiVec2 VuiVec2_min(VuiVec2 a, VuiVec2 b) { return VuiVec2_init(vui_min(a.x, b.x), vui_min(a.y, b.y)); }
static inline VuiVec2 VuiVec2_max(VuiVec2 a, VuiVec2 b) { return VuiVec2_init(vui_max(a.x, b.x), vui_max(a.y, b.y)); }
static inline VuiVec2 VuiVec2_clamp(VuiVec2 v, VuiVec2 min, VuiVec2 max) { return VuiVec2_init(vui_clamp(v.x, min.x, max.x), vui_clamp(v.y, min.y, max.y)); }
static inline float VuiVec2_x(VuiVec2 v) { return v.x; }
static inline float VuiVec2_y(VuiVec2 v) { return v.y; }
typedef struct VuiColor VuiColor;
struct VuiColor {
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
};
#define VuiColor_init(r, g, b, a) ((VuiColor){r,g,b,a})
#define VuiColor_white ((VuiColor){0xff, 0xff, 0xff, 0xff})
#define VuiColor_black ((VuiColor){0, 0, 0, 0xff})
#define VuiColor_transparent ((VuiColor){0})
static inline VuiColor VuiColor_lerp(VuiColor to, VuiColor from, float interp_ratio) {
return VuiColor_init(
vui_lerp(to.r, from.r, interp_ratio),
vui_lerp(to.g, from.g, interp_ratio),
vui_lerp(to.b, from.b, interp_ratio),
vui_lerp(to.a, from.a, interp_ratio)
);
}
typedef union VuiVec4 VuiVec4;
typedef VuiVec4 VuiRect;
typedef VuiVec4 VuiThickness;
union VuiVec4 {
// thickness: margin, padding
struct {
VuiVec2 left_top;
VuiVec2 right_bottom;
};
struct {
float left;
float top;
float right;
float bottom;
};
// rectangle
struct {
float x;
float y;
union {
struct {
float ex;
float ey;
};
struct {
float z;
float w;
};
};
};
float array[4];
};
#define VuiVec4_init(x_, y_, z_, w_) ((VuiVec4){.x=x_, .y=y_, .z=z_, .w=w_})
#define VuiThickness_zero ((VuiVec4){0})
#define VuiThickness_init(left_, top_, right_, bottom_) ((VuiVec4){.left = left_, .top = top_, .right = right_, .bottom = bottom_})
#define VuiThickness_init_even(size) ((VuiVec4){.left=size, .top=size, .right=size, .bottom=size})
#define VuiThickness_hv_init(horizontal, vertical) ((VuiVec4){.left=horizontal, .top=vertical, .right=horizontal, .bottom=vertical})
#define VuiThickness_hv(thickness) VuiVec2_init(((thickness).left + (thickness).right), (thickness).top + (thickness).bottom)
static inline float VuiThickness_horizontal(const VuiThickness* thickness) { return thickness->left + thickness->right; }
static inline float VuiThickness_vertical(const VuiThickness* thickness) { return thickness->top + thickness->bottom; }
static inline VuiVec2 VuiThickness_size(const VuiThickness* thickness) { return VuiVec2_init(thickness->left + thickness->right, thickness->top + thickness->bottom); }
static inline void VuiThickness_lerp(VuiThickness* result, const VuiThickness* to, const VuiThickness* from, float interp_ratio) {
result->left = vui_lerp(to->left, from->left, interp_ratio);
result->top = vui_lerp(to->top, from->top, interp_ratio);
result->right = vui_lerp(to->right, from->right, interp_ratio);
result->bottom = vui_lerp(to->bottom, from->bottom, interp_ratio);
}
#define VuiRect_zero ((VuiVec4){0})
#define VuiRect_init(left_, top_, right_, bottom_) ((VuiVec4){.left = left_, .top = top_, .right = right_, .bottom = bottom_})
#define VuiRect_init_wh(left_, top_, width, height) ((VuiVec4){.left = left_, .top = top_, .right = (left_) + (width), .bottom = (top_) + (height)})
#define VuiRect_init_v2(left_top_, right_bottom_) ((VuiVec4){.left_top = left_top_, .right_bottom = right_bottom_})
#define VuiRect_left_bottom(rect) VuiVec2_init((rect).left, (rect).bottom)
#define VuiRect_right_top(rect) VuiVec2_init((rect).right, (rect).top)
#define VuiRect_size(rect) VuiVec2_init((rect).right - (rect).left, (rect).bottom - (rect).top)
static inline float VuiRect_width(const VuiRect* rect) { return rect->right - rect->left; }
static inline float VuiRect_neg_width(const VuiRect* rect) { return rect->left - rect->right; }
static inline float VuiRect_height(const VuiRect* rect) { return rect->bottom - rect->top; }
VuiVec2 VuiRect_center(const VuiRect* r);
VuiRect VuiRect_clip(const VuiRect* a, const VuiRect* b);
VuiVec2 VuiRect_clip_pt(const VuiRect* rect, VuiVec2 pt);
VuiBool VuiRect_intersects(const VuiRect* a, const VuiRect* b);
VuiBool VuiRect_intersects_pt(const VuiRect* r, VuiVec2 pt);
typedef uint32_t VuiFontId;
typedef uint32_t VuiImageId;
typedef uint32_t VuiCtrlId;
typedef uint32_t VuiCtrlSibId;
typedef enum {
VuiFocusState_none = 0x0,
// signals that the ctrl is mouse or keyboard focused
VuiFocusState_focused = 0x1,
// signals has been pressed this frame
VuiFocusState_pressed = 0x2,
// signals has been double pressed this frame
VuiFocusState_double_pressed = 0x4,
// signals is being held this frame
VuiFocusState_held = 0x8,
// signals has been released this frame
VuiFocusState_released = 0x10,
} VuiFocusState;
// ===========================================================================================
//
//
// Input
//
//
// ===========================================================================================
typedef enum {
VuiMouseButtons_none = 0x0,
VuiMouseButtons_left = 0x1,
VuiMouseButtons_middle = 0x2,
VuiMouseButtons_right = 0x4,
} VuiMouseButtons;
typedef enum {
VuiInputActions_none = 0x0,
//
// the single key actions are the only ones you need
// to fill in from your applications input system
//
VuiInputActions_ctrl_pressed = 0x1,
VuiInputActions_shift_pressed = 0x2,
VuiInputActions_left = 0x4,
VuiInputActions_right = 0x8,
VuiInputActions_down = 0x10,
VuiInputActions_up = 0x20,
VuiInputActions_enter = 0x40,
VuiInputActions_space = 0x80,
VuiInputActions_backspace = 0x100,
VuiInputActions_delete = 0x200,
VuiInputActions_home = 0x400,
VuiInputActions_end = 0x800,
VuiInputActions_page_up = 0x1000,
VuiInputActions_page_down = 0x2000,
VuiInputActions_focus_next = 0x4000, // TAB
VuiInputActions_focus_pressed = 0x8000,
VuiInputActions_focus_held = 0x10000,
VuiInputActions_focus_released = 0x20000,
//
// these are the only two key actions that will have to be set manually
//
VuiInputActions_undo = 0x40001, // CTRL + Z
VuiInputActions_cut = 0x80001, // CTRL + X
VuiInputActions_copy = 0x100001, // CTRL + C
VuiInputActions_paste = 0x200001, // CTRL + V
VuiInputActions_select_all = 0x400001, // CTRL + A
//
// from here are no longer single keys and are
// automatically worked out based on the combinations
//
VuiInputActions_redo = 0x8003, // CTRL + SHIFT + Z
VuiInputActions_delete_to_start_of_word = 0x101, // CTRL + BACKSPACE
VuiInputActions_delete_to_end_of_word = 0x201, // CTRL + DELETE
VuiInputActions_word_start = 0x5, // CTRL + LEFT
VuiInputActions_word_end = 0x9, // CTRL + RIGHT
VuiInputActions_document_home = 0x401, // CTRL + HOME
VuiInputActions_document_end = 0x801, // CTRL + END
VuiInputActions_page_prev = 0x1001, // CTRL + PAGEUP
VuiInputActions_page_next = 0x2001, // CTRL + PAGEDOWN
VuiInputActions_select_left = 0x6, // SHIFT + LEFT
VuiInputActions_select_right = 0xa, // SHIFT + RIGHT
VuiInputActions_select_down = 0x12, // SHIFT + DOWN
VuiInputActions_select_up = 0x22, // SHIFT + UP
VuiInputActions_select_word_start = 0x7, // CTRL + SHIFT + LEFT
VuiInputActions_select_word_end = 0xb, // CTRL + SHIFT + RIGHT
VuiInputActions_select_home = 0x402, // SHIFT + HOME
VuiInputActions_select_end = 0x802, // SHIFT + END
VuiInputActions_select_page_up = 0x1002, // SHIFT + PAGEUP
VuiInputActions_select_page_down = 0x2002, // SHIFT + PAGEDOWN
VuiInputActions_select_to_document_home = 0x403, // CTRL + SHIFT + HOME
VuiInputActions_select_to_document_end = 0x803, // CTRL + SHIFT + HOME
VuiInputActions_focus_prev = 0x4002, // SHIFT + TAB
} VuiInputActions;
void vui_input_set_mouse_pos(float x, float y);
void vui_input_set_mouse_wheel_offset(float wheel_offset_x, float wheel_offset_y);
void vui_input_set_mouse_button_pressed(VuiMouseButtons buttons);
void vui_input_set_mouse_button_released(VuiMouseButtons buttons);
void vui_input_add_actions(VuiInputActions actions);
void vui_input_add_text(const char* string, uint32_t string_length);
VuiVec2 vui_mouse_pos();
VuiBool vui_has_mouse_over_ctrl();
VuiBool vui_has_mouse_focused_ctrl();
VuiBool vui_has_mouse_scroll_focused_ctrl();
VuiBool vui_has_text_box_focused();
VuiBool vui_ctrl_is_mouse_focused(VuiCtrlId ctrl_id);
VuiBool vui_ctrl_is_focused(VuiCtrlId ctrl_id);
VuiBool vui_ctrl_is_mouse_scroll_focused(VuiCtrlId ctrl_id);
void vui_ctrl_set_focused(VuiCtrlId ctrl_id);
// ===========================================================================================
//
//
// Control & Style Types & Funcions
//
//
// ===========================================================================================
typedef uint8_t VuiAlign;
enum {
VuiAlign_left_top,
VuiAlign_left_center,
VuiAlign_left_bottom,
VuiAlign_center_top,
VuiAlign_center,
VuiAlign_center_bottom,
VuiAlign_right_top,
VuiAlign_right_center,
VuiAlign_right_bottom,
VuiAlign_COUNT,
};
typedef uint8_t VuiImageScaleMode;
enum {
VuiImageScaleMode_stretch, // image is scaled to fit but will not maintain the aspect ratio
VuiImageScaleMode_uniform, // image is scaled to fit and will maintain the aspect ratio
VuiImageScaleMode_uniform_crop, // image is scaled to fit on a single side and crop the other, this will maintain the aspect ratio
VuiImageScaleMode_none, // image is not scaled, the original size is used.
};
typedef struct VuiCtrlAttrs VuiCtrlAttrs;
struct VuiCtrlAttrs {
float width;
float width_min;
float width_max;
float height;
float height_min;
float height_max;
float layout_spacing;
float layout_wrap_spacing;
VuiBool layout_wrap;
VuiVec2 offset;
VuiAlign align;
VuiImageScaleMode image_scale_mode;
float style_transition_time;
};
typedef uint8_t VuiCtrlAttr;
enum {
VuiCtrlAttr_width,
VuiCtrlAttr_width_min,
VuiCtrlAttr_width_max,
VuiCtrlAttr_height,
VuiCtrlAttr_height_min,
VuiCtrlAttr_height_max,
VuiCtrlAttr_offset,
VuiCtrlAttr_align,
VuiCtrlAttr_layout_spacing,
VuiCtrlAttr_layout_wrap_spacing,
VuiCtrlAttr_layout_wrap,
VuiCtrlAttr_image_scale_mode,
VuiCtrlAttr_style_transition_time,
VuiCtrlAttr_COUNT,
};
typedef union VuiCtrlAttrValue VuiCtrlAttrValue;
union VuiCtrlAttrValue {
float float_;
VuiVec2 vec2;
VuiAlign align;
VuiBool bool_;
VuiImageScaleMode image_scale_mode;
};
typedef uint8_t VuiCtrlState;
enum {
VuiCtrlState_default,
VuiCtrlState_focused,
VuiCtrlState_active,
VuiCtrlState_disabled,
VuiCtrlState_COUNT,
};
extern char* VuiCtrlState_strings[VuiCtrlState_COUNT];
typedef uint8_t VuiCtrlStateFlags;
enum {
VuiCtrlStateFlags_default = 1 << VuiCtrlState_default,
VuiCtrlStateFlags_focused = 1 << VuiCtrlState_focused,
VuiCtrlStateFlags_active = 1 << VuiCtrlState_active,
VuiCtrlStateFlags_disabled = 1 << VuiCtrlState_disabled,
};
extern void vui_push_disabled(VuiBool enabled);
extern void vui_pop_disabled();
#define vui_scope_disabled(enabled) _vui_defer_loop(vui_push_disabled(enabled), vui_pop_disabled())
//
// push and pop attributes for a given control state that override the global style
extern void _vui_push_ctrl_attr(VuiCtrlAttr attr, VuiCtrlAttrValue value);
extern void _vui_pop_ctrl_attr(VuiCtrlAttr attr);
#define vui_push_width(value) _vui_push_ctrl_attr(VuiCtrlAttr_width, (VuiCtrlAttrValue) { .float_ = value })
#define vui_pop_width() _vui_pop_ctrl_attr(VuiCtrlAttr_width)
#define vui_scope_width(value) _vui_defer_loop(vui_push_width(value), vui_pop_width())
#define vui_push_width_ratio(value) vui_push_width(-(value))
#define vui_pop_width_ratio() vui_pop_width()
#define vui_scope_width_ratio(value) _vui_defer_loop(vui_push_width_ratio(value), vui_pop_width())
#define vui_push_width_min(value) _vui_push_ctrl_attr(VuiCtrlAttr_width_min, (VuiCtrlAttrValue) { .float_ = value })
#define vui_pop_width_min() _vui_pop_ctrl_attr(VuiCtrlAttr_width_min)
#define vui_scope_width_min(value) _vui_defer_loop(vui_push_width_min(value), vui_pop_width_min())
#define vui_push_width_min_ratio(value) vui_push_width_min(-(value))
#define vui_pop_width_min_ratio() vui_pop_width_min()
#define vui_scope_width_min_ratio(value) _vui_defer_loop(vui_push_width_min_ratio(value), vui_pop_width_min())
#define vui_push_width_max(value) _vui_push_ctrl_attr(VuiCtrlAttr_width_max, (VuiCtrlAttrValue) { .float_ = value })
#define vui_pop_width_max() _vui_pop_ctrl_attr(VuiCtrlAttr_width_max)
#define vui_scope_width_max(value) _vui_defer_loop(vui_push_width_max(value), vui_pop_width_max())
#define vui_push_width_max_ratio(value) vui_push_width_max(-(value))
#define vui_pop_width_max_ratio() vui_pop_width_max()
#define vui_scope_width_max_ratio(value) _vui_defer_loop(vui_push_width_max_ratio(value), vui_pop_width_max())
#define vui_push_height(value) _vui_push_ctrl_attr(VuiCtrlAttr_height, (VuiCtrlAttrValue) { .float_ = value })
#define vui_pop_height() _vui_pop_ctrl_attr(VuiCtrlAttr_height)
#define vui_scope_height(value) _vui_defer_loop(vui_push_height(value), vui_pop_height())
#define vui_push_height_ratio(value) vui_push_height(-(value))
#define vui_pop_height_ratio() vui_pop_height()
#define vui_scope_height_ratio(value) _vui_defer_loop(vui_push_height_ratio(value), vui_pop_height())
#define vui_push_height_min(value) _vui_push_ctrl_attr(VuiCtrlAttr_height_min, (VuiCtrlAttrValue) { .float_ = value })
#define vui_pop_height_min() _vui_pop_ctrl_attr(VuiCtrlAttr_height_min)
#define vui_scope_height_min(value) _vui_defer_loop(vui_push_height_min(value), vui_pop_height_min())
#define vui_push_height_min_ratio(value) vui_push_height_min(-(value))
#define vui_pop_height_min_ratio() vui_pop_height_min()
#define vui_scope_height_min_ratio(value) _vui_defer_loop(vui_push_height_min_ratio(value), vui_pop_height_min())
#define vui_push_height_max(value) _vui_push_ctrl_attr(VuiCtrlAttr_height_max, (VuiCtrlAttrValue) { .float_ = value })
#define vui_pop_height_max() _vui_pop_ctrl_attr(VuiCtrlAttr_height_max)
#define vui_scope_height_max(value) _vui_defer_loop(vui_push_height_max(value), vui_pop_height_max())
#define vui_push_height_max_ratio(value) vui_push_height_max(-(value))
#define vui_pop_height_max_ratio() vui_pop_height_max()
#define vui_scope_height_max_ratio(value) _vui_defer_loop(vui_push_height_max_ratio(value), vui_pop_height_max())
#define vui_push_offset(x, y) _vui_push_ctrl_attr(VuiCtrlAttr_offset, (VuiCtrlAttrValue) { .vec2 = VuiVec2_init(x, y) })
#define vui_pop_offset() _vui_pop_ctrl_attr(VuiCtrlAttr_offset)
#define vui_scope_offset(x, y) _vui_defer_loop(vui_push_offset(x, y), vui_pop_offset())
#define vui_push_align(value) _vui_push_ctrl_attr(VuiCtrlAttr_align, (VuiCtrlAttrValue) { .align = value })
#define vui_pop_align() _vui_pop_ctrl_attr(VuiCtrlAttr_align)
#define vui_scope_align(value) _vui_defer_loop(vui_push_align(value), vui_pop_align())
#define vui_push_layout_spacing(value) _vui_push_ctrl_attr(VuiCtrlAttr_layout_spacing, (VuiCtrlAttrValue) { .float_ = value })
#define vui_pop_layout_spacing() _vui_pop_ctrl_attr(VuiCtrlAttr_layout_spacing)
#define vui_scope_layout_spacing(value) _vui_defer_loop(vui_push_layout_spacing(value), vui_pop_layout_spacing())
#define vui_push_layout_wrap_spacing(value) _vui_push_ctrl_attr(VuiCtrlAttr_layout_wrap_spacing, (VuiCtrlAttrValue) { .float_ = value })
#define vui_pop_layout_wrap_spacing() _vui_pop_ctrl_attr(VuiCtrlAttr_layout_wrap_spacing)
#define vui_scope_layout_wrap_spacing(value) _vui_defer_loop(vui_push_layout_wrap_spacing(value), vui_pop_layout_wrap_spacing())
#define vui_push_layout_wrap(value) _vui_push_ctrl_attr(VuiCtrlAttr_layout_wrap, (VuiCtrlAttrValue) { .bool_ = value })
#define vui_pop_layout_wrap() _vui_pop_ctrl_attr(VuiCtrlAttr_layout_wrap)
#define vui_scope_layout_wrap(value) _vui_defer_loop(vui_push_layout_wrap(value), vui_pop_layout_wrap())
#define vui_push_image_scale_mode(value) _vui_push_ctrl_attr(VuiCtrlAttr_image_scale_mode, (VuiCtrlAttrValue) { .image_scale_mode = value })
#define vui_pop_image_scale_mode() _vui_pop_ctrl_attr(VuiCtrlAttr_image_scale_mode)
#define vui_scope_image_scale_mode(value) _vui_defer_loop(vui_push_image_scale_mode(value), vui_pop_image_scale_mode())
#define vui_push_style_transition_time(value) _vui_push_ctrl_attr(VuiCtrlAttr_style_transition_time, (VuiCtrlAttrValue) { .float_ = value })
#define vui_pop_style_transition_time() _vui_pop_ctrl_attr(VuiCtrlAttr_style_transition_time)
#define vui_scope_style_transition_time(value) _vui_defer_loop(vui_push_style_transition_time(value), vui_pop_style_transition_time())
typedef uint64_t VuiCtrlFlags;
enum {
VuiCtrlFlags_focusable = 0x1,
VuiCtrlFlags_scrollable_vertical = 0x2,
VuiCtrlFlags_scrollable_vertical_always_show = 0x4,
VuiCtrlFlags_scrollable_horizontal = 0x8,
VuiCtrlFlags_scrollable_horizontal_always_show = 0x10,
VuiCtrlFlags_resizable = 0x20,
VuiCtrlFlags_pressable = 0x100,
VuiCtrlFlags_toggleable = 0x200,
VuiCtrlFlags_selectable = 0x400,
VuiCtrlFlags_focusable_scroll = 0x800,
VuiCtrlFlags_focusable_no_keyboard_actions = 0x1000,
VuiCtrlFlags_focusable_no_keyboard_focus_nav = 0x2000,
_VuiCtrlFlags_show_vertical_bar = 0x4000,
_VuiCtrlFlags_show_horizontal_bar = 0x8000,
_VuiCtrlFlags_is_new = 0x10000,
_VuiCtrlFlags_is_popover = 0x20000,
_VuiCtrlFlags_is_laid_out = 0x40000,
_VuiCtrlFlags_is_popover_open = 0x80000,
_VuiCtrlFlags_is_canvas = 0x100000,
_VuiCtrlFlags_is_removing = 0x200000,
};
typedef uint8_t VuiLayoutType;
enum {
VuiLayoutType_stack,
VuiLayoutType_row,
VuiLayoutType_column,
};
extern char* VuiLayoutType_strings[];
typedef struct VuiCtrl VuiCtrl;
typedef void (*VuiCtrlStyleAnimateFn)(VuiCtrl* ctrl, float dt, float interp_ratio, VuiBool changed_this_frame);
typedef struct VuiCtrlStyle VuiCtrlStyle;
struct VuiCtrlStyle {
VuiThickness margin;
VuiThickness padding;
VuiColor bg_color;
VuiColor border_color;
float border_width;
float radius;
union {
VuiColor colors[VuiCtrlStyle_max_colors];
struct {
VuiColor selection_color;
VuiColor cursor_color;
};
VuiColor text_color;
VuiColor bar_color;
VuiColor check_color;
inline_VuiCtrlStyleUserExtColors;
};
union {
float sizes[VuiCtrlStyle_max_sizes];
struct {
float bar_height;
float button_width;
};
float text_line_height;
struct {
float check_box_size;
float check_size;
};
float slider_width;
float cursor_width;
float separator_size;
inline_VuiCtrlStyleUserExtSizes;
};
VuiFontId font_id;
VuiCtrlStyleAnimateFn pre_animate_fn;
VuiCtrlStyleAnimateFn post_animate_fn;
union {
//
// all of these styles points to an array of styles, one for for each VuiCtrlState value
const VuiCtrlStyle* children[VuiCtrlStyle_max_children];
struct {
union {
const VuiCtrlStyle* image_styles;
const VuiCtrlStyle* bar_styles;
};
union {
const VuiCtrlStyle* text_styles;
const VuiCtrlStyle* button_styles;
};
const VuiCtrlStyle* slider_styles;
};
inline_VuiCtrlStyleUserExtChildren;
};
inline_VuiCtrlStyleUserExt;
};
typedef struct VuiCtrlAnimateAux VuiCtrlAnimateAux;
struct VuiCtrlAnimateAux {
VuiVec2 pos;
float radius;
VuiVec2 size;
inline_VuiCtrlAnimateAuxUserExt;
};
typedef uint8_t VuiCanvasItemType;
enum VuiCanvasItemType {
VuiCanvasItemType_line,
VuiCanvasItemType_line_dotted,
VuiCanvasItemType_line_dashed,
VuiCanvasItemType_rect,
VuiCanvasItemType_rect_border,
VuiCanvasItemType_circle,
VuiCanvasItemType_circle_border,
VuiCanvasItemType_triangle,
VuiCanvasItemType_triangle_border,
VuiCanvasItemType_bezier_curve,
};
typedef struct VuiCanvasItem VuiCanvasItem;
struct VuiCanvasItem {
union {
struct {
VuiVec2 start_pos;
VuiVec2 end_pos;
float width;
} line;
struct {
VuiVec2 start_pos;
VuiVec2 end_pos;
float width;
float gap;
} line_dotted;
struct {
VuiVec2 start_pos;
VuiVec2 end_pos;
float width;
float dash_length;
float gap;
} line_dashed;
struct {
VuiRect rect;
float radius;
float width;
} rect;
struct {
VuiVec2 pos;
float radius;
float width;
} circle;
struct {
VuiVec2 a;
VuiVec2 b;
VuiVec2 c;
float width;
} triangle;
struct {
VuiVec2 points[4];
float width;
} bezier_curve;
};
VuiColor color;
VuiCanvasItemType type;
};
typedef void (*VuiCtrlRenderFn)(VuiCtrl* ctrl, VuiRect* content_rect, float interp_ratio);
struct VuiCtrl {
VuiCtrlId id;
VuiCtrlId parent_id;
VuiCtrlId child_first_id;
VuiCtrlId child_last_id;
VuiCtrlId sibling_prev_id;
VuiCtrlId sibling_next_id;
VuiCtrlId scroll_content_id;
VuiCtrlSibId sib_id;
uint32_t last_frame_idx;
VuiRect rect;
VuiCtrlStateFlags state_flags;
VuiLayoutType layout_type;
VuiFocusState focus_state;
VuiCtrlFlags flags;
VuiCtrlRenderFn render_fn;
VuiVec2 scroll_offset;
float state_time;
VuiCtrlState state;
VuiCtrlState prev_state;
const VuiCtrlStyle* styles; // this is an array to be index with VuiCtrlState
VuiCtrlStyle prev_style;
VuiCtrlStyle style;
union {
struct {
VuiImageId image_id;
VuiColor image_tint;
};
struct {
uint32_t text_start_idx;
uint32_t text_length;
uint32_t text_word_wrap_at_width;
};
struct {
VuiVec2 scroll_view_size;
};
struct {
VuiCtrlId popover_target_ctrl_id;
VuiBool* popover_is_open_ptr;
};
VuiStk(VuiCanvasItem) canvas_items;
};
VuiCtrlAnimateAux prev_animate_aux;
VuiCtrlAnimateAux animate_aux;
VuiCtrlAttrs attributes;
};
// ===========================================================================================
//
//
// Default Style
//
//
// ===========================================================================================
#ifndef vui_margin_default
#define vui_margin_default VuiThickness_init_even(4.f)
#endif
#ifndef vui_padding_default
#define vui_padding_default VuiThickness_init_even(4.f)
#endif
#ifndef vui_border_width_default
#define vui_border_width_default 2.f
#endif
#ifndef vui_radius_default
#define vui_radius_default 4.f
#endif
#ifndef vui_line_height_header
#define vui_line_height_header 32.f
#endif
#ifndef vui_line_height_menu
#define vui_line_height_menu 24.f
#endif
#ifndef vui_scroll_bar_width_default
#define vui_scroll_bar_width_default 18.f
#endif
#ifndef vui_separator_size_default
#define vui_separator_size_default 4.f
#endif
#ifndef vui_check_box_size_default
#define vui_check_box_size_default 32.f
#endif
#ifndef vui_check_size_focused_default
#define vui_check_size_focused_default 8.f
#endif
#ifndef vui_check_size_active_default
#define vui_check_size_active_default 20.f
#endif
#ifndef vui_cursor_width_default
#define vui_cursor_width_default 2.f
#endif
#ifndef vui_slider_bar_height_default
#define vui_slider_bar_height_default 16.f
#endif
#ifndef vui_slider_button_width_default
#define vui_slider_button_width_default 32.f
#endif
#define vui_color_white VuiColor_init(0xfa, 0xfa, 0xfa, 0xff)
#define vui_color_gray VuiColor_init(0xbd, 0xbd, 0xbd, 0xff)
#define vui_color_dark_gray VuiColor_init(0x46, 0x46, 0x46, 0xff)
#define vui_color_very_dark_grayish_blue VuiColor_init(0x32, 0x39, 0x3d, 0xff)
#define vui_color_very_dark_gray VuiColor_init(0x37, 0x37, 0x37, 0xff)