debugLayer.ts 6.8 KB

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