1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
|
//- infinite Vulkan 2D diagram canvas
internal inline Vec2f canvas_w2s(View *view, Rect rect, f32 wx, f32 wy)
{
f32 cx = rect.x0 + rect_w(rect) * 0.5f;
f32 cy = rect.y0 + rect_h(rect) * 0.5f;
return Vec2f{cx + (wx - view->cam_x) * view->cam_zoom, cy + (wy - view->cam_y) * view->cam_zoom};
}
internal inline Vec2f canvas_s2w(View *view, Rect rect, f32 sx, f32 sy)
{
f32 cx = rect.x0 + rect_w(rect) * 0.5f;
f32 cy = rect.y0 + rect_h(rect) * 0.5f;
f32 z = Max(view->cam_zoom, 0.01f);
return Vec2f{view->cam_x + (sx - cx) / z, view->cam_y + (sy - cy) / z};
}
internal void canvas_fit_view(App *app, View *view, b32 selection_only)
{
Diagram *d = app->diagram;
f32 x0 = 1e9f, y0 = 1e9f, x1 = -1e9f, y1 = -1e9f;
u32 found = 0;
for(u32 i = 0; i < d->node_count; ++i) {
DiagramNode *n = &d->nodes[i];
if(!n->alive || diagram_node_hidden(d, (s32)i)) continue;
if(selection_only && !n->selected && (s32)i != app->active_node) continue;
x0 = Min(x0, n->x);
y0 = Min(y0, n->y);
x1 = Max(x1, n->x + n->w);
y1 = Max(y1, n->y + n->h);
++found;
}
if(!found) {
Rect b = diagram_bounds(d);
x0 = b.x0; y0 = b.y0; x1 = b.x1; y1 = b.y1;
}
view->cam_target_x = (x0 + x1) * 0.5f;
view->cam_target_y = (y0 + y1) * 0.5f;
// find the panel rect that holds this view
f32 vw = 760.0f, vh = 560.0f;
s32 vi = (s32)(view - app->layout.views);
for(s32 i = 0; i < UI_MAX_PANELS; ++i) {
Panel *p = &app->layout.panels[i];
if(p->kind == PANEL_LEAF && panel_active_view(&app->layout, i) == vi) {
vw = Max(rect_w(p->rect) - 48.0f, 160.0f);
vh = Max(rect_h(p->rect) - UI_TAB_H - 48.0f, 120.0f);
break;
}
}
f32 bw = Max(x1 - x0 + 80.0f, 200.0f);
f32 bh = Max(y1 - y0 + 80.0f, 160.0f);
view->cam_target_zoom = Clamp(Min(vw / bw, vh / bh), 0.12f, 1.35f);
if(!view->cam_init) {
view->cam_x = view->cam_target_x;
view->cam_y = view->cam_target_y;
view->cam_zoom = view->cam_target_zoom;
view->cam_init = true;
}
view->cam_fit_pending = false;
}
internal void canvas_pan_to_node(App *app, View *view, s32 node_index)
{
Diagram *d = app->diagram;
if(!view || node_index < 0 || node_index >= (s32)d->node_count || !d->nodes[node_index].alive) return;
DiagramNode *n = &d->nodes[node_index];
view->cam_target_x = n->x + n->w * 0.5f;
view->cam_target_y = n->y + n->h * 0.5f;
if(view->cam_target_zoom < 0.55f) view->cam_target_zoom = 0.85f;
}
// NOTE: subtle archetype accent tint derived from the theme so every color scheme stays cohesive
internal u32 node_accent_color(DiagramNode *n)
{
if(n->color) return n->color;
switch(n->kind) {
case NODE_STRUCT: return COLOR_ACCENT;
case NODE_CLASS: return color_lerp(COLOR_ACCENT, COLOR_TEXT, 0.35f);
case NODE_INTERFACE: return color_lerp(COLOR_ACCENT, COLOR_TEXT_DIM, 0.45f);
case NODE_ENUM: return color_lerp(COLOR_ACCENT, COLOR_TEXT_DIM, 0.25f);
case NODE_MODULE: return COLOR_FOCUS_EDGE;
case NODE_SYSTEM: return COLOR_ACCENT;
case NODE_STORAGE: return color_lerp(COLOR_ACCENT, COLOR_TEXT, 0.2f);
case NODE_STATE: return COLOR_ACCENT;
case NODE_NOTE: return COLOR_TEXT_DIM;
}
return COLOR_ACCENT;
}
internal void draw_dashed_rect_border(DrawList *d, Rect r, f32 thick, f32 period, u32 color)
{
draw_line_dashed(d, r.x0, r.y0, r.x1, r.y0, thick, period, 0.0f, color);
draw_line_dashed(d, r.x1, r.y0, r.x1, r.y1, thick, period, 0.0f, color);
draw_line_dashed(d, r.x1, r.y1, r.x0, r.y1, thick, period, 0.0f, color);
draw_line_dashed(d, r.x0, r.y1, r.x0, r.y0, thick, period, 0.0f, color);
}
internal void draw_canvas_grid(App *app, View *view, Rect rect)
{
if(ui_theme.grid_style == GRID_OFF) return;
DrawList *d = &app->draw;
f32 step = (f32)Max(ui_theme.grid_size, 8u);
while(step * view->cam_zoom < 14.0f) step *= 2.0f;
while(step * view->cam_zoom > 96.0f) step *= 0.5f;
Vec2f w0 = canvas_s2w(view, rect, rect.x0, rect.y0);
Vec2f w1 = canvas_s2w(view, rect, rect.x1, rect.y1);
f32 gx0 = floorf(w0.x / step) * step;
f32 gy0 = floorf(w0.y / step) * step;
u32 minor = color_alpha(COLOR_TEXT_FAINT, ui_theme.grid_style == GRID_DOTS ? 0.34f : 0.14f);
u32 major = color_alpha(COLOR_TEXT_FAINT, ui_theme.grid_style == GRID_DOTS ? 0.58f : 0.26f);
if(ui_theme.grid_style == GRID_LINES) {
for(f32 wx = gx0; wx <= w1.x; wx += step) {
Vec2f p = canvas_w2s(view, rect, wx, 0);
b32 m = ((s32)roundf(wx / step) % 4) == 0;
draw_rect(d, Rect{floorf(p.x), rect.y0, floorf(p.x) + 1.0f, rect.y1}, m ? major : minor, 0);
}
for(f32 wy = gy0; wy <= w1.y; wy += step) {
Vec2f p = canvas_w2s(view, rect, 0, wy);
b32 m = ((s32)roundf(wy / step) % 4) == 0;
draw_rect(d, Rect{rect.x0, floorf(p.y), rect.x1, floorf(p.y) + 1.0f}, m ? major : minor, 0);
}
} else {
f32 r = Clamp(view->cam_zoom * 1.15f, 0.75f, 1.6f);
u32 guard = 0;
for(f32 wy = gy0; wy <= w1.y && guard < 12000; wy += step) {
b32 my = ((s32)roundf(wy / step) % 4) == 0;
Vec2f py = canvas_w2s(view, rect, 0, wy);
for(f32 wx = gx0; wx <= w1.x && guard < 12000; wx += step) {
b32 mx = ((s32)roundf(wx / step) % 4) == 0;
Vec2f px = canvas_w2s(view, rect, wx, 0);
draw_square(d, px.x, py.y, (mx && my) ? r * 1.15f : r * 0.75f, (mx && my) ? major : minor);
++guard;
}
}
}
}
//- doc / link markers in a node's header (draw, hit test and tooltip share the geometry)
internal void node_marker_rects(App *app, View *view, Rect rect, s32 node_index, Rect *doc_r, Rect *link_r)
{
Diagram *dia = app->diagram;
DiagramNode *n = &dia->nodes[node_index];
FontSystem *fonts = &app->fonts;
f32 z = view->cam_zoom;
*doc_r = *link_r = Rect{0, 0, 0, 0};
if(!n->doc_len && !n->link[0]) return;
Vec2f p0 = canvas_w2s(view, rect, n->x, n->y);
Vec2f p1 = canvas_w2s(view, rect, n->x + n->w, n->y + n->h);
f32 sfont = Clamp(floorf(UI_FONT_SMALL * z + 0.5f), 7.5f, 18.0f);
f32 size = Clamp(13.0f * z, 9.0f, 18.0f);
f32 hh = n->header_h * z;
f32 right, cy;
if(n->kind == NODE_MODULE) {
f32 bw = font_measure(fonts, FONT_MONO, sfont * 0.88f, str8((char *)"MODULE"));
right = p1.x - bw - 14.0f * z;
cy = p0.y + hh * 0.5f;
} else if(node_kind_is_lifeline(n->kind)) {
right = p1.x - 4.0f * z;
cy = p0.y + size * 0.5f + 4.0f * z;
} else {
f32 bw = font_measure(fonts, FONT_MONO, sfont * 0.85f, str8((char *)node_kind_badge(n->kind)));
right = p1.x - bw - 9.0f * z * 0.8f - 6.0f * z;
f32 title_h = n->stereotype[0] ? 24.0f * z : Min(hh, p1.y - p0.y);
cy = p0.y + (n->stereotype[0] ? 2.0f * z : 0.0f) + title_h * 0.5f;
}
if(n->link[0]) {
*link_r = Rect{right - size, cy - size * 0.5f, right, cy + size * 0.5f};
right -= size + 3.0f * z;
}
if(n->doc_len) *doc_r = Rect{right - size, cy - size * 0.5f, right, cy + size * 0.5f};
}
internal void draw_node_markers(App *app, View *view, Rect rect, s32 node_index, f32 alpha_scale)
{
if(app->exporting) return;
DrawList *d = &app->draw;
Rect doc_r, link_r;
node_marker_rects(app, view, rect, node_index, &doc_r, &link_r);
b32 hot = (app->canvas_hover_marker == node_index);
u32 col = color_alpha(hot ? COLOR_ACCENT : COLOR_TEXT_DIM, alpha_scale);
if(rect_w(doc_r) > 0) {
// three lines of text
f32 w = rect_w(doc_r), h = rect_h(doc_r);
for(u32 i = 0; i < 3; ++i) {
f32 y = doc_r.y0 + h * (0.25f + 0.25f * (f32)i);
f32 x1 = doc_r.x0 + w * (i == 2 ? 0.62f : 0.9f);
draw_rect(d, Rect{doc_r.x0 + w * 0.1f, floorf(y), x1, floorf(y) + Max(1.0f, h * 0.1f)}, col, 0);
}
}
if(rect_w(link_r) > 0) {
// an arrow out of a box corner: "opens somewhere else"
f32 w = rect_w(link_r), h = rect_h(link_r);
f32 t = Max(1.2f, h * 0.1f);
draw_line_dashed(d, link_r.x0 + w * 0.15f, link_r.y0 + h * 0.4f, link_r.x0 + w * 0.15f, link_r.y1 - h * 0.15f, t, 0.0f, 0.0f, col);
draw_line_dashed(d, link_r.x0 + w * 0.15f, link_r.y1 - h * 0.15f, link_r.x1 - w * 0.4f, link_r.y1 - h * 0.15f, t, 0.0f, 0.0f, col);
draw_line_dashed(d, link_r.x0 + w * 0.38f, link_r.y1 - h * 0.38f, link_r.x1 - w * 0.1f, link_r.y0 + h * 0.1f, t, 0.0f, 0.0f, col);
draw_arrow(d, link_r.x1 - w * 0.4f, link_r.y0 + h * 0.4f, link_r.x1 - w * 0.08f, link_r.y0 + h * 0.08f, w * 0.22f, t, DRAW_ARROW_CHEVRON, col);
}
}
// NOTE: a small card of wrapped text near (x, y), kept inside bounds
internal void draw_tooltip(App *app, f32 x, f32 y, Rect bounds, String8 title, String8 body)
{
DrawList *d = &app->draw;
FontSystem *fonts = &app->fonts;
ScratchArena scratch = ScratchGet();
DocLines lines;
doc_wrap(scratch.arena, (const char *)body.data, (u32)body.size, 60, &lines);
u32 shown = Min(lines.count, 16u);
if(!body.size) shown = 0;
f32 fs = UI_FONT_SMALL, lh = fs * 1.45f;
f32 w = font_measure(fonts, FONT_SEMIBOLD, fs, title);
for(u32 i = 0; i < shown; ++i) w = Max(w, font_measure(fonts, FONT_MONO, fs, ToString8Size(body.data + lines.start[i], lines.length[i])));
w = Min(w + 20.0f, rect_w(bounds) - 16.0f);
f32 h = lh * (f32)(shown + 1) + 12.0f + (lines.count > shown ? lh : 0.0f);
Rect r = {x + 14.0f, y + 16.0f, 0, 0};
if(r.x0 + w > bounds.x1 - 8.0f) r.x0 = bounds.x1 - 8.0f - w;
if(r.y0 + h > bounds.y1 - 8.0f) r.y0 = y - 12.0f - h;
r.x0 = Max(r.x0, bounds.x0 + 8.0f);
r.y0 = Max(r.y0, bounds.y0 + 8.0f);
r.x1 = r.x0 + w;
r.y1 = r.y0 + h;
draw_shadow(d, r, 0x00000088u, 0, 10);
draw_rect(d, r, COLOR_TAB_ACTIVE, 0);
draw_border(d, r, COLOR_FOCUS_EDGE, 0, 1);
f32 ty = r.y0 + 6.0f;
draw_text_fit(d, fonts, FONT_SEMIBOLD, fs, r.x0 + 10.0f, ui_baseline(app, FONT_SEMIBOLD, fs, ty, lh), w - 20.0f, title, COLOR_TEXT);
ty += lh;
for(u32 i = 0; i < shown; ++i) {
draw_text_fit(d, fonts, FONT_MONO, fs, r.x0 + 10.0f, ui_baseline(app, FONT_MONO, fs, ty, lh), w - 20.0f, ToString8Size(body.data + lines.start[i], lines.length[i]), COLOR_TEXT_DIM);
ty += lh;
}
if(lines.count > shown) draw_text(d, fonts, FONT_MONO, fs, r.x0 + 10.0f, ui_baseline(app, FONT_MONO, fs, ty, lh), str8((char *)"... (docs view / enter for all of it)"), COLOR_TEXT_FAINT);
scratch_end(&scratch);
}
internal void draw_canvas_lifeline(App *app, View *view, Rect rect, s32 node_index, Rect nr, f32 alpha_scale, b32 sel, b32 hov, u32 accent)
{
Diagram *dia = app->diagram;
DiagramNode *n = &dia->nodes[node_index];
DrawList *d = &app->draw;
FontSystem *fonts = &app->fonts;
f32 z = view->cam_zoom;
f32 hh = n->header_h * z;
f32 cx = floorf((nr.x0 + nr.x1) * 0.5f) + 0.5f;
u32 line = color_alpha(sel ? COLOR_ACCENT : (hov ? COLOR_FOCUS_EDGE : COLOR_TEXT_FAINT), alpha_scale);
draw_line_dashed(d, cx, nr.y0 + hh, cx, nr.y1, (sel ? 1.8f : 1.2f) * Clamp(z, 0.7f, 1.4f), 9.0f * Clamp(z, 0.6f, 1.5f), 0.0f, line);
f32 fsize = Clamp(floorf(UI_FONT * z + 0.5f), 8.0f, 22.0f);
f32 sfont = Clamp(floorf(UI_FONT_SMALL * z + 0.5f), 7.5f, 18.0f);
b32 text = z >= 0.24f;
if(n->kind == NODE_ACTOR) {
u32 col = color_alpha(sel ? COLOR_ACCENT : accent, alpha_scale);
f32 t = 1.6f * Clamp(z, 0.7f, 1.4f);
f32 hy = nr.y0 + 10.0f * z;
draw_ring(d, cx, hy, 7.0f * z, t, col);
draw_line_dashed(d, cx, hy + 7.0f * z, cx, hy + 24.0f * z, t, 0.0f, 0.0f, col);
draw_line_dashed(d, cx - 11.0f * z, hy + 13.0f * z, cx + 11.0f * z, hy + 13.0f * z, t, 0.0f, 0.0f, col);
draw_line_dashed(d, cx, hy + 24.0f * z, cx - 9.0f * z, hy + 36.0f * z, t, 0.0f, 0.0f, col);
draw_line_dashed(d, cx, hy + 24.0f * z, cx + 9.0f * z, hy + 36.0f * z, t, 0.0f, 0.0f, col);
if(text) {
String8 name = str8(n->name);
f32 tw = Min(font_measure(fonts, FONT_SEMIBOLD, fsize, name), rect_w(nr));
f32 ny = nr.y0 + 46.0f * z;
draw_text_fit(d, fonts, FONT_SEMIBOLD, fsize, cx - tw * 0.5f, ui_baseline(app, FONT_SEMIBOLD, fsize, ny, 16.0f * z), rect_w(nr), name, color_alpha(sel ? COLOR_ACCENT : COLOR_TEXT, alpha_scale));
if(n->stereotype[0]) {
String8 st = str8(n->stereotype);
f32 sw = Min(font_measure(fonts, FONT_MONO, sfont * 0.85f, st), rect_w(nr));
draw_text_fit(d, fonts, FONT_MONO, sfont * 0.85f, cx - sw * 0.5f, ui_baseline(app, FONT_MONO, sfont * 0.85f, ny + 14.0f * z, 14.0f * z), rect_w(nr), st, color_alpha(COLOR_TEXT_DIM, alpha_scale));
}
}
} else {
Rect hdr = {nr.x0, nr.y0, nr.x1, nr.y0 + hh};
if(ui_theme.show_shadows && alpha_scale > 0.5f) draw_shadow(d, Rect{hdr.x0 + 2.0f * z, hdr.y0 + 3.0f * z, hdr.x1 + 2.0f * z, hdr.y1 + 3.0f * z}, 0x00000055u, 0, 10.0f * z);
draw_rect(d, hdr, color_alpha(COLOR_TAB_ACTIVE, alpha_scale), 0);
draw_rect(d, Rect{hdr.x0, hdr.y0, hdr.x1, hdr.y0 + 2.5f * Clamp(z, 0.7f, 1.4f)}, color_alpha(accent, alpha_scale), 0);
draw_border(d, hdr, color_alpha(sel ? COLOR_ACCENT : (hov ? COLOR_FOCUS_EDGE : COLOR_PANEL_EDGE), alpha_scale), 0, sel ? 1.8f : 1.0f);
if(text) {
String8 name = str8(n->name);
f32 tw = Min(font_measure(fonts, FONT_SEMIBOLD, fsize, name), rect_w(hdr) - 16.0f * z);
f32 title_h = n->stereotype[0] ? 26.0f * z : hh;
draw_text_fit(d, fonts, FONT_SEMIBOLD, fsize, cx - tw * 0.5f, ui_baseline(app, FONT_SEMIBOLD, fsize, hdr.y0 + (n->stereotype[0] ? 2.0f * z : 0.0f), title_h),
rect_w(hdr) - 16.0f * z, name, color_alpha(sel ? COLOR_ACCENT : COLOR_TEXT, alpha_scale));
if(n->stereotype[0]) {
String8 st = str8(n->stereotype);
f32 sw = Min(font_measure(fonts, FONT_MONO, sfont * 0.85f, st), rect_w(hdr) - 16.0f * z);
draw_text_fit(d, fonts, FONT_MONO, sfont * 0.85f, cx - sw * 0.5f, ui_baseline(app, FONT_MONO, sfont * 0.85f, hdr.y0 + 22.0f * z, 18.0f * z), rect_w(hdr) - 16.0f * z, st, color_alpha(COLOR_TEXT_DIM, alpha_scale));
}
}
}
if(text) draw_node_markers(app, view, rect, node_index, alpha_scale);
}
// NOTE: the lifeline nearest to (wx, wy) within tol world units of its line, for dropping messages
internal s32 nearest_lifeline(Diagram *dia, f32 wx, f32 wy, f32 tol)
{
s32 best = -1;
f32 best_d = tol;
for(u32 i = 0; i < dia->node_count; ++i) {
DiagramNode *n = &dia->nodes[i];
if(!n->alive || !node_kind_is_lifeline(n->kind) || diagram_node_hidden(dia, (s32)i)) continue;
if(wy < n->y || wy > n->y + n->h + 40.0f) continue;
f32 dx = fabsf(wx - (n->x + n->w * 0.5f));
if(dx < best_d) { best_d = dx; best = (s32)i; }
}
return best;
}
internal void draw_canvas_edge(App *app, View *view, Rect rect, s32 edge_index, f32 alpha_scale)
{
Diagram *dia = app->diagram;
DiagramEdge *e = &dia->edges[edge_index];
EdgePath ep;
if(!diagram_edge_route(dia, edge_index, &ep)) return;
// frustum cull against canvas viewport
Vec2f sb0 = canvas_w2s(view, rect, ep.bounds.x0, ep.bounds.y0);
Vec2f sb1 = canvas_w2s(view, rect, ep.bounds.x1, ep.bounds.y1);
if(sb1.x < rect.x0 || sb0.x > rect.x1 || sb1.y < rect.y0 || sb0.y > rect.y1) return;
++dia->visible_edges;
DrawList *d = &app->draw;
FontSystem *fonts = &app->fonts;
f32 z = view->cam_zoom;
b32 sel = !app->exporting && (e->selected || (app->active_edge == edge_index));
b32 hov = !app->exporting && (app->canvas_hover_edge == edge_index);
u32 base_col = e->color ? e->color : ((e->kind == EDGE_DATAFLOW) ? COLOR_ACCENT : COLOR_TEXT_DIM);
if(sel) base_col = COLOR_ACCENT;
else if(hov) base_col = COLOR_TEXT;
u32 col = color_alpha(base_col, alpha_scale);
f32 thick = ((e->kind == EDGE_DATAFLOW) ? 2.2f : 1.35f) * Clamp(z, 0.55f, 1.4f);
if(sel) thick += 0.8f;
b32 dashed = (e->kind == EDGE_DEPENDENCY || e->kind == EDGE_IMPLEMENTS || e->kind == EDGE_ASYNC || e->kind == EDGE_REPLY);
f32 period = dashed ? 10.0f * Clamp(z, 0.6f, 1.5f) : 0.0f;
f32 phase = 0.0f;
// pull endpoint slightly back from arrowhead tip for hollow triangles / diamonds
f32 src_trim = (e->kind == EDGE_COMPOSITION || e->kind == EDGE_AGGREGATION) ? 14.0f : 0.0f;
f32 dst_trim = (e->kind == EDGE_INHERITANCE || e->kind == EDGE_IMPLEMENTS) ? 12.0f : ((e->kind == EDGE_DATAFLOW || e->kind == EDGE_ASYNC || e->kind == EDGE_MESSAGE) ? 8.0f : 0.0f);
if(ep.routing == ROUTE_BEZIER) {
Vec2f p0 = canvas_w2s(view, rect, ep.points[0].x + ep.src_dir.x * src_trim, ep.points[0].y + ep.src_dir.y * src_trim);
Vec2f p1 = canvas_w2s(view, rect, ep.points[1].x - ep.dst_dir.x * dst_trim, ep.points[1].y - ep.dst_dir.y * dst_trim);
Vec2f c0 = canvas_w2s(view, rect, ep.c0.x, ep.c0.y);
Vec2f c1 = canvas_w2s(view, rect, ep.c1.x, ep.c1.y);
draw_bezier(d, p0.x, p0.y, c0.x, c0.y, c1.x, c1.y, p1.x, p1.y, thick, period, phase, col);
} else {
f32 accum = 0.0f;
for(u32 i = 0; i + 1 < ep.point_count; ++i) {
Vec2f a = ep.points[i];
Vec2f b = ep.points[i + 1];
if(i == 0 && src_trim > 0.0f) { a.x += ep.src_dir.x * src_trim; a.y += ep.src_dir.y * src_trim; }
if(i + 2 == ep.point_count && dst_trim > 0.0f) { b.x -= ep.dst_dir.x * dst_trim; b.y -= ep.dst_dir.y * dst_trim; }
Vec2f sa = canvas_w2s(view, rect, a.x, a.y);
Vec2f sb = canvas_w2s(view, rect, b.x, b.y);
f32 ph = (period > 0.5f) ? (phase - accum / period) : 0.0f;
draw_line_dashed(d, sa.x, sa.y, sb.x, sb.y, thick, period, ph, col);
accum += sqrtf((sb.x - sa.x) * (sb.x - sa.x) + (sb.y - sa.y) * (sb.y - sa.y));
}
}
// Arrowheads & UML diamonds (skipped only when zoomed extremely far out)
if(z >= 0.22f) {
f32 alen = 11.5f * Clamp(z, 0.55f, 1.35f);
f32 ahw = 5.5f * Clamp(z, 0.55f, 1.35f);
// Source marker: composition/aggregation diamond or bidir chevron
Vec2f s0 = canvas_w2s(view, rect, ep.points[0].x, ep.points[0].y);
if(e->kind == EDGE_COMPOSITION || e->kind == EDGE_AGGREGATION) {
f32 dlen = 14.0f * Clamp(z, 0.55f, 1.35f);
Vec2f stail = {s0.x + ep.src_dir.x * dlen, s0.y + ep.src_dir.y * dlen};
if(e->kind == EDGE_AGGREGATION) {
draw_arrow(d, stail.x, stail.y, s0.x, s0.y, ahw, 0.0f, DRAW_ARROW_DIAMOND, color_alpha(COLOR_BG, alpha_scale));
draw_arrow(d, stail.x, stail.y, s0.x, s0.y, ahw, 1.5f, DRAW_ARROW_DIAMOND, col);
} else {
draw_arrow(d, stail.x, stail.y, s0.x, s0.y, ahw, 0.0f, DRAW_ARROW_DIAMOND, col);
}
} else if(e->kind == EDGE_BIDIR) {
Vec2f stail = {s0.x + ep.src_dir.x * alen, s0.y + ep.src_dir.y * alen};
draw_arrow(d, stail.x, stail.y, s0.x, s0.y, ahw, thick, DRAW_ARROW_CHEVRON, col);
}
// Target marker
Vec2f s1 = canvas_w2s(view, rect, ep.points[ep.point_count - 1].x, ep.points[ep.point_count - 1].y);
Vec2f ttail = {s1.x - ep.dst_dir.x * alen, s1.y - ep.dst_dir.y * alen};
switch(e->kind) {
case EDGE_ASSOCIATION:
case EDGE_DEPENDENCY:
case EDGE_COMPOSITION:
case EDGE_AGGREGATION:
case EDGE_BIDIR:
case EDGE_REPLY:
draw_arrow(d, ttail.x, ttail.y, s1.x, s1.y, ahw, thick, DRAW_ARROW_CHEVRON, col);
break;
case EDGE_INHERITANCE:
case EDGE_IMPLEMENTS:
draw_arrow(d, ttail.x, ttail.y, s1.x, s1.y, ahw * 1.15f, 0.0f, DRAW_ARROW_TRIANGLE, color_alpha(COLOR_BG, alpha_scale));
draw_arrow(d, ttail.x, ttail.y, s1.x, s1.y, ahw * 1.15f, 1.5f, DRAW_ARROW_TRIANGLE, col);
break;
case EDGE_DATAFLOW:
case EDGE_ASYNC:
case EDGE_MESSAGE:
draw_arrow(d, ttail.x, ttail.y, s1.x, s1.y, ahw, 0.0f, DRAW_ARROW_TRIANGLE, col);
break;
default: break;
}
}
// Edge label & multiplicities (full LOD)
if(ui_theme.show_edge_labels && z >= 0.30f) {
f32 fsize = Clamp(floorf(UI_FONT_SMALL * z + 0.5f), 8.0f, 18.0f);
if(e->label[0]) {
String8 lbl = str8(e->label);
f32 tw = font_measure(fonts, FONT_MONO, fsize, lbl);
f32 lh = font_line_height(fonts, FONT_MONO, fsize) + 4.0f;
Vec2f lp = canvas_w2s(view, rect, ep.label_pos.x, ep.label_pos.y);
// NOTE: a self message's label starts right of its loop instead of sitting on it
if(e->src == e->dst && diagram_edge_is_message(dia, edge_index)) lp.x += tw * 0.5f + 6.0f;
Rect pill = {lp.x - tw * 0.5f - 6.0f, lp.y - lh * 0.5f, lp.x + tw * 0.5f + 6.0f, lp.y + lh * 0.5f};
draw_rect(d, pill, color_alpha(COLOR_BG, 0.90f * alpha_scale), 0);
if(e->doc_len && !app->exporting) draw_rect(d, Rect{pill.x1 - 3.0f, pill.y0, pill.x1, pill.y0 + 3.0f}, color_alpha(COLOR_ACCENT, alpha_scale), 0);
if(sel) draw_border(d, pill, color_alpha(COLOR_ACCENT, 0.65f), 0, 1.0f);
draw_text(d, fonts, FONT_MONO, fsize, pill.x0 + 6.0f, ui_baseline(app, FONT_MONO, fsize, pill.y0, lh), lbl, color_alpha(sel ? COLOR_ACCENT : COLOR_TEXT_DIM, alpha_scale));
}
if(e->src_mult[0]) {
Vec2f mp = canvas_w2s(view, rect, ep.src_mult_pos.x, ep.src_mult_pos.y);
draw_text(d, fonts, FONT_MONO, fsize * 0.9f, mp.x, mp.y, str8(e->src_mult), color_alpha(COLOR_TEXT_FAINT, alpha_scale));
}
if(e->dst_mult[0]) {
Vec2f mp = canvas_w2s(view, rect, ep.dst_mult_pos.x, ep.dst_mult_pos.y);
draw_text(d, fonts, FONT_MONO, fsize * 0.9f, mp.x, mp.y, str8(e->dst_mult), color_alpha(COLOR_TEXT_FAINT, alpha_scale));
}
}
}
internal void draw_canvas_node(App *app, View *view, Rect rect, s32 node_index, f32 alpha_scale)
{
Diagram *dia = app->diagram;
DiagramNode *n = &dia->nodes[node_index];
Vec2f p0 = canvas_w2s(view, rect, n->x, n->y);
Vec2f p1 = canvas_w2s(view, rect, n->x + n->w, n->y + n->h);
Rect nr = {p0.x, p0.y, p1.x, p1.y};
if(nr.x1 < rect.x0 - 20.0f || nr.x0 > rect.x1 + 20.0f || nr.y1 < rect.y0 - 20.0f || nr.y0 > rect.y1 + 20.0f) return;
++dia->visible_nodes;
DrawList *d = &app->draw;
FontSystem *fonts = &app->fonts;
f32 z = view->cam_zoom;
b32 sel = !app->exporting && (n->selected || (app->active_node == node_index));
b32 hov = !app->exporting && (app->canvas_hover_node == node_index);
u32 accent = node_accent_color(n);
f32 rad = (n->kind == NODE_STATE) ? 14.0f * z : 0.0f;
// Macro LOD when zoomed very far out (240fps on 1,000+ node graphs)
if(ui_theme.lod_enabled && z < 0.22f) {
u32 fill = (n->kind == NODE_MODULE) ? color_alpha(COLOR_PANEL, 0.28f * alpha_scale) : color_alpha(COLOR_PANEL, alpha_scale);
draw_rect(d, nr, fill, rad);
draw_border(d, nr, color_alpha(sel ? COLOR_ACCENT : accent, (sel ? 1.0f : 0.55f) * alpha_scale), rad, sel ? 1.8f : 1.0f);
return;
}
//- sequence participant: header (box or stick figure) and a dashed line down
if(node_kind_is_lifeline(n->kind)) {
draw_canvas_lifeline(app, view, rect, node_index, nr, alpha_scale, sel, hov, accent);
return;
}
//- shadow
if(ui_theme.show_shadows && n->kind != NODE_MODULE && alpha_scale > 0.5f) {
draw_shadow(d, Rect{nr.x0 + 2.0f * z, nr.y0 + 3.0f * z, nr.x1 + 2.0f * z, nr.y1 + 3.0f * z}, 0x00000055u, rad, 10.0f * z);
}
//- container (NODE_MODULE)
if(n->kind == NODE_MODULE) {
draw_rect(d, nr, color_alpha(COLOR_PANEL, 0.42f * alpha_scale), 0);
f32 hh = n->header_h * z;
Rect hdr = {nr.x0, nr.y0, nr.x1, Min(nr.y0 + hh, nr.y1)};
draw_rect(d, hdr, color_alpha(COLOR_TAB, 0.85f * alpha_scale), 0);
draw_rect(d, Rect{nr.x0, nr.y0, nr.x0 + 3.0f, hdr.y1}, color_alpha(accent, alpha_scale), 0);
u32 bcol = color_alpha(sel ? COLOR_ACCENT : (hov ? COLOR_FOCUS_EDGE : COLOR_PANEL_EDGE), alpha_scale);
if(n->border_style == BORDER_DASHED) draw_dashed_rect_border(d, nr, sel ? 1.6f : 1.1f, 10.0f * Clamp(z, 0.6f, 1.4f), bcol);
else draw_border(d, nr, bcol, 0, sel ? 1.6f : 1.1f);
if(z >= 0.30f) {
f32 fsize = Clamp(floorf(UI_FONT * z + 0.5f), 8.0f, 22.0f);
f32 sfont = Clamp(floorf(UI_FONT_SMALL * z + 0.5f), 7.5f, 18.0f);
f32 base = ui_baseline(app, FONT_SEMIBOLD, fsize, hdr.y0, rect_h(hdr));
f32 x = hdr.x0 + 10.0f * z;
draw_text(d, fonts, FONT_MONO, sfont, x, base, str8(n->collapsed ? "[+]" : "[-]"), color_alpha(COLOR_TEXT_DIM, alpha_scale));
x += 24.0f * z;
draw_text_fit(d, fonts, FONT_SEMIBOLD, fsize, x, base, rect_w(hdr) - 76.0f * z, str8(n->name), color_alpha(COLOR_TEXT, alpha_scale));
String8 badge = str8("MODULE");
f32 bw = font_measure(fonts, FONT_MONO, sfont * 0.88f, badge);
draw_text(d, fonts, FONT_MONO, sfont * 0.88f, hdr.x1 - bw - 8.0f * z, base, badge, color_alpha(COLOR_TEXT_FAINT, alpha_scale));
draw_node_markers(app, view, rect, node_index, alpha_scale);
}
if(sel) {
draw_square(d, nr.x1 - 4.0f, nr.y1 - 4.0f, 3.5f, COLOR_ACCENT);
}
return;
}
//- regular software structure node
u32 bg_col = (n->kind == NODE_NOTE) ? color_lerp(COLOR_PANEL, COLOR_TAB, 0.5f) : COLOR_PANEL;
u32 hdr_col = (n->kind == NODE_INTERFACE) ? COLOR_TAB : ((n->kind == NODE_NOTE) ? bg_col : COLOR_TAB_ACTIVE);
draw_rect(d, nr, color_alpha(bg_col, alpha_scale), rad);
f32 hh = Min(n->header_h * z, rect_h(nr));
Rect hdr = {nr.x0, nr.y0, nr.x1, nr.y0 + hh};
draw_rect(d, hdr, color_alpha(hdr_col, alpha_scale), rad);
if(rad > 0.5f && rect_h(nr) > hh + 2.0f) {
// square off bottom corners of header strip when body follows
draw_rect(d, Rect{hdr.x0, hdr.y1 - rad, hdr.x1, hdr.y1}, color_alpha(hdr_col, alpha_scale), 0);
}
// Kind-specific visual signifiers (crisp, minimalist, unmistakable)
if(n->kind == NODE_STORAGE) {
// top slab cap bar for storage/ring/arena/db
draw_rect(d, Rect{nr.x0, nr.y0, nr.x1, nr.y0 + 3.0f * Clamp(z, 0.7f, 1.5f)}, color_alpha(accent, alpha_scale), 0);
} else if(n->kind == NODE_ENUM) {
// double left bar for tagged union / enum
draw_rect(d, Rect{nr.x0, nr.y0, nr.x0 + 2.5f * Clamp(z, 0.7f, 1.4f), nr.y1}, color_alpha(accent, alpha_scale), 0);
draw_rect(d, Rect{nr.x0 + 4.5f * Clamp(z, 0.7f, 1.4f), nr.y0, nr.x0 + 6.0f * Clamp(z, 0.7f, 1.4f), hdr.y1}, color_alpha(accent, 0.55f * alpha_scale), 0);
} else if(n->kind != NODE_STATE && n->kind != NODE_NOTE) {
draw_rect(d, Rect{nr.x0, nr.y0, nr.x0 + 2.5f * Clamp(z, 0.7f, 1.4f), hdr.y1}, color_alpha(accent, alpha_scale), 0);
}
if(n->kind == NODE_NOTE) {
f32 f = 10.0f * z;
draw_triangle(d, Rect{nr.x1 - f, nr.y0, nr.x1, nr.y0 + f}, color_alpha(COLOR_FOCUS_EDGE, 0.5f * alpha_scale));
}
// Header text & badge
f32 fsize = Clamp(floorf(UI_FONT * z + 0.5f), 8.0f, 22.0f);
f32 sfont = Clamp(floorf(UI_FONT_SMALL * z + 0.5f), 7.5f, 18.0f);
{
String8 badge = str8(node_kind_badge(n->kind));
f32 bw = font_measure(fonts, FONT_MONO, sfont * 0.85f, badge);
f32 pad = 9.0f * z;
f32 title_h = n->stereotype[0] ? (24.0f * z) : hh;
f32 tbase = ui_baseline(app, FONT_SEMIBOLD, fsize, hdr.y0 + (n->stereotype[0] ? 2.0f * z : 0.0f), title_h);
draw_text_fit(d, fonts, FONT_SEMIBOLD, fsize, hdr.x0 + pad + (n->kind == NODE_ENUM ? 4.0f * z : 0.0f), tbase,
rect_w(hdr) - bw - pad * 2.5f, str8(n->name), color_alpha(sel ? COLOR_ACCENT : COLOR_TEXT, alpha_scale));
draw_text(d, fonts, FONT_MONO, sfont * 0.85f, hdr.x1 - bw - pad * 0.8f, tbase, badge, color_alpha(accent, 0.85f * alpha_scale));
if(n->stereotype[0] && z >= 0.30f) {
f32 sbase = ui_baseline(app, FONT_MONO, sfont * 0.88f, hdr.y0 + 22.0f * z, 18.0f * z);
draw_text_fit(d, fonts, FONT_MONO, sfont * 0.88f, hdr.x0 + pad, sbase, rect_w(hdr) - pad * 2.0f, str8(n->stereotype), color_alpha(COLOR_TEXT_DIM, alpha_scale));
}
if(z >= 0.30f) draw_node_markers(app, view, rect, node_index, alpha_scale);
}
// Compact LOD: stop before compartments
b32 full_lod = (!ui_theme.lod_enabled || z >= 0.28f);
if(!n->collapsed && full_lod) {
f32 y = hdr.y1;
f32 pad = 9.0f * z;
if(n->note[0]) {
draw_rect(d, Rect{nr.x0, y, nr.x1, y + 1.0f}, color_alpha(COLOR_PANEL_EDGE, alpha_scale), 0);
f32 nh = 24.0f * z;
draw_text_fit(d, fonts, FONT_REGULAR, sfont, nr.x0 + pad, ui_baseline(app, FONT_REGULAR, sfont, y, nh), rect_w(nr) - pad * 2.0f, str8(n->note), color_alpha(COLOR_TEXT_DIM, alpha_scale));
y += nh;
}
for(s32 s = n->first_section; s >= 0; s = dia->sections[s].next) {
NodeSection *sec = &dia->sections[s];
f32 sh = 20.0f * z;
draw_rect(d, Rect{nr.x0, y, nr.x1, y + 1.0f}, color_alpha(COLOR_PANEL_EDGE, alpha_scale), 0);
draw_rect(d, Rect{nr.x0 + 1.0f, y + 1.0f, nr.x1 - 1.0f, y + sh}, color_alpha(COLOR_HEADER, 0.75f * alpha_scale), 0);
f32 sb = ui_baseline(app, FONT_MONO, sfont * 0.85f, y, sh);
draw_text(d, fonts, FONT_MONO, sfont * 0.85f, nr.x0 + pad, sb, str8(sec->collapsed ? "+" : "-"), color_alpha(COLOR_TEXT_FAINT, alpha_scale));
draw_text_fit(d, fonts, FONT_MONO, sfont * 0.85f, nr.x0 + pad + 11.0f * z, sb, rect_w(nr) - pad * 2.0f - 12.0f * z, str8(sec->title), color_alpha(COLOR_TEXT_DIM, alpha_scale));
y += sh;
if(!sec->collapsed) {
f32 mh = 20.0f * z;
for(s32 m = sec->first_member; m >= 0; m = dia->members[m].next) {
NodeMember *mem = &dia->members[m];
f32 mb = ui_baseline(app, FONT_MONO, sfont, y, mh);
f32 mx = nr.x0 + pad;
if(mem->visibility) {
char vbuf[2] = {mem->visibility, 0};
u32 vcol = (mem->visibility == '+') ? accent : ((mem->visibility == '-') ? COLOR_TEXT_FAINT : COLOR_TEXT_DIM);
draw_text(d, fonts, FONT_MONO, sfont, mx, mb, str8(vbuf), color_alpha(vcol, alpha_scale));
mx += 12.0f * z;
}
// badge on the right if present
f32 right_limit = nr.x1 - pad;
if(mem->badge[0]) {
String8 bg = str8(mem->badge);
f32 gw = font_measure(fonts, FONT_MONO, sfont * 0.82f, bg) + 8.0f * z;
Rect br = {right_limit - gw, y + 3.0f * z, right_limit, y + mh - 3.0f * z};
draw_rect(d, br, color_alpha(COLOR_TRACK, alpha_scale), 0);
draw_text(d, fonts, FONT_MONO, sfont * 0.82f, br.x0 + 4.0f * z, ui_baseline(app, FONT_MONO, sfont * 0.82f, br.y0, rect_h(br)), bg, color_alpha(COLOR_TEXT_DIM, alpha_scale));
right_limit = br.x0 - 6.0f * z;
}
f32 nw = draw_text_fit(d, fonts, FONT_MONO, sfont, mx, mb, Max(right_limit - mx, 10.0f), str8(mem->name), color_alpha(COLOR_TEXT, alpha_scale));
if(mem->type[0] && mx + nw + 10.0f * z < right_limit) {
f32 tx = mx + nw + 6.0f * z;
draw_text_fit(d, fonts, FONT_MONO, sfont * 0.92f, tx, mb, right_limit - tx, str8(mem->type), color_alpha(COLOR_TEXT_DIM, alpha_scale));
}
y += mh;
}
y += 4.0f * z;
}
}
}
// Node border
u32 bcol = color_alpha(sel ? COLOR_ACCENT : (hov ? COLOR_FOCUS_EDGE : COLOR_PANEL_EDGE), alpha_scale);
f32 bw = sel ? 1.8f : (hov ? 1.4f : 1.0f);
if(n->border_style == BORDER_DASHED && rad < 0.5f) {
draw_dashed_rect_border(d, nr, bw, 8.0f * Clamp(z, 0.6f, 1.4f), bcol);
} else if(n->border_style == BORDER_DOUBLE && rad < 0.5f) {
draw_border(d, nr, bcol, 0, bw);
draw_border(d, rect_shrink(nr, 3.0f), color_alpha(bcol, 0.55f), 0, 1.0f);
} else {
draw_border(d, nr, bcol, rad, bw);
}
// Port handles (drag to connect!) and bottom-right resize handle
b32 show_ports = !app->exporting && ((ui_theme.port_visibility == PORT_VIS_ALWAYS) ||
(ui_theme.port_visibility == PORT_VIS_HOVER && (hov || sel || app->canvas_drag == CANVAS_DRAG_CONNECT)));
if(show_ports && z >= 0.40f && alpha_scale > 0.5f) {
f32 pr = 3.5f;
Vec2f ports[4] = {
{(nr.x0 + nr.x1) * 0.5f, nr.y0},
{(nr.x0 + nr.x1) * 0.5f, nr.y1},
{nr.x1, (nr.y0 + nr.y1) * 0.5f},
{nr.x0, (nr.y0 + nr.y1) * 0.5f},
};
for(u32 pi = 0; pi < 4; ++pi) {
draw_circle(d, ports[pi].x, ports[pi].y, pr, COLOR_BG);
draw_ring(d, ports[pi].x, ports[pi].y, pr, 1.4f, sel ? COLOR_ACCENT : COLOR_FOCUS_EDGE);
}
}
if(sel && z >= 0.35f) {
draw_square(d, nr.x1 - 3.5f, nr.y1 - 3.5f, 3.5f, COLOR_ACCENT);
}
}
internal void draw_canvas_minimap(App *app, PlatformInput *input, View *view, Rect rect, b32 modal)
{
if(!ui_theme.show_minimap || rect_w(rect) < 280.0f || rect_h(rect) < 220.0f) return;
DrawList *d = &app->draw;
Diagram *dia = app->diagram;
f32 mw = 156.0f, mh = 104.0f;
Rect mm = {rect.x1 - mw - 10.0f, rect.y1 - mh - 10.0f, rect.x1 - 10.0f, rect.y1 - 10.0f};
draw_rect(d, mm, color_alpha(COLOR_BG, 0.88f), 0);
draw_border(d, mm, COLOR_PANEL_EDGE, 0, 1.0f);
Rect wb = diagram_bounds(dia);
f32 pad = 80.0f;
wb.x0 -= pad; wb.y0 -= pad; wb.x1 += pad; wb.y1 += pad;
f32 sx = (mw - 12.0f) / Max(rect_w(wb), 1.0f);
f32 sy = (mh - 12.0f) / Max(rect_h(wb), 1.0f);
f32 s = Min(sx, sy);
f32 ox = mm.x0 + (mw - rect_w(wb) * s) * 0.5f;
f32 oy = mm.y0 + (mh - rect_h(wb) * s) * 0.5f;
draw_clip_push(d, rect_shrink(mm, 2.0f));
for(u32 i = 0; i < dia->node_count; ++i) {
DiagramNode *n = &dia->nodes[i];
if(!n->alive || diagram_node_hidden(dia, (s32)i)) continue;
Rect r = {ox + (n->x - wb.x0) * s, oy + (n->y - wb.y0) * s, ox + (n->x + n->w - wb.x0) * s, oy + (n->y + n->h - wb.y0) * s};
if(rect_w(r) < 2.0f) r.x1 = r.x0 + 2.0f;
if(rect_h(r) < 1.5f) r.y1 = r.y0 + 1.5f;
u32 col = (n->selected || (s32)i == app->active_node) ? COLOR_ACCENT : ((n->kind == NODE_MODULE) ? color_alpha(COLOR_PANEL_EDGE, 0.5f) : COLOR_TEXT_DIM);
draw_rect(d, r, col, 0);
}
// Current camera viewport box
Vec2f v0 = canvas_s2w(view, rect, rect.x0, rect.y0);
Vec2f v1 = canvas_s2w(view, rect, rect.x1, rect.y1);
Rect vp = {ox + (v0.x - wb.x0) * s, oy + (v0.y - wb.y0) * s, ox + (v1.x - wb.x0) * s, oy + (v1.y - wb.y0) * s};
draw_rect(d, vp, color_alpha(COLOR_ACCENT, 0.12f), 0);
draw_border(d, vp, COLOR_ACCENT, 0, 1.0f);
draw_clip_pop(d);
// Click or drag inside minimap to jump camera
s32 vi = (s32)(view - app->layout.views);
if(!modal && ui_hovered(input, mm) && ButtonPressed(input->mouse[0])) {
app->canvas_drag = CANVAS_DRAG_MINIMAP;
app->canvas_drag_view = vi;
}
if(app->canvas_drag == CANVAS_DRAG_MINIMAP && app->canvas_drag_view == vi) {
if(input->mouse[0].ended_down) {
view->cam_target_x = wb.x0 + (input->mouse_x - ox) / Max(s, 1e-4f);
view->cam_target_y = wb.y0 + (input->mouse_y - oy) / Max(s, 1e-4f);
view->cam_x = view->cam_target_x;
view->cam_y = view->cam_target_y;
} else {
app->canvas_drag = CANVAS_DRAG_NONE;
}
}
}
// NOTE: the diagram itself, shared by the canvas and the png export (which has no selection)
internal void draw_canvas_layers(App *app, View *view, Rect rect)
{
Diagram *dia = app->diagram;
s32 focus = app->exporting ? -1 : app->active_node;
dia->visible_nodes = 0;
dia->visible_edges = 0;
// 1. Containers
for(u32 i = 0; i < dia->node_count; ++i) {
if(!dia->nodes[i].alive || dia->nodes[i].kind != NODE_MODULE || diagram_node_hidden(dia, (s32)i)) continue;
f32 a = (diagram_tag_dimmed(dia, dia->nodes[i].tag) || !diagram_node_in_focus(dia, (s32)i, focus)) ? 0.25f : 1.0f;
draw_canvas_node(app, view, rect, (s32)i, a);
}
// 1b. Lifelines: under their messages
for(u32 i = 0; i < dia->node_count; ++i) {
if(!dia->nodes[i].alive || !node_kind_is_lifeline(dia->nodes[i].kind) || diagram_node_hidden(dia, (s32)i)) continue;
f32 a = (diagram_tag_dimmed(dia, dia->nodes[i].tag) || !diagram_node_in_focus(dia, (s32)i, focus)) ? 0.22f : 1.0f;
draw_canvas_node(app, view, rect, (s32)i, a);
}
// 2. Edges
for(u32 i = 0; i < dia->edge_count; ++i) {
if(!dia->edges[i].alive || diagram_edge_hidden(dia, (s32)i)) continue;
b32 in_focus = !dia->focus_mode || focus < 0 ||
(diagram_node_in_focus(dia, dia->edges[i].src, focus) && diagram_node_in_focus(dia, dia->edges[i].dst, focus));
f32 a = (diagram_tag_dimmed(dia, dia->edges[i].tag) || !in_focus) ? 0.16f : 1.0f;
draw_canvas_edge(app, view, rect, (s32)i, a);
}
// 3. Regular nodes
for(u32 i = 0; i < dia->node_count; ++i) {
if(!dia->nodes[i].alive || dia->nodes[i].kind == NODE_MODULE || node_kind_is_lifeline(dia->nodes[i].kind) || diagram_node_hidden(dia, (s32)i)) continue;
f32 a = (diagram_tag_dimmed(dia, dia->nodes[i].tag) || !diagram_node_in_focus(dia, (s32)i, focus)) ? 0.22f : 1.0f;
draw_canvas_node(app, view, rect, (s32)i, a);
}
}
// NOTE: the selected edge's bends (squares) and the middles of its segments (dots, drag one to add
// a bend there). geometry in screen space, shared by drawing and the mouse
typedef struct BendHit BendHit;
struct BendHit {
s32 bend; // an existing waypoint
s32 insert; // segment middle: insert a waypoint at this index
};
internal b32 bend_editable(App *app)
{
Diagram *dia = app->diagram;
s32 e = app->active_edge;
return !app->exporting && e >= 0 && e < (s32)dia->edge_count && dia->edges[e].alive && !diagram_edge_hidden(dia, e) &&
!diagram_edge_is_message(dia, e) && dia->edges[e].src != dia->edges[e].dst;
}
internal u32 bend_handles(App *app, View *view, Rect rect, Vec2f *bends, Vec2f *mids, s32 *mid_insert, u32 *mid_count)
{
Diagram *dia = app->diagram;
DiagramEdge *e = &dia->edges[app->active_edge];
EdgePath ep;
*mid_count = 0;
for(u32 w = 0; w < e->waypoint_count; ++w) bends[w] = canvas_w2s(view, rect, e->waypoints[w].x, e->waypoints[w].y);
if(!diagram_edge_route(dia, app->active_edge, &ep) || ep.routing == ROUTE_BEZIER) return e->waypoint_count;
// which path vertex each waypoint landed on, so a segment knows how many bends come before it
for(u32 i = 0; i + 1 < ep.point_count && *mid_count < EDGE_PATH_MAX_POINTS; ++i) {
Vec2f a = ep.points[i], b = ep.points[i + 1];
f32 len = fabsf(b.x - a.x) + fabsf(b.y - a.y);
if(len * view->cam_zoom < 28.0f) continue;
u32 before = 0;
for(u32 w = 0; w < e->waypoint_count; ++w) {
for(u32 k = 0; k <= i; ++k) {
if(fabsf(ep.points[k].x - e->waypoints[w].x) < 0.6f && fabsf(ep.points[k].y - e->waypoints[w].y) < 0.6f) { before = w + 1; break; }
}
}
mids[*mid_count] = canvas_w2s(view, rect, (a.x + b.x) * 0.5f, (a.y + b.y) * 0.5f);
mid_insert[*mid_count] = (s32)before;
++*mid_count;
}
return e->waypoint_count;
}
internal BendHit bend_hit_test(App *app, View *view, Rect rect, f32 mx, f32 my)
{
BendHit hit = {-1, -1};
if(!bend_editable(app)) return hit;
Vec2f bends[DIAGRAM_MAX_WAYPOINTS], mids[EDGE_PATH_MAX_POINTS];
s32 insert[EDGE_PATH_MAX_POINTS];
u32 mid_count = 0;
u32 count = bend_handles(app, view, rect, bends, mids, insert, &mid_count);
for(u32 i = 0; i < count; ++i) if(fabsf(mx - bends[i].x) < 7.0f && fabsf(my - bends[i].y) < 7.0f) { hit.bend = (s32)i; return hit; }
if(app->diagram->edges[app->active_edge].waypoint_count >= DIAGRAM_MAX_WAYPOINTS) return hit;
for(u32 i = 0; i < mid_count; ++i) if(fabsf(mx - mids[i].x) < 6.0f && fabsf(my - mids[i].y) < 6.0f) { hit.insert = insert[i]; return hit; }
return hit;
}
internal void draw_bend_handles(App *app, PlatformInput *input, View *view, Rect rect)
{
if(!bend_editable(app)) return;
DrawList *d = &app->draw;
Vec2f bends[DIAGRAM_MAX_WAYPOINTS], mids[EDGE_PATH_MAX_POINTS];
s32 insert[EDGE_PATH_MAX_POINTS];
u32 mid_count = 0;
u32 count = bend_handles(app, view, rect, bends, mids, insert, &mid_count);
b32 full = app->diagram->edges[app->active_edge].waypoint_count >= DIAGRAM_MAX_WAYPOINTS;
for(u32 i = 0; i < mid_count && !full; ++i) {
b32 hot = fabsf(input->mouse_x - mids[i].x) < 6.0f && fabsf(input->mouse_y - mids[i].y) < 6.0f;
draw_circle(d, mids[i].x, mids[i].y, hot ? 4.5f : 3.5f, COLOR_BG);
draw_ring(d, mids[i].x, mids[i].y, hot ? 4.5f : 3.5f, 1.3f, color_alpha(COLOR_ACCENT, hot ? 1.0f : 0.7f));
}
for(u32 i = 0; i < count; ++i) {
b32 hot = fabsf(input->mouse_x - bends[i].x) < 7.0f && fabsf(input->mouse_y - bends[i].y) < 7.0f;
draw_rect(d, Rect{bends[i].x - 4.5f, bends[i].y - 4.5f, bends[i].x + 4.5f, bends[i].y + 4.5f}, hot ? COLOR_ACCENT : COLOR_BG, 0);
draw_border(d, Rect{bends[i].x - 4.5f, bends[i].y - 4.5f, bends[i].x + 4.5f, bends[i].y + 4.5f}, COLOR_ACCENT, 0, 1.4f);
}
}
internal void draw_canvas(App *app, PlatformInput *input, s32 panel, View *view, Rect rect, b32 focused)
{
Diagram *dia = app->diagram;
DrawList *d = &app->draw;
FontSystem *fonts = &app->fonts;
b32 modal = (app->overlay != UI_OVERLAY_NONE);
s32 vi = (s32)(view - app->layout.views);
if(!view->cam_init || view->cam_fit_pending) {
canvas_fit_view(app, view, false);
}
if(app->pan_to_active && app->active_node >= 0) {
canvas_pan_to_node(app, view, app->active_node);
}
// Instant camera update (static, zero lingering glide animations)
view->cam_x = view->cam_target_x;
view->cam_y = view->cam_target_y;
view->cam_zoom = view->cam_target_zoom;
//- background & grid
draw_rect(d, rect, COLOR_BG, 0);
draw_canvas_grid(app, view, rect);
//- the floating inspector claims its rect before the diagram sees the mouse
b32 over_card = card_place(app, input, vi, view, rect) && ui_hovered(input, app->card_rect);
//- mouse interaction on canvas
b32 over = ui_hovered(input, rect) && !modal && !over_card && app->drag_view < 0 && app->resize_panel < 0;
Vec2f mworld = canvas_s2w(view, rect, input->mouse_x, input->mouse_y);
if(over) {
app->canvas_last_wx = mworld.x;
app->canvas_last_wy = mworld.y;
}
s32 tooltip_node = -1;
b32 tooltip_link = false;
f32 tooltip_x = 0.0f, tooltip_y = 0.0f;
app->canvas_hover_marker = -1;
// Check minimap hit so clicking minimap doesn't also click canvas behind it
Rect mm_rect = {rect.x1 - 166.0f, rect.y1 - 114.0f, rect.x1 - 10.0f, rect.y1 - 10.0f};
b32 over_mm = ui_theme.show_minimap && ui_hovered(input, mm_rect);
if(over && !over_mm) {
if(ButtonPressed(input->mouse[0]) || ButtonPressed(input->mouse[1]) || ButtonPressed(input->mouse[2])) {
app->canvas_press_x = input->mouse_x;
app->canvas_press_y = input->mouse_y;
}
// Wheel zoom to cursor (or horizontal pan)
if(input->scroll_y != 0.0f || input->scroll_x != 0.0f) {
app->layout.focused = panel;
if(input->mods & PLATFORM_MOD_SHIFT) {
view->cam_target_x += (input->scroll_y * 48.0f) / Max(view->cam_zoom, 0.1f);
view->cam_x = view->cam_target_x;
} else if(input->scroll_y != 0.0f) {
f32 factor = expf(-input->scroll_y * 0.14f);
f32 new_zoom = Clamp(view->cam_target_zoom * factor, 0.08f, 4.0f);
// keep world point under cursor fixed!
Vec2f before = canvas_s2w(view, rect, input->mouse_x, input->mouse_y);
view->cam_target_zoom = new_zoom;
f32 cx = rect.x0 + rect_w(rect) * 0.5f;
f32 cy = rect.y0 + rect_h(rect) * 0.5f;
view->cam_target_x = before.x - (input->mouse_x - cx) / new_zoom;
view->cam_target_y = before.y - (input->mouse_y - cy) / new_zoom;
view->cam_x = view->cam_target_x;
view->cam_y = view->cam_target_y;
view->cam_zoom = view->cam_target_zoom;
}
if(input->scroll_x != 0.0f) {
view->cam_target_x += (input->scroll_x * 48.0f) / Max(view->cam_zoom, 0.1f);
view->cam_x = view->cam_target_x;
}
}
// Hover hit test
s32 hit_node = diagram_hit_test_node(dia, mworld.x, mworld.y);
s32 hit_edge = (hit_node < 0) ? diagram_hit_test_edge(dia, mworld.x, mworld.y, 9.0f / Max(view->cam_zoom, 0.25f)) : -1;
app->canvas_hover_node = hit_node;
app->canvas_hover_edge = hit_edge;
// the selected edge's bend handles sit on top of everything
BendHit bend_hit = bend_hit_test(app, view, rect, input->mouse_x, input->mouse_y);
if(bend_hit.bend >= 0 || bend_hit.insert >= 0) { hit_node = -1; app->canvas_hover_node = -1; }
// doc / link markers in the hovered node's header
s32 marker_node = -1;
b32 marker_link = false;
if(hit_node >= 0 && view->cam_zoom >= 0.30f) {
Rect doc_r, link_r;
node_marker_rects(app, view, rect, hit_node, &doc_r, &link_r);
if(rect_w(link_r) > 0 && ui_hovered(input, rect_shrink(link_r, -2.0f))) { marker_node = hit_node; marker_link = true; }
else if(rect_w(doc_r) > 0 && ui_hovered(input, rect_shrink(doc_r, -2.0f))) { marker_node = hit_node; }
}
app->canvas_hover_marker = marker_node;
tooltip_node = marker_node;
tooltip_link = marker_link;
tooltip_x = input->mouse_x;
tooltip_y = input->mouse_y;
// Check if hovering a port dot or resize handle of hit_node / active_node
s32 port_node = -1;
s32 resize_node = -1;
for(s32 cand_i = 0; cand_i < 2; ++cand_i) {
s32 cand = (cand_i == 0) ? hit_node : app->active_node;
if(cand < 0 || cand >= (s32)dia->node_count || !dia->nodes[cand].alive || diagram_node_hidden(dia, cand)) continue;
if(bend_hit.bend >= 0 || bend_hit.insert >= 0) break;
DiagramNode *cn = &dia->nodes[cand];
if(node_kind_is_lifeline(cn->kind)) continue; // sized by their messages, connected by dragging the line
Vec2f sp0 = canvas_w2s(view, rect, cn->x, cn->y);
Vec2f sp1 = canvas_w2s(view, rect, cn->x + cn->w, cn->y + cn->h);
if(fabsf(input->mouse_x - (sp1.x - 3.5f)) < 8.0f && fabsf(input->mouse_y - (sp1.y - 3.5f)) < 8.0f) {
resize_node = cand;
break;
}
if(cn->kind != NODE_MODULE && ui_theme.port_visibility != PORT_VIS_OFF) {
Vec2f ports[4] = {
{(sp0.x + sp1.x) * 0.5f, sp0.y},
{(sp0.x + sp1.x) * 0.5f, sp1.y},
{sp1.x, (sp0.y + sp1.y) * 0.5f},
{sp0.x, (sp0.y + sp1.y) * 0.5f},
};
for(u32 pi = 0; pi < 4; ++pi) {
if(fabsf(input->mouse_x - ports[pi].x) < 8.0f && fabsf(input->mouse_y - ports[pi].y) < 8.0f) {
port_node = cand;
break;
}
}
if(port_node >= 0) break;
}
}
if(bend_hit.bend >= 0 || bend_hit.insert >= 0) app->canvas_cursor = PLATFORM_CURSOR_MOVE;
else if(marker_node >= 0) app->canvas_cursor = PLATFORM_CURSOR_POINTER;
else if(hit_node >= 0 && node_kind_is_lifeline(dia->nodes[hit_node].kind) && mworld.y > dia->nodes[hit_node].y + dia->nodes[hit_node].header_h) app->canvas_cursor = PLATFORM_CURSOR_CROSSHAIR;
else if(hit_edge >= 0 && diagram_edge_is_message(dia, hit_edge)) app->canvas_cursor = PLATFORM_CURSOR_RESIZE_Y;
else if(resize_node >= 0) app->canvas_cursor = PLATFORM_CURSOR_RESIZE_NWSE;
else if(port_node >= 0) app->canvas_cursor = PLATFORM_CURSOR_CROSSHAIR;
else if(hit_node >= 0) app->canvas_cursor = PLATFORM_CURSOR_MOVE;
else if(hit_edge >= 0) app->canvas_cursor = PLATFORM_CURSOR_POINTER;
// Middle button or Right-button on empty space = pan; Right-button on node = quick connect edge!
if(ButtonPressed(input->mouse[2]) || (ButtonPressed(input->mouse[1]) && hit_node < 0)) {
app->layout.focused = panel;
app->canvas_drag = CANVAS_DRAG_PAN;
app->canvas_drag_view = vi;
app->canvas_drag_start_x = input->mouse_x;
app->canvas_drag_start_y = input->mouse_y;
} else if(ButtonPressed(input->mouse[1]) && hit_node >= 0) {
app->layout.focused = panel;
app->active_node = hit_node;
app->active_edge = -1;
app->canvas_drag = CANVAS_DRAG_CONNECT;
app->canvas_drag_view = vi;
app->canvas_drag_node = hit_node;
app->canvas_press_wy = -1e9f;
} else if(ButtonPressed(input->mouse[0])) {
app->layout.focused = panel;
app->card_focused = false;
u64 now = time_now_ns();
b32 twice = (now - app->last_click_ns < (u64)ui_theme.double_click_ms * 1000000ull) &&
fabsf(input->mouse_x - app->last_click_x) < 5.0f && fabsf(input->mouse_y - app->last_click_y) < 5.0f;
if(bend_hit.bend >= 0 || bend_hit.insert >= 0) {
DiagramEdge *be = &dia->edges[app->active_edge];
diagram_push_undo(dia);
if(bend_hit.bend >= 0 && twice) {
// double click a bend: gone
for(u32 w = (u32)bend_hit.bend; w + 1 < be->waypoint_count; ++w) be->waypoints[w] = be->waypoints[w + 1];
--be->waypoint_count;
++dia->version;
now = 0;
} else {
s32 index = bend_hit.bend;
if(index < 0) {
index = Clamp(bend_hit.insert, 0, (s32)be->waypoint_count);
for(s32 w = (s32)be->waypoint_count; w > index; --w) be->waypoints[w] = be->waypoints[w - 1];
be->waypoints[index] = mworld;
++be->waypoint_count;
++dia->version;
}
app->canvas_drag = CANVAS_DRAG_BEND;
app->canvas_drag_view = vi;
app->canvas_drag_edge = app->active_edge;
app->canvas_drag_bend = index;
}
} else if(marker_node >= 0) {
diagram_clear_selection(dia);
dia->nodes[marker_node].selected = true;
app->active_node = marker_node;
app->active_edge = -1;
if(marker_link) app_open_link(app, marker_node, dia->repo[0] != 0);
else doc_edit_open(app, DOC_TARGET_NODE, marker_node);
} else if(hit_node >= 0 && node_kind_is_lifeline(dia->nodes[hit_node].kind) && mworld.y > dia->nodes[hit_node].y + dia->nodes[hit_node].header_h) {
// drag along a lifeline: a new message from here
diagram_clear_selection(dia);
dia->nodes[hit_node].selected = true;
app->active_node = hit_node;
app->active_edge = -1;
app->canvas_drag = CANVAS_DRAG_CONNECT;
app->canvas_drag_view = vi;
app->canvas_drag_node = hit_node;
app->canvas_press_wy = mworld.y;
} else if(resize_node >= 0) {
diagram_push_undo(dia);
app->active_node = resize_node;
app->canvas_drag = CANVAS_DRAG_RESIZE;
app->canvas_drag_view = vi;
app->canvas_drag_node = resize_node;
app->canvas_drag_last_wx = mworld.x;
app->canvas_drag_last_wy = mworld.y;
} else if(port_node >= 0) {
if(!dia->nodes[port_node].selected) {
diagram_clear_selection(dia);
dia->nodes[port_node].selected = true;
}
app->active_node = port_node;
app->active_edge = -1;
app->canvas_drag = CANVAS_DRAG_CONNECT;
app->canvas_drag_view = vi;
app->canvas_drag_node = port_node;
app->canvas_press_wy = -1e9f;
} else if(hit_node >= 0) {
DiagramNode *hn = &dia->nodes[hit_node];
// check click on collapse toggle (module [-] or section header)
b32 toggled = false;
if(hn->kind == NODE_MODULE && mworld.y < hn->y + hn->header_h && mworld.x < hn->x + 36.0f) {
diagram_push_undo(dia);
hn->collapsed = !hn->collapsed;
toggled = true;
} else if(!hn->collapsed && view->cam_zoom >= 0.44f && mworld.y > hn->y + hn->header_h) {
f32 sy = hn->y + hn->header_h + (hn->note[0] ? 24.0f : 0.0f);
for(s32 s = hn->first_section; s >= 0; s = dia->sections[s].next) {
if(mworld.y >= sy && mworld.y <= sy + 20.0f) {
diagram_push_undo(dia);
dia->sections[s].collapsed = !dia->sections[s].collapsed;
diagram_measure_node(dia, hit_node);
toggled = true;
break;
}
sy += 20.0f + (dia->sections[s].collapsed ? 0.0f : (f32)dia->sections[s].member_count * 20.0f + 4.0f);
}
}
if(!toggled) {
if(input->mods & PLATFORM_MOD_SHIFT) {
hn->selected = !hn->selected;
} else if(!hn->selected) {
diagram_clear_selection(dia);
hn->selected = true;
}
app->active_node = hit_node;
app->card_dismissed = 0; // clicking it again brings a closed card back
app->active_edge = -1;
if(twice) {
// double-click opens inline rename!
start_edit(app, EDIT_NODE_NAME, hit_node, hn->name);
now = 0;
} else {
diagram_push_undo(dia);
app->canvas_drag = CANVAS_DRAG_NODES;
app->canvas_drag_view = vi;
app->canvas_drag_node = hit_node;
app->canvas_drag_last_wx = mworld.x;
app->canvas_drag_last_wy = mworld.y;
}
}
} else if(hit_edge >= 0) {
if(!(input->mods & PLATFORM_MOD_SHIFT)) diagram_clear_selection(dia);
dia->edges[hit_edge].selected = true;
app->active_edge = hit_edge;
app->card_dismissed = 0;
app->active_node = -1;
if(twice) {
start_edit(app, EDIT_EDGE_LABEL, hit_edge, dia->edges[hit_edge].label);
now = 0;
} else if(diagram_edge_is_message(dia, hit_edge)) {
diagram_push_undo(dia);
app->canvas_drag = CANVAS_DRAG_MESSAGE;
app->canvas_drag_view = vi;
app->canvas_drag_edge = hit_edge;
app->canvas_drag_last_wy = mworld.y;
}
} else {
if(!(input->mods & PLATFORM_MOD_SHIFT)) {
diagram_clear_selection(dia);
app->active_node = -1;
app->active_edge = -1;
}
app->canvas_drag = CANVAS_DRAG_MARQUEE;
app->canvas_drag_view = vi;
app->canvas_marquee_x0 = mworld.x;
app->canvas_marquee_y0 = mworld.y;
app->canvas_marquee_x1 = mworld.x;
app->canvas_marquee_y1 = mworld.y;
}
app->last_click_ns = now;
app->last_click_x = input->mouse_x;
app->last_click_y = input->mouse_y;
}
}
//- ongoing drags for this canvas view
b32 guide_has_x = false, guide_has_y = false;
f32 guide_x = 0.0f, guide_y = 0.0f;
if(app->canvas_drag != CANVAS_DRAG_NONE && app->canvas_drag_view == vi) {
switch(app->canvas_drag) {
case CANVAS_DRAG_PAN: {
b32 held = input->mouse[2].ended_down || input->mouse[1].ended_down;
if(held) {
f32 dx = (input->mouse_x - app->canvas_drag_start_x) / Max(view->cam_zoom, 0.05f);
f32 dy = (input->mouse_y - app->canvas_drag_start_y) / Max(view->cam_zoom, 0.05f);
view->cam_x -= dx; view->cam_y -= dy;
view->cam_target_x = view->cam_x;
view->cam_target_y = view->cam_y;
app->canvas_drag_start_x = input->mouse_x;
app->canvas_drag_start_y = input->mouse_y;
app->canvas_cursor = PLATFORM_CURSOR_GRABBING;
} else {
app->canvas_drag = CANVAS_DRAG_NONE;
}
} break;
case CANVAS_DRAG_NODES: {
if(input->mouse[0].ended_down) {
f32 dx = mworld.x - app->canvas_drag_last_wx;
f32 dy = mworld.y - app->canvas_drag_last_wy;
if(fabsf(dx) > 0.01f || fabsf(dy) > 0.01f) {
for(u32 i = 0; i < dia->node_count; ++i) {
if(!dia->nodes[i].alive || !dia->nodes[i].selected) continue;
// avoid double-moving child if parent module is also selected
if(dia->nodes[i].parent >= 0 && dia->nodes[dia->nodes[i].parent].selected) continue;
diagram_move_node(dia, (s32)i, dx, dy);
}
// bends and messages whose both ends moved go along
for(u32 i = 0; i < dia->edge_count; ++i) {
DiagramEdge *e = &dia->edges[i];
if(!e->alive || (!e->waypoint_count && !diagram_edge_is_message(dia, (s32)i))) continue;
b32 moved_src = false, moved_dst = false;
for(s32 p = e->src; p >= 0 && !moved_src; p = dia->nodes[p].parent) moved_src = dia->nodes[p].selected;
for(s32 p = e->dst; p >= 0 && !moved_dst; p = dia->nodes[p].parent) moved_dst = dia->nodes[p].selected;
if(!moved_src || !moved_dst) continue;
for(u32 w = 0; w < e->waypoint_count; ++w) { e->waypoints[w].x += dx; e->waypoints[w].y += dy; }
if(diagram_edge_is_message(dia, (s32)i)) e->msg_y += dy;
}
app->canvas_drag_last_wx = mworld.x;
app->canvas_drag_last_wy = mworld.y;
}
// Smart alignment guides for active node
if(ui_theme.smart_guides && app->canvas_drag_node >= 0 && dia->nodes[app->canvas_drag_node].alive) {
DiagramNode *dn = &dia->nodes[app->canvas_drag_node];
f32 thresh = 6.0f / Max(view->cam_zoom, 0.3f);
for(u32 i = 0; i < dia->node_count; ++i) {
DiagramNode *o = &dia->nodes[i];
if(!o->alive || o->selected || o->kind == NODE_MODULE || diagram_node_hidden(dia, (s32)i)) continue;
if(!guide_has_x && fabsf(dn->x - o->x) < thresh) { guide_has_x = true; guide_x = o->x; }
else if(!guide_has_x && fabsf((dn->x + dn->w * 0.5f) - (o->x + o->w * 0.5f)) < thresh) { guide_has_x = true; guide_x = o->x + o->w * 0.5f; }
if(!guide_has_y && fabsf(dn->y - o->y) < thresh) { guide_has_y = true; guide_y = o->y; }
else if(!guide_has_y && fabsf((dn->y + dn->h * 0.5f) - (o->y + o->h * 0.5f)) < thresh) { guide_has_y = true; guide_y = o->y + o->h * 0.5f; }
}
}
app->canvas_cursor = PLATFORM_CURSOR_GRABBING;
} else {
// snap to grid on release
if(ui_theme.snap_to_grid && ui_theme.grid_size >= 8) {
f32 g = (f32)ui_theme.grid_size;
for(u32 i = 0; i < dia->node_count; ++i) {
if(!dia->nodes[i].alive || !dia->nodes[i].selected) continue;
dia->nodes[i].x = roundf(dia->nodes[i].x / g) * g;
dia->nodes[i].y = roundf(dia->nodes[i].y / g) * g;
}
}
diagram_fit_containers(dia);
diagram_fit_lifelines(dia);
++dia->version;
app->canvas_drag = CANVAS_DRAG_NONE;
}
} break;
case CANVAS_DRAG_BEND: {
s32 ei = app->canvas_drag_edge;
if(ei < 0 || ei >= (s32)dia->edge_count || !dia->edges[ei].alive || app->canvas_drag_bend >= (s32)dia->edges[ei].waypoint_count) {
app->canvas_drag = CANVAS_DRAG_NONE;
break;
}
Vec2f p = mworld;
if(ui_theme.snap_to_grid && ui_theme.grid_size >= 8) {
f32 g = (f32)ui_theme.grid_size * 0.5f;
p.x = roundf(p.x / g) * g;
p.y = roundf(p.y / g) * g;
}
dia->edges[ei].waypoints[app->canvas_drag_bend] = p;
app->canvas_cursor = PLATFORM_CURSOR_GRABBING;
if(!input->mouse[0].ended_down) {
++dia->version;
dia->dirty = true;
app->canvas_drag = CANVAS_DRAG_NONE;
}
} break;
case CANVAS_DRAG_MESSAGE: {
s32 ei = app->canvas_drag_edge;
if(ei < 0 || ei >= (s32)dia->edge_count || !dia->edges[ei].alive) { app->canvas_drag = CANVAS_DRAG_NONE; break; }
if(input->mouse[0].ended_down) {
f32 y = dia->edges[ei].msg_y + (mworld.y - app->canvas_drag_last_wy);
app->canvas_drag_last_wy = mworld.y;
// NOTE: never above the participants' heads
DiagramNode *a = &dia->nodes[dia->edges[ei].src], *b = &dia->nodes[dia->edges[ei].dst];
f32 top = Max(a->y + a->header_h, b->y + b->header_h) + 12.0f;
dia->edges[ei].msg_y = Max(y, top);
app->canvas_cursor = PLATFORM_CURSOR_RESIZE_Y;
} else {
if(ui_theme.snap_to_grid && ui_theme.grid_size >= 8) {
f32 g = (f32)ui_theme.grid_size * 0.5f;
dia->edges[ei].msg_y = roundf(dia->edges[ei].msg_y / g) * g;
}
diagram_fit_lifelines(dia);
++dia->version;
dia->dirty = true;
app->canvas_drag = CANVAS_DRAG_NONE;
}
} break;
case CANVAS_DRAG_RESIZE: {
if(input->mouse[0].ended_down && app->canvas_drag_node >= 0 && dia->nodes[app->canvas_drag_node].alive) {
DiagramNode *rn = &dia->nodes[app->canvas_drag_node];
rn->manual_size = true;
rn->w = Max(rn->min_w, mworld.x - rn->x);
rn->h = Max(rn->min_h, mworld.y - rn->y);
app->canvas_cursor = PLATFORM_CURSOR_RESIZE_NWSE;
} else {
++dia->version;
app->canvas_drag = CANVAS_DRAG_NONE;
}
} break;
case CANVAS_DRAG_CONNECT: {
b32 held = input->mouse[0].ended_down || input->mouse[1].ended_down;
app->canvas_cursor = PLATFORM_CURSOR_CROSSHAIR;
if(!held) {
s32 target = diagram_hit_test_node(dia, mworld.x, mworld.y);
// NOTE: a click on a port without dragging is not a request for a self loop
b32 moved = fabsf(input->mouse_x - app->canvas_press_x) > 3.0f || fabsf(input->mouse_y - app->canvas_press_y) > 3.0f;
b32 from_line = app->canvas_drag_node >= 0 && node_kind_is_lifeline(dia->nodes[app->canvas_drag_node].kind);
if(from_line && (target < 0 || !node_kind_is_lifeline(dia->nodes[target].kind))) {
s32 near = nearest_lifeline(dia, mworld.x, mworld.y, 40.0f / Max(view->cam_zoom, 0.2f));
if(near >= 0) target = near;
}
if(target >= 0 && app->canvas_drag_node >= 0 && (target != app->canvas_drag_node || moved)) {
diagram_push_undo(dia);
u32 kind = app->canvas_connect_kind;
b32 message = from_line && node_kind_is_lifeline(dia->nodes[target].kind);
if(message && !edge_kind_is_message(kind) && kind != EDGE_ASYNC) kind = EDGE_MESSAGE;
s32 ei = diagram_add_edge(dia, app->canvas_drag_node, target, (EdgeKind)kind, "");
if(ei >= 0 && message) {
f32 y = (app->canvas_press_wy > -1e8f) ? app->canvas_press_wy : mworld.y;
DiagramNode *a = &dia->nodes[app->canvas_drag_node], *b = &dia->nodes[target];
dia->edges[ei].msg_y = Max(y, Max(a->y + a->header_h, b->y + b->header_h) + 12.0f);
diagram_fit_lifelines(dia);
}
if(ei >= 0) {
diagram_clear_selection(dia);
dia->edges[ei].selected = true;
app->active_edge = ei;
app->active_node = -1;
app_message(app, "connected %s %s %s", dia->nodes[app->canvas_drag_node].name, edge_kind_symbol(app->canvas_connect_kind), dia->nodes[target].name);
}
}
app->canvas_drag = CANVAS_DRAG_NONE;
}
} break;
case CANVAS_DRAG_MARQUEE: {
if(input->mouse[0].ended_down) {
app->canvas_marquee_x1 = mworld.x;
app->canvas_marquee_y1 = mworld.y;
f32 mx0 = Min(app->canvas_marquee_x0, app->canvas_marquee_x1);
f32 my0 = Min(app->canvas_marquee_y0, app->canvas_marquee_y1);
f32 mx1 = Max(app->canvas_marquee_x0, app->canvas_marquee_x1);
f32 my1 = Max(app->canvas_marquee_y0, app->canvas_marquee_y1);
for(u32 i = 0; i < dia->node_count; ++i) {
DiagramNode *n = &dia->nodes[i];
if(!n->alive || n->kind == NODE_MODULE || diagram_node_hidden(dia, (s32)i)) continue;
b32 inside = (n->x + n->w > mx0 && n->x < mx1 && n->y + n->h > my0 && n->y < my1);
n->selected = inside;
}
} else {
app->canvas_drag = CANVAS_DRAG_NONE;
}
} break;
default: break;
}
}
//- render diagram layers: 1. containers -> 2. edges -> 3. nodes -> 4. overlays (guides/rubberband/marquee/minimap/hud)
draw_canvas_layers(app, view, rect);
draw_bend_handles(app, input, view, rect);
// 4. Smart alignment guides
if(guide_has_x) {
Vec2f g = canvas_w2s(view, rect, guide_x, 0);
draw_line_dashed(d, g.x, rect.y0, g.x, rect.y1, 1.0f, 8.0f, 0.0f, color_alpha(COLOR_ACCENT, 0.7f));
}
if(guide_has_y) {
Vec2f g = canvas_w2s(view, rect, 0, guide_y);
draw_line_dashed(d, rect.x0, g.y, rect.x1, g.y, 1.0f, 8.0f, 0.0f, color_alpha(COLOR_ACCENT, 0.7f));
}
// Rubber-band edge while dragging a connection
if(app->canvas_drag == CANVAS_DRAG_CONNECT && app->canvas_drag_view == vi && app->canvas_drag_node >= 0 && dia->nodes[app->canvas_drag_node].alive) {
DiagramNode *sn = &dia->nodes[app->canvas_drag_node];
Vec2f from = canvas_w2s(view, rect, sn->x + sn->w * 0.5f, sn->y + sn->h * 0.5f);
Vec2f to = {input->mouse_x, input->mouse_y};
if(node_kind_is_lifeline(sn->kind)) {
// a message is horizontal: from the line at the press height
f32 wy = (app->canvas_press_wy > -1e8f) ? app->canvas_press_wy : mworld.y;
from = canvas_w2s(view, rect, sn->x + sn->w * 0.5f, wy);
to.y = from.y;
}
draw_line_dashed(d, from.x, from.y, to.x, to.y, 1.8f, 8.0f, 0.0f, COLOR_ACCENT);
f32 dx = to.x - from.x, dy = to.y - from.y;
f32 len = Max(sqrtf(dx * dx + dy * dy), 1.0f);
draw_arrow(d, to.x - dx / len * 12.0f, to.y - dy / len * 12.0f, to.x, to.y, 6.0f, 1.8f, DRAW_ARROW_CHEVRON, COLOR_ACCENT);
}
// Marquee selection rectangle
if(app->canvas_drag == CANVAS_DRAG_MARQUEE && app->canvas_drag_view == vi) {
Vec2f s0 = canvas_w2s(view, rect, Min(app->canvas_marquee_x0, app->canvas_marquee_x1), Min(app->canvas_marquee_y0, app->canvas_marquee_y1));
Vec2f s1 = canvas_w2s(view, rect, Max(app->canvas_marquee_x0, app->canvas_marquee_x1), Max(app->canvas_marquee_y0, app->canvas_marquee_y1));
Rect mr = {s0.x, s0.y, s1.x, s1.y};
draw_rect(d, mr, COLOR_DROP, 0);
draw_border(d, mr, COLOR_ACCENT, 0, 1.0f);
}
// Compact canvas HUD strip (top-right inside canvas)
{
ScratchArena scratch = ScratchGet();
String8 hud = ui_format(scratch.arena, "%.0f%% %s%s%s %un %ue",
view->cam_zoom * 100.0f,
edge_routing_name(dia->default_routing),
ui_theme.snap_to_grid ? " snap" : "",
dia->focus_mode ? " FOCUS" : "",
dia->live_nodes, dia->live_edges);
f32 tw = font_measure(fonts, FONT_MONO, UI_FONT_SMALL, hud);
Rect pill = {rect.x1 - tw - 22.0f, rect.y0 + 8.0f, rect.x1 - 8.0f, rect.y0 + 28.0f};
draw_rect(d, pill, color_alpha(COLOR_PANEL, 0.88f), 0);
draw_border(d, pill, COLOR_PANEL_EDGE, 0, 1.0f);
draw_text(d, fonts, FONT_MONO, UI_FONT_SMALL, pill.x0 + 7.0f, ui_baseline(app, FONT_MONO, UI_FONT_SMALL, pill.y0, 20.0f), hud, COLOR_TEXT_DIM);
// Title badge top-left inside canvas
if(dia->title[0]) {
String8 title = ui_format(scratch.arena, "%s%s", dia->title, dia->dirty ? " *" : "");
f32 ttw = font_measure(fonts, FONT_SEMIBOLD, UI_FONT_SMALL, title);
Rect tpill = {rect.x0 + 8.0f, rect.y0 + 8.0f, rect.x0 + ttw + 24.0f, rect.y0 + 28.0f};
draw_rect(d, tpill, color_alpha(COLOR_PANEL, 0.88f), 0);
draw_border(d, tpill, focused ? COLOR_FOCUS_EDGE : COLOR_PANEL_EDGE, 0, 1.0f);
draw_text(d, fonts, FONT_SEMIBOLD, UI_FONT_SMALL, tpill.x0 + 8.0f, ui_baseline(app, FONT_SEMIBOLD, UI_FONT_SMALL, tpill.y0, 20.0f), title, COLOR_TEXT);
}
scratch_end(&scratch);
}
// Minimap (bottom-right)
draw_canvas_minimap(app, input, view, rect, modal || over_card);
// Marker tooltips: the doc text, or where the link goes
if(tooltip_node >= 0 && !modal && app->canvas_drag == CANVAS_DRAG_NONE) {
ScratchArena scratch = ScratchGet();
DiagramNode *tn = &dia->nodes[tooltip_node];
if(tooltip_link) {
char url[1024], path[1024];
u32 line = 0;
b32 has_url = diagram_link_url(dia, tn->link, url, sizeof(url));
b32 has_path = diagram_link_local(dia, tn->link, path, sizeof(path), &line);
String8 body = ui_format(scratch.arena, "%s%s%s\nclick: open %s gx: online gf: in the editor",
has_url ? url : "", has_url ? "\n" : "", has_path ? path : "", dia->repo[0] ? "online" : "the file");
draw_tooltip(app, tooltip_x, tooltip_y, rect, ui_format(scratch.arena, "%s", tn->link), body);
} else {
draw_tooltip(app, tooltip_x, tooltip_y, rect, ui_format(scratch.arena, "%s (click to edit)", tn->name), diagram_doc(dia, tn->doc_off, tn->doc_len));
}
scratch_end(&scratch);
}
// Floating inspector, on top of everything on the canvas, placed from this frame's final state
if(card_place(app, input, vi, view, rect)) {
app->card_visible = true;
card_draw(app, input, panel, rect, over_card);
}
}
|