inspector.ts 20 KB

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