Inspector.ts 15 KB

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