gamepadSceneComponent.ts 4.4 KB

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