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