babylon.gamepadSceneComponent.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. export interface FreeCameraInputsManager {
  27. addGamepad(): FreeCameraInputsManager;
  28. }
  29. FreeCameraInputsManager.prototype.addGamepad = function(): FreeCameraInputsManager {
  30. this.add(new FreeCameraGamepadInput());
  31. return this;
  32. }
  33. export interface ArcRotateCameraInputsManager {
  34. addGamepad(): ArcRotateCameraInputsManager;
  35. }
  36. ArcRotateCameraInputsManager.prototype.addGamepad = function(): ArcRotateCameraInputsManager {
  37. this.add(new ArcRotateCameraGamepadInput());
  38. return this;
  39. }
  40. /**
  41. * Defines the gamepad scene component responsible to manage gamepads in a given scene
  42. */
  43. export class GamepadSystemSceneComponent implements ISceneComponent {
  44. /**
  45. * The component name helpfull to identify the component in the list of scene components.
  46. */
  47. public readonly name = SceneComponentConstants.NAME_GAMEPAD;
  48. /**
  49. * The scene the component belongs to.
  50. */
  51. public scene: Scene;
  52. /**
  53. * Creates a new instance of the component for the given scene
  54. * @param scene Defines the scene to register the component in
  55. */
  56. constructor(scene: Scene) {
  57. this.scene = scene;
  58. }
  59. /**
  60. * Registers the component in a given scene
  61. */
  62. public register(): void {
  63. this.scene._beforeCameraUpdateStage.registerStep(SceneComponentConstants.STEP_BEFORECAMERAUPDATE_GAMEPAD, this, this._beforeCameraUpdate);
  64. }
  65. /**
  66. * Rebuilds the elements related to this component in case of
  67. * context lost for instance.
  68. */
  69. public rebuild(): void {
  70. // Nothing to do for gamepads
  71. }
  72. /**
  73. * Disposes the component and the associated ressources
  74. */
  75. public dispose(): void {
  76. let gamepadManager = this.scene._gamepadManager;
  77. if (gamepadManager) {
  78. gamepadManager.dispose();
  79. this.scene._gamepadManager = null;
  80. }
  81. }
  82. private _beforeCameraUpdate(): void {
  83. let gamepadManager = this.scene._gamepadManager;
  84. if (gamepadManager && gamepadManager._isMonitoring) {
  85. gamepadManager._checkGamepadsStatus();
  86. }
  87. }
  88. }
  89. }