inspector.ts 22 KB

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