babylon.arcRotateCameraGamepadInput.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. module BABYLON {
  2. export class ArcRotateCameraGamepadInput implements ICameraInput<ArcRotateCamera> {
  3. camera: ArcRotateCamera;
  4. public gamepad: Gamepad;
  5. private _onGamepadConnectedObserver : Observer<Gamepad>;
  6. private _onGamepadDisconnectedObserver : Observer<Gamepad>;
  7. @serialize()
  8. public gamepadRotationSensibility = 80;
  9. @serialize()
  10. public gamepadMoveSensibility = 40;
  11. attachControl(element: HTMLElement, noPreventDefault?: boolean) {
  12. let manager = this.camera.getScene().gamepadManager;
  13. this._onGamepadConnectedObserver = manager.onGamepadConnectedObservable.add((gamepad) => {
  14. if (gamepad.type !== Gamepad.POSE_ENABLED) {
  15. // prioritize XBOX gamepads.
  16. if (!this.gamepad || gamepad.type === Gamepad.XBOX) {
  17. this.gamepad = gamepad;
  18. }
  19. }
  20. });
  21. this._onGamepadDisconnectedObserver = manager.onGamepadDisconnectedObservable.add((gamepad)=> {
  22. if (this.gamepad === gamepad) {
  23. this.gamepad = null;
  24. }
  25. });
  26. this.gamepad = manager.getGamepadByType(Gamepad.XBOX);
  27. }
  28. detachControl(element: HTMLElement) {
  29. this.camera.getScene().gamepadManager.onGamepadConnectedObservable.remove(this._onGamepadConnectedObserver);
  30. this.camera.getScene().gamepadManager.onGamepadDisconnectedObservable.remove(this._onGamepadDisconnectedObserver);
  31. this.gamepad = null;
  32. }
  33. checkInputs() {
  34. if (this.gamepad) {
  35. var camera = this.camera;
  36. var RSValues = this.gamepad.rightStick;
  37. if (RSValues) {
  38. if (RSValues.x != 0) {
  39. var normalizedRX = RSValues.x / this.gamepadRotationSensibility;
  40. if (normalizedRX != 0 && Math.abs(normalizedRX) > 0.005) {
  41. camera.inertialAlphaOffset += normalizedRX;
  42. }
  43. }
  44. if (RSValues.y != 0) {
  45. var normalizedRY = RSValues.y / this.gamepadRotationSensibility;
  46. if (normalizedRY != 0 && Math.abs(normalizedRY) > 0.005) {
  47. camera.inertialBetaOffset += normalizedRY;
  48. }
  49. }
  50. }
  51. var LSValues = this.gamepad.leftStick;
  52. if (LSValues && LSValues.y != 0) {
  53. var normalizedLY = LSValues.y / this.gamepadMoveSensibility;
  54. if (normalizedLY != 0 && Math.abs(normalizedLY) > 0.005) {
  55. this.camera.inertialRadiusOffset -= normalizedLY;
  56. }
  57. }
  58. }
  59. }
  60. getClassName(): string {
  61. return "ArcRotateCameraGamepadInput";
  62. }
  63. getSimpleName() {
  64. return "gamepad";
  65. }
  66. }
  67. CameraInputTypes["ArcRotateCameraGamepadInput"] = ArcRotateCameraGamepadInput;
  68. }