graphNode.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 } 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 { GraphNodeGroup } from './graphNodeGroup';
  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<GraphNode | NodeLink | GraphNodeGroup>>>;
  34. private _onSelectionBoxMovedObserver: Nullable<Observer<ClientRect | DOMRect>>;
  35. private _onGroupAboutToMoveObserver: Nullable<Observer<GraphNodeGroup>>;
  36. private _onUpdateRequiredObserver: Nullable<Observer<void>>;
  37. private _ownerCanvas: GraphCanvasComponent;
  38. private _isSelected: boolean;
  39. private _displayManager: Nullable<IDisplayManager> = null;
  40. public get links() {
  41. return this._links;
  42. }
  43. public get gridAlignedX() {
  44. return this._gridAlignedX;
  45. }
  46. public get gridAlignedY() {
  47. return this._gridAlignedY;
  48. }
  49. public get x() {
  50. return this._x;
  51. }
  52. public set x(value: number) {
  53. if (this._x === value) {
  54. return;
  55. }
  56. this._x = value;
  57. this._gridAlignedX = this._ownerCanvas.getGridPosition(value);
  58. this._visual.style.left = `${this._gridAlignedX}px`;
  59. this._refreshLinks();
  60. }
  61. public get y() {
  62. return this._y;
  63. }
  64. public set y(value: number) {
  65. if (this._y === value) {
  66. return;
  67. }
  68. this._y = value;
  69. this._gridAlignedY = this._ownerCanvas.getGridPosition(value);
  70. this._visual.style.top = `${this._gridAlignedY}px`;
  71. this._refreshLinks();
  72. }
  73. public get width() {
  74. return this._visual.clientWidth;
  75. }
  76. public get height() {
  77. return this._visual.clientHeight;
  78. }
  79. public get id() {
  80. return this.block.uniqueId;
  81. }
  82. public get name() {
  83. return this.block.name;
  84. }
  85. public get isSelected() {
  86. return this._isSelected;
  87. }
  88. public set isSelected(value: boolean) {
  89. if (this._isSelected === value) {
  90. return;
  91. }
  92. this._isSelected = value;
  93. if (!value) {
  94. this._visual.classList.remove("selected");
  95. let indexInSelection = this._ownerCanvas.selectedNodes.indexOf(this);
  96. if (indexInSelection > -1) {
  97. this._ownerCanvas.selectedNodes.splice(indexInSelection, 1);
  98. }
  99. } else {
  100. this._globalState.onSelectionChangedObservable.notifyObservers(this);
  101. }
  102. }
  103. public constructor(public block: NodeMaterialBlock, globalState: GlobalState) {
  104. this._globalState = globalState;
  105. this._onSelectionChangedObserver = this._globalState.onSelectionChangedObservable.add(node => {
  106. if (node === this) {
  107. this._visual.classList.add("selected");
  108. } else {
  109. setTimeout(() => {
  110. if (this._ownerCanvas.selectedNodes.indexOf(this) === -1) {
  111. this._visual.classList.remove("selected");
  112. }
  113. })
  114. }
  115. });
  116. this._onUpdateRequiredObserver = this._globalState.onUpdateRequiredObservable.add(() => {
  117. this.refresh();
  118. });
  119. this._onSelectionBoxMovedObserver = this._globalState.onSelectionBoxMoved.add(rect1 => {
  120. const rect2 = this._visual.getBoundingClientRect();
  121. var overlap = !(rect1.right < rect2.left ||
  122. rect1.left > rect2.right ||
  123. rect1.bottom < rect2.top ||
  124. rect1.top > rect2.bottom);
  125. this.isSelected = overlap;
  126. });
  127. this._onGroupAboutToMoveObserver = this._globalState.onGroupAboutToMove.add(group => {
  128. const rect2 = this._visual.getBoundingClientRect();
  129. const rect1 = group.element.getBoundingClientRect();
  130. var overlap = !(rect1.right < rect2.left ||
  131. rect1.left > rect2.right ||
  132. rect1.bottom < rect2.top ||
  133. rect1.top > rect2.bottom);
  134. if (overlap) {
  135. group.nodes.push(this);
  136. }
  137. });
  138. }
  139. public getPortForConnectionPoint(point: NodeMaterialConnectionPoint) {
  140. for (var port of this._inputPorts) {
  141. let attachedPoint = port.connectionPoint;
  142. if (attachedPoint === point) {
  143. return port;
  144. }
  145. }
  146. for (var port of this._outputPorts) {
  147. let attachedPoint = port.connectionPoint;
  148. if (attachedPoint === point) {
  149. return port;
  150. }
  151. }
  152. return null;
  153. }
  154. public getLinksForConnectionPoint(point: NodeMaterialConnectionPoint) {
  155. return this._links.filter(link => link.portA.connectionPoint === point || link.portB!.connectionPoint === point);
  156. }
  157. private _refreshLinks() {
  158. for (var link of this._links) {
  159. link.update();
  160. }
  161. }
  162. public refresh() {
  163. if (this._displayManager) {
  164. this._header.innerHTML = this._displayManager.getHeaderText(this.block);
  165. this._displayManager.updatePreviewContent(this.block, this._content);
  166. this._visual.style.background = this._displayManager.getBackgroundColor(this.block);
  167. } else {
  168. this._header.innerHTML = this.block.name;
  169. }
  170. for (var port of this._inputPorts) {
  171. port.refresh();
  172. }
  173. for (var port of this._outputPorts) {
  174. port.refresh();
  175. }
  176. this._comments.innerHTML = this.block.comments || "";
  177. this._comments.title = this.block.comments || "";
  178. }
  179. private _appendConnection(connectionPoint: NodeMaterialConnectionPoint, root: HTMLDivElement, displayManager: Nullable<IDisplayManager>) {
  180. let portContainer = root.ownerDocument!.createElement("div");
  181. portContainer.classList.add("portLine");
  182. root.appendChild(portContainer);
  183. if (!displayManager || displayManager.shouldDisplayPortLabels(this.block)) {
  184. let portLabel = root.ownerDocument!.createElement("div");
  185. portLabel.classList.add("label");
  186. portLabel.innerHTML = connectionPoint.name;
  187. portContainer.appendChild(portLabel);
  188. }
  189. return new NodePort(portContainer, connectionPoint, this, this._globalState);
  190. }
  191. private _onDown(evt: PointerEvent) {
  192. // Check if this is coming from the port
  193. if (evt.srcElement && (evt.srcElement as HTMLElement).nodeName === "IMG") {
  194. return;
  195. }
  196. const indexInSelection = this._ownerCanvas.selectedNodes.indexOf(this) ;
  197. if (indexInSelection=== -1) {
  198. this._globalState.onSelectionChangedObservable.notifyObservers(this);
  199. } else if (evt.ctrlKey) {
  200. this.isSelected = false;
  201. }
  202. evt.stopPropagation();
  203. this._mouseStartPointX = evt.clientX;
  204. this._mouseStartPointY = evt.clientY;
  205. this._visual.setPointerCapture(evt.pointerId);
  206. }
  207. public cleanAccumulation() {
  208. this.x = this.gridAlignedX;
  209. this.y = this.gridAlignedY;
  210. }
  211. private _onUp(evt: PointerEvent) {
  212. evt.stopPropagation();
  213. for (var selectedNode of this._ownerCanvas.selectedNodes) {
  214. selectedNode.cleanAccumulation();
  215. }
  216. this._mouseStartPointX = null;
  217. this._mouseStartPointY = null;
  218. this._visual.releasePointerCapture(evt.pointerId);
  219. }
  220. private _onMove(evt: PointerEvent) {
  221. if (this._mouseStartPointX === null || this._mouseStartPointY === null || evt.ctrlKey) {
  222. return;
  223. }
  224. let newX = (evt.clientX - this._mouseStartPointX) / this._ownerCanvas.zoom;
  225. let newY = (evt.clientY - this._mouseStartPointY) / this._ownerCanvas.zoom;
  226. for (var selectedNode of this._ownerCanvas.selectedNodes) {
  227. selectedNode.x += newX;
  228. selectedNode.y += newY;
  229. }
  230. this._mouseStartPointX = evt.clientX;
  231. this._mouseStartPointY = evt.clientY;
  232. evt.stopPropagation();
  233. }
  234. public renderProperties(): Nullable<JSX.Element> {
  235. let control = PropertyLedger.RegisteredControls[this.block.getClassName()];
  236. if (!control) {
  237. control = GenericPropertyTabComponent;
  238. }
  239. return React.createElement(control, {
  240. globalState: this._globalState,
  241. block: this.block
  242. });
  243. }
  244. public appendVisual(root: HTMLDivElement, owner: GraphCanvasComponent) {
  245. this._ownerCanvas = owner;
  246. // Display manager
  247. let displayManagerClass = DisplayLedger.RegisteredControls[this.block.getClassName()];
  248. if (displayManagerClass) {
  249. this._displayManager = new displayManagerClass();
  250. }
  251. // DOM
  252. this._visual = root.ownerDocument!.createElement("div");
  253. this._visual.classList.add("visual");
  254. this._visual.addEventListener("pointerdown", evt => this._onDown(evt));
  255. this._visual.addEventListener("pointerup", evt => this._onUp(evt));
  256. this._visual.addEventListener("pointermove", evt => this._onMove(evt));
  257. this._header = root.ownerDocument!.createElement("div");
  258. this._header.classList.add("header");
  259. this._visual.appendChild(this._header);
  260. if (this._displayManager) {
  261. let additionalClass = this._displayManager.getHeaderClass(this.block);
  262. if (additionalClass) {
  263. this._header.classList.add(additionalClass);
  264. }
  265. }
  266. this._connections = root.ownerDocument!.createElement("div");
  267. this._connections.classList.add("connections");
  268. this._visual.appendChild(this._connections);
  269. this._inputsContainer = root.ownerDocument!.createElement("div");
  270. this._inputsContainer.classList.add("inputsContainer");
  271. this._connections.appendChild(this._inputsContainer);
  272. this._outputsContainer = root.ownerDocument!.createElement("div");
  273. this._outputsContainer.classList.add("outputsContainer");
  274. this._connections.appendChild(this._outputsContainer);
  275. this._content = root.ownerDocument!.createElement("div");
  276. this._content.classList.add("content");
  277. this._visual.appendChild(this._content);
  278. var selectionBorder = root.ownerDocument!.createElement("div");
  279. selectionBorder.classList.add("selection-border");
  280. this._visual.appendChild(selectionBorder);
  281. root.appendChild(this._visual);
  282. // Comments
  283. this._comments = root.ownerDocument!.createElement("div");
  284. this._comments.classList.add("comments");
  285. this._visual.appendChild(this._comments);
  286. // Connections
  287. for (var input of this.block.inputs) {
  288. this._inputPorts.push(this._appendConnection(input, this._inputsContainer, this._displayManager));
  289. }
  290. for (var output of this.block.outputs) {
  291. this._outputPorts.push(this._appendConnection(output, this._outputsContainer, this._displayManager));
  292. }
  293. this.refresh();
  294. }
  295. public dispose() {
  296. if (this._onSelectionChangedObserver) {
  297. this._globalState.onSelectionChangedObservable.remove(this._onSelectionChangedObserver);
  298. }
  299. if (this._onUpdateRequiredObserver) {
  300. this._globalState.onUpdateRequiredObservable.remove(this._onUpdateRequiredObserver);
  301. }
  302. if (this._onSelectionBoxMovedObserver) {
  303. this._globalState.onSelectionBoxMoved.remove(this._onSelectionBoxMovedObserver);
  304. }
  305. if (this._visual.parentElement) {
  306. this._visual.parentElement.removeChild(this._visual);
  307. }
  308. if (this._onGroupAboutToMoveObserver) {
  309. this._globalState.onGroupAboutToMove.remove(this._onGroupAboutToMoveObserver);
  310. }
  311. for (var port of this._inputPorts) {
  312. port.dispose();
  313. }
  314. for (var port of this._outputPorts) {
  315. port.dispose();
  316. }
  317. let links = this._links.slice(0);
  318. for (var link of links) {
  319. link.dispose();
  320. }
  321. this.block.dispose();
  322. }
  323. }