Inspector.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. /** True if the inspector is built as a popup tab */
  18. private _popupMode : boolean = false;
  19. /** The original canvas size, before applying the inspector*/
  20. private _canvasSize : {width:string, height:string};
  21. /** The inspector is created with the given engine.
  22. * If a HTML parent is not given as a parameter, the inspector is created as a right panel on the main window.
  23. * If a HTML parent is given, the inspector is created in this element, taking full size of its parent.
  24. */
  25. constructor(scene: BABYLON.Scene, parent?:HTMLElement) {
  26. // get canvas parent only if needed.
  27. this._scene = scene;
  28. // Save HTML document
  29. Inspector.DOCUMENT = window.document;
  30. // POPUP MODE if parent is defined
  31. if (parent) {
  32. // Build the inspector in the given parent
  33. this._buildInspector(parent);
  34. } else {
  35. // Get canvas and its DOM parent
  36. let canvas = this._scene.getEngine().getRenderingCanvas();
  37. let canvasParent = canvas.parentElement;
  38. // resize canvas
  39. // canvas.style.width = 'calc(100% - 750px - 12px)';
  40. // get canvas style
  41. let canvasStyle = window.getComputedStyle(canvas);
  42. this._canvasSize = { width:canvasStyle.width, height:canvasStyle.height};
  43. // Create c2di wrapper
  44. this._c2diwrapper = Helpers.CreateDiv('insp-wrapper', canvasParent);
  45. this._c2diwrapper.style.width = this._canvasSize.width;
  46. this._c2diwrapper.style.height = this._canvasSize.height;
  47. // Add canvas to the wrapper
  48. this._c2diwrapper.appendChild(canvas);
  49. // add inspector
  50. let inspector = Helpers.CreateDiv('insp-right-panel', this._c2diwrapper);
  51. // Add split bar
  52. Split([canvas, inspector], {
  53. direction:'horizontal',
  54. sizes : [75, 25],
  55. onDrag : () => {
  56. Helpers.SEND_EVENT('resize');
  57. if (this._tabbar) {
  58. this._tabbar.updateWidth()
  59. }
  60. }
  61. });
  62. // Build the inspector
  63. this._buildInspector(inspector);
  64. // Send resize event to the window
  65. Helpers.SEND_EVENT('resize');
  66. }
  67. // Refresh the inspector
  68. this.refresh();
  69. }
  70. /** Build the inspector panel in the given HTML element */
  71. private _buildInspector(parent:HTMLElement) {
  72. // tabbar
  73. this._tabbar = new TabBar(this);
  74. // Top panel
  75. this._topPanel = Helpers.CreateDiv('top-panel', parent);
  76. // Add tabbar
  77. this._topPanel.appendChild(this._tabbar.toHtml());
  78. this._tabbar.updateWidth();
  79. // Tab panel
  80. this._tabPanel = Helpers.CreateDiv('tab-panel-content', this._topPanel);
  81. }
  82. public get scene() : BABYLON.Scene {
  83. return this._scene;
  84. }
  85. public get popupMode() : boolean {
  86. return this._popupMode;
  87. }
  88. /**
  89. * Filter the list of item present in the tree.
  90. * All item returned should have the given filter contained in the item id.
  91. */
  92. public filterItem(filter:string){
  93. this._tabbar.getActiveTab().filter(filter);
  94. }
  95. /** Display the mesh tab on the given object */
  96. public displayObjectDetails(mesh:BABYLON.AbstractMesh) {
  97. this._tabbar.switchMeshTab(mesh);
  98. }
  99. /** Clean the whole tree of item and rebuilds it */
  100. public refresh() {
  101. // Clean top panel
  102. Helpers.CleanDiv(this._tabPanel);
  103. // Get the active tab and its items
  104. let activeTab = this._tabbar.getActiveTab();
  105. activeTab.update();
  106. this._tabPanel.appendChild(activeTab.getPanel());
  107. Helpers.SEND_EVENT('resize');
  108. }
  109. /** Remove the inspector panel when it's built as a right panel:
  110. * remove the right panel and remove the wrapper
  111. */
  112. private _disposeInspector() {
  113. if (!this._popupMode) {
  114. // Get canvas
  115. let canvas = this._scene.getEngine().getRenderingCanvas();
  116. // restore canvas size
  117. canvas.style.width = this._canvasSize.width;
  118. canvas.style.height = this._canvasSize.height;
  119. // Get parent of the wrapper
  120. let canvasParent = canvas.parentElement.parentElement;
  121. canvasParent.appendChild(canvas);
  122. // Remove wrapper
  123. Helpers.CleanDiv(this._c2diwrapper);
  124. this._c2diwrapper.remove();
  125. // Send resize event to the window
  126. Helpers.SEND_EVENT('resize');
  127. }
  128. }
  129. /** Open the inspector in a new popup */
  130. public openPopup() {
  131. // Create popup
  132. let popup = window.open('', 'Babylon.js INSPECTOR', 'toolbar=no,resizable=yes,menubar=no,width=750,height=1000');
  133. popup.document.title = 'Babylon.js INSPECTOR';
  134. // Get the inspector style
  135. let styles = Inspector.DOCUMENT.querySelectorAll('style');
  136. for (let s = 0; s<styles.length; s++) {
  137. popup.document.body.appendChild(styles[s].cloneNode(true));
  138. }
  139. let links = document.querySelectorAll('link');
  140. for (let l = 0; l<links.length; l++) {
  141. let link = popup.document.createElement("link");
  142. link.rel = "stylesheet";
  143. link.href = (links[l] as HTMLLinkElement).href;
  144. popup.document.head.appendChild(link);
  145. }
  146. // Dispose the right panel
  147. this._disposeInspector();
  148. // set the mode as popup
  149. this._popupMode = true;
  150. // Save the HTML document
  151. Inspector.DOCUMENT = popup.document;
  152. // Build the inspector wrapper
  153. this._c2diwrapper = Helpers.CreateDiv('insp-wrapper', popup.document.body);
  154. // add inspector
  155. let inspector = Helpers.CreateDiv('insp-right-panel', this._c2diwrapper);
  156. // and build it in the popup
  157. this._buildInspector(inspector);
  158. // Rebuild it
  159. this.refresh();
  160. }
  161. }
  162. }