arcRotateCameraGamepadInput.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { Nullable } from "../../types";
  2. import { serialize } from "../../Misc/decorators";
  3. import { Observer } from "../../Misc/observable";
  4. import { ArcRotateCamera } from "../../Cameras/arcRotateCamera";
  5. import { ICameraInput, CameraInputTypes } from "../../Cameras/cameraInputsManager";
  6. import { Gamepad } from "../../Gamepads/gamepad";
  7. /**
  8. * Manage the gamepad inputs to control an arc rotate camera.
  9. * @see https://doc.babylonjs.com/how_to/customizing_camera_inputs
  10. */
  11. export class ArcRotateCameraGamepadInput implements ICameraInput<ArcRotateCamera> {
  12. /**
  13. * Defines the camera the input is attached to.
  14. */
  15. public camera: ArcRotateCamera;
  16. /**
  17. * Defines the gamepad the input is gathering event from.
  18. */
  19. public gamepad: Nullable<Gamepad>;
  20. /**
  21. * Defines the gamepad rotation sensiblity.
  22. * This is the threshold from when rotation starts to be accounted for to prevent jittering.
  23. */
  24. @serialize()
  25. public gamepadRotationSensibility = 80;
  26. /**
  27. * Defines the gamepad move sensiblity.
  28. * This is the threshold from when moving starts to be accounted for for to prevent jittering.
  29. */
  30. @serialize()
  31. public gamepadMoveSensibility = 40;
  32. private _yAxisScale = 1.0;
  33. /**
  34. * Gets or sets a boolean indicating that Yaxis (for right stick) should be inverted
  35. */
  36. public get invertYAxis() {
  37. return this._yAxisScale !== 1.0;
  38. }
  39. public set invertYAxis(value: boolean) {
  40. this._yAxisScale = value ? -1.0 : 1.0;
  41. }
  42. private _onGamepadConnectedObserver: Nullable<Observer<Gamepad>>;
  43. private _onGamepadDisconnectedObserver: Nullable<Observer<Gamepad>>;
  44. /**
  45. * Attach the input controls to a specific dom element to get the input from.
  46. */
  47. public attachControl(): void {
  48. let manager = this.camera.getScene().gamepadManager;
  49. this._onGamepadConnectedObserver = manager.onGamepadConnectedObservable.add((gamepad) => {
  50. if (gamepad.type !== Gamepad.POSE_ENABLED) {
  51. // prioritize XBOX gamepads.
  52. if (!this.gamepad || gamepad.type === Gamepad.XBOX) {
  53. this.gamepad = gamepad;
  54. }
  55. }
  56. });
  57. this._onGamepadDisconnectedObserver = manager.onGamepadDisconnectedObservable.add((gamepad) => {
  58. if (this.gamepad === gamepad) {
  59. this.gamepad = null;
  60. }
  61. });
  62. this.gamepad = manager.getGamepadByType(Gamepad.XBOX);
  63. }
  64. /**
  65. * Detach the current controls from the specified dom element.
  66. */
  67. public detachControl(): void;
  68. /**
  69. * Detach the current controls from the specified dom element.
  70. * @param ignored defines an ignored parameter kept for backward compatibility. If you want to define the source input element, you can set engine.inputElement before calling camera.attachControl
  71. */
  72. public detachControl(ignored?: any): void {
  73. this.camera.getScene().gamepadManager.onGamepadConnectedObservable.remove(this._onGamepadConnectedObserver);
  74. this.camera.getScene().gamepadManager.onGamepadDisconnectedObservable.remove(this._onGamepadDisconnectedObserver);
  75. this.gamepad = null;
  76. }
  77. /**
  78. * Update the current camera state depending on the inputs that have been used this frame.
  79. * This is a dynamically created lambda to avoid the performance penalty of looping for inputs in the render loop.
  80. */
  81. public checkInputs(): void {
  82. if (this.gamepad) {
  83. var camera = this.camera;
  84. var RSValues = this.gamepad.rightStick;
  85. if (RSValues) {
  86. if (RSValues.x != 0) {
  87. var normalizedRX = RSValues.x / this.gamepadRotationSensibility;
  88. if (normalizedRX != 0 && Math.abs(normalizedRX) > 0.005) {
  89. camera.inertialAlphaOffset += normalizedRX;
  90. }
  91. }
  92. if (RSValues.y != 0) {
  93. var normalizedRY = (RSValues.y / this.gamepadRotationSensibility) * this._yAxisScale;
  94. if (normalizedRY != 0 && Math.abs(normalizedRY) > 0.005) {
  95. camera.inertialBetaOffset += normalizedRY;
  96. }
  97. }
  98. }
  99. var LSValues = this.gamepad.leftStick;
  100. if (LSValues && LSValues.y != 0) {
  101. var normalizedLY = LSValues.y / this.gamepadMoveSensibility;
  102. if (normalizedLY != 0 && Math.abs(normalizedLY) > 0.005) {
  103. this.camera.inertialRadiusOffset -= normalizedLY;
  104. }
  105. }
  106. }
  107. }
  108. /**
  109. * Gets the class name of the current intput.
  110. * @returns the class name
  111. */
  112. public getClassName(): string {
  113. return "ArcRotateCameraGamepadInput";
  114. }
  115. /**
  116. * Get the friendly name associated with the input class.
  117. * @returns the input friendly name
  118. */
  119. public getSimpleName(): string {
  120. return "gamepad";
  121. }
  122. }
  123. (<any>CameraInputTypes)["ArcRotateCameraGamepadInput"] = ArcRotateCameraGamepadInput;