graphNode.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. import { NodeMaterialBlock } from 'babylonjs/Materials/Node/nodeMaterialBlock';
  2. import { GlobalState } from '../globalState';
  3. import { Nullable } from 'babylonjs/types';
  4. import { Observer } from 'babylonjs/Misc/observable';
  5. import { NodeMaterialConnectionPoint } from 'babylonjs/Materials/Node/nodeMaterialBlockConnectionPoint';
  6. import { GraphCanvasComponent, FramePortData } from './graphCanvas';
  7. import { PropertyLedger } from './propertyLedger';
  8. import * as React from 'react';
  9. import { GenericPropertyTabComponent } from './properties/genericNodePropertyComponent';
  10. import { DisplayLedger } from './displayLedger';
  11. import { IDisplayManager } from './display/displayManager';
  12. import { NodeLink } from './nodeLink';
  13. import { NodePort } from './nodePort';
  14. import { GraphFrame } from './graphFrame';
  15. export class GraphNode {
  16. private _visual: HTMLDivElement;
  17. private _header: HTMLDivElement;
  18. private _connections: HTMLDivElement;
  19. private _inputsContainer: HTMLDivElement;
  20. private _outputsContainer: HTMLDivElement;
  21. private _content: HTMLDivElement;
  22. private _comments: HTMLDivElement;
  23. private _inputPorts: NodePort[] = [];
  24. private _outputPorts: NodePort[] = [];
  25. private _links: NodeLink[] = [];
  26. private _x = 0;
  27. private _y = 0;
  28. private _gridAlignedX = 0;
  29. private _gridAlignedY = 0;
  30. private _mouseStartPointX: Nullable<number> = null;
  31. private _mouseStartPointY: Nullable<number> = null
  32. private _globalState: GlobalState;
  33. private _onSelectionChangedObserver: Nullable<Observer<Nullable<GraphFrame | GraphNode | NodeLink | NodePort | FramePortData>>>;
  34. private _onSelectionBoxMovedObserver: Nullable<Observer<ClientRect | DOMRect>>;
  35. private _onFrameCreatedObserver: Nullable<Observer<GraphFrame>>;
  36. private _onUpdateRequiredObserver: Nullable<Observer<void>>;
  37. private _ownerCanvas: GraphCanvasComponent;
  38. private _isSelected: boolean;
  39. private _displayManager: Nullable<IDisplayManager> = null;
  40. private _isVisible = true;
  41. public get isVisible() {
  42. return this._isVisible;
  43. }
  44. public set isVisible(value: boolean) {
  45. this._isVisible = value;
  46. if (!value) {
  47. this._visual.classList.add("hidden");
  48. } else {
  49. this._visual.classList.remove("hidden");
  50. }
  51. for (var link of this._links) {
  52. link.isVisible = value;
  53. }
  54. this._refreshLinks();
  55. }
  56. public get outputPorts() {
  57. return this._outputPorts;
  58. }
  59. public get inputPorts() {
  60. return this._inputPorts;
  61. }
  62. public get links() {
  63. return this._links;
  64. }
  65. public get gridAlignedX() {
  66. return this._gridAlignedX;
  67. }
  68. public get gridAlignedY() {
  69. return this._gridAlignedY;
  70. }
  71. public get x() {
  72. return this._x;
  73. }
  74. public set x(value: number) {
  75. if (this._x === value) {
  76. return;
  77. }
  78. this._x = value;
  79. this._gridAlignedX = this._ownerCanvas.getGridPosition(value);
  80. this._visual.style.left = `${this._gridAlignedX}px`;
  81. this._refreshLinks();
  82. this._refreshFrames();
  83. }
  84. public get y() {
  85. return this._y;
  86. }
  87. public set y(value: number) {
  88. if (this._y === value) {
  89. return;
  90. }
  91. this._y = value;
  92. this._gridAlignedY = this._ownerCanvas.getGridPosition(value);
  93. this._visual.style.top = `${this._gridAlignedY}px`;
  94. this._refreshLinks();
  95. this._refreshFrames();
  96. }
  97. public get width() {
  98. return this._visual.clientWidth;
  99. }
  100. public get height() {
  101. return this._visual.clientHeight;
  102. }
  103. public get id() {
  104. return this.block.uniqueId;
  105. }
  106. public get name() {
  107. return this.block.name;
  108. }
  109. public get isSelected() {
  110. return this._isSelected;
  111. }
  112. public set isSelected(value: boolean) {
  113. if (this._isSelected === value) {
  114. return;
  115. }
  116. this._isSelected = value;
  117. if (!value) {
  118. this._visual.classList.remove("selected");
  119. let indexInSelection = this._ownerCanvas.selectedNodes.indexOf(this);
  120. if (indexInSelection > -1) {
  121. this._ownerCanvas.selectedNodes.splice(indexInSelection, 1);
  122. }
  123. } else {
  124. this._globalState.onSelectionChangedObservable.notifyObservers(this);
  125. }
  126. }
  127. public constructor(public block: NodeMaterialBlock, globalState: GlobalState) {
  128. this._globalState = globalState;
  129. this._onSelectionChangedObserver = this._globalState.onSelectionChangedObservable.add(node => {
  130. if (node === this) {
  131. this._visual.classList.add("selected");
  132. } else {
  133. setTimeout(() => {
  134. if (this._ownerCanvas.selectedNodes.indexOf(this) === -1) {
  135. this._visual.classList.remove("selected");
  136. }
  137. })
  138. }
  139. });
  140. this._onUpdateRequiredObserver = this._globalState.onUpdateRequiredObservable.add(() => {
  141. this.refresh();
  142. });
  143. this._onSelectionBoxMovedObserver = this._globalState.onSelectionBoxMoved.add(rect1 => {
  144. const rect2 = this._visual.getBoundingClientRect();
  145. var overlap = !(rect1.right < rect2.left ||
  146. rect1.left > rect2.right ||
  147. rect1.bottom < rect2.top ||
  148. rect1.top > rect2.bottom);
  149. this.isSelected = overlap;
  150. });
  151. this._onFrameCreatedObserver = this._globalState.onFrameCreatedObservable.add(frame => {
  152. if (this._ownerCanvas.frames.some(f => f.nodes.indexOf(this) !== -1)) {
  153. return;
  154. }
  155. if (this.isOverlappingFrame(frame)) {
  156. frame.nodes.push(this);
  157. }
  158. });
  159. }
  160. public isOverlappingFrame(frame: GraphFrame) {
  161. const rect2 = this._visual.getBoundingClientRect();
  162. const rect1 = frame.element.getBoundingClientRect();
  163. // Add a tiny margin
  164. rect1.width -= 5;
  165. rect1.height -= 5;
  166. return !(rect1.right < rect2.left ||
  167. rect1.left > rect2.right ||
  168. rect1.bottom < rect2.top ||
  169. rect1.top > rect2.bottom);
  170. }
  171. public getPortForConnectionPoint(point: NodeMaterialConnectionPoint) {
  172. for (var port of this._inputPorts) {
  173. let attachedPoint = port.connectionPoint;
  174. if (attachedPoint === point) {
  175. return port;
  176. }
  177. }
  178. for (var port of this._outputPorts) {
  179. let attachedPoint = port.connectionPoint;
  180. if (attachedPoint === point) {
  181. return port;
  182. }
  183. }
  184. return null;
  185. }
  186. public getLinksForConnectionPoint(point: NodeMaterialConnectionPoint) {
  187. return this._links.filter(link => link.portA.connectionPoint === point || link.portB!.connectionPoint === point);
  188. }
  189. private _refreshFrames() {
  190. if (this._ownerCanvas._frameIsMoving || this._ownerCanvas._isLoading) {
  191. return;
  192. }
  193. // Frames
  194. for (var frame of this._ownerCanvas.frames) {
  195. frame.syncNode(this);
  196. }
  197. }
  198. public _refreshLinks() {
  199. if (this._ownerCanvas._isLoading) {
  200. return;
  201. }
  202. for (var link of this._links) {
  203. link.update();
  204. }
  205. }
  206. public refresh() {
  207. if (this._displayManager) {
  208. this._header.innerHTML = this._displayManager.getHeaderText(this.block);
  209. this._displayManager.updatePreviewContent(this.block, this._content);
  210. this._visual.style.background = this._displayManager.getBackgroundColor(this.block);
  211. let additionalClass = this._displayManager.getHeaderClass(this.block);
  212. this._header.classList.value = "header";
  213. if (additionalClass) {
  214. this._header.classList.add(additionalClass);
  215. }
  216. } else {
  217. this._header.innerHTML = this.block.name;
  218. }
  219. for (var port of this._inputPorts) {
  220. port.refresh();
  221. }
  222. for (var port of this._outputPorts) {
  223. port.refresh();
  224. }
  225. this._comments.innerHTML = this.block.comments || "";
  226. this._comments.title = this.block.comments || "";
  227. }
  228. private _onDown(evt: PointerEvent) {
  229. // Check if this is coming from the port
  230. if (evt.srcElement && (evt.srcElement as HTMLElement).nodeName === "IMG") {
  231. return;
  232. }
  233. const indexInSelection = this._ownerCanvas.selectedNodes.indexOf(this) ;
  234. if (indexInSelection=== -1) {
  235. this._globalState.onSelectionChangedObservable.notifyObservers(this);
  236. } else if (evt.ctrlKey) {
  237. this.isSelected = false;
  238. }
  239. evt.stopPropagation();
  240. for (var selectedNode of this._ownerCanvas.selectedNodes) {
  241. selectedNode.cleanAccumulation();
  242. }
  243. this._mouseStartPointX = evt.clientX;
  244. this._mouseStartPointY = evt.clientY;
  245. this._visual.setPointerCapture(evt.pointerId);
  246. }
  247. public cleanAccumulation(useCeil = false) {
  248. this.x = this._ownerCanvas.getGridPosition(this.x, useCeil);
  249. this.y = this._ownerCanvas.getGridPosition(this.y, useCeil);
  250. }
  251. private _onUp(evt: PointerEvent) {
  252. evt.stopPropagation();
  253. for (var selectedNode of this._ownerCanvas.selectedNodes) {
  254. selectedNode.cleanAccumulation();
  255. }
  256. this._mouseStartPointX = null;
  257. this._mouseStartPointY = null;
  258. this._visual.releasePointerCapture(evt.pointerId);
  259. }
  260. private _onMove(evt: PointerEvent) {
  261. if (this._mouseStartPointX === null || this._mouseStartPointY === null || evt.ctrlKey) {
  262. return;
  263. }
  264. let newX = (evt.clientX - this._mouseStartPointX) / this._ownerCanvas.zoom;
  265. let newY = (evt.clientY - this._mouseStartPointY) / this._ownerCanvas.zoom;
  266. for (var selectedNode of this._ownerCanvas.selectedNodes) {
  267. selectedNode.x += newX;
  268. selectedNode.y += newY;
  269. }
  270. this._mouseStartPointX = evt.clientX;
  271. this._mouseStartPointY = evt.clientY;
  272. evt.stopPropagation();
  273. }
  274. public renderProperties(): Nullable<JSX.Element> {
  275. let control = PropertyLedger.RegisteredControls[this.block.getClassName()];
  276. if (!control) {
  277. control = GenericPropertyTabComponent;
  278. }
  279. return React.createElement(control, {
  280. globalState: this._globalState,
  281. block: this.block
  282. });
  283. }
  284. public appendVisual(root: HTMLDivElement, owner: GraphCanvasComponent) {
  285. this._ownerCanvas = owner;
  286. // Display manager
  287. let displayManagerClass = DisplayLedger.RegisteredControls[this.block.getClassName()];
  288. if (displayManagerClass) {
  289. this._displayManager = new displayManagerClass();
  290. }
  291. // DOM
  292. this._visual = root.ownerDocument!.createElement("div");
  293. this._visual.classList.add("visual");
  294. this._visual.addEventListener("pointerdown", evt => this._onDown(evt));
  295. this._visual.addEventListener("pointerup", evt => this._onUp(evt));
  296. this._visual.addEventListener("pointermove", evt => this._onMove(evt));
  297. this._header = root.ownerDocument!.createElement("div");
  298. this._header.classList.add("header");
  299. this._visual.appendChild(this._header);
  300. this._connections = root.ownerDocument!.createElement("div");
  301. this._connections.classList.add("connections");
  302. this._visual.appendChild(this._connections);
  303. this._inputsContainer = root.ownerDocument!.createElement("div");
  304. this._inputsContainer.classList.add("inputsContainer");
  305. this._connections.appendChild(this._inputsContainer);
  306. this._outputsContainer = root.ownerDocument!.createElement("div");
  307. this._outputsContainer.classList.add("outputsContainer");
  308. this._connections.appendChild(this._outputsContainer);
  309. this._content = root.ownerDocument!.createElement("div");
  310. this._content.classList.add("content");
  311. this._visual.appendChild(this._content);
  312. var selectionBorder = root.ownerDocument!.createElement("div");
  313. selectionBorder.classList.add("selection-border");
  314. this._visual.appendChild(selectionBorder);
  315. root.appendChild(this._visual);
  316. // Comments
  317. this._comments = root.ownerDocument!.createElement("div");
  318. this._comments.classList.add("comments");
  319. this._visual.appendChild(this._comments);
  320. // Connections
  321. for (var input of this.block.inputs) {
  322. this._inputPorts.push(NodePort.CreatePortElement(input, this, this._inputsContainer, this._displayManager, this._globalState));
  323. }
  324. for (var output of this.block.outputs) {
  325. this._outputPorts.push(NodePort.CreatePortElement(output, this, this._outputsContainer, this._displayManager, this._globalState));
  326. }
  327. this.refresh();
  328. }
  329. public dispose() {
  330. // notify frame observers that this node is being deleted
  331. this._globalState.onGraphNodeRemovalObservable.notifyObservers(this);
  332. if (this._onSelectionChangedObserver) {
  333. this._globalState.onSelectionChangedObservable.remove(this._onSelectionChangedObserver);
  334. }
  335. if (this._onUpdateRequiredObserver) {
  336. this._globalState.onUpdateRequiredObservable.remove(this._onUpdateRequiredObserver);
  337. }
  338. if (this._onSelectionBoxMovedObserver) {
  339. this._globalState.onSelectionBoxMoved.remove(this._onSelectionBoxMovedObserver);
  340. }
  341. if (this._visual.parentElement) {
  342. this._visual.parentElement.removeChild(this._visual);
  343. }
  344. if (this._onFrameCreatedObserver) {
  345. this._globalState.onFrameCreatedObservable.remove(this._onFrameCreatedObserver);
  346. }
  347. for (var port of this._inputPorts) {
  348. port.dispose();
  349. }
  350. for (var port of this._outputPorts) {
  351. port.dispose();
  352. }
  353. let links = this._links.slice(0);
  354. for (var link of links) {
  355. link.dispose();
  356. }
  357. this.block.dispose();
  358. }
  359. }