babylon.arcrotatecamera.input.gamepad.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. module BABYLON {
  2. export class ArcRotateCameraGamepadInput implements ICameraInput<ArcRotateCamera> {
  3. camera: ArcRotateCamera;
  4. public gamepad: Gamepad;
  5. private _gamepads: Gamepads<Gamepad>;
  6. @serialize()
  7. public gamepadRotationSensibility = 80;
  8. @serialize()
  9. public gamepadMoveSensibility = 40;
  10. attachControl(element: HTMLElement, noPreventDefault?: boolean) {
  11. this._gamepads = new Gamepads((gamepad: Gamepad) => { this._onNewGameConnected(gamepad); });
  12. }
  13. detachControl(element: HTMLElement) {
  14. if (this._gamepads) {
  15. this._gamepads.dispose();
  16. }
  17. this.gamepad = null;
  18. }
  19. checkInputs() {
  20. if (this.gamepad) {
  21. var camera = this.camera;
  22. var RSValues = this.gamepad.rightStick;
  23. if (RSValues.x != 0) {
  24. var normalizedRX = RSValues.x / this.gamepadRotationSensibility;
  25. if (normalizedRX != 0 && Math.abs(normalizedRX) > 0.005) {
  26. camera.inertialAlphaOffset += normalizedRX;
  27. }
  28. }
  29. if (RSValues.y != 0) {
  30. var normalizedRY = RSValues.y / this.gamepadRotationSensibility;
  31. if (normalizedRY != 0 && Math.abs(normalizedRY) > 0.005) {
  32. camera.inertialBetaOffset += normalizedRY;
  33. }
  34. }
  35. var LSValues = this.gamepad.leftStick;
  36. if (LSValues.y != 0) {
  37. var normalizedLY = LSValues.y / this.gamepadMoveSensibility;
  38. if (normalizedLY != 0 && Math.abs(normalizedLY) > 0.005) {
  39. this.camera.inertialRadiusOffset -= normalizedLY;
  40. }
  41. }
  42. }
  43. }
  44. private _onNewGameConnected(gamepad: Gamepad) {
  45. // Only the first gamepad can control the camera
  46. if (!this.gamepad && gamepad.type !== Gamepad.POSE_ENABLED) {
  47. this.gamepad = gamepad;
  48. }
  49. }
  50. getTypeName(): string {
  51. return "ArcRotateCameraGamepadInput";
  52. }
  53. getSimpleName() {
  54. return "gamepad";
  55. }
  56. }
  57. CameraInputTypes["ArcRotateCameraGamepadInput"] = ArcRotateCameraGamepadInput;
  58. }