graphFrame.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. import { GraphNode } from './graphNode';
  2. import { GraphCanvasComponent } from './graphCanvas';
  3. import { Nullable } from 'babylonjs/types';
  4. import { Observer } from 'babylonjs/Misc/observable';
  5. import { NodeLink } from './nodeLink';
  6. import { IFrameData } from '../nodeLocationInfo';
  7. import { Color3 } from 'babylonjs/Maths/math.color';
  8. import { NodePort } from './nodePort';
  9. export class GraphFrame {
  10. private _name: string;
  11. private _color: Color3;
  12. private _x = 0;
  13. private _y = 0;
  14. private _gridAlignedX = 0;
  15. private _gridAlignedY = 0;
  16. private _width: number;
  17. private _height: number;
  18. public element: HTMLDivElement;
  19. private _borderElement: HTMLDivElement;
  20. private _headerElement: HTMLDivElement;
  21. private _headerTextElement: HTMLDivElement;
  22. private _headerCollapseElement: HTMLDivElement;
  23. private _headerCloseElement: HTMLDivElement;
  24. private _portContainer: HTMLDivElement;
  25. private _outputPortContainer: HTMLDivElement;
  26. private _inputPortContainer: HTMLDivElement;
  27. private _nodes: GraphNode[] = [];
  28. private _ownerCanvas: GraphCanvasComponent;
  29. private _mouseStartPointX: Nullable<number> = null;
  30. private _mouseStartPointY: Nullable<number> = null;
  31. private _onSelectionChangedObserver: Nullable<Observer<Nullable<GraphNode | NodeLink | GraphFrame>>>;
  32. private _isCollapsed = false;
  33. private _ports: NodePort[] = [];
  34. private _controlledPorts: NodePort[] = [];
  35. private readonly CloseSVG = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30"><g id="Layer_2" data-name="Layer 2"><path d="M16,15l5.85,5.84-1,1L15,15.93,9.15,21.78l-1-1L14,15,8.19,9.12l1-1L15,14l5.84-5.84,1,1Z"/></g></svg>`;
  36. private readonly ExpandSVG = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30"><g id="Layer_2" data-name="Layer 2"><path d="M22.31,7.69V22.31H7.69V7.69ZM21.19,8.81H8.81V21.19H21.19Zm-6.75,6.75H11.06V14.44h3.38V11.06h1.12v3.38h3.38v1.12H15.56v3.38H14.44Z"/></g></svg>`;
  37. private readonly CollapseSVG = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30"><g id="Layer_2" data-name="Layer 2"><path d="M22.31,7.69V22.31H7.69V7.69ZM21.19,8.81H8.81V21.19H21.19Zm-2.25,6.75H11.06V14.44h7.88Z"/></g></svg>`;
  38. public get isCollapsed() {
  39. return this._isCollapsed;
  40. }
  41. private _createInputPort(port: NodePort, node: GraphNode) {
  42. let localPort = NodePort.CreatePortElement(port.connectionPoint, node, this._inputPortContainer, null, this._ownerCanvas.globalState)
  43. this._ports.push(localPort);
  44. port.delegatedPort = localPort;
  45. this._controlledPorts.push(port);
  46. }
  47. public set isCollapsed(value: boolean) {
  48. if (this._isCollapsed === value) {
  49. return;
  50. }
  51. this._isCollapsed = value;
  52. this._ownerCanvas._frameIsMoving = true;
  53. // Need to delegate the outside ports to the frame
  54. if (value) {
  55. this.element.classList.add("collapsed");
  56. this._moveFrame((this.width - 200) / 2, 0);
  57. for (var node of this._nodes) {
  58. node.isVisible = false;
  59. for (var port of node.outputPorts) { // Output
  60. if (port.connectionPoint.hasEndpoints) {
  61. let portAdded = false;
  62. for (var link of node.links) {
  63. if (link.portA === port && this.nodes.indexOf(link.nodeB!) === -1) {
  64. let localPort: NodePort;
  65. if (!portAdded) {
  66. portAdded = true;
  67. localPort = NodePort.CreatePortElement(port.connectionPoint, link.nodeB!, this._outputPortContainer, null, this._ownerCanvas.globalState);
  68. this._ports.push(localPort);
  69. } else {
  70. localPort = this._ports.filter(p => p.connectionPoint === port.connectionPoint)[0];
  71. }
  72. port.delegatedPort = localPort;
  73. this._controlledPorts.push(port);
  74. link.isVisible = true;
  75. }
  76. }
  77. } else {
  78. let localPort = NodePort.CreatePortElement(port.connectionPoint, node, this._outputPortContainer, null, this._ownerCanvas.globalState)
  79. this._ports.push(localPort);
  80. port.delegatedPort = localPort;
  81. this._controlledPorts.push(port);
  82. }
  83. }
  84. for (var port of node.inputPorts) { // Input
  85. if (port.connectionPoint.isConnected) {
  86. for (var link of node.links) {
  87. if (link.portB === port && this.nodes.indexOf(link.nodeA) === -1) {
  88. this._createInputPort(port, node);
  89. link.isVisible = true;
  90. }
  91. }
  92. } else {
  93. this._createInputPort(port, node);
  94. }
  95. }
  96. }
  97. } else {
  98. this.element.classList.remove("collapsed");
  99. this._outputPortContainer.innerHTML = "";
  100. this._inputPortContainer.innerHTML = "";
  101. this._ports.forEach(p => {
  102. p.dispose();
  103. });
  104. this._controlledPorts.forEach(port => {
  105. port.delegatedPort = null;
  106. port.refresh();
  107. })
  108. this._ports = [];
  109. this._controlledPorts = [];
  110. for (var node of this._nodes) {
  111. node.isVisible = true;
  112. }
  113. this._moveFrame(-(this.width - 200) / 2, 0);
  114. }
  115. this.cleanAccumulation();
  116. this._ownerCanvas._frameIsMoving = false;
  117. }
  118. public get nodes() {
  119. return this._nodes;
  120. }
  121. public get name() {
  122. return this._name;
  123. }
  124. public set name(value: string) {
  125. this._name = value;
  126. this._headerTextElement.innerHTML = value;
  127. }
  128. public get color() {
  129. return this._color;
  130. }
  131. public set color(value: Color3) {
  132. this._color = value;
  133. this._headerElement.style.background = `rgba(${value.r * 255}, ${value.g * 255}, ${value.b * 255}, 1)`;
  134. this._headerElement.style.borderColor = `rgba(${value.r * 255}, ${value.g * 255}, ${value.b * 255}, 1)`;
  135. this.element.style.background = `rgba(${value.r * 255}, ${value.g * 255}, ${value.b * 255}, 0.7)`;
  136. }
  137. public get x() {
  138. return this._x;
  139. }
  140. public set x(value: number) {
  141. // if (this._x === value) {
  142. // return;
  143. // }
  144. this._x = value;
  145. this._gridAlignedX = this._ownerCanvas.getGridPosition(value);
  146. this.element.style.left = `${this._gridAlignedX}px`;
  147. }
  148. public get y() {
  149. return this._y;
  150. }
  151. public set y(value: number) {
  152. // if (this._y === value) {
  153. // return;
  154. // }
  155. this._y = value;
  156. this._gridAlignedY = this._ownerCanvas.getGridPosition(value);
  157. this.element.style.top = `${this._gridAlignedY}px`;
  158. }
  159. public get width() {
  160. return this._width;
  161. }
  162. public set width(value: number) {
  163. if (this._width === value) {
  164. return;
  165. }
  166. this._width = value;
  167. var gridAlignedRight = this._ownerCanvas.getGridPositionCeil(value + this._gridAlignedX);
  168. this.element.style.width = `${gridAlignedRight - this._gridAlignedX}px`;
  169. }
  170. public get height() {
  171. return this._height;
  172. }
  173. public set height(value: number) {
  174. if (this._height === value) {
  175. return;
  176. }
  177. this._height = value;
  178. var gridAlignedBottom = this._ownerCanvas.getGridPositionCeil(value + this._gridAlignedY);
  179. this.element.style.height = `${gridAlignedBottom - this._gridAlignedY}px`;
  180. }
  181. public constructor(candidate: Nullable<HTMLDivElement>, canvas: GraphCanvasComponent, doNotCaptureNodes = false) {
  182. this._ownerCanvas = canvas;
  183. const root = canvas.frameContainer;
  184. this.element = root.ownerDocument!.createElement("div");
  185. this.element.classList.add("frame-box");
  186. root.appendChild(this.element);
  187. this._headerElement = root.ownerDocument!.createElement("div");
  188. this._headerElement.classList.add("frame-box-header");
  189. this._headerElement.addEventListener("dblclick", () => {
  190. this.isCollapsed = !this.isCollapsed;
  191. });
  192. this.element.appendChild(this._headerElement);
  193. this._borderElement = root.ownerDocument!.createElement("div");
  194. this._borderElement.classList.add("frame-box-border");
  195. this.element.appendChild(this._borderElement);
  196. this._headerTextElement = root.ownerDocument!.createElement("div");
  197. this._headerTextElement.classList.add("frame-box-header-title");
  198. this._headerElement.appendChild(this._headerTextElement);
  199. this._headerCollapseElement = root.ownerDocument!.createElement("div");
  200. this._headerCollapseElement.classList.add("frame-box-header-collapse");
  201. this._headerCollapseElement.classList.add("frame-box-header-button");
  202. this._headerCollapseElement.title = "Collapse";
  203. this._headerCollapseElement.ondragstart= () => false;
  204. this._headerCollapseElement.addEventListener("pointerdown", (evt) => {
  205. this._headerCollapseElement.classList.add("down");
  206. evt.stopPropagation();
  207. });
  208. this._headerCollapseElement.addEventListener("pointerup", (evt) => {
  209. evt.stopPropagation();
  210. this._headerCollapseElement.classList.remove("down");
  211. this.isCollapsed = !this.isCollapsed;
  212. if (this.isCollapsed) {
  213. this._headerCollapseElement.innerHTML = this.ExpandSVG;
  214. this._headerCollapseElement.title = "Expand";
  215. } else {
  216. this._headerCollapseElement.innerHTML = this.CollapseSVG;
  217. this._headerCollapseElement.title = "Collapse";
  218. }
  219. });
  220. this._headerCollapseElement.innerHTML = this.CollapseSVG;
  221. this._headerElement.appendChild(this._headerCollapseElement);
  222. this._headerCloseElement = root.ownerDocument!.createElement("div");
  223. this._headerCloseElement.classList.add("frame-box-header-close");
  224. this._headerCloseElement.classList.add("frame-box-header-button");
  225. this._headerCloseElement.title = "Close";
  226. this._headerCloseElement.ondragstart= () => false;
  227. this._headerCloseElement.addEventListener("pointerdown", (evt) => {
  228. evt.stopPropagation();
  229. });
  230. this._headerCloseElement.addEventListener("pointerup", (evt) => {
  231. evt.stopPropagation();
  232. this.dispose();
  233. });
  234. this._headerCloseElement.innerHTML = this.CloseSVG;
  235. this._headerElement.appendChild(this._headerCloseElement);
  236. this._portContainer = root.ownerDocument!.createElement("div");
  237. this._portContainer.classList.add("port-container");
  238. this.element.appendChild(this._portContainer);
  239. this._outputPortContainer = root.ownerDocument!.createElement("div");
  240. this._outputPortContainer.classList.add("outputsContainer");
  241. this._portContainer.appendChild(this._outputPortContainer);
  242. this._inputPortContainer = root.ownerDocument!.createElement("div");
  243. this._inputPortContainer.classList.add("inputsContainer");
  244. this._portContainer.appendChild(this._inputPortContainer);
  245. this.name = "Frame";
  246. this.color = Color3.FromInts(72, 72, 72);
  247. if (candidate) {
  248. this.x = parseFloat(candidate.style.left!.replace("px", ""));
  249. this.y = parseFloat(candidate.style.top!.replace("px", ""));
  250. this.width = parseFloat(candidate.style.width!.replace("px", ""));
  251. this.height = parseFloat(candidate.style.height!.replace("px", ""));
  252. this.cleanAccumulation();
  253. }
  254. this._headerTextElement.addEventListener("pointerdown", evt => this._onDown(evt));
  255. this._headerTextElement.addEventListener("pointerup", evt => this._onUp(evt));
  256. this._headerTextElement.addEventListener("pointermove", evt => this._onMove(evt));
  257. this._onSelectionChangedObserver = canvas.globalState.onSelectionChangedObservable.add(node => {
  258. if (node === this) {
  259. this.element.classList.add("selected");
  260. } else {
  261. this.element.classList.remove("selected");
  262. }
  263. });
  264. // Get nodes
  265. if (!doNotCaptureNodes) {
  266. this.refresh();
  267. }
  268. }
  269. public refresh() {
  270. this._nodes = [];
  271. this._ownerCanvas.globalState.onFrameCreated.notifyObservers(this);
  272. }
  273. public addNode(node: GraphNode) {
  274. let index = this.nodes.indexOf(node);
  275. if (index === -1) {
  276. this.nodes.push(node);
  277. }
  278. }
  279. public removeNode(node: GraphNode) {
  280. let index = this.nodes.indexOf(node);
  281. if (index > -1) {
  282. this.nodes.splice(index, 1);
  283. }
  284. }
  285. public syncNode(node: GraphNode) {
  286. if (this.isCollapsed) {
  287. return;
  288. }
  289. if (node.isOverlappingFrame(this)) {
  290. this.addNode(node);
  291. } else {
  292. this.removeNode(node);
  293. }
  294. }
  295. public cleanAccumulation() {
  296. for (var selectedNode of this._nodes) {
  297. selectedNode.cleanAccumulation();
  298. }
  299. this.x = this._ownerCanvas.getGridPosition(this.x);
  300. this.y = this._ownerCanvas.getGridPosition(this.y);
  301. }
  302. private _onDown(evt: PointerEvent) {
  303. evt.stopPropagation();
  304. this._mouseStartPointX = evt.clientX;
  305. this._mouseStartPointY = evt.clientY;
  306. this._headerTextElement.setPointerCapture(evt.pointerId);
  307. this._ownerCanvas.globalState.onSelectionChangedObservable.notifyObservers(this);
  308. this._ownerCanvas._frameIsMoving = true;
  309. let oldX = this.x;
  310. let oldY = this.y;
  311. this.x = this._ownerCanvas.getGridPosition(this.x);
  312. this.y = this._ownerCanvas.getGridPosition(this.y);
  313. for (var selectedNode of this._nodes) {
  314. selectedNode.x += this.x - oldX;
  315. selectedNode.y += this.y - oldY;
  316. selectedNode.cleanAccumulation(true);
  317. }
  318. }
  319. private _onUp(evt: PointerEvent) {
  320. evt.stopPropagation();
  321. this.cleanAccumulation();
  322. this._mouseStartPointX = null;
  323. this._mouseStartPointY = null;
  324. this._headerTextElement.releasePointerCapture(evt.pointerId);
  325. this._ownerCanvas._frameIsMoving = false;
  326. }
  327. private _moveFrame(offsetX: number, offsetY: number) {
  328. for (var selectedNode of this._nodes) {
  329. selectedNode.x += offsetX;
  330. selectedNode.y += offsetY;
  331. }
  332. this.x += offsetX;
  333. this.y += offsetY;
  334. }
  335. private _onMove(evt: PointerEvent) {
  336. if (this._mouseStartPointX === null || this._mouseStartPointY === null || evt.ctrlKey) {
  337. return;
  338. }
  339. let newX = (evt.clientX - this._mouseStartPointX) / this._ownerCanvas.zoom;
  340. let newY = (evt.clientY - this._mouseStartPointY) / this._ownerCanvas.zoom;
  341. this._moveFrame(newX, newY);
  342. this._mouseStartPointX = evt.clientX;
  343. this._mouseStartPointY = evt.clientY;
  344. evt.stopPropagation();
  345. }
  346. public dispose() {
  347. this.isCollapsed = false;
  348. if (this._onSelectionChangedObserver) {
  349. this._ownerCanvas.globalState.onSelectionChangedObservable.remove(this._onSelectionChangedObserver);
  350. }
  351. this.element.parentElement!.removeChild(this.element);
  352. this._ownerCanvas.frames.splice(this._ownerCanvas.frames.indexOf(this), 1);
  353. }
  354. public serialize(): IFrameData {
  355. return {
  356. x: this._x,
  357. y: this._y,
  358. width: this._width,
  359. height: this._height,
  360. color: this._color.asArray(),
  361. name: this.name,
  362. isCollapsed: this.isCollapsed
  363. }
  364. }
  365. public static Parse(serializationData: IFrameData, canvas: GraphCanvasComponent) {
  366. let newFrame = new GraphFrame(null, canvas, true);
  367. newFrame.x = serializationData.x;
  368. newFrame.y = serializationData.y;
  369. newFrame.width = serializationData.width;
  370. newFrame.height = serializationData.height;
  371. newFrame.name = serializationData.name;
  372. newFrame.color = Color3.FromArray(serializationData.color);
  373. newFrame.refresh();
  374. newFrame.isCollapsed = !!serializationData.isCollapsed;
  375. return newFrame;
  376. }
  377. }