inspector.ts 20 KB

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