1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- import * as React from "react";
- import * as ReactDOM from "react-dom";
- import { GlobalState } from './globalState';
- import { GraphEditor } from './graphEditor';
- import { NodeMaterial } from "babylonjs/Materials/Node/nodeMaterial"
- import { Popup } from "../src/sharedComponents/popup"
- import { Observable } from 'babylonjs';
- import { SerializationTools } from './serializationTools';
- /**
- * Interface used to specify creation options for the node editor
- */
- export interface INodeEditorOptions {
- nodeMaterial: NodeMaterial,
- hostElement?: HTMLElement,
- customSave?: {label: string, action: (data: string) => void};
- customLoadObservable?: Observable<any>
- }
- /**
- * Class used to create a node editor
- */
- export class NodeEditor {
- private static _CurrentState: GlobalState;
- /**
- * Show the node editor
- * @param options defines the options to use to configure the node editor
- */
- public static Show(options: INodeEditorOptions) {
- if (this._CurrentState) {
- var popupWindow = (Popup as any)["node-editor"];
- if (popupWindow) {
- popupWindow.close();
- }
- }
- let hostElement = options.hostElement;
-
- if (!hostElement) {
- hostElement = Popup.CreatePopup("BABYLON.JS NODE EDITOR", "node-editor", 1000, 800)!;
- }
-
- let globalState = new GlobalState();
- globalState.nodeMaterial = options.nodeMaterial
- globalState.hostElement = hostElement;
- globalState.hostDocument = hostElement.ownerDocument!;
- globalState.customSave = options.customSave;
- const graphEditor = React.createElement(GraphEditor, {
- globalState: globalState
- });
- ReactDOM.render(graphEditor, hostElement);
- if (options.customLoadObservable) {
- options.customLoadObservable.add(data => {
- SerializationTools.Deserialize(data, globalState);
- })
- }
- this._CurrentState = globalState;
- // Close the popup window when the page is refreshed or scene is disposed
- var popupWindow = (Popup as any)["node-editor"];
- if (globalState.nodeMaterial && popupWindow) {
- globalState.nodeMaterial.getScene().onDisposeObservable.addOnce(() => {
- if (popupWindow) {
- popupWindow.close();
- }
- })
- window.onbeforeunload = function(event) {
- var popupWindow = (Popup as any)["node-editor"];
- if (popupWindow) {
- popupWindow.close();
- }
- };
- }
- }
- }
|