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