Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[macos] Round non-freeform zoom gestures/commands to nearest integer
Browse files Browse the repository at this point in the history
Affects:
 - Double-tap gestures
 - Two-finger tap gestures
 - +/- button pushes
 - Shortcut keys
 - Menu items and shortcut keys (in macapp)
  • Loading branch information
friedbunny committed Feb 11, 2017
1 parent 74b1901 commit 4b227ed
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions platform/macos/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
* Fixed flickering that occurred when panning past the antimeridian. ([#7574](https://github.com/mapbox/mapbox-gl-native/pull/7574))
* Added a method to MGLMapViewDelegate, `-mapView:shouldChangeFromCamera:toCamera:`, that you can implement to restrict which parts the user can navigate to using gestures. ([#5584](https://github.com/mapbox/mapbox-gl-native/pull/5584))
* Added a `MGLDistanceFormatter` class for formatting geographic distances. ([#7888](https://github.com/mapbox/mapbox-gl-native/pull/7888))
* Zooming by double-tap, two-finger tap, zoom buttons, shortcut keys, or demo app menu items or shortcut keys now zooms to the nearest integer zoom level. ([#8027](https://github.com/mapbox/mapbox-gl-native/pull/8027))

## 0.3.1

Expand Down
4 changes: 2 additions & 2 deletions platform/macos/app/MapDocument.m
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,11 @@ - (IBAction)chooseCustomStyle:(id)sender {
}

- (IBAction)zoomIn:(id)sender {
[self.mapView setZoomLevel:self.mapView.zoomLevel + 1 animated:YES];
[self.mapView setZoomLevel:round(self.mapView.zoomLevel) + 1 animated:YES];
}

- (IBAction)zoomOut:(id)sender {
[self.mapView setZoomLevel:self.mapView.zoomLevel - 1 animated:YES];
[self.mapView setZoomLevel:round(self.mapView.zoomLevel) - 1 animated:YES];
}

- (IBAction)snapToNorth:(id)sender {
Expand Down
21 changes: 18 additions & 3 deletions platform/macos/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,22 @@ - (void)setZoomLevel:(double)zoomLevel animated:(BOOL)animated {
}

- (void)zoomBy:(double)zoomDelta animated:(BOOL)animated {
[self setZoomLevel:self.zoomLevel + zoomDelta animated:animated];
[self setZoomLevel:round(self.zoomLevel) + zoomDelta animated:animated];
}

- (void)zoomBy:(double)zoomDelta atPoint:(NSPoint)point animated:(BOOL)animated {
[self willChangeValueForKey:@"centerCoordinate"];
[self willChangeValueForKey:@"zoomLevel"];
double newZoom = round(self.zoomLevel) + zoomDelta;
MGLMapCamera *oldCamera = self.camera;
mbgl::ScreenCoordinate center(point.x, self.bounds.size.height - point.y);
_mbglMap->setZoom(newZoom, center, MGLDurationInSecondsFromTimeInterval(animated ? MGLAnimationDuration : 0));
if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:self.camera]) {
self.camera = oldCamera;
}
[self didChangeValueForKey:@"zoomLevel"];
[self didChangeValueForKey:@"centerCoordinate"];
}

- (void)scaleBy:(double)scaleFactor atPoint:(NSPoint)point animated:(BOOL)animated {
Expand Down Expand Up @@ -1500,7 +1515,7 @@ - (void)handleDoubleClickGesture:(NSClickGestureRecognizer *)gestureRecognizer {
_mbglMap->cancelTransitions();

NSPoint gesturePoint = [gestureRecognizer locationInView:self];
[self scaleBy:2 atPoint:gesturePoint animated:YES];
[self zoomBy:1 atPoint:gesturePoint animated:YES];
}

- (void)smartMagnifyWithEvent:(NSEvent *)event {
Expand All @@ -1512,7 +1527,7 @@ - (void)smartMagnifyWithEvent:(NSEvent *)event {

// Tap with two fingers (“right-click”) to zoom out on mice but not trackpads.
NSPoint gesturePoint = [self convertPoint:event.locationInWindow fromView:nil];
[self scaleBy:0.5 atPoint:gesturePoint animated:YES];
[self zoomBy:-1 atPoint:gesturePoint animated:YES];
}

/// Rotate fingers to rotate.
Expand Down

0 comments on commit 4b227ed

Please sign in to comment.