babylon.debugLayer.ts 6.1 KB

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