-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
guiTools.ts
70 lines (63 loc) · 2.77 KB
/
guiTools.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import type { AdvancedDynamicTexture } from "gui/2D/advancedDynamicTexture";
import { Engine } from "core/Engines/engine";
import { Tools } from "core/Misc/tools";
import { GUIEditor } from "gui-editor/guiEditor";
declare let BABYLON: any;
let editorUrl = `v${Engine.Version}/guiEditor/babylon.guiEditor.js`;
// eslint-disable-next-line @typescript-eslint/naming-convention
let guiEditorContainer: { GUIEditor: typeof GUIEditor };
/** Get the inspector from bundle or global */
function _getGlobalGUIEditor(): { GUIEditor: typeof GUIEditor } | undefined {
// UMD Global name detection from Webpack Bundle UMD Name.
if (typeof GUIEditor !== "undefined") {
// eslint-disable-next-line @typescript-eslint/naming-convention
return { GUIEditor };
}
// In case of module let's check the global emitted from the editor entry point.
if (typeof BABYLON !== "undefined" && typeof BABYLON.GUIEditor !== "undefined") {
return BABYLON;
}
return undefined;
}
/**
* Used to pass in the gui-editor package.
* @param guiEditorPackage
*/
export function InjectGUIEditor(guiEditorPackage: any) {
guiEditorContainer = guiEditorPackage;
}
/**
* Change the URL that the GUI editor loads from
* @param guiEditorURL
*/
export function SetGUIEditorURL(guiEditorURL: string) {
editorUrl = guiEditorURL;
}
/**
* Opens an ADT in the GUI editor
* if you are in an ES6 environment, you must first call InjectGUIEditor to provide the gui-editor package
* If you are in a UMD environment, it will load the package from a URL
* @param adt
* @param embed defines whether editor is being opened from the Playground
*/
export async function EditAdvancedDynamicTexture(adt: AdvancedDynamicTexture, embed?: boolean) {
guiEditorContainer = guiEditorContainer || _getGlobalGUIEditor();
if (!guiEditorContainer) {
if (typeof BABYLON !== "undefined") {
// we are in UMD environment
if (typeof guiEditorContainer === "undefined") {
// Load editor and add it to the DOM
try {
await Tools.LoadScriptAsync(editorUrl);
guiEditorContainer = guiEditorContainer || _getGlobalGUIEditor();
} catch {
throw `Failed to load GUI editor from ${editorUrl}`;
}
}
} else {
// we are in ES6 environment
throw `Tried to call EditAdvancedDynamicTexture without first injecting the GUI editor. You need to call InjectGUIEditor() with a reference to @babylonjs/gui-editor. It can be imported at runtime using await import("@babylonjs/gui-editor").`;
}
}
guiEditorContainer.GUIEditor.Show({ liveGuiTexture: adt }, embed);
}