graphNode.ts 16 KB

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