inspector.ts 22 KB

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