babylon.arcrotatecamera.input.pointers.js 11 KB

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