babylon.freecamera.input.touch.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. var BABYLON;
  8. (function (BABYLON) {
  9. var FreeCameraTouchInput = (function () {
  10. function FreeCameraTouchInput() {
  11. this._offsetX = null;
  12. this._offsetY = null;
  13. this._pointerCount = 0;
  14. this._pointerPressed = [];
  15. this.touchAngularSensibility = 200000.0;
  16. this.touchMoveSensibility = 250.0;
  17. }
  18. FreeCameraTouchInput.prototype.attachControl = function (element, noPreventDefault) {
  19. var _this = this;
  20. var previousPosition;
  21. if (this._pointerInput === undefined) {
  22. this._onLostFocus = function (evt) {
  23. _this._offsetX = null;
  24. _this._offsetY = null;
  25. };
  26. this._pointerInput = function (p, s) {
  27. var evt = p.event;
  28. if (evt.pointerType === "mouse") {
  29. return;
  30. }
  31. if (p.type === BABYLON.PointerEventTypes.POINTERDOWN) {
  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. else if (p.type === BABYLON.PointerEventTypes.POINTERUP) {
  45. if (!noPreventDefault) {
  46. evt.preventDefault();
  47. }
  48. var index = _this._pointerPressed.indexOf(evt.pointerId);
  49. if (index === -1) {
  50. return;
  51. }
  52. _this._pointerPressed.splice(index, 1);
  53. if (index != 0) {
  54. return;
  55. }
  56. previousPosition = null;
  57. _this._offsetX = null;
  58. _this._offsetY = null;
  59. }
  60. else if (p.type === BABYLON.PointerEventTypes.POINTERMOVE) {
  61. if (!noPreventDefault) {
  62. evt.preventDefault();
  63. }
  64. if (!previousPosition) {
  65. return;
  66. }
  67. var index = _this._pointerPressed.indexOf(evt.pointerId);
  68. if (index != 0) {
  69. return;
  70. }
  71. _this._offsetX = evt.clientX - previousPosition.x;
  72. _this._offsetY = -(evt.clientY - previousPosition.y);
  73. }
  74. };
  75. }
  76. this._observer = this.camera.getScene().onPointerObservable.add(this._pointerInput, BABYLON.PointerEventTypes.POINTERDOWN | BABYLON.PointerEventTypes.POINTERUP | BABYLON.PointerEventTypes.POINTERMOVE);
  77. element.addEventListener("blur", this._onLostFocus);
  78. };
  79. FreeCameraTouchInput.prototype.detachControl = function (element) {
  80. if (this._pointerInput && element) {
  81. this.camera.getScene().onPointerObservable.remove(this._observer);
  82. this._observer = null;
  83. element.removeEventListener("blur", this._onLostFocus);
  84. this._onLostFocus = null;
  85. this._pointerPressed = [];
  86. this._offsetX = null;
  87. this._offsetY = null;
  88. this._pointerCount = 0;
  89. }
  90. };
  91. FreeCameraTouchInput.prototype.checkInputs = function () {
  92. if (this._offsetX) {
  93. var camera = this.camera;
  94. camera.cameraRotation.y += this._offsetX / this.touchAngularSensibility;
  95. if (this._pointerPressed.length > 1) {
  96. camera.cameraRotation.x += -this._offsetY / this.touchAngularSensibility;
  97. }
  98. else {
  99. var speed = camera._computeLocalCameraSpeed();
  100. var direction = new BABYLON.Vector3(0, 0, speed * this._offsetY / this.touchMoveSensibility);
  101. BABYLON.Matrix.RotationYawPitchRollToRef(camera.rotation.y, camera.rotation.x, 0, camera._cameraRotationMatrix);
  102. camera.cameraDirection.addInPlace(BABYLON.Vector3.TransformCoordinates(direction, camera._cameraRotationMatrix));
  103. }
  104. }
  105. };
  106. FreeCameraTouchInput.prototype.getTypeName = function () {
  107. return "FreeCameraTouchInput";
  108. };
  109. FreeCameraTouchInput.prototype.getSimpleName = function () {
  110. return "touch";
  111. };
  112. __decorate([
  113. BABYLON.serialize()
  114. ], FreeCameraTouchInput.prototype, "touchAngularSensibility", void 0);
  115. __decorate([
  116. BABYLON.serialize()
  117. ], FreeCameraTouchInput.prototype, "touchMoveSensibility", void 0);
  118. return FreeCameraTouchInput;
  119. })();
  120. BABYLON.FreeCameraTouchInput = FreeCameraTouchInput;
  121. BABYLON.CameraInputTypes["FreeCameraTouchInput"] = FreeCameraTouchInput;
  122. })(BABYLON || (BABYLON = {}));