arcRotateCameraGamepadInput.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { ICameraInput, ArcRotateCamera, CameraInputTypes } from "index";
  2. import { Nullable } from "types";
  3. import { serialize, Observer } from "Tools";
  4. import {Gamepad} from "Gamepad";
  5. /**
  6. * Manage the gamepad inputs to control an arc rotate camera.
  7. * @see http://doc.babylonjs.com/how_to/customizing_camera_inputs
  8. */
  9. export class ArcRotateCameraGamepadInput implements ICameraInput<ArcRotateCamera> {
  10. /**
  11. * Defines the camera the input is attached to.
  12. */
  13. public camera: ArcRotateCamera;
  14. /**
  15. * Defines the gamepad the input is gathering event from.
  16. */
  17. public gamepad: Nullable<Gamepad>;
  18. /**
  19. * Defines the gamepad rotation sensiblity.
  20. * This is the threshold from when rotation starts to be accounted for to prevent jittering.
  21. */
  22. @serialize()
  23. public gamepadRotationSensibility = 80;
  24. /**
  25. * Defines the gamepad move sensiblity.
  26. * This is the threshold from when moving starts to be accounted for for to prevent jittering.
  27. */
  28. @serialize()
  29. public gamepadMoveSensibility = 40;
  30. private _onGamepadConnectedObserver : Nullable<Observer<Gamepad>>;
  31. private _onGamepadDisconnectedObserver : Nullable<Observer<Gamepad>>;
  32. /**
  33. * Attach the input controls to a specific dom element to get the input from.
  34. * @param element Defines the element the controls should be listened from
  35. * @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
  36. */
  37. public attachControl(element: HTMLElement, noPreventDefault?: boolean): void {
  38. let manager = this.camera.getScene().gamepadManager;
  39. this._onGamepadConnectedObserver = manager.onGamepadConnectedObservable.add((gamepad) => {
  40. if (gamepad.type !== Gamepad.POSE_ENABLED) {
  41. // prioritize XBOX gamepads.
  42. if (!this.gamepad || gamepad.type === Gamepad.XBOX) {
  43. this.gamepad = gamepad;
  44. }
  45. }
  46. });
  47. this._onGamepadDisconnectedObserver = manager.onGamepadDisconnectedObservable.add((gamepad) => {
  48. if (this.gamepad === gamepad) {
  49. this.gamepad = null;
  50. }
  51. });
  52. this.gamepad = manager.getGamepadByType(Gamepad.XBOX);
  53. }
  54. /**
  55. * Detach the current controls from the specified dom element.
  56. * @param element Defines the element to stop listening the inputs from
  57. */
  58. public detachControl(element: Nullable<HTMLElement>): void {
  59. this.camera.getScene().gamepadManager.onGamepadConnectedObservable.remove(this._onGamepadConnectedObserver);
  60. this.camera.getScene().gamepadManager.onGamepadDisconnectedObservable.remove(this._onGamepadDisconnectedObserver);
  61. this.gamepad = null;
  62. }
  63. /**
  64. * Update the current camera state depending on the inputs that have been used this frame.
  65. * This is a dynamically created lambda to avoid the performance penalty of looping for inputs in the render loop.
  66. */
  67. public checkInputs(): void {
  68. if (this.gamepad) {
  69. var camera = this.camera;
  70. var RSValues = this.gamepad.rightStick;
  71. if (RSValues) {
  72. if (RSValues.x != 0) {
  73. var normalizedRX = RSValues.x / this.gamepadRotationSensibility;
  74. if (normalizedRX != 0 && Math.abs(normalizedRX) > 0.005) {
  75. camera.inertialAlphaOffset += normalizedRX;
  76. }
  77. }
  78. if (RSValues.y != 0) {
  79. var normalizedRY = RSValues.y / this.gamepadRotationSensibility;
  80. if (normalizedRY != 0 && Math.abs(normalizedRY) > 0.005) {
  81. camera.inertialBetaOffset += normalizedRY;
  82. }
  83. }
  84. }
  85. var LSValues = this.gamepad.leftStick;
  86. if (LSValues && LSValues.y != 0) {
  87. var normalizedLY = LSValues.y / this.gamepadMoveSensibility;
  88. if (normalizedLY != 0 && Math.abs(normalizedLY) > 0.005) {
  89. this.camera.inertialRadiusOffset -= normalizedLY;
  90. }
  91. }
  92. }
  93. }
  94. /**
  95. * Gets the class name of the current intput.
  96. * @returns the class name
  97. */
  98. public getClassName(): string {
  99. return "ArcRotateCameraGamepadInput";
  100. }
  101. /**
  102. * Get the friendly name associated with the input class.
  103. * @returns the input friendly name
  104. */
  105. public getSimpleName(): string {
  106. return "gamepad";
  107. }
  108. }
  109. (<any>CameraInputTypes)["ArcRotateCameraGamepadInput"] = ArcRotateCameraGamepadInput;