nodeEditor.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import * as React from "react";
  2. import * as ReactDOM from "react-dom";
  3. import { GlobalState } from './globalState';
  4. import { GraphEditor } from './components/graphEditor';
  5. import {NodeMaterial} from "babylonjs"
  6. import {Inspector} from "../../inspector/src"
  7. /**
  8. * Interface used to specify creation options for the node editor
  9. */
  10. export interface INodeEditorOptions {
  11. /**
  12. * Defines the DOM element that will host the node editor
  13. */
  14. hostElement?: HTMLDivElement
  15. nodeMaterial?: NodeMaterial
  16. }
  17. /**
  18. * Class used to create a node editor
  19. */
  20. export class NodeEditor {
  21. /**
  22. * Show the node editor
  23. * @param options defines the options to use to configure the node editor
  24. */
  25. public static Show(options: INodeEditorOptions) {
  26. if(!options.hostElement){
  27. // var divElement = document.createElement("div");
  28. // document.body.prepend(divElement)
  29. // divElement.id = "node-editor";
  30. // divElement.style.background = "#474646"
  31. // divElement.style.width = "100%"
  32. // divElement.style.height = "300px"
  33. // divElement.style.display = "flex"
  34. // options.hostElement = divElement
  35. // debugger;
  36. // var canvas = EngineStore.LastCreatedEngine!.getRenderingCanvas();
  37. // let parentControl = (canvas!.parentElement) as HTMLDivElement;
  38. // Inspector._CreateCanvasContainer(parentControl)
  39. // options.hostElement = parentControl!;//Inspector._CreatePopup("SCENE EXPLORER", "node-editor")!;
  40. options.hostElement = Inspector._CreatePopup("SCENE EXPLORER", "node-editor", 1000, 800)!;
  41. }
  42. let globalState = new GlobalState();
  43. globalState.nodeMaterial = options.nodeMaterial
  44. globalState.hostDocument = options.hostElement.ownerDocument
  45. const graphEditor = React.createElement(GraphEditor, {
  46. globalState: globalState
  47. });
  48. ReactDOM.render(graphEditor, options.hostElement);
  49. // Close the popup window when the page is refreshed or scene is disposed
  50. var popupWindow = (Inspector as any)["node-editor"];
  51. if(globalState.nodeMaterial && popupWindow){
  52. globalState.nodeMaterial.getScene().onDisposeObservable.addOnce(()=>{
  53. if(popupWindow){
  54. popupWindow.close();
  55. }
  56. })
  57. window.onbeforeunload = function(event) {
  58. var popupWindow = (Inspector as any)["node-editor"];
  59. if(popupWindow){
  60. popupWindow.close();
  61. }
  62. };
  63. }
  64. }
  65. }