Inspector.ts 18 KB

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