Inspector.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. import * as React from "react";
  2. import * as ReactDOM from "react-dom";
  3. import { ActionTabsComponent } from "./components/actionTabs/actionTabsComponent";
  4. import { SceneExplorerComponent } from "./components/sceneExplorer/sceneExplorerComponent";
  5. import { Scene, Observable, Observer, Nullable, IInspectorOptions } from "babylonjs";
  6. import { EmbedHostComponent } from "./components/embedHost/embedHostComponent";
  7. import { PropertyChangedEvent } from "./components/propertyChangedEvent";
  8. import { GlobalState } from "./components/globalState";
  9. import { IGLTFLoaderExtension } from "babylonjs-gltf2interface";
  10. import {GLTFFileLoader} from "babylonjs-loaders"
  11. interface IInternalInspectorOptions extends IInspectorOptions {
  12. popup: boolean;
  13. original: boolean;
  14. explorerWidth?: string;
  15. inspectorWidth?: string;
  16. embedHostWidth?: string;
  17. }
  18. export class Inspector {
  19. private static _SceneExplorerHost: Nullable<HTMLElement>;
  20. private static _ActionTabsHost: Nullable<HTMLElement>;
  21. private static _EmbedHost: Nullable<HTMLElement>;
  22. private static _NewCanvasContainer: HTMLElement;
  23. private static _SceneExplorerWindow: Window;
  24. private static _ActionTabsWindow: Window;
  25. private static _EmbedHostWindow: Window;
  26. private static _Scene: Scene;
  27. private static _OpenedPane = 0;
  28. private static _OnBeforeRenderObserver: Nullable<Observer<Scene>>;
  29. public static OnSelectionChangeObservable = new BABYLON.Observable<string>();
  30. public static OnPropertyChangedObservable = new BABYLON.Observable<PropertyChangedEvent>();
  31. private static _GlobalState = new GlobalState();
  32. private static _CopyStyles(sourceDoc: HTMLDocument, targetDoc: HTMLDocument) {
  33. for (var index = 0; index < sourceDoc.styleSheets.length; index++) {
  34. var styleSheet: any = sourceDoc.styleSheets[index];
  35. if (styleSheet.cssRules) { // for <style> elements
  36. const newStyleEl = sourceDoc.createElement('style');
  37. for (var cssRule of styleSheet.cssRules) {
  38. // write the text of each rule into the body of the style element
  39. newStyleEl.appendChild(sourceDoc.createTextNode(cssRule.cssText));
  40. }
  41. targetDoc.head!.appendChild(newStyleEl);
  42. } else if (styleSheet.href) { // for <link> elements loading CSS from a URL
  43. const newLinkEl = sourceDoc.createElement('link');
  44. newLinkEl.rel = 'stylesheet';
  45. newLinkEl.href = styleSheet.href;
  46. targetDoc.head!.appendChild(newLinkEl);
  47. }
  48. }
  49. }
  50. private static _CreateSceneExplorer(scene: Scene, options: IInternalInspectorOptions, parentControlExplorer: Nullable<HTMLElement>) {
  51. // Duplicating the options as they can be different for each pane
  52. if (options.original) {
  53. options = {
  54. original: false,
  55. popup: options.popup,
  56. overlay: options.overlay,
  57. showExplorer: options.showExplorer,
  58. showInspector: options.showInspector,
  59. embedMode: options.embedMode,
  60. handleResize: options.handleResize,
  61. enablePopup: options.enablePopup,
  62. explorerExtensibility: options.explorerExtensibility
  63. };
  64. }
  65. // Prepare the scene explorer host
  66. if (parentControlExplorer) {
  67. this._SceneExplorerHost = parentControlExplorer.ownerDocument!.createElement("div");
  68. this._SceneExplorerHost.id = "scene-explorer-host";
  69. this._SceneExplorerHost.style.width = options.explorerWidth || "auto";
  70. if (!options.popup) {
  71. parentControlExplorer.insertBefore(this._SceneExplorerHost, this._NewCanvasContainer);
  72. } else {
  73. parentControlExplorer.appendChild(this._SceneExplorerHost);
  74. }
  75. if (!options.overlay) {
  76. this._SceneExplorerHost.style.gridColumn = "1";
  77. this._SceneExplorerHost.style.position = "relative";
  78. }
  79. }
  80. // Scene
  81. if (this._SceneExplorerHost) {
  82. this._OpenedPane++;
  83. const sceneExplorerElement = React.createElement(SceneExplorerComponent, {
  84. scene, globalState: this._GlobalState,
  85. extensibilityGroups: options.explorerExtensibility,
  86. noExpand: !options.enablePopup, popupMode: options.popup, onPopup: () => {
  87. ReactDOM.unmountComponentAtNode(this._SceneExplorerHost!);
  88. this._RemoveElementFromDOM(this._SceneExplorerHost);
  89. if (options.popup) {
  90. this._SceneExplorerWindow.close();
  91. }
  92. options.popup = !options.popup;
  93. options.showExplorer = true;
  94. options.showInspector = false;
  95. options.explorerWidth = options.popup ? "100%" : "300px";
  96. Inspector.Show(scene, options);
  97. }, onClose: () => {
  98. ReactDOM.unmountComponentAtNode(this._SceneExplorerHost!);
  99. Inspector._OpenedPane--;
  100. this._RemoveElementFromDOM(this._SceneExplorerHost);
  101. this._Cleanup();
  102. if (options.popup) {
  103. this._SceneExplorerWindow.close();
  104. }
  105. }
  106. });
  107. ReactDOM.render(sceneExplorerElement, this._SceneExplorerHost);
  108. }
  109. }
  110. private static _CreateActionTabs(scene: Scene, options: IInternalInspectorOptions, parentControlActions: Nullable<HTMLElement>) {
  111. options.original = false;
  112. // Prepare the inspector host
  113. if (parentControlActions) {
  114. const host = parentControlActions.ownerDocument!.createElement("div");
  115. host.id = "inspector-host";
  116. host.style.width = options.inspectorWidth || "auto";
  117. parentControlActions.appendChild(host);
  118. this._ActionTabsHost = host;
  119. if (!options.overlay) {
  120. this._ActionTabsHost.style.gridColumn = "3";
  121. this._ActionTabsHost.style.position = "relative";
  122. }
  123. }
  124. if (this._ActionTabsHost) {
  125. this._OpenedPane++;
  126. const actionTabsElement = React.createElement(ActionTabsComponent, {
  127. globalState: this._GlobalState, scene: scene, noExpand: !options.enablePopup, popupMode: options.popup, onPopup: () => {
  128. ReactDOM.unmountComponentAtNode(this._ActionTabsHost!);
  129. this._RemoveElementFromDOM(this._ActionTabsHost);
  130. if (options.popup) {
  131. this._ActionTabsWindow.close();
  132. }
  133. options.popup = !options.popup;
  134. options.showExplorer = false;
  135. options.showInspector = true;
  136. options.inspectorWidth = options.popup ? "100%" : "300px";
  137. Inspector.Show(scene, options);
  138. }, onClose: () => {
  139. ReactDOM.unmountComponentAtNode(this._ActionTabsHost!);
  140. Inspector._OpenedPane--;
  141. this._Cleanup();
  142. this._RemoveElementFromDOM(this._ActionTabsHost);
  143. if (options.popup) {
  144. this._ActionTabsWindow.close();
  145. }
  146. }
  147. });
  148. ReactDOM.render(actionTabsElement, this._ActionTabsHost);
  149. }
  150. }
  151. private static _CreateEmbedHost(scene: Scene, options: IInternalInspectorOptions, parentControl: Nullable<HTMLElement>, onSelectionChangeObservable: Observable<string>) {
  152. // Prepare the inspector host
  153. if (parentControl) {
  154. const host = parentControl.ownerDocument!.createElement("div");
  155. host.id = "embed-host";
  156. host.style.width = options.embedHostWidth || "300px";
  157. parentControl.appendChild(host);
  158. this._EmbedHost = host;
  159. if (!options.overlay) {
  160. this._EmbedHost.style.gridColumn = "2";
  161. this._EmbedHost.style.position = "relative";
  162. }
  163. }
  164. if (this._EmbedHost) {
  165. this._OpenedPane++;
  166. const embedHostElement = React.createElement(EmbedHostComponent, {
  167. globalState: this._GlobalState, scene: scene, popupMode: options.popup, onPopup: () => {
  168. ReactDOM.unmountComponentAtNode(this._EmbedHost!);
  169. if (options.popup) {
  170. this._EmbedHostWindow.close();
  171. }
  172. this._RemoveElementFromDOM(this._EmbedHost);
  173. options.popup = !options.popup;
  174. options.embedMode = true;
  175. options.showExplorer = true;
  176. options.showInspector = true;
  177. options.embedHostWidth = options.popup ? "100%" : "auto";
  178. Inspector.Show(scene, options);
  179. }, onClose: () => {
  180. ReactDOM.unmountComponentAtNode(this._EmbedHost!);
  181. this._OpenedPane = 0;
  182. this._Cleanup();
  183. this._RemoveElementFromDOM(this._EmbedHost);
  184. if (options.popup) {
  185. this._EmbedHostWindow.close();
  186. }
  187. }
  188. });
  189. ReactDOM.render(embedHostElement, this._EmbedHost);
  190. }
  191. }
  192. private static _CreatePopup(title: string, windowVariableName: string) {
  193. const windowCreationOptionsList = {
  194. width: 300,
  195. height: 800,
  196. top: (window.innerHeight - 800) / 2 + window.screenY,
  197. left: (window.innerWidth - 300) / 2 + window.screenX
  198. };
  199. var windowCreationOptions = Object.keys(windowCreationOptionsList)
  200. .map(
  201. (key) => key + '=' + (windowCreationOptionsList as any)[key]
  202. )
  203. .join(',');
  204. const popupWindow = window.open("", title, windowCreationOptions);
  205. if (!popupWindow) {
  206. return null;
  207. }
  208. const parentDocument = popupWindow.document;
  209. parentDocument.title = title;
  210. parentDocument.body.style.width = "100%";
  211. parentDocument.body.style.height = "100%";
  212. parentDocument.body.style.margin = "0";
  213. parentDocument.body.style.padding = "0";
  214. let parentControl = parentDocument.createElement("div");
  215. parentControl.style.width = "100%";
  216. parentControl.style.height = "100%";
  217. parentControl.style.margin = "0";
  218. parentControl.style.padding = "0";
  219. popupWindow.document.body.appendChild(parentControl);
  220. this._CopyStyles(window.document, parentDocument);
  221. (this as any)[windowVariableName] = popupWindow;
  222. return parentControl;
  223. }
  224. public static get IsVisible(): boolean {
  225. return this._OpenedPane > 0;
  226. }
  227. public static Show(scene: Scene, userOptions: Partial<IInspectorOptions>) {
  228. const options: IInternalInspectorOptions = {
  229. original: true,
  230. popup: false,
  231. overlay: false,
  232. showExplorer: true,
  233. showInspector: true,
  234. embedMode: false,
  235. handleResize: true,
  236. enablePopup: true,
  237. ...userOptions
  238. };
  239. // Prepare state
  240. if (!this._GlobalState.onPropertyChangedObservable) {
  241. this._GlobalState.onPropertyChangedObservable = this.OnPropertyChangedObservable;
  242. }
  243. if (!this._GlobalState.onSelectionChangeObservable) {
  244. this._GlobalState.onSelectionChangeObservable = this.OnSelectionChangeObservable;
  245. }
  246. if (!this._GlobalState.onPluginActivatedObserver) {
  247. this._GlobalState.onPluginActivatedObserver = BABYLON.SceneLoader.OnPluginActivatedObservable.add((loader: GLTFFileLoader) => {
  248. if (loader.name === "gltf") {
  249. this._GlobalState.prepareGLTFPlugin(loader);
  250. loader.onValidatedObservable.add((results: IGLTFValidationResults) => {
  251. this._GlobalState.validationResults = results;
  252. this._GlobalState.onValidationResultsUpdatedObservable.notifyObservers(results);
  253. });
  254. loader.onExtensionLoadedObservable.add((extension: IGLTFLoaderExtension) => {
  255. });
  256. }
  257. });
  258. }
  259. // Make sure it is not already opened
  260. if (this.IsVisible && options.original) {
  261. this.Hide();
  262. }
  263. if (!scene) {
  264. scene = BABYLON.Engine.LastCreatedScene!;
  265. }
  266. this._Scene = scene;
  267. var canvas = scene ? scene.getEngine().getRenderingCanvas() : BABYLON.Engine.LastCreatedEngine!.getRenderingCanvas();
  268. if (options.embedMode && options.showExplorer && options.showInspector) {
  269. if (options.popup) {
  270. this._CreateEmbedHost(scene, options, this._CreatePopup("INSPECTOR", "_EmbedHostWindow"), Inspector.OnSelectionChangeObservable);
  271. }
  272. else {
  273. let parentControl = (options.globalRoot ? options.globalRoot : canvas!.parentElement) as HTMLElement;
  274. if (!options.overlay && !this._NewCanvasContainer) {
  275. this._CreateCanvasContainer(parentControl);
  276. parentControl.style.gridTemplateColumns = "1fr auto";
  277. this._NewCanvasContainer!.style.gridColumn = "1";
  278. } else if (!options.overlay && this._NewCanvasContainer && this._NewCanvasContainer.parentElement) {
  279. // the root is now the parent of the canvas container
  280. parentControl = this._NewCanvasContainer.parentElement;
  281. }
  282. if (this._NewCanvasContainer) {
  283. // If we move things around, let's control the resize
  284. if (options.handleResize && scene) {
  285. this._OnBeforeRenderObserver = scene.onBeforeRenderObservable.add(() => {
  286. scene.getEngine().resize();
  287. });
  288. }
  289. }
  290. this._CreateEmbedHost(scene, options, parentControl, Inspector.OnSelectionChangeObservable);
  291. }
  292. }
  293. else if (options.popup) {
  294. if (options.showExplorer) {
  295. if (this._SceneExplorerHost) {
  296. this._SceneExplorerHost.style.width = "0";
  297. }
  298. this._CreateSceneExplorer(scene, options, this._CreatePopup("SCENE EXPLORER", "_SceneExplorerWindow"));
  299. }
  300. if (options.showInspector) {
  301. if (this._ActionTabsHost) {
  302. this._ActionTabsHost.style.width = "0";
  303. }
  304. this._CreateActionTabs(scene, options, this._CreatePopup("INSPECTOR", "_ActionTabsWindow"));
  305. }
  306. } else {
  307. let parentControl = (options.globalRoot ? options.globalRoot : canvas!.parentElement) as HTMLElement;
  308. if (!options.overlay && !this._NewCanvasContainer) {
  309. this._CreateCanvasContainer(parentControl);
  310. } else if (!options.overlay && this._NewCanvasContainer && this._NewCanvasContainer.parentElement) {
  311. // the root is now the parent of the canvas container
  312. parentControl = this._NewCanvasContainer.parentElement;
  313. }
  314. if (this._NewCanvasContainer) {
  315. // If we move things around, let's control the resize
  316. if (options.handleResize && scene) {
  317. this._OnBeforeRenderObserver = scene.onBeforeRenderObservable.add(() => {
  318. scene.getEngine().resize();
  319. });
  320. }
  321. }
  322. if (options.showExplorer) {
  323. this._CreateSceneExplorer(scene, options, parentControl);
  324. }
  325. if (options.showInspector) {
  326. this._CreateActionTabs(scene, options, parentControl);
  327. }
  328. }
  329. }
  330. private static _CreateCanvasContainer(parentControl: HTMLElement) {
  331. // Create a container for previous elements
  332. parentControl.style.display = "grid";
  333. parentControl.style.gridTemplateColumns = "auto 1fr auto";
  334. parentControl.style.gridTemplateRows = "100%";
  335. this._NewCanvasContainer = parentControl.ownerDocument!.createElement("div");
  336. while (parentControl.childElementCount > 0) {
  337. var child = parentControl.childNodes[0];
  338. parentControl.removeChild(child);
  339. this._NewCanvasContainer.appendChild(child);
  340. }
  341. parentControl.appendChild(this._NewCanvasContainer);
  342. this._NewCanvasContainer.style.gridRow = "1";
  343. this._NewCanvasContainer.style.gridColumn = "2";
  344. this._NewCanvasContainer.style.width = "100%";
  345. this._NewCanvasContainer.style.height = "100%";
  346. }
  347. private static _Cleanup() {
  348. if (Inspector._OpenedPane === 0 && this._OnBeforeRenderObserver && this._Scene) {
  349. this._Scene.onBeforeRenderObservable.remove(this._OnBeforeRenderObserver);
  350. this._OnBeforeRenderObserver = null;
  351. this._Scene.getEngine().resize();
  352. }
  353. }
  354. private static _RemoveElementFromDOM(element: Nullable<HTMLElement>) {
  355. if (element && element.parentElement) {
  356. element.parentElement.removeChild(element);
  357. }
  358. }
  359. public static Hide() {
  360. if (this._ActionTabsHost) {
  361. ReactDOM.unmountComponentAtNode(this._ActionTabsHost);
  362. this._RemoveElementFromDOM(this._ActionTabsHost);
  363. this._ActionTabsHost = null;
  364. }
  365. if (this._SceneExplorerHost) {
  366. ReactDOM.unmountComponentAtNode(this._SceneExplorerHost);
  367. if (this._SceneExplorerHost.parentElement) {
  368. this._SceneExplorerHost.parentElement.removeChild(this._SceneExplorerHost);
  369. }
  370. this._SceneExplorerHost = null;
  371. }
  372. if (this._EmbedHost) {
  373. ReactDOM.unmountComponentAtNode(this._EmbedHost);
  374. if (this._EmbedHost.parentElement) {
  375. this._EmbedHost.parentElement.removeChild(this._EmbedHost);
  376. }
  377. this._EmbedHost = null;
  378. }
  379. Inspector._OpenedPane = 0;
  380. this._Cleanup();
  381. if (!this._GlobalState.onPluginActivatedObserver) {
  382. BABYLON.SceneLoader.OnPluginActivatedObservable.remove(this._GlobalState.onPluginActivatedObserver);
  383. this._GlobalState.onPluginActivatedObserver = null;
  384. }
  385. }
  386. }