arcRotateCameraGamepadInput.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { __decorate } from "../../tslib.es6.js";
  2. import { serialize } from "../../Misc/decorators.js";
  3. import { CameraInputTypes } from "../../Cameras/cameraInputsManager.js";
  4. import { Gamepad } from "../../Gamepads/gamepad.js";
  5. /**
  6. * Manage the gamepad inputs to control an arc rotate camera.
  7. * @see https://doc.babylonjs.com/features/featuresDeepDive/cameras/customizingCameraInputs
  8. */
  9. export class ArcRotateCameraGamepadInput {
  10. constructor() {
  11. /**
  12. * Defines the gamepad rotation sensibility.
  13. * This is the threshold from when rotation starts to be accounted for to prevent jittering.
  14. */
  15. this.gamepadRotationSensibility = 80;
  16. /**
  17. * Defines the gamepad move sensibility.
  18. * This is the threshold from when moving starts to be accounted for for to prevent jittering.
  19. */
  20. this.gamepadMoveSensibility = 40;
  21. this._yAxisScale = 1.0;
  22. }
  23. /**
  24. * Gets or sets a boolean indicating that Yaxis (for right stick) should be inverted
  25. */
  26. get invertYAxis() {
  27. return this._yAxisScale !== 1.0;
  28. }
  29. set invertYAxis(value) {
  30. this._yAxisScale = value ? -1.0 : 1.0;
  31. }
  32. /**
  33. * Attach the input controls to a specific dom element to get the input from.
  34. */
  35. attachControl() {
  36. const manager = this.camera.getScene().gamepadManager;
  37. this._onGamepadConnectedObserver = manager.onGamepadConnectedObservable.add((gamepad) => {
  38. if (gamepad.type !== Gamepad.POSE_ENABLED) {
  39. // prioritize XBOX gamepads.
  40. if (!this.gamepad || gamepad.type === Gamepad.XBOX) {
  41. this.gamepad = gamepad;
  42. }
  43. }
  44. });
  45. this._onGamepadDisconnectedObserver = manager.onGamepadDisconnectedObservable.add((gamepad) => {
  46. if (this.gamepad === gamepad) {
  47. this.gamepad = null;
  48. }
  49. });
  50. this.gamepad = manager.getGamepadByType(Gamepad.XBOX);
  51. // if no xbox controller was found, but there are gamepad controllers, take the first one
  52. if (!this.gamepad && manager.gamepads.length) {
  53. this.gamepad = manager.gamepads[0];
  54. }
  55. }
  56. /**
  57. * Detach the current controls from the specified dom element.
  58. */
  59. detachControl() {
  60. this.camera.getScene().gamepadManager.onGamepadConnectedObservable.remove(this._onGamepadConnectedObserver);
  61. this.camera.getScene().gamepadManager.onGamepadDisconnectedObservable.remove(this._onGamepadDisconnectedObserver);
  62. this.gamepad = null;
  63. }
  64. /**
  65. * Update the current camera state depending on the inputs that have been used this frame.
  66. * This is a dynamically created lambda to avoid the performance penalty of looping for inputs in the render loop.
  67. */
  68. checkInputs() {
  69. if (this.gamepad) {
  70. const camera = this.camera;
  71. const rsValues = this.gamepad.rightStick;
  72. if (rsValues) {
  73. if (rsValues.x != 0) {
  74. const normalizedRX = rsValues.x / this.gamepadRotationSensibility;
  75. if (normalizedRX != 0 && Math.abs(normalizedRX) > 0.005) {
  76. camera.inertialAlphaOffset += normalizedRX;
  77. }
  78. }
  79. if (rsValues.y != 0) {
  80. const normalizedRY = (rsValues.y / this.gamepadRotationSensibility) * this._yAxisScale;
  81. if (normalizedRY != 0 && Math.abs(normalizedRY) > 0.005) {
  82. camera.inertialBetaOffset += normalizedRY;
  83. }
  84. }
  85. }
  86. const lsValues = this.gamepad.leftStick;
  87. if (lsValues && lsValues.y != 0) {
  88. const normalizedLY = lsValues.y / this.gamepadMoveSensibility;
  89. if (normalizedLY != 0 && Math.abs(normalizedLY) > 0.005) {
  90. this.camera.inertialRadiusOffset -= normalizedLY;
  91. }
  92. }
  93. }
  94. }
  95. /**
  96. * Gets the class name of the current intput.
  97. * @returns the class name
  98. */
  99. getClassName() {
  100. return "ArcRotateCameraGamepadInput";
  101. }
  102. /**
  103. * Get the friendly name associated with the input class.
  104. * @returns the input friendly name
  105. */
  106. getSimpleName() {
  107. return "gamepad";
  108. }
  109. }
  110. __decorate([
  111. serialize()
  112. ], ArcRotateCameraGamepadInput.prototype, "gamepadRotationSensibility", void 0);
  113. __decorate([
  114. serialize()
  115. ], ArcRotateCameraGamepadInput.prototype, "gamepadMoveSensibility", void 0);
  116. CameraInputTypes["ArcRotateCameraGamepadInput"] = ArcRotateCameraGamepadInput;
  117. //# sourceMappingURL=arcRotateCameraGamepadInput.js.map