babylon.touchCamera.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 touchAngularSensibility: number = 200000.0;
  13. public touchMoveSensibility: number = 250.0;
  14. constructor(name: string, position: Vector3, scene: Scene) {
  15. super(name, position, scene);
  16. }
  17. public _onLostFocus(e: FocusEvent): void {
  18. this._offsetX = null;
  19. this._offsetY = null;
  20. super._onLostFocus(e);
  21. }
  22. public attachControl(canvas: HTMLCanvasElement, noPreventDefault: boolean): void {
  23. var previousPosition;
  24. if (this._attachedCanvas) {
  25. return;
  26. }
  27. if (this._onPointerDown === undefined) {
  28. this._onPointerDown = (evt) => {
  29. if (evt.pointerType === "mouse") {
  30. return;
  31. }
  32. if (!noPreventDefault) {
  33. evt.preventDefault();
  34. }
  35. this._pointerPressed.push(evt.pointerId);
  36. if (this._pointerPressed.length !== 1) {
  37. return;
  38. }
  39. previousPosition = {
  40. x: evt.clientX,
  41. y: evt.clientY
  42. };
  43. };
  44. this._onPointerUp = (evt) => {
  45. if (evt.pointerType === "mouse") {
  46. return;
  47. }
  48. if (!noPreventDefault) {
  49. evt.preventDefault();
  50. }
  51. var index: number = this._pointerPressed.indexOf(evt.pointerId);
  52. if (index === -1) {
  53. return;
  54. }
  55. this._pointerPressed.splice(index, 1);
  56. if (index != 0) {
  57. return;
  58. }
  59. previousPosition = null;
  60. this._offsetX = null;
  61. this._offsetY = null;
  62. };
  63. this._onPointerMove = (evt) => {
  64. if (evt.pointerType === "mouse") {
  65. return;
  66. }
  67. if (!noPreventDefault) {
  68. evt.preventDefault();
  69. }
  70. if (!previousPosition) {
  71. return;
  72. }
  73. var index: number = this._pointerPressed.indexOf(evt.pointerId);
  74. if (index != 0) {
  75. return;
  76. }
  77. this._offsetX = evt.clientX - previousPosition.x;
  78. this._offsetY = -(evt.clientY - previousPosition.y);
  79. };
  80. }
  81. canvas.addEventListener("pointerdown", this._onPointerDown);
  82. canvas.addEventListener("pointerup", this._onPointerUp);
  83. canvas.addEventListener("pointerout", this._onPointerUp);
  84. canvas.addEventListener("pointermove", this._onPointerMove);
  85. super.attachControl(canvas);
  86. }
  87. public detachControl(canvas: HTMLCanvasElement): void {
  88. if (this._attachedCanvas !== canvas) {
  89. return;
  90. }
  91. canvas.removeEventListener("pointerdown", this._onPointerDown);
  92. canvas.removeEventListener("pointerup", this._onPointerUp);
  93. canvas.removeEventListener("pointerout", this._onPointerUp);
  94. canvas.removeEventListener("pointermove", this._onPointerMove);
  95. super.detachControl(canvas);
  96. }
  97. public _checkInputs(): void {
  98. if (this._offsetX) {
  99. this.cameraRotation.y += this._offsetX / this.touchAngularSensibility;
  100. if (this._pointerPressed.length > 1) {
  101. this.cameraRotation.x += -this._offsetY / this.touchAngularSensibility;
  102. } else {
  103. var speed = this._computeLocalCameraSpeed();
  104. var direction = new Vector3(0, 0, speed * this._offsetY / this.touchMoveSensibility);
  105. Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, 0, this._cameraRotationMatrix);
  106. this.cameraDirection.addInPlace(Vector3.TransformCoordinates(direction, this._cameraRotationMatrix));
  107. }
  108. }
  109. super._checkInputs();
  110. }
  111. public serialize(): any {
  112. var serializationObject = super.serialize();
  113. serializationObject.type = "TouchCamera";
  114. return serializationObject;
  115. }
  116. }
  117. }