babylon.arcrotatecamera.input.pointers.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 eventPrefix = BABYLON.Tools.GetPointerPrefix();
  10. var ArcRotateCameraPointersInput = (function () {
  11. function ArcRotateCameraPointersInput() {
  12. this.angularSensibilityX = 1000.0;
  13. this.angularSensibilityY = 1000.0;
  14. this.pinchPrecision = 6.0;
  15. this.panningSensibility = 50.0;
  16. this._isPanClick = false;
  17. this.pinchInwards = true;
  18. }
  19. ArcRotateCameraPointersInput.prototype.attachControl = function (element, noPreventDefault) {
  20. var _this = this;
  21. var engine = this.camera.getEngine();
  22. var cacheSoloPointer; // cache pointer object for better perf on camera rotation
  23. var pointA, pointB;
  24. var previousPinchDistance = 0;
  25. this._pointerInput = function (p, s) {
  26. var evt = p.event;
  27. if (p.type === BABYLON.PointerEventTypes.POINTERDOWN) {
  28. try {
  29. evt.srcElement.setPointerCapture(evt.pointerId);
  30. }
  31. catch (e) {
  32. }
  33. // Manage panning with pan button click
  34. _this._isPanClick = evt.button === _this.camera._panningMouseButton;
  35. // manage pointers
  36. cacheSoloPointer = { x: evt.clientX, y: evt.clientY, pointerId: evt.pointerId, type: evt.pointerType };
  37. if (pointA === undefined) {
  38. pointA = cacheSoloPointer;
  39. }
  40. else if (pointB === undefined) {
  41. pointB = cacheSoloPointer;
  42. }
  43. if (!noPreventDefault) {
  44. evt.preventDefault();
  45. element.focus();
  46. }
  47. }
  48. else if (p.type === BABYLON.PointerEventTypes.POINTERUP) {
  49. try {
  50. evt.srcElement.releasePointerCapture(evt.pointerId);
  51. }
  52. catch (e) {
  53. }
  54. cacheSoloPointer = null;
  55. previousPinchDistance = 0;
  56. //would be better to use pointers.remove(evt.pointerId) for multitouch gestures,
  57. //but emptying completly pointers collection is required to fix a bug on iPhone :
  58. //when changing orientation while pinching camera, one pointer stay pressed forever if we don't release all pointers
  59. //will be ok to put back pointers.remove(evt.pointerId); when iPhone bug corrected
  60. pointA = pointB = undefined;
  61. if (!noPreventDefault) {
  62. evt.preventDefault();
  63. }
  64. }
  65. else if (p.type === BABYLON.PointerEventTypes.POINTERMOVE) {
  66. if (!noPreventDefault) {
  67. evt.preventDefault();
  68. }
  69. // One button down
  70. if (pointA && pointB === undefined) {
  71. if (_this.panningSensibility !== 0 &&
  72. ((evt.ctrlKey && _this.camera._useCtrlForPanning) ||
  73. (!_this.camera._useCtrlForPanning && _this._isPanClick))) {
  74. _this.camera
  75. .inertialPanningX += -(evt.clientX - cacheSoloPointer.x) / _this.panningSensibility;
  76. _this.camera
  77. .inertialPanningY += (evt.clientY - cacheSoloPointer.y) / _this.panningSensibility;
  78. }
  79. else {
  80. var offsetX = evt.clientX - cacheSoloPointer.x;
  81. var offsetY = evt.clientY - cacheSoloPointer.y;
  82. _this.camera.inertialAlphaOffset -= offsetX / _this.angularSensibilityX;
  83. _this.camera.inertialBetaOffset -= offsetY / _this.angularSensibilityY;
  84. }
  85. cacheSoloPointer.x = evt.clientX;
  86. cacheSoloPointer.y = evt.clientY;
  87. }
  88. else if (pointA && pointB) {
  89. //if (noPreventDefault) { evt.preventDefault(); } //if pinch gesture, could be useful to force preventDefault to avoid html page scroll/zoom in some mobile browsers
  90. var ed = (pointA.pointerId === evt.pointerId) ? pointA : pointB;
  91. ed.x = evt.clientX;
  92. ed.y = evt.clientY;
  93. var direction = _this.pinchInwards ? 1 : -1;
  94. var distX = pointA.x - pointB.x;
  95. var distY = pointA.y - pointB.y;
  96. var pinchSquaredDistance = (distX * distX) + (distY * distY);
  97. if (previousPinchDistance === 0) {
  98. previousPinchDistance = pinchSquaredDistance;
  99. return;
  100. }
  101. if (pinchSquaredDistance !== previousPinchDistance) {
  102. _this.camera
  103. .inertialRadiusOffset += (pinchSquaredDistance - previousPinchDistance) /
  104. (_this.pinchPrecision *
  105. ((_this.angularSensibilityX + _this.angularSensibilityY) / 2) *
  106. direction);
  107. previousPinchDistance = pinchSquaredDistance;
  108. }
  109. }
  110. }
  111. };
  112. this._observer = this.camera.getScene().onPointerObservable.add(this._pointerInput, BABYLON.PointerEventTypes.POINTERDOWN | BABYLON.PointerEventTypes.POINTERUP | BABYLON.PointerEventTypes.POINTERMOVE);
  113. this._onContextMenu = function (evt) {
  114. evt.preventDefault();
  115. };
  116. if (!this.camera._useCtrlForPanning) {
  117. element.addEventListener("contextmenu", this._onContextMenu, false);
  118. }
  119. this._onLostFocus = function () {
  120. //this._keys = [];
  121. pointA = pointB = undefined;
  122. previousPinchDistance = 0;
  123. cacheSoloPointer = null;
  124. };
  125. this._onMouseMove = function (evt) {
  126. if (!engine.isPointerLock) {
  127. return;
  128. }
  129. var offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
  130. var offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
  131. _this.camera.inertialAlphaOffset -= offsetX / _this.angularSensibilityX;
  132. _this.camera.inertialBetaOffset -= offsetY / _this.angularSensibilityY;
  133. if (!noPreventDefault) {
  134. evt.preventDefault();
  135. }
  136. };
  137. this._onGestureStart = function (e) {
  138. if (window.MSGesture === undefined) {
  139. return;
  140. }
  141. if (!_this._MSGestureHandler) {
  142. _this._MSGestureHandler = new MSGesture();
  143. _this._MSGestureHandler.target = element;
  144. }
  145. _this._MSGestureHandler.addPointer(e.pointerId);
  146. };
  147. this._onGesture = function (e) {
  148. _this.camera.radius *= e.scale;
  149. if (e.preventDefault) {
  150. if (!noPreventDefault) {
  151. e.stopPropagation();
  152. e.preventDefault();
  153. }
  154. }
  155. };
  156. element.addEventListener("mousemove", this._onMouseMove, false);
  157. element.addEventListener("MSPointerDown", this._onGestureStart, false);
  158. element.addEventListener("MSGestureChange", this._onGesture, false);
  159. element.addEventListener("keydown", this._onKeyDown, false);
  160. element.addEventListener("keyup", this._onKeyUp, false);
  161. BABYLON.Tools.RegisterTopRootEvents([
  162. { name: "blur", handler: this._onLostFocus }
  163. ]);
  164. };
  165. ArcRotateCameraPointersInput.prototype.detachControl = function (element) {
  166. if (element && this._observer) {
  167. this.camera.getScene().onPointerObservable.remove(this._observer);
  168. this._observer = null;
  169. element.removeEventListener("contextmenu", this._onContextMenu);
  170. element.removeEventListener("mousemove", this._onMouseMove);
  171. element.removeEventListener("MSPointerDown", this._onGestureStart);
  172. element.removeEventListener("MSGestureChange", this._onGesture);
  173. element.removeEventListener("keydown", this._onKeyDown);
  174. element.removeEventListener("keyup", this._onKeyUp);
  175. this._isPanClick = false;
  176. this.pinchInwards = true;
  177. this._onKeyDown = null;
  178. this._onKeyUp = null;
  179. this._onMouseMove = null;
  180. this._onGestureStart = null;
  181. this._onGesture = null;
  182. this._MSGestureHandler = null;
  183. this._onLostFocus = null;
  184. this._onContextMenu = null;
  185. }
  186. BABYLON.Tools.UnregisterTopRootEvents([
  187. { name: "blur", handler: this._onLostFocus }
  188. ]);
  189. };
  190. ArcRotateCameraPointersInput.prototype.getTypeName = function () {
  191. return "ArcRotateCameraPointersInput";
  192. };
  193. ArcRotateCameraPointersInput.prototype.getSimpleName = function () {
  194. return "pointers";
  195. };
  196. __decorate([
  197. BABYLON.serialize()
  198. ], ArcRotateCameraPointersInput.prototype, "angularSensibilityX", void 0);
  199. __decorate([
  200. BABYLON.serialize()
  201. ], ArcRotateCameraPointersInput.prototype, "angularSensibilityY", void 0);
  202. __decorate([
  203. BABYLON.serialize()
  204. ], ArcRotateCameraPointersInput.prototype, "pinchPrecision", void 0);
  205. __decorate([
  206. BABYLON.serialize()
  207. ], ArcRotateCameraPointersInput.prototype, "panningSensibility", void 0);
  208. return ArcRotateCameraPointersInput;
  209. }());
  210. BABYLON.ArcRotateCameraPointersInput = ArcRotateCameraPointersInput;
  211. BABYLON.CameraInputTypes["ArcRotateCameraPointersInput"] = ArcRotateCameraPointersInput;
  212. })(BABYLON || (BABYLON = {}));