babylon.freecamera.input.touch.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. module BABYLON {
  2. export class FreeCameraTouchInput implements ICameraInput<FreeCamera> {
  3. camera: FreeCamera;
  4. private _offsetX: number = null;
  5. private _offsetY: number = null;
  6. private _pointerCount: number = 0;
  7. private _pointerPressed = [];
  8. private _pointerInput: (p: PointerInfo, s: EventState) => void;
  9. private _observer: Observer<PointerInfo>;
  10. private _onLostFocus: (e: FocusEvent) => any;
  11. @serialize()
  12. public touchAngularSensibility: number = 200000.0;
  13. @serialize()
  14. public touchMoveSensibility: number = 250.0;
  15. attachControl(element: HTMLElement, noPreventDefault?: boolean) {
  16. var previousPosition;
  17. if (this._pointerInput === undefined) {
  18. this._onLostFocus = (evt) => {
  19. this._offsetX = null;
  20. this._offsetY = null;
  21. }
  22. this._pointerInput = (p, s) => {
  23. var evt = <PointerEvent>p.event;
  24. if (p.type === PointerEventType.PointerDown) {
  25. if (evt.pointerType === "mouse") {
  26. return;
  27. }
  28. if (!noPreventDefault) {
  29. evt.preventDefault();
  30. }
  31. evt.srcElement.setPointerCapture(evt.pointerId);
  32. this._pointerPressed.push(evt.pointerId);
  33. if (this._pointerPressed.length !== 1) {
  34. return;
  35. }
  36. previousPosition = {
  37. x: evt.clientX,
  38. y: evt.clientY
  39. };
  40. }
  41. else if (p.type === PointerEventType.PointerUp) {
  42. if (evt.pointerType === "mouse") {
  43. return;
  44. }
  45. if (!noPreventDefault) {
  46. evt.preventDefault();
  47. }
  48. evt.srcElement.releasePointerCapture(evt.pointerId);
  49. var index: number = this._pointerPressed.indexOf(evt.pointerId);
  50. if (index === -1) {
  51. return;
  52. }
  53. this._pointerPressed.splice(index, 1);
  54. if (index != 0) {
  55. return;
  56. }
  57. previousPosition = null;
  58. this._offsetX = null;
  59. this._offsetY = null;
  60. }
  61. else if (p.type === PointerEventType.PointerMove) {
  62. if (evt.pointerType === "mouse") {
  63. return;
  64. }
  65. if (!noPreventDefault) {
  66. evt.preventDefault();
  67. }
  68. if (!previousPosition) {
  69. return;
  70. }
  71. var index: number = this._pointerPressed.indexOf(evt.pointerId);
  72. if (index != 0) {
  73. return;
  74. }
  75. this._offsetX = evt.clientX - previousPosition.x;
  76. this._offsetY = -(evt.clientY - previousPosition.y);
  77. }
  78. }
  79. }
  80. element.addEventListener("blur", this._onLostFocus);
  81. }
  82. detachControl(element: HTMLElement) {
  83. if (this._pointerInput && element) {
  84. this.camera.getScene().onPointerObservable.remove(this._observer);
  85. this._observer = null;
  86. element.removeEventListener("blur", this._onLostFocus);
  87. this._onLostFocus = null;
  88. this._pointerPressed = [];
  89. this._offsetX = null;
  90. this._offsetY = null;
  91. this._pointerCount = 0;
  92. }
  93. }
  94. checkInputs() {
  95. if (this._offsetX) {
  96. var camera = this.camera;
  97. camera.cameraRotation.y += this._offsetX / this.touchAngularSensibility;
  98. if (this._pointerPressed.length > 1) {
  99. camera.cameraRotation.x += -this._offsetY / this.touchAngularSensibility;
  100. } else {
  101. var speed = camera._computeLocalCameraSpeed();
  102. var direction = new Vector3(0, 0, speed * this._offsetY / this.touchMoveSensibility);
  103. Matrix.RotationYawPitchRollToRef(camera.rotation.y, camera.rotation.x, 0, camera._cameraRotationMatrix);
  104. camera.cameraDirection.addInPlace(Vector3.TransformCoordinates(direction, camera._cameraRotationMatrix));
  105. }
  106. }
  107. }
  108. getTypeName(): string {
  109. return "FreeCameraTouchInput";
  110. }
  111. getSimpleName(){
  112. return "touch";
  113. }
  114. }
  115. CameraInputTypes["FreeCameraTouchInput"] = FreeCameraTouchInput;
  116. }