babylon.touchCamera.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. var __extends = (this && this.__extends) || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  5. };
  6. var BABYLON;
  7. (function (BABYLON) {
  8. // We're mainly based on the logic defined into the FreeCamera code
  9. var TouchCamera = (function (_super) {
  10. __extends(TouchCamera, _super);
  11. function TouchCamera(name, position, scene) {
  12. _super.call(this, name, position, scene);
  13. this._offsetX = null;
  14. this._offsetY = null;
  15. this._pointerCount = 0;
  16. this._pointerPressed = [];
  17. this.touchAngularSensibility = 200000.0;
  18. this.touchMoveSensibility = 250.0;
  19. }
  20. TouchCamera.prototype._onLostFocus = function (e) {
  21. this._offsetX = null;
  22. this._offsetY = null;
  23. _super.prototype._onLostFocus.call(this, e);
  24. };
  25. TouchCamera.prototype.attachControl = function (canvas, noPreventDefault) {
  26. var _this = this;
  27. var previousPosition;
  28. if (this._attachedCanvas) {
  29. return;
  30. }
  31. if (this._onPointerDown === undefined) {
  32. this._onPointerDown = function (evt) {
  33. if (evt.pointerType === "mouse") {
  34. return;
  35. }
  36. if (!noPreventDefault) {
  37. evt.preventDefault();
  38. }
  39. _this._pointerPressed.push(evt.pointerId);
  40. if (_this._pointerPressed.length !== 1) {
  41. return;
  42. }
  43. previousPosition = {
  44. x: evt.clientX,
  45. y: evt.clientY
  46. };
  47. };
  48. this._onPointerUp = function (evt) {
  49. if (evt.pointerType === "mouse") {
  50. return;
  51. }
  52. if (!noPreventDefault) {
  53. evt.preventDefault();
  54. }
  55. var index = _this._pointerPressed.indexOf(evt.pointerId);
  56. if (index === -1) {
  57. return;
  58. }
  59. _this._pointerPressed.splice(index, 1);
  60. if (index != 0) {
  61. return;
  62. }
  63. previousPosition = null;
  64. _this._offsetX = null;
  65. _this._offsetY = null;
  66. };
  67. this._onPointerMove = function (evt) {
  68. if (evt.pointerType === "mouse") {
  69. return;
  70. }
  71. if (!noPreventDefault) {
  72. evt.preventDefault();
  73. }
  74. if (!previousPosition) {
  75. return;
  76. }
  77. var index = _this._pointerPressed.indexOf(evt.pointerId);
  78. if (index != 0) {
  79. return;
  80. }
  81. _this._offsetX = evt.clientX - previousPosition.x;
  82. _this._offsetY = -(evt.clientY - previousPosition.y);
  83. };
  84. }
  85. canvas.addEventListener("pointerdown", this._onPointerDown);
  86. canvas.addEventListener("pointerup", this._onPointerUp);
  87. canvas.addEventListener("pointerout", this._onPointerUp);
  88. canvas.addEventListener("pointermove", this._onPointerMove);
  89. _super.prototype.attachControl.call(this, canvas);
  90. };
  91. TouchCamera.prototype.detachControl = function (canvas) {
  92. if (this._attachedCanvas !== canvas) {
  93. return;
  94. }
  95. canvas.removeEventListener("pointerdown", this._onPointerDown);
  96. canvas.removeEventListener("pointerup", this._onPointerUp);
  97. canvas.removeEventListener("pointerout", this._onPointerUp);
  98. canvas.removeEventListener("pointermove", this._onPointerMove);
  99. _super.prototype.detachControl.call(this, canvas);
  100. };
  101. TouchCamera.prototype._checkInputs = function () {
  102. if (this._offsetX) {
  103. this.cameraRotation.y += this._offsetX / this.touchAngularSensibility;
  104. if (this._pointerPressed.length > 1) {
  105. this.cameraRotation.x += -this._offsetY / this.touchAngularSensibility;
  106. }
  107. else {
  108. var speed = this._computeLocalCameraSpeed();
  109. var direction = new BABYLON.Vector3(0, 0, speed * this._offsetY / this.touchMoveSensibility);
  110. BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, 0, this._cameraRotationMatrix);
  111. this.cameraDirection.addInPlace(BABYLON.Vector3.TransformCoordinates(direction, this._cameraRotationMatrix));
  112. }
  113. }
  114. _super.prototype._checkInputs.call(this);
  115. };
  116. TouchCamera.prototype.serialize = function () {
  117. var serializationObject = _super.prototype.serialize.call(this);
  118. serializationObject.type = "TouchCamera";
  119. return serializationObject;
  120. };
  121. return TouchCamera;
  122. })(BABYLON.FreeCamera);
  123. BABYLON.TouchCamera = TouchCamera;
  124. })(BABYLON || (BABYLON = {}));