Inspector.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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('Error : loading "babylon.gui.js". 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. /*
  170. * Refresh the inspector if the browser is not edge
  171. * Why not ?! Condition commented on 180525
  172. * To be tested
  173. */
  174. // if (!Helpers.IsBrowserEdge()) {
  175. this.refresh();
  176. // }
  177. // Check custom css colors
  178. if (newColors) {
  179. let bColor = newColors.backgroundColor || '#242424';
  180. let bColorl1 = newColors.backgroundColorLighter || '#2c2c2c';
  181. let bColorl2 = newColors.backgroundColorLighter2 || '#383838';
  182. let bColorl3 = newColors.backgroundColorLighter3 || '#454545';
  183. let color = newColors.color || '#ccc';
  184. let colorTop = newColors.colorTop || '#f29766';
  185. let colorBot = newColors.colorBot || '#5db0d7';
  186. let styles = Inspector.DOCUMENT.querySelectorAll('style');
  187. for (let s = 0; s < styles.length; s++) {
  188. let style = styles[s];
  189. if (style.innerHTML.indexOf('insp-wrapper') != -1) {
  190. styles[s].innerHTML = styles[s].innerHTML
  191. .replace(/#242424/g, bColor) // background color
  192. .replace(/#2c2c2c/g, bColorl1) // background-lighter
  193. .replace(/#383838/g, bColorl2) // background-lighter2
  194. .replace(/#454545/g, bColorl3) // background-lighter3
  195. .replace(/#ccc/g, color) // color
  196. .replace(/#f29766/g, colorTop) // color-top
  197. .replace(/#5db0d7/g, colorBot) // color-bot
  198. }
  199. }
  200. }
  201. }
  202. /**
  203. * If the given element has a position 'asbolute' or 'relative',
  204. * returns the first parent of the given element that has a position 'relative' or 'absolute'.
  205. * If the given element has no position, returns the first parent
  206. *
  207. */
  208. private _getRelativeParent(elem: HTMLElement, lookForAbsoluteOrRelative?: boolean): HTMLElement {
  209. // If the elem has no parent, returns himself
  210. if (!elem.parentElement) {
  211. return elem;
  212. }
  213. let computedStyle = Inspector.WINDOW.getComputedStyle(elem);
  214. // looking for the first element absolute or relative
  215. if (lookForAbsoluteOrRelative) {
  216. // if found, return this one
  217. if (computedStyle.position === "relative" || computedStyle.position === "absolute") {
  218. return elem;
  219. } else {
  220. // otherwise keep looking
  221. return this._getRelativeParent(elem.parentElement, true);
  222. }
  223. }
  224. // looking for the relative parent of the element
  225. else {
  226. if (computedStyle.position == "static") {
  227. return elem.parentElement;
  228. } else {
  229. // the elem has a position relative or absolute, look for the closest relative/absolute parent
  230. return this._getRelativeParent(elem.parentElement, true);
  231. }
  232. }
  233. }
  234. /** Build the inspector panel in the given HTML element */
  235. private _buildInspector(parent: HTMLElement) {
  236. // tabbar
  237. this._tabbar = new TabBar(this, this._initialTab);
  238. // Top panel
  239. this._topPanel = Helpers.CreateDiv('top-panel', parent);
  240. // Add tabbar
  241. this._topPanel.appendChild(this._tabbar.toHtml());
  242. this._tabbar.updateWidth();
  243. // Tab panel
  244. this._tabPanel = Helpers.CreateDiv('tab-panel-content', this._topPanel);
  245. }
  246. public get scene(): BABYLON.Scene {
  247. return this._scene;
  248. }
  249. public get popupMode(): boolean {
  250. return this._popupMode;
  251. }
  252. /**
  253. * Filter the list of item present in the tree.
  254. * All item returned should have the given filter contained in the item id.
  255. */
  256. public filterItem(filter: string) {
  257. let tab = this._tabbar.getActiveTab();
  258. if (tab) {
  259. tab.filter(filter);
  260. }
  261. }
  262. /** Display the mesh tab on the given object */
  263. public displayObjectDetails(mesh: BABYLON.AbstractMesh) {
  264. this._tabbar.switchMeshTab(mesh);
  265. }
  266. /** Clean the whole tree of item and rebuilds it */
  267. public refresh() {
  268. // Clean top panel
  269. Helpers.CleanDiv(this._tabPanel);
  270. // Get the active tab and its items
  271. let activeTab = this._tabbar.getActiveTab();
  272. if (!activeTab) {
  273. return;
  274. }
  275. activeTab.update();
  276. this._tabPanel.appendChild(activeTab.getPanel());
  277. Helpers.SEND_EVENT('resize');
  278. }
  279. /** Remove the inspector panel when it's built as a right panel:
  280. * remove the right panel and remove the wrapper
  281. */
  282. public dispose() {
  283. if (!this._popupMode) {
  284. let activeTab = this._tabbar.getActiveTab();
  285. if (activeTab) {
  286. activeTab.dispose();
  287. }
  288. // Get canvas
  289. let canvas = <HTMLElement>this._scene.getEngine().getRenderingCanvas();
  290. // restore canvas style
  291. for (let prop in this._canvasStyle) {
  292. (<any>canvas.style)[prop] = this._canvasStyle[prop];
  293. }
  294. // Get parent of the wrapper
  295. if (canvas.parentElement) {
  296. let canvasParent = canvas.parentElement.parentElement;
  297. if (canvasParent) {
  298. canvasParent.insertBefore(canvas, this._c2diwrapper);
  299. // Remove wrapper
  300. Helpers.CleanDiv(this._c2diwrapper);
  301. this._c2diwrapper.remove();
  302. // Send resize event to the window
  303. Helpers.SEND_EVENT('resize');
  304. }
  305. }
  306. }
  307. Scheduler.getInstance().dispose();
  308. }
  309. /** Open the inspector in a new popup
  310. * Set 'firstTime' to true if there is no inspector created beforehands
  311. */
  312. public openPopup(firstTime?: boolean) {
  313. // Create popup
  314. let popup = window.open('', 'Babylon.js INSPECTOR', 'toolbar=no,resizable=yes,menubar=no,width=750,height=1000');
  315. if (!popup) {
  316. alert("Please update your browser to open the Babylon.js inspector in an external view.");
  317. return;
  318. }
  319. popup.document.title = "Babylon.js INSPECTOR";
  320. // Get the inspector style
  321. let styles = Inspector.DOCUMENT.querySelectorAll('style');
  322. for (let s = 0; s < styles.length; s++) {
  323. popup.document.body.appendChild(styles[s].cloneNode(true));
  324. }
  325. let links = document.querySelectorAll('link');
  326. for (let l = 0; l < links.length; l++) {
  327. let link = popup.document.createElement("link");
  328. link.rel = "stylesheet";
  329. link.href = (links[l] as HTMLLinkElement).href;
  330. popup.document.head.appendChild(link);
  331. }
  332. // Dispose the right panel if existing
  333. if (!firstTime) {
  334. this.dispose();
  335. }
  336. // set the mode as popup
  337. this._popupMode = true;
  338. // Save the HTML document
  339. Inspector.DOCUMENT = popup.document;
  340. Inspector.WINDOW = popup;
  341. // Build the inspector wrapper
  342. this._c2diwrapper = Helpers.CreateDiv('insp-wrapper', popup.document.body);
  343. // add inspector
  344. let inspector = Helpers.CreateDiv('insp-right-panel', this._c2diwrapper);
  345. inspector.classList.add('popupmode');
  346. // and build it in the popup
  347. this._buildInspector(inspector);
  348. // Rebuild it
  349. this.refresh();
  350. popup.addEventListener('resize', () => {
  351. if (this._tabbar) {
  352. this._tabbar.updateWidth()
  353. }
  354. });
  355. }
  356. public getActiveTabIndex(): number {
  357. return this._tabbar.getActiveTabIndex();
  358. }
  359. }
  360. }