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