babylon.touchCamera.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. module BABYLON {
  2. // We're mainly based on the logic defined into the FreeCamera code
  3. export class TouchCamera extends FreeCamera {
  4. private _offsetX: number = null;
  5. private _offsetY: number = null;
  6. private _pointerCount:number = 0;
  7. private _pointerPressed = [];
  8. private _attachedCanvas: HTMLCanvasElement;
  9. private _onPointerDown: (e: PointerEvent) => any;
  10. private _onPointerUp: (e: PointerEvent) => any;
  11. private _onPointerMove: (e: PointerEvent) => any;
  12. public angularSensibility: number = 200000.0;
  13. public moveSensibility: number = 500.0;
  14. constructor(name: string, position: Vector3, scene: Scene) {
  15. super(name, position, scene);
  16. }
  17. public attachControl(canvas: HTMLCanvasElement, noPreventDefault: boolean): void {
  18. var previousPosition;
  19. if (this._attachedCanvas) {
  20. return;
  21. }
  22. this._attachedCanvas = canvas;
  23. if (this._onPointerDown === undefined) {
  24. this._onPointerDown = (evt) => {
  25. if (!noPreventDefault) {
  26. evt.preventDefault();
  27. }
  28. this._pointerPressed.push(evt.pointerId);
  29. if (this._pointerPressed.length !== 1) {
  30. return;
  31. }
  32. previousPosition = {
  33. x: evt.clientX,
  34. y: evt.clientY
  35. };
  36. };
  37. this._onPointerUp = (evt) => {
  38. if (!noPreventDefault) {
  39. evt.preventDefault();
  40. }
  41. var index: number = this._pointerPressed.indexOf(evt.pointerId);
  42. if (index === -1) {
  43. return;
  44. }
  45. this._pointerPressed.splice(index, 1);
  46. if (index != 0) {
  47. return;
  48. }
  49. previousPosition = null;
  50. this._offsetX = null;
  51. this._offsetY = null;
  52. };
  53. this._onPointerMove = (evt) => {
  54. if (!noPreventDefault) {
  55. evt.preventDefault();
  56. }
  57. if (!previousPosition) {
  58. return;
  59. }
  60. var index: number = this._pointerPressed.indexOf(evt.pointerId);
  61. if (index != 0) {
  62. return;
  63. }
  64. this._offsetX = evt.clientX - previousPosition.x;
  65. this._offsetY = -(evt.clientY - previousPosition.y);
  66. };
  67. this._onLostFocus = () => {
  68. this._offsetX = null;
  69. this._offsetY = null;
  70. };
  71. }
  72. canvas.addEventListener("pointerdown", this._onPointerDown);
  73. canvas.addEventListener("pointerup", this._onPointerUp);
  74. canvas.addEventListener("pointerout", this._onPointerUp);
  75. canvas.addEventListener("pointermove", this._onPointerMove);
  76. BABYLON.Tools.RegisterTopRootEvents([
  77. { name: "blur", handler: this._onLostFocus }
  78. ]);
  79. }
  80. public detachControl(canvas: HTMLCanvasElement): void {
  81. if (this._attachedCanvas != canvas) {
  82. return;
  83. }
  84. canvas.removeEventListener("pointerdown", this._onPointerDown);
  85. canvas.removeEventListener("pointerup", this._onPointerUp);
  86. canvas.removeEventListener("pointerout", this._onPointerUp);
  87. canvas.removeEventListener("pointermove", this._onPointerMove);
  88. BABYLON.Tools.UnregisterTopRootEvents([
  89. { name: "blur", handler: this._onLostFocus }
  90. ]);
  91. this._attachedCanvas = null;
  92. }
  93. public _checkInputs(): void {
  94. if (!this._offsetX) {
  95. return;
  96. }
  97. this.cameraRotation.y += this._offsetX / this.angularSensibility;
  98. if (this._pointerPressed.length > 1) {
  99. this.cameraRotation.x += -this._offsetY / this.angularSensibility;
  100. } else {
  101. var speed = this._computeLocalCameraSpeed();
  102. var direction = new BABYLON.Vector3(0, 0, speed * this._offsetY / this.moveSensibility);
  103. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, 0, this._cameraRotationMatrix);
  104. this.cameraDirection.addInPlace(BABYLON.Vector3.TransformCoordinates(direction, this._cameraRotationMatrix));
  105. }
  106. super._checkInputs();
  107. }
  108. }
  109. }