arcRotateCameraPointersInput.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. import { serialize, EventState, Observer, Tools } from "Tools";
  2. import { Nullable } from "types";
  3. import { ICameraInput, ArcRotateCamera, CameraInputTypes } from "Cameras";
  4. import { PointerInfo, PointerEventTypes } from "Events";
  5. /**
  6. * Manage the pointers inputs to control an arc rotate camera.
  7. * @see http://doc.babylonjs.com/how_to/customizing_camera_inputs
  8. */
  9. export class ArcRotateCameraPointersInput implements ICameraInput<ArcRotateCamera> {
  10. /**
  11. * Defines the camera the input is attached to.
  12. */
  13. public camera: ArcRotateCamera;
  14. /**
  15. * Defines the buttons associated with the input to handle camera move.
  16. */
  17. @serialize()
  18. public buttons = [0, 1, 2];
  19. /**
  20. * Defines the pointer angular sensibility along the X axis or how fast is the camera rotating.
  21. */
  22. @serialize()
  23. public angularSensibilityX = 1000.0;
  24. /**
  25. * Defines the pointer angular sensibility along the Y axis or how fast is the camera rotating.
  26. */
  27. @serialize()
  28. public angularSensibilityY = 1000.0;
  29. /**
  30. * Defines the pointer pinch precision or how fast is the camera zooming.
  31. */
  32. @serialize()
  33. public pinchPrecision = 12.0;
  34. /**
  35. * pinchDeltaPercentage will be used instead of pinchPrecision if different from 0.
  36. * It defines the percentage of current camera.radius to use as delta when pinch zoom is used.
  37. */
  38. @serialize()
  39. public pinchDeltaPercentage = 0;
  40. /**
  41. * Defines the pointer panning sensibility or how fast is the camera moving.
  42. */
  43. @serialize()
  44. public panningSensibility: number = 1000.0;
  45. /**
  46. * Defines whether panning (2 fingers swipe) is enabled through multitouch.
  47. */
  48. @serialize()
  49. public multiTouchPanning: boolean = true;
  50. /**
  51. * Defines whether panning is enabled for both pan (2 fingers swipe) and zoom (pinch) through multitouch.
  52. */
  53. @serialize()
  54. public multiTouchPanAndZoom: boolean = true;
  55. /**
  56. * Revers pinch action direction.
  57. */
  58. public pinchInwards = true;
  59. private _isPanClick: boolean = false;
  60. private _pointerInput: (p: PointerInfo, s: EventState) => void;
  61. private _observer: Nullable<Observer<PointerInfo>>;
  62. private _onMouseMove: Nullable<(e: MouseEvent) => any>;
  63. private _onGestureStart: Nullable<(e: PointerEvent) => void>;
  64. private _onGesture: Nullable<(e: MSGestureEvent) => void>;
  65. private _MSGestureHandler: Nullable<MSGesture>;
  66. private _onLostFocus: Nullable<(e: FocusEvent) => any>;
  67. private _onContextMenu: Nullable<(e: Event) => void>;
  68. /**
  69. * Attach the input controls to a specific dom element to get the input from.
  70. * @param element Defines the element the controls should be listened from
  71. * @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
  72. */
  73. public attachControl(element: HTMLElement, noPreventDefault?: boolean): void {
  74. var engine = this.camera.getEngine();
  75. var cacheSoloPointer: Nullable<{ x: number, y: number, pointerId: number, type: any }>; // cache pointer object for better perf on camera rotation
  76. var pointA: Nullable<{ x: number, y: number, pointerId: number, type: any }> = null;
  77. var pointB: Nullable<{ x: number, y: number, pointerId: number, type: any }> = null;
  78. var previousPinchSquaredDistance = 0;
  79. var initialDistance = 0;
  80. var twoFingerActivityCount = 0;
  81. var previousMultiTouchPanPosition: { x: number, y: number, isPaning: boolean, isPinching: boolean } = { x: 0, y: 0, isPaning: false, isPinching: false };
  82. this._pointerInput = (p, s) => {
  83. var evt = <PointerEvent>p.event;
  84. let isTouch = (<any>p.event).pointerType === "touch";
  85. if (engine.isInVRExclusivePointerMode) {
  86. return;
  87. }
  88. if (p.type !== PointerEventTypes.POINTERMOVE && this.buttons.indexOf(evt.button) === -1) {
  89. return;
  90. }
  91. let srcElement = <HTMLElement>(evt.srcElement || evt.target);
  92. if (p.type === PointerEventTypes.POINTERDOWN && srcElement) {
  93. try {
  94. srcElement.setPointerCapture(evt.pointerId);
  95. } catch (e) {
  96. //Nothing to do with the error. Execution will continue.
  97. }
  98. // Manage panning with pan button click
  99. this._isPanClick = evt.button === this.camera._panningMouseButton;
  100. // manage pointers
  101. cacheSoloPointer = { x: evt.clientX, y: evt.clientY, pointerId: evt.pointerId, type: evt.pointerType };
  102. if (pointA === null) {
  103. pointA = cacheSoloPointer;
  104. }
  105. else if (pointB === null) {
  106. pointB = cacheSoloPointer;
  107. }
  108. if (!noPreventDefault) {
  109. evt.preventDefault();
  110. element.focus();
  111. }
  112. }
  113. else if (p.type === PointerEventTypes.POINTERDOUBLETAP) {
  114. if (this.camera.useInputToRestoreState) {
  115. this.camera.restoreState();
  116. }
  117. }
  118. else if (p.type === PointerEventTypes.POINTERUP && srcElement) {
  119. try {
  120. srcElement.releasePointerCapture(evt.pointerId);
  121. } catch (e) {
  122. //Nothing to do with the error.
  123. }
  124. cacheSoloPointer = null;
  125. previousPinchSquaredDistance = 0;
  126. previousMultiTouchPanPosition.isPaning = false;
  127. previousMultiTouchPanPosition.isPinching = false;
  128. twoFingerActivityCount = 0;
  129. initialDistance = 0;
  130. if (!isTouch) {
  131. pointB = null; // Mouse and pen are mono pointer
  132. }
  133. //would be better to use pointers.remove(evt.pointerId) for multitouch gestures,
  134. //but emptying completly pointers collection is required to fix a bug on iPhone :
  135. //when changing orientation while pinching camera, one pointer stay pressed forever if we don't release all pointers
  136. //will be ok to put back pointers.remove(evt.pointerId); when iPhone bug corrected
  137. if (engine._badOS) {
  138. pointA = pointB = null;
  139. }
  140. else {
  141. //only remove the impacted pointer in case of multitouch allowing on most
  142. //platforms switching from rotate to zoom and pan seamlessly.
  143. if (pointB && pointA && pointA.pointerId == evt.pointerId) {
  144. pointA = pointB;
  145. pointB = null;
  146. cacheSoloPointer = { x: pointA.x, y: pointA.y, pointerId: pointA.pointerId, type: evt.pointerType };
  147. }
  148. else if (pointA && pointB && pointB.pointerId == evt.pointerId) {
  149. pointB = null;
  150. cacheSoloPointer = { x: pointA.x, y: pointA.y, pointerId: pointA.pointerId, type: evt.pointerType };
  151. }
  152. else {
  153. pointA = pointB = null;
  154. }
  155. }
  156. if (!noPreventDefault) {
  157. evt.preventDefault();
  158. }
  159. } else if (p.type === PointerEventTypes.POINTERMOVE) {
  160. if (!noPreventDefault) {
  161. evt.preventDefault();
  162. }
  163. // One button down
  164. if (pointA && pointB === null && cacheSoloPointer) {
  165. if (this.panningSensibility !== 0 &&
  166. ((evt.ctrlKey && this.camera._useCtrlForPanning) || this._isPanClick)) {
  167. this.camera.inertialPanningX += -(evt.clientX - cacheSoloPointer.x) / this.panningSensibility;
  168. this.camera.inertialPanningY += (evt.clientY - cacheSoloPointer.y) / this.panningSensibility;
  169. } else {
  170. var offsetX = evt.clientX - cacheSoloPointer.x;
  171. var offsetY = evt.clientY - cacheSoloPointer.y;
  172. this.camera.inertialAlphaOffset -= offsetX / this.angularSensibilityX;
  173. this.camera.inertialBetaOffset -= offsetY / this.angularSensibilityY;
  174. }
  175. cacheSoloPointer.x = evt.clientX;
  176. cacheSoloPointer.y = evt.clientY;
  177. }
  178. // Two buttons down: pinch/pan
  179. else if (pointA && pointB) {
  180. //if (noPreventDefault) { evt.preventDefault(); } //if pinch gesture, could be useful to force preventDefault to avoid html page scroll/zoom in some mobile browsers
  181. var ed = (pointA.pointerId === evt.pointerId) ? pointA : pointB;
  182. ed.x = evt.clientX;
  183. ed.y = evt.clientY;
  184. var direction = this.pinchInwards ? 1 : -1;
  185. var distX = pointA.x - pointB.x;
  186. var distY = pointA.y - pointB.y;
  187. var pinchSquaredDistance = (distX * distX) + (distY * distY);
  188. var pinchDistance = Math.sqrt(pinchSquaredDistance);
  189. if (previousPinchSquaredDistance === 0) {
  190. initialDistance = pinchDistance;
  191. previousPinchSquaredDistance = pinchSquaredDistance;
  192. previousMultiTouchPanPosition.x = (pointA.x + pointB.x) / 2;
  193. previousMultiTouchPanPosition.y = (pointA.y + pointB.y) / 2;
  194. return;
  195. }
  196. if (this.multiTouchPanAndZoom) {
  197. if (this.pinchDeltaPercentage) {
  198. this.camera.inertialRadiusOffset += ((pinchSquaredDistance - previousPinchSquaredDistance) * 0.001) * this.camera.radius * this.pinchDeltaPercentage;
  199. } else {
  200. this.camera.inertialRadiusOffset += (pinchSquaredDistance - previousPinchSquaredDistance) /
  201. (this.pinchPrecision *
  202. ((this.angularSensibilityX + this.angularSensibilityY) / 2) *
  203. direction);
  204. }
  205. if (this.panningSensibility !== 0) {
  206. var pointersCenterX = (pointA.x + pointB.x) / 2;
  207. var pointersCenterY = (pointA.y + pointB.y) / 2;
  208. var pointersCenterDistX = pointersCenterX - previousMultiTouchPanPosition.x;
  209. var pointersCenterDistY = pointersCenterY - previousMultiTouchPanPosition.y;
  210. previousMultiTouchPanPosition.x = pointersCenterX;
  211. previousMultiTouchPanPosition.y = pointersCenterY;
  212. this.camera.inertialPanningX += -(pointersCenterDistX) / (this.panningSensibility);
  213. this.camera.inertialPanningY += (pointersCenterDistY) / (this.panningSensibility);
  214. }
  215. }
  216. else {
  217. twoFingerActivityCount++;
  218. if (previousMultiTouchPanPosition.isPinching || (twoFingerActivityCount < 20 && Math.abs(pinchDistance - initialDistance) > this.camera.pinchToPanMaxDistance)) {
  219. if (this.pinchDeltaPercentage) {
  220. this.camera.inertialRadiusOffset += ((pinchSquaredDistance - previousPinchSquaredDistance) * 0.001) * this.camera.radius * this.pinchDeltaPercentage;
  221. } else {
  222. this.camera.inertialRadiusOffset += (pinchSquaredDistance - previousPinchSquaredDistance) /
  223. (this.pinchPrecision *
  224. ((this.angularSensibilityX + this.angularSensibilityY) / 2) *
  225. direction);
  226. }
  227. previousMultiTouchPanPosition.isPaning = false;
  228. previousMultiTouchPanPosition.isPinching = true;
  229. }
  230. else {
  231. if (cacheSoloPointer && cacheSoloPointer.pointerId === ed.pointerId && this.panningSensibility !== 0 && this.multiTouchPanning) {
  232. if (!previousMultiTouchPanPosition.isPaning) {
  233. previousMultiTouchPanPosition.isPaning = true;
  234. previousMultiTouchPanPosition.isPinching = false;
  235. previousMultiTouchPanPosition.x = ed.x;
  236. previousMultiTouchPanPosition.y = ed.y;
  237. return;
  238. }
  239. this.camera.inertialPanningX += -(ed.x - previousMultiTouchPanPosition.x) / (this.panningSensibility);
  240. this.camera.inertialPanningY += (ed.y - previousMultiTouchPanPosition.y) / (this.panningSensibility);
  241. }
  242. }
  243. if (cacheSoloPointer && cacheSoloPointer.pointerId === evt.pointerId) {
  244. previousMultiTouchPanPosition.x = ed.x;
  245. previousMultiTouchPanPosition.y = ed.y;
  246. }
  247. }
  248. previousPinchSquaredDistance = pinchSquaredDistance;
  249. }
  250. }
  251. };
  252. this._observer = this.camera.getScene().onPointerObservable.add(this._pointerInput, PointerEventTypes.POINTERDOWN | PointerEventTypes.POINTERUP | PointerEventTypes.POINTERMOVE | PointerEventTypes.POINTERDOUBLETAP);
  253. this._onContextMenu = (evt) => {
  254. evt.preventDefault();
  255. };
  256. if (!this.camera._useCtrlForPanning) {
  257. element.addEventListener("contextmenu", this._onContextMenu, false);
  258. }
  259. this._onLostFocus = () => {
  260. //this._keys = [];
  261. pointA = pointB = null;
  262. previousPinchSquaredDistance = 0;
  263. previousMultiTouchPanPosition.isPaning = false;
  264. previousMultiTouchPanPosition.isPinching = false;
  265. twoFingerActivityCount = 0;
  266. cacheSoloPointer = null;
  267. initialDistance = 0;
  268. };
  269. this._onMouseMove = (evt) => {
  270. if (!engine.isPointerLock) {
  271. return;
  272. }
  273. var offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
  274. var offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
  275. this.camera.inertialAlphaOffset -= offsetX / this.angularSensibilityX;
  276. this.camera.inertialBetaOffset -= offsetY / this.angularSensibilityY;
  277. if (!noPreventDefault) {
  278. evt.preventDefault();
  279. }
  280. };
  281. this._onGestureStart = (e) => {
  282. if (window.MSGesture === undefined) {
  283. return;
  284. }
  285. if (!this._MSGestureHandler) {
  286. this._MSGestureHandler = new MSGesture();
  287. this._MSGestureHandler.target = element;
  288. }
  289. this._MSGestureHandler.addPointer(e.pointerId);
  290. };
  291. this._onGesture = (e) => {
  292. this.camera.radius *= e.scale;
  293. if (e.preventDefault) {
  294. if (!noPreventDefault) {
  295. e.stopPropagation();
  296. e.preventDefault();
  297. }
  298. }
  299. };
  300. element.addEventListener("mousemove", this._onMouseMove, false);
  301. element.addEventListener("MSPointerDown", <EventListener>this._onGestureStart, false);
  302. element.addEventListener("MSGestureChange", <EventListener>this._onGesture, false);
  303. Tools.RegisterTopRootEvents([
  304. { name: "blur", handler: this._onLostFocus }
  305. ]);
  306. }
  307. /**
  308. * Detach the current controls from the specified dom element.
  309. * @param element Defines the element to stop listening the inputs from
  310. */
  311. public detachControl(element: Nullable<HTMLElement>): void {
  312. if (this._onLostFocus) {
  313. Tools.UnregisterTopRootEvents([
  314. { name: "blur", handler: this._onLostFocus }
  315. ]);
  316. }
  317. if (element && this._observer) {
  318. this.camera.getScene().onPointerObservable.remove(this._observer);
  319. this._observer = null;
  320. if (this._onContextMenu) {
  321. element.removeEventListener("contextmenu", this._onContextMenu);
  322. }
  323. if (this._onMouseMove) {
  324. element.removeEventListener("mousemove", this._onMouseMove);
  325. }
  326. if (this._onGestureStart) {
  327. element.removeEventListener("MSPointerDown", <EventListener>this._onGestureStart);
  328. }
  329. if (this._onGesture) {
  330. element.removeEventListener("MSGestureChange", <EventListener>this._onGesture);
  331. }
  332. this._isPanClick = false;
  333. this.pinchInwards = true;
  334. this._onMouseMove = null;
  335. this._onGestureStart = null;
  336. this._onGesture = null;
  337. this._MSGestureHandler = null;
  338. this._onLostFocus = null;
  339. this._onContextMenu = null;
  340. }
  341. }
  342. /**
  343. * Gets the class name of the current intput.
  344. * @returns the class name
  345. */
  346. public getClassName(): string {
  347. return "ArcRotateCameraPointersInput";
  348. }
  349. /**
  350. * Get the friendly name associated with the input class.
  351. * @returns the input friendly name
  352. */
  353. public getSimpleName(): string {
  354. return "pointers";
  355. }
  356. }
  357. (<any>CameraInputTypes)["ArcRotateCameraPointersInput"] = ArcRotateCameraPointersInput;