nodeEditor.ts 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 { 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 node 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 node editor
  23. */
  24. export class NodeEditor {
  25. private static _CurrentState: GlobalState;
  26. /**
  27. * Show the node editor
  28. * @param options defines the options to use to configure the node editor
  29. */
  30. public static Show(options: INodeEditorOptions) {
  31. if (this._CurrentState) {
  32. var popupWindow = (Popup as any)["node-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", "node-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(GraphEditor, {
  49. globalState: globalState
  50. });
  51. ReactDOM.render(graphEditor, hostElement);
  52. if (options.customLoadObservable) {
  53. options.customLoadObservable.add(data => {
  54. SerializationTools.Deserialize(data, globalState);
  55. globalState.mode = options.nodeMaterial.mode;
  56. globalState.onResetRequiredObservable.notifyObservers();
  57. globalState.onBuiltObservable.notifyObservers();
  58. })
  59. }
  60. this._CurrentState = globalState;
  61. // Close the popup window when the page is refreshed or scene is disposed
  62. var popupWindow = (Popup as any)["node-editor"];
  63. if (globalState.nodeMaterial && popupWindow) {
  64. globalState.nodeMaterial.getScene().onDisposeObservable.addOnce(() => {
  65. if (popupWindow) {
  66. popupWindow.close();
  67. }
  68. })
  69. window.onbeforeunload = () => {
  70. var popupWindow = (Popup as any)["node-editor"];
  71. if (popupWindow) {
  72. popupWindow.close();
  73. }
  74. };
  75. }
  76. window.addEventListener('beforeunload', () => {
  77. if(DataStorage.ReadNumber("PreviewType", PreviewType.Box) === PreviewType.Custom){
  78. DataStorage.WriteNumber("PreviewType", globalState.mode === NodeMaterialModes.Material ? PreviewType.Box : PreviewType.Bubbles);
  79. }
  80. });
  81. }
  82. }