gamepadSceneComponent.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { Scene } from "../scene.js";
  2. import { SceneComponentConstants } from "../sceneComponent.js";
  3. import { GamepadManager } from "./gamepadManager.js";
  4. import { FreeCameraInputsManager } from "../Cameras/freeCameraInputsManager.js";
  5. import { FreeCameraGamepadInput } from "../Cameras/Inputs/freeCameraGamepadInput.js";
  6. import { ArcRotateCameraInputsManager } from "../Cameras/arcRotateCameraInputsManager.js";
  7. import { ArcRotateCameraGamepadInput } from "../Cameras/Inputs/arcRotateCameraGamepadInput.js";
  8. Object.defineProperty(Scene.prototype, "gamepadManager", {
  9. get: function () {
  10. if (!this._gamepadManager) {
  11. this._gamepadManager = new GamepadManager(this);
  12. let component = this._getComponent(SceneComponentConstants.NAME_GAMEPAD);
  13. if (!component) {
  14. component = new GamepadSystemSceneComponent(this);
  15. this._addComponent(component);
  16. }
  17. }
  18. return this._gamepadManager;
  19. },
  20. enumerable: true,
  21. configurable: true,
  22. });
  23. /**
  24. * Adds a gamepad to the free camera inputs manager
  25. * @returns the FreeCameraInputsManager
  26. */
  27. FreeCameraInputsManager.prototype.addGamepad = function () {
  28. this.add(new FreeCameraGamepadInput());
  29. return this;
  30. };
  31. /**
  32. * Adds a gamepad to the arc rotate camera inputs manager
  33. * @returns the camera inputs manager
  34. */
  35. ArcRotateCameraInputsManager.prototype.addGamepad = function () {
  36. this.add(new ArcRotateCameraGamepadInput());
  37. return this;
  38. };
  39. /**
  40. * Defines the gamepad scene component responsible to manage gamepads in a given scene
  41. */
  42. export class GamepadSystemSceneComponent {
  43. /**
  44. * Creates a new instance of the component for the given scene
  45. * @param scene Defines the scene to register the component in
  46. */
  47. constructor(scene) {
  48. /**
  49. * The component name helpfull to identify the component in the list of scene components.
  50. */
  51. this.name = SceneComponentConstants.NAME_GAMEPAD;
  52. this.scene = scene;
  53. }
  54. /**
  55. * Registers the component in a given scene
  56. */
  57. register() {
  58. this.scene._beforeCameraUpdateStage.registerStep(SceneComponentConstants.STEP_BEFORECAMERAUPDATE_GAMEPAD, this, this._beforeCameraUpdate);
  59. }
  60. /**
  61. * Rebuilds the elements related to this component in case of
  62. * context lost for instance.
  63. */
  64. rebuild() {
  65. // Nothing to do for gamepads
  66. }
  67. /**
  68. * Disposes the component and the associated resources
  69. */
  70. dispose() {
  71. const gamepadManager = this.scene._gamepadManager;
  72. if (gamepadManager) {
  73. gamepadManager.dispose();
  74. this.scene._gamepadManager = null;
  75. }
  76. }
  77. _beforeCameraUpdate() {
  78. const gamepadManager = this.scene._gamepadManager;
  79. if (gamepadManager && gamepadManager._isMonitoring) {
  80. gamepadManager._checkGamepadsStatus();
  81. }
  82. }
  83. }
  84. //# sourceMappingURL=gamepadSceneComponent.js.map