babylon.debugLayer.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. module BABYLON {
  2. // declare INSPECTOR namespace for compilation issue
  3. declare var INSPECTOR: any;
  4. // load the inspector using require, if not present in the global namespace.
  5. export interface Scene {
  6. /**
  7. * @hidden
  8. * Backing field
  9. */
  10. _debugLayer: DebugLayer;
  11. /**
  12. * Gets the debug layer (aka Inspector) associated with the scene
  13. * @see http://doc.babylonjs.com/features/playground_debuglayer
  14. */
  15. debugLayer: DebugLayer;
  16. }
  17. Object.defineProperty(Scene.prototype, "debugLayer", {
  18. get: function (this: Scene) {
  19. if (!this._debugLayer) {
  20. this._debugLayer = new DebugLayer(this);
  21. }
  22. return this._debugLayer;
  23. },
  24. enumerable: true,
  25. configurable: true
  26. });
  27. /**
  28. * The debug layer (aka Inspector) is the go to tool in order to better understand
  29. * what is happening in your scene
  30. * @see http://doc.babylonjs.com/features/playground_debuglayer
  31. */
  32. export class DebugLayer {
  33. /**
  34. * Define the url to get the inspector script from.
  35. * By default it uses the babylonjs CDN.
  36. * @ignoreNaming
  37. */
  38. public static InspectorURL = 'https://preview.babylonjs.com/inspector/babylon.inspector.bundle.js';
  39. private _scene: Scene;
  40. // The inspector instance
  41. private _inspector: any;
  42. private BJSINSPECTOR = typeof INSPECTOR !== 'undefined' ? INSPECTOR : undefined;
  43. /**
  44. * Observable triggered when a property is changed through the inspector.
  45. */
  46. public onPropertyChangedObservable = new BABYLON.Observable<{ object: any, property: string, value: any, initialValue: any }>();
  47. /**
  48. * Instantiates a new debug layer.
  49. * The debug layer (aka Inspector) is the go to tool in order to better understand
  50. * what is happening in your scene
  51. * @see http://doc.babylonjs.com/features/playground_debuglayer
  52. * @param scene Defines the scene to inspect
  53. */
  54. constructor(scene: Scene) {
  55. this._scene = scene;
  56. this._scene.onDisposeObservable.add(() => {
  57. // Debug layer
  58. if (this._scene._debugLayer) {
  59. this._scene._debugLayer.hide();
  60. }
  61. })
  62. }
  63. /** Creates the inspector window. */
  64. private _createInspector(config: {
  65. popup?: boolean,
  66. initialTab?: number | string,
  67. parentElement?: HTMLElement,
  68. newColors?: {
  69. backgroundColor?: string,
  70. backgroundColorLighter?: string,
  71. backgroundColorLighter2?: string,
  72. backgroundColorLighter3?: string,
  73. color?: string,
  74. colorTop?: string,
  75. colorBot?: string
  76. }
  77. } = {}) {
  78. let popup = config.popup || false;
  79. let initialTab = config.initialTab || 0;
  80. let parentElement = config.parentElement || null;
  81. if (!this._inspector) {
  82. this.BJSINSPECTOR = this.BJSINSPECTOR || typeof INSPECTOR !== 'undefined' ? INSPECTOR : undefined;
  83. this._inspector = new this.BJSINSPECTOR.Inspector(this._scene, popup, initialTab, parentElement, config.newColors);
  84. } // else nothing to do as instance is already created
  85. }
  86. /**
  87. * Get if the inspector is visible or not.
  88. * @returns true if visible otherwise, false
  89. */
  90. public isVisible(): boolean {
  91. if (!this._inspector) {
  92. return false;
  93. }
  94. return true;
  95. }
  96. /**
  97. * Hide the inspector and close its window.
  98. */
  99. public hide() {
  100. if (this._inspector) {
  101. try {
  102. this._inspector.dispose();
  103. } catch (e) {
  104. // If the inspector has been removed directly from the inspector tool
  105. }
  106. this.onPropertyChangedObservable.clear();
  107. this._inspector = null;
  108. }
  109. }
  110. /**
  111. *
  112. * Launch the debugLayer.
  113. *
  114. * initialTab:
  115. * | Value | Tab Name |
  116. * | --- | --- |
  117. * | 0 | Scene |
  118. * | 1 | Console |
  119. * | 2 | Stats |
  120. * | 3 | Textures |
  121. * | 4 | Mesh |
  122. * | 5 | Light |
  123. * | 6 | Material |
  124. * | 7 | GLTF |
  125. * | 8 | GUI |
  126. * | 9 | Physics |
  127. * | 10 | Camera |
  128. * | 11 | Audio |
  129. *
  130. * @param config Define the configuration of the inspector
  131. */
  132. public show(config: {
  133. popup?: boolean,
  134. initialTab?: number | string,
  135. parentElement?: HTMLElement,
  136. newColors?: {
  137. backgroundColor?: string,
  138. backgroundColorLighter?: string,
  139. backgroundColorLighter2?: string,
  140. backgroundColorLighter3?: string,
  141. color?: string,
  142. colorTop?: string,
  143. colorBot?: string
  144. }
  145. } = {}): void {
  146. if (typeof this.BJSINSPECTOR == 'undefined') {
  147. // Load inspector and add it to the DOM
  148. Tools.LoadScript(DebugLayer.InspectorURL, this._createInspector.bind(this, config));
  149. } else {
  150. // Otherwise creates the inspector
  151. this._createInspector(config);
  152. }
  153. }
  154. /**
  155. * Gets the active tab
  156. * @return the index of the active tab or -1 if the inspector is hidden
  157. */
  158. public getActiveTab(): number {
  159. return this._inspector ? this._inspector.getActiveTabIndex() : -1;
  160. }
  161. }
  162. }