inspector.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. initialTab: options.initialTab
  161. });
  162. ReactDOM.render(actionTabsElement, this._ActionTabsHost);
  163. }
  164. }
  165. private static _CreateEmbedHost(scene: Scene, options: IInternalInspectorOptions, parentControl: Nullable<HTMLElement>, onSelectionChangedObservable: Observable<string>) {
  166. // Prepare the inspector host
  167. if (parentControl) {
  168. const host = parentControl.ownerDocument!.createElement("div");
  169. host.id = "embed-host";
  170. host.style.width = options.embedHostWidth || "auto";
  171. parentControl.appendChild(host);
  172. this._EmbedHost = host;
  173. if (!options.overlay) {
  174. this._EmbedHost.style.position = "relative";
  175. }
  176. }
  177. if (this._EmbedHost) {
  178. this._OpenedPane++;
  179. const embedHostElement = React.createElement(EmbedHostComponent, {
  180. globalState: this._GlobalState, scene: scene,
  181. extensibilityGroups: options.explorerExtensibility,
  182. noExpand: !options.enablePopup,
  183. noClose: !options.enableClose,
  184. popupMode: options.popup, onPopup: () => {
  185. ReactDOM.unmountComponentAtNode(this._EmbedHost!);
  186. if (options.popup) {
  187. this._EmbedHostWindow.close();
  188. }
  189. this._RemoveElementFromDOM(this._EmbedHost);
  190. options.popup = !options.popup;
  191. options.embedMode = true;
  192. options.showExplorer = true;
  193. options.showInspector = true;
  194. options.embedHostWidth = options.popup ? "100%" : "auto";
  195. Inspector.Show(scene, options);
  196. }, onClose: () => {
  197. ReactDOM.unmountComponentAtNode(this._EmbedHost!);
  198. this._OpenedPane = 0;
  199. this._Cleanup();
  200. this._RemoveElementFromDOM(this._EmbedHost);
  201. if (options.popup) {
  202. this._EmbedHostWindow.close();
  203. }
  204. },
  205. initialTab: options.initialTab
  206. });
  207. ReactDOM.render(embedHostElement, this._EmbedHost);
  208. }
  209. }
  210. public static _CreatePopup(title: string, windowVariableName: string, width = 300, height = 800) {
  211. const windowCreationOptionsList = {
  212. width: width,
  213. height: height,
  214. top: (window.innerHeight - width) / 2 + window.screenY,
  215. left: (window.innerWidth - height) / 2 + window.screenX
  216. };
  217. var windowCreationOptions = Object.keys(windowCreationOptionsList)
  218. .map(
  219. (key) => key + '=' + (windowCreationOptionsList as any)[key]
  220. )
  221. .join(',');
  222. const popupWindow = window.open("", title, windowCreationOptions);
  223. if (!popupWindow) {
  224. return null;
  225. }
  226. const parentDocument = popupWindow.document;
  227. parentDocument.title = title;
  228. parentDocument.body.style.width = "100%";
  229. parentDocument.body.style.height = "100%";
  230. parentDocument.body.style.margin = "0";
  231. parentDocument.body.style.padding = "0";
  232. let parentControl = parentDocument.createElement("div");
  233. parentControl.style.width = "100%";
  234. parentControl.style.height = "100%";
  235. parentControl.style.margin = "0";
  236. parentControl.style.padding = "0";
  237. popupWindow.document.body.appendChild(parentControl);
  238. this._CopyStyles(window.document, parentDocument);
  239. (this as any)[windowVariableName] = popupWindow;
  240. return parentControl;
  241. }
  242. public static get IsVisible(): boolean {
  243. return this._OpenedPane > 0;
  244. }
  245. public static EarlyAttachToLoader() {
  246. if (!this._GlobalState.onPluginActivatedObserver) {
  247. this._GlobalState.onPluginActivatedObserver = SceneLoader.OnPluginActivatedObservable.add((rawLoader) => {
  248. const loader = rawLoader as GLTFFileLoader;
  249. if (loader.name === "gltf") {
  250. this._GlobalState.prepareGLTFPlugin(loader);
  251. }
  252. });
  253. }
  254. }
  255. public static Show(scene: Scene, userOptions: Partial<IInspectorOptions>) {
  256. const options: IInternalInspectorOptions = {
  257. original: true,
  258. popup: false,
  259. overlay: false,
  260. showExplorer: true,
  261. showInspector: true,
  262. embedMode: false,
  263. enableClose: true,
  264. handleResize: true,
  265. enablePopup: true,
  266. ...userOptions
  267. };
  268. // Prepare state
  269. if (!this._GlobalState.onPropertyChangedObservable) {
  270. this._GlobalState.init(this.OnPropertyChangedObservable);
  271. }
  272. if (!this._GlobalState.onSelectionChangedObservable) {
  273. this._GlobalState.onSelectionChangedObservable = this.OnSelectionChangeObservable;
  274. }
  275. // Make sure it is not already opened
  276. if (this.IsVisible && options.original) {
  277. this.Hide();
  278. }
  279. if (!scene) {
  280. scene = EngineStore.LastCreatedScene!;
  281. }
  282. this._Scene = scene;
  283. var rootElement = scene ? scene.getEngine().getInputElement() : EngineStore.LastCreatedEngine!.getInputElement();
  284. if (options.embedMode && options.showExplorer && options.showInspector) {
  285. if (options.popup) {
  286. this._CreateEmbedHost(scene, options, this._CreatePopup("INSPECTOR", "_EmbedHostWindow"), Inspector.OnSelectionChangeObservable);
  287. }
  288. else {
  289. let parentControl = (options.globalRoot ? options.globalRoot : rootElement!.parentElement) as HTMLElement;
  290. if (!options.overlay && !this._NewCanvasContainer) {
  291. this._CreateCanvasContainer(parentControl);
  292. } else if (!options.overlay && this._NewCanvasContainer && this._NewCanvasContainer.parentElement) {
  293. // the root is now the parent of the canvas container
  294. parentControl = this._NewCanvasContainer.parentElement;
  295. }
  296. if (this._NewCanvasContainer) {
  297. // If we move things around, let's control the resize
  298. if (options.handleResize && scene) {
  299. this._OnBeforeRenderObserver = scene.onBeforeRenderObservable.add(() => {
  300. scene.getEngine().resize();
  301. });
  302. }
  303. }
  304. this._CreateEmbedHost(scene, options, parentControl, Inspector.OnSelectionChangeObservable);
  305. }
  306. }
  307. else if (options.popup) {
  308. if (options.showExplorer) {
  309. if (this._SceneExplorerHost) {
  310. this._SceneExplorerHost.style.width = "0";
  311. }
  312. this._CreateSceneExplorer(scene, options, this._CreatePopup("SCENE EXPLORER", "_SceneExplorerWindow"));
  313. }
  314. if (options.showInspector) {
  315. if (this._ActionTabsHost) {
  316. this._ActionTabsHost.style.width = "0";
  317. }
  318. this._CreateActionTabs(scene, options, this._CreatePopup("INSPECTOR", "_ActionTabsWindow"));
  319. }
  320. } else {
  321. let parentControl = (options.globalRoot ? options.globalRoot : rootElement!.parentElement) as HTMLElement;
  322. if (!options.overlay && !this._NewCanvasContainer) {
  323. this._CreateCanvasContainer(parentControl);
  324. } else if (!options.overlay && this._NewCanvasContainer && this._NewCanvasContainer.parentElement) {
  325. // the root is now the parent of the canvas container
  326. parentControl = this._NewCanvasContainer.parentElement;
  327. }
  328. if (this._NewCanvasContainer) {
  329. // If we move things around, let's control the resize
  330. if (options.handleResize && scene) {
  331. this._OnBeforeRenderObserver = scene.onBeforeRenderObservable.add(() => {
  332. scene.getEngine().resize();
  333. });
  334. }
  335. }
  336. if (options.showExplorer) {
  337. this._CreateSceneExplorer(scene, options, parentControl);
  338. }
  339. if (options.showInspector) {
  340. this._CreateActionTabs(scene, options, parentControl);
  341. }
  342. }
  343. }
  344. public static _CreateCanvasContainer(parentControl: HTMLElement) {
  345. // Create a container for previous elements
  346. this._NewCanvasContainer = parentControl.ownerDocument!.createElement("div");
  347. this._NewCanvasContainer.style.display = parentControl.style.display;
  348. parentControl.style.display = "flex";
  349. while (parentControl.childElementCount > 0) {
  350. var child = parentControl.childNodes[0];
  351. parentControl.removeChild(child);
  352. this._NewCanvasContainer.appendChild(child);
  353. }
  354. parentControl.appendChild(this._NewCanvasContainer);
  355. this._NewCanvasContainer.style.width = "100%";
  356. this._NewCanvasContainer.style.height = "100%";
  357. }
  358. private static _DestroyCanvasContainer() {
  359. const parentControl = this._NewCanvasContainer.parentElement!;
  360. while (this._NewCanvasContainer.childElementCount > 0) {
  361. const child = this._NewCanvasContainer.childNodes[0];
  362. this._NewCanvasContainer.removeChild(child);
  363. parentControl.appendChild(child);
  364. }
  365. parentControl.removeChild(this._NewCanvasContainer);
  366. parentControl.style.display = this._NewCanvasContainer.style.display;
  367. delete this._NewCanvasContainer;
  368. }
  369. private static _Cleanup() {
  370. if (Inspector._OpenedPane !== 0) {
  371. return;
  372. }
  373. // Gizmo disposal
  374. this._GlobalState.lightGizmos.forEach((g) => {
  375. if (g.light) {
  376. this._GlobalState.enableLightGizmo(g.light, false);
  377. }
  378. })
  379. if (this._Scene && this._Scene.reservedDataStore && this._Scene.reservedDataStore.gizmoManager) {
  380. this._Scene.reservedDataStore.gizmoManager.dispose();
  381. this._Scene.reservedDataStore.gizmoManager = null;
  382. }
  383. if (this._NewCanvasContainer) {
  384. this._DestroyCanvasContainer();
  385. }
  386. if (this._OnBeforeRenderObserver && this._Scene) {
  387. this._Scene.onBeforeRenderObservable.remove(this._OnBeforeRenderObserver);
  388. this._OnBeforeRenderObserver = null;
  389. this._Scene.getEngine().resize();
  390. }
  391. this._GlobalState.onInspectorClosedObservable.notifyObservers(this._Scene);
  392. }
  393. private static _RemoveElementFromDOM(element: Nullable<HTMLElement>) {
  394. if (element && element.parentElement) {
  395. element.parentElement.removeChild(element);
  396. }
  397. }
  398. public static Hide() {
  399. if (this._ActionTabsHost) {
  400. ReactDOM.unmountComponentAtNode(this._ActionTabsHost);
  401. this._RemoveElementFromDOM(this._ActionTabsHost);
  402. this._ActionTabsHost = null;
  403. }
  404. if (this._SceneExplorerHost) {
  405. ReactDOM.unmountComponentAtNode(this._SceneExplorerHost);
  406. if (this._SceneExplorerHost.parentElement) {
  407. this._SceneExplorerHost.parentElement.removeChild(this._SceneExplorerHost);
  408. }
  409. this._SceneExplorerHost = null;
  410. }
  411. if (this._EmbedHost) {
  412. ReactDOM.unmountComponentAtNode(this._EmbedHost);
  413. if (this._EmbedHost.parentElement) {
  414. this._EmbedHost.parentElement.removeChild(this._EmbedHost);
  415. }
  416. this._EmbedHost = null;
  417. }
  418. Inspector._OpenedPane = 0;
  419. this._Cleanup();
  420. if (!this._GlobalState.onPluginActivatedObserver) {
  421. SceneLoader.OnPluginActivatedObservable.remove(this._GlobalState.onPluginActivatedObserver);
  422. this._GlobalState.onPluginActivatedObserver = null;
  423. }
  424. }
  425. }
  426. Inspector.EarlyAttachToLoader();