nodeEditor.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. import { SerializationTools } from './serializationTools';
  8. import { Observable } from 'babylonjs/Misc/observable';
  9. import { PreviewMeshType } from './components/preview/previewMeshType';
  10. import { DataStorage } from 'babylonjs/Misc/dataStorage';
  11. /**
  12. * Interface used to specify creation options for the node editor
  13. */
  14. export interface INodeEditorOptions {
  15. nodeMaterial: NodeMaterial,
  16. hostElement?: HTMLElement,
  17. customSave?: {label: string, action: (data: string) => Promise<void>};
  18. customLoadObservable?: Observable<any>
  19. }
  20. /**
  21. * Class used to create a node editor
  22. */
  23. export class NodeEditor {
  24. private static _CurrentState: GlobalState;
  25. /**
  26. * Show the node editor
  27. * @param options defines the options to use to configure the node editor
  28. */
  29. public static Show(options: INodeEditorOptions) {
  30. if (this._CurrentState) {
  31. var popupWindow = (Popup as any)["node-editor"];
  32. if (popupWindow) {
  33. popupWindow.close();
  34. }
  35. }
  36. let hostElement = options.hostElement;
  37. if (!hostElement) {
  38. hostElement = Popup.CreatePopup("BABYLON.JS NODE EDITOR", "node-editor", 1000, 800)!;
  39. }
  40. let globalState = new GlobalState();
  41. globalState.nodeMaterial = options.nodeMaterial;
  42. globalState.hostElement = hostElement;
  43. globalState.hostDocument = hostElement.ownerDocument!;
  44. globalState.customSave = options.customSave;
  45. globalState.hostWindow = hostElement.ownerDocument!.defaultView!;
  46. const graphEditor = React.createElement(GraphEditor, {
  47. globalState: globalState
  48. });
  49. ReactDOM.render(graphEditor, hostElement);
  50. if (options.customLoadObservable) {
  51. options.customLoadObservable.add(data => {
  52. SerializationTools.Deserialize(data, globalState);
  53. globalState.onBuiltObservable.notifyObservers();
  54. })
  55. }
  56. this._CurrentState = globalState;
  57. // Close the popup window when the page is refreshed or scene is disposed
  58. var popupWindow = (Popup as any)["node-editor"];
  59. if (globalState.nodeMaterial && popupWindow) {
  60. globalState.nodeMaterial.getScene().onDisposeObservable.addOnce(() => {
  61. if (popupWindow) {
  62. popupWindow.close();
  63. }
  64. })
  65. window.onbeforeunload = () => {
  66. var popupWindow = (Popup as any)["node-editor"];
  67. if (popupWindow) {
  68. popupWindow.close();
  69. }
  70. };
  71. }
  72. window.addEventListener('beforeunload', () => {
  73. if(DataStorage.ReadNumber("PreviewMeshType", PreviewMeshType.Box) === PreviewMeshType.Custom){
  74. DataStorage.WriteNumber("PreviewMeshType", PreviewMeshType.Box)
  75. }
  76. });
  77. }
  78. }