ui.c (138863B)
1 /* See LICENSE for license details. */ 2 /* TODO(rnp): 3 * [ ]: refactor: ui kind of needs to be mostly thrown away 4 * - want all drawing to be immediate mode 5 * - only layout information should be retained 6 * - leaf nodes of layout have a kind associated which 7 * instructs the builder code on how to build the view 8 * - ui items (currently called Variables) are stored in a hash 9 * table and are looked up for state information at frame building time 10 * - removed/recycled when last building frame index is less than drawing frame index 11 * - building: 12 * - loop over tiled layout tree and floating layout tree as is currently done 13 * - this will build current frame ui draw tree 14 * - for each view use a stack structure with grouping, similar to how tables are made 15 * - more general though: sub groups contain a draw axis (x or y) 16 * - each ui item gets looked up in the hash table for previous frame drawing info 17 * - this can then be used to construct a "ui comm" which contains relevant info 18 * about how that ui item is being interacted with 19 * - drawing: 20 * - must be separated into layout constraint solving and rendering 21 * - layout constraint solving handles sizing, clipping, etc. 22 * - this will need multiple passes per subgroup to allow for autosizing 23 * - pay attention to the fixed size points in the hierarchy. fixed size 24 * items are complete once their children are complete. 25 * - rendering simply uses the rect/clipping regions produced by layout 26 * to send draw commands 27 * [ ]: bug: resizing live view causes texture to jump around 28 * [ ]: bug: group at end of parameter listing 29 * [ ]: refactor: ui should be in its own thread and that thread should only be concerned with the ui 30 * [ ]: refactor: remove all the excessive measure_texts (cell drawing, hover_interaction in params table) 31 * [ ]: refactor: move remaining fragment shader stuff into ui 32 * [ ]: refactor: scale table to rect 33 * [ ]: scroll bar for views that don't have enough space 34 * [ ]: allow views to collapse to just their title bar 35 * - title bar struct with expanded. Check when pushing onto draw stack; if expanded 36 * do normal behaviour else make size title bar size and ignore the splits fraction. 37 * [ ]: enforce a minimum region size or allow regions themselves to scroll 38 * [ ]: refactor: add_variable_no_link() 39 * [ ]: refactor: draw_text_limited should clamp to rect and measure text itself 40 * [ ]: draw the ui with a post-order traversal instead of pre-order traversal 41 * [ ]: consider V_HOVER_GROUP and use that to implement submenus 42 * [ ]: menu's need to support nested groups 43 * [ ]: don't redraw on every refresh; instead redraw on mouse movement/event or when a new frame 44 * arrives. For animations the ui can have a list of "timers" which while active will 45 * do a redraw on every refresh until completed. 46 * [ ]: show full non-truncated string on hover 47 * [ ]: refactor: hovered element type and show hovered element in full even when truncated 48 * [ ]: bug: cross-plane view with different dimensions for each plane 49 * [ ]: refactor: make table_skip_rows useful 50 * [ ]: refactor: better method of grouping variables for views such as FrameView/ComputeStatsView 51 */ 52 53 #include "assets/generated/assets.c" 54 55 #define BG_COLOUR (v4){{0.15f, 0.12f, 0.13f, 1.0f}} 56 #define FG_COLOUR (v4){{0.92f, 0.88f, 0.78f, 1.0f}} 57 #define FOCUSED_COLOUR (v4){{0.86f, 0.28f, 0.21f, 1.0f}} 58 #define HOVERED_COLOUR (v4){{0.11f, 0.50f, 0.59f, 1.0f}} 59 #define RULER_COLOUR (v4){{1.00f, 0.70f, 0.00f, 1.0f}} 60 #define BORDER_COLOUR v4_lerp(FG_COLOUR, BG_COLOUR, 0.85f) 61 62 #define MENU_PLUS_COLOUR (v4){{0.33f, 0.42f, 1.00f, 1.00f}} 63 #define MENU_CLOSE_COLOUR FOCUSED_COLOUR 64 65 read_only global v4 g_colour_palette[] = { 66 {{0.32f, 0.20f, 0.50f, 1.00f}}, 67 {{0.14f, 0.39f, 0.61f, 1.00f}}, 68 {{0.61f, 0.14f, 0.25f, 1.00f}}, 69 {{0.20f, 0.60f, 0.24f, 1.00f}}, 70 {{0.80f, 0.60f, 0.20f, 1.00f}}, 71 {{0.15f, 0.51f, 0.74f, 1.00f}}, 72 }; 73 74 #define HOVER_SPEED 5.0f 75 #define BLINK_SPEED 1.5f 76 77 #define TABLE_CELL_PAD_HEIGHT 2.0f 78 #define TABLE_CELL_PAD_WIDTH 8.0f 79 80 #define RULER_TEXT_PAD 10.0f 81 #define RULER_TICK_LENGTH 20.0f 82 83 #define UI_SPLIT_HANDLE_THICK 8.0f 84 #define UI_REGION_PAD 32.0f 85 86 /* TODO(rnp) smooth scroll */ 87 #define UI_SCROLL_SPEED 12.0f 88 89 #define LISTING_LINE_PAD 6.0f 90 #define TITLE_BAR_PAD 6.0f 91 92 typedef struct v2_sll { 93 struct v2_sll *next; 94 v2 v; 95 } v2_sll; 96 97 typedef struct { 98 f32 t; 99 f32 scale; 100 } UIBlinker; 101 102 typedef struct BeamformerUI BeamformerUI; 103 typedef struct Variable Variable; 104 105 typedef struct { 106 u8 buf[128]; 107 i32 count; 108 i32 cursor; 109 b32 numeric; 110 UIBlinker cursor_blink; 111 Font *font, *hot_font; 112 Variable *container; 113 } InputState; 114 115 typedef enum { 116 RulerState_None, 117 RulerState_Start, 118 RulerState_Hold, 119 } RulerState; 120 121 typedef struct { 122 v2 start; 123 v2 end; 124 RulerState state; 125 } Ruler; 126 127 typedef enum { 128 SB_LATERAL, 129 SB_AXIAL, 130 } ScaleBarDirection; 131 132 typedef struct { 133 f32 *min_value, *max_value; 134 v2_sll *savepoint_stack; 135 v2 scroll_scale; 136 f32 zoom_starting_coord; 137 ScaleBarDirection direction; 138 } ScaleBar; 139 140 typedef struct { f32 val, scale; } scaled_f32; 141 142 typedef enum { 143 RSD_VERTICAL, 144 RSD_HORIZONTAL, 145 } RegionSplitDirection; 146 147 typedef struct { 148 Variable *left; 149 Variable *right; 150 f32 fraction; 151 RegionSplitDirection direction; 152 } RegionSplit; 153 154 #define COMPUTE_STATS_VIEW_LIST \ 155 X(Average, "Average") \ 156 X(Bar, "Bar") 157 158 #define X(kind, ...) ComputeStatsViewKind_ ##kind, 159 typedef enum {COMPUTE_STATS_VIEW_LIST ComputeStatsViewKind_Count} ComputeStatsViewKind; 160 #undef X 161 162 typedef struct { 163 ComputeShaderStats *compute_shader_stats; 164 Variable *cycler; 165 ComputeStatsViewKind kind; 166 UIBlinker blink; 167 } ComputeStatsView; 168 169 typedef struct { 170 b32 *processing; 171 f32 *progress; 172 f32 display_t; 173 f32 display_t_velocity; 174 } ComputeProgressBar; 175 176 typedef enum { 177 VT_NULL, 178 VT_B32, 179 VT_F32, 180 VT_I32, 181 VT_U32, 182 VT_GROUP, 183 VT_CYCLER, 184 VT_SCALED_F32, 185 VT_BEAMFORMER_VARIABLE, 186 VT_BEAMFORMER_FRAME_VIEW, 187 VT_COMPUTE_STATS_VIEW, 188 VT_COMPUTE_PROGRESS_BAR, 189 VT_LIVE_CONTROLS_VIEW, 190 VT_LIVE_CONTROLS_STRING, 191 VT_SCALE_BAR, 192 VT_UI_BUTTON, 193 VT_UI_MENU, 194 VT_UI_REGION_SPLIT, 195 VT_UI_TEXT_BOX, 196 VT_UI_VIEW, 197 VT_X_PLANE_SHIFT, 198 } VariableType; 199 200 typedef enum { 201 VariableGroupKind_List, 202 /* NOTE(rnp): special group for vectors with components 203 * stored in separate memory locations */ 204 VariableGroupKind_Vector, 205 } VariableGroupKind; 206 207 typedef struct { 208 VariableGroupKind kind; 209 b32 expanded; 210 Variable *first; 211 Variable *last; 212 Variable *container; 213 } VariableGroup; 214 215 typedef enum { 216 UIViewFlag_CustomText = 1 << 0, 217 UIViewFlag_Floating = 1 << 1, 218 } UIViewFlags; 219 220 typedef struct { 221 Variable *child; 222 Variable *close; 223 Variable *menu; 224 Rect rect; 225 UIViewFlags flags; 226 } UIView; 227 228 /* X(id, text) */ 229 #define FRAME_VIEW_BUTTONS \ 230 X(FV_COPY_HORIZONTAL, "Copy Horizontal") \ 231 X(FV_COPY_VERTICAL, "Copy Vertical") 232 233 #define GLOBAL_MENU_BUTTONS \ 234 X(GM_OPEN_VIEW_RIGHT, "Open View Right") \ 235 X(GM_OPEN_VIEW_BELOW, "Open View Below") 236 237 #define X(id, text) UI_BID_ ##id, 238 typedef enum { 239 UI_BID_VIEW_CLOSE, 240 GLOBAL_MENU_BUTTONS 241 FRAME_VIEW_BUTTONS 242 } UIButtonID; 243 #undef X 244 245 typedef struct { 246 s8 *labels; 247 u32 *state; 248 u32 cycle_length; 249 } VariableCycler; 250 251 typedef struct { 252 s8 suffix; 253 f32 display_scale; 254 f32 scroll_scale; 255 v2 limits; 256 f32 *store; 257 } BeamformerVariable; 258 259 typedef struct { 260 v3 start_point; 261 v3 end_point; 262 } XPlaneShift; 263 264 typedef enum { 265 V_INPUT = 1 << 0, 266 V_TEXT = 1 << 1, 267 V_RADIO_BUTTON = 1 << 2, 268 V_EXTRA_ACTION = 1 << 3, 269 V_HIDES_CURSOR = 1 << 4, 270 V_LIVE_CONTROL = 1 << 28, 271 V_CAUSES_COMPUTE = 1 << 29, 272 V_UPDATE_VIEW = 1 << 30, 273 } VariableFlags; 274 275 struct Variable { 276 s8 name; 277 union { 278 void *generic; 279 BeamformerVariable beamformer_variable; 280 ComputeProgressBar compute_progress_bar; 281 ComputeStatsView compute_stats_view; 282 RegionSplit region_split; 283 ScaleBar scale_bar; 284 UIButtonID button; 285 UIView view; 286 VariableCycler cycler; 287 VariableGroup group; 288 XPlaneShift x_plane_shift; 289 scaled_f32 scaled_real32; 290 b32 bool32; 291 i32 signed32; 292 u32 unsigned32; 293 f32 real32; 294 }; 295 Variable *next; 296 Variable *parent; 297 VariableFlags flags; 298 VariableType type; 299 300 f32 hover_t; 301 f32 name_width; 302 }; 303 304 #define BEAMFORMER_FRAME_VIEW_KIND_LIST \ 305 X(Latest, "Latest") \ 306 X(3DXPlane, "3D X-Plane") \ 307 X(Indexed, "Indexed") \ 308 X(Copy, "Copy") 309 310 typedef enum { 311 #define X(kind, ...) BeamformerFrameViewKind_##kind, 312 BEAMFORMER_FRAME_VIEW_KIND_LIST 313 #undef X 314 BeamformerFrameViewKind_Count, 315 } BeamformerFrameViewKind; 316 317 typedef struct BeamformerFrameView BeamformerFrameView; 318 struct BeamformerFrameView { 319 BeamformerFrameViewKind kind; 320 b32 dirty; 321 BeamformerFrame *frame; 322 BeamformerFrameView *prev, *next; 323 324 iv2 texture_dim; 325 u32 textures[2]; 326 i32 texture_mipmaps; 327 328 /* NOTE(rnp): any pointers to variables are added to the menu and will 329 * be put onto the freelist if the view is closed. */ 330 331 Variable *kind_cycler; 332 Variable *log_scale; 333 Variable threshold; 334 Variable dynamic_range; 335 Variable gamma; 336 337 union { 338 /* BeamformerFrameViewKind_Latest/BeamformerFrameViewKind_Indexed */ 339 struct { 340 Variable lateral_scale_bar; 341 Variable axial_scale_bar; 342 Variable *lateral_scale_bar_active; 343 Variable *axial_scale_bar_active; 344 /* NOTE(rnp): if kind is Latest selects which plane to use 345 * if kind is Indexed selects the index */ 346 Variable *cycler; 347 u32 cycler_state; 348 349 Ruler ruler; 350 351 v3 min_coordinate; 352 v3 max_coordinate; 353 }; 354 355 /* BeamformerFrameViewKind_3DXPlane */ 356 struct { 357 Variable x_plane_shifts[2]; 358 Variable *demo; 359 f32 rotation; 360 v3 hit_test_point; 361 }; 362 }; 363 }; 364 365 typedef struct BeamformerLiveControlsView BeamformerLiveControlsView; 366 struct BeamformerLiveControlsView { 367 Variable transmit_power; 368 Variable tgc_control_points[countof(((BeamformerLiveImagingParameters *)0)->tgc_control_points)]; 369 Variable save_button; 370 Variable stop_button; 371 Variable save_text; 372 UIBlinker save_button_blink; 373 u32 hot_field_flag; 374 u32 active_field_flag; 375 }; 376 377 typedef enum { 378 InteractionKind_None, 379 InteractionKind_Nop, 380 InteractionKind_Auto, 381 InteractionKind_Button, 382 InteractionKind_Drag, 383 InteractionKind_Menu, 384 InteractionKind_Ruler, 385 InteractionKind_Scroll, 386 InteractionKind_Set, 387 InteractionKind_Text, 388 } InteractionKind; 389 390 typedef struct { 391 InteractionKind kind; 392 union { 393 void *generic; 394 Variable *var; 395 }; 396 Rect rect; 397 } Interaction; 398 399 #define auto_interaction(r, v) (Interaction){.kind = InteractionKind_Auto, .var = v, .rect = r} 400 401 struct BeamformerUI { 402 Arena arena; 403 404 Font font; 405 Font small_font; 406 407 Variable *regions; 408 Variable *variable_freelist; 409 410 Variable floating_widget_sentinal; 411 412 BeamformerFrameView *views; 413 BeamformerFrameView *view_freelist; 414 BeamformerFrame *frame_freelist; 415 416 Interaction interaction; 417 Interaction hot_interaction; 418 Interaction next_interaction; 419 420 InputState text_input_state; 421 422 /* TODO(rnp): ideally this isn't copied all over the place */ 423 BeamformerRenderModel unit_cube_model; 424 425 v2_sll *scale_bar_savepoint_freelist; 426 427 BeamformerFrame *latest_plane[BeamformerViewPlaneTag_Count + 1]; 428 429 BeamformerUIParameters params; 430 b32 flush_params; 431 u32 selected_parameter_block; 432 433 FrameViewRenderContext *frame_view_render_context; 434 435 SharedMemoryRegion shared_memory; 436 BeamformerCtx *beamformer_context; 437 }; 438 439 typedef enum { 440 TF_NONE = 0, 441 TF_ROTATED = 1 << 0, 442 TF_LIMITED = 1 << 1, 443 TF_OUTLINED = 1 << 2, 444 } TextFlags; 445 446 typedef enum { 447 TextAlignment_Center, 448 TextAlignment_Left, 449 TextAlignment_Right, 450 } TextAlignment; 451 452 typedef struct { 453 Font *font; 454 Rect limits; 455 v4 colour; 456 v4 outline_colour; 457 f32 outline_thick; 458 f32 rotation; 459 TextAlignment align; 460 TextFlags flags; 461 } TextSpec; 462 463 typedef enum { 464 TRK_CELLS, 465 TRK_TABLE, 466 } TableRowKind; 467 468 typedef enum { 469 TableCellKind_None, 470 TableCellKind_Variable, 471 TableCellKind_VariableGroup, 472 } TableCellKind; 473 474 typedef struct { 475 s8 text; 476 union { 477 i64 integer; 478 Variable *var; 479 void *generic; 480 }; 481 TableCellKind kind; 482 f32 width; 483 } TableCell; 484 485 typedef struct { 486 void *data; 487 TableRowKind kind; 488 } TableRow; 489 490 typedef struct Table { 491 TableRow *data; 492 iz count; 493 iz capacity; 494 495 /* NOTE(rnp): counted by columns */ 496 TextAlignment *alignment; 497 f32 *widths; 498 499 v4 border_colour; 500 f32 column_border_thick; 501 f32 row_border_thick; 502 v2 size; 503 v2 cell_pad; 504 505 /* NOTE(rnp): row count including nested tables */ 506 i32 rows; 507 i32 columns; 508 509 struct Table *parent; 510 } Table; 511 512 typedef struct { 513 Table *table; 514 i32 row_index; 515 } TableStackFrame; 516 517 typedef struct { 518 TableStackFrame *data; 519 iz count; 520 iz capacity; 521 } TableStack; 522 523 typedef enum { 524 TIK_ROWS, 525 TIK_CELLS, 526 } TableIteratorKind; 527 528 typedef struct { 529 TableStack stack; 530 TableStackFrame frame; 531 532 TableRow *row; 533 i16 column; 534 i16 sub_table_depth; 535 536 TableIteratorKind kind; 537 538 f32 start_x; 539 TextAlignment alignment; 540 Rect cell_rect; 541 } TableIterator; 542 543 function f32 544 ui_blinker_update(UIBlinker *b, f32 scale) 545 { 546 b->t += b->scale * dt_for_frame; 547 if (b->t >= 1.0f) b->scale = -scale; 548 if (b->t <= 0.0f) b->scale = scale; 549 f32 result = b->t; 550 return result; 551 } 552 553 function v2 554 measure_glyph(Font font, u32 glyph) 555 { 556 assert(glyph >= 0x20); 557 v2 result = {.y = (f32)font.baseSize}; 558 /* NOTE: assumes font glyphs are ordered ASCII */ 559 result.x = (f32)font.glyphs[glyph - 0x20].advanceX; 560 if (result.x == 0) 561 result.x = (font.recs[glyph - 0x20].width + (f32)font.glyphs[glyph - 0x20].offsetX); 562 return result; 563 } 564 565 function v2 566 measure_text(Font font, s8 text) 567 { 568 v2 result = {.y = (f32)font.baseSize}; 569 for (iz i = 0; i < text.len; i++) 570 result.x += measure_glyph(font, text.data[i]).x; 571 return result; 572 } 573 574 function s8 575 clamp_text_to_width(Font font, s8 text, f32 limit) 576 { 577 s8 result = text; 578 f32 width = 0; 579 for (iz i = 0; i < text.len; i++) { 580 f32 next = measure_glyph(font, text.data[i]).w; 581 if (width + next > limit) { 582 result.len = i; 583 break; 584 } 585 width += next; 586 } 587 return result; 588 } 589 590 function v2 591 align_text_in_rect(s8 text, Rect r, Font font) 592 { 593 v2 size = measure_text(font, text); 594 v2 pos = v2_add(r.pos, v2_scale(v2_sub(r.size, size), 0.5)); 595 v2 result = clamp_v2_rect(pos, r); 596 return result; 597 } 598 599 function Texture 600 make_raylib_texture(BeamformerFrameView *v) 601 { 602 Texture result; 603 result.id = v->textures[0]; 604 result.width = v->texture_dim.w; 605 result.height = v->texture_dim.h; 606 result.mipmaps = v->texture_mipmaps; 607 result.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8; 608 return result; 609 } 610 611 function void 612 stream_append_variable(Stream *s, Variable *var) 613 { 614 switch (var->type) { 615 case VT_UI_BUTTON: 616 case VT_GROUP:{ stream_append_s8(s, var->name); }break; 617 case VT_F32:{ stream_append_f64(s, var->real32, 100); }break; 618 case VT_B32:{ stream_append_s8(s, var->bool32 ? s8("True") : s8("False")); }break; 619 case VT_SCALED_F32:{ stream_append_f64(s, var->scaled_real32.val, 100); }break; 620 case VT_BEAMFORMER_VARIABLE:{ 621 BeamformerVariable *bv = &var->beamformer_variable; 622 stream_append_f64(s, *bv->store * bv->display_scale, 100); 623 }break; 624 case VT_CYCLER:{ 625 u32 index = *var->cycler.state; 626 if (var->cycler.labels) stream_append_s8(s, var->cycler.labels[index]); 627 else stream_append_u64(s, index); 628 }break; 629 case VT_LIVE_CONTROLS_STRING:{ 630 BeamformerLiveImagingParameters *lip = var->generic; 631 stream_append_s8(s, (s8){.data = (u8 *)lip->save_name_tag, .len = lip->save_name_tag_length}); 632 if (lip->save_name_tag_length <= 0) stream_append_s8(s, s8("Tag...")); 633 }break; 634 InvalidDefaultCase; 635 } 636 } 637 638 function void 639 stream_append_variable_group(Stream *s, Variable *var) 640 { 641 switch (var->type) { 642 case VT_GROUP:{ 643 switch (var->group.kind) { 644 case VariableGroupKind_Vector:{ 645 Variable *v = var->group.first; 646 stream_append_s8(s, s8("{")); 647 while (v) { 648 stream_append_variable(s, v); 649 v = v->next; 650 if (v) stream_append_s8(s, s8(", ")); 651 } 652 stream_append_s8(s, s8("}")); 653 }break; 654 InvalidDefaultCase; 655 } 656 }break; 657 InvalidDefaultCase; 658 } 659 } 660 661 function s8 662 push_acquisition_kind(Stream *s, BeamformerAcquisitionKind kind, u32 transmit_count) 663 { 664 s8 name = beamformer_acquisition_kind_strings[kind]; 665 b32 fixed_transmits = beamformer_acquisition_kind_has_fixed_transmits[kind]; 666 if (kind >= BeamformerAcquisitionKind_Count || kind < 0) { 667 fixed_transmits = 0; 668 name = s8("Invalid"); 669 } 670 671 stream_append_s8(s, name); 672 if (!fixed_transmits) { 673 stream_append_byte(s, '-'); 674 stream_append_u64(s, transmit_count); 675 } 676 677 return stream_to_s8(s); 678 } 679 680 function s8 681 push_custom_view_title(Stream *s, Variable *var) 682 { 683 switch (var->type) { 684 case VT_COMPUTE_STATS_VIEW:{ 685 stream_append_s8(s, s8("Compute Stats: ")); 686 stream_append_variable(s, var->compute_stats_view.cycler); 687 }break; 688 case VT_COMPUTE_PROGRESS_BAR:{ 689 stream_append_s8(s, s8("Compute Progress: ")); 690 stream_append_f64(s, 100 * *var->compute_progress_bar.progress, 100); 691 stream_append_byte(s, '%'); 692 } break; 693 case VT_BEAMFORMER_FRAME_VIEW:{ 694 BeamformerFrameView *bv = var->generic; 695 stream_append_s8(s, s8("Frame View")); 696 switch (bv->kind) { 697 case BeamformerFrameViewKind_Copy:{ stream_append_s8(s, s8(": Copy [")); }break; 698 case BeamformerFrameViewKind_Latest:{ 699 #define X(plane, id, pretty) s8_comp(": " pretty " ["), 700 read_only local_persist s8 labels[BeamformerViewPlaneTag_Count + 1] = { 701 BEAMFORMER_VIEW_PLANE_TAG_LIST 702 s8_comp(": Live [") 703 }; 704 #undef X 705 stream_append_s8(s, labels[*bv->cycler->cycler.state % (BeamformerViewPlaneTag_Count + 1)]); 706 }break; 707 case BeamformerFrameViewKind_Indexed:{ 708 stream_append_s8(s, s8(": Index {")); 709 stream_append_u64(s, *bv->cycler->cycler.state % BeamformerMaxSavedFrames); 710 stream_append_s8(s, s8("} [")); 711 }break; 712 case BeamformerFrameViewKind_3DXPlane:{ stream_append_s8(s, s8(": 3D X-Plane")); }break; 713 InvalidDefaultCase; 714 } 715 if (bv->kind != BeamformerFrameViewKind_3DXPlane) { 716 stream_append_hex_u64(s, bv->frame? bv->frame->id : 0); 717 stream_append_byte(s, ']'); 718 } 719 }break; 720 InvalidDefaultCase; 721 } 722 return stream_to_s8(s); 723 } 724 725 #define table_new(a, init, ...) table_new_(a, init, arg_list(TextAlignment, ##__VA_ARGS__)) 726 function Table * 727 table_new_(Arena *a, i32 initial_capacity, TextAlignment *alignment, i32 columns) 728 { 729 Table *result = push_struct(a, Table); 730 da_reserve(a, result, initial_capacity); 731 result->columns = columns; 732 result->alignment = push_array(a, TextAlignment, columns); 733 result->widths = push_array(a, f32, columns); 734 result->cell_pad = (v2){{TABLE_CELL_PAD_WIDTH, TABLE_CELL_PAD_HEIGHT}}; 735 mem_copy(result->alignment, alignment, sizeof(*alignment) * (u32)columns); 736 return result; 737 } 738 739 function i32 740 table_skip_rows(Table *t, f32 draw_height, f32 text_height) 741 { 742 i32 max_rows = (i32)(draw_height / (text_height + t->cell_pad.h)); 743 i32 result = t->rows - MIN(t->rows, max_rows); 744 return result; 745 } 746 747 function TableIterator * 748 table_iterator_new(Table *table, TableIteratorKind kind, Arena *a, i32 starting_row, v2 at, Font *font) 749 { 750 TableIterator *result = push_struct(a, TableIterator); 751 result->kind = kind; 752 result->frame.table = table; 753 result->frame.row_index = starting_row; 754 result->start_x = at.x; 755 result->cell_rect.size.h = (f32)font->baseSize; 756 result->cell_rect.pos = v2_add(at, v2_scale(table->cell_pad, 0.5f)); 757 result->cell_rect.pos.y += (f32)(starting_row - 1) * (result->cell_rect.size.h + table->cell_pad.h + table->row_border_thick); 758 da_reserve(a, &result->stack, 4); 759 return result; 760 } 761 762 function void * 763 table_iterator_next(TableIterator *it, Arena *a) 764 { 765 void *result = 0; 766 767 if (!it->row || it->kind == TIK_ROWS) { 768 for (;;) { 769 TableRow *row = it->frame.table->data + it->frame.row_index++; 770 if (it->frame.row_index <= it->frame.table->count) { 771 if (row->kind == TRK_TABLE) { 772 *da_push(a, &it->stack) = it->frame; 773 it->frame = (TableStackFrame){.table = row->data}; 774 it->sub_table_depth++; 775 } else { 776 result = row; 777 break; 778 } 779 } else if (it->stack.count) { 780 it->frame = it->stack.data[--it->stack.count]; 781 it->sub_table_depth--; 782 } else { 783 break; 784 } 785 } 786 Table *t = it->frame.table; 787 it->row = result; 788 it->column = 0; 789 it->cell_rect.pos.x = it->start_x + t->cell_pad.w / 2 + 790 it->cell_rect.size.h * it->sub_table_depth; 791 it->cell_rect.pos.y += it->cell_rect.size.h + t->row_border_thick + t->cell_pad.h; 792 } 793 794 if (it->row && it->kind == TIK_CELLS) { 795 Table *t = it->frame.table; 796 i32 column = it->column++; 797 it->cell_rect.pos.x += column > 0 ? it->cell_rect.size.w + t->cell_pad.w : 0; 798 it->cell_rect.size.w = t->widths[column]; 799 it->alignment = t->alignment[column]; 800 result = (TableCell *)it->row->data + column; 801 802 if (it->column == t->columns) 803 it->row = 0; 804 } 805 806 return result; 807 } 808 809 function f32 810 table_width(Table *t) 811 { 812 f32 result = 0; 813 i32 valid = 0; 814 for (i32 i = 0; i < t->columns; i++) { 815 result += t->widths[i]; 816 if (t->widths[i] > 0) valid++; 817 } 818 result += t->cell_pad.w * (f32)valid; 819 result += MAX(0, ((f32)valid - 1)) * t->column_border_thick; 820 return result; 821 } 822 823 function v2 824 table_extent(Table *t, Arena arena, Font *font) 825 { 826 TableIterator *it = table_iterator_new(t, TIK_ROWS, &arena, 0, (v2){0}, font); 827 for (TableRow *row = table_iterator_next(it, &arena); 828 row; 829 row = table_iterator_next(it, &arena)) 830 { 831 for (i32 i = 0; i < it->frame.table->columns; i++) { 832 TableCell *cell = (TableCell *)row->data + i; 833 if (!cell->text.len && cell->var && cell->var->flags & V_RADIO_BUTTON) { 834 cell->width = (f32)font->baseSize; 835 } else { 836 cell->width = measure_text(*font, cell->text).w; 837 } 838 it->frame.table->widths[i] = MAX(cell->width, it->frame.table->widths[i]); 839 } 840 } 841 842 t->size = (v2){.x = table_width(t), .y = it->cell_rect.pos.y - t->cell_pad.h / 2}; 843 v2 result = t->size; 844 return result; 845 } 846 847 function v2 848 table_cell_align(TableCell *cell, TextAlignment align, Rect r) 849 { 850 v2 result = r.pos; 851 if (r.size.w >= cell->width) { 852 switch (align) { 853 case TextAlignment_Left:{}break; 854 case TextAlignment_Right:{ result.x += (r.size.w - cell->width); }break; 855 case TextAlignment_Center:{ result.x += (r.size.w - cell->width) / 2; }break; 856 } 857 } 858 return result; 859 } 860 861 function TableCell 862 table_variable_cell(Arena *a, Variable *var) 863 { 864 TableCell result = {.var = var, .kind = TableCellKind_Variable}; 865 if ((var->flags & V_RADIO_BUTTON) == 0) { 866 Stream text = arena_stream(*a); 867 stream_append_variable(&text, var); 868 result.text = arena_stream_commit(a, &text); 869 } 870 return result; 871 } 872 873 function TableRow * 874 table_push_row(Table *t, Arena *a, TableRowKind kind) 875 { 876 TableRow *result = da_push(a, t); 877 if (kind == TRK_CELLS) { 878 result->data = push_array(a, TableCell, t->columns); 879 /* NOTE(rnp): do not increase rows for an empty subtable */ 880 t->rows++; 881 } 882 result->kind = kind; 883 return result; 884 } 885 886 function TableRow * 887 table_push_parameter_row(Table *t, Arena *a, s8 label, Variable *var, s8 suffix) 888 { 889 ASSERT(t->columns >= 3); 890 TableRow *result = table_push_row(t, a, TRK_CELLS); 891 TableCell *cells = result->data; 892 893 cells[0].text = label; 894 cells[1] = table_variable_cell(a, var); 895 cells[2].text = suffix; 896 897 return result; 898 } 899 900 #define table_begin_subtable(t, a, ...) table_begin_subtable_(t, a, arg_list(TextAlignment, ##__VA_ARGS__)) 901 function Table * 902 table_begin_subtable_(Table *table, Arena *a, TextAlignment *alignment, i32 columns) 903 { 904 TableRow *row = table_push_row(table, a, TRK_TABLE); 905 Table *result = row->data = table_new_(a, 0, alignment, columns); 906 result->parent = table; 907 return result; 908 } 909 910 function Table * 911 table_end_subtable(Table *table) 912 { 913 Table *result = table->parent ? table->parent : table; 914 return result; 915 } 916 917 function void 918 resize_frame_view(BeamformerFrameView *view, iv2 dim, b32 depth) 919 { 920 glDeleteTextures(countof(view->textures), view->textures); 921 glCreateTextures(GL_TEXTURE_2D, depth ? countof(view->textures) : countof(view->textures) - 1, view->textures); 922 923 view->texture_dim = dim; 924 view->texture_mipmaps = (i32)ctz_u32((u32)MAX(dim.x, dim.y)) + 1; 925 glTextureStorage2D(view->textures[0], view->texture_mipmaps, GL_RGBA8, dim.x, dim.y); 926 if (depth) glTextureStorage2D(view->textures[1], 1, GL_DEPTH_COMPONENT24, dim.x, dim.y); 927 928 glGenerateTextureMipmap(view->textures[0]); 929 930 /* NOTE(rnp): work around raylib's janky texture sampling */ 931 v4 border_colour = {0}; 932 if (view->kind == BeamformerFrameViewKind_Copy) border_colour = (v4){{0, 0, 0, 1}}; 933 glTextureParameteri(view->textures[0], GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); 934 glTextureParameteri(view->textures[0], GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); 935 glTextureParameterfv(view->textures[0], GL_TEXTURE_BORDER_COLOR, border_colour.E); 936 /* TODO(rnp): better choice when depth component is included */ 937 glTextureParameteri(view->textures[0], GL_TEXTURE_MAG_FILTER, GL_NEAREST); 938 glTextureParameteri(view->textures[0], GL_TEXTURE_MIN_FILTER, GL_NEAREST); 939 940 /* TODO(rnp): add some ID for the specific view here */ 941 LABEL_GL_OBJECT(GL_TEXTURE, view->textures[0], s8("Frame View Texture")); 942 } 943 944 function void 945 ui_beamformer_frame_view_release_subresources(BeamformerUI *ui, BeamformerFrameView *bv, BeamformerFrameViewKind kind) 946 { 947 if (kind == BeamformerFrameViewKind_Copy && bv->frame) { 948 glDeleteTextures(1, &bv->frame->texture); 949 bv->frame->texture = 0; 950 SLLPushFreelist(bv->frame, ui->frame_freelist); 951 } 952 953 if (kind != BeamformerFrameViewKind_3DXPlane) { 954 if (bv->axial_scale_bar.scale_bar.savepoint_stack) 955 SLLPushFreelist(bv->axial_scale_bar.scale_bar.savepoint_stack, ui->scale_bar_savepoint_freelist); 956 if (bv->lateral_scale_bar.scale_bar.savepoint_stack) 957 SLLPushFreelist(bv->lateral_scale_bar.scale_bar.savepoint_stack, ui->scale_bar_savepoint_freelist); 958 } 959 } 960 961 function void 962 ui_variable_free(BeamformerUI *ui, Variable *var) 963 { 964 if (var) { 965 var->parent = 0; 966 while (var) { 967 if (var->type == VT_GROUP) { 968 var = var->group.first; 969 } else { 970 if (var->type == VT_BEAMFORMER_FRAME_VIEW) { 971 /* TODO(rnp): instead there should be a way of linking these up */ 972 BeamformerFrameView *bv = var->generic; 973 ui_beamformer_frame_view_release_subresources(ui, bv, bv->kind); 974 DLLRemove(bv); 975 /* TODO(rnp): hack; use a sentinal */ 976 if (bv == ui->views) 977 ui->views = bv->next; 978 SLLPushFreelist(bv, ui->view_freelist); 979 } 980 981 Variable *dead = var; 982 if (var->next) { 983 var = var->next; 984 } else { 985 var = var->parent; 986 /* NOTE(rnp): when we assign parent here we have already 987 * released the children. Assign type so we don't loop */ 988 if (var) var->type = VT_NULL; 989 } 990 SLLPushFreelist(dead, ui->variable_freelist); 991 } 992 } 993 } 994 } 995 996 function void 997 ui_variable_free_group_items(BeamformerUI *ui, Variable *group) 998 { 999 assert(group->type == VT_GROUP); 1000 /* NOTE(rnp): prevent traversal back to us */ 1001 group->group.last->parent = 0; 1002 ui_variable_free(ui, group->group.first); 1003 group->group.first = group->group.last = 0; 1004 } 1005 1006 function void 1007 ui_view_free(BeamformerUI *ui, Variable *view) 1008 { 1009 assert(view->type == VT_UI_VIEW); 1010 ui_variable_free(ui, view->view.child); 1011 ui_variable_free(ui, view->view.close); 1012 ui_variable_free(ui, view->view.menu); 1013 ui_variable_free(ui, view); 1014 } 1015 1016 function Variable * 1017 fill_variable(Variable *var, Variable *group, s8 name, u32 flags, VariableType type, Font font) 1018 { 1019 var->flags = flags; 1020 var->type = type; 1021 var->name = name; 1022 var->parent = group; 1023 var->name_width = measure_text(font, name).x; 1024 1025 if (group && group->type == VT_GROUP) { 1026 if (group->group.last) group->group.last = group->group.last->next = var; 1027 else group->group.last = group->group.first = var; 1028 } 1029 1030 return var; 1031 } 1032 1033 function Variable * 1034 add_variable(BeamformerUI *ui, Variable *group, Arena *arena, s8 name, u32 flags, 1035 VariableType type, Font font) 1036 { 1037 Variable *result = SLLPopFreelist(ui->variable_freelist); 1038 if (result) zero_struct(result); 1039 else result = push_struct(arena, Variable); 1040 return fill_variable(result, group, name, flags, type, font); 1041 } 1042 1043 function Variable * 1044 add_variable_group(BeamformerUI *ui, Variable *group, Arena *arena, s8 name, VariableGroupKind kind, Font font) 1045 { 1046 Variable *result = add_variable(ui, group, arena, name, V_INPUT, VT_GROUP, font); 1047 result->group.kind = kind; 1048 return result; 1049 } 1050 1051 function Variable * 1052 end_variable_group(Variable *group) 1053 { 1054 ASSERT(group->type == VT_GROUP); 1055 return group->parent; 1056 } 1057 1058 function void 1059 fill_variable_cycler(Variable *cycler, u32 *store, s8 *labels, u32 cycle_count) 1060 { 1061 cycler->cycler.cycle_length = cycle_count; 1062 cycler->cycler.state = store; 1063 cycler->cycler.labels = labels; 1064 } 1065 1066 function Variable * 1067 add_variable_cycler(BeamformerUI *ui, Variable *group, Arena *arena, u32 flags, Font font, s8 name, 1068 u32 *store, s8 *labels, u32 cycle_count) 1069 { 1070 Variable *result = add_variable(ui, group, arena, name, V_INPUT|flags, VT_CYCLER, font); 1071 fill_variable_cycler(result, store, labels, cycle_count); 1072 return result; 1073 } 1074 1075 function Variable * 1076 add_button(BeamformerUI *ui, Variable *group, Arena *arena, s8 name, UIButtonID id, 1077 u32 flags, Font font) 1078 { 1079 Variable *result = add_variable(ui, group, arena, name, V_INPUT|flags, VT_UI_BUTTON, font); 1080 result->button = id; 1081 return result; 1082 } 1083 1084 function Variable * 1085 add_ui_split(BeamformerUI *ui, Variable *parent, Arena *arena, s8 name, f32 fraction, 1086 RegionSplitDirection direction, Font font) 1087 { 1088 Variable *result = add_variable(ui, parent, arena, name, V_HIDES_CURSOR, VT_UI_REGION_SPLIT, font); 1089 result->region_split.direction = direction; 1090 result->region_split.fraction = fraction; 1091 return result; 1092 } 1093 1094 function Variable * 1095 add_global_menu_to_group(BeamformerUI *ui, Arena *arena, Variable *group) 1096 { 1097 #define X(id, text) add_button(ui, group, arena, s8(text), UI_BID_ ##id, 0, ui->small_font); 1098 GLOBAL_MENU_BUTTONS 1099 #undef X 1100 return group; 1101 } 1102 1103 function Variable * 1104 add_global_menu(BeamformerUI *ui, Arena *arena, Variable *parent) 1105 { 1106 Variable *result = add_variable_group(ui, 0, arena, s8(""), VariableGroupKind_List, ui->small_font); 1107 result->parent = parent; 1108 return add_global_menu_to_group(ui, arena, result); 1109 } 1110 1111 function Variable * 1112 add_ui_view(BeamformerUI *ui, Variable *parent, Arena *arena, s8 name, u32 view_flags, b32 menu, b32 closable) 1113 { 1114 Variable *result = add_variable(ui, parent, arena, name, 0, VT_UI_VIEW, ui->small_font); 1115 UIView *view = &result->view; 1116 view->flags = view_flags; 1117 if (menu) view->menu = add_global_menu(ui, arena, result); 1118 if (closable) { 1119 view->close = add_button(ui, 0, arena, s8(""), UI_BID_VIEW_CLOSE, 0, ui->small_font); 1120 /* NOTE(rnp): we do this explicitly so that close doesn't end up in the view group */ 1121 view->close->parent = result; 1122 } 1123 return result; 1124 } 1125 1126 function Variable * 1127 add_floating_view(BeamformerUI *ui, Arena *arena, VariableType type, v2 at, Variable *child, b32 closable) 1128 { 1129 Variable *result = add_ui_view(ui, 0, arena, s8(""), UIViewFlag_Floating, 0, closable); 1130 result->type = type; 1131 result->view.rect.pos = at; 1132 result->view.child = child; 1133 1134 result->parent = &ui->floating_widget_sentinal; 1135 result->next = ui->floating_widget_sentinal.next; 1136 result->next->parent = result; 1137 ui->floating_widget_sentinal.next = result; 1138 return result; 1139 } 1140 1141 function void 1142 fill_beamformer_variable(Variable *var, s8 suffix, f32 *store, v2 limits, f32 display_scale, f32 scroll_scale) 1143 { 1144 BeamformerVariable *bv = &var->beamformer_variable; 1145 bv->suffix = suffix; 1146 bv->store = store; 1147 bv->display_scale = display_scale; 1148 bv->scroll_scale = scroll_scale; 1149 bv->limits = limits; 1150 } 1151 1152 function void 1153 add_beamformer_variable(BeamformerUI *ui, Variable *group, Arena *arena, s8 name, s8 suffix, f32 *store, 1154 v2 limits, f32 display_scale, f32 scroll_scale, u32 flags, Font font) 1155 { 1156 Variable *var = add_variable(ui, group, arena, name, flags, VT_BEAMFORMER_VARIABLE, font); 1157 fill_beamformer_variable(var, suffix, store, limits, display_scale, scroll_scale); 1158 } 1159 1160 function Variable * 1161 add_beamformer_parameters_view(Variable *parent, BeamformerCtx *ctx) 1162 { 1163 BeamformerUI *ui = ctx->ui; 1164 BeamformerUIParameters *bp = &ui->params; 1165 1166 v2 v2_inf = {.x = -F32_INFINITY, .y = F32_INFINITY}; 1167 1168 /* TODO(rnp): this can be closable once we have a way of opening new views */ 1169 Variable *result = add_ui_view(ui, parent, &ui->arena, s8("Parameters"), 0, 1, 0); 1170 Variable *group = result->view.child = add_variable(ui, result, &ui->arena, s8(""), 0, 1171 VT_GROUP, ui->font); 1172 1173 add_beamformer_variable(ui, group, &ui->arena, s8("Sampling Frequency:"), s8("[MHz]"), 1174 &bp->sampling_frequency, (v2){0}, 1e-6f, 0, 0, ui->font); 1175 1176 add_beamformer_variable(ui, group, &ui->arena, s8("Demodulation Frequency:"), s8("[MHz]"), 1177 &bp->demodulation_frequency, (v2){.y = 100e6f}, 1e-6f, 0, 0, ui->font); 1178 1179 add_beamformer_variable(ui, group, &ui->arena, s8("Speed of Sound:"), s8("[m/s]"), 1180 &bp->speed_of_sound, (v2){.y = 1e6f}, 1.0f, 10.0f, 1181 V_INPUT|V_TEXT|V_CAUSES_COMPUTE, ui->font); 1182 1183 group = add_variable_group(ui, group, &ui->arena, s8("Lateral Extent:"), 1184 VariableGroupKind_Vector, ui->font); 1185 { 1186 add_beamformer_variable(ui, group, &ui->arena, s8("Min:"), s8("[mm]"), 1187 bp->output_min_coordinate.E + 0, v2_inf, 1e3f, 0.5e-3f, 1188 V_INPUT|V_TEXT|V_CAUSES_COMPUTE, ui->font); 1189 1190 add_beamformer_variable(ui, group, &ui->arena, s8("Max:"), s8("[mm]"), 1191 bp->output_max_coordinate.E + 0, v2_inf, 1e3f, 0.5e-3f, 1192 V_INPUT|V_TEXT|V_CAUSES_COMPUTE, ui->font); 1193 } 1194 group = end_variable_group(group); 1195 1196 group = add_variable_group(ui, group, &ui->arena, s8("Axial Extent:"), 1197 VariableGroupKind_Vector, ui->font); 1198 { 1199 add_beamformer_variable(ui, group, &ui->arena, s8("Min:"), s8("[mm]"), 1200 bp->output_min_coordinate.E + 2, v2_inf, 1e3f, 0.5e-3f, 1201 V_INPUT|V_TEXT|V_CAUSES_COMPUTE, ui->font); 1202 1203 add_beamformer_variable(ui, group, &ui->arena, s8("Max:"), s8("[mm]"), 1204 bp->output_max_coordinate.E + 2, v2_inf, 1e3f, 0.5e-3f, 1205 V_INPUT|V_TEXT|V_CAUSES_COMPUTE, ui->font); 1206 } 1207 group = end_variable_group(group); 1208 1209 add_beamformer_variable(ui, group, &ui->arena, s8("Off Axis Position:"), s8("[mm]"), 1210 &bp->off_axis_pos, (v2){{-1e3f, 1e3f}}, 0.25e3f, 0.5e-3f, 1211 V_INPUT|V_TEXT|V_CAUSES_COMPUTE, ui->font); 1212 1213 read_only local_persist s8 beamform_plane_labels[] = {s8_comp("XZ"), s8_comp("YZ")}; 1214 add_variable_cycler(ui, group, &ui->arena, V_CAUSES_COMPUTE, ui->font, s8("Beamform Plane:"), 1215 (u32 *)&bp->beamform_plane, beamform_plane_labels, countof(beamform_plane_labels)); 1216 1217 add_beamformer_variable(ui, group, &ui->arena, s8("F#:"), s8(""), &bp->f_number, (v2){.y = 1e3f}, 1218 1, 0.1f, V_INPUT|V_TEXT|V_CAUSES_COMPUTE, ui->font); 1219 1220 add_variable_cycler(ui, group, &ui->arena, V_CAUSES_COMPUTE, ui->font, s8("Interpolation:"), 1221 &bp->interpolation_mode, beamformer_interpolation_mode_strings, 1222 countof(beamformer_interpolation_mode_strings)); 1223 1224 read_only local_persist s8 true_false_labels[] = {s8_comp("False"), s8_comp("True")}; 1225 add_variable_cycler(ui, group, &ui->arena, V_CAUSES_COMPUTE, ui->font, s8("Coherency Weighting:"), 1226 &bp->coherency_weighting, true_false_labels, countof(true_false_labels)); 1227 1228 return result; 1229 } 1230 1231 function void 1232 ui_beamformer_frame_view_convert(BeamformerUI *ui, Arena *arena, Variable *view, Variable *menu, 1233 BeamformerFrameViewKind kind, BeamformerFrameView *old, b32 log_scale) 1234 { 1235 assert(menu->group.first == menu->group.last && menu->group.first == 0); 1236 assert(view->type == VT_BEAMFORMER_FRAME_VIEW); 1237 1238 BeamformerFrameView *bv = view->generic; 1239 bv->kind = kind; 1240 bv->dirty = 1; 1241 1242 fill_variable(&bv->dynamic_range, view, s8("Dynamic Range:"), V_INPUT|V_TEXT|V_UPDATE_VIEW, 1243 VT_F32, ui->small_font); 1244 fill_variable(&bv->threshold, view, s8("Threshold:"), V_INPUT|V_TEXT|V_UPDATE_VIEW, 1245 VT_F32, ui->small_font); 1246 fill_variable(&bv->gamma, view, s8("Gamma:"), V_INPUT|V_TEXT|V_UPDATE_VIEW, 1247 VT_SCALED_F32, ui->small_font); 1248 1249 bv->dynamic_range.real32 = old? old->dynamic_range.real32 : 50.0f; 1250 bv->threshold.real32 = old? old->threshold.real32 : 55.0f; 1251 bv->gamma.scaled_real32.val = old? old->gamma.scaled_real32.val : 1.0f; 1252 bv->gamma.scaled_real32.scale = old? old->gamma.scaled_real32.scale : 0.05f; 1253 bv->min_coordinate = (old && old->frame)? old->frame->min_coordinate : (v3){0}; 1254 bv->max_coordinate = (old && old->frame)? old->frame->max_coordinate : (v3){0}; 1255 1256 #define X(_t, pretty) s8_comp(pretty), 1257 read_only local_persist s8 kind_labels[] = {BEAMFORMER_FRAME_VIEW_KIND_LIST}; 1258 #undef X 1259 bv->kind_cycler = add_variable_cycler(ui, menu, arena, V_EXTRA_ACTION, ui->small_font, 1260 s8("Kind:"), (u32 *)&bv->kind, kind_labels, countof(kind_labels)); 1261 1262 switch (kind) { 1263 case BeamformerFrameViewKind_3DXPlane:{ 1264 view->flags |= V_HIDES_CURSOR; 1265 resize_frame_view(bv, (iv2){{FRAME_VIEW_RENDER_TARGET_SIZE}}, 0); 1266 glTextureParameteri(bv->textures[0], GL_TEXTURE_MAG_FILTER, GL_LINEAR); 1267 glTextureParameteri(bv->textures[0], GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 1268 fill_variable(bv->x_plane_shifts + 0, view, s8("XZ Shift"), V_INPUT|V_HIDES_CURSOR, 1269 VT_X_PLANE_SHIFT, ui->small_font); 1270 fill_variable(bv->x_plane_shifts + 1, view, s8("YZ Shift"), V_INPUT|V_HIDES_CURSOR, 1271 VT_X_PLANE_SHIFT, ui->small_font); 1272 bv->demo = add_variable(ui, menu, arena, s8("Demo Mode"), V_INPUT|V_RADIO_BUTTON, VT_B32, ui->small_font); 1273 }break; 1274 default:{ 1275 view->flags &= ~(u32)V_HIDES_CURSOR; 1276 fill_variable(&bv->lateral_scale_bar, view, s8(""), V_INPUT, VT_SCALE_BAR, ui->small_font); 1277 fill_variable(&bv->axial_scale_bar, view, s8(""), V_INPUT, VT_SCALE_BAR, ui->small_font); 1278 ScaleBar *lateral = &bv->lateral_scale_bar.scale_bar; 1279 ScaleBar *axial = &bv->axial_scale_bar.scale_bar; 1280 lateral->direction = SB_LATERAL; 1281 axial->direction = SB_AXIAL; 1282 lateral->scroll_scale = (v2){{-0.5e-3f, 0.5e-3f}}; 1283 axial->scroll_scale = (v2){{ 0, 1.0e-3f}}; 1284 lateral->zoom_starting_coord = F32_INFINITY; 1285 axial->zoom_starting_coord = F32_INFINITY; 1286 1287 b32 copy = kind == BeamformerFrameViewKind_Copy; 1288 lateral->min_value = copy ? &bv->min_coordinate.x : &ui->params.output_min_coordinate.x; 1289 lateral->max_value = copy ? &bv->max_coordinate.x : &ui->params.output_max_coordinate.x; 1290 axial->min_value = copy ? &bv->min_coordinate.z : &ui->params.output_min_coordinate.z; 1291 axial->max_value = copy ? &bv->max_coordinate.z : &ui->params.output_max_coordinate.z; 1292 1293 #define X(id, text) add_button(ui, menu, arena, s8(text), UI_BID_ ##id, 0, ui->small_font); 1294 FRAME_VIEW_BUTTONS 1295 #undef X 1296 1297 bv->axial_scale_bar_active = add_variable(ui, menu, arena, s8("Axial Scale Bar"), 1298 V_INPUT|V_RADIO_BUTTON, VT_B32, ui->small_font); 1299 bv->lateral_scale_bar_active = add_variable(ui, menu, arena, s8("Lateral Scale Bar"), 1300 V_INPUT|V_RADIO_BUTTON, VT_B32, ui->small_font); 1301 1302 if (kind == BeamformerFrameViewKind_Latest) { 1303 bv->axial_scale_bar_active->bool32 = 1; 1304 bv->lateral_scale_bar_active->bool32 = 1; 1305 bv->axial_scale_bar.flags |= V_CAUSES_COMPUTE; 1306 bv->lateral_scale_bar.flags |= V_CAUSES_COMPUTE; 1307 } 1308 }break; 1309 } 1310 1311 bv->log_scale = add_variable(ui, menu, arena, s8("Log Scale"), 1312 V_INPUT|V_UPDATE_VIEW|V_RADIO_BUTTON, VT_B32, ui->small_font); 1313 bv->log_scale->bool32 = log_scale; 1314 1315 switch (kind) { 1316 case BeamformerFrameViewKind_Latest:{ 1317 #define X(_type, _id, pretty) s8_comp(pretty), 1318 read_only local_persist s8 labels[] = {BEAMFORMER_VIEW_PLANE_TAG_LIST s8_comp("Any")}; 1319 #undef X 1320 bv->cycler = add_variable_cycler(ui, menu, arena, 0, ui->small_font, s8("Live:"), 1321 &bv->cycler_state, labels, countof(labels)); 1322 bv->cycler_state = BeamformerViewPlaneTag_Count; 1323 }break; 1324 case BeamformerFrameViewKind_Indexed:{ 1325 bv->cycler = add_variable_cycler(ui, menu, arena, 0, ui->small_font, s8("Index:"), 1326 &bv->cycler_state, 0, BeamformerMaxSavedFrames); 1327 }break; 1328 default:{}break; 1329 } 1330 1331 add_global_menu_to_group(ui, arena, menu); 1332 } 1333 1334 function BeamformerFrameView * 1335 ui_beamformer_frame_view_new(BeamformerUI *ui, Arena *arena) 1336 { 1337 BeamformerFrameView *result = SLLPopFreelist(ui->view_freelist); 1338 if (result) zero_struct(result); 1339 else result = push_struct(arena, typeof(*result)); 1340 DLLPushDown(result, ui->views); 1341 return result; 1342 } 1343 1344 function Variable * 1345 add_beamformer_frame_view(BeamformerUI *ui, Variable *parent, Arena *arena, 1346 BeamformerFrameViewKind kind, b32 closable, BeamformerFrameView *old) 1347 { 1348 /* TODO(rnp): this can be always closable once we have a way of opening new views */ 1349 Variable *result = add_ui_view(ui, parent, arena, s8(""), UIViewFlag_CustomText, 1, closable); 1350 Variable *var = result->view.child = add_variable(ui, result, arena, s8(""), 0, 1351 VT_BEAMFORMER_FRAME_VIEW, ui->small_font); 1352 Variable *menu = result->view.menu = add_variable_group(ui, 0, arena, s8(""), 1353 VariableGroupKind_List, ui->small_font); 1354 menu->parent = result; 1355 var->generic = ui_beamformer_frame_view_new(ui, arena); 1356 ui_beamformer_frame_view_convert(ui, arena, var, menu, kind, old, old? old->log_scale->bool32 : 0); 1357 return result; 1358 } 1359 1360 function Variable * 1361 add_compute_progress_bar(Variable *parent, BeamformerCtx *ctx) 1362 { 1363 BeamformerUI *ui = ctx->ui; 1364 /* TODO(rnp): this can be closable once we have a way of opening new views */ 1365 Variable *result = add_ui_view(ui, parent, &ui->arena, s8(""), UIViewFlag_CustomText, 1, 0); 1366 result->view.child = add_variable(ui, result, &ui->arena, s8(""), 0, 1367 VT_COMPUTE_PROGRESS_BAR, ui->small_font); 1368 ComputeProgressBar *bar = &result->view.child->compute_progress_bar; 1369 bar->progress = &ctx->compute_context.processing_progress; 1370 bar->processing = &ctx->compute_context.processing_compute; 1371 1372 return result; 1373 } 1374 1375 function Variable * 1376 add_compute_stats_view(BeamformerUI *ui, Variable *parent, Arena *arena, BeamformerCtx *ctx) 1377 { 1378 /* TODO(rnp): this can be closable once we have a way of opening new views */ 1379 Variable *result = add_ui_view(ui, parent, arena, s8(""), UIViewFlag_CustomText, 0, 0); 1380 result->view.child = add_variable(ui, result, &ui->arena, s8(""), 0, 1381 VT_COMPUTE_STATS_VIEW, ui->small_font); 1382 1383 Variable *menu = result->view.menu = add_variable_group(ui, 0, arena, s8(""), 1384 VariableGroupKind_List, ui->small_font); 1385 menu->parent = result; 1386 1387 #define X(_k, label) s8_comp(label), 1388 read_only local_persist s8 labels[] = {COMPUTE_STATS_VIEW_LIST}; 1389 #undef X 1390 1391 ComputeStatsView *csv = &result->view.child->compute_stats_view; 1392 csv->compute_shader_stats = ctx->compute_shader_stats; 1393 csv->cycler = add_variable_cycler(ui, menu, arena, 0, ui->small_font, s8("Stats View:"), 1394 (u32 *)&csv->kind, labels, countof(labels)); 1395 add_global_menu_to_group(ui, arena, menu); 1396 return result; 1397 } 1398 1399 function Variable * 1400 add_live_controls_view(BeamformerUI *ui, Variable *parent, Arena *arena) 1401 { 1402 BeamformerSharedMemory *sm = ui->shared_memory.region; 1403 BeamformerLiveImagingParameters *lip = &sm->live_imaging_parameters; 1404 /* TODO(rnp): this can be closable once we have a way of opening new views */ 1405 Variable *result = add_ui_view(ui, parent, &ui->arena, s8("Live Controls"), 0, 1, 0); 1406 result->view.child = add_variable(ui, result, &ui->arena, s8(""), 0, 1407 VT_LIVE_CONTROLS_VIEW, ui->small_font); 1408 Variable *view = result->view.child; 1409 BeamformerLiveControlsView *lv = view->generic = push_struct(arena, typeof(*lv)); 1410 1411 fill_variable(&lv->transmit_power, view, s8(""), V_INPUT|V_LIVE_CONTROL, 1412 VT_BEAMFORMER_VARIABLE, ui->small_font); 1413 fill_beamformer_variable(&lv->transmit_power, s8(""), &lip->transmit_power, (v2){{0, 1.0f}}, 100.0f, 0.05f); 1414 1415 for (u32 i = 0; i < countof(lv->tgc_control_points); i++) { 1416 Variable *v = lv->tgc_control_points + i; 1417 fill_variable(v, view, s8(""), V_INPUT|V_LIVE_CONTROL, VT_BEAMFORMER_VARIABLE, ui->small_font); 1418 fill_beamformer_variable(v, s8(""), lip->tgc_control_points + i, (v2){{0, 1.0f}}, 0, 0.05f); 1419 } 1420 1421 fill_variable(&lv->stop_button, view, s8("Stop Imaging"), V_INPUT|V_LIVE_CONTROL, 1422 VT_B32, ui->small_font); 1423 1424 read_only local_persist s8 save_labels[] = {s8_comp("Save Data"), s8_comp("Saving...")}; 1425 fill_variable(&lv->save_button, view, s8("Save Data"), V_INPUT|V_LIVE_CONTROL, 1426 VT_CYCLER, ui->small_font); 1427 fill_variable_cycler(&lv->save_button, &lip->save_active, save_labels, countof(save_labels)); 1428 1429 fill_variable(&lv->save_text, view, s8(""), V_INPUT|V_TEXT|V_LIVE_CONTROL, 1430 VT_LIVE_CONTROLS_STRING, ui->small_font); 1431 lv->save_text.generic = lip; 1432 1433 return result; 1434 } 1435 1436 function Variable * 1437 ui_split_region(BeamformerUI *ui, Variable *region, Variable *split_side, RegionSplitDirection direction) 1438 { 1439 Variable *result = add_ui_split(ui, region, &ui->arena, s8(""), 0.5, direction, ui->small_font); 1440 if (split_side == region->region_split.left) { 1441 region->region_split.left = result; 1442 } else { 1443 region->region_split.right = result; 1444 } 1445 split_side->parent = result; 1446 result->region_split.left = split_side; 1447 return result; 1448 } 1449 1450 function void 1451 ui_add_live_frame_view(BeamformerUI *ui, Variable *view, RegionSplitDirection direction, 1452 BeamformerFrameViewKind kind) 1453 { 1454 Variable *region = view->parent; 1455 assert(region->type == VT_UI_REGION_SPLIT); 1456 assert(view->type == VT_UI_VIEW); 1457 Variable *new_region = ui_split_region(ui, region, view, direction); 1458 new_region->region_split.right = add_beamformer_frame_view(ui, new_region, &ui->arena, kind, 1, 0); 1459 } 1460 1461 function void 1462 ui_beamformer_frame_view_copy_frame(BeamformerUI *ui, BeamformerFrameView *new, BeamformerFrameView *old) 1463 { 1464 assert(old->frame); 1465 new->frame = SLLPopFreelist(ui->frame_freelist); 1466 if (!new->frame) new->frame = push_struct(&ui->arena, typeof(*new->frame)); 1467 1468 mem_copy(new->frame, old->frame, sizeof(*new->frame)); 1469 new->frame->texture = 0; 1470 new->frame->next = 0; 1471 alloc_beamform_frame(new->frame, old->frame->dim, old->frame->gl_kind, s8("Frame Copy: "), ui->arena); 1472 1473 glCopyImageSubData(old->frame->texture, GL_TEXTURE_3D, 0, 0, 0, 0, 1474 new->frame->texture, GL_TEXTURE_3D, 0, 0, 0, 0, 1475 new->frame->dim.x, new->frame->dim.y, new->frame->dim.z); 1476 glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT); 1477 /* TODO(rnp): x vs y here */ 1478 resize_frame_view(new, (iv2){{new->frame->dim.x, new->frame->dim.z}}, 1); 1479 } 1480 1481 function void 1482 ui_copy_frame(BeamformerUI *ui, Variable *view, RegionSplitDirection direction) 1483 { 1484 Variable *region = view->parent; 1485 assert(region->type == VT_UI_REGION_SPLIT); 1486 assert(view->type == VT_UI_VIEW); 1487 1488 BeamformerFrameView *old = view->view.child->generic; 1489 /* TODO(rnp): hack; it would be better if this was unreachable with a 0 old->frame */ 1490 if (!old->frame) 1491 return; 1492 1493 Variable *new_region = ui_split_region(ui, region, view, direction); 1494 new_region->region_split.right = add_beamformer_frame_view(ui, new_region, &ui->arena, 1495 BeamformerFrameViewKind_Copy, 1, old); 1496 1497 BeamformerFrameView *bv = new_region->region_split.right->view.child->generic; 1498 ui_beamformer_frame_view_copy_frame(ui, bv, old); 1499 } 1500 1501 function v3 1502 beamformer_frame_view_plane_size(BeamformerUI *ui, BeamformerFrameView *view) 1503 { 1504 v3 result; 1505 if (view->kind == BeamformerFrameViewKind_3DXPlane) { 1506 result = v3_sub(ui->params.output_max_coordinate, ui->params.output_min_coordinate); 1507 swap(result.y, result.z); 1508 result.x = MAX(1e-3f, result.x); 1509 result.y = MAX(1e-3f, result.y); 1510 result.z = MAX(1e-3f, result.z); 1511 } else { 1512 v2 size = v2_sub(XZ(view->max_coordinate), XZ(view->min_coordinate)); 1513 result = (v3){.x = size.x, .y = size.y}; 1514 } 1515 return result; 1516 } 1517 1518 function f32 1519 x_plane_rotation_for_view_plane(BeamformerFrameView *view, BeamformerViewPlaneTag tag) 1520 { 1521 f32 result = view->rotation; 1522 if (tag == BeamformerViewPlaneTag_YZ) 1523 result += 0.25f; 1524 return result; 1525 } 1526 1527 function v2 1528 normalized_p_in_rect(Rect r, v2 p, b32 invert_y) 1529 { 1530 v2 result = v2_div(v2_scale(v2_sub(p, r.pos), 2.0f), r.size); 1531 if (invert_y) result = (v2){{result.x - 1.0f, 1.0f - result.y}}; 1532 else result = v2_sub(result, (v2){{1.0f, 1.0f}}); 1533 return result; 1534 } 1535 1536 function v3 1537 x_plane_position(BeamformerUI *ui) 1538 { 1539 f32 y_min = ui->params.output_min_coordinate.E[2]; 1540 f32 y_max = ui->params.output_max_coordinate.E[2]; 1541 v3 result = {.y = y_min + (y_max - y_min) / 2}; 1542 return result; 1543 } 1544 1545 function v3 1546 offset_x_plane_position(BeamformerUI *ui, BeamformerFrameView *view, BeamformerViewPlaneTag tag) 1547 { 1548 BeamformerSharedMemory *sm = ui->shared_memory.region; 1549 BeamformerLiveImagingParameters *li = &sm->live_imaging_parameters; 1550 m4 x_rotation = m4_rotation_about_y(x_plane_rotation_for_view_plane(view, tag)); 1551 v3 Z = x_rotation.c[2].xyz; 1552 v3 offset = v3_scale(Z, li->image_plane_offsets[tag]); 1553 v3 result = v3_add(x_plane_position(ui), offset); 1554 return result; 1555 } 1556 1557 function v3 1558 camera_for_x_plane_view(BeamformerUI *ui, BeamformerFrameView *view) 1559 { 1560 v3 size = beamformer_frame_view_plane_size(ui, view); 1561 v3 target = x_plane_position(ui); 1562 f32 dist = v2_magnitude(XY(size)); 1563 v3 result = v3_add(target, (v3){{dist, -0.5f * size.y * tan_f32(50.0f * PI / 180.0f), dist}}); 1564 return result; 1565 } 1566 1567 function m4 1568 view_matrix_for_x_plane_view(BeamformerUI *ui, BeamformerFrameView *view, v3 camera) 1569 { 1570 assert(view->kind == BeamformerFrameViewKind_3DXPlane); 1571 m4 result = camera_look_at(camera, x_plane_position(ui)); 1572 return result; 1573 } 1574 1575 function m4 1576 projection_matrix_for_x_plane_view(BeamformerFrameView *view) 1577 { 1578 assert(view->kind == BeamformerFrameViewKind_3DXPlane); 1579 f32 aspect = (f32)view->texture_dim.w / (f32)view->texture_dim.h; 1580 m4 result = perspective_projection(10e-3f, 500e-3f, 45.0f * PI / 180.0f, aspect); 1581 return result; 1582 } 1583 1584 function ray 1585 ray_for_x_plane_view(BeamformerUI *ui, BeamformerFrameView *view, v2 uv) 1586 { 1587 assert(view->kind == BeamformerFrameViewKind_3DXPlane); 1588 ray result = {.origin = camera_for_x_plane_view(ui, view)}; 1589 v4 ray_clip = {{uv.x, uv.y, -1.0f, 1.0f}}; 1590 1591 /* TODO(rnp): combine these so we only do one matrix inversion */ 1592 m4 proj_m = projection_matrix_for_x_plane_view(view); 1593 m4 view_m = view_matrix_for_x_plane_view(ui, view, result.origin); 1594 m4 proj_inv = m4_inverse(proj_m); 1595 m4 view_inv = m4_inverse(view_m); 1596 1597 v4 ray_eye = {.z = -1}; 1598 ray_eye.x = v4_dot(m4_row(proj_inv, 0), ray_clip); 1599 ray_eye.y = v4_dot(m4_row(proj_inv, 1), ray_clip); 1600 result.direction = v3_normalize(m4_mul_v4(view_inv, ray_eye).xyz); 1601 1602 return result; 1603 } 1604 1605 function BeamformerViewPlaneTag 1606 view_plane_tag_from_x_plane_shift(BeamformerFrameView *view, Variable *x_plane_shift) 1607 { 1608 assert(BETWEEN(x_plane_shift, view->x_plane_shifts + 0, view->x_plane_shifts + 1)); 1609 BeamformerViewPlaneTag result = BeamformerViewPlaneTag_XZ; 1610 if (x_plane_shift == view->x_plane_shifts + 1) 1611 result = BeamformerViewPlaneTag_YZ; 1612 return result; 1613 } 1614 1615 function void 1616 render_single_xplane(BeamformerUI *ui, BeamformerFrameView *view, Variable *x_plane_shift, 1617 u32 program, f32 rotation_turns, v3 translate, BeamformerViewPlaneTag tag) 1618 { 1619 u32 texture = 0; 1620 if (ui->latest_plane[tag]) 1621 texture = ui->latest_plane[tag]->texture; 1622 1623 v3 scale = beamformer_frame_view_plane_size(ui, view); 1624 m4 model_transform = y_aligned_volume_transform(scale, translate, rotation_turns); 1625 1626 v4 colour = v4_lerp(FG_COLOUR, HOVERED_COLOUR, x_plane_shift->hover_t); 1627 glProgramUniformMatrix4fv(program, FRAME_VIEW_MODEL_MATRIX_LOC, 1, 0, model_transform.E); 1628 glProgramUniform4fv(program, FRAME_VIEW_BB_COLOUR_LOC, 1, colour.E); 1629 glProgramUniform1ui(program, FRAME_VIEW_SOLID_BB_LOC, 0); 1630 glBindTextureUnit(0, texture); 1631 glDrawElements(GL_TRIANGLES, ui->unit_cube_model.elements, GL_UNSIGNED_SHORT, 1632 (void *)ui->unit_cube_model.elements_offset); 1633 1634 XPlaneShift *xp = &x_plane_shift->x_plane_shift; 1635 v3 xp_delta = v3_sub(xp->end_point, xp->start_point); 1636 if (!f32_cmp(v3_magnitude(xp_delta), 0)) { 1637 m4 x_rotation = m4_rotation_about_y(rotation_turns); 1638 v3 Z = x_rotation.c[2].xyz; 1639 v3 f = v3_scale(Z, v3_dot(Z, v3_sub(xp->end_point, xp->start_point))); 1640 1641 /* TODO(rnp): there is no reason to compute the rotation matrix again */ 1642 model_transform = y_aligned_volume_transform(scale, v3_add(f, translate), rotation_turns); 1643 1644 glProgramUniformMatrix4fv(program, FRAME_VIEW_MODEL_MATRIX_LOC, 1, 0, model_transform.E); 1645 glProgramUniform1ui(program, FRAME_VIEW_SOLID_BB_LOC, 1); 1646 glProgramUniform4fv(program, FRAME_VIEW_BB_COLOUR_LOC, 1, HOVERED_COLOUR.E); 1647 glDrawElements(GL_TRIANGLES, ui->unit_cube_model.elements, GL_UNSIGNED_SHORT, 1648 (void *)ui->unit_cube_model.elements_offset); 1649 } 1650 } 1651 1652 function void 1653 render_3D_xplane(BeamformerUI *ui, BeamformerFrameView *view, u32 program) 1654 { 1655 if (view->demo->bool32) { 1656 view->rotation += dt_for_frame * 0.125f; 1657 if (view->rotation > 1.0f) view->rotation -= 1.0f; 1658 } 1659 1660 v3 camera = camera_for_x_plane_view(ui, view); 1661 m4 view_m = view_matrix_for_x_plane_view(ui, view, camera); 1662 m4 projection = projection_matrix_for_x_plane_view(view); 1663 1664 glProgramUniformMatrix4fv(program, FRAME_VIEW_VIEW_MATRIX_LOC, 1, 0, view_m.E); 1665 glProgramUniformMatrix4fv(program, FRAME_VIEW_PROJ_MATRIX_LOC, 1, 0, projection.E); 1666 glProgramUniform1f(program, FRAME_VIEW_BB_FRACTION_LOC, FRAME_VIEW_BB_FRACTION); 1667 1668 v3 model_translate = offset_x_plane_position(ui, view, BeamformerViewPlaneTag_XZ); 1669 render_single_xplane(ui, view, view->x_plane_shifts + 0, program, 1670 x_plane_rotation_for_view_plane(view, BeamformerViewPlaneTag_XZ), 1671 model_translate, BeamformerViewPlaneTag_XZ); 1672 model_translate = offset_x_plane_position(ui, view, BeamformerViewPlaneTag_YZ); 1673 model_translate.y -= 0.0001f; 1674 render_single_xplane(ui, view, view->x_plane_shifts + 1, program, 1675 x_plane_rotation_for_view_plane(view, BeamformerViewPlaneTag_YZ), 1676 model_translate, BeamformerViewPlaneTag_YZ); 1677 } 1678 1679 function void 1680 render_2D_plane(BeamformerUI *ui, BeamformerFrameView *view, u32 program) 1681 { 1682 m4 view_m = m4_identity(); 1683 v3 size = beamformer_frame_view_plane_size(ui, view); 1684 m4 model = m4_scale(size); 1685 m4 projection = orthographic_projection(0, 1, size.y / 2, size.x / 2); 1686 1687 glProgramUniformMatrix4fv(program, FRAME_VIEW_MODEL_MATRIX_LOC, 1, 0, model.E); 1688 glProgramUniformMatrix4fv(program, FRAME_VIEW_VIEW_MATRIX_LOC, 1, 0, view_m.E); 1689 glProgramUniformMatrix4fv(program, FRAME_VIEW_PROJ_MATRIX_LOC, 1, 0, projection.E); 1690 1691 glProgramUniform1f(program, FRAME_VIEW_BB_FRACTION_LOC, 0); 1692 glBindTextureUnit(0, view->frame->texture); 1693 glDrawElements(GL_TRIANGLES, ui->unit_cube_model.elements, GL_UNSIGNED_SHORT, 1694 (void *)ui->unit_cube_model.elements_offset); 1695 } 1696 1697 function b32 1698 frame_view_ready_to_present(BeamformerUI *ui, BeamformerFrameView *view) 1699 { 1700 b32 result = !iv2_equal((iv2){0}, view->texture_dim) && view->frame; 1701 result |= view->kind == BeamformerFrameViewKind_3DXPlane && 1702 ui->latest_plane[BeamformerViewPlaneTag_Count]; 1703 return result; 1704 } 1705 1706 function b32 1707 view_update(BeamformerUI *ui, BeamformerFrameView *view) 1708 { 1709 if (view->kind == BeamformerFrameViewKind_Latest) { 1710 u32 index = *view->cycler->cycler.state; 1711 view->dirty |= view->frame != ui->latest_plane[index]; 1712 view->frame = ui->latest_plane[index]; 1713 if (view->dirty) { 1714 view->min_coordinate = ui->params.output_min_coordinate; 1715 view->max_coordinate = ui->params.output_max_coordinate; 1716 } 1717 } 1718 1719 /* TODO(rnp): x-z or y-z */ 1720 /* TODO(rnp): add method of setting a target size in frame view */ 1721 iv2 current = view->texture_dim; 1722 iv2 target = {.w = ui->params.output_points.E[0], .h = ui->params.output_points.E[2]}; 1723 if (view->kind != BeamformerFrameViewKind_Copy && 1724 view->kind != BeamformerFrameViewKind_3DXPlane && 1725 !iv2_equal(current, target) && !iv2_equal(target, (iv2){0})) 1726 { 1727 resize_frame_view(view, target, 1); 1728 view->dirty = 1; 1729 } 1730 view->dirty |= ui->frame_view_render_context->updated; 1731 view->dirty |= view->kind == BeamformerFrameViewKind_3DXPlane; 1732 1733 b32 result = frame_view_ready_to_present(ui, view) && view->dirty; 1734 return result; 1735 } 1736 1737 function void 1738 update_frame_views(BeamformerUI *ui, Rect window) 1739 { 1740 FrameViewRenderContext *ctx = ui->frame_view_render_context; 1741 b32 fbo_bound = 0; 1742 for (BeamformerFrameView *view = ui->views; view; view = view->next) { 1743 if (view_update(ui, view)) { 1744 if (!fbo_bound) { 1745 fbo_bound = 1; 1746 glBindFramebuffer(GL_FRAMEBUFFER, ctx->framebuffers[0]); 1747 glUseProgram(ctx->shader); 1748 glBindVertexArray(ui->unit_cube_model.vao); 1749 glEnable(GL_DEPTH_TEST); 1750 } 1751 1752 u32 fb = ctx->framebuffers[0]; 1753 u32 program = ctx->shader; 1754 glViewport(0, 0, view->texture_dim.w, view->texture_dim.h); 1755 glProgramUniform1f(program, FRAME_VIEW_THRESHOLD_LOC, view->threshold.real32); 1756 glProgramUniform1f(program, FRAME_VIEW_DYNAMIC_RANGE_LOC, view->dynamic_range.real32); 1757 glProgramUniform1f(program, FRAME_VIEW_GAMMA_LOC, view->gamma.scaled_real32.val); 1758 glProgramUniform1ui(program, FRAME_VIEW_LOG_SCALE_LOC, view->log_scale->bool32); 1759 1760 if (view->kind == BeamformerFrameViewKind_3DXPlane) { 1761 glNamedFramebufferRenderbuffer(fb, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, ctx->renderbuffers[0]); 1762 glNamedFramebufferRenderbuffer(fb, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, ctx->renderbuffers[1]); 1763 glClearNamedFramebufferfv(fb, GL_COLOR, 0, (f32 []){0, 0, 0, 0}); 1764 glClearNamedFramebufferfv(fb, GL_DEPTH, 0, (f32 []){1}); 1765 render_3D_xplane(ui, view, program); 1766 /* NOTE(rnp): resolve multisampled scene */ 1767 glNamedFramebufferTexture(ctx->framebuffers[1], GL_COLOR_ATTACHMENT0, view->textures[0], 0); 1768 glBlitNamedFramebuffer(fb, ctx->framebuffers[1], 0, 0, FRAME_VIEW_RENDER_TARGET_SIZE, 1769 0, 0, FRAME_VIEW_RENDER_TARGET_SIZE, GL_COLOR_BUFFER_BIT, GL_NEAREST); 1770 } else { 1771 glNamedFramebufferTexture(fb, GL_COLOR_ATTACHMENT0, view->textures[0], 0); 1772 glNamedFramebufferTexture(fb, GL_DEPTH_ATTACHMENT, view->textures[1], 0); 1773 glClearNamedFramebufferfv(fb, GL_COLOR, 0, (f32 []){0, 0, 0, 0}); 1774 glClearNamedFramebufferfv(fb, GL_DEPTH, 0, (f32 []){1}); 1775 render_2D_plane(ui, view, program); 1776 } 1777 glGenerateTextureMipmap(view->textures[0]); 1778 view->dirty = 0; 1779 } 1780 } 1781 if (fbo_bound) { 1782 glBindFramebuffer(GL_FRAMEBUFFER, 0); 1783 glViewport((i32)window.pos.x, (i32)window.pos.y, (i32)window.size.w, (i32)window.size.h); 1784 /* NOTE(rnp): I don't trust raylib to not mess with us */ 1785 glBindVertexArray(0); 1786 glDisable(GL_DEPTH_TEST); 1787 } 1788 } 1789 1790 function Color 1791 colour_from_normalized(v4 rgba) 1792 { 1793 Color result = {.r = (u8)(rgba.r * 255.0f), .g = (u8)(rgba.g * 255.0f), 1794 .b = (u8)(rgba.b * 255.0f), .a = (u8)(rgba.a * 255.0f)}; 1795 return result; 1796 } 1797 1798 function Color 1799 fade(Color a, f32 visibility) 1800 { 1801 a.a = (u8)((f32)a.a * visibility); 1802 return a; 1803 } 1804 1805 function v2 1806 draw_text_base(Font font, s8 text, v2 pos, Color colour) 1807 { 1808 v2 off = v2_floor(pos); 1809 f32 glyph_pad = (f32)font.glyphPadding; 1810 for (iz i = 0; i < text.len; i++) { 1811 /* NOTE: assumes font glyphs are ordered ASCII */ 1812 i32 idx = text.data[i] - 0x20; 1813 Rectangle dst = { 1814 off.x + (f32)font.glyphs[idx].offsetX - glyph_pad, 1815 off.y + (f32)font.glyphs[idx].offsetY - glyph_pad, 1816 font.recs[idx].width + 2.0f * glyph_pad, 1817 font.recs[idx].height + 2.0f * glyph_pad 1818 }; 1819 Rectangle src = { 1820 font.recs[idx].x - glyph_pad, 1821 font.recs[idx].y - glyph_pad, 1822 font.recs[idx].width + 2.0f * glyph_pad, 1823 font.recs[idx].height + 2.0f * glyph_pad 1824 }; 1825 DrawTexturePro(font.texture, src, dst, (Vector2){0}, 0, colour); 1826 1827 off.x += (f32)font.glyphs[idx].advanceX; 1828 if (font.glyphs[idx].advanceX == 0) 1829 off.x += font.recs[idx].width; 1830 } 1831 v2 result = {{off.x - pos.x, (f32)font.baseSize}}; 1832 return result; 1833 } 1834 1835 /* NOTE(rnp): expensive but of the available options in raylib this gives the best results */ 1836 function v2 1837 draw_outlined_text(s8 text, v2 pos, TextSpec *ts) 1838 { 1839 f32 ow = ts->outline_thick; 1840 Color outline = colour_from_normalized(ts->outline_colour); 1841 Color colour = colour_from_normalized(ts->colour); 1842 draw_text_base(*ts->font, text, v2_sub(pos, (v2){{ ow, ow}}), outline); 1843 draw_text_base(*ts->font, text, v2_sub(pos, (v2){{ ow, -ow}}), outline); 1844 draw_text_base(*ts->font, text, v2_sub(pos, (v2){{-ow, ow}}), outline); 1845 draw_text_base(*ts->font, text, v2_sub(pos, (v2){{-ow, -ow}}), outline); 1846 1847 v2 result = draw_text_base(*ts->font, text, pos, colour); 1848 1849 return result; 1850 } 1851 1852 function v2 1853 draw_text(s8 text, v2 pos, TextSpec *ts) 1854 { 1855 if (ts->flags & TF_ROTATED) { 1856 rlPushMatrix(); 1857 rlTranslatef(pos.x, pos.y, 0); 1858 rlRotatef(ts->rotation, 0, 0, 1); 1859 pos = (v2){0}; 1860 } 1861 1862 v2 result = measure_text(*ts->font, text); 1863 /* TODO(rnp): the size of this should be stored for each font */ 1864 s8 ellipsis = s8("..."); 1865 b32 clamped = ts->flags & TF_LIMITED && result.w > ts->limits.size.w; 1866 if (clamped) { 1867 f32 ellipsis_width = measure_text(*ts->font, ellipsis).x; 1868 if (ellipsis_width < ts->limits.size.w) { 1869 text = clamp_text_to_width(*ts->font, text, ts->limits.size.w - ellipsis_width); 1870 } else { 1871 text.len = 0; 1872 ellipsis.len = 0; 1873 } 1874 } 1875 1876 Color colour = colour_from_normalized(ts->colour); 1877 if (ts->flags & TF_OUTLINED) result.x = draw_outlined_text(text, pos, ts).x; 1878 else result.x = draw_text_base(*ts->font, text, pos, colour).x; 1879 1880 if (clamped) { 1881 pos.x += result.x; 1882 if (ts->flags & TF_OUTLINED) result.x += draw_outlined_text(ellipsis, pos, ts).x; 1883 else result.x += draw_text_base(*ts->font, ellipsis, pos, 1884 colour).x; 1885 } 1886 1887 if (ts->flags & TF_ROTATED) rlPopMatrix(); 1888 1889 return result; 1890 } 1891 1892 function Rect 1893 extend_rect_centered(Rect r, v2 delta) 1894 { 1895 r.size.w += delta.x; 1896 r.size.h += delta.y; 1897 r.pos.x -= delta.x / 2; 1898 r.pos.y -= delta.y / 2; 1899 return r; 1900 } 1901 1902 function Rect 1903 shrink_rect_centered(Rect r, v2 delta) 1904 { 1905 delta.x = MIN(delta.x, r.size.w); 1906 delta.y = MIN(delta.y, r.size.h); 1907 r.size.w -= delta.x; 1908 r.size.h -= delta.y; 1909 r.pos.x += delta.x / 2; 1910 r.pos.y += delta.y / 2; 1911 return r; 1912 } 1913 1914 function Rect 1915 scale_rect_centered(Rect r, v2 scale) 1916 { 1917 Rect or = r; 1918 r.size.w *= scale.x; 1919 r.size.h *= scale.y; 1920 r.pos.x += (or.size.w - r.size.w) / 2; 1921 r.pos.y += (or.size.h - r.size.h) / 2; 1922 return r; 1923 } 1924 1925 function b32 1926 interactions_equal(Interaction a, Interaction b) 1927 { 1928 b32 result = (a.kind == b.kind) && (a.generic == b.generic); 1929 return result; 1930 } 1931 1932 function b32 1933 interaction_is_sticky(Interaction a) 1934 { 1935 b32 result = a.kind == InteractionKind_Text || a.kind == InteractionKind_Ruler; 1936 return result; 1937 } 1938 1939 function b32 1940 interaction_is_hot(BeamformerUI *ui, Interaction a) 1941 { 1942 b32 result = interactions_equal(ui->hot_interaction, a); 1943 return result; 1944 } 1945 1946 function b32 1947 point_in_rect(v2 p, Rect r) 1948 { 1949 v2 end = v2_add(r.pos, r.size); 1950 b32 result = BETWEEN(p.x, r.pos.x, end.x) & BETWEEN(p.y, r.pos.y, end.y); 1951 return result; 1952 } 1953 1954 function v2 1955 screen_point_to_world_2d(v2 p, v2 screen_min, v2 screen_max, v2 world_min, v2 world_max) 1956 { 1957 v2 pixels_to_m = v2_div(v2_sub(world_max, world_min), v2_sub(screen_max, screen_min)); 1958 v2 result = v2_add(v2_mul(v2_sub(p, screen_min), pixels_to_m), world_min); 1959 return result; 1960 } 1961 1962 function v2 1963 world_point_to_screen_2d(v2 p, v2 world_min, v2 world_max, v2 screen_min, v2 screen_max) 1964 { 1965 v2 m_to_pixels = v2_div(v2_sub(screen_max, screen_min), v2_sub(world_max, world_min)); 1966 v2 result = v2_add(v2_mul(v2_sub(p, world_min), m_to_pixels), screen_min); 1967 return result; 1968 } 1969 1970 function b32 1971 hover_interaction(BeamformerUI *ui, v2 mouse, Interaction interaction) 1972 { 1973 Variable *var = interaction.var; 1974 b32 result = point_in_rect(mouse, interaction.rect); 1975 if (result) ui->next_interaction = interaction; 1976 if (interaction_is_hot(ui, interaction)) var->hover_t += HOVER_SPEED * dt_for_frame; 1977 else var->hover_t -= HOVER_SPEED * dt_for_frame; 1978 var->hover_t = CLAMP01(var->hover_t); 1979 return result; 1980 } 1981 1982 function void 1983 draw_close_button(BeamformerUI *ui, Variable *close, v2 mouse, Rect r, v2 x_scale) 1984 { 1985 assert(close->type == VT_UI_BUTTON); 1986 hover_interaction(ui, mouse, auto_interaction(r, close)); 1987 1988 Color colour = colour_from_normalized(v4_lerp(MENU_CLOSE_COLOUR, FG_COLOUR, close->hover_t)); 1989 r = scale_rect_centered(r, x_scale); 1990 DrawLineEx(r.pos.rl, v2_add(r.pos, r.size).rl, 4, colour); 1991 DrawLineEx(v2_add(r.pos, (v2){.x = r.size.w}).rl, 1992 v2_add(r.pos, (v2){.y = r.size.h}).rl, 4, colour); 1993 } 1994 1995 function Rect 1996 draw_title_bar(BeamformerUI *ui, Arena arena, Variable *ui_view, Rect r, v2 mouse) 1997 { 1998 assert(ui_view->type == VT_UI_VIEW); 1999 UIView *view = &ui_view->view; 2000 2001 s8 title = ui_view->name; 2002 if (view->flags & UIViewFlag_CustomText) { 2003 Stream buf = arena_stream(arena); 2004 push_custom_view_title(&buf, ui_view->view.child); 2005 title = arena_stream_commit(&arena, &buf); 2006 } 2007 2008 Rect result, title_rect; 2009 cut_rect_vertical(r, (f32)ui->small_font.baseSize + TITLE_BAR_PAD, &title_rect, &result); 2010 cut_rect_vertical(result, LISTING_LINE_PAD, 0, &result); 2011 2012 DrawRectangleRec(title_rect.rl, BLACK); 2013 2014 title_rect = shrink_rect_centered(title_rect, (v2){.x = 1.5f * TITLE_BAR_PAD}); 2015 DrawRectangleRounded(title_rect.rl, 0.5f, 0, fade(colour_from_normalized(BG_COLOUR), 0.55f)); 2016 title_rect = shrink_rect_centered(title_rect, (v2){.x = 3.0f * TITLE_BAR_PAD}); 2017 2018 if (view->close) { 2019 Rect close; 2020 cut_rect_horizontal(title_rect, title_rect.size.w - title_rect.size.h, &title_rect, &close); 2021 draw_close_button(ui, view->close, mouse, close, (v2){{0.4f, 0.4f}}); 2022 } 2023 2024 if (view->menu) { 2025 Rect menu; 2026 cut_rect_horizontal(title_rect, title_rect.size.w - title_rect.size.h, &title_rect, &menu); 2027 Interaction interaction = {.kind = InteractionKind_Menu, .var = view->menu, .rect = menu}; 2028 hover_interaction(ui, mouse, interaction); 2029 2030 Color colour = colour_from_normalized(v4_lerp(MENU_PLUS_COLOUR, FG_COLOUR, view->menu->hover_t)); 2031 menu = shrink_rect_centered(menu, (v2){{14.0f, 14.0f}}); 2032 DrawLineEx(v2_add(menu.pos, (v2){.x = menu.size.w / 2}).rl, 2033 v2_add(menu.pos, (v2){.x = menu.size.w / 2, .y = menu.size.h}).rl, 4, colour); 2034 DrawLineEx(v2_add(menu.pos, (v2){.y = menu.size.h / 2}).rl, 2035 v2_add(menu.pos, (v2){.x = menu.size.w, .y = menu.size.h / 2}).rl, 4, colour); 2036 } 2037 2038 v2 title_pos = title_rect.pos; 2039 title_pos.y += 0.5f * TITLE_BAR_PAD; 2040 TextSpec text_spec = {.font = &ui->small_font, .flags = TF_LIMITED, .colour = FG_COLOUR, 2041 .limits.size = title_rect.size}; 2042 draw_text(title, title_pos, &text_spec); 2043 2044 return result; 2045 } 2046 2047 /* TODO(rnp): once this has more callers decide if it would be better for this to take 2048 * an orientation rather than force CCW/right-handed */ 2049 function void 2050 draw_ruler(BeamformerUI *ui, Arena arena, v2 start_point, v2 end_point, 2051 f32 start_value, f32 end_value, f32 *markers, u32 marker_count, 2052 u32 segments, s8 suffix, v4 marker_colour, v4 txt_colour) 2053 { 2054 b32 draw_plus = SIGN(start_value) != SIGN(end_value); 2055 2056 end_point = v2_sub(end_point, start_point); 2057 f32 rotation = atan2_f32(end_point.y, end_point.x) * 180 / PI; 2058 2059 rlPushMatrix(); 2060 rlTranslatef(start_point.x, start_point.y, 0); 2061 rlRotatef(rotation, 0, 0, 1); 2062 2063 f32 inc = v2_magnitude(end_point) / (f32)segments; 2064 f32 value_inc = (end_value - start_value) / (f32)segments; 2065 f32 value = start_value; 2066 2067 Stream buf = arena_stream(arena); 2068 v2 sp = {0}, ep = {.y = RULER_TICK_LENGTH}; 2069 v2 tp = {{(f32)ui->small_font.baseSize / 2.0f, ep.y + RULER_TEXT_PAD}}; 2070 TextSpec text_spec = {.font = &ui->small_font, .rotation = 90.0f, .colour = txt_colour, .flags = TF_ROTATED}; 2071 Color rl_txt_colour = colour_from_normalized(txt_colour); 2072 for (u32 j = 0; j <= segments; j++) { 2073 DrawLineEx(sp.rl, ep.rl, 3, rl_txt_colour); 2074 2075 stream_reset(&buf, 0); 2076 if (draw_plus && value > 0) stream_append_byte(&buf, '+'); 2077 stream_append_f64(&buf, value, 10); 2078 stream_append_s8(&buf, suffix); 2079 draw_text(stream_to_s8(&buf), tp, &text_spec); 2080 2081 value += value_inc; 2082 sp.x += inc; 2083 ep.x += inc; 2084 tp.x += inc; 2085 } 2086 2087 Color rl_marker_colour = colour_from_normalized(marker_colour); 2088 ep.y += RULER_TICK_LENGTH; 2089 for (u32 i = 0; i < marker_count; i++) { 2090 if (markers[i] < F32_INFINITY) { 2091 ep.x = sp.x = markers[i]; 2092 DrawLineEx(sp.rl, ep.rl, 3, rl_marker_colour); 2093 DrawCircleV(ep.rl, 3, rl_marker_colour); 2094 } 2095 } 2096 2097 rlPopMatrix(); 2098 } 2099 2100 function void 2101 do_scale_bar(BeamformerUI *ui, Arena arena, Variable *scale_bar, v2 mouse, Rect draw_rect, 2102 f32 start_value, f32 end_value, s8 suffix) 2103 { 2104 assert(scale_bar->type == VT_SCALE_BAR); 2105 ScaleBar *sb = &scale_bar->scale_bar; 2106 2107 v2 txt_s = measure_text(ui->small_font, s8("-288.8 mm")); 2108 2109 Rect tick_rect = draw_rect; 2110 v2 start_pos = tick_rect.pos; 2111 v2 end_pos = tick_rect.pos; 2112 v2 relative_mouse = v2_sub(mouse, tick_rect.pos); 2113 2114 f32 markers[2]; 2115 u32 marker_count = 1; 2116 2117 v2 world_zoom_point = {{sb->zoom_starting_coord, sb->zoom_starting_coord}}; 2118 v2 screen_zoom_point = world_point_to_screen_2d(world_zoom_point, 2119 (v2){{*sb->min_value, *sb->min_value}}, 2120 (v2){{*sb->max_value, *sb->max_value}}, 2121 (v2){0}, tick_rect.size); 2122 u32 tick_count; 2123 if (sb->direction == SB_AXIAL) { 2124 tick_rect.size.x = RULER_TEXT_PAD + RULER_TICK_LENGTH + txt_s.x; 2125 tick_count = (u32)(tick_rect.size.y / (1.5f * (f32)ui->small_font.baseSize)); 2126 start_pos.y += tick_rect.size.y; 2127 markers[0] = tick_rect.size.y - screen_zoom_point.y; 2128 markers[1] = tick_rect.size.y - relative_mouse.y; 2129 } else { 2130 tick_rect.size.y = RULER_TEXT_PAD + RULER_TICK_LENGTH + txt_s.x; 2131 tick_count = (u32)(tick_rect.size.x / (1.5f * (f32)ui->small_font.baseSize)); 2132 end_pos.x += tick_rect.size.x; 2133 markers[0] = screen_zoom_point.x; 2134 markers[1] = relative_mouse.x; 2135 } 2136 2137 if (hover_interaction(ui, mouse, auto_interaction(tick_rect, scale_bar))) 2138 marker_count = 2; 2139 2140 draw_ruler(ui, arena, start_pos, end_pos, start_value, end_value, markers, marker_count, 2141 tick_count, suffix, RULER_COLOUR, v4_lerp(FG_COLOUR, HOVERED_COLOUR, scale_bar->hover_t)); 2142 } 2143 2144 function v2 2145 draw_radio_button(BeamformerUI *ui, Variable *var, v2 at, v2 mouse, v4 base_colour, f32 size) 2146 { 2147 assert(var->type == VT_B32); 2148 b32 value = var->bool32; 2149 2150 v2 result = (v2){.x = size, .y = size}; 2151 Rect hover_rect = {.pos = at, .size = result}; 2152 hover_rect.pos.y += 1; 2153 hover_interaction(ui, mouse, auto_interaction(hover_rect, var)); 2154 2155 hover_rect = shrink_rect_centered(hover_rect, (v2){{8.0f, 8.0f}}); 2156 Rect inner = shrink_rect_centered(hover_rect, (v2){{4.0f, 4.0f}}); 2157 v4 fill = v4_lerp(value? base_colour : (v4){0}, HOVERED_COLOUR, var->hover_t); 2158 DrawRectangleRoundedLinesEx(hover_rect.rl, 0.2f, 0, 2, colour_from_normalized(base_colour)); 2159 DrawRectangleRec(inner.rl, colour_from_normalized(fill)); 2160 2161 return result; 2162 } 2163 2164 function f32 2165 draw_variable_slider(BeamformerUI *ui, Variable *var, Rect r, f32 fill, v4 fill_colour, v2 mouse) 2166 { 2167 f32 border_thick = 3.0f; 2168 f32 bar_height_frac = 0.8f; 2169 v2 bar_size = {{6.0f, bar_height_frac * r.size.y}}; 2170 2171 Rect inner = shrink_rect_centered(r, (v2){{2.0f * border_thick, // NOTE(rnp): raylib jank 2172 MAX(0, 2.0f * (r.size.y - bar_size.y))}}); 2173 Rect filled = inner; 2174 filled.size.w *= fill; 2175 2176 Rect bar; 2177 bar.pos = v2_add(r.pos, (v2){{fill * (r.size.w - bar_size.w), (1 - bar_height_frac) * 0.5f * r.size.y}}); 2178 bar.size = bar_size; 2179 v4 bar_colour = v4_lerp(FG_COLOUR, FOCUSED_COLOUR, var->hover_t); 2180 2181 hover_interaction(ui, mouse, auto_interaction(inner, var)); 2182 2183 DrawRectangleRec(filled.rl, colour_from_normalized(fill_colour)); 2184 DrawRectangleRoundedLinesEx(inner.rl, 0.2f, 0, border_thick, BLACK); 2185 DrawRectangleRounded(bar.rl, 0.6f, 1, colour_from_normalized(bar_colour)); 2186 2187 return r.size.y; 2188 } 2189 2190 function v2 2191 draw_fancy_button(BeamformerUI *ui, Variable *var, s8 label, Rect r, v4 border_colour, v2 mouse, TextSpec ts) 2192 { 2193 assert((f32)ts.font->baseSize <= r.size.h * 0.8f); 2194 f32 pad = 0.1f * r.size.h; 2195 2196 v2 shadow_off = {{2.5f, 3.0f}}; 2197 f32 border_thick = 3.0f; 2198 v2 border_size = v2_add((v2){{pad + 2.0f * border_thick, pad}}, shadow_off); 2199 2200 Rect border = shrink_rect_centered(r, border_size); 2201 Rect inner = shrink_rect_centered(border, (v2){{pad, pad}}); 2202 2203 ts.limits.size = inner.size; 2204 hover_interaction(ui, mouse, auto_interaction(inner, var)); 2205 2206 border.pos = v2_add(border.pos, shadow_off); 2207 2208 DrawRectangleRoundedLinesEx(border.rl, 0.6f, 0, border_thick, fade(BLACK, 0.8f)); 2209 border.pos = v2_sub(border.pos, shadow_off); 2210 DrawRectangleRounded(border.rl, 0.6f, 1, colour_from_normalized(BG_COLOUR)); 2211 DrawRectangleRoundedLinesEx(border.rl, 0.6f, 0, border_thick, colour_from_normalized(border_colour)); 2212 2213 /* TODO(rnp): teach draw_text() about alignment */ 2214 v2 at = align_text_in_rect(label, inner, *ts.font); 2215 at = v2_add(at, (v2){{3.0f, 3.0f}}); 2216 v4 base_colour = ts.colour; 2217 ts.colour = (v4){{0, 0, 0, 0.8f}}; 2218 draw_text(label, at, &ts); 2219 2220 at = v2_sub(at, (v2){{3.0f, 3.0f}}); 2221 ts.colour = v4_lerp(base_colour, HOVERED_COLOUR, var->hover_t); 2222 draw_text(label, at, &ts); 2223 2224 v2 result = v2_add(r.size, border_size); 2225 return result; 2226 } 2227 2228 function v2 2229 draw_variable(BeamformerUI *ui, Arena arena, Variable *var, v2 at, v2 mouse, v4 base_colour, TextSpec text_spec) 2230 { 2231 v2 result; 2232 if (var->flags & V_RADIO_BUTTON) { 2233 result = draw_radio_button(ui, var, at, mouse, base_colour, (f32)text_spec.font->baseSize); 2234 } else { 2235 Stream buf = arena_stream(arena); 2236 stream_append_variable(&buf, var); 2237 s8 text = arena_stream_commit(&arena, &buf); 2238 result = measure_text(*text_spec.font, text); 2239 2240 if (var->flags & V_INPUT) { 2241 Rect text_rect = {.pos = at, .size = result}; 2242 text_rect = extend_rect_centered(text_rect, (v2){.x = 8}); 2243 if (hover_interaction(ui, mouse, auto_interaction(text_rect, var)) && (var->flags & V_TEXT)) 2244 ui->text_input_state.hot_font = text_spec.font; 2245 text_spec.colour = v4_lerp(base_colour, HOVERED_COLOUR, var->hover_t); 2246 } 2247 2248 draw_text(text, at, &text_spec); 2249 } 2250 return result; 2251 } 2252 2253 function void 2254 draw_table_cell(BeamformerUI *ui, Arena arena, TableCell *cell, Rect cell_rect, 2255 TextAlignment alignment, TextSpec ts, v2 mouse) 2256 { 2257 f32 x_off = cell_rect.pos.x; 2258 v2 cell_at = table_cell_align(cell, alignment, cell_rect); 2259 ts.limits.size.w -= (cell_at.x - x_off); 2260 cell_rect.size.w = MIN(ts.limits.size.w, cell_rect.size.w); 2261 2262 /* TODO(rnp): push truncated text for hovering */ 2263 switch (cell->kind) { 2264 case TableCellKind_None:{ draw_text(cell->text, cell_at, &ts); }break; 2265 case TableCellKind_Variable:{ 2266 if (cell->var->flags & V_INPUT) { 2267 draw_variable(ui, arena, cell->var, cell_at, mouse, ts.colour, ts); 2268 } else if (cell->text.len) { 2269 draw_text(cell->text, cell_at, &ts); 2270 } 2271 }break; 2272 case TableCellKind_VariableGroup:{ 2273 Variable *v = cell->var->group.first; 2274 f32 dw = draw_text(s8("{"), cell_at, &ts).x; 2275 while (v) { 2276 cell_at.x += dw; 2277 ts.limits.size.w -= dw; 2278 dw = draw_variable(ui, arena, v, cell_at, mouse, ts.colour, ts).x; 2279 2280 v = v->next; 2281 if (v) { 2282 cell_at.x += dw; 2283 ts.limits.size.w -= dw; 2284 dw = draw_text(s8(", "), cell_at, &ts).x; 2285 } 2286 } 2287 cell_at.x += dw; 2288 ts.limits.size.w -= dw; 2289 draw_text(s8("}"), cell_at, &ts); 2290 }break; 2291 } 2292 } 2293 2294 function void 2295 draw_table_borders(Table *t, Rect r, f32 line_height) 2296 { 2297 if (t->column_border_thick > 0) { 2298 v2 start = {.x = r.pos.x, .y = r.pos.y + t->cell_pad.h / 2}; 2299 v2 end = start; 2300 end.y += t->size.y - t->cell_pad.y; 2301 for (i32 i = 0; i < t->columns - 1; i++) { 2302 f32 dx = t->widths[i] + t->cell_pad.w + t->column_border_thick; 2303 start.x += dx; 2304 end.x += dx; 2305 if (t->widths[i + 1] > 0) 2306 DrawLineEx(start.rl, end.rl, t->column_border_thick, fade(BLACK, 0.8f)); 2307 } 2308 } 2309 2310 if (t->row_border_thick > 0) { 2311 v2 start = {.x = r.pos.x + t->cell_pad.w / 2, .y = r.pos.y}; 2312 v2 end = start; 2313 end.x += t->size.x - t->cell_pad.x; 2314 for (i32 i = 0; i < t->rows - 1; i++) { 2315 f32 dy = line_height + t->cell_pad.y + t->row_border_thick; 2316 start.y += dy; 2317 end.y += dy; 2318 DrawLineEx(start.rl, end.rl, t->row_border_thick, fade(BLACK, 0.8f)); 2319 } 2320 } 2321 } 2322 2323 function v2 2324 draw_table(BeamformerUI *ui, Arena arena, Table *table, Rect draw_rect, TextSpec ts, v2 mouse, b32 skip_rows) 2325 { 2326 ts.flags |= TF_LIMITED; 2327 2328 v2 result = {.x = table_width(table)}; 2329 i32 row_index = skip_rows? table_skip_rows(table, draw_rect.size.h, (f32)ts.font->baseSize) : 0; 2330 TableIterator *it = table_iterator_new(table, TIK_CELLS, &arena, row_index, draw_rect.pos, ts.font); 2331 for (TableCell *cell = table_iterator_next(it, &arena); 2332 cell; 2333 cell = table_iterator_next(it, &arena)) 2334 { 2335 ts.limits.size.w = draw_rect.size.w - (it->cell_rect.pos.x - it->start_x); 2336 draw_table_cell(ui, arena, cell, it->cell_rect, it->alignment, ts, mouse); 2337 } 2338 draw_table_borders(table, draw_rect, (f32)ts.font->baseSize); 2339 result.y = it->cell_rect.pos.y - draw_rect.pos.y - table->cell_pad.h / 2.0f; 2340 return result; 2341 } 2342 2343 function void 2344 draw_view_ruler(BeamformerFrameView *view, Arena a, Rect view_rect, TextSpec ts) 2345 { 2346 v2 vr_max_p = v2_add(view_rect.pos, view_rect.size); 2347 v2 start_p = world_point_to_screen_2d(view->ruler.start, XZ(view->min_coordinate), 2348 XZ(view->max_coordinate), view_rect.pos, vr_max_p); 2349 v2 end_p = world_point_to_screen_2d(view->ruler.end, XZ(view->min_coordinate), 2350 XZ(view->max_coordinate), view_rect.pos, vr_max_p); 2351 2352 Color rl_colour = colour_from_normalized(ts.colour); 2353 DrawCircleV(start_p.rl, 3, rl_colour); 2354 DrawLineEx(end_p.rl, start_p.rl, 2, rl_colour); 2355 DrawCircleV(end_p.rl, 3, rl_colour); 2356 2357 Stream buf = arena_stream(a); 2358 stream_append_f64(&buf, 1e3 * v2_magnitude(v2_sub(view->ruler.end, view->ruler.start)), 100); 2359 stream_append_s8(&buf, s8(" mm")); 2360 2361 v2 txt_p = start_p; 2362 v2 txt_s = measure_text(*ts.font, stream_to_s8(&buf)); 2363 v2 pixel_delta = v2_sub(start_p, end_p); 2364 if (pixel_delta.y < 0) txt_p.y -= txt_s.y; 2365 if (pixel_delta.x < 0) txt_p.x -= txt_s.x; 2366 if (txt_p.x < view_rect.pos.x) txt_p.x = view_rect.pos.x; 2367 if (txt_p.x + txt_s.x > vr_max_p.x) txt_p.x -= (txt_p.x + txt_s.x) - vr_max_p.x; 2368 2369 draw_text(stream_to_s8(&buf), txt_p, &ts); 2370 } 2371 2372 function v2 2373 draw_frame_view_controls(BeamformerUI *ui, Arena arena, BeamformerFrameView *view, Rect vr, v2 mouse) 2374 { 2375 TextSpec text_spec = {.font = &ui->small_font, .flags = TF_LIMITED|TF_OUTLINED, 2376 .colour = RULER_COLOUR, .outline_thick = 1, .outline_colour.a = 1, 2377 .limits.size.x = vr.size.w}; 2378 2379 Table *table = table_new(&arena, 3, TextAlignment_Left, TextAlignment_Left, TextAlignment_Left); 2380 table_push_parameter_row(table, &arena, view->gamma.name, &view->gamma, s8("")); 2381 table_push_parameter_row(table, &arena, view->threshold.name, &view->threshold, s8("")); 2382 if (view->log_scale->bool32) 2383 table_push_parameter_row(table, &arena, view->dynamic_range.name, &view->dynamic_range, s8("[dB]")); 2384 2385 Rect table_rect = vr; 2386 f32 height = table_extent(table, arena, text_spec.font).y; 2387 height = MIN(height, vr.size.h); 2388 table_rect.pos.w += 8; 2389 table_rect.pos.y += vr.size.h - height - 8; 2390 table_rect.size.h = height; 2391 table_rect.size.w = vr.size.w - 16; 2392 2393 return draw_table(ui, arena, table, table_rect, text_spec, mouse, 0); 2394 } 2395 2396 function void 2397 draw_3D_xplane_frame_view(BeamformerUI *ui, Arena arena, Variable *var, Rect display_rect, v2 mouse) 2398 { 2399 assert(var->type == VT_BEAMFORMER_FRAME_VIEW); 2400 BeamformerFrameView *view = var->generic; 2401 2402 f32 aspect = (f32)view->texture_dim.w / (f32)view->texture_dim.h; 2403 Rect vr = display_rect; 2404 if (aspect > 1.0f) vr.size.w = vr.size.h; 2405 else vr.size.h = vr.size.w; 2406 2407 if (vr.size.w > display_rect.size.w) { 2408 vr.size.w -= (vr.size.w - display_rect.size.w); 2409 vr.size.h = vr.size.w / aspect; 2410 } else if (vr.size.h > display_rect.size.h) { 2411 vr.size.h -= (vr.size.h - display_rect.size.h); 2412 vr.size.w = vr.size.h * aspect; 2413 } 2414 vr.pos = v2_add(vr.pos, v2_scale(v2_sub(display_rect.size, vr.size), 0.5)); 2415 2416 i32 id = -1; 2417 if (hover_interaction(ui, mouse, auto_interaction(vr, var))) { 2418 ray mouse_ray = ray_for_x_plane_view(ui, view, normalized_p_in_rect(vr, mouse, 0)); 2419 v3 x_size = v3_scale(beamformer_frame_view_plane_size(ui, view), 0.5f); 2420 2421 f32 rotation = x_plane_rotation_for_view_plane(view, BeamformerViewPlaneTag_XZ); 2422 m4 x_rotation = m4_rotation_about_y(rotation); 2423 v3 x_position = offset_x_plane_position(ui, view, BeamformerViewPlaneTag_XZ); 2424 2425 f32 test[2] = {0}; 2426 test[0] = obb_raycast(x_rotation, x_size, x_position, mouse_ray); 2427 2428 x_position = offset_x_plane_position(ui, view, BeamformerViewPlaneTag_YZ); 2429 rotation = x_plane_rotation_for_view_plane(view, BeamformerViewPlaneTag_YZ); 2430 x_rotation = m4_rotation_about_y(rotation); 2431 test[1] = obb_raycast(x_rotation, x_size, x_position, mouse_ray); 2432 2433 if (test[0] >= 0 && test[1] >= 0) id = test[1] < test[0]? 1 : 0; 2434 else if (test[0] >= 0) id = 0; 2435 else if (test[1] >= 0) id = 1; 2436 2437 if (id != -1) { 2438 view->hit_test_point = v3_add(mouse_ray.origin, v3_scale(mouse_ray.direction, test[id])); 2439 } 2440 } 2441 2442 for (i32 i = 0; i < countof(view->x_plane_shifts); i++) { 2443 Variable *it = view->x_plane_shifts + i; 2444 Interaction interaction = auto_interaction(vr, it); 2445 if (id == i) ui->next_interaction = interaction; 2446 if (interaction_is_hot(ui, interaction)) it->hover_t += HOVER_SPEED * dt_for_frame; 2447 else it->hover_t -= HOVER_SPEED * dt_for_frame; 2448 it->hover_t = CLAMP01(it->hover_t); 2449 } 2450 2451 Rectangle tex_r = {0, 0, (f32)view->texture_dim.w, (f32)view->texture_dim.h}; 2452 NPatchInfo tex_np = {tex_r, 0, 0, 0, 0, NPATCH_NINE_PATCH}; 2453 DrawTextureNPatch(make_raylib_texture(view), tex_np, vr.rl, (Vector2){0}, 0, WHITE); 2454 2455 draw_frame_view_controls(ui, arena, view, vr, mouse); 2456 } 2457 2458 function void 2459 draw_beamformer_frame_view(BeamformerUI *ui, Arena a, Variable *var, Rect display_rect, v2 mouse) 2460 { 2461 assert(var->type == VT_BEAMFORMER_FRAME_VIEW); 2462 BeamformerFrameView *view = var->generic; 2463 BeamformerFrame *frame = view->frame; 2464 2465 f32 txt_w = measure_text(ui->small_font, s8("-288.8 mm")).w; 2466 f32 scale_bar_size = 1.2f * txt_w + RULER_TICK_LENGTH; 2467 2468 v3 min = view->min_coordinate; 2469 v3 max = view->max_coordinate; 2470 v2 requested_dim = v2_sub(XZ(max), XZ(min)); 2471 f32 aspect = requested_dim.w / requested_dim.h; 2472 2473 Rect vr = display_rect; 2474 v2 scale_bar_area = {0}; 2475 if (view->axial_scale_bar_active->bool32) { 2476 vr.pos.y += 0.5f * (f32)ui->small_font.baseSize; 2477 scale_bar_area.x += scale_bar_size; 2478 scale_bar_area.y += (f32)ui->small_font.baseSize; 2479 } 2480 2481 if (view->lateral_scale_bar_active->bool32) { 2482 vr.pos.x += 0.5f * (f32)ui->small_font.baseSize; 2483 scale_bar_area.x += (f32)ui->small_font.baseSize; 2484 scale_bar_area.y += scale_bar_size; 2485 } 2486 2487 vr.size = v2_sub(vr.size, scale_bar_area); 2488 if (aspect > 1) vr.size.h = vr.size.w / aspect; 2489 else vr.size.w = vr.size.h * aspect; 2490 2491 v2 occupied = v2_add(vr.size, scale_bar_area); 2492 if (occupied.w > display_rect.size.w) { 2493 vr.size.w -= (occupied.w - display_rect.size.w); 2494 vr.size.h = vr.size.w / aspect; 2495 } else if (occupied.h > display_rect.size.h) { 2496 vr.size.h -= (occupied.h - display_rect.size.h); 2497 vr.size.w = vr.size.h * aspect; 2498 } 2499 occupied = v2_add(vr.size, scale_bar_area); 2500 vr.pos = v2_add(vr.pos, v2_scale(v2_sub(display_rect.size, occupied), 0.5)); 2501 2502 /* TODO(rnp): make this depend on the requested draw orientation (x-z or y-z or x-y) */ 2503 v2 output_dim = v2_sub(XZ(frame->max_coordinate), XZ(frame->min_coordinate)); 2504 v2 pixels_per_meter = { 2505 .w = (f32)view->texture_dim.w / output_dim.w, 2506 .h = (f32)view->texture_dim.h / output_dim.h, 2507 }; 2508 2509 /* NOTE(rnp): math to resize the texture without stretching when the view changes 2510 * but the texture hasn't been (or cannot be) rebeamformed */ 2511 v2 texture_points = v2_mul(pixels_per_meter, requested_dim); 2512 /* TODO(rnp): this also depends on x-y, y-z, x-z */ 2513 v2 texture_start = { 2514 .x = pixels_per_meter.x * 0.5f * (output_dim.x - requested_dim.x), 2515 .y = pixels_per_meter.y * (frame->max_coordinate.z - max.z), 2516 }; 2517 2518 Rectangle tex_r = {texture_start.x, texture_start.y, texture_points.x, texture_points.y}; 2519 NPatchInfo tex_np = { tex_r, 0, 0, 0, 0, NPATCH_NINE_PATCH }; 2520 DrawTextureNPatch(make_raylib_texture(view), tex_np, vr.rl, (Vector2){0}, 0, WHITE); 2521 2522 v2 start_pos = vr.pos; 2523 start_pos.y += vr.size.y; 2524 2525 if (vr.size.w > 0 && view->lateral_scale_bar_active->bool32) { 2526 do_scale_bar(ui, a, &view->lateral_scale_bar, mouse, 2527 (Rect){.pos = start_pos, .size = vr.size}, 2528 *view->lateral_scale_bar.scale_bar.min_value * 1e3f, 2529 *view->lateral_scale_bar.scale_bar.max_value * 1e3f, s8(" mm")); 2530 } 2531 2532 start_pos = vr.pos; 2533 start_pos.x += vr.size.x; 2534 2535 if (vr.size.h > 0 && view->axial_scale_bar_active->bool32) { 2536 do_scale_bar(ui, a, &view->axial_scale_bar, mouse, 2537 (Rect){.pos = start_pos, .size = vr.size}, 2538 *view->axial_scale_bar.scale_bar.max_value * 1e3f, 2539 *view->axial_scale_bar.scale_bar.min_value * 1e3f, s8(" mm")); 2540 } 2541 2542 TextSpec text_spec = {.font = &ui->small_font, .flags = TF_LIMITED|TF_OUTLINED, 2543 .colour = RULER_COLOUR, .outline_thick = 1, .outline_colour.a = 1, 2544 .limits.size.x = vr.size.w}; 2545 2546 f32 draw_table_width = vr.size.w; 2547 /* NOTE: avoid hover_t modification */ 2548 Interaction viewer = auto_interaction(vr, var); 2549 if (point_in_rect(mouse, viewer.rect)) { 2550 ui->next_interaction = viewer; 2551 2552 v2 world = screen_point_to_world_2d(mouse, vr.pos, v2_add(vr.pos, vr.size), 2553 XZ(view->min_coordinate), 2554 XZ(view->max_coordinate)); 2555 Stream buf = arena_stream(a); 2556 stream_append_v2(&buf, v2_scale(world, 1e3f)); 2557 2558 text_spec.limits.size.w -= 4.0f; 2559 v2 txt_s = measure_text(*text_spec.font, stream_to_s8(&buf)); 2560 v2 txt_p = { 2561 .x = vr.pos.x + vr.size.w - txt_s.w - 4.0f, 2562 .y = vr.pos.y + vr.size.h - txt_s.h - 4.0f, 2563 }; 2564 txt_p.x = MAX(vr.pos.x, txt_p.x); 2565 draw_table_width -= draw_text(stream_to_s8(&buf), txt_p, &text_spec).w; 2566 text_spec.limits.size.w += 4.0f; 2567 } 2568 2569 { 2570 Stream buf = arena_stream(a); 2571 s8 shader = push_acquisition_kind(&buf, frame->acquisition_kind, frame->compound_count); 2572 text_spec.font = &ui->font; 2573 text_spec.limits.size.w -= 16; 2574 v2 txt_s = measure_text(*text_spec.font, shader); 2575 v2 txt_p = { 2576 .x = vr.pos.x + vr.size.w - txt_s.w - 16, 2577 .y = vr.pos.y + 4, 2578 }; 2579 txt_p.x = MAX(vr.pos.x, txt_p.x); 2580 draw_text(stream_to_s8(&buf), txt_p, &text_spec); 2581 text_spec.font = &ui->small_font; 2582 text_spec.limits.size.w += 16; 2583 } 2584 2585 if (view->ruler.state != RulerState_None) draw_view_ruler(view, a, vr, text_spec); 2586 2587 vr.size.w = draw_table_width; 2588 draw_frame_view_controls(ui, a, view, vr, mouse); 2589 } 2590 2591 function v2 2592 draw_compute_progress_bar(BeamformerUI *ui, ComputeProgressBar *state, Rect r) 2593 { 2594 if (*state->processing) state->display_t_velocity += 65.0f * dt_for_frame; 2595 else state->display_t_velocity -= 45.0f * dt_for_frame; 2596 2597 state->display_t_velocity = CLAMP(state->display_t_velocity, -10.0f, 10.0f); 2598 state->display_t += state->display_t_velocity * dt_for_frame; 2599 state->display_t = CLAMP01(state->display_t); 2600 2601 if (state->display_t > (1.0f / 255.0f)) { 2602 Rect outline = {.pos = r.pos, .size = {{r.size.w, (f32)ui->font.baseSize}}}; 2603 outline = scale_rect_centered(outline, (v2){{0.96f, 0.7f}}); 2604 Rect filled = outline; 2605 filled.size.w *= *state->progress; 2606 DrawRectangleRounded(filled.rl, 2.0f, 0, fade(colour_from_normalized(HOVERED_COLOUR), 2607 state->display_t)); 2608 DrawRectangleRoundedLinesEx(outline.rl, 2.0f, 0, 3, fade(BLACK, state->display_t)); 2609 } 2610 2611 v2 result = {{r.size.w, (f32)ui->font.baseSize}}; 2612 return result; 2613 } 2614 2615 function s8 2616 push_compute_time(Arena *arena, s8 prefix, f32 time) 2617 { 2618 Stream sb = arena_stream(*arena); 2619 stream_append_s8(&sb, prefix); 2620 stream_append_f64_e(&sb, time); 2621 return arena_stream_commit(arena, &sb); 2622 } 2623 2624 function v2 2625 draw_compute_stats_bar_view(BeamformerUI *ui, Arena arena, ComputeShaderStats *stats, 2626 BeamformerShaderKind *stages, u32 stages_count, f32 compute_time_sum, 2627 TextSpec ts, Rect r, v2 mouse) 2628 { 2629 read_only local_persist s8 frame_labels[] = {s8_comp("0:"), s8_comp("-1:"), s8_comp("-2:"), s8_comp("-3:")}; 2630 f32 total_times[countof(frame_labels)] = {0}; 2631 Table *table = table_new(&arena, countof(frame_labels), TextAlignment_Right, TextAlignment_Left); 2632 for (u32 i = 0; i < countof(frame_labels); i++) { 2633 TableCell *cells = table_push_row(table, &arena, TRK_CELLS)->data; 2634 cells[0].text = frame_labels[i]; 2635 u32 frame_index = (stats->latest_frame_index - i) % countof(stats->table.times); 2636 u32 seen_shaders = 0; 2637 for (u32 j = 0; j < stages_count; j++) { 2638 if ((seen_shaders & (1u << stages[j])) == 0) 2639 total_times[i] += stats->table.times[frame_index][stages[j]]; 2640 seen_shaders |= (1u << stages[j]); 2641 } 2642 } 2643 2644 v2 result = table_extent(table, arena, ts.font); 2645 2646 f32 remaining_width = r.size.w - result.w - table->cell_pad.w; 2647 f32 average_width = 0.8f * remaining_width; 2648 2649 s8 mouse_text = s8(""); 2650 v2 text_pos; 2651 2652 u32 row_index = 0; 2653 TableIterator *it = table_iterator_new(table, TIK_ROWS, &arena, 0, r.pos, ts.font); 2654 for (TableRow *row = table_iterator_next(it, &arena); 2655 row; 2656 row = table_iterator_next(it, &arena)) 2657 { 2658 Rect cr = it->cell_rect; 2659 cr.size.w = table->widths[0]; 2660 ts.limits.size.w = cr.size.w; 2661 draw_table_cell(ui, arena, (TableCell *)row->data, cr, table->alignment[0], ts, mouse); 2662 2663 u32 frame_index = (stats->latest_frame_index - row_index) % countof(stats->table.times); 2664 f32 total_width = average_width * total_times[row_index] / compute_time_sum; 2665 Rect rect; 2666 rect.pos = v2_add(cr.pos, (v2){{cr.size.w + table->cell_pad.w , cr.size.h * 0.15f}}); 2667 rect.size = (v2){.y = 0.7f * cr.size.h}; 2668 for (u32 i = 0; i < stages_count; i++) { 2669 rect.size.w = total_width * stats->table.times[frame_index][stages[i]] / total_times[row_index]; 2670 Color color = colour_from_normalized(g_colour_palette[i % countof(g_colour_palette)]); 2671 DrawRectangleRec(rect.rl, color); 2672 if (point_in_rect(mouse, rect)) { 2673 text_pos = v2_add(rect.pos, (v2){.x = table->cell_pad.w}); 2674 s8 name = push_s8_from_parts(&arena, s8(""), beamformer_shader_names[stages[i]], s8(": ")); 2675 mouse_text = push_compute_time(&arena, name, stats->table.times[frame_index][stages[i]]); 2676 } 2677 rect.pos.x += rect.size.w; 2678 } 2679 row_index++; 2680 } 2681 2682 v2 start = v2_add(r.pos, (v2){.x = table->widths[0] + average_width + table->cell_pad.w}); 2683 v2 end = v2_add(start, (v2){.y = result.y}); 2684 DrawLineEx(start.rl, end.rl, 4, colour_from_normalized(FG_COLOUR)); 2685 2686 if (mouse_text.len) { 2687 ts.font = &ui->small_font; 2688 ts.flags &= ~(u32)TF_LIMITED; 2689 ts.flags |= (u32)TF_OUTLINED; 2690 ts.outline_colour = (v4){.a = 1}; 2691 ts.outline_thick = 1; 2692 draw_text(mouse_text, text_pos, &ts); 2693 } 2694 2695 return result; 2696 } 2697 2698 function void 2699 push_table_time_row(Table *table, Arena *arena, s8 label, f32 time) 2700 { 2701 assert(table->columns == 3); 2702 TableCell *cells = table_push_row(table, arena, TRK_CELLS)->data; 2703 cells[0].text = push_s8_from_parts(arena, s8(""), label, s8(":")); 2704 cells[1].text = push_compute_time(arena, s8(""), time); 2705 cells[2].text = s8("[s]"); 2706 } 2707 2708 function void 2709 push_table_time_row_with_fps(Table *table, Arena *arena, s8 label, f32 time) 2710 { 2711 assert(table->columns == 3); 2712 TableCell *cells = table_push_row(table, arena, TRK_CELLS)->data; 2713 2714 Stream sb = arena_stream(*arena); 2715 stream_append_f64_e(&sb, time); 2716 stream_append_s8(&sb, s8(" (")); 2717 stream_append_f64(&sb, time > 0 ? 1.0f / time : 0, 100); 2718 stream_append_s8(&sb, s8(")")); 2719 2720 cells[0].text = label; 2721 cells[1].text = arena_stream_commit(arena, &sb); 2722 cells[2].text = s8("[s] (FPS)"); 2723 } 2724 2725 function void 2726 push_table_memory_size_row(Table *table, Arena *arena, s8 label, u64 memory_size) 2727 { 2728 TableCell *cells = table_push_row(table, arena, TRK_CELLS)->data; 2729 Stream sb = arena_stream(*arena); 2730 stream_append_u64(&sb, memory_size); 2731 cells[0].text = label; 2732 cells[1].text = arena_stream_commit(arena, &sb); 2733 cells[2].text = s8("[B/F]"); 2734 } 2735 2736 function v2 2737 draw_compute_stats_view(BeamformerUI *ui, Arena arena, Variable *view, Rect r, v2 mouse) 2738 { 2739 assert(view->type == VT_COMPUTE_STATS_VIEW); 2740 2741 read_only local_persist BeamformerComputePlan dummy_plan = {0}; 2742 u32 selected_plan = ui->selected_parameter_block % BeamformerMaxParameterBlockSlots; 2743 BeamformerComputePlan *cp = ui->beamformer_context->compute_context.compute_plans[selected_plan]; 2744 if (!cp) cp = &dummy_plan; 2745 2746 ComputeStatsView *csv = &view->compute_stats_view; 2747 ComputeShaderStats *stats = csv->compute_shader_stats; 2748 f32 compute_time_sum = 0; 2749 u32 stages = cp->pipeline.shader_count; 2750 TextSpec text_spec = {.font = &ui->font, .colour = FG_COLOUR, .flags = TF_LIMITED}; 2751 2752 ui_blinker_update(&csv->blink, BLINK_SPEED); 2753 2754 static_assert(BeamformerShaderKind_ComputeCount <= 32, "shader kind bitfield test"); 2755 u32 seen_shaders = 0; 2756 for (u32 i = 0; i < stages; i++) { 2757 BeamformerShaderKind index = cp->pipeline.shaders[i]; 2758 if ((seen_shaders & (1u << index)) == 0) 2759 compute_time_sum += stats->average_times[index]; 2760 seen_shaders |= (1u << index); 2761 } 2762 2763 v2 result = {0}; 2764 2765 Table *table = table_new(&arena, 2, TextAlignment_Left, TextAlignment_Left, TextAlignment_Left); 2766 switch (csv->kind) { 2767 case ComputeStatsViewKind_Average:{ 2768 da_reserve(&arena, table, stages); 2769 for (u32 i = 0; i < stages; i++) { 2770 push_table_time_row(table, &arena, beamformer_shader_names[cp->pipeline.shaders[i]], 2771 stats->average_times[cp->pipeline.shaders[i]]); 2772 } 2773 }break; 2774 case ComputeStatsViewKind_Bar:{ 2775 result = draw_compute_stats_bar_view(ui, arena, stats, cp->pipeline.shaders, stages, 2776 compute_time_sum, text_spec, r, mouse); 2777 r.pos = v2_add(r.pos, (v2){.y = result.y}); 2778 }break; 2779 InvalidDefaultCase; 2780 } 2781 2782 u32 rf_size = ui->beamformer_context->compute_context.rf_buffer.active_rf_size; 2783 push_table_time_row_with_fps(table, &arena, s8("Compute Total:"), compute_time_sum); 2784 push_table_time_row_with_fps(table, &arena, s8("RF Upload Delta:"), stats->rf_time_delta_average); 2785 push_table_memory_size_row(table, &arena, s8("Input RF Size:"), rf_size); 2786 if (rf_size != cp->rf_size) 2787 push_table_memory_size_row(table, &arena, s8("DAS RF Size:"), cp->rf_size); 2788 2789 result = v2_add(result, table_extent(table, arena, text_spec.font)); 2790 2791 u32 row_index = 0; 2792 TableIterator *it = table_iterator_new(table, TIK_ROWS, &arena, 0, r.pos, text_spec.font); 2793 for (TableRow *row = table_iterator_next(it, &arena); 2794 row; 2795 row = table_iterator_next(it, &arena), row_index++) 2796 { 2797 Table *t = it->frame.table; 2798 Rect cell_rect = it->cell_rect; 2799 for (i32 column = 0; column < t->columns; column++) { 2800 TableCell *cell = (TableCell *)it->row->data + column; 2801 cell_rect.size.w = t->widths[column]; 2802 text_spec.limits.size.w = r.size.w - (cell_rect.pos.x - it->start_x); 2803 2804 if (column == 0 && row_index < stages && cp->programs[row_index] == 0 && 2805 cp->pipeline.shaders[row_index] != BeamformerShaderKind_CudaHilbert && 2806 cp->pipeline.shaders[row_index] != BeamformerShaderKind_CudaDecode) 2807 { 2808 text_spec.colour = v4_lerp(FG_COLOUR, FOCUSED_COLOUR, ease_in_out_quartic(csv->blink.t)); 2809 } else { 2810 text_spec.colour = FG_COLOUR; 2811 } 2812 2813 draw_table_cell(ui, arena, cell, cell_rect, t->alignment[column], text_spec, mouse); 2814 2815 cell_rect.pos.x += cell_rect.size.w + t->cell_pad.w; 2816 } 2817 } 2818 2819 return result; 2820 } 2821 2822 function v2 2823 draw_live_controls_view(BeamformerUI *ui, Variable *var, Rect r, v2 mouse, Arena arena) 2824 { 2825 BeamformerSharedMemory *sm = ui->shared_memory.region; 2826 BeamformerLiveImagingParameters *lip = &sm->live_imaging_parameters; 2827 BeamformerLiveControlsView *lv = var->generic; 2828 2829 TextSpec text_spec = {.font = &ui->font, .colour = FG_COLOUR, .flags = TF_LIMITED}; 2830 2831 v2 slider_size = {{MIN(140.0f, r.size.w), (f32)ui->font.baseSize}}; 2832 v2 button_size = {{MIN(r.size.w, slider_size.x + (f32)ui->font.baseSize), (f32)ui->font.baseSize * 1.5f}}; 2833 2834 f32 text_off = r.pos.x + 0.5f * MAX(0, (r.size.w - slider_size.w - (f32)ui->font.baseSize)); 2835 f32 slider_off = r.pos.x + 0.5f * (r.size.w - slider_size.w); 2836 f32 button_off = r.pos.x + 0.5f * (r.size.w - button_size.w); 2837 2838 text_spec.limits.size.w = r.size.w - (text_off - r.pos.x); 2839 2840 v2 at = {{text_off, r.pos.y}}; 2841 2842 v4 hsv_power_slider = {{0.35f * ease_in_out_cubic(1.0f - lip->transmit_power), 0.65f, 0.65f, 1}}; 2843 at.y += draw_text(s8("Power:"), at, &text_spec).y; 2844 at.x = slider_off; 2845 at.y += draw_variable_slider(ui, &lv->transmit_power, (Rect){.pos = at, .size = slider_size}, 2846 lip->transmit_power, hsv_to_rgb(hsv_power_slider), mouse); 2847 2848 at.x = text_off; 2849 at.y += draw_text(s8("TGC:"), at, &text_spec).y; 2850 at.x = slider_off; 2851 for (u32 i = 0; i < countof(lip->tgc_control_points); i++) { 2852 Variable *v = lv->tgc_control_points + i; 2853 at.y += draw_variable_slider(ui, v, (Rect){.pos = at, .size = slider_size}, 2854 lip->tgc_control_points[i], g_colour_palette[1], mouse); 2855 2856 if (interaction_is_hot(ui, auto_interaction(r, v))) 2857 lv->hot_field_flag = BeamformerLiveImagingDirtyFlags_TGCControlPoints; 2858 } 2859 2860 at.x = button_off; 2861 at.y += (f32)ui->font.baseSize * 0.5f; 2862 at.y += draw_fancy_button(ui, &lv->stop_button, lv->stop_button.name, 2863 (Rect){.pos = at, .size = button_size}, 2864 BORDER_COLOUR, mouse, text_spec).y; 2865 2866 if (lip->save_enabled) { 2867 b32 active = lip->save_active; 2868 s8 label = lv->save_button.cycler.labels[active % lv->save_button.cycler.cycle_length]; 2869 2870 f32 save_t = ui_blinker_update(&lv->save_button_blink, BLINK_SPEED); 2871 v4 border_colour = BORDER_COLOUR; 2872 if (active) border_colour = v4_lerp(BORDER_COLOUR, FOCUSED_COLOUR, ease_in_out_cubic(save_t)); 2873 2874 at.x = text_off; 2875 at.y += draw_text(s8("File Tag:"), at, &text_spec).y; 2876 at.x += (f32)text_spec.font->baseSize / 2; 2877 text_spec.limits.size.w -= (f32)text_spec.font->baseSize; 2878 2879 v4 save_text_colour = FG_COLOUR; 2880 if (lip->save_name_tag_length <= 0) 2881 save_text_colour.a = 0.6f; 2882 at.y += draw_variable(ui, arena, &lv->save_text, at, mouse, save_text_colour, text_spec).y; 2883 text_spec.limits.size.w += (f32)text_spec.font->baseSize; 2884 2885 at.x = button_off; 2886 at.y += (f32)ui->font.baseSize * 0.25f; 2887 at.y += draw_fancy_button(ui, &lv->save_button, label, (Rect){.pos = at, .size = button_size}, 2888 border_colour, mouse, text_spec).y; 2889 2890 if (interaction_is_hot(ui, auto_interaction(r, &lv->save_text))) 2891 lv->hot_field_flag = BeamformerLiveImagingDirtyFlags_SaveNameTag; 2892 if (interaction_is_hot(ui, auto_interaction(r, &lv->save_button))) 2893 lv->hot_field_flag = BeamformerLiveImagingDirtyFlags_SaveData; 2894 } 2895 2896 if (interaction_is_hot(ui, auto_interaction(r, &lv->transmit_power))) 2897 lv->hot_field_flag = BeamformerLiveImagingDirtyFlags_TransmitPower; 2898 if (interaction_is_hot(ui, auto_interaction(r, &lv->stop_button))) 2899 lv->hot_field_flag = BeamformerLiveImagingDirtyFlags_StopImaging; 2900 2901 v2 result = {{r.size.w, at.y - r.pos.y}}; 2902 return result; 2903 } 2904 2905 struct variable_iterator { Variable *current; }; 2906 function i32 2907 variable_iterator_next(struct variable_iterator *it) 2908 { 2909 i32 result = 0; 2910 2911 if (it->current->type == VT_GROUP && it->current->group.expanded) { 2912 it->current = it->current->group.first; 2913 result++; 2914 } else { 2915 while (it->current) { 2916 if (it->current->next) { 2917 it->current = it->current->next; 2918 break; 2919 } 2920 it->current = it->current->parent; 2921 result--; 2922 } 2923 } 2924 2925 return result; 2926 } 2927 2928 function v2 2929 draw_ui_view_menu(BeamformerUI *ui, Variable *group, Arena arena, Rect r, v2 mouse, TextSpec text_spec) 2930 { 2931 assert(group->type == VT_GROUP); 2932 Table *table = table_new(&arena, 0, TextAlignment_Left, TextAlignment_Right); 2933 table->row_border_thick = 2.0f; 2934 table->cell_pad = (v2){{16.0f, 8.0f}}; 2935 2936 i32 nesting = 0; 2937 for (struct variable_iterator it = {group->group.first}; 2938 it.current; 2939 nesting = variable_iterator_next(&it)) 2940 { 2941 (void)nesting; 2942 assert(nesting == 0); 2943 Variable *var = it.current; 2944 TableCell *cells = table_push_row(table, &arena, TRK_CELLS)->data; 2945 switch (var->type) { 2946 case VT_B32: 2947 case VT_CYCLER: 2948 { 2949 cells[0] = (TableCell){.text = var->name}; 2950 cells[1] = table_variable_cell(&arena, var); 2951 }break; 2952 case VT_UI_BUTTON:{ 2953 cells[0] = (TableCell){.text = var->name, .kind = TableCellKind_Variable, .var = var}; 2954 }break; 2955 InvalidDefaultCase; 2956 } 2957 } 2958 2959 r.size = table_extent(table, arena, text_spec.font); 2960 return draw_table(ui, arena, table, r, text_spec, mouse, 0); 2961 } 2962 2963 function v2 2964 draw_ui_view_listing(BeamformerUI *ui, Variable *group, Arena arena, Rect r, v2 mouse, TextSpec text_spec) 2965 { 2966 assert(group->type == VT_GROUP); 2967 Table *table = table_new(&arena, 0, TextAlignment_Left, TextAlignment_Left, TextAlignment_Right); 2968 2969 i32 nesting = 0; 2970 for (struct variable_iterator it = {group->group.first}; 2971 it.current; 2972 nesting = variable_iterator_next(&it)) 2973 { 2974 while (nesting > 0) { 2975 table = table_begin_subtable(table, &arena, TextAlignment_Left, 2976 TextAlignment_Center, TextAlignment_Right); 2977 nesting--; 2978 } 2979 while (nesting < 0) { table = table_end_subtable(table); nesting++; } 2980 2981 Variable *var = it.current; 2982 switch (var->type) { 2983 case VT_CYCLER: 2984 case VT_BEAMFORMER_VARIABLE: 2985 { 2986 s8 suffix = s8(""); 2987 if (var->type == VT_BEAMFORMER_VARIABLE) 2988 suffix = var->beamformer_variable.suffix; 2989 table_push_parameter_row(table, &arena, var->name, var, suffix); 2990 }break; 2991 case VT_GROUP:{ 2992 VariableGroup *g = &var->group; 2993 2994 TableCell *cells = table_push_row(table, &arena, TRK_CELLS)->data; 2995 cells[0] = (TableCell){.text = var->name, .kind = TableCellKind_Variable, .var = var}; 2996 2997 if (!g->expanded) { 2998 Stream sb = arena_stream(arena); 2999 stream_append_variable_group(&sb, var); 3000 cells[1].kind = TableCellKind_VariableGroup; 3001 cells[1].text = arena_stream_commit(&arena, &sb); 3002 cells[1].var = var; 3003 3004 Variable *v = g->first; 3005 assert(!v || v->type == VT_BEAMFORMER_VARIABLE); 3006 /* NOTE(rnp): assume the suffix is the same for all elements */ 3007 if (v) cells[2].text = v->beamformer_variable.suffix; 3008 } 3009 }break; 3010 InvalidDefaultCase; 3011 } 3012 } 3013 3014 v2 result = table_extent(table, arena, text_spec.font); 3015 draw_table(ui, arena, table, r, text_spec, mouse, 0); 3016 return result; 3017 } 3018 3019 function Rect 3020 draw_ui_view_container(BeamformerUI *ui, Variable *var, v2 mouse, Rect bounds) 3021 { 3022 UIView *fw = &var->view; 3023 Rect result = fw->rect; 3024 if (fw->rect.size.x > 0 && fw->rect.size.y > 0) { 3025 f32 line_height = (f32)ui->small_font.baseSize; 3026 3027 f32 pad = MAX(line_height + 5.0f, UI_REGION_PAD); 3028 if (fw->rect.pos.y < pad) 3029 fw->rect.pos.y += pad - fw->rect.pos.y; 3030 result = fw->rect; 3031 3032 f32 delta_x = (result.pos.x + result.size.x) - (bounds.size.x + bounds.pos.x); 3033 if (delta_x > 0) { 3034 result.pos.x -= delta_x; 3035 result.pos.x = MAX(0, result.pos.x); 3036 } 3037 3038 Rect container = result; 3039 if (fw->close) { 3040 container.pos.y -= 5 + line_height; 3041 container.size.y += 2 + line_height; 3042 Rect handle = {{container.pos, (v2){.x = container.size.w, .y = 2 + line_height}}}; 3043 Rect close; 3044 hover_interaction(ui, mouse, auto_interaction(container, var)); 3045 cut_rect_horizontal(handle, handle.size.w - handle.size.h - 6, 0, &close); 3046 close.size.w = close.size.h; 3047 DrawRectangleRounded(handle.rl, 0.1f, 0, colour_from_normalized(BG_COLOUR)); 3048 DrawRectangleRoundedLinesEx(handle.rl, 0.2f, 0, 2, BLACK); 3049 draw_close_button(ui, fw->close, mouse, close, (v2){{0.45f, 0.45f}}); 3050 } else { 3051 hover_interaction(ui, mouse, auto_interaction(container, var)); 3052 } 3053 f32 roundness = 12.0f / fw->rect.size.y; 3054 DrawRectangleRounded(result.rl, roundness / 2.0f, 0, colour_from_normalized(BG_COLOUR)); 3055 DrawRectangleRoundedLinesEx(result.rl, roundness, 0, 2, BLACK); 3056 } 3057 return result; 3058 } 3059 3060 function void 3061 draw_ui_view(BeamformerUI *ui, Variable *ui_view, Rect r, v2 mouse, TextSpec text_spec) 3062 { 3063 assert(ui_view->type == VT_UI_VIEW || ui_view->type == VT_UI_MENU || ui_view->type == VT_UI_TEXT_BOX); 3064 3065 UIView *view = &ui_view->view; 3066 3067 if (view->flags & UIViewFlag_Floating) { 3068 r = draw_ui_view_container(ui, ui_view, mouse, r); 3069 } else { 3070 if (view->rect.size.h - r.size.h < view->rect.pos.h) 3071 view->rect.pos.h = view->rect.size.h - r.size.h; 3072 3073 if (view->rect.size.h - r.size.h < 0) 3074 view->rect.pos.h = 0; 3075 3076 r.pos.y -= view->rect.pos.h; 3077 } 3078 3079 v2 size = {0}; 3080 3081 Variable *var = view->child; 3082 switch (var->type) { 3083 case VT_GROUP:{ 3084 if (ui_view->type == VT_UI_MENU) 3085 size = draw_ui_view_menu(ui, var, ui->arena, r, mouse, text_spec); 3086 else { 3087 size = draw_ui_view_listing(ui, var, ui->arena, r, mouse, text_spec); 3088 } 3089 }break; 3090 case VT_BEAMFORMER_FRAME_VIEW: { 3091 BeamformerFrameView *bv = var->generic; 3092 if (frame_view_ready_to_present(ui, bv)) { 3093 if (bv->kind == BeamformerFrameViewKind_3DXPlane) 3094 draw_3D_xplane_frame_view(ui, ui->arena, var, r, mouse); 3095 else 3096 draw_beamformer_frame_view(ui, ui->arena, var, r, mouse); 3097 } 3098 } break; 3099 case VT_COMPUTE_PROGRESS_BAR: { 3100 size = draw_compute_progress_bar(ui, &var->compute_progress_bar, r); 3101 } break; 3102 case VT_COMPUTE_STATS_VIEW:{ size = draw_compute_stats_view(ui, ui->arena, var, r, mouse); }break; 3103 case VT_LIVE_CONTROLS_VIEW:{ 3104 if (view->rect.size.h - r.size.h < 0) 3105 r.pos.y += 0.5f * (r.size.h - view->rect.size.h); 3106 BeamformerSharedMemory *sm = ui->shared_memory.region; 3107 if (sm->live_imaging_parameters.active) 3108 size = draw_live_controls_view(ui, var, r, mouse, ui->arena); 3109 }break; 3110 InvalidDefaultCase; 3111 } 3112 3113 view->rect.size = size; 3114 } 3115 3116 function void 3117 draw_layout_variable(BeamformerUI *ui, Variable *var, Rect draw_rect, v2 mouse) 3118 { 3119 if (var->type != VT_UI_REGION_SPLIT) { 3120 v2 shrink = {.x = UI_REGION_PAD, .y = UI_REGION_PAD}; 3121 draw_rect = shrink_rect_centered(draw_rect, shrink); 3122 draw_rect.size = v2_floor(draw_rect.size); 3123 BeginScissorMode((i32)draw_rect.pos.x, (i32)draw_rect.pos.y, (i32)draw_rect.size.w, (i32)draw_rect.size.h); 3124 draw_rect = draw_title_bar(ui, ui->arena, var, draw_rect, mouse); 3125 EndScissorMode(); 3126 } 3127 3128 /* TODO(rnp): post order traversal of the ui tree will remove the need for this */ 3129 if (!CheckCollisionPointRec(mouse.rl, draw_rect.rl)) 3130 mouse = (v2){.x = F32_INFINITY, .y = F32_INFINITY}; 3131 3132 draw_rect.size = v2_floor(draw_rect.size); 3133 BeginScissorMode((i32)draw_rect.pos.x, (i32)draw_rect.pos.y, (i32)draw_rect.size.w, (i32)draw_rect.size.h); 3134 switch (var->type) { 3135 case VT_UI_VIEW: { 3136 hover_interaction(ui, mouse, auto_interaction(draw_rect, var)); 3137 TextSpec text_spec = {.font = &ui->font, .colour = FG_COLOUR, .flags = TF_LIMITED}; 3138 draw_ui_view(ui, var, draw_rect, mouse, text_spec); 3139 } break; 3140 case VT_UI_REGION_SPLIT: { 3141 RegionSplit *rs = &var->region_split; 3142 3143 Rect split, hover; 3144 switch (rs->direction) { 3145 case RSD_VERTICAL: { 3146 split_rect_vertical(draw_rect, rs->fraction, 0, &split); 3147 split.pos.x += UI_REGION_PAD; 3148 split.pos.y -= UI_SPLIT_HANDLE_THICK / 2; 3149 split.size.h = UI_SPLIT_HANDLE_THICK; 3150 split.size.w -= 2 * UI_REGION_PAD; 3151 hover = extend_rect_centered(split, (v2){.y = 0.75f * UI_REGION_PAD}); 3152 } break; 3153 case RSD_HORIZONTAL: { 3154 split_rect_horizontal(draw_rect, rs->fraction, 0, &split); 3155 split.pos.x -= UI_SPLIT_HANDLE_THICK / 2; 3156 split.pos.y += UI_REGION_PAD; 3157 split.size.w = UI_SPLIT_HANDLE_THICK; 3158 split.size.h -= 2 * UI_REGION_PAD; 3159 hover = extend_rect_centered(split, (v2){.x = 0.75f * UI_REGION_PAD}); 3160 } break; 3161 } 3162 3163 Interaction drag = {.kind = InteractionKind_Drag, .rect = hover, .var = var}; 3164 hover_interaction(ui, mouse, drag); 3165 3166 v4 colour = HOVERED_COLOUR; 3167 colour.a = var->hover_t; 3168 DrawRectangleRounded(split.rl, 0.6f, 0, colour_from_normalized(colour)); 3169 } break; 3170 InvalidDefaultCase; 3171 } 3172 EndScissorMode(); 3173 } 3174 3175 function void 3176 draw_ui_regions(BeamformerUI *ui, Rect window, v2 mouse) 3177 { 3178 struct region_frame { 3179 Variable *var; 3180 Rect rect; 3181 } init[16]; 3182 3183 struct { 3184 struct region_frame *data; 3185 iz count; 3186 iz capacity; 3187 } stack = {init, 0, ARRAY_COUNT(init)}; 3188 3189 TempArena arena_savepoint = begin_temp_arena(&ui->arena); 3190 3191 *da_push(&ui->arena, &stack) = (struct region_frame){ui->regions, window}; 3192 while (stack.count) { 3193 struct region_frame *top = stack.data + --stack.count; 3194 Rect rect = top->rect; 3195 draw_layout_variable(ui, top->var, rect, mouse); 3196 3197 if (top->var->type == VT_UI_REGION_SPLIT) { 3198 Rect first, second; 3199 RegionSplit *rs = &top->var->region_split; 3200 switch (rs->direction) { 3201 case RSD_VERTICAL: { 3202 split_rect_vertical(rect, rs->fraction, &first, &second); 3203 } break; 3204 case RSD_HORIZONTAL: { 3205 split_rect_horizontal(rect, rs->fraction, &first, &second); 3206 } break; 3207 } 3208 3209 *da_push(&ui->arena, &stack) = (struct region_frame){rs->right, second}; 3210 *da_push(&ui->arena, &stack) = (struct region_frame){rs->left, first}; 3211 } 3212 } 3213 3214 end_temp_arena(arena_savepoint); 3215 } 3216 3217 function void 3218 draw_floating_widgets(BeamformerUI *ui, Rect window_rect, v2 mouse) 3219 { 3220 TextSpec text_spec = {.font = &ui->small_font, .colour = FG_COLOUR}; 3221 window_rect = shrink_rect_centered(window_rect, (v2){{UI_REGION_PAD, UI_REGION_PAD}}); 3222 for (Variable *var = ui->floating_widget_sentinal.parent; 3223 var != &ui->floating_widget_sentinal; 3224 var = var->parent) 3225 { 3226 if (var->type == VT_UI_TEXT_BOX) { 3227 UIView *fw = &var->view; 3228 InputState *is = &ui->text_input_state; 3229 3230 draw_ui_view_container(ui, var, mouse, fw->rect); 3231 3232 f32 cursor_width = (is->cursor == is->count) ? 0.55f * (f32)is->font->baseSize : 4.0f; 3233 s8 text = {.len = is->count, .data = is->buf}; 3234 v2 text_size = measure_text(*is->font, text); 3235 3236 f32 text_pad = 4.0f; 3237 f32 desired_width = text_pad + text_size.w + cursor_width; 3238 fw->rect.size = (v2){{MAX(desired_width, fw->rect.size.w), text_size.h + text_pad}}; 3239 3240 v2 text_position = {{fw->rect.pos.x + text_pad / 2, fw->rect.pos.y + text_pad / 2}}; 3241 f32 cursor_offset = measure_text(*is->font, (s8){is->cursor, text.data}).w; 3242 cursor_offset += text_position.x; 3243 3244 Rect cursor; 3245 cursor.pos = (v2){{cursor_offset, text_position.y}}; 3246 cursor.size = (v2){{cursor_width, text_size.h}}; 3247 3248 v4 cursor_colour = FOCUSED_COLOUR; 3249 cursor_colour.a = ease_in_out_cubic(is->cursor_blink.t); 3250 v4 text_colour = v4_lerp(FG_COLOUR, HOVERED_COLOUR, fw->child->hover_t); 3251 3252 TextSpec input_text_spec = {.font = is->font, .colour = text_colour}; 3253 draw_text(text, text_position, &input_text_spec); 3254 DrawRectanglePro(cursor.rl, (Vector2){0}, 0, colour_from_normalized(cursor_colour)); 3255 } else { 3256 draw_ui_view(ui, var, window_rect, mouse, text_spec); 3257 } 3258 } 3259 } 3260 3261 function void 3262 scroll_interaction(Variable *var, f32 delta) 3263 { 3264 switch (var->type) { 3265 case VT_B32:{ var->bool32 = !var->bool32; }break; 3266 case VT_F32:{ var->real32 += delta; }break; 3267 case VT_I32:{ var->signed32 += (i32)delta; }break; 3268 case VT_SCALED_F32:{ var->scaled_real32.val += delta * var->scaled_real32.scale; }break; 3269 case VT_BEAMFORMER_FRAME_VIEW:{ 3270 BeamformerFrameView *bv = var->generic; 3271 bv->threshold.real32 += delta; 3272 bv->dirty = 1; 3273 } break; 3274 case VT_BEAMFORMER_VARIABLE:{ 3275 BeamformerVariable *bv = &var->beamformer_variable; 3276 f32 value = *bv->store + delta * bv->scroll_scale; 3277 *bv->store = CLAMP(value, bv->limits.x, bv->limits.y); 3278 }break; 3279 case VT_CYCLER:{ 3280 if (delta > 0) *var->cycler.state += 1; 3281 else *var->cycler.state -= 1; 3282 *var->cycler.state %= var->cycler.cycle_length; 3283 }break; 3284 case VT_UI_VIEW:{ 3285 var->view.rect.pos.h += UI_SCROLL_SPEED * delta; 3286 var->view.rect.pos.h = MAX(0, var->view.rect.pos.h); 3287 }break; 3288 InvalidDefaultCase; 3289 } 3290 } 3291 3292 function void 3293 begin_text_input(InputState *is, Rect r, Variable *container, v2 mouse) 3294 { 3295 assert(container->type == VT_UI_TEXT_BOX); 3296 Font *font = is->font = is->hot_font; 3297 Stream s = {.cap = countof(is->buf), .data = is->buf}; 3298 stream_append_variable(&s, container->view.child); 3299 is->count = s.widx; 3300 is->container = container; 3301 3302 is->numeric = container->view.child->type != VT_LIVE_CONTROLS_STRING; 3303 if (container->view.child->type == VT_LIVE_CONTROLS_STRING) { 3304 BeamformerLiveImagingParameters *lip = container->view.child->generic; 3305 if (lip->save_name_tag_length <= 0) 3306 is->count = 0; 3307 } 3308 3309 /* NOTE: extra offset to help with putting a cursor at idx 0 */ 3310 f32 text_half_char_width = 10.0f; 3311 f32 hover_p = CLAMP01((mouse.x - r.pos.x) / r.size.w); 3312 i32 i; 3313 f32 x_off = text_half_char_width, x_bounds = r.size.w * hover_p; 3314 for (i = 0; i < is->count && x_off < x_bounds; i++) { 3315 /* NOTE: assumes font glyphs are ordered ASCII */ 3316 i32 idx = is->buf[i] - 0x20; 3317 x_off += (f32)font->glyphs[idx].advanceX; 3318 if (font->glyphs[idx].advanceX == 0) 3319 x_off += font->recs[idx].width; 3320 } 3321 is->cursor = i; 3322 } 3323 3324 function void 3325 end_text_input(InputState *is, Variable *var) 3326 { 3327 f32 value = 0; 3328 if (is->numeric) value = (f32)parse_f64((s8){.len = is->count, .data = is->buf}); 3329 3330 switch (var->type) { 3331 case VT_SCALED_F32:{ var->scaled_real32.val = value; }break; 3332 case VT_F32:{ var->real32 = value; }break; 3333 case VT_BEAMFORMER_VARIABLE:{ 3334 BeamformerVariable *bv = &var->beamformer_variable; 3335 *bv->store = CLAMP(value / bv->display_scale, bv->limits.x, bv->limits.y); 3336 var->hover_t = 0; 3337 }break; 3338 case VT_LIVE_CONTROLS_STRING:{ 3339 BeamformerLiveImagingParameters *lip = var->generic; 3340 mem_copy(lip->save_name_tag, is->buf, (uz)is->count % countof(lip->save_name_tag)); 3341 lip->save_name_tag_length = is->count % countof(lip->save_name_tag); 3342 }break; 3343 InvalidDefaultCase; 3344 } 3345 } 3346 3347 function b32 3348 update_text_input(InputState *is, Variable *var) 3349 { 3350 assert(is->cursor != -1); 3351 3352 ui_blinker_update(&is->cursor_blink, BLINK_SPEED); 3353 3354 var->hover_t -= 2 * HOVER_SPEED * dt_for_frame; 3355 var->hover_t = CLAMP01(var->hover_t); 3356 3357 /* NOTE: handle multiple input keys on a single frame */ 3358 for (i32 key = GetCharPressed(); 3359 is->count < countof(is->buf) && key > 0; 3360 key = GetCharPressed()) 3361 { 3362 b32 allow_key = !is->numeric || (BETWEEN(key, '0', '9') || (key == '.') || 3363 (key == '-' && is->cursor == 0)); 3364 if (allow_key) { 3365 mem_move(is->buf + is->cursor + 1, 3366 is->buf + is->cursor, 3367 (uz)(is->count - is->cursor)); 3368 is->buf[is->cursor++] = (u8)key; 3369 is->count++; 3370 } 3371 } 3372 3373 is->cursor -= (IsKeyPressed(KEY_LEFT) || IsKeyPressedRepeat(KEY_LEFT)) && is->cursor > 0; 3374 is->cursor += (IsKeyPressed(KEY_RIGHT) || IsKeyPressedRepeat(KEY_RIGHT)) && is->cursor < is->count; 3375 3376 if ((IsKeyPressed(KEY_BACKSPACE) || IsKeyPressedRepeat(KEY_BACKSPACE)) && is->cursor > 0) { 3377 is->cursor--; 3378 if (is->cursor < countof(is->buf) - 1) { 3379 mem_move(is->buf + is->cursor, 3380 is->buf + is->cursor + 1, 3381 (uz)(is->count - is->cursor - 1)); 3382 } 3383 is->count--; 3384 } 3385 3386 if ((IsKeyPressed(KEY_DELETE) || IsKeyPressedRepeat(KEY_DELETE)) && is->cursor < is->count) { 3387 mem_move(is->buf + is->cursor, 3388 is->buf + is->cursor + 1, 3389 (uz)(is->count - is->cursor - 1)); 3390 is->count--; 3391 } 3392 3393 b32 result = IsKeyPressed(KEY_ENTER); 3394 return result; 3395 } 3396 3397 function void 3398 scale_bar_interaction(BeamformerUI *ui, ScaleBar *sb, v2 mouse) 3399 { 3400 Interaction *it = &ui->interaction; 3401 b32 mouse_left_pressed = IsMouseButtonPressed(MOUSE_BUTTON_LEFT); 3402 b32 mouse_right_pressed = IsMouseButtonPressed(MOUSE_BUTTON_RIGHT); 3403 f32 mouse_wheel = GetMouseWheelMoveV().y; 3404 3405 if (mouse_left_pressed) { 3406 v2 world_mouse = screen_point_to_world_2d(mouse, it->rect.pos, 3407 v2_add(it->rect.pos, it->rect.size), 3408 (v2){{*sb->min_value, *sb->min_value}}, 3409 (v2){{*sb->max_value, *sb->max_value}}); 3410 f32 new_coord = F32_INFINITY; 3411 switch (sb->direction) { 3412 case SB_LATERAL: new_coord = world_mouse.x; break; 3413 case SB_AXIAL: new_coord = world_mouse.y; break; 3414 } 3415 if (sb->zoom_starting_coord == F32_INFINITY) { 3416 sb->zoom_starting_coord = new_coord; 3417 } else { 3418 f32 min = sb->zoom_starting_coord; 3419 f32 max = new_coord; 3420 if (min > max) swap(min, max); 3421 3422 v2_sll *savepoint = SLLPopFreelist(ui->scale_bar_savepoint_freelist); 3423 if (!savepoint) savepoint = push_struct(&ui->arena, v2_sll); 3424 3425 savepoint->v.x = *sb->min_value; 3426 savepoint->v.y = *sb->max_value; 3427 SLLPush(savepoint, sb->savepoint_stack); 3428 3429 *sb->min_value = min; 3430 *sb->max_value = max; 3431 3432 sb->zoom_starting_coord = F32_INFINITY; 3433 } 3434 } 3435 3436 if (mouse_right_pressed) { 3437 v2_sll *savepoint = sb->savepoint_stack; 3438 if (savepoint) { 3439 *sb->min_value = savepoint->v.x; 3440 *sb->max_value = savepoint->v.y; 3441 sb->savepoint_stack = savepoint->next; 3442 SLLPushFreelist(savepoint, ui->scale_bar_savepoint_freelist); 3443 } 3444 sb->zoom_starting_coord = F32_INFINITY; 3445 } 3446 3447 if (mouse_wheel != 0) { 3448 *sb->min_value += mouse_wheel * sb->scroll_scale.x; 3449 *sb->max_value += mouse_wheel * sb->scroll_scale.y; 3450 } 3451 } 3452 3453 function void 3454 ui_widget_bring_to_front(Variable *sentinal, Variable *widget) 3455 { 3456 /* TODO(rnp): clean up the linkage so this can be a macro */ 3457 widget->parent->next = widget->next; 3458 widget->next->parent = widget->parent; 3459 3460 widget->parent = sentinal; 3461 widget->next = sentinal->next; 3462 widget->next->parent = widget; 3463 sentinal->next = widget; 3464 } 3465 3466 function void 3467 ui_view_close(BeamformerUI *ui, Variable *view) 3468 { 3469 switch (view->type) { 3470 case VT_UI_MENU: 3471 case VT_UI_TEXT_BOX: 3472 { 3473 UIView *fw = &view->view; 3474 if (view->type == VT_UI_MENU) { 3475 assert(fw->child->type == VT_GROUP); 3476 fw->child->group.expanded = 0; 3477 fw->child->group.container = 0; 3478 } else { 3479 end_text_input(&ui->text_input_state, fw->child); 3480 } 3481 view->parent->next = view->next; 3482 view->next->parent = view->parent; 3483 if (fw->close) SLLPushFreelist(fw->close, ui->variable_freelist); 3484 SLLPushFreelist(view, ui->variable_freelist); 3485 }break; 3486 case VT_UI_VIEW:{ 3487 assert(view->parent->type == VT_UI_REGION_SPLIT); 3488 Variable *region = view->parent; 3489 3490 Variable *parent = region->parent; 3491 Variable *remaining = region->region_split.left; 3492 if (remaining == view) remaining = region->region_split.right; 3493 3494 ui_view_free(ui, view); 3495 3496 assert(parent->type == VT_UI_REGION_SPLIT); 3497 if (parent->region_split.left == region) { 3498 parent->region_split.left = remaining; 3499 } else { 3500 parent->region_split.right = remaining; 3501 } 3502 remaining->parent = parent; 3503 3504 SLLPushFreelist(region, ui->variable_freelist); 3505 }break; 3506 InvalidDefaultCase; 3507 } 3508 } 3509 3510 function void 3511 ui_button_interaction(BeamformerUI *ui, Variable *button) 3512 { 3513 assert(button->type == VT_UI_BUTTON); 3514 switch (button->button) { 3515 case UI_BID_VIEW_CLOSE:{ ui_view_close(ui, button->parent); }break; 3516 case UI_BID_FV_COPY_HORIZONTAL:{ 3517 ui_copy_frame(ui, button->parent->parent, RSD_HORIZONTAL); 3518 }break; 3519 case UI_BID_FV_COPY_VERTICAL:{ 3520 ui_copy_frame(ui, button->parent->parent, RSD_VERTICAL); 3521 }break; 3522 case UI_BID_GM_OPEN_VIEW_RIGHT:{ 3523 ui_add_live_frame_view(ui, button->parent->parent, RSD_HORIZONTAL, BeamformerFrameViewKind_Latest); 3524 }break; 3525 case UI_BID_GM_OPEN_VIEW_BELOW:{ 3526 ui_add_live_frame_view(ui, button->parent->parent, RSD_VERTICAL, BeamformerFrameViewKind_Latest); 3527 }break; 3528 } 3529 } 3530 3531 function void 3532 ui_begin_interact(BeamformerUI *ui, BeamformerInput *input, b32 scroll) 3533 { 3534 Interaction hot = ui->hot_interaction; 3535 if (hot.kind != InteractionKind_None) { 3536 if (hot.kind == InteractionKind_Auto) { 3537 switch (hot.var->type) { 3538 case VT_NULL:{ hot.kind = InteractionKind_Nop; }break; 3539 case VT_B32:{ hot.kind = InteractionKind_Set; }break; 3540 case VT_SCALE_BAR:{ hot.kind = InteractionKind_Set; }break; 3541 case VT_UI_BUTTON:{ hot.kind = InteractionKind_Button; }break; 3542 case VT_GROUP:{ hot.kind = InteractionKind_Set; }break; 3543 case VT_UI_TEXT_BOX: 3544 case VT_UI_MENU: 3545 { 3546 if (hot.var->type == VT_UI_MENU) { 3547 hot.kind = InteractionKind_Drag; 3548 } else { 3549 hot.kind = InteractionKind_Text; 3550 begin_text_input(&ui->text_input_state, hot.rect, hot.var, input->mouse); 3551 } 3552 ui_widget_bring_to_front(&ui->floating_widget_sentinal, hot.var); 3553 }break; 3554 case VT_UI_VIEW:{ 3555 if (scroll) hot.kind = InteractionKind_Scroll; 3556 else hot.kind = InteractionKind_Nop; 3557 }break; 3558 case VT_X_PLANE_SHIFT:{ 3559 assert(hot.var->parent && hot.var->parent->type == VT_BEAMFORMER_FRAME_VIEW); 3560 BeamformerFrameView *bv = hot.var->parent->generic; 3561 if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) { 3562 XPlaneShift *xp = &hot.var->x_plane_shift; 3563 xp->start_point = xp->end_point = bv->hit_test_point; 3564 hot.kind = InteractionKind_Drag; 3565 } else { 3566 if (scroll) { 3567 hot.kind = InteractionKind_Scroll; 3568 hot.var = &bv->threshold; 3569 } else { 3570 hot.kind = InteractionKind_Nop; 3571 } 3572 } 3573 }break; 3574 case VT_BEAMFORMER_FRAME_VIEW:{ 3575 if (scroll) { 3576 hot.kind = InteractionKind_Scroll; 3577 } else { 3578 BeamformerFrameView *bv = hot.var->generic; 3579 switch (bv->kind) { 3580 case BeamformerFrameViewKind_3DXPlane:{ hot.kind = InteractionKind_Drag; }break; 3581 default:{ 3582 hot.kind = InteractionKind_Nop; 3583 switch (++bv->ruler.state) { 3584 case RulerState_Start:{ 3585 hot.kind = InteractionKind_Ruler; 3586 v2 r_max = v2_add(hot.rect.pos, hot.rect.size); 3587 v2 p = screen_point_to_world_2d(input->mouse, hot.rect.pos, r_max, 3588 XZ(bv->min_coordinate), 3589 XZ(bv->max_coordinate)); 3590 bv->ruler.start = p; 3591 }break; 3592 case RulerState_Hold:{}break; 3593 default:{ bv->ruler.state = RulerState_None; }break; 3594 } 3595 }break; 3596 } 3597 } 3598 }break; 3599 case VT_CYCLER:{ 3600 if (scroll) hot.kind = InteractionKind_Scroll; 3601 else hot.kind = InteractionKind_Set; 3602 }break; 3603 case VT_BEAMFORMER_VARIABLE: 3604 case VT_LIVE_CONTROLS_STRING: 3605 case VT_F32: 3606 case VT_SCALED_F32: 3607 { 3608 if (scroll) { 3609 hot.kind = InteractionKind_Scroll; 3610 } else if (hot.var->flags & V_TEXT) { 3611 hot.kind = InteractionKind_Text; 3612 Variable *w = add_floating_view(ui, &ui->arena, VT_UI_TEXT_BOX, 3613 hot.rect.pos, hot.var, 0); 3614 w->view.rect = hot.rect; 3615 begin_text_input(&ui->text_input_state, hot.rect, w, input->mouse); 3616 } else { 3617 hot.kind = InteractionKind_Drag; 3618 } 3619 }break; 3620 InvalidDefaultCase; 3621 } 3622 } 3623 3624 ui->interaction = hot; 3625 3626 if (ui->interaction.var->flags & V_LIVE_CONTROL) { 3627 assert(ui->interaction.var->parent->type == VT_LIVE_CONTROLS_VIEW); 3628 BeamformerLiveControlsView *lv = ui->interaction.var->parent->generic; 3629 lv->active_field_flag = lv->hot_field_flag; 3630 } 3631 3632 if (ui->interaction.var->flags & V_HIDES_CURSOR) { 3633 HideCursor(); 3634 DisableCursor(); 3635 /* wtf raylib */ 3636 SetMousePosition((i32)input->mouse.x, (i32)input->mouse.y); 3637 } 3638 } else { 3639 ui->interaction.kind = InteractionKind_Nop; 3640 } 3641 } 3642 3643 function u32 3644 ui_cycler_delta_for_frame(void) 3645 { 3646 u32 result = (u32)GetMouseWheelMoveV().y; 3647 if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) result += 1; 3648 if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) result -= 1; 3649 return result; 3650 } 3651 3652 function void 3653 ui_extra_actions(BeamformerUI *ui, Variable *var) 3654 { 3655 switch (var->type) { 3656 case VT_CYCLER:{ 3657 assert(var->parent && var->parent->parent && var->parent->parent->type == VT_UI_VIEW); 3658 Variable *view_var = var->parent->parent; 3659 UIView *view = &view_var->view; 3660 switch (view->child->type) { 3661 case VT_BEAMFORMER_FRAME_VIEW:{ 3662 u32 delta = ui_cycler_delta_for_frame(); 3663 BeamformerFrameView *old = view->child->generic; 3664 BeamformerFrameView *new = view->child->generic = ui_beamformer_frame_view_new(ui, &ui->arena); 3665 BeamformerFrameViewKind last_kind = (old->kind - delta) % BeamformerFrameViewKind_Count; 3666 3667 /* NOTE(rnp): log_scale gets released below before its needed */ 3668 b32 log_scale = old->log_scale->bool32; 3669 ui_variable_free_group_items(ui, view->menu); 3670 3671 ui_beamformer_frame_view_release_subresources(ui, old, last_kind); 3672 ui_beamformer_frame_view_convert(ui, &ui->arena, view->child, view->menu, old->kind, old, log_scale); 3673 if (new->kind == BeamformerFrameViewKind_Copy && old->frame) 3674 ui_beamformer_frame_view_copy_frame(ui, new, old); 3675 3676 DLLRemove(old); 3677 SLLPushFreelist(old, ui->view_freelist); 3678 }break; 3679 InvalidDefaultCase; 3680 } 3681 }break; 3682 InvalidDefaultCase; 3683 } 3684 } 3685 3686 function void 3687 ui_live_control_update(BeamformerUI *ui, Variable *controls) 3688 { 3689 assert(controls->type == VT_LIVE_CONTROLS_VIEW); 3690 BeamformerSharedMemory *sm = ui->shared_memory.region; 3691 BeamformerLiveControlsView *lv = controls->generic; 3692 atomic_or_u32(&sm->live_imaging_dirty_flags, lv->active_field_flag); 3693 } 3694 3695 function void 3696 ui_end_interact(BeamformerUI *ui, v2 mouse) 3697 { 3698 Interaction *it = &ui->interaction; 3699 Variable *parent = it->var->parent; 3700 u32 flags = it->var->flags; 3701 3702 switch (it->kind) { 3703 case InteractionKind_Nop:{}break; 3704 case InteractionKind_Drag:{ 3705 switch (it->var->type) { 3706 case VT_X_PLANE_SHIFT:{ 3707 assert(parent && parent->type == VT_BEAMFORMER_FRAME_VIEW); 3708 XPlaneShift *xp = &it->var->x_plane_shift; 3709 BeamformerFrameView *view = parent->generic; 3710 BeamformerViewPlaneTag plane = view_plane_tag_from_x_plane_shift(view, it->var); 3711 f32 rotation = x_plane_rotation_for_view_plane(view, plane); 3712 m4 x_rotation = m4_rotation_about_y(rotation); 3713 v3 Z = x_rotation.c[2].xyz; 3714 f32 delta = v3_dot(Z, v3_sub(xp->end_point, xp->start_point)); 3715 xp->start_point = xp->end_point; 3716 3717 BeamformerSharedMemory *sm = ui->shared_memory.region; 3718 BeamformerLiveImagingParameters *li = &sm->live_imaging_parameters; 3719 li->image_plane_offsets[plane] += delta; 3720 atomic_or_u32(&sm->live_imaging_dirty_flags, BeamformerLiveImagingDirtyFlags_ImagePlaneOffsets); 3721 }break; 3722 default:{}break; 3723 } 3724 }break; 3725 case InteractionKind_Set:{ 3726 switch (it->var->type) { 3727 case VT_B32:{ it->var->bool32 = !it->var->bool32; }break; 3728 case VT_GROUP:{ it->var->group.expanded = !it->var->group.expanded; }break; 3729 case VT_SCALE_BAR:{ scale_bar_interaction(ui, &it->var->scale_bar, mouse); }break; 3730 case VT_CYCLER:{ 3731 *it->var->cycler.state += ui_cycler_delta_for_frame(); 3732 *it->var->cycler.state %= it->var->cycler.cycle_length; 3733 }break; 3734 InvalidDefaultCase; 3735 } 3736 }break; 3737 case InteractionKind_Menu:{ 3738 assert(it->var->type == VT_GROUP); 3739 VariableGroup *g = &it->var->group; 3740 if (g->container) { 3741 ui_widget_bring_to_front(&ui->floating_widget_sentinal, g->container); 3742 } else { 3743 g->container = add_floating_view(ui, &ui->arena, VT_UI_MENU, mouse, it->var, 1); 3744 } 3745 }break; 3746 case InteractionKind_Ruler:{ 3747 assert(it->var->type == VT_BEAMFORMER_FRAME_VIEW); 3748 ((BeamformerFrameView *)it->var->generic)->ruler.state = RulerState_None; 3749 }break; 3750 case InteractionKind_Button:{ ui_button_interaction(ui, it->var); }break; 3751 case InteractionKind_Scroll:{ scroll_interaction(it->var, GetMouseWheelMoveV().y); }break; 3752 case InteractionKind_Text:{ ui_view_close(ui, ui->text_input_state.container); }break; 3753 InvalidDefaultCase; 3754 } 3755 3756 if (flags & V_CAUSES_COMPUTE) 3757 ui->flush_params = 1; 3758 3759 if (flags & V_UPDATE_VIEW) { 3760 BeamformerFrameView *frame = parent->generic; 3761 /* TODO(rnp): more straight forward way of achieving this */ 3762 if (parent->type != VT_BEAMFORMER_FRAME_VIEW) { 3763 assert(parent->parent->type == VT_UI_VIEW); 3764 assert(parent->parent->view.child->type == VT_BEAMFORMER_FRAME_VIEW); 3765 frame = parent->parent->view.child->generic; 3766 } 3767 frame->dirty = 1; 3768 } 3769 3770 if (flags & V_LIVE_CONTROL) 3771 ui_live_control_update(ui, it->var->parent); 3772 3773 if (flags & V_HIDES_CURSOR) 3774 EnableCursor(); 3775 3776 if (flags & V_EXTRA_ACTION) 3777 ui_extra_actions(ui, it->var); 3778 3779 ui->interaction = (Interaction){.kind = InteractionKind_None}; 3780 } 3781 3782 function void 3783 ui_sticky_interaction_check_end(BeamformerUI *ui, v2 mouse) 3784 { 3785 Interaction *it = &ui->interaction; 3786 switch (it->kind) { 3787 case InteractionKind_Ruler:{ 3788 if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT) || !point_in_rect(mouse, it->rect)) 3789 ui_end_interact(ui, mouse); 3790 }break; 3791 case InteractionKind_Text:{ 3792 Interaction text_box = auto_interaction({{0}}, ui->text_input_state.container); 3793 if (!interactions_equal(text_box, ui->hot_interaction)) 3794 ui_end_interact(ui, mouse); 3795 }break; 3796 InvalidDefaultCase; 3797 } 3798 } 3799 3800 function void 3801 ui_interact(BeamformerUI *ui, BeamformerInput *input, Rect window_rect) 3802 { 3803 Interaction *it = &ui->interaction; 3804 if (it->kind == InteractionKind_None || interaction_is_sticky(*it)) { 3805 ui->hot_interaction = ui->next_interaction; 3806 3807 b32 mouse_left_pressed = IsMouseButtonPressed(MOUSE_BUTTON_LEFT); 3808 b32 mouse_right_pressed = IsMouseButtonPressed(MOUSE_BUTTON_RIGHT); 3809 b32 wheel_moved = GetMouseWheelMoveV().y != 0; 3810 if (mouse_right_pressed || mouse_left_pressed || wheel_moved) { 3811 if (it->kind != InteractionKind_None) 3812 ui_sticky_interaction_check_end(ui, input->mouse); 3813 ui_begin_interact(ui, input, wheel_moved); 3814 } 3815 } 3816 3817 switch (it->kind) { 3818 case InteractionKind_Nop:{ it->kind = InteractionKind_None; }break; 3819 case InteractionKind_None:{}break; 3820 case InteractionKind_Text:{ 3821 if (update_text_input(&ui->text_input_state, it->var)) 3822 ui_end_interact(ui, input->mouse); 3823 }break; 3824 case InteractionKind_Ruler:{ 3825 assert(it->var->type == VT_BEAMFORMER_FRAME_VIEW); 3826 BeamformerFrameView *bv = it->var->generic; 3827 v2 r_max = v2_add(it->rect.pos, it->rect.size); 3828 v2 mouse = clamp_v2_rect(input->mouse, it->rect); 3829 bv->ruler.end = screen_point_to_world_2d(mouse, it->rect.pos, r_max, 3830 XZ(bv->min_coordinate), 3831 XZ(bv->max_coordinate)); 3832 }break; 3833 case InteractionKind_Drag:{ 3834 if (!IsMouseButtonDown(MOUSE_BUTTON_LEFT) && !IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) { 3835 ui_end_interact(ui, input->mouse); 3836 } else { 3837 v2 ws = window_rect.size; 3838 v2 dMouse = v2_sub(input->mouse, input->last_mouse); 3839 3840 switch (it->var->type) { 3841 case VT_BEAMFORMER_VARIABLE:{ 3842 BeamformerVariable *bv = &it->var->beamformer_variable; 3843 /* TODO(rnp): vertical sliders? */ 3844 f32 mouse_frac = CLAMP01((input->mouse.x - it->rect.pos.x) / it->rect.size.w); 3845 *bv->store = bv->limits.x + mouse_frac * (bv->limits.y - bv->limits.x); 3846 }break; 3847 case VT_X_PLANE_SHIFT:{ 3848 assert(it->var->parent && it->var->parent->type == VT_BEAMFORMER_FRAME_VIEW); 3849 v2 mouse = clamp_v2_rect(input->mouse, it->rect); 3850 XPlaneShift *xp = &it->var->x_plane_shift; 3851 ray mouse_ray = ray_for_x_plane_view(ui, it->var->parent->generic, 3852 normalized_p_in_rect(it->rect, mouse, 0)); 3853 /* NOTE(rnp): project start point onto ray */ 3854 v3 s = v3_sub(xp->start_point, mouse_ray.origin); 3855 v3 r = v3_sub(mouse_ray.direction, mouse_ray.origin); 3856 f32 scale = v3_dot(s, r) / v3_magnitude_squared(r); 3857 xp->end_point = v3_add(mouse_ray.origin, v3_scale(r, scale)); 3858 }break; 3859 case VT_BEAMFORMER_FRAME_VIEW:{ 3860 BeamformerFrameView *bv = it->var->generic; 3861 switch (bv->kind) { 3862 case BeamformerFrameViewKind_3DXPlane:{ 3863 bv->rotation += dMouse.x / ws.w; 3864 if (bv->rotation > 1.0f) bv->rotation -= 1.0f; 3865 if (bv->rotation < 0.0f) bv->rotation += 1.0f; 3866 }break; 3867 InvalidDefaultCase; 3868 } 3869 }break; 3870 case VT_UI_MENU:{ 3871 v2 *pos = &ui->interaction.var->view.rect.pos; 3872 *pos = clamp_v2_rect(v2_add(*pos, dMouse), window_rect); 3873 }break; 3874 case VT_UI_REGION_SPLIT:{ 3875 f32 min_fraction = 0; 3876 dMouse = v2_mul(dMouse, (v2){{1.0f / ws.w, 1.0f / ws.h}}); 3877 RegionSplit *rs = &ui->interaction.var->region_split; 3878 switch (rs->direction) { 3879 case RSD_VERTICAL: { 3880 min_fraction = (UI_SPLIT_HANDLE_THICK + 0.5f * UI_REGION_PAD) / ws.h; 3881 rs->fraction += dMouse.y; 3882 } break; 3883 case RSD_HORIZONTAL: { 3884 min_fraction = (UI_SPLIT_HANDLE_THICK + 0.5f * UI_REGION_PAD) / ws.w; 3885 rs->fraction += dMouse.x; 3886 } break; 3887 } 3888 rs->fraction = CLAMP(rs->fraction, min_fraction, 1 - min_fraction); 3889 }break; 3890 default:{}break; 3891 } 3892 if (it->var->flags & V_LIVE_CONTROL) 3893 ui_live_control_update(ui, it->var->parent); 3894 } 3895 } break; 3896 default:{ ui_end_interact(ui, input->mouse); }break; 3897 } 3898 3899 ui->next_interaction = (Interaction){.kind = InteractionKind_None}; 3900 } 3901 3902 /* NOTE(rnp): this only exists to make asan less annoying. do not waste 3903 * people's time by freeing, closing, etc... */ 3904 DEBUG_EXPORT BEAMFORMER_DEBUG_UI_DEINIT_FN(beamformer_debug_ui_deinit) 3905 { 3906 #if ASAN_ACTIVE 3907 BeamformerUI *ui = ctx->ui; 3908 UnloadFont(ui->font); 3909 UnloadFont(ui->small_font); 3910 CloseWindow(); 3911 #endif 3912 } 3913 3914 function void 3915 ui_init(BeamformerCtx *ctx, Arena store) 3916 { 3917 BeamformerUI *ui = ctx->ui; 3918 if (!ui) { 3919 ui = ctx->ui = push_struct(&store, typeof(*ui)); 3920 ui->arena = store; 3921 ui->frame_view_render_context = &ctx->frame_view_render_context; 3922 ui->unit_cube_model = ctx->compute_context.unit_cube_model; 3923 ui->shared_memory = ctx->shared_memory; 3924 ui->beamformer_context = ctx; 3925 3926 /* TODO(rnp): better font, this one is jank at small sizes */ 3927 ui->font = LoadFontFromMemory(".ttf", beamformer_base_font, sizeof(beamformer_base_font), 28, 0, 0); 3928 ui->small_font = LoadFontFromMemory(".ttf", beamformer_base_font, sizeof(beamformer_base_font), 20, 0, 0); 3929 3930 ui->floating_widget_sentinal.parent = &ui->floating_widget_sentinal; 3931 ui->floating_widget_sentinal.next = &ui->floating_widget_sentinal; 3932 3933 Variable *split = ui->regions = add_ui_split(ui, 0, &ui->arena, s8("UI Root"), 0.36f, 3934 RSD_HORIZONTAL, ui->font); 3935 split->region_split.left = add_ui_split(ui, split, &ui->arena, s8(""), 0.475f, 3936 RSD_VERTICAL, ui->font); 3937 3938 split = split->region_split.right = add_ui_split(ui, split, &ui->arena, s8(""), 0.70f, 3939 RSD_HORIZONTAL, ui->font); 3940 { 3941 split->region_split.left = add_beamformer_frame_view(ui, split, &ui->arena, 3942 BeamformerFrameViewKind_Latest, 0, 0); 3943 split->region_split.right = add_live_controls_view(ui, split, &ui->arena); 3944 } 3945 split = split->parent; 3946 3947 split = split->region_split.left; 3948 split->region_split.left = add_beamformer_parameters_view(split, ctx); 3949 split->region_split.right = add_ui_split(ui, split, &ui->arena, s8(""), 0.22f, 3950 RSD_VERTICAL, ui->font); 3951 split = split->region_split.right; 3952 3953 split->region_split.left = add_compute_progress_bar(split, ctx); 3954 split->region_split.right = add_compute_stats_view(ui, split, &ui->arena, ctx); 3955 3956 /* NOTE(rnp): shrink variable size once this fires */ 3957 assert((uz)(ui->arena.beg - (u8 *)ui) < KB(64)); 3958 } 3959 } 3960 3961 function void 3962 validate_ui_parameters(BeamformerUIParameters *p) 3963 { 3964 if (p->output_min_coordinate.x > p->output_max_coordinate.x) 3965 swap(p->output_min_coordinate.x, p->output_max_coordinate.x); 3966 if (p->output_min_coordinate.z > p->output_max_coordinate.z) 3967 swap(p->output_min_coordinate.z, p->output_max_coordinate.z); 3968 } 3969 3970 function void 3971 draw_ui(BeamformerCtx *ctx, BeamformerInput *input, BeamformerFrame *frame_to_draw, BeamformerViewPlaneTag frame_plane) 3972 { 3973 BeamformerUI *ui = ctx->ui; 3974 BeamformerSharedMemory *sm = ctx->shared_memory.region; 3975 3976 ui->latest_plane[BeamformerViewPlaneTag_Count] = frame_to_draw; 3977 ui->latest_plane[frame_plane] = frame_to_draw; 3978 3979 asan_poison_region(ui->arena.beg, ui->arena.end - ui->arena.beg); 3980 3981 u32 selected_block = ui->selected_parameter_block % BeamformerMaxParameterBlockSlots; 3982 u32 selected_mask = 1 << selected_block; 3983 if (ctx->ui_dirty_parameter_blocks & selected_mask) { 3984 BeamformerParameterBlock *pb = beamformer_parameter_block_lock(&ctx->shared_memory, selected_block, 0); 3985 if (pb) { 3986 mem_copy(&ui->params, &pb->parameters_ui, sizeof(ui->params)); 3987 ui->flush_params = 0; 3988 atomic_and_u32(&ctx->ui_dirty_parameter_blocks, ~selected_mask); 3989 beamformer_parameter_block_unlock(&ctx->shared_memory, selected_block); 3990 } 3991 } 3992 3993 /* NOTE: process interactions first because the user interacted with 3994 * the ui that was presented last frame */ 3995 Rect window_rect = {.size = {{(f32)ctx->window_size.w, (f32)ctx->window_size.h}}}; 3996 ui_interact(ui, input, window_rect); 3997 3998 if (ui->flush_params) { 3999 validate_ui_parameters(&ui->params); 4000 if (ctx->latest_frame) { 4001 BeamformerParameterBlock *pb = beamformer_parameter_block_lock(&ctx->shared_memory, selected_block, 0); 4002 if (pb) { 4003 ui->flush_params = 0; 4004 mem_copy(&pb->parameters_ui, &ui->params, sizeof(ui->params)); 4005 mark_parameter_block_region_dirty(ctx->shared_memory.region, selected_block, 4006 BeamformerParameterBlockRegion_Parameters); 4007 beamformer_parameter_block_unlock(&ctx->shared_memory, selected_block); 4008 4009 BeamformerSharedMemoryLockKind dispatch_lock = BeamformerSharedMemoryLockKind_DispatchCompute; 4010 if (!sm->live_imaging_parameters.active && 4011 os_shared_memory_region_lock(&ctx->shared_memory, sm->locks, (i32)dispatch_lock, 0)) 4012 { 4013 BeamformWork *work = beamform_work_queue_push(ctx->beamform_work_queue); 4014 BeamformerViewPlaneTag tag = frame_to_draw ? frame_to_draw->view_plane_tag : 0; 4015 if (fill_frame_compute_work(ctx, work, tag, selected_block, 0)) 4016 beamform_work_queue_push_commit(ctx->beamform_work_queue); 4017 } 4018 os_wake_waiters(&ctx->compute_worker.sync_variable); 4019 } 4020 } 4021 } 4022 4023 /* NOTE(rnp): can't render to a different framebuffer in the middle of BeginDrawing()... */ 4024 update_frame_views(ui, window_rect); 4025 4026 BeginDrawing(); 4027 glClearNamedFramebufferfv(0, GL_COLOR, 0, BG_COLOUR.E); 4028 glClearNamedFramebufferfv(0, GL_DEPTH, 0, (f32 []){1}); 4029 4030 draw_ui_regions(ui, window_rect, input->mouse); 4031 draw_floating_widgets(ui, window_rect, input->mouse); 4032 EndDrawing(); 4033 }