babylon.freeCameraTouchInput.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. module BABYLON {
  2. export class FreeCameraTouchInput implements ICameraInput<FreeCamera> {
  3. camera: FreeCamera;
  4. private _offsetX: Nullable<number> = null;
  5. private _offsetY: Nullable<number> = null;
  6. private _pointerCount: number = 0;
  7. private _pointerPressed = new Array<number>();
  8. private _pointerInput: (p: PointerInfo, s: EventState) => void;
  9. private _observer: Nullable<Observer<PointerInfo>>;
  10. private _onLostFocus: Nullable<(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: Nullable<{x: number, y: number}> = null;
  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. if (this._onLostFocus) {
  74. element.addEventListener("blur", this._onLostFocus);
  75. }
  76. }
  77. detachControl(element: Nullable<HTMLElement>) {
  78. if (this._pointerInput && element) {
  79. if (this._observer) {
  80. this.camera.getScene().onPointerObservable.remove(this._observer);
  81. this._observer = null;
  82. }
  83. if (this._onLostFocus) {
  84. element.removeEventListener("blur", this._onLostFocus);
  85. this._onLostFocus = null;
  86. }
  87. this._pointerPressed = [];
  88. this._offsetX = null;
  89. this._offsetY = null;
  90. this._pointerCount = 0;
  91. }
  92. }
  93. checkInputs() {
  94. if (this._offsetX && this._offsetY) {
  95. var camera = this.camera;
  96. camera.cameraRotation.y += this._offsetX / this.touchAngularSensibility;
  97. if (this._pointerPressed.length > 1) {
  98. camera.cameraRotation.x += -this._offsetY / this.touchAngularSensibility;
  99. } else {
  100. var speed = camera._computeLocalCameraSpeed();
  101. var direction = new Vector3(0, 0, speed * this._offsetY / this.touchMoveSensibility);
  102. Matrix.RotationYawPitchRollToRef(camera.rotation.y, camera.rotation.x, 0, camera._cameraRotationMatrix);
  103. camera.cameraDirection.addInPlace(Vector3.TransformCoordinates(direction, camera._cameraRotationMatrix));
  104. }
  105. }
  106. }
  107. getClassName(): string {
  108. return "FreeCameraTouchInput";
  109. }
  110. getSimpleName() {
  111. return "touch";
  112. }
  113. }
  114. (<any>CameraInputTypes)["FreeCameraTouchInput"] = FreeCameraTouchInput;
  115. }