babylon.arcrotatecamera.input.pointers.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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._isRightClick = 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 right click
  35. _this._isRightClick = evt.button === 2;
  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. }
  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. ((_this._isCtrlPushed && _this.camera._useCtrlForPanning) ||
  73. (!_this.camera._useCtrlForPanning && _this._isRightClick))) {
  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._onKeyDown = function (evt) {
  126. _this._isCtrlPushed = evt.ctrlKey;
  127. };
  128. this._onKeyUp = function (evt) {
  129. _this._isCtrlPushed = evt.ctrlKey;
  130. };
  131. this._onMouseMove = function (evt) {
  132. if (!engine.isPointerLock) {
  133. return;
  134. }
  135. var offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
  136. var offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
  137. _this.camera.inertialAlphaOffset -= offsetX / _this.angularSensibilityX;
  138. _this.camera.inertialBetaOffset -= offsetY / _this.angularSensibilityY;
  139. if (!noPreventDefault) {
  140. evt.preventDefault();
  141. }
  142. };
  143. this._onGestureStart = function (e) {
  144. if (window.MSGesture === undefined) {
  145. return;
  146. }
  147. if (!_this._MSGestureHandler) {
  148. _this._MSGestureHandler = new MSGesture();
  149. _this._MSGestureHandler.target = element;
  150. }
  151. _this._MSGestureHandler.addPointer(e.pointerId);
  152. };
  153. this._onGesture = function (e) {
  154. _this.camera.radius *= e.scale;
  155. if (e.preventDefault) {
  156. if (!noPreventDefault) {
  157. e.stopPropagation();
  158. e.preventDefault();
  159. }
  160. }
  161. };
  162. element.addEventListener("mousemove", this._onMouseMove, false);
  163. element.addEventListener("MSPointerDown", this._onGestureStart, false);
  164. element.addEventListener("MSGestureChange", this._onGesture, false);
  165. BABYLON.Tools.RegisterTopRootEvents([
  166. { name: "keydown", handler: this._onKeyDown },
  167. { name: "keyup", handler: this._onKeyUp },
  168. { name: "blur", handler: this._onLostFocus }
  169. ]);
  170. };
  171. ArcRotateCameraPointersInput.prototype.detachControl = function (element) {
  172. if (element && this._observer) {
  173. this.camera.getScene().onPointerObservable.remove(this._observer);
  174. this._observer = null;
  175. element.removeEventListener("contextmenu", this._onContextMenu);
  176. element.removeEventListener("mousemove", this._onMouseMove);
  177. element.removeEventListener("MSPointerDown", this._onGestureStart);
  178. element.removeEventListener("MSGestureChange", this._onGesture);
  179. this._isRightClick = false;
  180. this._isCtrlPushed = false;
  181. this.pinchInwards = true;
  182. this._onKeyDown = null;
  183. this._onKeyUp = null;
  184. this._onMouseMove = null;
  185. this._onGestureStart = null;
  186. this._onGesture = null;
  187. this._MSGestureHandler = null;
  188. this._onLostFocus = null;
  189. this._onContextMenu = null;
  190. }
  191. BABYLON.Tools.UnregisterTopRootEvents([
  192. { name: "keydown", handler: this._onKeyDown },
  193. { name: "keyup", handler: this._onKeyUp },
  194. { name: "blur", handler: this._onLostFocus }
  195. ]);
  196. };
  197. ArcRotateCameraPointersInput.prototype.getTypeName = function () {
  198. return "ArcRotateCameraPointersInput";
  199. };
  200. ArcRotateCameraPointersInput.prototype.getSimpleName = function () {
  201. return "pointers";
  202. };
  203. __decorate([
  204. BABYLON.serialize()
  205. ], ArcRotateCameraPointersInput.prototype, "angularSensibilityX", void 0);
  206. __decorate([
  207. BABYLON.serialize()
  208. ], ArcRotateCameraPointersInput.prototype, "angularSensibilityY", void 0);
  209. __decorate([
  210. BABYLON.serialize()
  211. ], ArcRotateCameraPointersInput.prototype, "pinchPrecision", void 0);
  212. __decorate([
  213. BABYLON.serialize()
  214. ], ArcRotateCameraPointersInput.prototype, "panningSensibility", void 0);
  215. return ArcRotateCameraPointersInput;
  216. })();
  217. BABYLON.ArcRotateCameraPointersInput = ArcRotateCameraPointersInput;
  218. BABYLON.CameraInputTypes["ArcRotateCameraPointersInput"] = ArcRotateCameraPointersInput;
  219. })(BABYLON || (BABYLON = {}));