graphEditor.tsx 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. import * as React from "react";
  2. import { GlobalState } from './globalState';
  3. import { NodeMaterialBlock } from 'babylonjs/Materials/Node/nodeMaterialBlock';
  4. import { NodeListComponent } from './components/nodeList/nodeListComponent';
  5. import { PropertyTabComponent } from './components/propertyTab/propertyTabComponent';
  6. import { Portal } from './portal';
  7. import { LogComponent, LogEntry } from './components/log/logComponent';
  8. import { DataStorage } from 'babylonjs/Misc/dataStorage';
  9. import { NodeMaterialBlockConnectionPointTypes } from 'babylonjs/Materials/Node/Enums/nodeMaterialBlockConnectionPointTypes';
  10. import { InputBlock } from 'babylonjs/Materials/Node/Blocks/Input/inputBlock';
  11. import { Nullable } from 'babylonjs/types';
  12. import { MessageDialogComponent } from './sharedComponents/messageDialog';
  13. import { BlockTools } from './blockTools';
  14. import { PreviewManager } from './components/preview/previewManager';
  15. import { IEditorData } from './nodeLocationInfo';
  16. import { PreviewMeshControlComponent } from './components/preview/previewMeshControlComponent';
  17. import { PreviewAreaComponent } from './components/preview/previewAreaComponent';
  18. import { SerializationTools } from './serializationTools';
  19. import { GraphCanvasComponent } from './diagram/graphCanvas';
  20. import { GraphNode } from './diagram/graphNode';
  21. import { GraphFrame } from './diagram/graphFrame';
  22. import * as ReactDOM from 'react-dom';
  23. import { IInspectorOptions } from "babylonjs/Debug/debugLayer";
  24. require("./main.scss");
  25. interface IGraphEditorProps {
  26. globalState: GlobalState;
  27. }
  28. interface IGraphEditorState {
  29. showPreviewPopUp: boolean;
  30. };
  31. interface IInternalPreviewAreaOptions extends IInspectorOptions {
  32. popup: boolean;
  33. original: boolean;
  34. explorerWidth?: string;
  35. inspectorWidth?: string;
  36. embedHostWidth?: string;
  37. }
  38. export class GraphEditor extends React.Component<IGraphEditorProps, IGraphEditorState> {
  39. private readonly NodeWidth = 100;
  40. private _graphCanvas: GraphCanvasComponent;
  41. private _startX: number;
  42. private _moveInProgress: boolean;
  43. private _leftWidth = DataStorage.ReadNumber("LeftWidth", 200);
  44. private _rightWidth = DataStorage.ReadNumber("RightWidth", 300);
  45. private _blocks = new Array<NodeMaterialBlock>();
  46. private _previewManager: PreviewManager;
  47. private _copiedNodes: GraphNode[] = [];
  48. private _copiedFrame: Nullable<GraphFrame> = null;
  49. private _mouseLocationX = 0;
  50. private _mouseLocationY = 0;
  51. private _onWidgetKeyUpPointer: any;
  52. private _previewHost: Nullable<HTMLElement>;
  53. private _popUpWindow: Window;
  54. /**
  55. * Creates a node and recursivly creates its parent nodes from it's input
  56. * @param nodeMaterialBlock
  57. */
  58. public createNodeFromObject(block: NodeMaterialBlock, recursion = true) {
  59. if (this._blocks.indexOf(block) !== -1) {
  60. return this._graphCanvas.nodes.filter(n => n.block === block)[0];
  61. }
  62. this._blocks.push(block);
  63. if (this.props.globalState.nodeMaterial!.attachedBlocks.indexOf(block) === -1) {
  64. this.props.globalState.nodeMaterial!.attachedBlocks.push(block);
  65. }
  66. if (block.isFinalMerger) {
  67. this.props.globalState.nodeMaterial!.addOutputNode(block);
  68. }
  69. // Connections
  70. if (block.inputs.length) {
  71. for (var input of block.inputs) {
  72. if (input.isConnected && recursion) {
  73. this.createNodeFromObject(input.sourceBlock!);
  74. }
  75. }
  76. }
  77. // Graph
  78. const node = this._graphCanvas.appendBlock(block);
  79. // Links
  80. if (block.inputs.length && recursion) {
  81. for (var input of block.inputs) {
  82. if (input.isConnected) {
  83. this._graphCanvas.connectPorts(input.connectedPoint!, input);
  84. }
  85. }
  86. }
  87. return node;
  88. }
  89. addValueNode(type: string) {
  90. let nodeType: NodeMaterialBlockConnectionPointTypes = BlockTools.GetConnectionNodeTypeFromString(type);
  91. let newInputBlock = new InputBlock(type, undefined, nodeType);
  92. return this.createNodeFromObject(newInputBlock);
  93. }
  94. componentDidMount() {
  95. if (this.props.globalState.hostDocument) {
  96. this._graphCanvas = (this.refs["graphCanvas"] as GraphCanvasComponent);
  97. this._previewManager = new PreviewManager(this.props.globalState.hostDocument.getElementById("preview-canvas") as HTMLCanvasElement, this.props.globalState);
  98. }
  99. if (navigator.userAgent.indexOf("Mobile") !== -1) {
  100. ((this.props.globalState.hostDocument || document).querySelector(".blocker") as HTMLElement).style.visibility = "visible";
  101. }
  102. this.build();
  103. }
  104. componentWillUnmount() {
  105. if (this.props.globalState.hostDocument) {
  106. this.props.globalState.hostDocument!.removeEventListener("keyup", this._onWidgetKeyUpPointer, false);
  107. }
  108. if (this._previewManager) {
  109. this._previewManager.dispose();
  110. }
  111. }
  112. constructor(props: IGraphEditorProps) {
  113. super(props);
  114. this.state = {
  115. showPreviewPopUp: false
  116. };
  117. this.props.globalState.onRebuildRequiredObservable.add(() => {
  118. if (this.props.globalState.nodeMaterial) {
  119. this.buildMaterial();
  120. }
  121. });
  122. this.props.globalState.onResetRequiredObservable.add(() => {
  123. this.build();
  124. if (this.props.globalState.nodeMaterial) {
  125. this.buildMaterial();
  126. }
  127. });
  128. this.props.globalState.onZoomToFitRequiredObservable.add(() => {
  129. this.zoomToFit();
  130. });
  131. this.props.globalState.onReOrganizedRequiredObservable.add(() => {
  132. this.reOrganize();
  133. });
  134. this.props.globalState.onGetNodeFromBlock = (block) => {
  135. return this._graphCanvas.findNodeFromBlock(block);
  136. }
  137. this.props.globalState.hostDocument!.addEventListener("keydown", evt => {
  138. if ((evt.keyCode === 46 || evt.keyCode === 8) && !this.props.globalState.blockKeyboardEvents) { // Delete
  139. let selectedItems = this._graphCanvas.selectedNodes;
  140. for (var selectedItem of selectedItems) {
  141. selectedItem.dispose();
  142. let targetBlock = selectedItem.block;
  143. this.props.globalState.nodeMaterial!.removeBlock(targetBlock);
  144. let blockIndex = this._blocks.indexOf(targetBlock);
  145. if (blockIndex > -1) {
  146. this._blocks.splice(blockIndex, 1);
  147. }
  148. }
  149. if (this._graphCanvas.selectedLink) {
  150. this._graphCanvas.selectedLink.dispose();
  151. }
  152. if (this._graphCanvas.selectedFrame) {
  153. this._graphCanvas.selectedFrame.dispose();
  154. }
  155. this.props.globalState.onSelectionChangedObservable.notifyObservers(null);
  156. this.props.globalState.onRebuildRequiredObservable.notifyObservers();
  157. return;
  158. }
  159. if (!evt.ctrlKey || this.props.globalState.blockKeyboardEvents) {
  160. return;
  161. }
  162. if (evt.key === "c") { // Copy
  163. this._copiedNodes = [];
  164. this._copiedFrame = null;
  165. if (this._graphCanvas.selectedFrame) {
  166. this._copiedFrame = this._graphCanvas.selectedFrame;
  167. return;
  168. }
  169. let selectedItems = this._graphCanvas.selectedNodes;
  170. if (!selectedItems.length) {
  171. return;
  172. }
  173. let selectedItem = selectedItems[0] as GraphNode;
  174. if (!selectedItem.block) {
  175. return;
  176. }
  177. this._copiedNodes = selectedItems.slice(0);
  178. } else if (evt.key === "v") { // Paste
  179. const rootElement = this.props.globalState.hostDocument!.querySelector(".diagram-container") as HTMLDivElement;
  180. const zoomLevel = this._graphCanvas.zoom;
  181. let currentY = (this._mouseLocationY - rootElement.offsetTop - this._graphCanvas.y - 20) / zoomLevel;
  182. if (this._copiedFrame) {
  183. // New frame
  184. let newFrame = new GraphFrame(null, this._graphCanvas, true);
  185. this._graphCanvas.frames.push(newFrame);
  186. newFrame.width = this._copiedFrame.width;
  187. newFrame.height = this._copiedFrame.height;newFrame.width / 2
  188. newFrame.name = this._copiedFrame.name;
  189. newFrame.color = this._copiedFrame.color;
  190. let currentX = (this._mouseLocationX - rootElement.offsetLeft - this._graphCanvas.x) / zoomLevel;
  191. newFrame.x = currentX - newFrame.width / 2;
  192. newFrame.y = currentY;
  193. // Paste nodes
  194. if (this._copiedFrame.nodes.length) {
  195. currentX = newFrame.x + this._copiedFrame.nodes[0].x - this._copiedFrame.x;
  196. currentY = newFrame.y + this._copiedFrame.nodes[0].y - this._copiedFrame.y;
  197. this.pasteSelection(this._copiedFrame.nodes, currentX, currentY);
  198. }
  199. if (this._copiedFrame.isCollapsed) {
  200. newFrame.isCollapsed = true;
  201. }
  202. // Select
  203. this.props.globalState.onSelectionChangedObservable.notifyObservers(newFrame);
  204. return;
  205. }
  206. if (!this._copiedNodes.length) {
  207. return;
  208. }
  209. let currentX = (this._mouseLocationX - rootElement.offsetLeft - this._graphCanvas.x - this.NodeWidth) / zoomLevel;
  210. this.pasteSelection(this._copiedNodes, currentX, currentY, true);
  211. }
  212. }, false);
  213. }
  214. reconnectNewNodes(nodeIndex: number, newNodes:GraphNode[], sourceNodes:GraphNode[], done: boolean[]) {
  215. if (done[nodeIndex]) {
  216. return;
  217. }
  218. const currentNode = newNodes[nodeIndex];
  219. const block = currentNode.block;
  220. const sourceNode = sourceNodes[nodeIndex];
  221. for (var inputIndex = 0; inputIndex < sourceNode.block.inputs.length; inputIndex++) {
  222. let sourceInput = sourceNode.block.inputs[inputIndex];
  223. const currentInput = block.inputs[inputIndex];
  224. if (!sourceInput.isConnected) {
  225. continue;
  226. }
  227. const sourceBlock = sourceInput.connectedPoint!.ownerBlock;
  228. const activeNodes = sourceNodes.filter(s => s.block === sourceBlock);
  229. if (activeNodes.length > 0) {
  230. const activeNode = activeNodes[0];
  231. let indexInList = sourceNodes.indexOf(activeNode);
  232. // First make sure to connect the other one
  233. this.reconnectNewNodes(indexInList, newNodes, sourceNodes, done);
  234. // Then reconnect
  235. const outputIndex = sourceBlock.outputs.indexOf(sourceInput.connectedPoint!);
  236. const newOutput = newNodes[indexInList].block.outputs[outputIndex];
  237. newOutput.connectTo(currentInput);
  238. } else {
  239. // Connect with outside blocks
  240. sourceInput._connectedPoint!.connectTo(currentInput);
  241. }
  242. this._graphCanvas.connectPorts(currentInput.connectedPoint!, currentInput);
  243. }
  244. currentNode.refresh();
  245. done[nodeIndex] = true;
  246. }
  247. pasteSelection(copiedNodes: GraphNode[], currentX: number, currentY: number, selectNew = false) {
  248. let originalNode: Nullable<GraphNode> = null;
  249. let newNodes:GraphNode[] = [];
  250. // Copy to prevent recursive side effects while creating nodes.
  251. copiedNodes = copiedNodes.slice();
  252. // Cancel selection
  253. this.props.globalState.onSelectionChangedObservable.notifyObservers(null);
  254. // Create new nodes
  255. for (var node of copiedNodes) {
  256. let block = node.block;
  257. if (!block) {
  258. continue;
  259. }
  260. let clone = block.clone(this.props.globalState.nodeMaterial.getScene());
  261. if (!clone) {
  262. return;
  263. }
  264. let newNode = this.createNodeFromObject(clone, false);
  265. let x = 0;
  266. let y = 0;
  267. if (originalNode) {
  268. x = currentX + node.x - originalNode.x;
  269. y = currentY + node.y - originalNode.y;
  270. } else {
  271. originalNode = node;
  272. x = currentX;
  273. y = currentY;
  274. }
  275. newNode.x = x;
  276. newNode.y = y;
  277. newNode.cleanAccumulation();
  278. newNodes.push(newNode);
  279. if (selectNew) {
  280. this.props.globalState.onSelectionChangedObservable.notifyObservers(newNode);
  281. }
  282. }
  283. // Relink
  284. let done = new Array<boolean>(newNodes.length);
  285. for (var index = 0; index < newNodes.length; index++) {
  286. this.reconnectNewNodes(index, newNodes, copiedNodes, done);
  287. }
  288. }
  289. zoomToFit() {
  290. this._graphCanvas.zoomToFit();
  291. }
  292. buildMaterial() {
  293. if (!this.props.globalState.nodeMaterial) {
  294. return;
  295. }
  296. try {
  297. this.props.globalState.nodeMaterial.options.emitComments = true;
  298. this.props.globalState.nodeMaterial.build(true);
  299. this.props.globalState.onLogRequiredObservable.notifyObservers(new LogEntry("Node material build successful", false));
  300. }
  301. catch (err) {
  302. this.props.globalState.onLogRequiredObservable.notifyObservers(new LogEntry(err, true));
  303. }
  304. SerializationTools.UpdateLocations(this.props.globalState.nodeMaterial, this.props.globalState);
  305. this.props.globalState.onBuiltObservable.notifyObservers();
  306. }
  307. build() {
  308. let editorData = this.props.globalState.nodeMaterial.editorData;
  309. this._graphCanvas._isLoading = true; // Will help loading large graphes
  310. if (editorData instanceof Array) {
  311. editorData = {
  312. locations: editorData
  313. }
  314. }
  315. // setup the diagram model
  316. this._blocks = [];
  317. this._graphCanvas.reset();
  318. // Load graph of nodes from the material
  319. if (this.props.globalState.nodeMaterial) {
  320. var material = this.props.globalState.nodeMaterial;
  321. material._vertexOutputNodes.forEach((n: any) => {
  322. this.createNodeFromObject(n);
  323. });
  324. material._fragmentOutputNodes.forEach((n: any) => {
  325. this.createNodeFromObject(n);
  326. });
  327. material.attachedBlocks.forEach((n: any) => {
  328. this.createNodeFromObject(n);
  329. });
  330. // Links
  331. material.attachedBlocks.forEach((n: any) => {
  332. if (n.inputs.length) {
  333. for (var input of n.inputs) {
  334. if (input.isConnected) {
  335. this._graphCanvas.connectPorts(input.connectedPoint!, input);
  336. }
  337. }
  338. }
  339. });
  340. }
  341. this.reOrganize(editorData);
  342. }
  343. showWaitScreen() {
  344. this.props.globalState.hostDocument.querySelector(".wait-screen")?.classList.remove("hidden");
  345. }
  346. hideWaitScreen() {
  347. this.props.globalState.hostDocument.querySelector(".wait-screen")?.classList.add("hidden");
  348. }
  349. reOrganize(editorData: Nullable<IEditorData> = null) {
  350. this.showWaitScreen();
  351. this._graphCanvas._isLoading = true; // Will help loading large graphes
  352. setTimeout(() => {
  353. if (!editorData || !editorData.locations) {
  354. this._graphCanvas.distributeGraph();
  355. } else {
  356. // Locations
  357. for (var location of editorData.locations) {
  358. for (var node of this._graphCanvas.nodes) {
  359. if (node.block && node.block.uniqueId === location.blockId) {
  360. node.x = location.x;
  361. node.y = location.y;
  362. node.cleanAccumulation();
  363. break;
  364. }
  365. }
  366. }
  367. this._graphCanvas.processEditorData(editorData);
  368. }
  369. this._graphCanvas._isLoading = false;
  370. for (var node of this._graphCanvas.nodes) {
  371. node._refreshLinks();
  372. }
  373. this.hideWaitScreen();
  374. });
  375. }
  376. onPointerDown(evt: React.PointerEvent<HTMLDivElement>) {
  377. this._startX = evt.clientX;
  378. this._moveInProgress = true;
  379. evt.currentTarget.setPointerCapture(evt.pointerId);
  380. }
  381. onPointerUp(evt: React.PointerEvent<HTMLDivElement>) {
  382. this._moveInProgress = false;
  383. evt.currentTarget.releasePointerCapture(evt.pointerId);
  384. }
  385. resizeColumns(evt: React.PointerEvent<HTMLDivElement>, forLeft = true) {
  386. if (!this._moveInProgress) {
  387. return;
  388. }
  389. const deltaX = evt.clientX - this._startX;
  390. const rootElement = evt.currentTarget.ownerDocument!.getElementById("node-editor-graph-root") as HTMLDivElement;
  391. if (forLeft) {
  392. this._leftWidth += deltaX;
  393. this._leftWidth = Math.max(150, Math.min(400, this._leftWidth));
  394. DataStorage.WriteNumber("LeftWidth", this._leftWidth);
  395. } else {
  396. this._rightWidth -= deltaX;
  397. this._rightWidth = Math.max(250, Math.min(500, this._rightWidth));
  398. DataStorage.WriteNumber("RightWidth", this._rightWidth);
  399. rootElement.ownerDocument!.getElementById("preview")!.style.height = this._rightWidth + "px";
  400. }
  401. rootElement.style.gridTemplateColumns = this.buildColumnLayout();
  402. this._startX = evt.clientX;
  403. }
  404. buildColumnLayout() {
  405. return `${this._leftWidth}px 4px calc(100% - ${this._leftWidth + 8 + this._rightWidth}px) 4px ${this._rightWidth}px`;
  406. }
  407. emitNewBlock(event: React.DragEvent<HTMLDivElement>) {
  408. var data = event.dataTransfer.getData("babylonjs-material-node") as string;
  409. let newNode: GraphNode;
  410. if (data.indexOf("Block") === -1) {
  411. newNode = this.addValueNode(data);
  412. } else {
  413. let block = BlockTools.GetBlockFromString(data, this.props.globalState.nodeMaterial.getScene(), this.props.globalState.nodeMaterial)!;
  414. if (block.isUnique) {
  415. const className = block.getClassName();
  416. for (var other of this._blocks) {
  417. if (other !== block && other.getClassName() === className) {
  418. this.props.globalState.onErrorMessageDialogRequiredObservable.notifyObservers(`You can only have one ${className} per graph`);
  419. return;
  420. }
  421. }
  422. }
  423. block.autoConfigure(this.props.globalState.nodeMaterial);
  424. newNode = this.createNodeFromObject(block);
  425. };
  426. let x = event.clientX - event.currentTarget.offsetLeft - this._graphCanvas.x - this.NodeWidth;
  427. let y = event.clientY - event.currentTarget.offsetTop - this._graphCanvas.y - 20;
  428. newNode.x = x / this._graphCanvas.zoom;
  429. newNode.y = y / this._graphCanvas.zoom;
  430. newNode.cleanAccumulation();
  431. this.props.globalState.onSelectionChangedObservable.notifyObservers(null);
  432. this.props.globalState.onSelectionChangedObservable.notifyObservers(newNode);
  433. let block = newNode.block;
  434. x -= this.NodeWidth + 150;
  435. block.inputs.forEach((connection) => {
  436. if (connection.connectedPoint) {
  437. var existingNodes = this._graphCanvas.nodes.filter((n) => { return n.block === (connection as any).connectedPoint.ownerBlock });
  438. let connectedNode = existingNodes[0];
  439. if (connectedNode.x === 0 && connectedNode.y === 0) {
  440. connectedNode.x = x / this._graphCanvas.zoom;
  441. connectedNode.y = y / this._graphCanvas.zoom;
  442. connectedNode.cleanAccumulation();
  443. y += 80;
  444. }
  445. }
  446. });
  447. this.forceUpdate();
  448. }
  449. handlePopUp = () => {
  450. this.setState({
  451. showPreviewPopUp : true
  452. });
  453. this.createPopUp();
  454. this.props.globalState.hostWindow.addEventListener('beforeunload', this.handleClosingPopUp);
  455. }
  456. handleClosingPopUp = () => {
  457. this._previewManager.dispose();
  458. this._popUpWindow.close();
  459. this.setState({
  460. showPreviewPopUp: false
  461. }, () => this.initiatePreviewArea()
  462. );
  463. }
  464. initiatePreviewArea = (canvas: HTMLCanvasElement = this.props.globalState.hostDocument.getElementById("preview-canvas") as HTMLCanvasElement) => {
  465. this._previewManager = new PreviewManager(canvas, this.props.globalState);
  466. }
  467. createPopUp = () => {
  468. const userOptions = {
  469. original: true,
  470. popup: false,
  471. overlay: false,
  472. embedMode: false,
  473. enableClose: true,
  474. handleResize: true,
  475. enablePopup: true,
  476. };
  477. const options = {
  478. embedHostWidth: "100%",
  479. popup: true,
  480. ...userOptions
  481. };
  482. const popUpWindow = this.createPopupWindow("PREVIEW AREA", "_PreviewHostWindow");
  483. if (popUpWindow) {
  484. popUpWindow.addEventListener('beforeunload', this.handleClosingPopUp);
  485. const parentControl = popUpWindow.document.getElementById('node-editor-graph-root');
  486. this.createPreviewMeshControlHost(options, parentControl);
  487. this.createPreviewHost(options, parentControl);
  488. if (parentControl) {
  489. this.fixPopUpStyles(parentControl.ownerDocument!);
  490. this.initiatePreviewArea(parentControl.ownerDocument!.getElementById("preview-canvas") as HTMLCanvasElement);
  491. }
  492. }
  493. }
  494. createPopupWindow = (title: string, windowVariableName: string, width = 500, height = 500): Window | null => {
  495. const windowCreationOptionsList = {
  496. width: width,
  497. height: height,
  498. top: (this.props.globalState.hostWindow.innerHeight - width) / 2 + window.screenY,
  499. left: (this.props.globalState.hostWindow.innerWidth - height) / 2 + window.screenX
  500. };
  501. var windowCreationOptions = Object.keys(windowCreationOptionsList)
  502. .map(
  503. (key) => key + '=' + (windowCreationOptionsList as any)[key]
  504. )
  505. .join(',');
  506. const popupWindow = this.props.globalState.hostWindow.open("", title, windowCreationOptions);
  507. if (!popupWindow) {
  508. return null;
  509. }
  510. const parentDocument = popupWindow.document;
  511. parentDocument.title = title;
  512. parentDocument.body.style.width = "100%";
  513. parentDocument.body.style.height = "100%";
  514. parentDocument.body.style.margin = "0";
  515. parentDocument.body.style.padding = "0";
  516. let parentControl = parentDocument.createElement("div");
  517. parentControl.style.width = "100%";
  518. parentControl.style.height = "100%";
  519. parentControl.style.margin = "0";
  520. parentControl.style.padding = "0";
  521. parentControl.style.display = "grid";
  522. parentControl.style.gridTemplateRows = "40px auto";
  523. parentControl.id = 'node-editor-graph-root';
  524. parentControl.className = 'right-panel';
  525. popupWindow.document.body.appendChild(parentControl);
  526. this.copyStyles(this.props.globalState.hostWindow.document, parentDocument);
  527. (this as any)[windowVariableName] = popupWindow;
  528. this._popUpWindow = popupWindow;
  529. return popupWindow;
  530. }
  531. copyStyles = (sourceDoc: HTMLDocument, targetDoc: HTMLDocument) => {
  532. const styleContainer = [];
  533. for (var index = 0; index < sourceDoc.styleSheets.length; index++) {
  534. var styleSheet: any = sourceDoc.styleSheets[index];
  535. try {
  536. if (styleSheet.href) { // for <link> elements loading CSS from a URL
  537. const newLinkEl = sourceDoc.createElement('link');
  538. newLinkEl.rel = 'stylesheet';
  539. newLinkEl.href = styleSheet.href;
  540. targetDoc.head!.appendChild(newLinkEl);
  541. styleContainer.push(newLinkEl);
  542. }
  543. else if (styleSheet.cssRules) { // for <style> elements
  544. const newStyleEl = sourceDoc.createElement('style');
  545. for (var cssRule of styleSheet.cssRules) {
  546. newStyleEl.appendChild(sourceDoc.createTextNode(cssRule.cssText));
  547. }
  548. targetDoc.head!.appendChild(newStyleEl);
  549. styleContainer.push(newStyleEl);
  550. }
  551. } catch (e) {
  552. console.log(e);
  553. }
  554. }
  555. }
  556. createPreviewMeshControlHost = (options: IInternalPreviewAreaOptions, parentControl: Nullable<HTMLElement>) => {
  557. // Prepare the preview control host
  558. if (parentControl) {
  559. const host = parentControl.ownerDocument!.createElement("div");
  560. host.id = "PreviewMeshControl-host";
  561. host.style.width = options.embedHostWidth || "auto";
  562. parentControl.appendChild(host);
  563. const PreviewMeshControlComponentHost = React.createElement(PreviewMeshControlComponent, {
  564. globalState: this.props.globalState,
  565. togglePreviewAreaComponent: this.handlePopUp
  566. });
  567. ReactDOM.render(PreviewMeshControlComponentHost, host);
  568. }
  569. }
  570. createPreviewHost = (options: IInternalPreviewAreaOptions, parentControl: Nullable<HTMLElement>) => {
  571. // Prepare the preview host
  572. if (parentControl) {
  573. const host = parentControl.ownerDocument!.createElement("div");
  574. host.id = "PreviewAreaComponent-host";
  575. host.style.width = options.embedHostWidth || "auto";
  576. host.style.display = "grid";
  577. host.style.gridRow = '2';
  578. host.style.gridTemplateRows = "auto 40px";
  579. parentControl.appendChild(host);
  580. this._previewHost = host;
  581. if (!options.overlay) {
  582. this._previewHost.style.position = "relative";
  583. }
  584. }
  585. if (this._previewHost) {
  586. const PreviewAreaComponentHost = React.createElement(PreviewAreaComponent, {
  587. globalState: this.props.globalState,
  588. width: 200
  589. });
  590. ReactDOM.render(PreviewAreaComponentHost, this._previewHost);
  591. }
  592. }
  593. fixPopUpStyles = (document: Document) => {
  594. const previewContainer = document.getElementById("preview");
  595. if (previewContainer) {
  596. previewContainer.style.height = "auto";
  597. previewContainer.style.gridRow = "1";
  598. }
  599. const previewConfigBar = document.getElementById("preview-config-bar");
  600. if (previewConfigBar) {
  601. previewConfigBar.style.gridRow = "2";
  602. }
  603. const newWindowButton = document.getElementById('preview-new-window');
  604. if (newWindowButton) {
  605. newWindowButton.style.display = 'none';
  606. }
  607. const previewMeshBar = document.getElementById('preview-mesh-bar');
  608. if (previewMeshBar) {
  609. previewMeshBar.style.gridTemplateColumns = "auto 1fr 40px 40px";
  610. }
  611. }
  612. render() {
  613. return (
  614. <Portal globalState={this.props.globalState}>
  615. <div id="node-editor-graph-root" style={
  616. {
  617. gridTemplateColumns: this.buildColumnLayout()
  618. }}
  619. onMouseMove={evt => {
  620. this._mouseLocationX = evt.pageX;
  621. this._mouseLocationY = evt.pageY;
  622. }}
  623. onMouseDown={(evt) => {
  624. if ((evt.target as HTMLElement).nodeName === "INPUT") {
  625. return;
  626. }
  627. this.props.globalState.blockKeyboardEvents = false;
  628. }}
  629. >
  630. {/* Node creation menu */}
  631. <NodeListComponent globalState={this.props.globalState} />
  632. <div id="leftGrab"
  633. onPointerDown={evt => this.onPointerDown(evt)}
  634. onPointerUp={evt => this.onPointerUp(evt)}
  635. onPointerMove={evt => this.resizeColumns(evt)}
  636. ></div>
  637. {/* The node graph diagram */}
  638. <div className="diagram-container"
  639. onDrop={event => {
  640. this.emitNewBlock(event);
  641. }}
  642. onDragOver={event => {
  643. event.preventDefault();
  644. }}
  645. >
  646. <GraphCanvasComponent ref={"graphCanvas"} globalState={this.props.globalState}/>
  647. </div>
  648. <div id="rightGrab"
  649. onPointerDown={evt => this.onPointerDown(evt)}
  650. onPointerUp={evt => this.onPointerUp(evt)}
  651. onPointerMove={evt => this.resizeColumns(evt, false)}
  652. ></div>
  653. {/* Property tab */}
  654. <div className="right-panel">
  655. <PropertyTabComponent globalState={this.props.globalState} />
  656. {!this.state.showPreviewPopUp ? <PreviewMeshControlComponent globalState={this.props.globalState} togglePreviewAreaComponent={this.handlePopUp} /> : null }
  657. {!this.state.showPreviewPopUp ? <PreviewAreaComponent globalState={this.props.globalState} width={this._rightWidth} /> : null}
  658. </div>
  659. <LogComponent globalState={this.props.globalState} />
  660. </div>
  661. <MessageDialogComponent globalState={this.props.globalState} />
  662. <div className="blocker">
  663. Node Material Editor runs only on desktop
  664. </div>
  665. <div className="wait-screen hidden">
  666. Processing...please wait
  667. </div>
  668. </Portal>
  669. );
  670. }
  671. }