Inspector.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. import { AbstractMesh, Nullable, Scene, Tools, Observable } from "babylonjs";
  2. import "../sass/main.scss";
  3. import { Helpers } from "./helpers/Helpers";
  4. import { loadGUIProperties } from "./properties_gui";
  5. import { Scheduler } from "./scheduler/Scheduler";
  6. import { TabBar } from "./tabs/TabBar";
  7. import * as Split from "Split";
  8. export class Inspector {
  9. private _c2diwrapper: HTMLElement;
  10. // private _detailsPanel: DetailPanel;
  11. /** The panel displayed at the top of the inspector */
  12. private _topPanel: HTMLElement;
  13. /** The div containing the content of the active tab */
  14. private _tabPanel: HTMLElement;
  15. /** The panel containing the list if items */
  16. // private _treePanel : HTMLElement;
  17. private _tabbar: TabBar;
  18. private _scene: Scene;
  19. /** The HTML document relative to this inspector (the window or the popup depending on its mode) */
  20. public static DOCUMENT: HTMLDocument;
  21. /** The HTML window. In popup mode, it's the popup itself. Otherwise, it's the current tab */
  22. public static WINDOW: Window;
  23. /** True if the inspector is built as a popup tab */
  24. private _popupMode: boolean = false;
  25. /** The original canvas style, before applying the inspector*/
  26. private _canvasStyle: any;
  27. private _initialTab: number | string;
  28. private _parentElement: Nullable<HTMLElement>;
  29. public onGUILoaded: Observable<any>;
  30. public static GUIObject: any; // should be typeof "babylonjs-gui";
  31. /** The inspector is created with the given engine.
  32. * If the parameter 'popup' is false, the inspector is created as a right panel on the main window.
  33. * If the parameter 'popup' is true, the inspector is created in another popup.
  34. */
  35. constructor(scene: Scene, popup?: boolean, initialTab: number | string = 0, parentElement: Nullable<HTMLElement> = null, newColors?: {
  36. backgroundColor?: string,
  37. backgroundColorLighter?: string,
  38. backgroundColorLighter2?: string,
  39. backgroundColorLighter3?: string,
  40. color?: string,
  41. colorTop?: string,
  42. colorBot?: string
  43. }) {
  44. this.onGUILoaded = new Observable();
  45. import("babylonjs-gui").then(GUI => {
  46. // Load GUI library if not already done
  47. if (!GUI || (typeof GUI !== "undefined" && Object.keys(GUI).indexOf("default") !== -1)) {
  48. Tools.LoadScript("https://preview.babylonjs.com/gui/babylon.gui.min.js", () => {
  49. Inspector.GUIObject = (<any>BABYLON).GUI;
  50. this.onGUILoaded.notifyObservers(Inspector.GUIObject);
  51. //Load properties of GUI objects now as GUI has to be declared before
  52. loadGUIProperties(Inspector.GUIObject);
  53. }, () => {
  54. console.warn('Error : loading "babylon.gui.min.js". Please add script https://preview.babylonjs.com/gui/babylon.min.gui.js to the HTML file.');
  55. });
  56. }
  57. else {
  58. Inspector.GUIObject = GUI;
  59. this.onGUILoaded.notifyObservers(Inspector.GUIObject);
  60. //Load properties of GUI objects now as GUI has to be declared before
  61. loadGUIProperties(Inspector.GUIObject);
  62. }
  63. })
  64. //get Tabbar initialTab
  65. this._initialTab = initialTab;
  66. //get parentElement of our Inspector
  67. this._parentElement = parentElement;
  68. // get canvas parent only if needed.
  69. this._scene = scene;
  70. // Save HTML document and window
  71. Inspector.DOCUMENT = window.document;
  72. Inspector.WINDOW = window;
  73. // POPUP MODE
  74. if (popup) {
  75. // Build the inspector in the given parent
  76. this.openPopup(true);// set to true in order to NOT dispose the inspector (done in openPopup), as it's not existing yet
  77. } else {
  78. // Get canvas and its DOM parent
  79. let canvas = <HTMLElement>this._scene.getEngine().getRenderingCanvas();
  80. let canvasParent = canvas.parentElement;
  81. // get canvas style
  82. let canvasComputedStyle = Inspector.WINDOW.getComputedStyle(canvas);
  83. this._canvasStyle = {
  84. width: Helpers.Css(canvas, 'width'),
  85. height: Helpers.Css(canvas, 'height'),
  86. position: canvasComputedStyle.position,
  87. top: canvasComputedStyle.top,
  88. bottom: canvasComputedStyle.bottom,
  89. left: canvasComputedStyle.left,
  90. right: canvasComputedStyle.right,
  91. padding: canvasComputedStyle.padding,
  92. paddingBottom: canvasComputedStyle.paddingBottom,
  93. paddingLeft: canvasComputedStyle.paddingLeft,
  94. paddingTop: canvasComputedStyle.paddingTop,
  95. paddingRight: canvasComputedStyle.paddingRight,
  96. margin: canvasComputedStyle.margin,
  97. marginBottom: canvasComputedStyle.marginBottom,
  98. marginLeft: canvasComputedStyle.marginLeft,
  99. marginTop: canvasComputedStyle.marginTop,
  100. marginRight: canvasComputedStyle.marginRight
  101. };
  102. if (this._parentElement) {
  103. // Build the inspector wrapper
  104. this._c2diwrapper = Helpers.CreateDiv('insp-wrapper', this._parentElement);
  105. this._c2diwrapper.style.width = '100%';
  106. this._c2diwrapper.style.height = '100%';
  107. this._c2diwrapper.style.paddingLeft = '5px';
  108. // add inspector
  109. let inspector = Helpers.CreateDiv('insp-right-panel', this._c2diwrapper);
  110. inspector.style.width = '100%';
  111. inspector.style.height = '100%';
  112. // and build it in the popup
  113. this._buildInspector(inspector);
  114. } else {
  115. // Create c2di wrapper
  116. this._c2diwrapper = Helpers.CreateDiv('insp-wrapper');
  117. // copy style from canvas to wrapper
  118. for (let prop in this._canvasStyle) {
  119. (<any>this._c2diwrapper.style)[prop] = this._canvasStyle[prop];
  120. }
  121. if (!canvasComputedStyle.width || !canvasComputedStyle.height || !canvasComputedStyle.left) {
  122. return;
  123. }
  124. // Convert wrapper size in % (because getComputedStyle returns px only)
  125. let widthPx = parseFloat(canvasComputedStyle.width.substr(0, canvasComputedStyle.width.length - 2)) || 0;
  126. let heightPx = parseFloat(canvasComputedStyle.height.substr(0, canvasComputedStyle.height.length - 2)) || 0;
  127. // If the canvas position is absolute, restrain the wrapper width to the window width + left positionning
  128. if (canvasComputedStyle.position === "absolute" || canvasComputedStyle.position === "relative") {
  129. // compute only left as it takes predominance if right is also specified (and it will be for the wrapper)
  130. let leftPx = parseFloat(canvasComputedStyle.left.substr(0, canvasComputedStyle.left.length - 2)) || 0;
  131. if (widthPx + leftPx >= Inspector.WINDOW.innerWidth) {
  132. this._c2diwrapper.style.maxWidth = `${widthPx - leftPx}px`;
  133. }
  134. }
  135. // Check if the parent of the canvas is the body page. If yes, the size ratio is computed
  136. let parent = this._getRelativeParent(canvas);
  137. let parentWidthPx = parent.clientWidth;
  138. let parentHeightPx = parent.clientHeight;
  139. let pWidth = widthPx / parentWidthPx * 100;
  140. let pheight = heightPx / parentHeightPx * 100;
  141. this._c2diwrapper.style.width = pWidth + "%";
  142. this._c2diwrapper.style.height = pheight + "%";
  143. // reset canvas style
  144. canvas.style.position = "static";
  145. canvas.style.width = "100%";
  146. canvas.style.height = "100%";
  147. canvas.style.paddingBottom = "0";
  148. canvas.style.paddingLeft = "0";
  149. canvas.style.paddingTop = "0";
  150. canvas.style.paddingRight = "0";
  151. canvas.style.margin = "0";
  152. canvas.style.marginBottom = "0";
  153. canvas.style.marginLeft = "0";
  154. canvas.style.marginTop = "0";
  155. canvas.style.marginRight = "0";
  156. // Replace canvas with the wrapper...
  157. if (canvasParent) {
  158. canvasParent.replaceChild(this._c2diwrapper, canvas);
  159. }
  160. // ... and add canvas to the wrapper
  161. this._c2diwrapper.appendChild(canvas);
  162. // add inspector
  163. let inspector = Helpers.CreateDiv('insp-right-panel', this._c2diwrapper);
  164. // Add split bar
  165. if (!this._parentElement) {
  166. Split([canvas, inspector], {
  167. direction: 'horizontal',
  168. sizes: [75, 25],
  169. onDrag: () => {
  170. Helpers.SEND_EVENT('resize');
  171. if (this._tabbar) {
  172. this._tabbar.updateWidth()
  173. }
  174. }
  175. });
  176. }
  177. // Build the inspector
  178. this._buildInspector(inspector);
  179. }
  180. // Send resize event to the window
  181. Helpers.SEND_EVENT('resize');
  182. this._tabbar.updateWidth();
  183. }
  184. /*
  185. * Refresh the inspector if the browser is not edge
  186. * Why not ?! Condition commented on 180525
  187. * To be tested
  188. */
  189. // if (!Helpers.IsBrowserEdge()) {
  190. this.refresh();
  191. // }
  192. // Check custom css colors
  193. if (newColors) {
  194. let bColor = newColors.backgroundColor || '#242424';
  195. let bColorl1 = newColors.backgroundColorLighter || '#2c2c2c';
  196. let bColorl2 = newColors.backgroundColorLighter2 || '#383838';
  197. let bColorl3 = newColors.backgroundColorLighter3 || '#454545';
  198. let color = newColors.color || '#ccc';
  199. let colorTop = newColors.colorTop || '#f29766';
  200. let colorBot = newColors.colorBot || '#5db0d7';
  201. let styles = Inspector.DOCUMENT.querySelectorAll('style');
  202. for (let s = 0; s < styles.length; s++) {
  203. let style = styles[s];
  204. if (style.innerHTML.indexOf('insp-wrapper') != -1) {
  205. styles[s].innerHTML = styles[s].innerHTML
  206. .replace(/#242424/g, bColor) // background color
  207. .replace(/#2c2c2c/g, bColorl1) // background-lighter
  208. .replace(/#383838/g, bColorl2) // background-lighter2
  209. .replace(/#454545/g, bColorl3) // background-lighter3
  210. .replace(/#ccc/g, color) // color
  211. .replace(/#f29766/g, colorTop) // color-top
  212. .replace(/#5db0d7/g, colorBot) // color-bot
  213. }
  214. }
  215. }
  216. }
  217. /**
  218. * If the given element has a position 'asbolute' or 'relative',
  219. * returns the first parent of the given element that has a position 'relative' or 'absolute'.
  220. * If the given element has no position, returns the first parent
  221. *
  222. */
  223. private _getRelativeParent(elem: HTMLElement, lookForAbsoluteOrRelative?: boolean): HTMLElement {
  224. // If the elem has no parent, returns himself
  225. if (!elem.parentElement) {
  226. return elem;
  227. }
  228. let computedStyle = Inspector.WINDOW.getComputedStyle(elem);
  229. // looking for the first element absolute or relative
  230. if (lookForAbsoluteOrRelative) {
  231. // if found, return this one
  232. if (computedStyle.position === "relative" || computedStyle.position === "absolute") {
  233. return elem;
  234. } else {
  235. // otherwise keep looking
  236. return this._getRelativeParent(elem.parentElement, true);
  237. }
  238. }
  239. // looking for the relative parent of the element
  240. else {
  241. if (computedStyle.position == "static") {
  242. return elem.parentElement;
  243. } else {
  244. // the elem has a position relative or absolute, look for the closest relative/absolute parent
  245. return this._getRelativeParent(elem.parentElement, true);
  246. }
  247. }
  248. }
  249. /** Build the inspector panel in the given HTML element */
  250. private _buildInspector(parent: HTMLElement) {
  251. // tabbar
  252. this._tabbar = new TabBar(this, this._initialTab);
  253. // Top panel
  254. this._topPanel = Helpers.CreateDiv('top-panel', parent);
  255. // Add tabbar
  256. this._topPanel.appendChild(this._tabbar.toHtml());
  257. this._tabbar.updateWidth();
  258. // Tab panel
  259. this._tabPanel = Helpers.CreateDiv('tab-panel-content', this._topPanel);
  260. }
  261. public get scene(): Scene {
  262. return this._scene;
  263. }
  264. public get popupMode(): boolean {
  265. return this._popupMode;
  266. }
  267. /**
  268. * Filter the list of item present in the tree.
  269. * All item returned should have the given filter contained in the item id.
  270. */
  271. public filterItem(filter: string) {
  272. let tab = this._tabbar.getActiveTab();
  273. if (tab) {
  274. tab.filter(filter);
  275. }
  276. }
  277. /** Display the mesh tab on the given object */
  278. public displayObjectDetails(mesh: AbstractMesh) {
  279. this._tabbar.switchMeshTab(mesh);
  280. }
  281. /** Clean the whole tree of item and rebuilds it */
  282. public refresh() {
  283. // Clean top panel
  284. Helpers.CleanDiv(this._tabPanel);
  285. // Get the active tab and its items
  286. let activeTab = this._tabbar.getActiveTab();
  287. if (!activeTab) {
  288. return;
  289. }
  290. activeTab.update();
  291. this._tabPanel.appendChild(activeTab.getPanel());
  292. Helpers.SEND_EVENT('resize');
  293. }
  294. /** Remove the inspector panel when it's built as a right panel:
  295. * remove the right panel and remove the wrapper
  296. */
  297. public dispose() {
  298. if (!this._popupMode) {
  299. let activeTab = this._tabbar.getActiveTab();
  300. if (activeTab) {
  301. activeTab.dispose();
  302. }
  303. // Get canvas
  304. let canvas = <HTMLElement>this._scene.getEngine().getRenderingCanvas();
  305. // restore canvas style
  306. for (let prop in this._canvasStyle) {
  307. (<any>canvas.style)[prop] = this._canvasStyle[prop];
  308. }
  309. // Get parent of the wrapper
  310. if (canvas.parentElement) {
  311. let canvasParent = canvas.parentElement.parentElement;
  312. if (canvasParent) {
  313. canvasParent.insertBefore(canvas, this._c2diwrapper);
  314. // Remove wrapper
  315. Helpers.CleanDiv(this._c2diwrapper);
  316. this._c2diwrapper.remove();
  317. // Send resize event to the window
  318. Helpers.SEND_EVENT('resize');
  319. }
  320. }
  321. }
  322. Scheduler.getInstance().dispose();
  323. }
  324. /** Open the inspector in a new popup
  325. * Set 'firstTime' to true if there is no inspector created beforehands
  326. */
  327. public openPopup(firstTime?: boolean) {
  328. // Create popup
  329. let popup = window.open('', 'js INSPECTOR', 'toolbar=no,resizable=yes,menubar=no,width=750,height=1000');
  330. if (!popup) {
  331. alert("Please update your browser to open the js inspector in an external view.");
  332. return;
  333. }
  334. popup.document.title = "js INSPECTOR";
  335. // Get the inspector style
  336. let styles = Inspector.DOCUMENT.querySelectorAll('style');
  337. for (let s = 0; s < styles.length; s++) {
  338. popup.document.body.appendChild(styles[s].cloneNode(true));
  339. }
  340. let links = document.querySelectorAll('link');
  341. for (let l = 0; l < links.length; l++) {
  342. let link = popup.document.createElement("link");
  343. link.rel = "stylesheet";
  344. link.href = (links[l] as HTMLLinkElement).href;
  345. popup.document.head.appendChild(link);
  346. }
  347. // Dispose the right panel if existing
  348. if (!firstTime) {
  349. this.dispose();
  350. }
  351. // set the mode as popup
  352. this._popupMode = true;
  353. // Save the HTML document
  354. Inspector.DOCUMENT = popup.document;
  355. Inspector.WINDOW = popup;
  356. // Build the inspector wrapper
  357. this._c2diwrapper = Helpers.CreateDiv('insp-wrapper', popup.document.body);
  358. // add inspector
  359. let inspector = Helpers.CreateDiv('insp-right-panel', this._c2diwrapper);
  360. inspector.classList.add('popupmode');
  361. // and build it in the popup
  362. this._buildInspector(inspector);
  363. // Rebuild it
  364. this.refresh();
  365. popup.addEventListener('resize', () => {
  366. if (this._tabbar) {
  367. this._tabbar.updateWidth()
  368. }
  369. });
  370. }
  371. public getActiveTabIndex(): number {
  372. return this._tabbar.getActiveTabIndex();
  373. }
  374. }