inspector.ts 22 KB

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