inspector.ts 21 KB

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