Skip to content

Commit

Permalink
[ios] Fix camera paddings reset on map view gestures performing. (#188)
Browse files Browse the repository at this point in the history
* Fix camera paddings reset on map view gestures performing.

* Asymmetrical paddings fix demo app.

* Remove the demo for the PR branch.
  • Loading branch information
OlexandrStepanov authored Dec 2, 2021
1 parent 004a849 commit ba1ec44
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 64 deletions.
4 changes: 4 additions & 0 deletions platform/ios/platform/darwin/src/MGLGeometry_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ NS_INLINE mbgl::EdgeInsets MGLEdgeInsetsFromNSEdgeInsets(MGLEdgeInsets insets) {
return { insets.top, insets.left, insets.bottom, insets.right };
}

NS_INLINE MGLEdgeInsets NSEdgeInsetsFromMGLEdgeInsets(const mbgl::EdgeInsets& insets) {
return MGLEdgeInsetsMake(insets.top(), insets.left(), insets.bottom(), insets.right());
}

/// Returns the combination of two edge insets.
NS_INLINE MGLEdgeInsets MGLEdgeInsetsInsetEdgeInset(MGLEdgeInsets base, MGLEdgeInsets inset) {
return MGLEdgeInsetsMake(base.top + inset.top,
Expand Down
13 changes: 13 additions & 0 deletions platform/ios/platform/ios/src/MGLMapView.h
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,19 @@ MGL_EXPORT
*/
@property (nonatomic, assign) UIEdgeInsets contentInset;

/**
The current edge insets of the current map view’s camera.
Camera edge insets are formed as accumulation of map view's content insets
and the edge padding passed to the method like `seCamera:...edgePadding:`,
`setVisibleCoordinates:...edgePadding:`, `showAnnotations:...edgePadding:` etc.
The camera edge insets influences the `centerCoordinate` of the viewport.
This value is read-only, in order to apply paddings, use either persistent
`contentInset`, either transient `edgePadding` parameter of the `set...` methods.
*/
@property (nonatomic, readonly) UIEdgeInsets cameraEdgeInsets;

/**
Deprecated. Sets the distance from the edges of the map view’s frame to the edges
of the map view’s logical viewport with an optional transition animation.
Expand Down
58 changes: 24 additions & 34 deletions platform/ios/platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2084,8 +2084,7 @@ - (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinch
{
self.mbglMap.jumpTo(mbgl::CameraOptions()
.withZoom(newZoom)
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y })
.withPadding(MGLEdgeInsetsFromNSEdgeInsets(self.contentInset)));
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y }));

// The gesture recognizer only reports the gesture’s current center
// point, so use the previous center point to anchor the transition.
Expand Down Expand Up @@ -2145,8 +2144,8 @@ - (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinch
{
self.mbglMap.easeTo(mbgl::CameraOptions()
.withZoom(zoom)
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y })
.withPadding(MGLEdgeInsetsFromNSEdgeInsets(self.contentInset)), MGLDurationFromTimeInterval(duration));
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y }),
MGLDurationFromTimeInterval(duration));
}
}

Expand Down Expand Up @@ -2217,8 +2216,7 @@ - (void)handleRotateGesture:(UIRotationGestureRecognizer *)rotate
{
self.mbglMap.jumpTo(mbgl::CameraOptions()
.withBearing(newDegrees)
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y})
.withPadding(MGLEdgeInsetsFromNSEdgeInsets(self.contentInset)));
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y}));
}

[self cameraIsChanging];
Expand Down Expand Up @@ -2259,8 +2257,7 @@ - (void)handleRotateGesture:(UIRotationGestureRecognizer *)rotate
{
self.mbglMap.easeTo(mbgl::CameraOptions()
.withBearing(newDegrees)
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y })
.withPadding(MGLEdgeInsetsFromNSEdgeInsets(self.contentInset)),
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y }),
MGLDurationFromTimeInterval(decelerationRate));

[self notifyGestureDidEndWithDrift:YES];
Expand Down Expand Up @@ -2390,8 +2387,8 @@ - (void)handleDoubleTapGesture:(UITapGestureRecognizer *)doubleTap
mbgl::ScreenCoordinate center(gesturePoint.x, gesturePoint.y);
self.mbglMap.easeTo(mbgl::CameraOptions()
.withZoom(newZoom)
.withAnchor(center)
.withPadding(MGLEdgeInsetsFromNSEdgeInsets(self.contentInset)), MGLDurationFromTimeInterval(MGLAnimationDuration));
.withAnchor(center),
MGLDurationFromTimeInterval(MGLAnimationDuration));

__weak MGLMapView *weakSelf = self;

Expand Down Expand Up @@ -2431,8 +2428,8 @@ - (void)handleTwoFingerTapGesture:(UITapGestureRecognizer *)twoFingerTap
mbgl::ScreenCoordinate center(gesturePoint.x, gesturePoint.y);
self.mbglMap.easeTo(mbgl::CameraOptions()
.withZoom(newZoom)
.withAnchor(center)
.withPadding(MGLEdgeInsetsFromNSEdgeInsets(self.contentInset)), MGLDurationFromTimeInterval(MGLAnimationDuration));
.withAnchor(center),
MGLDurationFromTimeInterval(MGLAnimationDuration));

__weak MGLMapView *weakSelf = self;

Expand Down Expand Up @@ -2476,8 +2473,7 @@ - (void)handleQuickZoomGesture:(UILongPressGestureRecognizer *)quickZoom
{
self.mbglMap.jumpTo(mbgl::CameraOptions()
.withZoom(newZoom)
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y })
.withPadding(MGLEdgeInsetsFromNSEdgeInsets(self.contentInset)));
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y }));
}

[self cameraIsChanging];
Expand Down Expand Up @@ -2544,8 +2540,7 @@ - (void)handleTwoFingerDragGesture:(UIPanGestureRecognizer *)twoFingerDrag
{
self.mbglMap.jumpTo(mbgl::CameraOptions()
.withPitch(pitchNew)
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y })
.withPadding(MGLEdgeInsetsFromNSEdgeInsets(self.contentInset)));
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y }));
}

[self cameraIsChanging];
Expand Down Expand Up @@ -2591,8 +2586,7 @@ - (MGLMapCamera *)cameraByZoomingToZoomLevel:(double)zoom aroundAnchorPoint:(CGP

- (MGLMapCamera *)cameraByRotatingToDirection:(CLLocationDirection)degrees aroundAnchorPoint:(CGPoint)anchorPoint
{
mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(self.contentInset);
mbgl::CameraOptions currentCameraOptions = self.mbglMap.getCameraOptions(padding);
mbgl::CameraOptions currentCameraOptions = self.mbglMap.getCameraOptions();

MGLMapCamera *camera;

Expand All @@ -2606,8 +2600,7 @@ - (MGLMapCamera *)cameraByRotatingToDirection:(CLLocationDirection)degrees aroun

- (MGLMapCamera *)cameraByTiltingToPitch:(CGFloat)pitch
{
mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(self.contentInset);
mbgl::CameraOptions currentCameraOptions = self.mbglMap.getCameraOptions(padding);
mbgl::CameraOptions currentCameraOptions = self.mbglMap.getCameraOptions();

MGLMapCamera *camera;

Expand Down Expand Up @@ -3548,8 +3541,7 @@ - (void)accessibilityScaleBy:(double)scaleFactor
double newZoom = round(self.zoomLevel) + log2(scaleFactor);
self.mbglMap.jumpTo(mbgl::CameraOptions()
.withZoom(newZoom)
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y })
.withPadding(MGLEdgeInsetsFromNSEdgeInsets(self.contentInset)));
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y }));
[self unrotateIfNeededForGesture];

_accessibilityValueAnnouncementIsPending = YES;
Expand All @@ -3576,8 +3568,7 @@ - (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate

- (CLLocationCoordinate2D)centerCoordinate
{
mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(self.contentInset);
return MGLLocationCoordinate2DFromLatLng(*self.mbglMap.getCameraOptions(padding).center);
return MGLLocationCoordinate2DFromLatLng(*self.mbglMap.getCameraOptions().center);
}

- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(double)zoomLevel animated:(BOOL)animated
Expand Down Expand Up @@ -3687,8 +3678,7 @@ - (void)_setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate edgePaddin

- (double)zoomLevel
{
mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(self.contentInset);
return *self.mbglMap.getCameraOptions(padding).zoom;
return *self.mbglMap.getCameraOptions().zoom;
}

- (void)setZoomLevel:(double)zoomLevel
Expand All @@ -3708,9 +3698,7 @@ - (void)setZoomLevel:(double)zoomLevel animated:(BOOL)animated
CGFloat duration = animated ? MGLAnimationDuration : 0;

self.mbglMap.easeTo(mbgl::CameraOptions()
.withZoom(zoomLevel)
.withPadding(MGLEdgeInsetsFromNSEdgeInsets(self.contentInset)),
MGLDurationFromTimeInterval(duration));
.withZoom(zoomLevel), MGLDurationFromTimeInterval(duration));
}

- (void)setMinimumZoomLevel:(double)minimumZoomLevel
Expand Down Expand Up @@ -3938,9 +3926,7 @@ - (void)_setDirection:(CLLocationDirection)direction animated:(BOOL)animated

if (self.userTrackingMode == MGLUserTrackingModeNone)
{
self.mbglMap.easeTo(mbgl::CameraOptions()
.withBearing(direction)
.withPadding(MGLEdgeInsetsFromNSEdgeInsets(self.contentInset)),
self.mbglMap.easeTo(mbgl::CameraOptions().withBearing(direction),
MGLDurationFromTimeInterval(duration));
}
else
Expand Down Expand Up @@ -3969,6 +3955,11 @@ - (void)setDirection:(CLLocationDirection)direction
return [NSSet setWithObjects:@"longitude", @"latitude", @"centerCoordinate", @"zoomLevel", @"direction", nil];
}

- (UIEdgeInsets)cameraEdgeInsets {
mbgl::CameraOptions cameraOptions = self.mbglMap.getCameraOptions();
return NSEdgeInsetsFromMGLEdgeInsets(cameraOptions.padding.value_or(mbgl::EdgeInsets()));
}

- (MGLMapCamera *)camera
{
if (!_mbglMap)
Expand All @@ -3977,8 +3968,7 @@ - (MGLMapCamera *)camera
return self.residualCamera;
}

mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(self.contentInset);
return [self cameraForCameraOptions:self.mbglMap.getCameraOptions(padding)];
return [self cameraForCameraOptions:self.mbglMap.getCameraOptions()];
}

- (void)setCamera:(MGLMapCamera *)camera
Expand Down
3 changes: 0 additions & 3 deletions platform/ios/platform/ios/test/MGLMapViewContentInsetTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ - (void)testContentInsetCenter {
(self.screenBounds.size.height/2) + ((contentInset.top - contentInset.bottom) / 2));
XCTAssertEqualWithAccuracy(shiftedPoint.x, expectedShiftedPoint.x, 0.01);
XCTAssertEqualWithAccuracy(shiftedPoint.y, expectedShiftedPoint.y, 0.01);


}

- (void)testContentInsetOrnaments {
Expand Down Expand Up @@ -171,7 +169,6 @@ - (void)testContentInsetOrnaments {
y = self.screenBounds.size.height - attributionView.bounds.size.height - margin;
expectedAttributionOrigin = CGPointMake(x, y);
XCTAssertTrue(CGPointEqualToPoint(attributionView.frame.origin, expectedAttributionOrigin));

}

@end
Loading

0 comments on commit ba1ec44

Please sign in to comment.