babylon.gamepadSceneComponent.ts 3.9 KB

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