guiEditor.ts 3.7 KB

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