nodeEditor.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import * as React from "react";
  2. import * as ReactDOM from "react-dom";
  3. import { GlobalState } from './globalState';
  4. import { GraphEditor } from './graphEditor';
  5. import { NodeMaterial } from "babylonjs/Materials/Node/nodeMaterial"
  6. import { Popup } from "../src/sharedComponents/popup"
  7. /**
  8. * Interface used to specify creation options for the node editor
  9. */
  10. export interface INodeEditorOptions {
  11. nodeMaterial: NodeMaterial
  12. }
  13. /**
  14. * Class used to create a node editor
  15. */
  16. export class NodeEditor {
  17. /**
  18. * Show the node editor
  19. * @param options defines the options to use to configure the node editor
  20. */
  21. public static Show(options: INodeEditorOptions) {
  22. let hostElement = Popup.CreatePopup("BABYLON.JS NODE EDITOR", "node-editor", 1000, 800)!;
  23. let globalState = new GlobalState();
  24. globalState.nodeMaterial = options.nodeMaterial
  25. globalState.hostElement = hostElement;
  26. globalState.hostDocument = hostElement.ownerDocument!;
  27. const graphEditor = React.createElement(GraphEditor, {
  28. globalState: globalState
  29. });
  30. ReactDOM.render(graphEditor, hostElement);
  31. // Close the popup window when the page is refreshed or scene is disposed
  32. var popupWindow = (Popup as any)["node-editor"];
  33. if (globalState.nodeMaterial && popupWindow) {
  34. globalState.nodeMaterial.getScene().onDisposeObservable.addOnce(() => {
  35. if (popupWindow) {
  36. popupWindow.close();
  37. }
  38. })
  39. window.onbeforeunload = function(event) {
  40. var popupWindow = (Popup as any)["node-editor"];
  41. if (popupWindow) {
  42. popupWindow.close();
  43. }
  44. };
  45. }
  46. }
  47. }