babylon.arcrotatecamera.input.gamepad.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. constructor(){
  9. this._gamepads = new Gamepads((gamepad: Gamepad) => { this._onNewGameConnected(gamepad); });
  10. }
  11. attachCamera(camera : ArcRotateCamera){
  12. this.camera = camera;
  13. }
  14. detach(){
  15. this._gamepads.dispose();
  16. }
  17. checkInputs(){
  18. if (this.gamepad) {
  19. var camera = this.camera;
  20. var LSValues = this.gamepad.leftStick;
  21. if (LSValues.x != 0){
  22. var normalizedLX = LSValues.x / this.gamepadRotationSensibility;
  23. if (normalizedLX != 0 && Math.abs(normalizedLX) > 0.005) {
  24. camera.inertialAlphaOffset += normalizedLX;
  25. }
  26. }
  27. if (LSValues.y != 0){
  28. var normalizedLY = LSValues.y / this.gamepadRotationSensibility;
  29. if (normalizedLY != 0 && Math.abs(normalizedLY) > 0.005) {
  30. camera.inertialBetaOffset += normalizedLY;
  31. }
  32. }
  33. }
  34. }
  35. private _onNewGameConnected(gamepad: Gamepad) {
  36. // Only the first gamepad can control the camera
  37. if (gamepad.index === 0) {
  38. this.gamepad = gamepad;
  39. }
  40. }
  41. getTypeName(): string{
  42. return "ArcRotateCameraGamepadInput";
  43. }
  44. getSimpleName(){
  45. return "gamepad";
  46. }
  47. }
  48. CameraInputTypes["ArcRotateCameraGamepadInput"] = ArcRotateCameraGamepadInput;
  49. }