guiEditor.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 { Popup } from "./sharedUiComponents/lines/popup";
  6. import { Observable } from "babylonjs/Misc/observable";
  7. /**
  8. * Interface used to specify creation options for the gui editor
  9. */
  10. export interface IGUIEditorOptions {
  11. hostElement?: HTMLElement;
  12. customSave?: { label: string; action: (data: string) => Promise<void> };
  13. currentSnippetToken?: string;
  14. customLoadObservable?: Observable<any>;
  15. }
  16. /**
  17. * Class used to create a gui editor
  18. */
  19. export class GUIEditor {
  20. private static _CurrentState: GlobalState;
  21. /**
  22. * Show the gui editor
  23. * @param options defines the options to use to configure the gui editor
  24. */
  25. public static Show(options: IGUIEditorOptions) {
  26. if (this._CurrentState) {
  27. var popupWindow = (Popup as any)["gui-editor"];
  28. if (popupWindow) {
  29. popupWindow.close();
  30. }
  31. if(options.currentSnippetToken) {
  32. try {
  33. this._CurrentState.workbench.loadFromSnippet(options.currentSnippetToken);
  34. } catch (error) {
  35. //swallow and continue
  36. }
  37. }
  38. return;
  39. }
  40. let hostElement = options.hostElement;
  41. if (!hostElement) {
  42. hostElement = Popup.CreatePopup("BABYLON.JS GUI EDITOR", "gui-editor", 1000, 800)!;
  43. }
  44. let globalState = new GlobalState();
  45. globalState.hostElement = hostElement;
  46. globalState.hostDocument = hostElement.ownerDocument!;
  47. globalState.customSave = options.customSave;
  48. globalState.hostWindow = hostElement.ownerDocument!.defaultView!;
  49. const graphEditor = React.createElement(WorkbenchEditor, {
  50. globalState: globalState,
  51. });
  52. ReactDOM.render(graphEditor, hostElement);
  53. // create the middle workbench canvas
  54. if (!globalState.guiTexture) {
  55. globalState.workbench.createGUICanvas();
  56. if(options.currentSnippetToken) {
  57. try {
  58. globalState.workbench.loadFromSnippet(options.currentSnippetToken);
  59. } catch (error) {
  60. //swallow and continue
  61. }
  62. }
  63. }
  64. if (options.customLoadObservable) {
  65. options.customLoadObservable.add((data) => {
  66. globalState.onResetRequiredObservable.notifyObservers();
  67. globalState.onBuiltObservable.notifyObservers();
  68. });
  69. }
  70. this._CurrentState = globalState;
  71. // Close the popup window when the page is refreshed or scene is disposed
  72. var popupWindow = (Popup as any)["gui-editor"];
  73. if (popupWindow) {
  74. window.onbeforeunload = () => {
  75. var popupWindow = (Popup as any)["gui-editor"];
  76. if (popupWindow) {
  77. popupWindow.close();
  78. }
  79. };
  80. }
  81. window.addEventListener("beforeunload", () => {});
  82. }
  83. }