graphEditor.tsx 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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 './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. return;
  203. }
  204. if (!this._copiedNodes.length) {
  205. return;
  206. }
  207. let currentX = (this._mouseLocationX - rootElement.offsetLeft - this._graphCanvas.x - this.NodeWidth) / zoomLevel;
  208. this.pasteSelection(this._copiedNodes, currentX, currentY);
  209. }
  210. }, false);
  211. }
  212. reconnectNewNodes(nodeIndex: number, newNodes:GraphNode[], sourceNodes:GraphNode[], done: boolean[]) {
  213. if (done[nodeIndex]) {
  214. return;
  215. }
  216. const currentNode = newNodes[nodeIndex];
  217. const block = currentNode.block;
  218. const sourceNode = sourceNodes[nodeIndex];
  219. for (var inputIndex = 0; inputIndex < sourceNode.block.inputs.length; inputIndex++) {
  220. let sourceInput = sourceNode.block.inputs[inputIndex];
  221. const currentInput = block.inputs[inputIndex];
  222. if (!sourceInput.isConnected) {
  223. continue;
  224. }
  225. const sourceBlock = sourceInput.connectedPoint!.ownerBlock;
  226. const activeNodes = sourceNodes.filter(s => s.block === sourceBlock);
  227. if (activeNodes.length > 0) {
  228. const activeNode = activeNodes[0];
  229. let indexInList = sourceNodes.indexOf(activeNode);
  230. // First make sure to connect the other one
  231. this.reconnectNewNodes(indexInList, newNodes, sourceNodes, done);
  232. // Then reconnect
  233. const outputIndex = sourceBlock.outputs.indexOf(sourceInput.connectedPoint!);
  234. const newOutput = newNodes[indexInList].block.outputs[outputIndex];
  235. newOutput.connectTo(currentInput);
  236. } else {
  237. // Connect with outside blocks
  238. sourceInput._connectedPoint!.connectTo(currentInput);
  239. }
  240. this._graphCanvas.connectPorts(currentInput.connectedPoint!, currentInput);
  241. }
  242. currentNode.refresh();
  243. done[nodeIndex] = true;
  244. }
  245. pasteSelection(copiedNodes: GraphNode[], currentX: number, currentY: number) {
  246. let originalNode: Nullable<GraphNode> = null;
  247. let newNodes:GraphNode[] = [];
  248. // Create new nodes
  249. for (var node of copiedNodes) {
  250. let block = node.block;
  251. if (!block) {
  252. continue;
  253. }
  254. let clone = block.clone(this.props.globalState.nodeMaterial.getScene());
  255. if (!clone) {
  256. return;
  257. }
  258. let newNode = this.createNodeFromObject(clone, false);
  259. let x = 0;
  260. let y = 0;
  261. if (originalNode) {
  262. x = currentX + node.x - originalNode.x;
  263. y = currentY + node.y - originalNode.y;
  264. } else {
  265. originalNode = node;
  266. x = currentX;
  267. y = currentY;
  268. }
  269. newNode.x = x;
  270. newNode.y = y;
  271. newNode.cleanAccumulation();
  272. newNodes.push(newNode);
  273. }
  274. // Relink
  275. let done = new Array<boolean>(newNodes.length);
  276. for (var index = 0; index < newNodes.length; index++) {
  277. this.reconnectNewNodes(index, newNodes, copiedNodes, done);
  278. }
  279. }
  280. zoomToFit() {
  281. this._graphCanvas.zoomToFit();
  282. }
  283. buildMaterial() {
  284. if (!this.props.globalState.nodeMaterial) {
  285. return;
  286. }
  287. try {
  288. this.props.globalState.nodeMaterial.options.emitComments = true;
  289. this.props.globalState.nodeMaterial.build(true);
  290. this.props.globalState.onLogRequiredObservable.notifyObservers(new LogEntry("Node material build successful", false));
  291. }
  292. catch (err) {
  293. this.props.globalState.onLogRequiredObservable.notifyObservers(new LogEntry(err, true));
  294. }
  295. SerializationTools.UpdateLocations(this.props.globalState.nodeMaterial, this.props.globalState);
  296. this.props.globalState.onBuiltObservable.notifyObservers();
  297. }
  298. build() {
  299. let editorData = this.props.globalState.nodeMaterial.editorData;
  300. this._graphCanvas._isLoading = true; // Will help loading large graphes
  301. if (editorData instanceof Array) {
  302. editorData = {
  303. locations: editorData
  304. }
  305. }
  306. // setup the diagram model
  307. this._blocks = [];
  308. this._graphCanvas.reset();
  309. // Load graph of nodes from the material
  310. if (this.props.globalState.nodeMaterial) {
  311. var material = this.props.globalState.nodeMaterial;
  312. material._vertexOutputNodes.forEach((n: any) => {
  313. this.createNodeFromObject(n);
  314. });
  315. material._fragmentOutputNodes.forEach((n: any) => {
  316. this.createNodeFromObject(n);
  317. });
  318. material.attachedBlocks.forEach((n: any) => {
  319. this.createNodeFromObject(n);
  320. });
  321. // Links
  322. material.attachedBlocks.forEach((n: any) => {
  323. if (n.inputs.length) {
  324. for (var input of n.inputs) {
  325. if (input.isConnected) {
  326. this._graphCanvas.connectPorts(input.connectedPoint!, input);
  327. }
  328. }
  329. }
  330. });
  331. }
  332. this.reOrganize(editorData);
  333. }
  334. showWaitScreen() {
  335. this.props.globalState.hostDocument.querySelector(".wait-screen")?.classList.remove("hidden");
  336. }
  337. hideWaitScreen() {
  338. this.props.globalState.hostDocument.querySelector(".wait-screen")?.classList.add("hidden");
  339. }
  340. reOrganize(editorData: Nullable<IEditorData> = null) {
  341. this.showWaitScreen();
  342. this._graphCanvas._isLoading = true; // Will help loading large graphes
  343. setTimeout(() => {
  344. if (!editorData || !editorData.locations) {
  345. this._graphCanvas.distributeGraph();
  346. } else {
  347. // Locations
  348. for (var location of editorData.locations) {
  349. for (var node of this._graphCanvas.nodes) {
  350. if (node.block && node.block.uniqueId === location.blockId) {
  351. node.x = location.x;
  352. node.y = location.y;
  353. node.cleanAccumulation();
  354. break;
  355. }
  356. }
  357. }
  358. this._graphCanvas.processEditorData(editorData);
  359. }
  360. this._graphCanvas._isLoading = false;
  361. for (var node of this._graphCanvas.nodes) {
  362. node._refreshLinks();
  363. }
  364. this.hideWaitScreen();
  365. });
  366. }
  367. onPointerDown(evt: React.PointerEvent<HTMLDivElement>) {
  368. this._startX = evt.clientX;
  369. this._moveInProgress = true;
  370. evt.currentTarget.setPointerCapture(evt.pointerId);
  371. }
  372. onPointerUp(evt: React.PointerEvent<HTMLDivElement>) {
  373. this._moveInProgress = false;
  374. evt.currentTarget.releasePointerCapture(evt.pointerId);
  375. }
  376. resizeColumns(evt: React.PointerEvent<HTMLDivElement>, forLeft = true) {
  377. if (!this._moveInProgress) {
  378. return;
  379. }
  380. const deltaX = evt.clientX - this._startX;
  381. const rootElement = evt.currentTarget.ownerDocument!.getElementById("node-editor-graph-root") as HTMLDivElement;
  382. if (forLeft) {
  383. this._leftWidth += deltaX;
  384. this._leftWidth = Math.max(150, Math.min(400, this._leftWidth));
  385. DataStorage.StoreNumber("LeftWidth", this._leftWidth);
  386. } else {
  387. this._rightWidth -= deltaX;
  388. this._rightWidth = Math.max(250, Math.min(500, this._rightWidth));
  389. DataStorage.StoreNumber("RightWidth", this._rightWidth);
  390. rootElement.ownerDocument!.getElementById("preview")!.style.height = this._rightWidth + "px";
  391. }
  392. rootElement.style.gridTemplateColumns = this.buildColumnLayout();
  393. this._startX = evt.clientX;
  394. }
  395. buildColumnLayout() {
  396. return `${this._leftWidth}px 4px calc(100% - ${this._leftWidth + 8 + this._rightWidth}px) 4px ${this._rightWidth}px`;
  397. }
  398. emitNewBlock(event: React.DragEvent<HTMLDivElement>) {
  399. var data = event.dataTransfer.getData("babylonjs-material-node") as string;
  400. let newNode: GraphNode;
  401. if (data.indexOf("Block") === -1) {
  402. newNode = this.addValueNode(data);
  403. } else {
  404. let block = BlockTools.GetBlockFromString(data, this.props.globalState.nodeMaterial.getScene(), this.props.globalState.nodeMaterial)!;
  405. if (block.isUnique) {
  406. const className = block.getClassName();
  407. for (var other of this._blocks) {
  408. if (other !== block && other.getClassName() === className) {
  409. this.props.globalState.onErrorMessageDialogRequiredObservable.notifyObservers(`You can only have one ${className} per graph`);
  410. return;
  411. }
  412. }
  413. }
  414. block.autoConfigure(this.props.globalState.nodeMaterial);
  415. newNode = this.createNodeFromObject(block);
  416. };
  417. let x = event.clientX - event.currentTarget.offsetLeft - this._graphCanvas.x - this.NodeWidth;
  418. let y = event.clientY - event.currentTarget.offsetTop - this._graphCanvas.y - 20;
  419. newNode.x = x / this._graphCanvas.zoom;
  420. newNode.y = y / this._graphCanvas.zoom;
  421. newNode.cleanAccumulation();
  422. this.props.globalState.onSelectionChangedObservable.notifyObservers(null);
  423. this.props.globalState.onSelectionChangedObservable.notifyObservers(newNode);
  424. let block = newNode.block;
  425. x -= this.NodeWidth + 150;
  426. block.inputs.forEach((connection) => {
  427. if (connection.connectedPoint) {
  428. var existingNodes = this._graphCanvas.nodes.filter((n) => { return n.block === (connection as any).connectedPoint.ownerBlock });
  429. let connectedNode = existingNodes[0];
  430. if (connectedNode.x === 0 && connectedNode.y === 0) {
  431. connectedNode.x = x / this._graphCanvas.zoom;
  432. connectedNode.y = y / this._graphCanvas.zoom;
  433. connectedNode.cleanAccumulation();
  434. y += 80;
  435. }
  436. }
  437. });
  438. this.forceUpdate();
  439. }
  440. handlePopUp = () => {
  441. this.setState({
  442. showPreviewPopUp : true
  443. });
  444. this.createPopUp();
  445. this.props.globalState.hostWindow.addEventListener('beforeunload', this.handleClosingPopUp);
  446. }
  447. handleClosingPopUp = () => {
  448. this._previewManager.dispose();
  449. this._popUpWindow.close();
  450. this.setState({
  451. showPreviewPopUp: false
  452. }, () => this.initiatePreviewArea()
  453. );
  454. }
  455. initiatePreviewArea = (canvas: HTMLCanvasElement = this.props.globalState.hostDocument.getElementById("preview-canvas") as HTMLCanvasElement) => {
  456. this._previewManager = new PreviewManager(canvas, this.props.globalState);
  457. }
  458. createPopUp = () => {
  459. const userOptions = {
  460. original: true,
  461. popup: false,
  462. overlay: false,
  463. embedMode: false,
  464. enableClose: true,
  465. handleResize: true,
  466. enablePopup: true,
  467. };
  468. const options = {
  469. embedHostWidth: "100%",
  470. popup: true,
  471. ...userOptions
  472. };
  473. const popUpWindow = this.createPopupWindow("PREVIEW AREA", "_PreviewHostWindow");
  474. if (popUpWindow) {
  475. popUpWindow.addEventListener('beforeunload', this.handleClosingPopUp);
  476. const parentControl = popUpWindow.document.getElementById('node-editor-graph-root');
  477. this.createPreviewMeshControlHost(options, parentControl);
  478. this.createPreviewHost(options, parentControl);
  479. if (parentControl) {
  480. this.fixPopUpStyles(parentControl.ownerDocument!);
  481. this.initiatePreviewArea(parentControl.ownerDocument!.getElementById("preview-canvas") as HTMLCanvasElement);
  482. }
  483. }
  484. }
  485. createPopupWindow = (title: string, windowVariableName: string, width = 500, height = 500): Window | null => {
  486. const windowCreationOptionsList = {
  487. width: width,
  488. height: height,
  489. top: (this.props.globalState.hostWindow.innerHeight - width) / 2 + window.screenY,
  490. left: (this.props.globalState.hostWindow.innerWidth - height) / 2 + window.screenX
  491. };
  492. var windowCreationOptions = Object.keys(windowCreationOptionsList)
  493. .map(
  494. (key) => key + '=' + (windowCreationOptionsList as any)[key]
  495. )
  496. .join(',');
  497. const popupWindow = this.props.globalState.hostWindow.open("", title, windowCreationOptions);
  498. if (!popupWindow) {
  499. return null;
  500. }
  501. const parentDocument = popupWindow.document;
  502. parentDocument.title = title;
  503. parentDocument.body.style.width = "100%";
  504. parentDocument.body.style.height = "100%";
  505. parentDocument.body.style.margin = "0";
  506. parentDocument.body.style.padding = "0";
  507. let parentControl = parentDocument.createElement("div");
  508. parentControl.style.width = "100%";
  509. parentControl.style.height = "100%";
  510. parentControl.style.margin = "0";
  511. parentControl.style.padding = "0";
  512. parentControl.style.display = "grid";
  513. parentControl.style.gridTemplateRows = "40px auto";
  514. parentControl.id = 'node-editor-graph-root';
  515. parentControl.className = 'right-panel';
  516. popupWindow.document.body.appendChild(parentControl);
  517. this.copyStyles(this.props.globalState.hostWindow.document, parentDocument);
  518. (this as any)[windowVariableName] = popupWindow;
  519. this._popUpWindow = popupWindow;
  520. return popupWindow;
  521. }
  522. copyStyles = (sourceDoc: HTMLDocument, targetDoc: HTMLDocument) => {
  523. const styleContainer = [];
  524. for (var index = 0; index < sourceDoc.styleSheets.length; index++) {
  525. var styleSheet: any = sourceDoc.styleSheets[index];
  526. try {
  527. if (styleSheet.href) { // for <link> elements loading CSS from a URL
  528. const newLinkEl = sourceDoc.createElement('link');
  529. newLinkEl.rel = 'stylesheet';
  530. newLinkEl.href = styleSheet.href;
  531. targetDoc.head!.appendChild(newLinkEl);
  532. styleContainer.push(newLinkEl);
  533. }
  534. else if (styleSheet.cssRules) { // for <style> elements
  535. const newStyleEl = sourceDoc.createElement('style');
  536. for (var cssRule of styleSheet.cssRules) {
  537. newStyleEl.appendChild(sourceDoc.createTextNode(cssRule.cssText));
  538. }
  539. targetDoc.head!.appendChild(newStyleEl);
  540. styleContainer.push(newStyleEl);
  541. }
  542. } catch (e) {
  543. console.log(e);
  544. }
  545. }
  546. }
  547. createPreviewMeshControlHost = (options: IInternalPreviewAreaOptions, parentControl: Nullable<HTMLElement>) => {
  548. // Prepare the preview control host
  549. if (parentControl) {
  550. const host = parentControl.ownerDocument!.createElement("div");
  551. host.id = "PreviewMeshControl-host";
  552. host.style.width = options.embedHostWidth || "auto";
  553. parentControl.appendChild(host);
  554. const PreviewMeshControlComponentHost = React.createElement(PreviewMeshControlComponent, {
  555. globalState: this.props.globalState,
  556. togglePreviewAreaComponent: this.handlePopUp
  557. });
  558. ReactDOM.render(PreviewMeshControlComponentHost, host);
  559. }
  560. }
  561. createPreviewHost = (options: IInternalPreviewAreaOptions, parentControl: Nullable<HTMLElement>) => {
  562. // Prepare the preview host
  563. if (parentControl) {
  564. const host = parentControl.ownerDocument!.createElement("div");
  565. host.id = "PreviewAreaComponent-host";
  566. host.style.width = options.embedHostWidth || "auto";
  567. host.style.display = "grid";
  568. host.style.gridRow = '2';
  569. host.style.gridTemplateRows = "auto 40px";
  570. parentControl.appendChild(host);
  571. this._previewHost = host;
  572. if (!options.overlay) {
  573. this._previewHost.style.position = "relative";
  574. }
  575. }
  576. if (this._previewHost) {
  577. const PreviewAreaComponentHost = React.createElement(PreviewAreaComponent, {
  578. globalState: this.props.globalState,
  579. width: 200
  580. });
  581. ReactDOM.render(PreviewAreaComponentHost, this._previewHost);
  582. }
  583. }
  584. fixPopUpStyles = (document: Document) => {
  585. const previewContainer = document.getElementById("preview");
  586. if (previewContainer) {
  587. previewContainer.style.height = "auto";
  588. previewContainer.style.gridRow = "1";
  589. }
  590. const previewConfigBar = document.getElementById("preview-config-bar");
  591. if (previewConfigBar) {
  592. previewConfigBar.style.gridRow = "2";
  593. }
  594. const newWindowButton = document.getElementById('preview-new-window');
  595. if (newWindowButton) {
  596. newWindowButton.style.display = 'none';
  597. }
  598. const previewMeshBar = document.getElementById('preview-mesh-bar');
  599. if (previewMeshBar) {
  600. previewMeshBar.style.gridTemplateColumns = "auto 1fr 40px 40px";
  601. }
  602. }
  603. render() {
  604. return (
  605. <Portal globalState={this.props.globalState}>
  606. <div id="node-editor-graph-root" style={
  607. {
  608. gridTemplateColumns: this.buildColumnLayout()
  609. }}
  610. onMouseMove={evt => {
  611. this._mouseLocationX = evt.pageX;
  612. this._mouseLocationY = evt.pageY;
  613. }}
  614. onMouseDown={(evt) => {
  615. if ((evt.target as HTMLElement).nodeName === "INPUT") {
  616. return;
  617. }
  618. this.props.globalState.blockKeyboardEvents = false;
  619. }}
  620. >
  621. {/* Node creation menu */}
  622. <NodeListComponent globalState={this.props.globalState} />
  623. <div id="leftGrab"
  624. onPointerDown={evt => this.onPointerDown(evt)}
  625. onPointerUp={evt => this.onPointerUp(evt)}
  626. onPointerMove={evt => this.resizeColumns(evt)}
  627. ></div>
  628. {/* The node graph diagram */}
  629. <div className="diagram-container"
  630. onDrop={event => {
  631. this.emitNewBlock(event);
  632. }}
  633. onDragOver={event => {
  634. event.preventDefault();
  635. }}
  636. >
  637. <GraphCanvasComponent ref={"graphCanvas"} globalState={this.props.globalState}/>
  638. </div>
  639. <div id="rightGrab"
  640. onPointerDown={evt => this.onPointerDown(evt)}
  641. onPointerUp={evt => this.onPointerUp(evt)}
  642. onPointerMove={evt => this.resizeColumns(evt, false)}
  643. ></div>
  644. {/* Property tab */}
  645. <div className="right-panel">
  646. <PropertyTabComponent globalState={this.props.globalState} />
  647. {!this.state.showPreviewPopUp ? <PreviewMeshControlComponent globalState={this.props.globalState} togglePreviewAreaComponent={this.handlePopUp} /> : null }
  648. {!this.state.showPreviewPopUp ? <PreviewAreaComponent globalState={this.props.globalState} width={this._rightWidth} /> : null}
  649. </div>
  650. <LogComponent globalState={this.props.globalState} />
  651. </div>
  652. <MessageDialogComponent globalState={this.props.globalState} />
  653. <div className="blocker">
  654. Node Material Editor runs only on desktop
  655. </div>
  656. <div className="wait-screen hidden">
  657. Processing...please wait
  658. </div>
  659. </Portal>
  660. );
  661. }
  662. }