sunrize 2.3.4 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2368 @@
1
+ "use strict";
2
+
3
+ const
4
+ $ = require ("jquery"),
5
+ electron = require ("electron"),
6
+ Interface = require ("../Application/Interface"),
7
+ X3D = require ("../X3D"),
8
+ Editor = require ("../Undo/Editor"),
9
+ UndoManager = require ("../Undo/UndoManager"),
10
+ util = require ("util"),
11
+ _ = require ("../Application/GetText");
12
+
13
+ module .exports = class RouteGraph extends Interface
14
+ {
15
+ #style = window .getComputedStyle ($("#route-graph") [0]);
16
+
17
+ get outlineEditor ()
18
+ {
19
+ const document = require ("../Application/Window");
20
+
21
+ return document .sidebar .outlineEditor;
22
+ }
23
+
24
+ get page ()
25
+ {
26
+ const
27
+ active = this .top .tabs ("option", "active"),
28
+ pages = this .pages,
29
+ page = pages [active];
30
+
31
+ return page;
32
+ }
33
+
34
+ constructor (element)
35
+ {
36
+ super (`Sunrize.RouteGraph.${element .attr ("id")}.`);
37
+
38
+ this .config .global .setDefaultValues ({
39
+ snapToGrid: false,
40
+ addConnectedNodes: true,
41
+ });
42
+
43
+ this .editor = element
44
+ .on ("dragenter dragover", event => this .dragEnter (event))
45
+ .on ("drop", event => this .drop (event));
46
+
47
+ this .top = $("<div></div>")
48
+ .addClass ("route-graph-top")
49
+ .on ("scroll", () => this .top .scrollTop (0))
50
+ .on ("tabsactivate", () => this .activatePage ())
51
+ .appendTo (this .editor);
52
+
53
+ this .topCanvas = $("<canvas></canvas>")
54
+ .appendTo (this .top);
55
+
56
+ this .tabs = $("<ul></ul>")
57
+ .sortable ()
58
+ .on ("sortupdate", (event, ui) => this .reorderPages (ui .item))
59
+ .appendTo (this .top);
60
+
61
+ this .toolbar = $("<div></div>")
62
+ .addClass (["toolbar", "vertical-toolbar", "secondary-toolbar", "routing-toolbar"])
63
+ .appendTo (this .editor);
64
+
65
+ this .addPageButton = $("<span></span>")
66
+ .addClass ("material-icons")
67
+ .attr ("title", _("Add new logic page."))
68
+ .text ("add")
69
+ .appendTo (this .toolbar)
70
+ .on ("click", () => this .addPage ());
71
+
72
+ $("<span></span>") .addClass ("separator") .appendTo (this .toolbar);
73
+
74
+ this .snapToGridButton = $("<span></span>")
75
+ .addClass ("material-symbols-outlined")
76
+ .attr ("title", _("Toggle snap to grid."))
77
+ .css ({ transform: "scale(0.9)" })
78
+ .text ("grid_4x4")
79
+ .appendTo (this .toolbar)
80
+ .on ("click", () => this .setSnapToGrid (!this .config .global .snapToGrid));
81
+
82
+ $("<span></span>") .addClass ("separator") .appendTo (this .toolbar);
83
+
84
+ this .addConnectedNodesButton = $("<span></span>")
85
+ .addClass ("material-symbols-outlined")
86
+ .attr ("title", _("Toggle add connected nodes."))
87
+ .css ({ transform: "scale(0.8)" })
88
+ .text ("hub")
89
+ .appendTo (this .toolbar)
90
+ .on ("click", () => this .setAddConnectedNodes (!this .config .global .addConnectedNodes));
91
+
92
+ $("<span></span>") .addClass ("separator") .appendTo (this .toolbar);
93
+
94
+ this .zoomOutIcon = $("<span></span>")
95
+ .addClass ("material-icons")
96
+ .attr ("title", _("Zoom nodes out. (-)"))
97
+ .css ("transform", "scale(1.5)")
98
+ .css ("margin-top", "13px")
99
+ .css ("margin-bottom", "15px")
100
+ .text ("zoom_out")
101
+ .appendTo (this .toolbar)
102
+ .on ("click", () => this .zoomOut ());
103
+
104
+ this .zoomInIcon = $("<span></span>")
105
+ .addClass ("material-icons")
106
+ .attr ("title", _("Zoom nodes in. (+)"))
107
+ .css ("transform", "scale(1.5)")
108
+ .css ("margin-bottom", "15px")
109
+ .text ("zoom_in")
110
+ .appendTo (this .toolbar)
111
+ .on ("click", () => this .zoomIn ());
112
+
113
+ this .zoomFitIcon = $("<span></span>")
114
+ .addClass ("material-icons")
115
+ .attr ("title", _("Zoom nodes to fit in window. (0)"))
116
+ .css ("transform", "scale(1.4)")
117
+ .css ("margin-bottom", "15px")
118
+ .text ("fit_screen")
119
+ .appendTo (this .toolbar)
120
+ .on ("click", () => this .zoomFit ());
121
+
122
+ this .zoom100Icon = $("<span></span>")
123
+ .addClass ("material-icons")
124
+ .attr ("title", _("Default nodes zoom. (1)"))
125
+ .css ("transform", "scale(1.4)")
126
+ .css ("margin-bottom", "15px")
127
+ .text ("1x_mobiledata")
128
+ .appendTo (this .toolbar)
129
+ .on ("click", () => this .zoom100 ());
130
+
131
+ this .left = $("<div></div>")
132
+ .addClass ("pages")
133
+ .appendTo (this .editor);
134
+
135
+ this .canvas = $("<canvas></canvas>")
136
+ .addClass ("routes")
137
+ .appendTo (this .left);
138
+
139
+ this .resizer = new ResizeObserver (() => this .resizeCanvas ());
140
+ this .resizer .observe (this .left [0]);
141
+
142
+ this .nodes = $("<div></div>")
143
+ .attr ("tabindex", 0)
144
+ .addClass ("nodes")
145
+ .on ("mouseup", () => this .clearInputOutput ())
146
+ .on ("mouseup", event => this .selectRoute (event, false))
147
+ .on ("dblclick", event => this .deleteRoute (event))
148
+ .on ("scroll", () => this .scrollPage ())
149
+ .on ("scrollend", () => this .scrollPageEnd ())
150
+ .on ("wheel", event => this .scalePageWithWheel (event))
151
+ .on ("contextmenu", event => this .showContextMenu (event))
152
+ .on ("mousemove", event => this .mouseMove (event))
153
+ .on ("mousedown", event => this .drawLassoStart (event))
154
+ .on ("keydown", event => this .keyDown (event))
155
+ .appendTo (this .left);
156
+
157
+ this .title = $("<input>")
158
+ .addClass ("title")
159
+ .on ("input", () => this .updateTitle ())
160
+ .on ("mousedown contextmenu", event => event .stopPropagation ())
161
+ .appendTo (this .nodes);
162
+
163
+ this .placeholder = $("<div></div>")
164
+ .addClass ("placeholder")
165
+ .text (_("Drag and drop nodes here."))
166
+ .appendTo (this .nodes);
167
+
168
+ this .overlay = $("<canvas></canvas>")
169
+ .addClass ("overlay")
170
+ .appendTo (this .left);
171
+
172
+ electron .ipcRenderer .on ("activate", (event, value) => this .activate (value));
173
+ electron .ipcRenderer .on ("route-graph", (event, key, ... args) => this [key] (... args));
174
+ electron .ipcRenderer .on ("context-menu-will-close", (event, id) => this .closeContextMenu (id));
175
+
176
+ this .setup ();
177
+ }
178
+
179
+ configure ()
180
+ {
181
+ super .configure ();
182
+
183
+ this .config .file .setDefaultValues ({
184
+ activePage: 0,
185
+ });
186
+
187
+ this .activate (true);
188
+ this .restorePages ();
189
+ this .updatePages ();
190
+ this .resizeCanvas ();
191
+
192
+ // Must be last call, because is checked in requestSavePages.
193
+ this .browser .currentScene .sceneGraph_changed .addInterest ("requestSavePages", this);
194
+ }
195
+
196
+ activate (active)
197
+ {
198
+ if (active)
199
+ {
200
+ this .setSnapToGrid (this .config .global .snapToGrid);
201
+ this .setAddConnectedNodes (this .config .global .addConnectedNodes);
202
+ }
203
+ }
204
+
205
+ colorScheme (/* shouldUseDarkColors */)
206
+ {
207
+ this .requestUpdateCanvas ();
208
+ }
209
+
210
+ #menu;
211
+ #menuId;
212
+
213
+ showContextMenu (event, id)
214
+ {
215
+ event .preventDefault ();
216
+ event .stopPropagation ();
217
+
218
+ const element = this .nodes .find (`.node[node-id=${id}]`);
219
+
220
+ if (element .length && !element .is (".selected"))
221
+ this .setNodeSelection (this .getNode (element .data ("node-id")));
222
+
223
+ const menu = [
224
+ {
225
+ label: _("New Page"),
226
+ args: ["addPage"],
227
+ },
228
+ { type: "separator" },
229
+ {
230
+ label: _("Snap to Grid"),
231
+ type: "checkbox",
232
+ checked: this .config .global .snapToGrid,
233
+ args: ["setSnapToGrid", !this .config .global .snapToGrid],
234
+ },
235
+ { type: "separator" },
236
+ {
237
+ label: _("Add Connected Nodes"),
238
+ type: "checkbox",
239
+ checked: this .config .global .addConnectedNodes,
240
+ args: ["setAddConnectedNodes", !this .config .global .addConnectedNodes],
241
+ },
242
+ {
243
+ label: _("Find Node"),
244
+ enabled: !! id,
245
+ args: ["findNode", id, false],
246
+ },
247
+ {
248
+ label: _("Select All Nodes"),
249
+ accelerator: "CmdOrCtrl+A",
250
+ args: ["selectAllNodes"],
251
+ },
252
+ {
253
+ label: _("Deselect All Nodes"),
254
+ accelerator: "Shift+CmdOrCtrl+A",
255
+ args: ["clearNodeSelection"],
256
+ },
257
+ {
258
+ label: _("Remove Selected Nodes"),
259
+ accelerator: "CmdOrCtrl+Backspace",
260
+ enabled: !! this .#selectedNodes .size,
261
+ args: ["removeSelectedNodes"],
262
+ },
263
+ { type: "separator" },
264
+ {
265
+ label: _("Select All Routes"),
266
+ args: ["selectAllRoutes"],
267
+ },
268
+ {
269
+ label: _("Deselect All Routes"),
270
+ args: ["clearRouteSelection"],
271
+ },
272
+ {
273
+ label: _("Delete Selected Routes"),
274
+ enabled: !! this .#selectedRoutes .size,
275
+ args: ["deleteSelectedRoutes"],
276
+ },
277
+ ];
278
+
279
+ this .#menu = true;
280
+ this .#menuId = Math .random ();
281
+
282
+ electron .ipcRenderer .send ("context-menu", "route-graph", menu, this .#menuId);
283
+ }
284
+
285
+ closeContextMenu (id)
286
+ {
287
+ if (id === this .#menuId)
288
+ this .#menu = false;
289
+ }
290
+
291
+ setSnapToGrid (snapToGrid)
292
+ {
293
+ this .config .global .snapToGrid = snapToGrid;
294
+
295
+ if (snapToGrid)
296
+ this .snapToGridButton .addClass ("active");
297
+ else
298
+ this .snapToGridButton .removeClass ("active");
299
+ }
300
+
301
+ setAddConnectedNodes (addConnectedNodes)
302
+ {
303
+ this .config .global .addConnectedNodes = addConnectedNodes;
304
+
305
+ if (addConnectedNodes)
306
+ this .addConnectedNodesButton .addClass ("active");
307
+ else
308
+ this .addConnectedNodesButton .removeClass ("active");
309
+ }
310
+
311
+ updatePages ()
312
+ {
313
+ const
314
+ active = this .config .file .activePage,
315
+ pages = this .pages;
316
+
317
+ if (!pages .length)
318
+ return this .addPage ();
319
+
320
+ this .top .find ("> div") .remove ();
321
+ this .tabs .empty ();
322
+
323
+ for (const [index, { title }] of pages .entries ())
324
+ {
325
+ // Add tab.
326
+ $("<li></li>")
327
+ .data ("index", index)
328
+ .on ("click", () => this .top .tabs ("option", "active", index))
329
+ .append ($("<a></a>")
330
+ .addClass ("text")
331
+ .attr ("href", `#routing-page-${index}-tab`)
332
+ .attr ("title", title)
333
+ .text (title))
334
+ .append ($("<span></span>")
335
+ .addClass (["material-icons", "button"])
336
+ .text ("close")
337
+ .on ("click", () => this .closePage (index)))
338
+ .appendTo (this .tabs);
339
+
340
+ // Add hidden empty panel.
341
+ $("<div></div>")
342
+ .attr ("id", `routing-page-${index}-tab`)
343
+ .appendTo (this .top);
344
+ }
345
+
346
+ this .top .tabs ();
347
+ this .top .tabs ("option", "classes.ui-tabs", "top");
348
+ this .top .tabs ("refresh");
349
+
350
+ if (this .top .tabs ("option", "active") === active)
351
+ this .activatePage ();
352
+ else
353
+ this .top .tabs ("option", "active", active);
354
+
355
+ this .requestSavePages ();
356
+ }
357
+
358
+ reorderPages (item)
359
+ {
360
+ const
361
+ current = item .data ("index"),
362
+ indices = Array .from (this .tabs .find ("> li"), li => $(li) .data ("index"));
363
+
364
+ const
365
+ pages = this .pages,
366
+ reorderedPages = indices .map (i => pages [i]);
367
+
368
+ this .pages = reorderedPages;
369
+
370
+ if (current < this .config .file .activePage)
371
+ {
372
+ if (indices .indexOf (current) >= this .config .file .activePage)
373
+ -- this .config .file .activePage;
374
+
375
+ }
376
+ else if (current > this .config .file .activePage)
377
+ {
378
+ if (indices .indexOf (current) <= this .config .file .activePage)
379
+ ++ this .config .file .activePage;
380
+ }
381
+ else
382
+ {
383
+ this .config .file .activePage = indices .indexOf (current);
384
+ }
385
+
386
+ this .updatePages ();
387
+ }
388
+
389
+ #savePagesId;
390
+
391
+ requestSavePages ()
392
+ {
393
+ if (!this .browser .currentScene .sceneGraph_changed .hasInterest ("requestSavePages", this))
394
+ return;
395
+
396
+ clearTimeout (this .#savePagesId);
397
+
398
+ this .#savePagesId = setTimeout (() => this .savePages ());
399
+ }
400
+
401
+ savePages ()
402
+ {
403
+ const
404
+ pages = structuredClone (this .pages),
405
+ ids = new Set ();
406
+
407
+ for (const page of pages)
408
+ {
409
+ for (const node of page .nodes)
410
+ ids .add (node .id);
411
+ }
412
+
413
+ if (ids .size)
414
+ {
415
+ const paths = this .getPathsFromScene (this .browser .currentScene, ids);
416
+
417
+ for (const page of pages)
418
+ {
419
+ delete page .selectedNodes;
420
+ delete page .selectedRoutes;
421
+
422
+ for (const node of page .nodes)
423
+ {
424
+ node .path = paths .get (node .id) ?? "";
425
+
426
+ delete node .id;
427
+ }
428
+ }
429
+
430
+ const
431
+ string = JSON .stringify (pages),
432
+ readConfig = Editor .getConfigNode (this .browser .currentScene);
433
+
434
+ if (readConfig ?.getMetaData ("Sunrize/RouteGraph/pages") .toString () !== string)
435
+ {
436
+ const configNode = readConfig ?? Editor .getConfigNode (this .browser .currentScene, true);
437
+
438
+ configNode .setMetaData ("Sunrize/RouteGraph/pages", string);
439
+
440
+ UndoManager .shared .saveNeeded = true;
441
+ }
442
+ }
443
+ else
444
+ {
445
+ const configNode = Editor .getConfigNode (this .browser .currentScene);
446
+
447
+ if (configNode ?.hasMetaData ("Sunrize/RouteGraph/pages"))
448
+ {
449
+ configNode .removeMetaData ("Sunrize/RouteGraph/pages");
450
+
451
+ UndoManager .shared .saveNeeded = true;
452
+ }
453
+ }
454
+ }
455
+
456
+ getPathsFromScene (scene, ids, path = [ ], paths = new Map (), seen = new Set ())
457
+ {
458
+ // protos
459
+
460
+ path .push ("protos");
461
+
462
+ for (const proto of scene .protos)
463
+ {
464
+ path .push (proto .name);
465
+
466
+ // fields
467
+
468
+ path .push ("fields");
469
+
470
+ this .getPathsFromNode (proto, true, ids, path, paths, seen);
471
+
472
+ path .pop ();
473
+
474
+ // body
475
+
476
+ this .getPathsFromScene (proto .getBody (), ids, path, paths, seen);
477
+
478
+ path .pop ();
479
+ }
480
+
481
+ path .pop ();
482
+
483
+ // rootNodes
484
+
485
+ path .push ("rootNodes");
486
+
487
+ this .getPathsFromNodes (scene .rootNodes, ids, path, paths, seen);
488
+
489
+ path .pop ();
490
+
491
+ // importedNodes
492
+
493
+ path .push ("importedNodes");
494
+
495
+ for (const [i, importedNode] of scene .importedNodes .entries ())
496
+ {
497
+ path .push (i);
498
+
499
+ this .getPathsFromNode (importedNode .getExportedNode (), false, ids, path, paths, seen);
500
+
501
+ path .pop ();
502
+ }
503
+
504
+ path .pop ();
505
+
506
+ return paths;
507
+ }
508
+
509
+ getPathsFromNodes (nodes, ids, path, paths, seen)
510
+ {
511
+ for (const [i, node] of nodes .entries ())
512
+ {
513
+ path .push (i);
514
+
515
+ this .getPathsFromNode (node ?.getValue (), true, ids, path, paths, seen);
516
+
517
+ path .pop ();
518
+ }
519
+ }
520
+
521
+ getPathsFromNode (node, fields, ids, path, paths, seen)
522
+ {
523
+ if (!node)
524
+ return;
525
+
526
+ if (seen .has (node))
527
+ return;
528
+
529
+ seen .add (node);
530
+
531
+ if (ids .has (node .getId ()))
532
+ paths .set (node .getId (), path .join (","));
533
+
534
+ if (!fields)
535
+ return;
536
+
537
+ for (const field of node .getFields ())
538
+ {
539
+ switch (field .getType ())
540
+ {
541
+ case X3D .X3DConstants .SFNode:
542
+ {
543
+ path .push (field .getName ());
544
+
545
+ this .getPathsFromNode (field .getValue (), true, ids, path, paths, seen);
546
+
547
+ path .pop ();
548
+ break;
549
+ }
550
+ case X3D .X3DConstants .MFNode:
551
+ {
552
+ path .push (field .getName ());
553
+
554
+ this .getPathsFromNodes (field, ids, path, paths, seen);
555
+
556
+ path .pop ();
557
+ break;
558
+ }
559
+ }
560
+ }
561
+ }
562
+
563
+ restorePages ()
564
+ {
565
+ try
566
+ {
567
+ const
568
+ configNode = Editor .getConfigNode (this .browser .currentScene),
569
+ pages = JSON .parse (configNode ?.getMetaData ("Sunrize/RouteGraph/pages")),
570
+ paths = new Set ();
571
+
572
+ for (const page of pages)
573
+ {
574
+ for (const node of page .nodes)
575
+ paths .add (node .path);
576
+ }
577
+
578
+ if (paths .size)
579
+ {
580
+ const ids = this .getIdsFromScene (this .browser .currentScene, paths);
581
+
582
+ for (const page of pages)
583
+ {
584
+ for (const node of page .nodes)
585
+ node .id = ids .get (node .path);
586
+
587
+ page .nodes = page .nodes .filter (node => node .id !== undefined);
588
+
589
+ page .scale ??= 1;
590
+ }
591
+
592
+ this .pages = pages;
593
+ }
594
+ else
595
+ {
596
+ this .pages = [ ];
597
+ }
598
+ }
599
+ catch
600
+ {
601
+ this .pages = [ ];
602
+ }
603
+ }
604
+
605
+ getIdsFromScene (scene, paths, path = [ ], ids = new Map (), seen = new Set ())
606
+ {
607
+ // protos
608
+
609
+ path .push ("protos");
610
+
611
+ for (const proto of scene .protos)
612
+ {
613
+ path .push (proto .name);
614
+
615
+ // fields
616
+
617
+ path .push ("fields");
618
+
619
+ this .getIdsFromNode (proto, true, paths, path, ids, seen);
620
+
621
+ path .pop ();
622
+
623
+ // body
624
+
625
+ this .getIdsFromScene (proto .getBody (), paths, path, ids, seen);
626
+
627
+ path .pop ();
628
+ }
629
+
630
+ path .pop ();
631
+
632
+ // rootNodes
633
+
634
+ path .push ("rootNodes");
635
+
636
+ this .getIdsFromNodes (scene .rootNodes, paths, path, ids, seen);
637
+
638
+ path .pop ();
639
+
640
+ // importedNodes
641
+
642
+ path .push ("importedNodes");
643
+
644
+ for (const [i, importedNode] of scene .importedNodes .entries ())
645
+ {
646
+ path .push (i);
647
+
648
+ this .getIdsFromNode (importedNode .getExportedNode (), false, paths, path, ids, seen);
649
+
650
+ path .pop ();
651
+ }
652
+
653
+ path .pop ();
654
+
655
+ return ids;
656
+ }
657
+
658
+ getIdsFromNodes (nodes, paths, path, ids, seen)
659
+ {
660
+ for (const [i, node] of nodes .entries ())
661
+ {
662
+ path .push (i);
663
+
664
+ this .getIdsFromNode (node ?.getValue (), true, paths, path, ids, seen);
665
+
666
+ path .pop ();
667
+ }
668
+ }
669
+
670
+ getIdsFromNode (node, fields, paths, path, ids, seen)
671
+ {
672
+ if (!node)
673
+ return;
674
+
675
+ if (seen .has (node))
676
+ return;
677
+
678
+ seen .add (node);
679
+
680
+ if (paths .has (path .join (",")))
681
+ ids .set (path .join (","), node .getId ());
682
+
683
+ this .setNode (node);
684
+
685
+ if (!fields)
686
+ return;
687
+
688
+ for (const field of node .getFields ())
689
+ {
690
+ switch (field .getType ())
691
+ {
692
+ case X3D .X3DConstants .SFNode:
693
+ {
694
+ path .push (field .getName ());
695
+
696
+ this .getIdsFromNode (field .getValue (), true, paths, path, ids, seen);
697
+
698
+ path .pop ();
699
+ break;
700
+ }
701
+ case X3D .X3DConstants .MFNode:
702
+ {
703
+ path .push (field .getName ());
704
+
705
+ this .getIdsFromNodes (field, paths, path, ids, seen);
706
+
707
+ path .pop ();
708
+ break;
709
+ }
710
+ }
711
+ }
712
+ }
713
+
714
+ savePage (page)
715
+ {
716
+ page .selectedNodes = Array .from (this .#selectedNodes);
717
+ page .selectedRoutes = Array .from (this .#selectedRoutes);
718
+ }
719
+
720
+ restorePage ()
721
+ {
722
+ const page = this .page;
723
+
724
+ page .nodes = page .nodes .filter (node => this .getNode (node .id) .isLive ());
725
+
726
+ this .clearNodeSelection ();
727
+ this .clearRouteSelection ();
728
+ this .clearInputOutput ();
729
+
730
+ const currentNodes = Array .from (this .nodes .find (".node"), element => this .getNode ($(element) .data ("node-id")));
731
+
732
+ for (const node of currentNodes)
733
+ this .removeNodeElement (node);
734
+
735
+ for (const node of page .nodes)
736
+ this .addNodeElement (this .getNode (node .id), node);
737
+
738
+ // Restore selections.
739
+
740
+ const ids = page .nodes .map (node => node .id);
741
+
742
+ for (const node of page .selectedNodes ?? [ ])
743
+ {
744
+ if (ids .includes (node .getId ()))
745
+ this .addNodeSelection (node);
746
+ }
747
+
748
+ for (const route of page .selectedRoutes ?? [ ])
749
+ {
750
+ if (route .getExecutionContext () .routes .includes (route))
751
+ this .addRouteSelection (route);
752
+ }
753
+
754
+ // Restore scroll position.
755
+
756
+ const { scrollLeft = 0, scrollTop = 0 } = page;
757
+
758
+ this .nodes .scrollLeft (scrollLeft);
759
+ this .nodes .scrollTop (scrollTop);
760
+
761
+ // Wait until page is drawn and try again.
762
+
763
+ setTimeout (() =>
764
+ {
765
+ this .nodes .scrollLeft (scrollLeft);
766
+ this .nodes .scrollTop (scrollTop);
767
+ });
768
+ }
769
+
770
+ addPage ()
771
+ {
772
+ const
773
+ pages = this .pages,
774
+ next = pages .reduce ((i, page) => Math .max (i, (page .title .match (/(\d+)\s*$/) ?.[1]|0) + 1), 1);
775
+
776
+ pages .push ({
777
+ title: `${util .format (_("New Logic %d"), next)}`,
778
+ nodes: [ ],
779
+ scale: 1,
780
+ });
781
+
782
+ this .updatePages ();
783
+
784
+ this .top .tabs ("option", "active", pages .length - 1);
785
+ }
786
+
787
+ closePage (index)
788
+ {
789
+ const active = this .top .tabs ("option", "active");
790
+
791
+ if (active === this .config .file .activePage)
792
+ this .config .file .activePage = -1;
793
+
794
+ this .pages .splice (index, 1);
795
+
796
+ this .updatePages ();
797
+ }
798
+
799
+ activatePage ()
800
+ {
801
+ const
802
+ active = this .top .tabs ("option", "active"),
803
+ pages = this .pages,
804
+ page = pages [active],
805
+ current = pages [this .config .file .activePage];
806
+
807
+ if (current)
808
+ this .savePage (current);
809
+
810
+ this .config .file .activePage = active;
811
+
812
+ this .title .val (page .title);
813
+
814
+ this .updateTitle ();
815
+ this .restorePage ();
816
+ this .requestUpdateCanvas ();
817
+ }
818
+
819
+ updateTitle ()
820
+ {
821
+ const
822
+ active = this .config .file .activePage,
823
+ pages = this .pages,
824
+ title = this .title .val () || _("New Logic");
825
+
826
+ $(`a[href="#routing-page-${active}-tab"]`)
827
+ .attr ("title", title)
828
+ .text (title);
829
+
830
+ pages [active] .title = title;
831
+
832
+ this .requestSavePages ();
833
+ }
834
+
835
+ scrollPage ()
836
+ {
837
+ const page = this .page;
838
+
839
+ page .scrollLeft = this .nodes .scrollLeft ();
840
+ page .scrollTop = this .nodes .scrollTop ();
841
+
842
+ this .updateCanvas ();
843
+ }
844
+
845
+ scrollPageEnd ()
846
+ {
847
+ this .requestSavePages ();
848
+ }
849
+
850
+ scalePageWithWheel (event)
851
+ {
852
+ if (!(event .metaKey && event .ctrlKey))
853
+ return;
854
+
855
+ event .preventDefault ();
856
+ event .stopPropagation ();
857
+
858
+ const deltaY = event .originalEvent .deltaY;
859
+
860
+ this .scalePage (deltaY > 0 ? this .page .scale * 0.98 : this .page .scale * 1.02);
861
+ }
862
+
863
+ scalePage (scale, reset)
864
+ {
865
+ const
866
+ page = this .page,
867
+ currentScale = reset ? 1 : page .scale;
868
+
869
+ scale = X3D .Algorithm .clamp (scale, 0.2, 2);
870
+
871
+ // Determine translation to scale from minX and minY.
872
+
873
+ let originX = Infinity, originY = Infinity;
874
+
875
+ for (const node of page .nodes)
876
+ {
877
+ originX = Math .min (originX, node .x);
878
+ originY = Math .min (originY, node .y);
879
+ }
880
+
881
+ // Update node transform.
882
+
883
+ for (const node of page .nodes)
884
+ {
885
+ const
886
+ left = (node .x - originX) * scale + originX * currentScale,
887
+ top = (node .y - originY) * scale + originY * currentScale;
888
+
889
+ this .nodes .find (`.node[node-id=${node .id}]`)
890
+ .css ("left", left)
891
+ .css ("top", top)
892
+ .css ("scale", scale);
893
+
894
+ node .x = left / scale;
895
+ node .y = top / scale;
896
+ }
897
+
898
+ page .scale = scale;
899
+
900
+ this .requestSavePages ();
901
+ this .requestUpdateCanvas ();
902
+ }
903
+
904
+ zoomOut ()
905
+ {
906
+ this .scalePage (this .page .scale * 0.9);
907
+ }
908
+
909
+ zoomIn ()
910
+ {
911
+ this .scalePage (this .page .scale * 1.1);
912
+ }
913
+
914
+ zoomFit ()
915
+ {
916
+ const
917
+ page = this .page,
918
+ padLeft = this .#gridSize * 2,
919
+ padTop = this .#gridSize * 3,
920
+ padRight = this .#gridSize * 2,
921
+ padBottom = this .#gridSize * 2;
922
+
923
+ // Adjust scroll positions.
924
+
925
+ page .scrollLeft = 0;
926
+ page .scrollTop = 0;
927
+
928
+ this .nodes .scrollLeft (0);
929
+ this .nodes .scrollTop (0);
930
+
931
+ if (page .nodes .length)
932
+ {
933
+ // Determine bounding box of all nodes.
934
+
935
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
936
+
937
+ for (const node of page .nodes)
938
+ {
939
+ const element = this .nodes .find (`.node[node-id=${node .id}]`);
940
+
941
+ minX = Math .min (minX, node .x);
942
+ minY = Math .min (minY, node .y);
943
+ maxX = Math .max (maxX, node .x + element .width ());
944
+ maxY = Math .max (maxY, node .y + element .height ());
945
+ }
946
+
947
+ // Reposition nodes.
948
+
949
+ const
950
+ translationX = padLeft - minX,
951
+ translationY = padTop - minY;
952
+
953
+ for (const node of page .nodes)
954
+ {
955
+ node .x += translationX;
956
+ node .y += translationY;
957
+ }
958
+
959
+ // Scale nodes.
960
+
961
+ const
962
+ scaleX = (this .nodes .innerWidth () - (padLeft + padRight)) / (maxX - minX),
963
+ scaleY = (this .nodes .innerHeight () - (padTop + padBottom)) / (maxY - minY),
964
+ scale = Math .min (scaleX, scaleY);
965
+
966
+ this .scalePage (scale, true);
967
+ }
968
+ else
969
+ {
970
+ this .scalePage (1);
971
+ }
972
+ }
973
+
974
+ zoom100 ()
975
+ {
976
+ this .scalePage (1);
977
+ }
978
+
979
+ keyDown (event)
980
+ {
981
+ // console .log (event .key);
982
+
983
+ if (document .activeElement !== this .nodes [0])
984
+ return;
985
+
986
+ switch (event .key)
987
+ {
988
+ case "-":
989
+ {
990
+ event .preventDefault ();
991
+ event .stopPropagation ();
992
+
993
+ this .zoomOut ();
994
+ break;
995
+ }
996
+ case "+":
997
+ {
998
+ event .preventDefault ();
999
+ event .stopPropagation ();
1000
+
1001
+ this .zoomIn ();
1002
+ break;
1003
+ }
1004
+ case "0":
1005
+ {
1006
+ event .preventDefault ();
1007
+ event .stopPropagation ();
1008
+
1009
+ this .zoomFit ();
1010
+ break;
1011
+ }
1012
+ case "1":
1013
+ {
1014
+ event .preventDefault ();
1015
+ event .stopPropagation ();
1016
+
1017
+ this .zoom100 ();
1018
+ break;
1019
+ }
1020
+ case "a":
1021
+ {
1022
+ if (event .metaKey || event .ctrlKey)
1023
+ {
1024
+ event .preventDefault ();
1025
+ event .stopPropagation ();
1026
+
1027
+ if (event .shiftKey)
1028
+ this .clearNodeSelection ();
1029
+ else
1030
+ this .selectAllNodes ();
1031
+ }
1032
+
1033
+ break;
1034
+ }
1035
+ case "Backspace":
1036
+ {
1037
+ if (event .metaKey || event .ctrlKey)
1038
+ {
1039
+ event .preventDefault ();
1040
+ event .stopPropagation ();
1041
+
1042
+ this .removeSelectedNodes ();
1043
+ }
1044
+
1045
+ break;
1046
+ }
1047
+ }
1048
+ }
1049
+
1050
+ getNode (id)
1051
+ {
1052
+ return this .outlineEditor .objects .get (id);
1053
+ }
1054
+
1055
+ setNode (node)
1056
+ {
1057
+ this .outlineEditor .objects .set (node .getId (), node .valueOf ());
1058
+ }
1059
+
1060
+ addNode (node, { x, y })
1061
+ {
1062
+ const
1063
+ page = this .page,
1064
+ nodes = page .nodes,
1065
+ id = node .getId ();
1066
+
1067
+ this .setNode (node);
1068
+
1069
+ if (nodes .find (node => node .id === id))
1070
+ return;
1071
+
1072
+ if (this .config .global .snapToGrid)
1073
+ {
1074
+ x = this .round (x, this .#gridSize);
1075
+ y = this .round (y, this .#gridSize);
1076
+ }
1077
+
1078
+ nodes .push ({ id, x, y });
1079
+
1080
+ this .addNodeElement (node, { x, y });
1081
+ this .requestSavePages ();
1082
+ this .requestUpdateCanvas ();
1083
+ }
1084
+
1085
+ #gaps = new X3D .Vector2 (5, 2); // In grid units.
1086
+
1087
+ addConnectedNodes (node, { x, y })
1088
+ {
1089
+ const
1090
+ scale = this .page .scale,
1091
+ columns = this .getConnectedNodes (node .getExecutionContext (), node, 0);
1092
+
1093
+ x /= scale;
1094
+ y /= scale;
1095
+
1096
+ for (const index of Array .from (columns .keys ()) .sort ((a, b) => a - b))
1097
+ {
1098
+ const column = columns .get (index);
1099
+
1100
+ let
1101
+ offsetX = 0,
1102
+ offsetY = y;
1103
+
1104
+ for (const node of column)
1105
+ {
1106
+ this .addNode (node, { x, y: offsetY });
1107
+
1108
+ const element = this .nodes .find (`.node[node-id=${node .getId ()}]`);
1109
+
1110
+ x = parseInt (element .css ("left")) / scale;
1111
+ offsetX = Math .max (offsetX, element .width ());
1112
+ offsetY += element .height () + this .#gridSize * this .#gaps .y;
1113
+ }
1114
+
1115
+ x += offsetX + this .#gridSize * this .#gaps .x;
1116
+ }
1117
+ }
1118
+
1119
+ getConnectedNodes (executionContext, node, index, columns = new Map (), nodes = new Set ())
1120
+ {
1121
+ if (nodes .has (node))
1122
+ return;
1123
+
1124
+ if (node .getExecutionContext () !== executionContext)
1125
+ return;
1126
+
1127
+ this .setNode (node);
1128
+
1129
+ nodes .add (node);
1130
+ columns .getOrInsert (index, [ ]) .push (node);
1131
+
1132
+ for (const field of node .getFields ())
1133
+ {
1134
+ for (const route of field .getInputRoutes ())
1135
+ this .getConnectedNodes (executionContext, route .getSourceNode (), index - 1, columns, nodes);
1136
+
1137
+ for (const route of field .getOutputRoutes ())
1138
+ this .getConnectedNodes (executionContext, route .getDestinationNode (), index + 1, columns, nodes);
1139
+ }
1140
+
1141
+ return columns;
1142
+ }
1143
+
1144
+ addNodeElement (node, { x, y }, selected)
1145
+ {
1146
+ const
1147
+ id = node .getId (),
1148
+ scale = this .page .scale;
1149
+
1150
+ node .getLive () .addInterest ("nodeLive", this, id);
1151
+
1152
+ node .name_changed .addInterest ("updateNodeHeader", this, id);
1153
+ node .typeName_changed .addInterest ("updateNodeHeader", this, id);
1154
+
1155
+ node .getPredefinedFields () .addInterest ("updateNodeElement", this, id);
1156
+ node .getUserDefinedFields () .addInterest ("updateNodeElement", this, id);
1157
+
1158
+ if (node instanceof X3D .X3DImportedNodeProxy)
1159
+ {
1160
+ // These events are connected and must never be removed.
1161
+ node .getImportedNode () .getInlineNode () .getLoadState () .addInterest ("updateNodeElement", this, id);
1162
+ node .getExecutionContext () .importedNodes .addInterest ("updateImportedNodes", this);
1163
+ }
1164
+
1165
+ const element = $("<div></div>")
1166
+ .draggable ()
1167
+ .data ("node-id", id)
1168
+ .attr ("node-id", id)
1169
+ .attr ("execution-context-id", node .getExecutionContext () .getId ())
1170
+ .css ("position", "")
1171
+ .css ("left", x * scale)
1172
+ .css ("top", y * scale)
1173
+ .css ("scale", scale)
1174
+ .addClass ("node")
1175
+ .on ("mousedown", () => this .raiseNode (id))
1176
+ .on ("mouseup", event => this .selectNode (event, id))
1177
+ .on ("dragstart", () => this .moveNodeStart (id))
1178
+ .on ("drag", (event, ui) => this .moveNode (id, ui .position))
1179
+ .on ("contextmenu", event => this .showContextMenu (event, id));
1180
+
1181
+ if (selected)
1182
+ element .addClass ("selected");
1183
+
1184
+ if (node instanceof X3D .X3DImportedNodeProxy)
1185
+ element .addClass ("imported-node");
1186
+
1187
+ const header = $("<div></div>")
1188
+ .addClass ("header")
1189
+ .appendTo (element);
1190
+
1191
+ const icon = $("<img>")
1192
+ .addClass ("icon")
1193
+ .appendTo (header);
1194
+
1195
+ if (node instanceof X3D .X3DImportedNodeProxy)
1196
+ icon .attr ("src", "../images/OutlineEditor/Node/ImportedNode.svg");
1197
+ else
1198
+ icon .attr ("src", "../images/OutlineEditor/Node/X3DBaseNode.svg");
1199
+
1200
+ const title = $("<div></div>")
1201
+ .addClass ("title")
1202
+ .appendTo (header);
1203
+
1204
+ $("<span></span>")
1205
+ .addClass ("name")
1206
+ .text (node .getDisplayName () || _("<unnamed>"))
1207
+ .appendTo (title);
1208
+
1209
+ $("<span></span>")
1210
+ .addClass ("type-name")
1211
+ .text (node .getTypeName ())
1212
+ .appendTo (title);
1213
+
1214
+ $("<span></span>")
1215
+ .addClass (["material-symbols-outlined", "button", "arrow"])
1216
+ .text ("left_click")
1217
+ .attr ("title", _("Find node."))
1218
+ .on ("mouseup", event =>
1219
+ {
1220
+ if (event .button !== 0)
1221
+ return;
1222
+
1223
+ this .findNode (id, event .shiftKey);
1224
+ })
1225
+ .appendTo (header);
1226
+
1227
+ const fieldsElement = $("<ul></ul>")
1228
+ .addClass ("fields")
1229
+ .appendTo (element);
1230
+
1231
+ for (const [i, fields] of [node .getPredefinedFields (), node .getUserDefinedFields () .toReversed ()] .entries ())
1232
+ {
1233
+ const first = fieldsElement .find ("> :first-child");
1234
+
1235
+ for (const field of fields)
1236
+ {
1237
+ if (field .getAccessType () === X3D .X3DConstants .initializeOnly)
1238
+ continue;
1239
+
1240
+ field .addRouteCallback (this, () => this .requestUpdateCanvas ());
1241
+
1242
+ const fieldElement = $("<li></li>")
1243
+ .attr ("name", field .getName ())
1244
+ .attr ("type-name", field .getTypeName ())
1245
+ .addClass ("field");
1246
+
1247
+ if (i)
1248
+ first .after (fieldElement .addClass ("user-defined"));
1249
+ else
1250
+ fieldElement .appendTo (fieldsElement);
1251
+
1252
+ if (field .isInput ())
1253
+ {
1254
+ $("<div></div>")
1255
+ .addClass ("input")
1256
+ .on ("mousedown", false)
1257
+ .on ("mouseup", event => this .selectInput (event, id, field .getName ()))
1258
+ .appendTo (fieldElement);
1259
+ }
1260
+
1261
+ $("<img>")
1262
+ .addClass ("icon")
1263
+ .attr ("src", `../images/OutlineEditor/Fields/${field .getTypeName ()}.svg`)
1264
+ .appendTo (fieldElement);
1265
+
1266
+ $("<span></span>")
1267
+ .addClass ("name")
1268
+ .text (field .getName ())
1269
+ .appendTo (fieldElement);
1270
+
1271
+ if (field .isOutput ())
1272
+ {
1273
+ $("<div></div>")
1274
+ .addClass ("output")
1275
+ .on ("mousedown", false)
1276
+ .on ("mouseup", event => this .selectOutput (event, id, field .getName ()))
1277
+ .appendTo (fieldElement);
1278
+ }
1279
+ }
1280
+ }
1281
+
1282
+ $("<div></div>")
1283
+ .addClass ("footer")
1284
+ .appendTo (element);
1285
+
1286
+ this .nodes .append (element);
1287
+ }
1288
+
1289
+ removeNode (id)
1290
+ {
1291
+ const
1292
+ active = this .config .file .activePage,
1293
+ pages = this .pages,
1294
+ page = pages [active],
1295
+ node = this .getNode (id);
1296
+
1297
+ page .nodes = page .nodes .filter (node => node .id !== id);
1298
+
1299
+ this .removeNodeElement (node);
1300
+ this .requestSavePages ();
1301
+ this .requestUpdateCanvas ();
1302
+ }
1303
+
1304
+ removeNodeElement (node)
1305
+ {
1306
+ const id = node .getId ();
1307
+
1308
+ node .getLive () .removeInterest ("nodeLive", this);
1309
+
1310
+ node .name_changed .removeInterest ("updateNodeHeader", this);
1311
+ node .typeName_changed .removeInterest ("updateNodeHeader", this);
1312
+
1313
+ node .getPredefinedFields () .removeInterest ("updateNodeElement", this);
1314
+ node .getUserDefinedFields () .removeInterest ("updateNodeElement", this);
1315
+
1316
+ this .nodes .find (`.node[node-id=${id}]`) .remove ();
1317
+
1318
+ for (const field of node .getFields ())
1319
+ field .removeRouteCallback (this);
1320
+
1321
+ // Clear selections.
1322
+
1323
+ this .removeNodeSelection (node);
1324
+
1325
+ for (const field of node .getFields ())
1326
+ {
1327
+ for (const route of field .getInputRoutes ())
1328
+ this .clearRouteSelection (route);
1329
+
1330
+ for (const route of field .getOutputRoutes ())
1331
+ this .clearRouteSelection (route);
1332
+ }
1333
+ }
1334
+
1335
+ updateNodeElement (id)
1336
+ {
1337
+ const element = this .nodes .find (`.node[node-id=${id}]`);
1338
+
1339
+ if (!element .length)
1340
+ return;
1341
+
1342
+ const
1343
+ page = this .page,
1344
+ node = this .getNode (id),
1345
+ position = page .nodes .find (node => node .id === id),
1346
+ selected = this .isNodeSelected (node);
1347
+
1348
+ this .removeNodeElement (node);
1349
+ this .addNodeElement (node, position, selected);
1350
+ this .requestUpdateCanvas ();
1351
+ }
1352
+
1353
+ updateNodeHeader (id)
1354
+ {
1355
+ const element = this .nodes .find (`.node[node-id=${id}]`);
1356
+
1357
+ if (!element .length)
1358
+ return;
1359
+
1360
+ const node = this .getNode (id);
1361
+
1362
+ element .find (".header .name") .text (node .getDisplayName () || _("<unnamed>"));
1363
+ element .find (".header .type-name") .text (node .getTypeName ());
1364
+ }
1365
+
1366
+ raiseNode (id)
1367
+ {
1368
+ this .nodes .find (`.node[node-id=${id}]`) .appendTo (this .nodes);
1369
+ }
1370
+
1371
+ #movingNode = false;
1372
+
1373
+ moveNodeStart (id)
1374
+ {
1375
+ const node = this .getNode (id);
1376
+
1377
+ this .#movingNode = true;
1378
+
1379
+ $(document) .on ("mouseup.move-node", () => this .moveNodeEnd ());
1380
+
1381
+ if (!this .isNodeSelected (node))
1382
+ this .setNodeSelection (node);
1383
+
1384
+ this .clearInputOutput ();
1385
+ }
1386
+
1387
+ moveNode (id, position)
1388
+ {
1389
+ const
1390
+ page = this .page,
1391
+ scale = page .scale,
1392
+ node = page .nodes .find (node => node .id === id);
1393
+
1394
+ // Constrain position.
1395
+
1396
+ position .left = Math .max (position .left, 0);
1397
+ position .top = Math .max (position .top, 0);
1398
+
1399
+ if (this .config .global .snapToGrid)
1400
+ {
1401
+ position .left = this .round (position .left, this .#gridSize);
1402
+ position .top = this .round (position .top, this .#gridSize);
1403
+ }
1404
+
1405
+ node .x = position .left / scale;
1406
+ node .y = position .top / scale;
1407
+
1408
+ // Move selected nodes.
1409
+
1410
+ const
1411
+ element = this .nodes .find (`.node[node-id=${id}]`),
1412
+ others = this .nodes .find (`.node.selected:not([node-id=${id}])`);
1413
+
1414
+ const
1415
+ deltaX = position .left - parseInt (element .css ("left")),
1416
+ deltaY = position .top - parseInt (element .css ("top"));
1417
+
1418
+ for (const other of others)
1419
+ {
1420
+ const
1421
+ element = $(other),
1422
+ id = element .data ("node-id"),
1423
+ left = parseInt (element .css ("left")) + deltaX,
1424
+ top = parseInt (element .css ("top")) + deltaY;
1425
+
1426
+ element .css ("left", left);
1427
+ element .css ("top", top);
1428
+
1429
+ const node = page .nodes .find (node => node .id === id);
1430
+
1431
+ node .x = left / scale;
1432
+ node .y = top / scale;
1433
+ }
1434
+
1435
+ this .requestUpdateCanvas ();
1436
+ }
1437
+
1438
+ moveNodeEnd ()
1439
+ {
1440
+ this .#movingNode = false;
1441
+
1442
+ $(document) .off (".move-node");
1443
+
1444
+ this .nodes .trigger ("focus");
1445
+
1446
+ this .requestSavePages ();
1447
+ }
1448
+
1449
+ findNode (id, add)
1450
+ {
1451
+ const
1452
+ node = this .getNode (id),
1453
+ outlineEditor = this .outlineEditor;
1454
+
1455
+ outlineEditor .expandTo (node, { expandObject: true, expandInlineNodes: true, expandAll: true });
1456
+
1457
+ const elements = Array .from (outlineEditor .sceneGraph .find (`.node[node-id=${id}], .imported-node[node-id=${id}]`));
1458
+
1459
+ if (!elements .length)
1460
+ return;
1461
+
1462
+ for (const [i, element] of elements .entries ())
1463
+ {
1464
+ if ($(element) .is (".node"))
1465
+ outlineEditor .selectNodeElement ($(element), { add: add || i > 0, target: true });
1466
+
1467
+ else if ($(element) .is (".imported-node"))
1468
+ outlineEditor .selectPrimaryElement ($(element), { add: add || i > 0, target: true });
1469
+ }
1470
+
1471
+ // Scroll element into view.
1472
+ // Hide scrollbars during scroll to prevent overlay issue.
1473
+
1474
+ outlineEditor .treeView .css ("overflow", "hidden");
1475
+
1476
+ elements [0] ?.scrollIntoView ({ block: "center", inline: "start", behavior: "smooth" });
1477
+ $(window) .scrollTop (0);
1478
+
1479
+ setTimeout (() => outlineEditor .treeView .css ("overflow", ""), 1000);
1480
+ }
1481
+
1482
+ selectNode (event, id)
1483
+ {
1484
+ if (event .button !== 0)
1485
+ return;
1486
+
1487
+ if (this .#lasso || this .#movingNode)
1488
+ return;
1489
+
1490
+ this .nodes .trigger ("focus");
1491
+
1492
+ const node = this .getNode (id);
1493
+
1494
+ if (event .shiftKey)
1495
+ {
1496
+ if (this .isNodeSelected (node))
1497
+ this .removeNodeSelection (node);
1498
+ else
1499
+ this .addNodeSelection (node);
1500
+ }
1501
+ else
1502
+ {
1503
+ this .setNodeSelection (node);
1504
+ }
1505
+ }
1506
+
1507
+ selectAllNodes ()
1508
+ {
1509
+ this .clearNodeSelection ();
1510
+
1511
+ for (const element of this .nodes .find (".node"))
1512
+ this .addNodeSelection (this .getNode ($(element) .data ("node-id")));
1513
+ }
1514
+
1515
+ removeSelectedNodes ()
1516
+ {
1517
+ for (const node of Array .from (this .#selectedNodes))
1518
+ this .removeNode (node .getId ());
1519
+ }
1520
+
1521
+ #selectedNodes = new Set ();
1522
+
1523
+ addNodeSelection (node)
1524
+ {
1525
+ this .#selectedNodes .add (node);
1526
+
1527
+ this .nodes .find (`.node[node-id=${node .getId ()}]`) .addClass ("selected");
1528
+ }
1529
+
1530
+ removeNodeSelection (node)
1531
+ {
1532
+ this .#selectedNodes .delete (node);
1533
+
1534
+ this .nodes .find (`.node[node-id=${node .getId ()}]`) .removeClass ("selected");
1535
+ }
1536
+
1537
+ setNodeSelection (node)
1538
+ {
1539
+ this .clearNodeSelection ();
1540
+ this .addNodeSelection (node);
1541
+ }
1542
+
1543
+ clearNodeSelection ()
1544
+ {
1545
+ this .#selectedNodes .clear ();
1546
+
1547
+ this .nodes .find (`.node`) .removeClass ("selected");
1548
+ }
1549
+
1550
+ isNodeSelected (node)
1551
+ {
1552
+ return this .#selectedNodes .has (node);
1553
+ }
1554
+
1555
+ nodeLive (id)
1556
+ {
1557
+ const node = this .getNode (id);
1558
+
1559
+ if (node .isLive ())
1560
+ return;
1561
+
1562
+ this .removeNode (id);
1563
+ }
1564
+
1565
+ updateImportedNodes ()
1566
+ {
1567
+ const
1568
+ active = this .config .file .activePage,
1569
+ pages = this .pages,
1570
+ page = pages [active];
1571
+
1572
+ for (const { id } of page .nodes)
1573
+ {
1574
+ const element = this .nodes .find (`.imported-node[node-id=${id}]`);
1575
+
1576
+ if (!element .length)
1577
+ continue;
1578
+
1579
+ const
1580
+ node = this .getNode (id),
1581
+ executionContext = node .getExecutionContext ();
1582
+
1583
+ if (executionContext .importedNodes .find (importedNode => importedNode .getExportedNode () === node))
1584
+ continue;
1585
+
1586
+ this .removeNode (id);
1587
+ }
1588
+
1589
+ this .requestUpdateCanvas ();
1590
+ }
1591
+
1592
+ #lasso;
1593
+ #lassoStart;
1594
+ #lassoScroll;
1595
+ #lassoNodes;
1596
+
1597
+ drawLassoStart (event)
1598
+ {
1599
+ if (event .button !== 0)
1600
+ return;
1601
+
1602
+ if (this .#menu || this .#input || this .#output)
1603
+ return;
1604
+
1605
+ this .#lassoStart = this .#pointer .copy ();
1606
+ this .#lassoScroll = new X3D .Vector2 (this .nodes .scrollLeft (), this .nodes .scrollTop ());
1607
+ this .#lassoNodes = new Set (this .#selectedNodes);
1608
+
1609
+ this .nodes .on ("scroll.lasso", event => this .drawLasso (event));
1610
+ $(document) .on ("mousemove.lasso", event => this .drawLasso (event));
1611
+ $(document) .on ("mouseup.lasso", event => this .drawLassoEnd (event));
1612
+ }
1613
+
1614
+ drawLasso (event)
1615
+ {
1616
+ if (event .button !== 0)
1617
+ return;
1618
+
1619
+ if (this .#movingNode)
1620
+ return this .drawLassoEnd (event);
1621
+
1622
+ // Draw Lasso
1623
+
1624
+ const
1625
+ context = this .overlay [0] .getContext ("2d"),
1626
+ width = this .overlay .width (),
1627
+ height = this .overlay .height (),
1628
+ contentScale = window .devicePixelRatio,
1629
+ pointer = this .getRelativePosition (event, false),
1630
+ scroll = new X3D .Vector2 (this .nodes .scrollLeft (), this .nodes .scrollTop ()),
1631
+ deltaScroll = this .#lassoScroll .copy () .subtract (scroll),
1632
+ start = this .#lassoStart .copy () .add (deltaScroll),
1633
+ size = pointer .copy () .subtract (start);
1634
+
1635
+ const
1636
+ fillStyle = this .#style .getPropertyValue ("--lasso-fill"),
1637
+ strokeStyle = this .#style .getPropertyValue ("--lasso-stroke");
1638
+
1639
+ context .fillStyle = fillStyle;
1640
+ context .strokeStyle = strokeStyle;
1641
+ context .lineWidth = 2;
1642
+
1643
+ context .save ();
1644
+ context .scale (contentScale, contentScale);
1645
+ context .clearRect (0, 0, width, height);
1646
+
1647
+ context .beginPath ();
1648
+ context .rect (... start, ... size);
1649
+ context .fill ();
1650
+
1651
+ context .beginPath ();
1652
+ context .rect (... start, ... size);
1653
+ context .stroke ();
1654
+
1655
+ context .restore ();
1656
+
1657
+ // Intersection Test
1658
+
1659
+ if (Math .max (... size .copy () .abs ()) < 10)
1660
+ return;
1661
+
1662
+ this .#lasso = true;
1663
+
1664
+ this .nodes .addClass ("lasso");
1665
+
1666
+ const
1667
+ page = this .page,
1668
+ end = start .copy () .add (size);
1669
+
1670
+ const lasso = {
1671
+ left: Math .min (start .x, end .x) + scroll .x,
1672
+ top: Math .min (start .y, end .y) + scroll .y,
1673
+ right: Math .max (start .x, end .x) + scroll .x,
1674
+ bottom: Math .max (start .y, end .y) + scroll .y,
1675
+ };
1676
+
1677
+ if (!event .shiftKey)
1678
+ this .clearNodeSelection ();
1679
+
1680
+ for (const { id } of page .nodes)
1681
+ {
1682
+ const
1683
+ node = this .getNode (id),
1684
+ element = this .nodes .find (`.node[node-id=${id}]`),
1685
+ left = parseInt (element .css ("left")),
1686
+ top = parseInt (element .css ("top")),
1687
+ rect = element [0] .getBoundingClientRect (),
1688
+ rectangle = { left, top, right: left + rect .width, bottom: top + rect .height };
1689
+
1690
+ if (this .rectanglesIntersect (lasso, rectangle))
1691
+ {
1692
+ if (event .shiftKey)
1693
+ {
1694
+ if (this .#lassoNodes .has (node))
1695
+ this .removeNodeSelection (node);
1696
+ else
1697
+ this .addNodeSelection (node);
1698
+ }
1699
+ else
1700
+ {
1701
+ this .addNodeSelection (node);
1702
+ }
1703
+ }
1704
+ else
1705
+ {
1706
+ if (event .shiftKey)
1707
+ {
1708
+ if (this .#lassoNodes .has (node))
1709
+ this .addNodeSelection (node);
1710
+ else
1711
+ this .removeNodeSelection (node);
1712
+ }
1713
+ }
1714
+ }
1715
+
1716
+ const
1717
+ overscrollX = end .x - this .nodes .width (),
1718
+ overscrollY = end .y - this .nodes .height ();
1719
+
1720
+ if (end .x < 10)
1721
+ this .nodes .scrollLeft (this .nodes .scrollLeft () - 10);
1722
+ else if (overscrollX > -10)
1723
+ this .nodes .scrollLeft (this .nodes .scrollLeft () + 10);
1724
+
1725
+ if (end .y < 10)
1726
+ this .nodes .scrollTop (this .nodes .scrollTop () - 10);
1727
+ else if (overscrollY > -10)
1728
+ this .nodes .scrollTop (this .nodes .scrollTop () + 10);
1729
+ }
1730
+
1731
+ drawLassoEnd (event)
1732
+ {
1733
+ if (event .button !== 0)
1734
+ return;
1735
+
1736
+ this .#lasso = false;
1737
+
1738
+ this .nodes .removeClass ("lasso") .off (".lasso");
1739
+ $(document) .off (".lasso");
1740
+
1741
+ const
1742
+ context = this .overlay [0] .getContext ("2d"),
1743
+ width = this .overlay .width (),
1744
+ height = this .overlay .height (),
1745
+ contentScale = window .devicePixelRatio;
1746
+
1747
+ context .save ();
1748
+ context .scale (contentScale, contentScale);
1749
+ context .clearRect (0, 0, width, height);
1750
+ context .restore ();
1751
+ }
1752
+
1753
+ rectanglesIntersect (a, b)
1754
+ {
1755
+ return Math .max (a .left, b .left) < Math .min (a .right, b .right) &&
1756
+ Math .max (a .top, b .top) < Math .min (a .bottom, b .bottom);
1757
+ }
1758
+
1759
+ #input = null;
1760
+ #output = null;
1761
+
1762
+ selectInput (event, id, fieldName)
1763
+ {
1764
+ if (event .button !== 0)
1765
+ return;
1766
+
1767
+ if (this .#lasso)
1768
+ return;
1769
+
1770
+ event .preventDefault ();
1771
+ event .stopPropagation ();
1772
+
1773
+ if (this .#output)
1774
+ {
1775
+ const
1776
+ executionContext = this .#output .node .getExecutionContext (),
1777
+ node = this .getNode (id);
1778
+
1779
+ // Add route.
1780
+
1781
+ Editor .addRoute (executionContext, this .#output .node, this .#output .field .getName (), node, fieldName);
1782
+
1783
+ this .clearInputOutput ();
1784
+ }
1785
+ else
1786
+ {
1787
+ const
1788
+ node = this .getNode (id),
1789
+ field = node .getField (fieldName),
1790
+ executionContext = node .getExecutionContext ();
1791
+
1792
+ const
1793
+ offset = this .nodes .offset (),
1794
+ destinationNodeElement = this .nodes .find (`.node[node-id=${node .getId ()}]`),
1795
+ destinationElement = destinationNodeElement .find (`.field[name="${field .getName ()}"] .input`),
1796
+ [toX, toY] = this .getDestinationPosition (offset, destinationElement),
1797
+ scrollLeft = this .nodes .scrollLeft (),
1798
+ scrollTop = this .nodes .scrollTop ();
1799
+
1800
+ this .#input = { node, field, toX: toX + scrollLeft, toY: toY + scrollTop };
1801
+
1802
+ this .nodes .find (`.input, .field:not([type-name="${field .getTypeName ()}"]) .output`)
1803
+ .css ("visibility", "hidden");
1804
+
1805
+ this .nodes .find (`.node:not([execution-context-id=${executionContext .getId ()}]) .output`)
1806
+ .css ("visibility", "hidden");
1807
+
1808
+ this .nodes .find (`.node[node-id=${node .getId ()}] .field[name=${field .getName ()}] .output`)
1809
+ .css ("visibility", "hidden");
1810
+ }
1811
+ }
1812
+
1813
+ selectOutput (event, id, fieldName)
1814
+ {
1815
+ if (event .button !== 0)
1816
+ return;
1817
+
1818
+ if (this .#lasso)
1819
+ return;
1820
+
1821
+ event .preventDefault ();
1822
+ event .stopPropagation ();
1823
+
1824
+ if (this .#input)
1825
+ {
1826
+ const
1827
+ executionContext = this .#input .node .getExecutionContext (),
1828
+ node = this .getNode (id);
1829
+
1830
+ // Add route.
1831
+
1832
+ Editor .addRoute (executionContext, node, fieldName, this .#input .node, this .#input .field .getName ());
1833
+
1834
+ this .clearInputOutput ();
1835
+ }
1836
+ else
1837
+ {
1838
+ const
1839
+ node = this .getNode (id),
1840
+ field = node .getField (fieldName),
1841
+ executionContext = node .getExecutionContext ();
1842
+
1843
+ const
1844
+ offset = this .nodes .offset (),
1845
+ sourceNodeElement = this .nodes .find (`.node[node-id=${node .getId ()}]`),
1846
+ sourceElement = sourceNodeElement .find (`.field[name="${field .getName ()}"] .output`),
1847
+ [fromX, fromY] = this .getSourcePosition (offset, sourceElement),
1848
+ scrollLeft = this .nodes .scrollLeft (),
1849
+ scrollTop = this .nodes .scrollTop ();
1850
+
1851
+ this .#output = { node, field, fromX: fromX + scrollLeft, fromY: fromY + scrollTop };
1852
+
1853
+ this .nodes .find (`.field:not([type-name="${field .getTypeName ()}"]) .input, .output`)
1854
+ .css ("visibility", "hidden");
1855
+
1856
+ this .nodes .find (`.node:not([execution-context-id=${executionContext .getId ()}]) .input`)
1857
+ .css ("visibility", "hidden");
1858
+
1859
+ this .nodes .find (`.node[node-id=${node .getId ()}] .field[name=${field .getName ()}] .input`)
1860
+ .css ("visibility", "hidden");
1861
+ }
1862
+ }
1863
+
1864
+ clearInputOutput ()
1865
+ {
1866
+ this .#input = null;
1867
+ this .#output = null;
1868
+
1869
+ this .nodes .find (`.input, .output`) .css ("visibility", "");
1870
+
1871
+ this .requestUpdateCanvas ();
1872
+ }
1873
+
1874
+ #pointer = new X3D .Vector2 ();
1875
+
1876
+ mouseMove (event)
1877
+ {
1878
+ this .#pointer = this .getRelativePosition (event, false);
1879
+
1880
+ if (this .#input || this .#output)
1881
+ this .updateCanvas ();
1882
+ }
1883
+
1884
+ selectRoute (event, deleteRoute)
1885
+ {
1886
+ if (event .button !== 0)
1887
+ return;
1888
+
1889
+ const
1890
+ page = this .page,
1891
+ scale = page .scale,
1892
+ nodes = new Set (page .nodes .map (node => this .getNode (node .id))),
1893
+ offset = this .nodes .offset (),
1894
+ pointer = this .getRelativePosition (event, false);
1895
+
1896
+ for (const sourceNode of nodes)
1897
+ {
1898
+ const sourceNodeElement = this .nodes .find (`.node[node-id=${sourceNode .getId ()}]`);
1899
+
1900
+ for (const sourceField of sourceNode .getFields ())
1901
+ {
1902
+ for (const route of sourceField .getOutputRoutes ())
1903
+ {
1904
+ const destinationNode = route .getDestinationNode ();
1905
+
1906
+ if (!nodes .has (destinationNode))
1907
+ continue;
1908
+
1909
+ const
1910
+ sourceElement = sourceNodeElement .find (`.field[name="${sourceField .getName ()}"] .output`),
1911
+ destinationField = destinationNode .getField (route .getDestinationField ()),
1912
+ destinationNodeElement = this .nodes .find (`.node[node-id=${destinationNode .getId ()}]`),
1913
+ destinationElement = destinationNodeElement .find (`.field[name="${destinationField .getName ()}"] .input`);
1914
+
1915
+ const
1916
+ [fromX, fromY] = this .getSourcePosition (offset, sourceElement),
1917
+ [toX, toY] = this .getDestinationPosition (offset, destinationElement);
1918
+
1919
+ const
1920
+ wp = (toX - fromX) * 0.5,
1921
+ x0 = fromX,
1922
+ y0 = fromY,
1923
+ x1 = fromX + wp,
1924
+ y1 = fromY,
1925
+ x2 = toX - wp,
1926
+ y2 = toY,
1927
+ x3 = toX,
1928
+ y3 = toY;
1929
+
1930
+ const
1931
+ arrowRotation = this .getBezierTangentAngle (x0, y0, x1, y1, x2, y2, x3, y3, 0.5),
1932
+ arrow = this .getRouteArrow (new X3D .Vector2 (x0, y0), new X3D .Vector2 (x3, y3), arrowRotation, scale);
1933
+
1934
+ if (X3D .Triangle2 .isPointInTriangle (pointer, ... arrow))
1935
+ {
1936
+ if (deleteRoute)
1937
+ {
1938
+ Editor .deleteRoute (route .getExecutionContext (), route .sourceNode, route .sourceField, route .destinationNode, route .destinationField);
1939
+
1940
+ this .removeRouteSelection (route);
1941
+ }
1942
+ else
1943
+ {
1944
+ if (event .shiftKey)
1945
+ {
1946
+ if (this .isRouteSelected (route))
1947
+ this .removeRouteSelection (route);
1948
+ else
1949
+ this .addRouteSelection (route);
1950
+ }
1951
+ else
1952
+ {
1953
+ this .setRouteSelection (route);
1954
+ }
1955
+ }
1956
+
1957
+ // Only select or delete one route.
1958
+ return;
1959
+ }
1960
+ }
1961
+ }
1962
+ }
1963
+ }
1964
+
1965
+ deleteRoute (event)
1966
+ {
1967
+ this .selectRoute (event, true);
1968
+ }
1969
+
1970
+ selectAllRoutes ()
1971
+ {
1972
+ const
1973
+ page = this .page,
1974
+ nodes = new Set (page .nodes .map (node => this .getNode (node .id)));
1975
+
1976
+ this .clearRouteSelection ();
1977
+
1978
+ for (const sourceNode of nodes)
1979
+ {
1980
+ for (const sourceField of sourceNode .getFields ())
1981
+ {
1982
+ for (const route of sourceField .getOutputRoutes ())
1983
+ {
1984
+ const destinationNode = route .getDestinationNode ();
1985
+
1986
+ if (!nodes .has (destinationNode))
1987
+ continue;
1988
+
1989
+ this .addRouteSelection (route);
1990
+ }
1991
+ }
1992
+ }
1993
+ }
1994
+
1995
+ deleteSelectedRoutes ()
1996
+ {
1997
+ UndoManager .shared .beginUndo (_("Delete Selected Routes"));
1998
+
1999
+ for (const route of this .#selectedRoutes)
2000
+ {
2001
+ Editor .deleteRoute (route .getExecutionContext (), route .sourceNode, route .sourceField, route .destinationNode, route .destinationField);
2002
+ }
2003
+
2004
+ UndoManager .shared .endUndo ();
2005
+
2006
+ this .clearRouteSelection ();
2007
+ }
2008
+
2009
+ #selectedRoutes = new Set ();
2010
+
2011
+ addRouteSelection (route)
2012
+ {
2013
+ this .#selectedRoutes .add (route);
2014
+
2015
+ this .requestUpdateCanvas ();
2016
+ }
2017
+
2018
+ removeRouteSelection (route)
2019
+ {
2020
+ this .#selectedRoutes .delete (route);
2021
+
2022
+ this .requestUpdateCanvas ();
2023
+ }
2024
+
2025
+ setRouteSelection (route)
2026
+ {
2027
+ this .clearRouteSelection ();
2028
+ this .addRouteSelection (route);
2029
+ }
2030
+
2031
+ clearRouteSelection ()
2032
+ {
2033
+ this .#selectedRoutes .clear ();
2034
+
2035
+ this .requestUpdateCanvas ();
2036
+ }
2037
+
2038
+ isRouteSelected (route)
2039
+ {
2040
+ return this .#selectedRoutes .has (route);
2041
+ }
2042
+
2043
+ resizeCanvas ()
2044
+ {
2045
+ const contentScale = window .devicePixelRatio;
2046
+
2047
+ this .topCanvas
2048
+ .prop ("width", this .topCanvas .width () * contentScale)
2049
+ .prop ("height", this .topCanvas .height () * contentScale);
2050
+
2051
+ this .canvas
2052
+ .prop ("width", this .canvas .width () * contentScale)
2053
+ .prop ("height", this .canvas .height () * contentScale);
2054
+
2055
+ this .overlay
2056
+ .prop ("width", this .overlay .width () * contentScale)
2057
+ .prop ("height", this .overlay .height () * contentScale);
2058
+
2059
+ this .updateCanvas ();
2060
+ }
2061
+
2062
+ #updateCanvasId;
2063
+
2064
+ requestUpdateCanvas ()
2065
+ {
2066
+ clearTimeout (this .#updateCanvasId);
2067
+
2068
+ this .#updateCanvasId = setTimeout (() => this .updateCanvas ());
2069
+ }
2070
+
2071
+ updateCanvas ()
2072
+ {
2073
+ const
2074
+ context = this .canvas [0] .getContext ("2d"),
2075
+ topContext = this .topCanvas [0] .getContext ("2d"),
2076
+ width = this .canvas .width (),
2077
+ height = this .canvas .height (),
2078
+ offsetX = this .nodes .scrollLeft (),
2079
+ offsetY = this .nodes .scrollTop (),
2080
+ top = this .topCanvas .height (),
2081
+ contentScale = window .devicePixelRatio;
2082
+
2083
+ // Tabs
2084
+
2085
+ topContext .save ();
2086
+ topContext .scale (contentScale, contentScale);
2087
+
2088
+ this .drawGrid (topContext, width, top, offsetX, offsetY - top);
2089
+
2090
+ topContext .restore ();
2091
+
2092
+ // Routes
2093
+
2094
+ context .save ();
2095
+ context .scale (contentScale, contentScale);
2096
+
2097
+ this .drawGrid (context, width, height, offsetX, offsetY);
2098
+ this .drawRoutes (context);
2099
+
2100
+ context .restore ();
2101
+ }
2102
+
2103
+ #gridSize = 20;
2104
+
2105
+ drawGrid (context, width, height, offsetX, offsetY)
2106
+ {
2107
+ const
2108
+ size = this .#gridSize,
2109
+ color = this .#style .getPropertyValue ("--system-gray5");
2110
+
2111
+ context .clearRect (0, 0, width, height);
2112
+
2113
+ context .strokeStyle = color;
2114
+ context .lineWidth = 1;
2115
+
2116
+ for (let x = -offsetX % size; x <= width; x += size)
2117
+ {
2118
+ context .beginPath ();
2119
+ context .moveTo (x, 0);
2120
+ context .lineTo (x, height);
2121
+ context .stroke ();
2122
+ }
2123
+
2124
+ for (let y = -offsetY % size; y <= height; y += size)
2125
+ {
2126
+ context .beginPath ();
2127
+ context .moveTo (0, y);
2128
+ context .lineTo (width, y);
2129
+ context .stroke ();
2130
+ }
2131
+ }
2132
+
2133
+ drawRoutes (context)
2134
+ {
2135
+ const
2136
+ page = this .page,
2137
+ scale = page .scale,
2138
+ nodes = new Set (page .nodes .map (node => this .getNode (node .id))),
2139
+ offset = this .nodes .offset (),
2140
+ scrollLeft = this .nodes .scrollLeft (),
2141
+ scrollTop = this .nodes .scrollTop ();
2142
+
2143
+ // DEBUG
2144
+ nodes .delete (undefined);
2145
+
2146
+ this .nodes .attr ("title", "");
2147
+
2148
+ const
2149
+ color = this .#style .getPropertyValue ("--route-color"),
2150
+ selectedColor = this .#style .getPropertyValue ("--route-selected-color");
2151
+
2152
+ context .lineWidth = 3 * scale;
2153
+
2154
+ for (const sourceNode of nodes)
2155
+ {
2156
+ const sourceNodeElement = this .nodes .find (`.node[node-id=${sourceNode .getId ()}]`);
2157
+
2158
+ for (const sourceField of sourceNode .getFields ())
2159
+ {
2160
+ for (const route of sourceField .getOutputRoutes ())
2161
+ {
2162
+ const destinationNode = route .getDestinationNode ();
2163
+
2164
+ if (!nodes .has (destinationNode))
2165
+ continue;
2166
+
2167
+ const
2168
+ sourceElement = sourceNodeElement .find (`.field[name="${sourceField .getName ()}"] .output`),
2169
+ destinationField = destinationNode .getField (route .getDestinationField ()),
2170
+ destinationNodeElement = this .nodes .find (`.node[node-id=${destinationNode .getId ()}]`),
2171
+ destinationElement = destinationNodeElement .find (`.field[name="${destinationField .getName ()}"] .input`);
2172
+
2173
+ const
2174
+ [fromX, fromY] = this .getSourcePosition (offset, sourceElement),
2175
+ [toX, toY] = this .getDestinationPosition (offset, destinationElement);
2176
+
2177
+ if (this .isRouteSelected (route))
2178
+ {
2179
+ context .fillStyle = selectedColor;
2180
+ context .strokeStyle = selectedColor;
2181
+ }
2182
+ else
2183
+ {
2184
+ context .fillStyle = color;
2185
+ context .strokeStyle = color;
2186
+ }
2187
+
2188
+ this .drawRoute (context, fromX, fromY, toX, toY, scale, selectedColor, route);
2189
+ }
2190
+ }
2191
+ }
2192
+
2193
+ context .fillStyle = color;
2194
+ context .strokeStyle = color;
2195
+
2196
+ if (this .#input)
2197
+ this .drawRoute (context, ... this .#pointer, this .#input .toX - scrollLeft, this .#input .toY - scrollTop, scale);
2198
+
2199
+ if (this .#output)
2200
+ this .drawRoute (context, this .#output .fromX - scrollLeft, this .#output .fromY - scrollTop, ... this .#pointer, scale);
2201
+ }
2202
+
2203
+ drawRoute (context, fromX, fromY, toX, toY, scale, selectedColor, route)
2204
+ {
2205
+ // Draw sine curved route.
2206
+
2207
+ const
2208
+ wp = (toX - fromX) * 0.5,
2209
+ x0 = fromX,
2210
+ y0 = fromY,
2211
+ x1 = fromX + wp,
2212
+ y1 = fromY,
2213
+ x2 = toX - wp,
2214
+ y2 = toY,
2215
+ x3 = toX,
2216
+ y3 = toY;
2217
+
2218
+ context .beginPath ();
2219
+ context .moveTo (x0, y0);
2220
+ context .bezierCurveTo (x1, y1, x2, y2, x3, y3);
2221
+ context .stroke ();
2222
+
2223
+ // Draw arrow
2224
+
2225
+ const
2226
+ arrowRotation = this .getBezierTangentAngle (x0, y0, x1, y1, x2, y2, x3, y3, 0.5),
2227
+ arrow = this .getRouteArrow (new X3D .Vector2 (x0, y0), new X3D .Vector2 (x3, y3), arrowRotation, scale);
2228
+
2229
+ if (selectedColor && X3D .Triangle2 .isPointInTriangle (this .#pointer, ... arrow))
2230
+ {
2231
+ context .fillStyle = selectedColor;
2232
+
2233
+ const sourceNodeName = route .getSourceNode () instanceof X3D .X3DNode
2234
+ ? route .getSourceNode () .getName ()
2235
+ : route .getSourceNode () .getImportedName ();
2236
+
2237
+ const destinationNodeName = route .getDestinationNode () instanceof X3D .X3DNode
2238
+ ? route .getDestinationNode () .getName ()
2239
+ : route .getDestinationNode () .getImportedName ();
2240
+
2241
+ this .nodes .attr ("title", util .format (_("Route from %s<%s>.%s to %s<%s>.%s"), route .getSourceNode () .getTypeName (), sourceNodeName || _("unnamed"), route .sourceField, route .getDestinationNode () .getTypeName (), destinationNodeName || _("unnamed"), route .destinationField));
2242
+ }
2243
+
2244
+ context .beginPath ();
2245
+ context .moveTo (arrow [0] .x, arrow [0] .y);
2246
+ context .lineTo (arrow [1] .x, arrow [1] .y);
2247
+ context .lineTo (arrow [2] .x, arrow [2] .y);
2248
+ context .fill ();
2249
+ }
2250
+
2251
+ getRouteArrow (sourcePosition, destinationPosition, rotation, scale)
2252
+ {
2253
+ // Intersect with arrow
2254
+
2255
+ const
2256
+ p1 = new X3D .Vector2 (),
2257
+ p2 = new X3D .Vector2 (0, 14),
2258
+ p3 = new X3D .Vector2 (10 / 9 * 14, 7),
2259
+ c = p1 .copy () .add (p2) .add (p3) .divide (3) .negate (),
2260
+ t = sourcePosition .copy () .add (destinationPosition) .divide (2),
2261
+ m = new X3D .Matrix3 ();
2262
+
2263
+ m .translate (t);
2264
+ m .rotate (rotation);
2265
+ m .scale (new X3D .Vector2 (scale));
2266
+ m .translate (c);
2267
+
2268
+ return [m .multVecMatrix (p1), m .multVecMatrix (p2), m .multVecMatrix (p3)];
2269
+ }
2270
+
2271
+ getBezierTangentAngle (x0, y0, x1, y1, x2, y2, x3, y3, t)
2272
+ {
2273
+ const mt = 1 - t;
2274
+
2275
+ // Get coefficients for the first derivative of a cubic Bézier curve.
2276
+ const a = 3 * mt * mt;
2277
+ const b = 6 * mt * t;
2278
+ const c = 3 * t * t;
2279
+
2280
+ // Calculate the tangent vector (dx, dy).
2281
+ const dx = a * (x1 - x0) + b * (x2 - x1) + c * (x3 - x2);
2282
+ const dy = a * (y1 - y0) + b * (y2 - y1) + c * (y3 - y2);
2283
+
2284
+ // Return the angle in radians (-PI to PI).
2285
+ return Math .atan2 (dy, dx);
2286
+ }
2287
+
2288
+ getSourcePosition (offset, sourceElement)
2289
+ {
2290
+ const
2291
+ sourceOffset = sourceElement .offset (),
2292
+ rect = sourceElement [0] .getBoundingClientRect (),
2293
+ fromX = sourceOffset .left - offset .left + rect .width / 2,
2294
+ fromY = sourceOffset .top - offset .top + rect .height / 2;
2295
+
2296
+ return [fromX, fromY];
2297
+ }
2298
+
2299
+ getDestinationPosition (offset, destinationElement)
2300
+ {
2301
+ const
2302
+ destinationOffset = destinationElement .offset (),
2303
+ rect = destinationElement [0] .getBoundingClientRect (),
2304
+ toX = destinationOffset .left - offset .left + rect .width / 2,
2305
+ toY = destinationOffset .top - offset .top + rect .height / 2;
2306
+
2307
+ return [toX, toY];
2308
+ }
2309
+
2310
+ dragEnter (event)
2311
+ {
2312
+ event .preventDefault ();
2313
+ event .stopPropagation ();
2314
+
2315
+ if (event .originalEvent .dataTransfer .types .includes ("sunrize/nodes") ||
2316
+ event .originalEvent .dataTransfer .types .includes ("sunrize/imported-node"))
2317
+ {
2318
+ event .originalEvent .dataTransfer .dropEffect = "copy";
2319
+ }
2320
+ else
2321
+ {
2322
+ event .originalEvent .dataTransfer .dropEffect = "none";
2323
+ }
2324
+ }
2325
+
2326
+ drop (event)
2327
+ {
2328
+ if (!event .originalEvent .dataTransfer .types .includes ("sunrize/nodes") &&
2329
+ !event .originalEvent .dataTransfer .types .includes ("sunrize/imported-node"))
2330
+ {
2331
+ return;
2332
+ }
2333
+
2334
+ this .nodes .trigger ("focus");
2335
+
2336
+ const ids = event .originalEvent .dataTransfer .types .includes ("sunrize/imported-node")
2337
+ ? event .originalEvent .dataTransfer .getData ("sunrize/imported-node") .split (",")
2338
+ : event .originalEvent .dataTransfer .getData ("sunrize/nodes") .split (",");
2339
+
2340
+ for (const id of ids)
2341
+ {
2342
+ const
2343
+ element = $(`#${id}`),
2344
+ node = this .outlineEditor .getNode (element),
2345
+ position = this .getRelativePosition (event, true);
2346
+
2347
+ if (this .config .global .addConnectedNodes)
2348
+ this .addConnectedNodes (node, position);
2349
+ else
2350
+ this .addNode (node, position);
2351
+ }
2352
+ }
2353
+
2354
+ getRelativePosition (event, scroll)
2355
+ {
2356
+ const
2357
+ bounds = this .nodes .offset (),
2358
+ x = event .clientX - bounds .left + (scroll ? this .nodes .scrollLeft () : 0),
2359
+ y = event .clientY - bounds .top + (scroll ? this .nodes .scrollTop () : 0);
2360
+
2361
+ return new X3D .Vector2 (x, y);
2362
+ }
2363
+
2364
+ round (value, steps)
2365
+ {
2366
+ return Math .round (value / steps) * steps;
2367
+ }
2368
+ };