babylon.arcrotatecamera.input.gamepad.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. module BABYLON {
  2. export class ArcRotateCameraGamepadInput implements ICameraInput<ArcRotateCamera> {
  3. camera : ArcRotateCamera;
  4. public gamepad: Gamepad;
  5. private _gamepads: Gamepads;
  6. @serialize()
  7. public gamepadRotationSensibility = 80;
  8. @serialize()
  9. public gamepadMoveSensibility = 40;
  10. constructor(){
  11. this._gamepads = new Gamepads((gamepad: Gamepad) => { this._onNewGameConnected(gamepad); });
  12. }
  13. attachCamera(camera : ArcRotateCamera){
  14. this.camera = camera;
  15. }
  16. detach(){
  17. this._gamepads.dispose();
  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 (gamepad.index === 0) {
  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. }