graphNode.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. private _isEnclosedInAFrame: boolean;
  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 isEnclosedInAFrame() {
  122. return this._isEnclosedInAFrame;
  123. }
  124. public set isEnclosedInAFrame(value: boolean) {
  125. this._isEnclosedInAFrame = 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. this._isEnclosedInAFrame = isOverlappingFrame;
  186. return isOverlappingFrame;
  187. }
  188. public getPortForConnectionPoint(point: NodeMaterialConnectionPoint) {
  189. for (var port of this._inputPorts) {
  190. let attachedPoint = port.connectionPoint;
  191. if (attachedPoint === point) {
  192. return port;
  193. }
  194. }
  195. for (var port of this._outputPorts) {
  196. let attachedPoint = port.connectionPoint;
  197. if (attachedPoint === point) {
  198. return port;
  199. }
  200. }
  201. return null;
  202. }
  203. public getLinksForConnectionPoint(point: NodeMaterialConnectionPoint) {
  204. return this._links.filter(link => link.portA.connectionPoint === point || link.portB!.connectionPoint === point);
  205. }
  206. private _refreshFrames() {
  207. if (this._ownerCanvas._frameIsMoving || this._ownerCanvas._isLoading) {
  208. return;
  209. }
  210. // Frames
  211. for (var frame of this._ownerCanvas.frames) {
  212. frame.syncNode(this);
  213. }
  214. }
  215. public _refreshLinks() {
  216. if (this._ownerCanvas._isLoading) {
  217. return;
  218. }
  219. for (var link of this._links) {
  220. link.update();
  221. }
  222. }
  223. public refresh() {
  224. if (this._displayManager) {
  225. this._header.innerHTML = this._displayManager.getHeaderText(this.block);
  226. this._displayManager.updatePreviewContent(this.block, this._content);
  227. this._visual.style.background = this._displayManager.getBackgroundColor(this.block);
  228. let additionalClass = this._displayManager.getHeaderClass(this.block);
  229. this._header.classList.value = "header";
  230. if (additionalClass) {
  231. this._header.classList.add(additionalClass);
  232. }
  233. } else {
  234. this._header.innerHTML = this.block.name;
  235. }
  236. for (var port of this._inputPorts) {
  237. port.refresh();
  238. }
  239. for (var port of this._outputPorts) {
  240. port.refresh();
  241. }
  242. this._comments.innerHTML = this.block.comments || "";
  243. this._comments.title = this.block.comments || "";
  244. }
  245. private _onDown(evt: PointerEvent) {
  246. // Check if this is coming from the port
  247. if (evt.srcElement && (evt.srcElement as HTMLElement).nodeName === "IMG") {
  248. return;
  249. }
  250. const indexInSelection = this._ownerCanvas.selectedNodes.indexOf(this) ;
  251. if (indexInSelection=== -1) {
  252. this._globalState.onSelectionChangedObservable.notifyObservers(this);
  253. } else if (evt.ctrlKey) {
  254. this.isSelected = false;
  255. }
  256. evt.stopPropagation();
  257. for (var selectedNode of this._ownerCanvas.selectedNodes) {
  258. selectedNode.cleanAccumulation();
  259. }
  260. this._mouseStartPointX = evt.clientX;
  261. this._mouseStartPointY = evt.clientY;
  262. this._visual.setPointerCapture(evt.pointerId);
  263. }
  264. public cleanAccumulation(useCeil = false) {
  265. this.x = this._ownerCanvas.getGridPosition(this.x, useCeil);
  266. this.y = this._ownerCanvas.getGridPosition(this.y, useCeil);
  267. }
  268. private _onUp(evt: PointerEvent) {
  269. evt.stopPropagation();
  270. for (var selectedNode of this._ownerCanvas.selectedNodes) {
  271. selectedNode.cleanAccumulation();
  272. }
  273. this._mouseStartPointX = null;
  274. this._mouseStartPointY = null;
  275. this._visual.releasePointerCapture(evt.pointerId);
  276. }
  277. private _onMove(evt: PointerEvent) {
  278. if (this._mouseStartPointX === null || this._mouseStartPointY === null || evt.ctrlKey) {
  279. return;
  280. }
  281. let newX = (evt.clientX - this._mouseStartPointX) / this._ownerCanvas.zoom;
  282. let newY = (evt.clientY - this._mouseStartPointY) / this._ownerCanvas.zoom;
  283. for (var selectedNode of this._ownerCanvas.selectedNodes) {
  284. selectedNode.x += newX;
  285. selectedNode.y += newY;
  286. }
  287. this._mouseStartPointX = evt.clientX;
  288. this._mouseStartPointY = evt.clientY;
  289. evt.stopPropagation();
  290. }
  291. public renderProperties(): Nullable<JSX.Element> {
  292. let control = PropertyLedger.RegisteredControls[this.block.getClassName()];
  293. if (!control) {
  294. control = GenericPropertyTabComponent;
  295. }
  296. return React.createElement(control, {
  297. globalState: this._globalState,
  298. block: this.block
  299. });
  300. }
  301. public appendVisual(root: HTMLDivElement, owner: GraphCanvasComponent) {
  302. this._ownerCanvas = owner;
  303. // Display manager
  304. let displayManagerClass = DisplayLedger.RegisteredControls[this.block.getClassName()];
  305. if (displayManagerClass) {
  306. this._displayManager = new displayManagerClass();
  307. }
  308. // DOM
  309. this._visual = root.ownerDocument!.createElement("div");
  310. this._visual.classList.add("visual");
  311. this._visual.addEventListener("pointerdown", evt => this._onDown(evt));
  312. this._visual.addEventListener("pointerup", evt => this._onUp(evt));
  313. this._visual.addEventListener("pointermove", evt => this._onMove(evt));
  314. this._header = root.ownerDocument!.createElement("div");
  315. this._header.classList.add("header");
  316. this._visual.appendChild(this._header);
  317. this._connections = root.ownerDocument!.createElement("div");
  318. this._connections.classList.add("connections");
  319. this._visual.appendChild(this._connections);
  320. this._inputsContainer = root.ownerDocument!.createElement("div");
  321. this._inputsContainer.classList.add("inputsContainer");
  322. this._connections.appendChild(this._inputsContainer);
  323. this._outputsContainer = root.ownerDocument!.createElement("div");
  324. this._outputsContainer.classList.add("outputsContainer");
  325. this._connections.appendChild(this._outputsContainer);
  326. this._content = root.ownerDocument!.createElement("div");
  327. this._content.classList.add("content");
  328. this._visual.appendChild(this._content);
  329. var selectionBorder = root.ownerDocument!.createElement("div");
  330. selectionBorder.classList.add("selection-border");
  331. this._visual.appendChild(selectionBorder);
  332. root.appendChild(this._visual);
  333. // Comments
  334. this._comments = root.ownerDocument!.createElement("div");
  335. this._comments.classList.add("comments");
  336. this._visual.appendChild(this._comments);
  337. // Connections
  338. for (var input of this.block.inputs) {
  339. this._inputPorts.push(NodePort.CreatePortElement(input, this, this._inputsContainer, this._displayManager, this._globalState));
  340. }
  341. for (var output of this.block.outputs) {
  342. this._outputPorts.push(NodePort.CreatePortElement(output, this, this._outputsContainer, this._displayManager, this._globalState));
  343. }
  344. this.refresh();
  345. }
  346. public dispose() {
  347. // notify frame observers that this node is being deleted
  348. this._globalState.onGraphNodeRemovalObservable.notifyObservers(this);
  349. if (this._onSelectionChangedObserver) {
  350. this._globalState.onSelectionChangedObservable.remove(this._onSelectionChangedObserver);
  351. }
  352. if (this._onUpdateRequiredObserver) {
  353. this._globalState.onUpdateRequiredObservable.remove(this._onUpdateRequiredObserver);
  354. }
  355. if (this._onSelectionBoxMovedObserver) {
  356. this._globalState.onSelectionBoxMoved.remove(this._onSelectionBoxMovedObserver);
  357. }
  358. if (this._visual.parentElement) {
  359. this._visual.parentElement.removeChild(this._visual);
  360. }
  361. if (this._onFrameCreatedObserver) {
  362. this._globalState.onFrameCreatedObservable.remove(this._onFrameCreatedObserver);
  363. }
  364. for (var port of this._inputPorts) {
  365. port.dispose();
  366. }
  367. for (var port of this._outputPorts) {
  368. port.dispose();
  369. }
  370. let links = this._links.slice(0);
  371. for (var link of links) {
  372. link.dispose();
  373. }
  374. this.block.dispose();
  375. }
  376. }