inspector.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 { GLTFFileLoader } from "babylonjs-loaders";
  10. interface IInternalInspectorOptions extends IInspectorOptions {
  11. popup: boolean;
  12. original: boolean;
  13. explorerWidth?: string;
  14. inspectorWidth?: string;
  15. embedHostWidth?: string;
  16. }
  17. export class Inspector {
  18. private static _SceneExplorerHost: Nullable<HTMLElement>;
  19. private static _ActionTabsHost: Nullable<HTMLElement>;
  20. private static _EmbedHost: Nullable<HTMLElement>;
  21. private static _NewCanvasContainer: HTMLElement;
  22. private static _SceneExplorerWindow: Window;
  23. private static _ActionTabsWindow: Window;
  24. private static _EmbedHostWindow: Window;
  25. private static _Scene: Scene;
  26. private static _OpenedPane = 0;
  27. private static _OnBeforeRenderObserver: Nullable<Observer<Scene>>;
  28. public static OnSelectionChangeObservable = new BABYLON.Observable<string>();
  29. public static OnPropertyChangedObservable = new BABYLON.Observable<PropertyChangedEvent>();
  30. private static _GlobalState = new GlobalState();
  31. private static _CopyStyles(sourceDoc: HTMLDocument, targetDoc: HTMLDocument) {
  32. for (var index = 0; index < sourceDoc.styleSheets.length; index++) {
  33. var styleSheet: any = sourceDoc.styleSheets[index];
  34. if (styleSheet.cssRules) { // for <style> elements
  35. const newStyleEl = sourceDoc.createElement('style');
  36. for (var cssRule of styleSheet.cssRules) {
  37. // write the text of each rule into the body of the style element
  38. newStyleEl.appendChild(sourceDoc.createTextNode(cssRule.cssText));
  39. }
  40. targetDoc.head!.appendChild(newStyleEl);
  41. } else if (styleSheet.href) { // for <link> elements loading CSS from a URL
  42. const newLinkEl = sourceDoc.createElement('link');
  43. newLinkEl.rel = 'stylesheet';
  44. newLinkEl.href = styleSheet.href;
  45. targetDoc.head!.appendChild(newLinkEl);
  46. }
  47. }
  48. }
  49. private static _CreateSceneExplorer(scene: Scene, options: IInternalInspectorOptions, parentControlExplorer: Nullable<HTMLElement>) {
  50. // Duplicating the options as they can be different for each pane
  51. if (options.original) {
  52. options = {
  53. original: false,
  54. popup: options.popup,
  55. overlay: options.overlay,
  56. showExplorer: options.showExplorer,
  57. showInspector: options.showInspector,
  58. embedMode: options.embedMode,
  59. handleResize: options.handleResize,
  60. enablePopup: options.enablePopup,
  61. enableClose: options.enableClose,
  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.position = "relative";
  77. }
  78. }
  79. // Scene
  80. if (this._SceneExplorerHost) {
  81. this._OpenedPane++;
  82. const sceneExplorerElement = React.createElement(SceneExplorerComponent, {
  83. scene, globalState: this._GlobalState,
  84. extensibilityGroups: options.explorerExtensibility,
  85. noClose: !options.enableClose,
  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.position = "relative";
  121. }
  122. }
  123. if (this._ActionTabsHost) {
  124. this._OpenedPane++;
  125. const actionTabsElement = React.createElement(ActionTabsComponent, {
  126. globalState: this._GlobalState, scene: scene,
  127. noClose: !options.enableClose,
  128. noExpand: !options.enablePopup, popupMode: options.popup, onPopup: () => {
  129. ReactDOM.unmountComponentAtNode(this._ActionTabsHost!);
  130. this._RemoveElementFromDOM(this._ActionTabsHost);
  131. if (options.popup) {
  132. this._ActionTabsWindow.close();
  133. }
  134. options.popup = !options.popup;
  135. options.showExplorer = false;
  136. options.showInspector = true;
  137. options.inspectorWidth = options.popup ? "100%" : "300px";
  138. Inspector.Show(scene, options);
  139. }, onClose: () => {
  140. ReactDOM.unmountComponentAtNode(this._ActionTabsHost!);
  141. Inspector._OpenedPane--;
  142. this._Cleanup();
  143. this._RemoveElementFromDOM(this._ActionTabsHost);
  144. if (options.popup) {
  145. this._ActionTabsWindow.close();
  146. }
  147. }
  148. });
  149. ReactDOM.render(actionTabsElement, this._ActionTabsHost);
  150. }
  151. }
  152. private static _CreateEmbedHost(scene: Scene, options: IInternalInspectorOptions, parentControl: Nullable<HTMLElement>, onSelectionChangedObservable: Observable<string>) {
  153. // Prepare the inspector host
  154. if (parentControl) {
  155. const host = parentControl.ownerDocument!.createElement("div");
  156. host.id = "embed-host";
  157. host.style.width = options.embedHostWidth || "auto";
  158. parentControl.appendChild(host);
  159. this._EmbedHost = host;
  160. if (!options.overlay) {
  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,
  168. noExpand: !options.enablePopup,
  169. noClose: !options.enableClose,
  170. popupMode: options.popup, onPopup: () => {
  171. ReactDOM.unmountComponentAtNode(this._EmbedHost!);
  172. if (options.popup) {
  173. this._EmbedHostWindow.close();
  174. }
  175. this._RemoveElementFromDOM(this._EmbedHost);
  176. options.popup = !options.popup;
  177. options.embedMode = true;
  178. options.showExplorer = true;
  179. options.showInspector = true;
  180. options.embedHostWidth = options.popup ? "100%" : "auto";
  181. Inspector.Show(scene, options);
  182. }, onClose: () => {
  183. ReactDOM.unmountComponentAtNode(this._EmbedHost!);
  184. this._OpenedPane = 0;
  185. this._Cleanup();
  186. this._RemoveElementFromDOM(this._EmbedHost);
  187. if (options.popup) {
  188. this._EmbedHostWindow.close();
  189. }
  190. }
  191. });
  192. ReactDOM.render(embedHostElement, this._EmbedHost);
  193. }
  194. }
  195. private static _CreatePopup(title: string, windowVariableName: string) {
  196. const windowCreationOptionsList = {
  197. width: 300,
  198. height: 800,
  199. top: (window.innerHeight - 800) / 2 + window.screenY,
  200. left: (window.innerWidth - 300) / 2 + window.screenX
  201. };
  202. var windowCreationOptions = Object.keys(windowCreationOptionsList)
  203. .map(
  204. (key) => key + '=' + (windowCreationOptionsList as any)[key]
  205. )
  206. .join(',');
  207. const popupWindow = window.open("", title, windowCreationOptions);
  208. if (!popupWindow) {
  209. return null;
  210. }
  211. const parentDocument = popupWindow.document;
  212. parentDocument.title = title;
  213. parentDocument.body.style.width = "100%";
  214. parentDocument.body.style.height = "100%";
  215. parentDocument.body.style.margin = "0";
  216. parentDocument.body.style.padding = "0";
  217. let parentControl = parentDocument.createElement("div");
  218. parentControl.style.width = "100%";
  219. parentControl.style.height = "100%";
  220. parentControl.style.margin = "0";
  221. parentControl.style.padding = "0";
  222. popupWindow.document.body.appendChild(parentControl);
  223. this._CopyStyles(window.document, parentDocument);
  224. (this as any)[windowVariableName] = popupWindow;
  225. return parentControl;
  226. }
  227. public static get IsVisible(): boolean {
  228. return this._OpenedPane > 0;
  229. }
  230. public static EarlyAttachToLoader() {
  231. if (!this._GlobalState.onPluginActivatedObserver) {
  232. this._GlobalState.onPluginActivatedObserver = BABYLON.SceneLoader.OnPluginActivatedObservable.add((loader: GLTFFileLoader) => {
  233. if (loader.name === "gltf") {
  234. this._GlobalState.prepareGLTFPlugin(loader);
  235. }
  236. });
  237. }
  238. }
  239. public static Show(scene: Scene, userOptions: Partial<IInspectorOptions>) {
  240. const options: IInternalInspectorOptions = {
  241. original: true,
  242. popup: false,
  243. overlay: false,
  244. showExplorer: true,
  245. showInspector: true,
  246. embedMode: false,
  247. enableClose: true,
  248. handleResize: true,
  249. enablePopup: true,
  250. ...userOptions
  251. };
  252. // Prepare state
  253. if (!this._GlobalState.onPropertyChangedObservable) {
  254. this._GlobalState.onPropertyChangedObservable = this.OnPropertyChangedObservable;
  255. }
  256. if (!this._GlobalState.onSelectionChangedObservable) {
  257. this._GlobalState.onSelectionChangedObservable = this.OnSelectionChangeObservable;
  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. } else if (!options.overlay && this._NewCanvasContainer && this._NewCanvasContainer.parentElement) {
  277. // the root is now the parent of the canvas container
  278. parentControl = this._NewCanvasContainer.parentElement;
  279. }
  280. if (this._NewCanvasContainer) {
  281. // If we move things around, let's control the resize
  282. if (options.handleResize && scene) {
  283. this._OnBeforeRenderObserver = scene.onBeforeRenderObservable.add(() => {
  284. scene.getEngine().resize();
  285. });
  286. }
  287. }
  288. this._CreateEmbedHost(scene, options, parentControl, Inspector.OnSelectionChangeObservable);
  289. }
  290. }
  291. else if (options.popup) {
  292. if (options.showExplorer) {
  293. if (this._SceneExplorerHost) {
  294. this._SceneExplorerHost.style.width = "0";
  295. }
  296. this._CreateSceneExplorer(scene, options, this._CreatePopup("SCENE EXPLORER", "_SceneExplorerWindow"));
  297. }
  298. if (options.showInspector) {
  299. if (this._ActionTabsHost) {
  300. this._ActionTabsHost.style.width = "0";
  301. }
  302. this._CreateActionTabs(scene, options, this._CreatePopup("INSPECTOR", "_ActionTabsWindow"));
  303. }
  304. } else {
  305. let parentControl = (options.globalRoot ? options.globalRoot : canvas!.parentElement) as HTMLElement;
  306. if (!options.overlay && !this._NewCanvasContainer) {
  307. this._CreateCanvasContainer(parentControl);
  308. } else if (!options.overlay && this._NewCanvasContainer && this._NewCanvasContainer.parentElement) {
  309. // the root is now the parent of the canvas container
  310. parentControl = this._NewCanvasContainer.parentElement;
  311. }
  312. if (this._NewCanvasContainer) {
  313. // If we move things around, let's control the resize
  314. if (options.handleResize && scene) {
  315. this._OnBeforeRenderObserver = scene.onBeforeRenderObservable.add(() => {
  316. scene.getEngine().resize();
  317. });
  318. }
  319. }
  320. if (options.showExplorer) {
  321. this._CreateSceneExplorer(scene, options, parentControl);
  322. }
  323. if (options.showInspector) {
  324. this._CreateActionTabs(scene, options, parentControl);
  325. }
  326. }
  327. }
  328. private static _CreateCanvasContainer(parentControl: HTMLElement) {
  329. // Create a container for previous elements
  330. this._NewCanvasContainer = parentControl.ownerDocument!.createElement("div");
  331. this._NewCanvasContainer.style.display = parentControl.style.display;
  332. parentControl.style.display = "flex";
  333. while (parentControl.childElementCount > 0) {
  334. var child = parentControl.childNodes[0];
  335. parentControl.removeChild(child);
  336. this._NewCanvasContainer.appendChild(child);
  337. }
  338. parentControl.appendChild(this._NewCanvasContainer);
  339. this._NewCanvasContainer.style.width = "100%";
  340. this._NewCanvasContainer.style.height = "100%";
  341. }
  342. private static _DestroyCanvasContainer() {
  343. const parentControl = this._NewCanvasContainer.parentElement!;
  344. while (this._NewCanvasContainer.childElementCount > 0) {
  345. const child = this._NewCanvasContainer.childNodes[0];
  346. this._NewCanvasContainer.removeChild(child);
  347. parentControl.appendChild(child);
  348. }
  349. parentControl.removeChild(this._NewCanvasContainer);
  350. parentControl.style.display = this._NewCanvasContainer.style.display;
  351. delete this._NewCanvasContainer;
  352. }
  353. private static _Cleanup() {
  354. if (Inspector._OpenedPane !== 0) {
  355. return;
  356. }
  357. if (this._NewCanvasContainer) {
  358. this._DestroyCanvasContainer();
  359. }
  360. if (this._OnBeforeRenderObserver && this._Scene) {
  361. this._Scene.onBeforeRenderObservable.remove(this._OnBeforeRenderObserver);
  362. this._OnBeforeRenderObserver = null;
  363. this._Scene.getEngine().resize();
  364. }
  365. }
  366. private static _RemoveElementFromDOM(element: Nullable<HTMLElement>) {
  367. if (element && element.parentElement) {
  368. element.parentElement.removeChild(element);
  369. }
  370. }
  371. public static Hide() {
  372. if (this._ActionTabsHost) {
  373. ReactDOM.unmountComponentAtNode(this._ActionTabsHost);
  374. this._RemoveElementFromDOM(this._ActionTabsHost);
  375. this._ActionTabsHost = null;
  376. }
  377. if (this._SceneExplorerHost) {
  378. ReactDOM.unmountComponentAtNode(this._SceneExplorerHost);
  379. if (this._SceneExplorerHost.parentElement) {
  380. this._SceneExplorerHost.parentElement.removeChild(this._SceneExplorerHost);
  381. }
  382. this._SceneExplorerHost = null;
  383. }
  384. if (this._EmbedHost) {
  385. ReactDOM.unmountComponentAtNode(this._EmbedHost);
  386. if (this._EmbedHost.parentElement) {
  387. this._EmbedHost.parentElement.removeChild(this._EmbedHost);
  388. }
  389. this._EmbedHost = null;
  390. }
  391. Inspector._OpenedPane = 0;
  392. this._Cleanup();
  393. if (!this._GlobalState.onPluginActivatedObserver) {
  394. BABYLON.SceneLoader.OnPluginActivatedObservable.remove(this._GlobalState.onPluginActivatedObserver);
  395. this._GlobalState.onPluginActivatedObserver = null;
  396. }
  397. }
  398. }
  399. Inspector.EarlyAttachToLoader();