nodeEditor.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.mode = options.nodeMaterial.mode;
  43. globalState.hostElement = hostElement;
  44. globalState.hostDocument = hostElement.ownerDocument!;
  45. globalState.customSave = options.customSave;
  46. globalState.hostWindow = hostElement.ownerDocument!.defaultView!;
  47. const graphEditor = React.createElement(GraphEditor, {
  48. globalState: globalState
  49. });
  50. ReactDOM.render(graphEditor, hostElement);
  51. if (options.customLoadObservable) {
  52. options.customLoadObservable.add(data => {
  53. SerializationTools.Deserialize(data, globalState);
  54. globalState.onBuiltObservable.notifyObservers();
  55. })
  56. }
  57. this._CurrentState = globalState;
  58. // Close the popup window when the page is refreshed or scene is disposed
  59. var popupWindow = (Popup as any)["node-editor"];
  60. if (globalState.nodeMaterial && popupWindow) {
  61. globalState.nodeMaterial.getScene().onDisposeObservable.addOnce(() => {
  62. if (popupWindow) {
  63. popupWindow.close();
  64. }
  65. })
  66. window.onbeforeunload = () => {
  67. var popupWindow = (Popup as any)["node-editor"];
  68. if (popupWindow) {
  69. popupWindow.close();
  70. }
  71. };
  72. }
  73. window.addEventListener('beforeunload', () => {
  74. if(DataStorage.ReadNumber("PreviewMeshType", PreviewMeshType.Box) === PreviewMeshType.Custom){
  75. DataStorage.WriteNumber("PreviewMeshType", PreviewMeshType.Box)
  76. }
  77. });
  78. }
  79. }