Repository navigation
Expand file tree
/
Copy pathui.c
More file actions
5581 lines (4745 loc) · 184 KB
/
Copy pathui.c
File metadata and controls
5581 lines (4745 loc) · 184 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
/* See LICENSE for license details. */
/* TODO(rnp):
* [ ]: track active panel
* - when beamformer gets command to open tab it goes to this location by default
* [ ]: animation state
* [ ]: tooltips
* [ ]: extra copy view settings
* - i.e. crop, zoom, pan
* [ ]: refactor: all drag overlay floating elements can be children of the drag_root.
* as long as we layout before chaining them on there won't be an issue.
* [ ]: refactor: can the scroll container just use the ViewScroll flags like the tab bar?
* [ ]: refactor: it would be nice to have some table building helpers
*
* [ ]: refactor: cross plane view for non XZ/YZ planes. math needs to be cleaned up
* to support this.
* - model transform needs to first rotate so that Z is normal, then scale, the rotate from Z to Y.
* - ideally the hardcoded +0.25f rotation for YZ should just be a consequence of the math
* [ ]: command window
* [ ]: 3D data view
* - add extra view controls, change view without recompute
* - to start just have starting plane/normal, plane uvs, rotation, and offset
* - confirmation on recompute
* [ ]: rich highlighting for parameters -> X-Plane link
*
* [ ]: multi-os windows
* [ ]: ui color configuration at runtime
*/
#include "assets/generated/assets.c"
#define NIL_COLOUR (v4){{0.76f, 0.00f, 0.65f, 1.0f}}
#define BG_COLOUR (v4){{0.15f, 0.12f, 0.13f, 1.0f}}
#define FG_COLOUR (v4){{0.92f, 0.88f, 0.78f, 1.0f}}
#define FOCUSED_COLOUR (v4){{0.86f, 0.28f, 0.21f, 1.0f}}
#define HOVERED_COLOUR (v4){{0.11f, 0.50f, 0.59f, 1.0f}}
#define SELECTION_COLOUR (v4){{0.07f, 0.37f, 0.90f, 0.5f}}
#define RULER_COLOUR (v4){{1.00f, 0.70f, 0.00f, 1.0f}}
#define BORDER_COLOUR v4_lerp(FG_COLOUR, BG_COLOUR, 0.85f)
#define NODE_SPLIT_COLOUR (v4){{0.6f, 0.6f, 0.6f, 0.5f}}
#define FRAME_VIEW_BB_COLOUR (v4){{0.92f, 0.88f, 0.78f, 1.0f}}
#define FRAME_VIEW_BB_FRACTION 0.007f
#define FRAME_VIEW_RENDER_TARGET_SIZE 1024, 1024
#define MENU_PLUS_COLOUR (v4){{0.33f, 0.42f, 1.00f, 1.00f}}
#define MENU_CLOSE_COLOUR FOCUSED_COLOUR
#define UI_NODE_PAD 8.f
#define UI_BORDER_THICK 4.f
#define UI_HASH_TABLE_COUNT 4096
read_only v4 g_colour_palette[] = {
{{0.32f, 0.20f, 0.50f, 1.00f}},
{{0.14f, 0.39f, 0.61f, 1.00f}},
{{0.61f, 0.14f, 0.25f, 1.00f}},
{{0.20f, 0.60f, 0.24f, 1.00f}},
{{0.80f, 0.60f, 0.20f, 1.00f}},
{{0.15f, 0.51f, 0.74f, 1.00f}},
};
#define HOVER_SPEED 5.0f
#define BLINK_SPEED 1.5f
#define TABLE_CELL_PAD_HEIGHT 2.0f
#define TABLE_CELL_PAD_WIDTH 8.0f
#define RULER_TEXT_PAD 6.0f
#define RULER_TICK_LENGTH 20.0f
#define UI_SPLIT_HANDLE_THICK 5.0f
#define UI_REGION_PAD 32.0f
/* TODO(rnp) smooth scroll */
#define UI_SCROLL_SPEED 12.0f
#define LISTING_LINE_PAD 6.0f
#define TITLE_BAR_PAD 6.0f
typedef enum {
UINodeFlag_MouseClickable = 1ull << 0,
UINodeFlag_KeyboardClickable = 1ull << 1,
UINodeFlag_DropSite = 1ull << 2,
UINodeFlag_ClickToFocus = 1ull << 3,
UINodeFlag_Scroll = 1ull << 4,
UINodeFlag_FocusHot = 1ull << 5,
UINodeFlag_FocusActive = 1ull << 6,
UINodeFlag_FocusHotDisabled = 1ull << 7,
UINodeFlag_FocusActiveDisabled = 1ull << 8,
UINodeFlag_Disabled = 1ull << 9,
UINodeFlag_FloatingX = 1ull << 10,
UINodeFlag_FloatingY = 1ull << 11,
UINodeFlag_FixedWidth = 1ull << 12,
UINodeFlag_FixedHeight = 1ull << 13,
UINodeFlag_AllowOverflowX = 1ull << 14,
UINodeFlag_AllowOverflowY = 1ull << 15,
// NOTE(rnp): for scrollable containers
UINodeFlag_ViewScrollX = 1ull << 16,
UINodeFlag_ViewScrollY = 1ull << 17,
UINodeFlag_DrawDropShadow = 1ull << 18,
UINodeFlag_DrawBackgroundBlur = 1ull << 19,
UINodeFlag_DrawBackground = 1ull << 20,
UINodeFlag_DrawBorder = 1ull << 21,
UINodeFlag_DrawText = 1ull << 22,
UINodeFlag_DrawHotEffects = 1ull << 23,
UINodeFlag_DrawActiveEffects = 1ull << 24,
UINodeFlag_DrawOverlay = 1ull << 25,
UINodeFlag_Clip = 1ull << 26,
UINodeFlag_DisableTextTrunc = 1ull << 27,
UINodeFlag_DisableFocusBorder = 1ull << 28,
UINodeFlag_DisableFocusOverlay = 1ull << 29,
UINodeFlag_TextInput = 1ull << 30,
UINodeFlag_TextInputNumeric = 1ull << 31,
UINodeFlag_TextInputClearOnStart = 1ull << 32,
UINodeFlag_CustomDraw = 1ull << 33,
// TODO(rnp): hack: when text is not drawn with raylib do something smarter
UINodeFlag_IconText = 1ull << 34,
UINodeFlag_Clickable = UINodeFlag_MouseClickable|UINodeFlag_KeyboardClickable,
UINodeFlag_Floating = UINodeFlag_FloatingX|UINodeFlag_FloatingY,
UINodeFlag_FixedSize = UINodeFlag_FixedWidth|UINodeFlag_FixedHeight,
UINodeFlag_AllowOverflow = UINodeFlag_AllowOverflowX|UINodeFlag_AllowOverflowY,
UINodeFlag_DisableFocusEffects = UINodeFlag_DisableFocusBorder|UINodeFlag_DisableFocusOverlay,
UINodeFlag_ViewScroll = UINodeFlag_ViewScrollX|UINodeFlag_ViewScrollY,
} UINodeFlags;
typedef struct UINodeFlagsNode UINodeFlagsNode;
struct UINodeFlagsNode {UINodeFlagsNode *next; UINodeFlags v;};
typedef struct Axis2Node Axis2Node;
struct Axis2Node {Axis2Node *next; Axis2 v;};
typedef enum {
UISizeKind_Nil,
UISizeKind_Pixels,
UISizeKind_TextContent,
UISizeKind_PercentOfParent,
UISizeKind_ChildrenSum,
} UISizeKind;
typedef struct {
UISizeKind kind;
f32 value;
f32 strictness;
} UISize;
typedef struct UISizeNode UISizeNode;
struct UISizeNode {UISizeNode *next; UISize v;};
typedef enum {
UIAlign_Left,
UIAlign_Right,
UIAlign_Center,
UIAlign_Count,
} UIAlign;
typedef struct UIAlignNode UIAlignNode;
struct UIAlignNode {UIAlignNode *next; UIAlign v;};
typedef struct {u64 value;} UINodeKey;
typedef struct UINode UINode;
#define UI_CUSTOM_DRAW_FUNCTION(name) void name(UINode *node, Rect node_rect)
typedef UI_CUSTOM_DRAW_FUNCTION(UICustomDrawFunction);
struct UINode {
UINode *parent;
UINode *first_child;
UINode *last_child;
UINode *previous_sibling;
UINode *next_sibling;
u32 child_count;
UINodeFlags flags;
str8 string;
// NOTE(rnp): desired sizing info from build step
union {
struct {
UISize semantic_width;
UISize semantic_height;
};
UISize semantic_size[Axis2_Count];
};
union {
struct {
UIAlign alignment_x;
UIAlign alignment_y;
};
UIAlign alignment[Axis2_Count];
};
UIAlign text_alignment;
Axis2 child_layout_axis;
f32 font_size;
u64 first_frame_active_index;
u64 last_frame_active_index;
UINodeKey key;
UINode *hash_prev;
UINode *hash_next;
// NOTE(rnp): recomputed every frame before drawing. also
// used on next frame for mouse collision detection.
f32 computed_position[Axis2_Count];
f32 computed_size[Axis2_Count];
v2 text_size;
// NOTE(rnp): persistent data
f32 active_t;
f32 hot_t;
v2 view_scroll_offset;
v4 bg_colour;
v4 text_colour;
v4 text_outline_colour;
f32 text_outline_thickness;
v4 border_colour;
f32 border_thickness;
UICustomDrawFunction *custom_draw_function;
void *custom_draw_context;
};
typedef struct {UINode *first, *last;} UINodeHashBucket;
typedef struct UIParentNode UIParentNode;
struct UIParentNode {UIParentNode *next; UINode *v;};
typedef enum {
UIMouseButtonKind_Left,
UIMouseButtonKind_Middle,
UIMouseButtonKind_Right,
UIMouseButtonKind_Count,
} UIMouseButtonKind;
typedef enum {
UISignalFlag_LeftPressed = (1 << 0),
UISignalFlag_MiddlePressed = (1 << 1),
UISignalFlag_RightPressed = (1 << 2),
UISignalFlag_LeftDragging = (1 << 3),
UISignalFlag_MiddleDragging = (1 << 4),
UISignalFlag_RightDragging = (1 << 5),
UISignalFlag_LeftDoubleDragging = (1 << 6),
UISignalFlag_MiddleDoubleDragging = (1 << 7),
UISignalFlag_RightDoubleDragging = (1 << 8),
UISignalFlag_LeftTripleDragging = (1 << 9),
UISignalFlag_MiddleTripleDragging = (1 << 10),
UISignalFlag_RightTripleDragging = (1 << 11),
UISignalFlag_LeftReleased = (1 << 12),
UISignalFlag_MiddleReleased = (1 << 13),
UISignalFlag_RightReleased = (1 << 14),
UISignalFlag_LeftClicked = (1 << 15),
UISignalFlag_MiddleClicked = (1 << 16),
UISignalFlag_RightClicked = (1 << 17),
UISignalFlag_LeftDoubleClicked = (1 << 18),
UISignalFlag_MiddleDoubleClicked = (1 << 19),
UISignalFlag_RightDoubleClicked = (1 << 20),
UISignalFlag_LeftTripleClicked = (1 << 21),
UISignalFlag_MiddleTripleClicked = (1 << 22),
UISignalFlag_RightTripleClicked = (1 << 23),
UISignalFlag_ScrolledX = (1 << 24),
UISignalFlag_ScrolledY = (1 << 25),
UISignalFlag_KeyboardPressed = (1 << 26),
UISignalFlag_Hovering = (1 << 27),
UISignalFlag_TextCommit = (1 << 28),
UISignalFlag_Scrolled = UISignalFlag_ScrolledX|UISignalFlag_ScrolledY,
UISignalFlag_Pressed = UISignalFlag_LeftPressed|UISignalFlag_KeyboardPressed,
UISignalFlag_Released = UISignalFlag_LeftReleased,
UISignalFlag_Clicked = UISignalFlag_LeftClicked|UISignalFlag_KeyboardPressed,
UISignalFlag_DoubleClicked = UISignalFlag_LeftDoubleClicked,
UISignalFlag_TripleClicked = UISignalFlag_LeftTripleClicked,
UISignalFlag_Dragging = UISignalFlag_LeftDragging,
} UISignalFlags;
typedef struct {
UINode *node;
v2 scroll;
str8 string;
UISignalFlags flags;
} UISignal;
typedef struct {
UINodeKey node_key;
UINodeKey next_node_key;
UINodeKey last_node_key;
i16 cursor;
i16 mark;
i16 count;
i16 last_count;
b32 numeric;
b32 changed;
// TODO(rnp): animation key
BeamformerUIBlinker blinker;
u8 buffer[256];
u8 last_buffer[256];
} UITextInputState;
typedef struct F32Node F32Node;
struct F32Node {F32Node *next; f32 v;};
typedef struct V4Node V4Node;
struct V4Node {V4Node *next; v4 v;};
#define UI_STACK_LIST \
X(Axis2Node, child_layout_axis, Axis2, 0) \
X(F32Node, font_size, f32, 0) \
X(F32Node, border_thickness, f32, UI_BORDER_THICK) \
X(F32Node, text_outline_thickness, f32, 0) \
X(UINodeFlagsNode, flags, UINodeFlags, 0) \
X(UIParentNode, parent, UINode *, ((UINode *)&ui_node_nil)) \
X(UISizeNode, semantic_height, UISize, {0}) \
X(UISizeNode, semantic_width, UISize, {0}) \
X(UIAlignNode, alignment_y, UIAlign, 0) \
X(UIAlignNode, alignment_x, UIAlign, 0) \
X(UIAlignNode, text_alignment, UIAlign, UIAlign_Left) \
X(V4Node, text_colour, v4, FG_COLOUR) \
X(V4Node, text_outline_colour, v4, NIL_COLOUR) \
X(V4Node, border_colour, v4, NIL_COLOUR) \
X(V4Node, bg_colour, v4, NIL_COLOUR) \
typedef struct {
u64 current_frame_index;
Arena *arena;
v2 current_mouse;
v2 last_mouse;
u64 input_consumed[countof(((BeamformerInput *)0)->event_queue) / 64];
static_assert(countof(((BeamformerInput *)0)->event_queue) % 64 == 0, "");
Font font;
Font small_font;
BeamformerFrameView *view_first;
BeamformerFrameView *view_last;
BeamformerFrameView *view_freelist;
VulkanHandle pipelines[BeamformerShaderKind_RenderCount];
OSHandle render_semaphores_export[2];
GPUSemaphore render_semaphores[2];
u32 render_semaphores_gl[2];
GPUImage render_3d_image;
GPUImage render_3d_depth_image;
RenderModel unit_cube_model;
BeamformerFrame latest_plane[BeamformerViewPlaneTag_Count];
b32 flush_parameters;
u32 flush_dirty_flags;
u32 selected_parameter_block;
// TODO(rnp): this should be per parameter block
f32 beamform_plane;
BeamformerUIPanel *tree;
BeamformerUIPanel *tree_node_freelist;
// NOTE(rnp): context menu
UINode *context_menu_root;
UINodeKey context_menu_anchor_key;
UINodeKey context_menu_next_anchor_key;
BeamformerUIPanel *context_menu_panel;
BeamformerUIPanel *context_menu_next_panel;
f32 context_menu_open_t;
b32 context_menu_state_changed;
// NOTE(rnp): drag info
UINodeKey drop_target_key; // alway a stable node
UINode *drop_target_node; // may point to a transient node
UINode *drag_root;
UINode *drag_overlay_root;
UINode *drag_overlay_edges_root;
UINode *drag_overlay_tab_root;
BeamformerUIPanel *drag_panel;
f32 drag_open_t;
b32 drag_end;
// NOTE(rnp): User Interaction
UINodeKey hot_node_key;
UINodeKey active_node_key[UIMouseButtonKind_Count];
// TODO(rnp): click timestamp history (double/triple press)
// NOTE(rnp): Builder State
UINode *node_freelist;
UINode *root_node;
Arena *build_arenas[2];
// NOTE(rnp): Builder Stacks
#define X(type, name, ...) struct {type *top; type *free; u64 count;} name##_node_stack;
UI_STACK_LIST
#undef X
UINodeHashBucket node_hash_table[UI_HASH_TABLE_COUNT];
UITextInputState text_input_state;
} BeamformerUI;
typedef enum {
TF_NONE = 0,
TF_ROTATED = 1 << 0,
TF_LIMITED = 1 << 1,
TF_OUTLINED = 1 << 2,
} TextFlags;
typedef enum {
TextAlignment_Center,
TextAlignment_Left,
TextAlignment_Right,
} TextAlignment;
typedef struct {
Font *font;
Rect limits;
v4 colour;
v4 outline_colour;
f32 outline_thick;
f32 rotation;
TextAlignment align;
TextFlags flags;
} TextSpec;
global BeamformerUI *ui_context;
global BeamformerInput *beamformer_input;
read_only UINode ui_node_nil = {
.parent = (UINode *)&ui_node_nil,
.first_child = (UINode *)&ui_node_nil,
.last_child = (UINode *)&ui_node_nil,
.previous_sibling = (UINode *)&ui_node_nil,
.next_sibling = (UINode *)&ui_node_nil,
};
#define X(type, name, _t, impl) read_only type ui_##name##_node_nil = {.v = impl};
UI_STACK_LIST
#undef X
#define ui_node_is_nil(n) ((n) == 0 || (n) == &ui_node_nil)
//#define ui_node_is_nil(n) ((n) == 0 || (n) == ui_context->nil_node)
#define ui_build_arena() (ui_context->build_arenas[(ui_context->current_frame_index % countof(ui_context->build_arenas))])
#define UIStackPushBody(name_upper, name_lower, type, new_value) \
name_upper *node = SLLPop(ui_context->name_lower##_node_stack.free, next); \
if (!node) node = push_struct_no_zero(ui_build_arena(), name_upper); \
node->v = new_value; \
type result = ui_context->name_lower##_node_stack.top->v; \
SLLStackPush(ui_context->name_lower##_node_stack.top, node, next); \
ui_context->name_lower##_node_stack.count++; \
return result
#define UIStackPopBody(name_upper, name_lower, type) \
name_upper *node = ui_context->name_lower##_node_stack.top; \
type result = node->v; \
if (node != &ui_##name_lower##_node_nil) { \
node = SLLPop(ui_context->name_lower##_node_stack.top, next); \
SLLStackPush(ui_context->name_lower##_node_stack.free, node, next); \
} \
return result
#define UIAlign(v) DeferLoop(ui_push_alignment(UIAlign_##v), ui_pop_alignment())
#define UIAxisAlign(axis, v) DeferLoop(ui_push_axis_alignment(axis, UIAlign_##v), ui_pop_axis_alignment(axis))
#define UIAxisSize(axis, v) DeferLoop(ui_push_axis_size(axis, v), ui_pop_axis_size(axis))
#define UIBorderColour(v) DeferLoop(ui_push_border_colour(v), ui_pop_border_colour())
#define UIBorderThickness(v) DeferLoop(ui_push_border_thickness(v), ui_pop_border_thickness())
#define UIBGColour(v) DeferLoop(ui_push_bg_colour(v), ui_pop_bg_colour())
#define UIChildLayoutAxis(v) DeferLoop(ui_push_child_layout_axis(v), ui_pop_child_layout_axis())
#define UIFlags(v) DeferLoop(ui_push_flags(v), ui_pop_flags())
#define UIFontSize(v) DeferLoop(ui_push_font_size(v), ui_pop_font_size())
#define UIParent(v) DeferLoop(ui_push_parent(v), ui_pop_parent())
#define UIPrefHeight(v) DeferLoop(ui_push_semantic_height(v), ui_pop_semantic_height())
#define UIPrefWidth(v) DeferLoop(ui_push_semantic_width(v), ui_pop_semantic_width())
#define UISize(v) DeferLoop(ui_push_size(v), ui_pop_size())
#define UITextAlign(v) DeferLoop(ui_push_text_alignment(UIAlign_##v), ui_pop_text_alignment())
#define UITextOutlineColour(v) DeferLoop(ui_push_text_outline_colour(v), ui_pop_text_outline_colour())
#define UITextOutlineThickness(v) DeferLoop(ui_push_text_outline_thickness(v), ui_pop_text_outline_thickness())
#define UITextColour(v) DeferLoop(ui_push_text_colour(v), ui_pop_text_colour())
#define UIScroll(axis) DeferLoop(ui_scroll_begin(axis), ui_scroll_end())
#define X(type, name, value_type, ...) \
function value_type ui_push_##name(value_type v) {UIStackPushBody(type, name, value_type, v);} \
function value_type ui_pop_##name(void) {UIStackPopBody(type, name, value_type);} \
function value_type ui_top_##name(void) {return ui_context->name##_node_stack.top->v;}
UI_STACK_LIST
#undef X
#define ui_size(k, v, s) (UISize){.kind = UISizeKind_##k, .value = (v), .strictness = (s)}
#define ui_em(value, strictness) ui_size(Pixels, (value) * ui_top_font_size(), (strictness))
#define ui_px(value, strictness) ui_size(Pixels, (value), (strictness))
#define ui_pct(value, strictness) ui_size(PercentOfParent, (value), (strictness))
#define ui_children_sum(strictness) ui_size(ChildrenSum, 0.f, (strictness))
#define ui_text_dim(padding, strictness) ui_size(TextContent, (padding), (strictness))
#define ui_node_key_zero() (UINodeKey){0}
#define ui_spacer(flags) ui_build_node_from_key(flags, ui_node_key_zero())
#define ui_padw(v) UIPrefWidth(ui_px(v, 1.f)) ui_spacer(0)
#define ui_padh(v) UIPrefHeight(ui_px(v, 1.f)) ui_spacer(0)
#define ui_pads(v) UISize(ui_px(v, 1.f)) ui_spacer(0)
#define ui_dragging(s) (!!((s).flags & UISignalFlag_Dragging))
#define ui_released(s) (!!((s).flags & UISignalFlag_Released))
#define ui_pressed(s) (!!((s).flags & UISignalFlag_Pressed))
#define ui_scrolled(s) (!!((s).flags & UISignalFlag_Scrolled))
#define ui_context_menu(p) ((p) == ui_context->context_menu_panel)
function UIAlign
ui_push_axis_alignment(Axis2 axis, UIAlign v)
{
UIAlign result = 0;
switch (axis) {
case Axis2_X:{result = ui_push_alignment_x(v);}break;
case Axis2_Y:{result = ui_push_alignment_y(v);}break;
InvalidDefaultCase;
}
return result;
}
function UIAlign
ui_pop_axis_alignment(Axis2 axis)
{
UIAlign result = 0;
switch (axis) {
case Axis2_X:{result = ui_pop_alignment_x();}break;
case Axis2_Y:{result = ui_pop_alignment_y();}break;
InvalidDefaultCase;
}
return result;
}
function UIAlign
ui_push_alignment(UIAlign v)
{
UIAlign result = ui_push_axis_alignment(ui_top_child_layout_axis(), v);
return result;
}
function UIAlign
ui_pop_alignment(void)
{
UIAlign result = ui_pop_axis_alignment(ui_top_child_layout_axis());
return result;
}
function UISize
ui_push_axis_size(Axis2 axis, UISize v)
{
UISize result = {0};
switch (axis) {
case Axis2_X:{result = ui_push_semantic_width(v); }break;
case Axis2_Y:{result = ui_push_semantic_height(v);}break;
InvalidDefaultCase;
}
return result;
}
function UISize
ui_pop_axis_size(Axis2 axis)
{
UISize result = {0};
switch (axis) {
case Axis2_X:{result = ui_pop_semantic_width(); }break;
case Axis2_Y:{result = ui_pop_semantic_height();}break;
InvalidDefaultCase;
}
return result;
}
function UISize
ui_push_size(UISize v)
{
UISize result = ui_push_axis_size(ui_top_child_layout_axis(), v);
return result;
}
function UISize
ui_pop_size(void)
{
UISize result = ui_pop_axis_size(ui_top_child_layout_axis());
return result;
}
#define ui_node_key_nil(k) (ui_node_key_equal((k), ui_node_key_zero()))
#define ui_node_hot(n) (ui_node_key_equal((n)->key, ui_context->hot_node_key))
function b32
ui_node_key_equal(UINodeKey a, UINodeKey b)
{
b32 result = a.value == b.value;
return result;
}
function UINodeKey
ui_node_ancestor_key(void)
{
UINode *node = ui_top_parent();
while (!ui_node_is_nil(node) && ui_node_key_equal(node->key, ui_node_key_zero()))
node = node->parent;
UINodeKey result = node->key;
return result;
}
function Rect
ui_node_rect(UINode *node)
{
Rect result = {0};
result.size = (v2){{node->computed_size[0], node->computed_size[1]}};
result.pos = (v2){{node->computed_position[0], node->computed_position[1]}};
return result;
}
function f32
ui_alignment_correction(UIAlign alignment, f32 delta)
{
f32 result = 0;
switch (alignment) {
InvalidDefaultCase;
case UIAlign_Left:{ result = 0; }break;
case UIAlign_Center:{result = 0.5f * delta;}break;
case UIAlign_Right:{ result = delta; }break;
}
return result;
}
function v2
ui_node_text_position(UINode *node)
{
Rect r = ui_node_rect(node);
v2 result = r.pos;
result.x += ui_alignment_correction(node->text_alignment, r.size.x - node->text_size.x);
result.y += (r.size.y - node->text_size.y) / 2.f;
return result;
}
function void
ui_disable_cursor(void)
{
HideCursor();
DisableCursor();
/* wtf raylib */
SetMousePosition((i32)ui_context->current_mouse.x, (i32)ui_context->current_mouse.y);
}
function void
ui_enable_cursor(void)
{
EnableCursor();
}
function Vector2
rl_v2(v2 a)
{
Vector2 result = {a.x, a.y};
return result;
}
function Rectangle
rl_rect(Rect a)
{
Rectangle result = {a.pos.x, a.pos.y, a.size.w, a.size.h};
return result;
}
function f32
beamformer_ui_blinker_update(BeamformerUIBlinker *b, f32 scale)
{
b->t += b->scale * dt_for_frame;
if (b->t >= 1.0f) b->scale = -scale;
if (b->t <= 0.0f) b->scale = scale;
f32 result = b->t;
return result;
}
function v2
measure_glyph(Font font, u32 glyph)
{
assert(glyph >= 0x20);
v2 result = {.y = (f32)font.baseSize};
/* NOTE: assumes font glyphs are ordered ASCII */
result.x = (f32)font.glyphs[glyph - 0x20].advanceX;
if (result.x == 0)
result.x = (font.recs[glyph - 0x20].width + (f32)font.glyphs[glyph - 0x20].offsetX);
return result;
}
function v2
measure_text_tight(Font font, str8 text)
{
v2 result = {0};
for (i64 i = 0; i < text.length; i++) {
assert(text.data[i] >= 0x20);
u8 glyph = text.data[i] - 0x20;
result.x += font.recs[glyph].width;
result.y = Max(font.recs[glyph].height, result.y);
}
return result;
}
function v2
measure_text(Font font, str8 text)
{
v2 result = {.y = (f32)font.baseSize};
for (i64 i = 0; i < text.length; i++)
result.x += measure_glyph(font, text.data[i]).x;
return result;
}
function str8
clamp_text_to_width(Font font, str8 text, f32 limit)
{
str8 result = text;
f32 width = 0;
for (i64 i = 0; i < text.length; i++) {
f32 next = measure_glyph(font, text.data[i]).w;
if (width + next > limit) {
result.length = i;
break;
}
width += next;
}
return result;
}
function Texture
make_raylib_texture(BeamformerFrameView *v)
{
Texture result;
result.id = v->texture;
result.width = v->colour_image.width;
result.height = v->colour_image.height;
result.mipmaps = v->colour_image.mip_map_levels;
result.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
return result;
}
function str8
push_acquisition_kind(Arena *arena, BeamformerAcquisitionKind kind, u32 transmit_count, BeamformerContrastMode contrast_mode)
{
str8 name = str8("Invalid");
b32 fixed_transmits = 0;
if Between(kind, 0, BeamformerAcquisitionKind_Count - 1) {
name = beamformer_acquisition_kind_strings[kind];
fixed_transmits = (beamformer_acquisition_kind_flags[kind] & BeamformerAcquisitionKindFlags_FixedTransmits) != 0;
}
Stream sb = arena_stream(arena);
stream_append_str8(&sb, name);
if (!fixed_transmits) {
stream_append_byte(&sb, '-');
stream_append_u64(&sb, transmit_count);
}
if (contrast_mode != BeamformerContrastMode_None)
stream_append_str8s(&sb, str8(" ("), beamformer_contrast_mode_strings[contrast_mode], str8(")"));
str8 result = arena_stream_commit(arena, &sb);
return result;
}
function void
resize_frame_view(BeamformerFrameView *view, uv2 dim)
{
if ValidHandle(view->export_handle) os_release_handle(view->export_handle);
glDeleteMemoryObjectsEXT(1, &view->memory_object);
glCreateMemoryObjectsEXT(1, &view->memory_object);
glDeleteTextures(1, &view->texture);
glCreateTextures(GL_TEXTURE_2D, 1, &view->texture);
/* TODO(rnp): add some ID for the specific view here */
str8 label = str8("Frame View Texture");
vk_image_allocate(&view->colour_image, dim.w, dim.h, 1, 1, VulkanImageUsage_Colour,
GPUUsageFlag_ImageSampling, &view->export_handle, label);
glMemoryObjectParameterivEXT(view->memory_object, GL_DEDICATED_MEMORY_OBJECT_EXT, (GLint []){1});
if (OS_WINDOWS) {
glImportMemoryWin32HandleEXT(view->memory_object, view->colour_image.memory_size,
GL_HANDLE_TYPE_OPAQUE_WIN32_EXT, (void *)view->export_handle.value[0]);
// NOTE(rnp): w32 does not transfer ownership from handle back to driver
} else {
glImportMemoryFdEXT(view->memory_object, view->colour_image.memory_size,
GL_HANDLE_TYPE_OPAQUE_FD_EXT, view->export_handle.value[0]);
view->export_handle.value[0] = OSInvalidHandleValue;
}
glTextureStorageMem2DEXT(view->texture, view->colour_image.mip_map_levels, GL_RGBA8,
view->colour_image.width, view->colour_image.height,
view->memory_object, 0);
/* NOTE(rnp): work around raylib's janky texture sampling */
v4 border_colour = {{0, 0, 0, 1}};
if (view->kind != BeamformerFrameViewKind_Copy) border_colour = (v4){0};
glTextureParameteri(view->texture, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTextureParameteri(view->texture, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTextureParameterfv(view->texture, GL_TEXTURE_BORDER_COLOR, border_colour.E);
/* TODO(rnp): better choice when depth component is included */
glTextureParameteri(view->texture, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTextureParameteri(view->texture, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glObjectLabel(GL_TEXTURE, view->texture, (i32)label.length, (char *)label.data);
}
function void
beamformer_ui_frame_view_release_subresources(BeamformerFrameView *bv, BeamformerFrameViewKind kind)
{
if (kind == BeamformerFrameViewKind_Copy)
gpu_buffer_release(&bv->copy_buffer);
}
function void
beamformer_ui_frame_view_copy_frame(BeamformerFrameView *new, BeamformerFrameView *old)
{
memory_copy(&new->frame, &old->frame, sizeof(old->frame));
iv3 points = new->frame.points;
i64 frame_size = points.x * points.y * points.z * beamformer_data_kind_byte_size[new->frame.data_kind];
Stream sb = arena_stream(ui_context->arena);
stream_append_str8(&sb, str8("Frame Copy ["));
stream_append_hex_u64(&sb, new->frame.id);
stream_append_str8(&sb, str8("]"));
stream_append_byte(&sb, 0);
GPUBufferAllocateInfo allocate_info = {
.size = frame_size,
.flags = GPUUsageFlag_TransferDestination,
.label = stream_to_str8(&sb),
};
gpu_buffer_allocate(&new->copy_buffer, allocate_info);
GPUBuffer *buffer = beamformer_gpu_buffer_from_frame(&old->frame);
assert(buffer);
GPUCommandList cmd = gpu_command_list_begin(GPUTimeline_Compute);
gpu_command_wait_timeline(cmd, GPUTimeline_Compute, old->frame.timeline_valid_value);
u64 offset = old->frame.gpu_pointer - buffer->gpu_pointer;
gpu_command_copy_buffer(cmd, &new->copy_buffer, 0, buffer, offset, frame_size);
new->frame.timeline_valid_value = gpu_command_list_end(cmd, 0, 0, 0, 0);
}
function BeamformerFrameView *
beamformer_ui_frame_view_new(BeamformerFrameViewKind kind)
{
BeamformerFrameView *old = (BeamformerFrameView *)beamformer_registers()->frame_view;
BeamformerFrameView *result = SLLPopFreelist(ui_context->view_freelist);
if (!result) result = push_struct_no_zero(ui_context->arena, typeof(*result));
zero_struct(result);
DLLInsertLast(0, ui_context->view_first, ui_context->view_last, result, next, prev);
result->export_handle.value[0] = OSInvalidHandleValue;
result->kind = kind;
result->dirty = 1;
result->log_scale = old? old->log_scale : 0;
result->dynamic_range = old? old->dynamic_range : 50.0f;
result->threshold = old? old->threshold : 55.0f;
result->gamma = old? old->gamma : 1.0f;
/* TODO(rnp): this is quite dumb. what we actually want is to render directly
* into the view region with the appropriate size for that region (scissor) */
resize_frame_view(result, (uv2){{FRAME_VIEW_RENDER_TARGET_SIZE}});
switch (kind) {
default:{
b32 copy = kind == BeamformerFrameViewKind_Copy;
result->scale_bar_active[0] = copy ? old->scale_bar_active[0] : 1;
result->scale_bar_active[1] = copy ? old->scale_bar_active[1] : 1;
}break;
case BeamformerFrameViewKind_3DXPlane:{
glTextureParameteri(result->texture, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTextureParameteri(result->texture, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
result->demo = 1;
result->plane_drag_index = -1;
result->plane_active[BeamformerViewPlaneTag_XZ] = 1;
result->plane_active[BeamformerViewPlaneTag_YZ] = 1;
}break;
}
if (kind == BeamformerFrameViewKind_Copy) {
assert(old != 0);
beamformer_ui_frame_view_copy_frame(result, old);
}
if (kind == BeamformerFrameViewKind_Latest)
result->view_plane = BeamformerViewPlaneTag_Count;
return result;
}
function v3
x_plane_display_size(BeamformerFrame *frame)
{
v3 result = {0};
v2 min_2d, max_2d;
plane_corners_from_transform(frame->voxel_transform, &min_2d, &max_2d);
result.xy = v2_sub(max_2d, min_2d);
result.x = Max(1e-3f, result.x);
result.y = Max(1e-3f, result.y);
result.z = Max(1e-3f, result.z);
return result;
}
function f32
x_plane_rotation_for_view_plane(BeamformerFrameView *view, BeamformerViewPlaneTag tag)
{
f32 result = view->rotation;
if (tag == BeamformerViewPlaneTag_YZ)
result += 0.25f;
return result;
}
function v3
x_plane_position(BeamformerFrame *frame)
{
v2 min_2d, max_2d;
plane_corners_from_transform(frame->voxel_transform, &min_2d, &max_2d);
f32 y_min = min_2d.y;
f32 y_max = max_2d.y;
v3 result = {.y = y_min + (y_max - y_min) / 2};
return result;
}
function v3
x_plane_offset_position(BeamformerFrameView *view, BeamformerFrame *frame, BeamformerViewPlaneTag tag)
{
BeamformerLiveImagingParameters *li = &beamformer_context->shared_memory->live_imaging_parameters;
m4 x_rotation = m4_rotation_about_y(x_plane_rotation_for_view_plane(view, tag));
v3 Z = x_rotation.c[2].xyz;
v3 offset = v3_scale(Z, li->image_plane_offsets[tag]);
v3 result = v3_add(x_plane_position(frame), offset);
return result;
}
function v3
x_plane_camera(BeamformerFrame *frame)
{
v3 size = x_plane_display_size(frame);
v3 target = x_plane_position(frame);
f32 dist = v2_magnitude(size.xy);
v3 result = v3_add(target, (v3){{dist, -0.5f * size.y * tan_f32(50.0f * PI / 180.0f), dist}});
return result;
}
function m4
x_plane_view_matrix(BeamformerFrame *frame, v3 camera)
{
m4 result = camera_look_at(camera, x_plane_position(frame));
return result;
}
function m4
x_plane_projection_matrix(f32 aspect)
{
m4 result = perspective_projection(10e-3f, 500e-3f, 45.0f * PI / 180.0f, aspect);
return result;
}
function ray
x_plane_raycast(BeamformerFrameView *view, BeamformerFrame *frame, v2 uv)
{
assert(view->kind == BeamformerFrameViewKind_3DXPlane);
ray result = {.origin = x_plane_camera(frame)};
v4 ray_clip = {{uv.x, uv.y, -1.0f, 1.0f}};
/* TODO(rnp): combine these so we only do one matrix inversion */
m4 proj_m = x_plane_projection_matrix((f32)view->colour_image.width / (f32)view->colour_image.height);
m4 view_m = x_plane_view_matrix(frame, result.origin);
m4 proj_inv = m4_inverse(proj_m);
m4 view_inv = m4_inverse(view_m);