debugLayer.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import { Tools } from "../Misc/tools";
  2. import { Observable } from "../Misc/observable";
  3. import { Scene } from "../scene";
  4. import { Engine } from "../Engines/engine";
  5. // declare INSPECTOR namespace for compilation issue
  6. declare var INSPECTOR: any;
  7. declare var BABYLON: any;
  8. // load the inspector using require, if not present in the global namespace.
  9. /**
  10. * Interface used to define scene explorer extensibility option
  11. */
  12. export interface IExplorerExtensibilityOption {
  13. /**
  14. * Define the option label
  15. */
  16. label: string;
  17. /**
  18. * Defines the action to execute on click
  19. */
  20. action: (entity: any) => void;
  21. }
  22. /**
  23. * Defines a group of actions associated with a predicate to use when extending the Inspector scene explorer
  24. */
  25. export interface IExplorerExtensibilityGroup {
  26. /**
  27. * Defines a predicate to test if a given type mut be extended
  28. */
  29. predicate: (entity: any) => boolean;
  30. /**
  31. * Gets the list of options added to a type
  32. */
  33. entries: IExplorerExtensibilityOption[];
  34. }
  35. /**
  36. * Interface used to define the options to use to create the Inspector
  37. */
  38. export interface IInspectorOptions {
  39. /**
  40. * Display in overlay mode (default: false)
  41. */
  42. overlay?: boolean;
  43. /**
  44. * HTML element to use as root (the parent of the rendering canvas will be used as default value)
  45. */
  46. globalRoot?: HTMLElement;
  47. /**
  48. * Display the Scene explorer
  49. */
  50. showExplorer?: boolean;
  51. /**
  52. * Display the property inspector
  53. */
  54. showInspector?: boolean;
  55. /**
  56. * Display in embed mode (both panes on the right)
  57. */
  58. embedMode?: boolean;
  59. /**
  60. * let the Inspector handles resize of the canvas when panes are resized (default to true)
  61. */
  62. handleResize?: boolean;
  63. /**
  64. * Allow the panes to popup (default: true)
  65. */
  66. enablePopup?: boolean;
  67. /**
  68. * Allow the panes to be closed by users (default: true)
  69. */
  70. enableClose?: boolean;
  71. /**
  72. * Optional list of extensibility entries
  73. */
  74. explorerExtensibility?: IExplorerExtensibilityGroup[];
  75. /**
  76. * Optional URL to get the inspector script from (by default it uses the babylonjs CDN).
  77. */
  78. inspectorURL?: string;
  79. /**
  80. * Optional initial tab (default to DebugLayerTab.PROPERTIES)
  81. */
  82. initialTab?: DebugLayerTab.PROPERTIES;
  83. }
  84. declare module "../scene" {
  85. export interface Scene {
  86. /**
  87. * @hidden
  88. * Backing field
  89. */
  90. _debugLayer: DebugLayer;
  91. /**
  92. * Gets the debug layer (aka Inspector) associated with the scene
  93. * @see http://doc.babylonjs.com/features/playground_debuglayer
  94. */
  95. debugLayer: DebugLayer;
  96. }
  97. }
  98. Object.defineProperty(Scene.prototype, "debugLayer", {
  99. get: function(this: Scene) {
  100. if (!this._debugLayer) {
  101. this._debugLayer = new DebugLayer(this);
  102. }
  103. return this._debugLayer;
  104. },
  105. enumerable: true,
  106. configurable: true
  107. });
  108. /**
  109. * Enum of inspector action tab
  110. */
  111. export enum DebugLayerTab {
  112. /**
  113. * Properties tag (default)
  114. */
  115. PROPERTIES = 0,
  116. /**
  117. * Debug tab
  118. */
  119. DEBUG = 1,
  120. /**
  121. * Statistics tab
  122. */
  123. STATISTICS = 2,
  124. /**
  125. * Tools tab
  126. */
  127. TOOLS = 3,
  128. /**
  129. * Settings tab
  130. */
  131. SETTINGS = 4
  132. }
  133. /**
  134. * The debug layer (aka Inspector) is the go to tool in order to better understand
  135. * what is happening in your scene
  136. * @see http://doc.babylonjs.com/features/playground_debuglayer
  137. */
  138. export class DebugLayer {
  139. /**
  140. * Define the url to get the inspector script from.
  141. * By default it uses the babylonjs CDN.
  142. * @ignoreNaming
  143. */
  144. public static InspectorURL = `https://unpkg.com/babylonjs-inspector@${Engine.Version}/babylon.inspector.bundle.js`;
  145. private _scene: Scene;
  146. private BJSINSPECTOR = this._getGlobalInspector();
  147. private _onPropertyChangedObservable?: Observable<{ object: any, property: string, value: any, initialValue: any }>;
  148. /**
  149. * Observable triggered when a property is changed through the inspector.
  150. */
  151. public get onPropertyChangedObservable() {
  152. if (this.BJSINSPECTOR && this.BJSINSPECTOR.Inspector) {
  153. return this.BJSINSPECTOR.Inspector.OnPropertyChangedObservable;
  154. }
  155. if (!this._onPropertyChangedObservable) {
  156. this._onPropertyChangedObservable = new Observable<{ object: any, property: string, value: any, initialValue: any }>();
  157. }
  158. return this._onPropertyChangedObservable;
  159. }
  160. /**
  161. * Instantiates a new debug layer.
  162. * The debug layer (aka Inspector) is the go to tool in order to better understand
  163. * what is happening in your scene
  164. * @see http://doc.babylonjs.com/features/playground_debuglayer
  165. * @param scene Defines the scene to inspect
  166. */
  167. constructor(scene: Scene) {
  168. this._scene = scene;
  169. this._scene.onDisposeObservable.add(() => {
  170. // Debug layer
  171. if (this._scene._debugLayer) {
  172. this._scene._debugLayer.hide();
  173. }
  174. });
  175. }
  176. /** Creates the inspector window. */
  177. private _createInspector(config?: Partial<IInspectorOptions>) {
  178. if (this.isVisible()) {
  179. return;
  180. }
  181. if (this._onPropertyChangedObservable) {
  182. for (var observer of this._onPropertyChangedObservable!.observers) {
  183. this.BJSINSPECTOR.Inspector.OnPropertyChangedObservable.add(observer);
  184. }
  185. this._onPropertyChangedObservable.clear();
  186. this._onPropertyChangedObservable = undefined;
  187. }
  188. const userOptions: IInspectorOptions = {
  189. overlay: false,
  190. showExplorer: true,
  191. showInspector: true,
  192. embedMode: false,
  193. handleResize: true,
  194. enablePopup: true,
  195. ...config
  196. };
  197. this.BJSINSPECTOR = this.BJSINSPECTOR || this._getGlobalInspector();
  198. this.BJSINSPECTOR.Inspector.Show(this._scene, userOptions);
  199. }
  200. /**
  201. * Select a specific entity in the scene explorer and highlight a specific block in that entity property grid
  202. * @param entity defines the entity to select
  203. * @param lineContainerTitle defines the specific block to highlight
  204. */
  205. public select(entity: any, lineContainerTitle?: string) {
  206. if (this.BJSINSPECTOR) {
  207. this.BJSINSPECTOR.Inspector.MarkLineContainerTitleForHighlighting(lineContainerTitle);
  208. this.BJSINSPECTOR.Inspector.OnSelectionChangeObservable.notifyObservers(entity);
  209. }
  210. }
  211. /** Get the inspector from bundle or global */
  212. private _getGlobalInspector(): any {
  213. // UMD Global name detection from Webpack Bundle UMD Name.
  214. if (typeof INSPECTOR !== 'undefined') {
  215. return INSPECTOR;
  216. }
  217. // In case of module let s check the global emitted from the Inspector entry point.
  218. if (typeof BABYLON !== 'undefined' && typeof BABYLON.Inspector !== 'undefined') {
  219. return BABYLON;
  220. }
  221. return undefined;
  222. }
  223. /**
  224. * Get if the inspector is visible or not.
  225. * @returns true if visible otherwise, false
  226. */
  227. public isVisible(): boolean {
  228. return this.BJSINSPECTOR && this.BJSINSPECTOR.Inspector.IsVisible;
  229. }
  230. /**
  231. * Hide the inspector and close its window.
  232. */
  233. public hide() {
  234. if (this.BJSINSPECTOR) {
  235. this.BJSINSPECTOR.Inspector.Hide();
  236. }
  237. }
  238. /**
  239. * Launch the debugLayer.
  240. * @param config Define the configuration of the inspector
  241. * @return a promise fulfilled when the debug layer is visible
  242. */
  243. public show(config?: IInspectorOptions): Promise<DebugLayer> {
  244. return new Promise((resolve, reject) => {
  245. if (typeof this.BJSINSPECTOR == 'undefined') {
  246. const inspectorUrl = config && config.inspectorURL ? config.inspectorURL : DebugLayer.InspectorURL;
  247. // Load inspector and add it to the DOM
  248. Tools.LoadScript(inspectorUrl, () => {
  249. this._createInspector(config);
  250. resolve(this);
  251. });
  252. } else {
  253. // Otherwise creates the inspector
  254. this._createInspector(config);
  255. resolve(this);
  256. }
  257. });
  258. }
  259. }