babylon.debugLayer.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 IExplorerExtensibilityOption {
  6. label: string;
  7. action: (entity: any) => void;
  8. }
  9. export interface IExplorerExtensibilityGroup {
  10. predicate: (entity: any) => boolean;
  11. entries: IExplorerExtensibilityOption[];
  12. }
  13. export interface IInspectorOptions {
  14. overlay?: boolean;
  15. globalRoot?: HTMLElement;
  16. showExplorer?: boolean;
  17. showInspector?: boolean;
  18. embedMode?: boolean;
  19. handleResize?: boolean;
  20. enablePopup?: boolean;
  21. explorerExtensibility?: IExplorerExtensibilityGroup[];
  22. }
  23. export interface Scene {
  24. /**
  25. * @hidden
  26. * Backing field
  27. */
  28. _debugLayer: DebugLayer;
  29. /**
  30. * Gets the debug layer (aka Inspector) associated with the scene
  31. * @see http://doc.babylonjs.com/features/playground_debuglayer
  32. */
  33. debugLayer: DebugLayer;
  34. }
  35. Object.defineProperty(Scene.prototype, "debugLayer", {
  36. get: function(this: Scene) {
  37. if (!this._debugLayer) {
  38. this._debugLayer = new DebugLayer(this);
  39. }
  40. return this._debugLayer;
  41. },
  42. enumerable: true,
  43. configurable: true
  44. });
  45. /**
  46. * The debug layer (aka Inspector) is the go to tool in order to better understand
  47. * what is happening in your scene
  48. * @see http://doc.babylonjs.com/features/playground_debuglayer
  49. */
  50. export class DebugLayer {
  51. /**
  52. * Define the url to get the inspector script from.
  53. * By default it uses the babylonjs CDN.
  54. * @ignoreNaming
  55. */
  56. public static InspectorURL = 'https://preview.babylonjs.com/inspector/babylon.inspector.bundle.js';
  57. private _scene: Scene;
  58. private BJSINSPECTOR = typeof INSPECTOR !== 'undefined' ? INSPECTOR : undefined;
  59. /**
  60. * Observable triggered when a property is changed through the inspector.
  61. */
  62. public onPropertyChangedObservable = new BABYLON.Observable<{ object: any, property: string, value: any, initialValue: any }>();
  63. /**
  64. * Instantiates a new debug layer.
  65. * The debug layer (aka Inspector) is the go to tool in order to better understand
  66. * what is happening in your scene
  67. * @see http://doc.babylonjs.com/features/playground_debuglayer
  68. * @param scene Defines the scene to inspect
  69. */
  70. constructor(scene: Scene) {
  71. this._scene = scene;
  72. this._scene.onDisposeObservable.add(() => {
  73. // Debug layer
  74. if (this._scene._debugLayer) {
  75. this._scene._debugLayer.hide();
  76. }
  77. });
  78. }
  79. /** Creates the inspector window. */
  80. private _createInspector(config?: Partial<IInspectorOptions>) {
  81. if (this.isVisible()) {
  82. return;
  83. }
  84. const userOptions: IInspectorOptions = {
  85. overlay: false,
  86. showExplorer: true,
  87. showInspector: true,
  88. embedMode: false,
  89. handleResize: true,
  90. enablePopup: true,
  91. ...config
  92. };
  93. this.BJSINSPECTOR = this.BJSINSPECTOR || typeof INSPECTOR !== 'undefined' ? INSPECTOR : undefined;
  94. this.BJSINSPECTOR.Inspector.Show(this._scene, userOptions);
  95. }
  96. /**
  97. * Get if the inspector is visible or not.
  98. * @returns true if visible otherwise, false
  99. */
  100. public isVisible(): boolean {
  101. return this.BJSINSPECTOR.Inspector.IsVisible;
  102. }
  103. /**
  104. * Hide the inspector and close its window.
  105. */
  106. public hide() {
  107. this.BJSINSPECTOR.Inspector.Hide();
  108. }
  109. /**
  110. * Launch the debugLayer.
  111. * @param config Define the configuration of the inspector
  112. */
  113. public show(config?: IInspectorOptions): void {
  114. if (typeof this.BJSINSPECTOR == 'undefined') {
  115. // Load inspector and add it to the DOM
  116. Tools.LoadScript(DebugLayer.InspectorURL, this._createInspector.bind(this, config));
  117. } else {
  118. // Otherwise creates the inspector
  119. this._createInspector(config);
  120. }
  121. }
  122. }
  123. }