gamepadSceneComponent.ts 4.3 KB

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