babylon.arcRotateCameraGamepadInput.ts 5.0 KB

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