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. 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: Nullable<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. if (!rootElement) {
  316. return;
  317. }
  318. let parentControl = (options.globalRoot ? options.globalRoot : rootElement.parentElement) as HTMLElement;
  319. if (!options.overlay && !this._NewCanvasContainer) {
  320. this._CreateCanvasContainer(parentControl);
  321. } else if (!options.overlay && this._NewCanvasContainer && this._NewCanvasContainer.parentElement) {
  322. // the root is now the parent of the canvas container
  323. parentControl = this._NewCanvasContainer.parentElement;
  324. }
  325. if (this._NewCanvasContainer) {
  326. // If we move things around, let's control the resize
  327. if (options.handleResize && scene) {
  328. this._OnBeforeRenderObserver = scene.onBeforeRenderObservable.add(() => {
  329. scene.getEngine().resize();
  330. });
  331. }
  332. }
  333. this._CreateEmbedHost(scene, options, parentControl, Inspector.OnSelectionChangeObservable);
  334. }
  335. } else if (options.popup) {
  336. if (options.showExplorer) {
  337. if (this._SceneExplorerHost) {
  338. this._SceneExplorerHost.style.width = "0";
  339. }
  340. this._CreateSceneExplorer(scene, options, this._CreatePopup("SCENE EXPLORER", "_SceneExplorerWindow"));
  341. }
  342. if (options.showInspector) {
  343. if (this._ActionTabsHost) {
  344. this._ActionTabsHost.style.width = "0";
  345. }
  346. this._CreateActionTabs(scene, options, this._CreatePopup("INSPECTOR", "_ActionTabsWindow"));
  347. }
  348. } else {
  349. let parentControl = (options.globalRoot ? options.globalRoot : rootElement!.parentElement) as HTMLElement;
  350. if (!options.overlay && !this._NewCanvasContainer) {
  351. this._CreateCanvasContainer(parentControl);
  352. } else if (!options.overlay && this._NewCanvasContainer && this._NewCanvasContainer.parentElement) {
  353. // the root is now the parent of the canvas container
  354. parentControl = this._NewCanvasContainer.parentElement;
  355. }
  356. if (this._NewCanvasContainer) {
  357. // If we move things around, let's control the resize
  358. if (options.handleResize && scene) {
  359. this._OnBeforeRenderObserver = scene.onBeforeRenderObservable.add(() => {
  360. scene.getEngine().resize();
  361. });
  362. }
  363. }
  364. if (options.showExplorer) {
  365. this._CreateSceneExplorer(scene, options, parentControl);
  366. }
  367. if (options.showInspector) {
  368. this._CreateActionTabs(scene, options, parentControl);
  369. }
  370. }
  371. }
  372. public static _SetNewScene(scene: Scene) {
  373. this._Scene = scene;
  374. this._GlobalState.onNewSceneObservable.notifyObservers(scene);
  375. }
  376. public static _CreateCanvasContainer(parentControl: HTMLElement) {
  377. // Create a container for previous elements
  378. this._NewCanvasContainer = parentControl.ownerDocument!.createElement("div");
  379. this._NewCanvasContainer.style.display = parentControl.style.display;
  380. parentControl.style.display = "flex";
  381. while (parentControl.childElementCount > 0) {
  382. var child = parentControl.childNodes[0];
  383. parentControl.removeChild(child);
  384. this._NewCanvasContainer.appendChild(child);
  385. }
  386. parentControl.appendChild(this._NewCanvasContainer);
  387. this._NewCanvasContainer.style.width = "100%";
  388. this._NewCanvasContainer.style.height = "100%";
  389. }
  390. private static _DestroyCanvasContainer() {
  391. if (!this._NewCanvasContainer) {
  392. return;
  393. }
  394. const parentControl = this._NewCanvasContainer.parentElement!;
  395. while (this._NewCanvasContainer.childElementCount > 0) {
  396. const child = this._NewCanvasContainer.childNodes[0];
  397. this._NewCanvasContainer.removeChild(child);
  398. parentControl.appendChild(child);
  399. }
  400. parentControl.removeChild(this._NewCanvasContainer);
  401. parentControl.style.display = this._NewCanvasContainer.style.display;
  402. this._NewCanvasContainer = null;
  403. }
  404. private static _Cleanup() {
  405. if (Inspector._OpenedPane !== 0) {
  406. return;
  407. }
  408. // Gizmo disposal
  409. this._GlobalState.lightGizmos.forEach((g) => {
  410. if (g.light) {
  411. this._GlobalState.enableLightGizmo(g.light, false);
  412. }
  413. });
  414. this._GlobalState.cameraGizmos.forEach((g) => {
  415. if (g.camera) {
  416. this._GlobalState.enableCameraGizmo(g.camera, false);
  417. }
  418. });
  419. if (this._Scene && this._Scene.reservedDataStore && this._Scene.reservedDataStore.gizmoManager) {
  420. this._Scene.reservedDataStore.gizmoManager.dispose();
  421. this._Scene.reservedDataStore.gizmoManager = null;
  422. }
  423. if (this._NewCanvasContainer) {
  424. this._DestroyCanvasContainer();
  425. }
  426. if (this._OnBeforeRenderObserver && this._Scene) {
  427. this._Scene.onBeforeRenderObservable.remove(this._OnBeforeRenderObserver);
  428. this._OnBeforeRenderObserver = null;
  429. this._Scene.getEngine().resize();
  430. }
  431. this._GlobalState.onInspectorClosedObservable.notifyObservers(this._Scene);
  432. }
  433. private static _RemoveElementFromDOM(element: Nullable<HTMLElement>) {
  434. if (element && element.parentElement) {
  435. element.parentElement.removeChild(element);
  436. }
  437. }
  438. public static Hide() {
  439. if (this._ActionTabsHost) {
  440. ReactDOM.unmountComponentAtNode(this._ActionTabsHost);
  441. this._RemoveElementFromDOM(this._ActionTabsHost);
  442. this._ActionTabsHost = null;
  443. }
  444. if (this._SceneExplorerHost) {
  445. ReactDOM.unmountComponentAtNode(this._SceneExplorerHost);
  446. if (this._SceneExplorerHost.parentElement) {
  447. this._SceneExplorerHost.parentElement.removeChild(this._SceneExplorerHost);
  448. }
  449. this._SceneExplorerHost = null;
  450. }
  451. if (this._EmbedHost) {
  452. ReactDOM.unmountComponentAtNode(this._EmbedHost);
  453. if (this._EmbedHost.parentElement) {
  454. this._EmbedHost.parentElement.removeChild(this._EmbedHost);
  455. }
  456. this._EmbedHost = null;
  457. }
  458. Inspector._OpenedPane = 0;
  459. this._Cleanup();
  460. if (!this._GlobalState.onPluginActivatedObserver) {
  461. SceneLoader.OnPluginActivatedObservable.remove(this._GlobalState.onPluginActivatedObserver);
  462. this._GlobalState.onPluginActivatedObserver = null;
  463. }
  464. }
  465. }
  466. Inspector.EarlyAttachToLoader();