arcRotateCameraGamepadInput.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 http://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 _onGamepadConnectedObserver : Nullable<Observer<Gamepad>>;
  33. private _onGamepadDisconnectedObserver : Nullable<Observer<Gamepad>>;
  34. /**
  35. * Attach the input controls to a specific dom element to get the input from.
  36. * @param element Defines the element the controls should be listened from
  37. * @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
  38. */
  39. public attachControl(element: HTMLElement, noPreventDefault?: boolean): void {
  40. let manager = this.camera.getScene().gamepadManager;
  41. this._onGamepadConnectedObserver = manager.onGamepadConnectedObservable.add((gamepad) => {
  42. if (gamepad.type !== Gamepad.POSE_ENABLED) {
  43. // prioritize XBOX gamepads.
  44. if (!this.gamepad || gamepad.type === Gamepad.XBOX) {
  45. this.gamepad = gamepad;
  46. }
  47. }
  48. });
  49. this._onGamepadDisconnectedObserver = manager.onGamepadDisconnectedObservable.add((gamepad) => {
  50. if (this.gamepad === gamepad) {
  51. this.gamepad = null;
  52. }
  53. });
  54. this.gamepad = manager.getGamepadByType(Gamepad.XBOX);
  55. }
  56. /**
  57. * Detach the current controls from the specified dom element.
  58. * @param element Defines the element to stop listening the inputs from
  59. */
  60. public detachControl(element: Nullable<HTMLElement>): void {
  61. this.camera.getScene().gamepadManager.onGamepadConnectedObservable.remove(this._onGamepadConnectedObserver);
  62. this.camera.getScene().gamepadManager.onGamepadDisconnectedObservable.remove(this._onGamepadDisconnectedObserver);
  63. this.gamepad = null;
  64. }
  65. /**
  66. * Update the current camera state depending on the inputs that have been used this frame.
  67. * This is a dynamically created lambda to avoid the performance penalty of looping for inputs in the render loop.
  68. */
  69. public checkInputs(): void {
  70. if (this.gamepad) {
  71. var camera = this.camera;
  72. var RSValues = this.gamepad.rightStick;
  73. if (RSValues) {
  74. if (RSValues.x != 0) {
  75. var normalizedRX = RSValues.x / this.gamepadRotationSensibility;
  76. if (normalizedRX != 0 && Math.abs(normalizedRX) > 0.005) {
  77. camera.inertialAlphaOffset += normalizedRX;
  78. }
  79. }
  80. if (RSValues.y != 0) {
  81. var normalizedRY = RSValues.y / this.gamepadRotationSensibility;
  82. if (normalizedRY != 0 && Math.abs(normalizedRY) > 0.005) {
  83. camera.inertialBetaOffset += normalizedRY;
  84. }
  85. }
  86. }
  87. var LSValues = this.gamepad.leftStick;
  88. if (LSValues && LSValues.y != 0) {
  89. var normalizedLY = LSValues.y / this.gamepadMoveSensibility;
  90. if (normalizedLY != 0 && Math.abs(normalizedLY) > 0.005) {
  91. this.camera.inertialRadiusOffset -= normalizedLY;
  92. }
  93. }
  94. }
  95. }
  96. /**
  97. * Gets the class name of the current intput.
  98. * @returns the class name
  99. */
  100. public getClassName(): string {
  101. return "ArcRotateCameraGamepadInput";
  102. }
  103. /**
  104. * Get the friendly name associated with the input class.
  105. * @returns the input friendly name
  106. */
  107. public getSimpleName(): string {
  108. return "gamepad";
  109. }
  110. }
  111. (<any>CameraInputTypes)["ArcRotateCameraGamepadInput"] = ArcRotateCameraGamepadInput;