babylon.freeCameraTouchInput.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 (evt.pointerType === "mouse") {
  25. return;
  26. }
  27. if (p.type === PointerEventTypes.POINTERDOWN) {
  28. if (!noPreventDefault) {
  29. evt.preventDefault();
  30. }
  31. this._pointerPressed.push(evt.pointerId);
  32. if (this._pointerPressed.length !== 1) {
  33. return;
  34. }
  35. previousPosition = {
  36. x: evt.clientX,
  37. y: evt.clientY
  38. };
  39. }
  40. else if (p.type === PointerEventTypes.POINTERUP) {
  41. if (!noPreventDefault) {
  42. evt.preventDefault();
  43. }
  44. var index: number = this._pointerPressed.indexOf(evt.pointerId);
  45. if (index === -1) {
  46. return;
  47. }
  48. this._pointerPressed.splice(index, 1);
  49. if (index != 0) {
  50. return;
  51. }
  52. previousPosition = null;
  53. this._offsetX = null;
  54. this._offsetY = null;
  55. }
  56. else if (p.type === PointerEventTypes.POINTERMOVE) {
  57. if (!noPreventDefault) {
  58. evt.preventDefault();
  59. }
  60. if (!previousPosition) {
  61. return;
  62. }
  63. var index: number = this._pointerPressed.indexOf(evt.pointerId);
  64. if (index != 0) {
  65. return;
  66. }
  67. this._offsetX = evt.clientX - previousPosition.x;
  68. this._offsetY = -(evt.clientY - previousPosition.y);
  69. }
  70. }
  71. }
  72. this._observer = this.camera.getScene().onPointerObservable.add(this._pointerInput, PointerEventTypes.POINTERDOWN | PointerEventTypes.POINTERUP | PointerEventTypes.POINTERMOVE);
  73. element.addEventListener("blur", this._onLostFocus);
  74. }
  75. detachControl(element: HTMLElement) {
  76. if (this._pointerInput && element) {
  77. this.camera.getScene().onPointerObservable.remove(this._observer);
  78. this._observer = null;
  79. element.removeEventListener("blur", this._onLostFocus);
  80. this._onLostFocus = null;
  81. this._pointerPressed = [];
  82. this._offsetX = null;
  83. this._offsetY = null;
  84. this._pointerCount = 0;
  85. }
  86. }
  87. checkInputs() {
  88. if (this._offsetX) {
  89. var camera = this.camera;
  90. camera.cameraRotation.y += this._offsetX / this.touchAngularSensibility;
  91. if (this._pointerPressed.length > 1) {
  92. camera.cameraRotation.x += -this._offsetY / this.touchAngularSensibility;
  93. } else {
  94. var speed = camera._computeLocalCameraSpeed();
  95. var direction = new Vector3(0, 0, speed * this._offsetY / this.touchMoveSensibility);
  96. Matrix.RotationYawPitchRollToRef(camera.rotation.y, camera.rotation.x, 0, camera._cameraRotationMatrix);
  97. camera.cameraDirection.addInPlace(Vector3.TransformCoordinates(direction, camera._cameraRotationMatrix));
  98. }
  99. }
  100. }
  101. getTypeName(): string {
  102. return "FreeCameraTouchInput";
  103. }
  104. getSimpleName() {
  105. return "touch";
  106. }
  107. }
  108. CameraInputTypes["FreeCameraTouchInput"] = FreeCameraTouchInput;
  109. }