nodeLink.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { GraphCanvasComponent, FramePortData } from './graphCanvas';
  2. import { GraphNode } from './graphNode';
  3. import { NodePort } from './nodePort';
  4. import { Nullable } from 'babylonjs/types';
  5. import { Observer, Observable } from 'babylonjs/Misc/observable';
  6. import { GraphFrame } from './graphFrame';
  7. import { FrameNodePort } from './frameNodePort';
  8. export class NodeLink {
  9. private _graphCanvas: GraphCanvasComponent;
  10. private _portA: NodePort | FrameNodePort;
  11. private _portB?: NodePort | FrameNodePort;
  12. private _nodeA: GraphNode;
  13. private _nodeB?: GraphNode;
  14. private _path: SVGPathElement;
  15. private _selectionPath: SVGPathElement;
  16. private _onSelectionChangedObserver: Nullable<Observer<Nullable<GraphFrame | GraphNode | NodeLink | NodePort | FramePortData>>>;
  17. private _isVisible = true;
  18. public onDisposedObservable = new Observable<NodeLink>();
  19. public get isVisible() {
  20. return this._isVisible;
  21. }
  22. public set isVisible(value: boolean) {
  23. this._isVisible = value;
  24. if (!value) {
  25. this._path.classList.add("hidden");
  26. this._selectionPath.classList.add("hidden");
  27. } else {
  28. this._path.classList.remove("hidden");
  29. this._selectionPath.classList.remove("hidden");
  30. }
  31. this.update();
  32. }
  33. public get portA() {
  34. return this._portA;
  35. }
  36. public get portB() {
  37. return this._portB;
  38. }
  39. public get nodeA() {
  40. return this._nodeA;
  41. }
  42. public get nodeB() {
  43. return this._nodeB;
  44. }
  45. public update(endX = 0, endY = 0, straight = false) {
  46. const rectA = this._portA.element.getBoundingClientRect();
  47. const rootRect = this._graphCanvas.canvasContainer.getBoundingClientRect();
  48. const zoom = this._graphCanvas.zoom;
  49. const xOffset = rootRect.left;
  50. const yOffset = rootRect.top;
  51. var startX = (rectA.left - xOffset + 0.5 * rectA.width) / zoom;
  52. var startY = (rectA.top - yOffset + 0.5 * rectA.height) / zoom;
  53. if (this._portB) {
  54. const rectB = this._portB.element.getBoundingClientRect();
  55. endX = (rectB.left - xOffset + 0.5 * rectB.width) / zoom;
  56. endY = (rectB.top - yOffset + 0.5 * rectB.height) / zoom;
  57. }
  58. if (straight) {
  59. this._path.setAttribute("d", `M${startX},${startY} L${endX},${endY}`);
  60. this._path.setAttribute("stroke-dasharray", "10, 10");
  61. this._path.setAttribute("stroke-linecap", "round");
  62. } else {
  63. const deltaX = endX - startX;
  64. const deltaY = endY - startY;
  65. const tangentLength = Math.min(Math.sqrt(deltaX * deltaX + deltaY * deltaY) * 0.5, 300);
  66. this._path.setAttribute("d", `M${startX},${startY} C${startX + tangentLength},${startY} ${endX - tangentLength},${endY} ${endX},${endY}`);
  67. this._selectionPath.setAttribute("d", `M${startX},${startY} C${startX + tangentLength},${startY} ${endX - tangentLength},${endY} ${endX},${endY}`);
  68. }
  69. this._path.setAttribute("stroke", this._portA.element.style.backgroundColor!);
  70. }
  71. public constructor(graphCanvas: GraphCanvasComponent, portA: NodePort, nodeA: GraphNode, portB?: NodePort, nodeB?: GraphNode) {
  72. this._portA = portA;
  73. this._portB = portB;
  74. this._nodeA = nodeA;
  75. this._nodeB = nodeB;
  76. this._graphCanvas = graphCanvas;
  77. var document = portA.element.ownerDocument!;
  78. var svg = graphCanvas.svgCanvas;
  79. // Create path
  80. this._path = document.createElementNS('http://www.w3.org/2000/svg', "path");
  81. this._path.setAttribute("fill", "none");
  82. this._path.classList.add("link");
  83. svg.appendChild(this._path);
  84. this._selectionPath = document.createElementNS('http://www.w3.org/2000/svg', "path");
  85. this._selectionPath.setAttribute("fill", "none");
  86. this._selectionPath.classList.add("selection-link");
  87. svg.appendChild(this._selectionPath);
  88. this._selectionPath.onmousedown = () => this.onClick();
  89. if (this._portB) {
  90. // Update
  91. this.update();
  92. }
  93. this._onSelectionChangedObserver = this._graphCanvas.globalState.onSelectionChangedObservable.add((selection) => {
  94. if (selection === this) {
  95. this._path.classList.add("selected");
  96. this._selectionPath.classList.add("selected");
  97. } else {
  98. this._path.classList.remove("selected");
  99. this._selectionPath.classList.remove("selected");
  100. }
  101. });
  102. }
  103. onClick() {
  104. this._graphCanvas.globalState.onSelectionChangedObservable.notifyObservers(this);
  105. }
  106. public dispose() {
  107. this._graphCanvas.globalState.onSelectionChangedObservable.remove(this._onSelectionChangedObserver);
  108. if (this._path.parentElement) {
  109. this._path.parentElement.removeChild(this._path);
  110. }
  111. if (this._selectionPath.parentElement) {
  112. this._selectionPath.parentElement.removeChild(this._selectionPath);
  113. }
  114. if (this._nodeB) {
  115. this._nodeA.links.splice(this._nodeA.links.indexOf(this), 1);
  116. this._nodeB.links.splice(this._nodeB.links.indexOf(this), 1);
  117. this._graphCanvas.links.splice(this._graphCanvas.links.indexOf(this), 1);
  118. this._portA.connectionPoint.disconnectFrom(this._portB!.connectionPoint);
  119. }
  120. this.onDisposedObservable.notifyObservers(this);
  121. }
  122. }