babylon.touchCamera.js 5.0 KB

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