gamepadSceneComponent.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { Nullable } from "types";
  2. import { FreeCameraGamepadInput, FreeCameraInputsManager, ArcRotateCameraInputsManager, ArcRotateCameraGamepadInput } from "Cameras";
  3. import { Scene } from "scene";
  4. import {GamepadManager} from "Gamepad"
  5. import { SceneComponentConstants, ISceneComponent } from "sceneComponent";
  6. import { _TimeToken } from "Instrumentation";
  7. import { _DepthCullingState, _StencilState, _AlphaState } from "States";
  8. declare module "scene" {
  9. export interface Scene {
  10. /** @hidden */
  11. _gamepadManager: Nullable<GamepadManager>;
  12. /**
  13. * Gets the gamepad manager associated with the scene
  14. * @see http://doc.babylonjs.com/how_to/how_to_use_gamepads
  15. */
  16. gamepadManager: GamepadManager;
  17. }
  18. }
  19. Object.defineProperty(Scene.prototype, "gamepadManager", {
  20. get: function(this: Scene) {
  21. if (!this._gamepadManager) {
  22. this._gamepadManager = new GamepadManager(this);
  23. let component = this._getComponent(SceneComponentConstants.NAME_GAMEPAD) as GamepadSystemSceneComponent;
  24. if (!component) {
  25. component = new GamepadSystemSceneComponent(this);
  26. this._addComponent(component);
  27. }
  28. }
  29. return this._gamepadManager;
  30. },
  31. enumerable: true,
  32. configurable: true
  33. });
  34. /**
  35. * Interface representing a free camera inputs manager
  36. */
  37. export interface FreeCameraInputsManager {
  38. /**
  39. * Adds gamepad input support to the FreeCameraInputsManager.
  40. * @returns the FreeCameraInputsManager
  41. */
  42. addGamepad(): FreeCameraInputsManager;
  43. }
  44. /**
  45. * Adds a gamepad to the free camera inputs manager
  46. */
  47. FreeCameraInputsManager.prototype.addGamepad = function(): FreeCameraInputsManager {
  48. this.add(new FreeCameraGamepadInput());
  49. return this;
  50. };
  51. /**
  52. * Interface representing an arc rotate camera inputs manager
  53. */
  54. export interface ArcRotateCameraInputsManager {
  55. /**
  56. * Adds gamepad input support to the ArcRotateCamera InputManager.
  57. * @returns the camera inputs manager
  58. */
  59. addGamepad(): ArcRotateCameraInputsManager;
  60. }
  61. /**
  62. * Adds a gamepad to the arc rotate camera inputs manager
  63. */
  64. ArcRotateCameraInputsManager.prototype.addGamepad = function(): ArcRotateCameraInputsManager {
  65. this.add(new ArcRotateCameraGamepadInput());
  66. return this;
  67. };
  68. /**
  69. * Defines the gamepad scene component responsible to manage gamepads in a given scene
  70. */
  71. export class GamepadSystemSceneComponent implements ISceneComponent {
  72. /**
  73. * The component name helpfull to identify the component in the list of scene components.
  74. */
  75. public readonly name = SceneComponentConstants.NAME_GAMEPAD;
  76. /**
  77. * The scene the component belongs to.
  78. */
  79. public scene: Scene;
  80. /**
  81. * Creates a new instance of the component for the given scene
  82. * @param scene Defines the scene to register the component in
  83. */
  84. constructor(scene: Scene) {
  85. this.scene = scene;
  86. }
  87. /**
  88. * Registers the component in a given scene
  89. */
  90. public register(): void {
  91. this.scene._beforeCameraUpdateStage.registerStep(SceneComponentConstants.STEP_BEFORECAMERAUPDATE_GAMEPAD, this, this._beforeCameraUpdate);
  92. }
  93. /**
  94. * Rebuilds the elements related to this component in case of
  95. * context lost for instance.
  96. */
  97. public rebuild(): void {
  98. // Nothing to do for gamepads
  99. }
  100. /**
  101. * Disposes the component and the associated ressources
  102. */
  103. public dispose(): void {
  104. let gamepadManager = this.scene._gamepadManager;
  105. if (gamepadManager) {
  106. gamepadManager.dispose();
  107. this.scene._gamepadManager = null;
  108. }
  109. }
  110. private _beforeCameraUpdate(): void {
  111. let gamepadManager = this.scene._gamepadManager;
  112. if (gamepadManager && gamepadManager._isMonitoring) {
  113. gamepadManager._checkGamepadsStatus();
  114. }
  115. }
  116. }