debugLayer.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { Tools } from "Misc/tools";
  2. import { Observable } from "Misc/observable";
  3. import { Scene } from "scene";
  4. // declare INSPECTOR namespace for compilation issue
  5. declare var INSPECTOR: any;
  6. // load the inspector using require, if not present in the global namespace.
  7. /**
  8. * Interface used to define scene explorer extensibility option
  9. */
  10. export interface IExplorerExtensibilityOption {
  11. /**
  12. * Define the option label
  13. */
  14. label: string;
  15. /**
  16. * Defines the action to execute on click
  17. */
  18. action: (entity: any) => void;
  19. }
  20. /**
  21. * Defines a group of actions associated with a predicate to use when extending the Inspector scene explorer
  22. */
  23. export interface IExplorerExtensibilityGroup {
  24. /**
  25. * Defines a predicate to test if a given type mut be extended
  26. */
  27. predicate: (entity: any) => boolean;
  28. /**
  29. * Gets the list of options added to a type
  30. */
  31. entries: IExplorerExtensibilityOption[];
  32. }
  33. /**
  34. * Interface used to define the options to use to create the Inspector
  35. */
  36. export interface IInspectorOptions {
  37. /**
  38. * Display in overlay mode (default: false)
  39. */
  40. overlay?: boolean;
  41. /**
  42. * HTML element to use as root (the parent of the rendering canvas will be used as default value)
  43. */
  44. globalRoot?: HTMLElement;
  45. /**
  46. * Display the Scene explorer
  47. */
  48. showExplorer?: boolean;
  49. /**
  50. * Display the property inspector
  51. */
  52. showInspector?: boolean;
  53. /**
  54. * Display in embed mode (both panes on the right)
  55. */
  56. embedMode?: boolean;
  57. /**
  58. * let the Inspector handles resize of the canvas when panes are resized (default to true)
  59. */
  60. handleResize?: boolean;
  61. /**
  62. * Allow the panes to popup (default: true)
  63. */
  64. enablePopup?: boolean;
  65. /**
  66. * Allow the panes to be closed by users (default: true)
  67. */
  68. enableClose?: boolean;
  69. /**
  70. * Optional list of extensibility entries
  71. */
  72. explorerExtensibility?: IExplorerExtensibilityGroup[];
  73. }
  74. declare module "scene" {
  75. export interface Scene {
  76. /**
  77. * @hidden
  78. * Backing field
  79. */
  80. _debugLayer: DebugLayer;
  81. /**
  82. * Gets the debug layer (aka Inspector) associated with the scene
  83. * @see http://doc.babylonjs.com/features/playground_debuglayer
  84. */
  85. debugLayer: DebugLayer;
  86. }
  87. }
  88. Object.defineProperty(Scene.prototype, "debugLayer", {
  89. get: function(this: Scene) {
  90. if (!this._debugLayer) {
  91. this._debugLayer = new DebugLayer(this);
  92. }
  93. return this._debugLayer;
  94. },
  95. enumerable: true,
  96. configurable: true
  97. });
  98. /**
  99. * The debug layer (aka Inspector) is the go to tool in order to better understand
  100. * what is happening in your scene
  101. * @see http://doc.babylonjs.com/features/playground_debuglayer
  102. */
  103. export class DebugLayer {
  104. /**
  105. * Define the url to get the inspector script from.
  106. * By default it uses the babylonjs CDN.
  107. * @ignoreNaming
  108. */
  109. public static InspectorURL = 'https://preview.babylonjs.com/inspector/babylon.inspector.bundle.js';
  110. private _scene: Scene;
  111. private BJSINSPECTOR = typeof INSPECTOR !== 'undefined' ? INSPECTOR : undefined;
  112. /**
  113. * Observable triggered when a property is changed through the inspector.
  114. */
  115. public onPropertyChangedObservable = new Observable<{ object: any, property: string, value: any, initialValue: any }>();
  116. /**
  117. * Instantiates a new debug layer.
  118. * The debug layer (aka Inspector) is the go to tool in order to better understand
  119. * what is happening in your scene
  120. * @see http://doc.babylonjs.com/features/playground_debuglayer
  121. * @param scene Defines the scene to inspect
  122. */
  123. constructor(scene: Scene) {
  124. this._scene = scene;
  125. this._scene.onDisposeObservable.add(() => {
  126. // Debug layer
  127. if (this._scene._debugLayer) {
  128. this._scene._debugLayer.hide();
  129. }
  130. });
  131. }
  132. /** Creates the inspector window. */
  133. private _createInspector(config?: Partial<IInspectorOptions>) {
  134. if (this.isVisible()) {
  135. return;
  136. }
  137. const userOptions: IInspectorOptions = {
  138. overlay: false,
  139. showExplorer: true,
  140. showInspector: true,
  141. embedMode: false,
  142. handleResize: true,
  143. enablePopup: true,
  144. ...config
  145. };
  146. this.BJSINSPECTOR = this.BJSINSPECTOR || typeof INSPECTOR !== 'undefined' ? INSPECTOR : undefined;
  147. this.BJSINSPECTOR.Inspector.Show(this._scene, userOptions);
  148. }
  149. /**
  150. * Get if the inspector is visible or not.
  151. * @returns true if visible otherwise, false
  152. */
  153. public isVisible(): boolean {
  154. return this.BJSINSPECTOR && this.BJSINSPECTOR.Inspector.IsVisible;
  155. }
  156. /**
  157. * Hide the inspector and close its window.
  158. */
  159. public hide() {
  160. this.BJSINSPECTOR.Inspector.Hide();
  161. }
  162. /**
  163. * Launch the debugLayer.
  164. * @param config Define the configuration of the inspector
  165. */
  166. public show(config?: IInspectorOptions): void {
  167. if (typeof this.BJSINSPECTOR == 'undefined') {
  168. // Load inspector and add it to the DOM
  169. Tools.LoadScript(DebugLayer.InspectorURL, this._createInspector.bind(this, config));
  170. } else {
  171. // Otherwise creates the inspector
  172. this._createInspector(config);
  173. }
  174. }
  175. }