nodeLink.ts 5.4 KB

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