babylon.arcrotatecamera.input.pointers.js 11 KB

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