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

Commit

Permalink
[ios] Removed duplicated methods, improved code readability
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-guerra committed Feb 9, 2017
1 parent a54a2a1 commit cb1e02b
Showing 1 changed file with 31 additions and 55 deletions.
86 changes: 31 additions & 55 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,7 @@ - (void)handlePanGesture:(UIPanGestureRecognizer *)pan
{
CGPoint delta = [pan translationInView:pan.view];

toCamera = [self currentCameraWithEstimatedEndPoint:delta panGesture:pan];
toCamera = [self cameraByPanningWithTranslation:delta panGesture:pan];

if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
Expand All @@ -1246,7 +1246,7 @@ - (void)handlePanGesture:(UIPanGestureRecognizer *)pan
if (drift)
{
CGPoint offset = CGPointMake(velocity.x * self.decelerationRate / 4, velocity.y * self.decelerationRate / 4);
toCamera = [self currentCameraWithEstimatedEndPoint:offset panGesture:pan];
toCamera = [self cameraByPanningWithTranslation:offset panGesture:pan];

if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
Expand Down Expand Up @@ -1283,8 +1283,6 @@ - (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinch

CGPoint centerPoint = [self anchorPointForGesture:pinch];
MGLMapCamera *oldCamera = self.camera;
MGLMapCamera *toCamera;
double zoom = 0.0;

if (pinch.state == UIGestureRecognizerStateBegan)
{
Expand All @@ -1301,8 +1299,9 @@ - (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinch
if (log2(newScale) < _mbglMap->getMinZoom()) return;

// Calculates the final camera zoom, has no effect within current map camera.
zoom = log2(newScale);
toCamera = [self currentCameraWithEstimatedZoom:zoom];
MGLMapCamera *toCamera;
double zoom = log2(newScale);
toCamera = [self cameraByZoomingToZoomLevel:zoom aroundAnchorPoint:centerPoint];

if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
Expand Down Expand Up @@ -1357,15 +1356,16 @@ - (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinch
BOOL drift = velocity && duration;

// Calculates the final camera zoom, this has no effect within current map camera.
zoom = log2(newScale);
toCamera = [self currentCameraWithEstimatedZoom:zoom];
MGLMapCamera *toCamera;
double zoom = log2(newScale);
toCamera = [self cameraByZoomingToZoomLevel:zoom aroundAnchorPoint:centerPoint];

if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
{
self.camera = oldCamera;
drift = NO;
}else {
} else {
if (drift)
{
_mbglMap->setScale(newScale, mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y }, MGLDurationInSecondsFromTimeInterval(duration));
Expand Down Expand Up @@ -1416,7 +1416,7 @@ - (void)handleRotateGesture:(UIRotationGestureRecognizer *)rotate
newDegrees = fmaxf(newDegrees, -30);
}

toCamera = [self currentCameraWithEstimatedDegrees:newDegrees anchorPoint:centerPoint];
toCamera = [self cameraByRotatingToDirection:newDegrees aroundAnchorPoint:centerPoint];

if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
Expand All @@ -1439,7 +1439,7 @@ - (void)handleRotateGesture:(UIRotationGestureRecognizer *)rotate
CGFloat newRadians = radians + velocity * decelerationRate * 0.1;
CGFloat newDegrees = MGLDegreesFromRadians(newRadians) * -1;

toCamera = [self currentCameraWithEstimatedDegrees:newDegrees anchorPoint:centerPoint];
toCamera = [self cameraByRotatingToDirection:newDegrees aroundAnchorPoint:centerPoint];

if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
Expand Down Expand Up @@ -1575,18 +1575,18 @@ - (void)handleDoubleTapGesture:(UITapGestureRecognizer *)doubleTap
{
MGLMapCamera *oldCamera = self.camera;

double zoom = [self currentCameraZoom];
double zoom = self.zoomLevel;
double newZoom = zoom + 1.0;
CGPoint gesturePoint = [self anchorPointForGesture:doubleTap];

MGLMapCamera *toCamera = [self currentCameraWithEstimatedZoom:newZoom];
MGLMapCamera *toCamera = [self cameraByZoomingToZoomLevel:newZoom aroundAnchorPoint:gesturePoint];

if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
{
self.camera = oldCamera;
} else {
[self trackGestureEvent:MGLEventGestureDoubleTap forRecognizer:doubleTap];
CGPoint gesturePoint = [self anchorPointForGesture:doubleTap];

mbgl::ScreenCoordinate center(gesturePoint.x, gesturePoint.y);
_mbglMap->scaleBy(2, center, MGLDurationInSecondsFromTimeInterval(MGLAnimationDuration));
Expand Down Expand Up @@ -1618,17 +1618,17 @@ - (void)handleTwoFingerTapGesture:(UITapGestureRecognizer *)twoFingerTap
{
MGLMapCamera *oldCamera = self.camera;

double zoom = [self currentCameraZoom];
double zoom = self.zoomLevel;
double newZoom = zoom - 1.0;
CGPoint gesturePoint = [self anchorPointForGesture:twoFingerTap];

MGLMapCamera *toCamera = [self currentCameraWithEstimatedZoom:newZoom];
MGLMapCamera *toCamera = [self cameraByZoomingToZoomLevel:newZoom aroundAnchorPoint:gesturePoint];

if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
{
self.camera = oldCamera;
} else {
CGPoint gesturePoint = [self anchorPointForGesture:twoFingerTap];

mbgl::ScreenCoordinate center(gesturePoint.x, gesturePoint.y);
_mbglMap->scaleBy(0.5, center, MGLDurationInSecondsFromTimeInterval(MGLAnimationDuration));
Expand Down Expand Up @@ -1672,18 +1672,18 @@ - (void)handleQuickZoomGesture:(UILongPressGestureRecognizer *)quickZoom

MGLMapCamera *oldCamera = self.camera;

double zoom = [self currentCameraZoom];
double zoom = self.zoomLevel;
double scale = powf(2, newZoom) / _mbglMap->getScale();

double estimatedZoom = zoom * scale;

MGLMapCamera *toCamera = [self currentCameraWithEstimatedZoom:estimatedZoom];
MGLMapCamera *toCamera = [self cameraByZoomingToZoomLevel:estimatedZoom aroundAnchorPoint:centerPoint];

if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
{
self.camera = oldCamera;
}else {
} else {
_mbglMap->scaleBy(scale, mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y });
}

Expand Down Expand Up @@ -1719,7 +1719,7 @@ - (void)handleTwoFingerDragGesture:(UIPanGestureRecognizer *)twoFingerDrag

CGPoint centerPoint = [self anchorPointForGesture:twoFingerDrag];

toCamera = [self currentCameraWithEstimatedPitch:pitchNew anchorPoint:centerPoint];
toCamera = [self cameraByTiltingToPitch:pitchNew];

if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
Expand All @@ -1739,43 +1739,34 @@ - (void)handleTwoFingerDragGesture:(UIPanGestureRecognizer *)twoFingerDrag

}

- (MGLMapCamera *)currentCameraWithEstimatedEndPoint:(CGPoint)endPoint panGesture:(UIPanGestureRecognizer *)pan
- (MGLMapCamera *)cameraByPanningWithTranslation:(CGPoint)endPoint panGesture:(UIPanGestureRecognizer *)pan
{
MGLMapCamera *panCamera = [self.camera copy];
CGFloat width = CGRectGetWidth(self.bounds);
CGFloat height = CGRectGetHeight(self.bounds);

mbgl::ScreenCoordinate screenCenter = {
width/2.,
height/2.,
};

mbgl::ScreenCoordinate centerOffset = {
endPoint.x,
-endPoint.y,
};

mbgl::ScreenCoordinate centerPoint = screenCenter - centerOffset;
CGPoint endCameraPoint = CGPointMake(centerPoint.x, centerPoint.y);
CGPoint centerPoint = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
CGPoint endCameraPoint = CGPointMake(centerPoint.x - endPoint.x, centerPoint.y - endPoint.y);
CLLocationCoordinate2D panCoordinate = [self convertPoint:endCameraPoint toCoordinateFromView:pan.view];

panCamera.centerCoordinate = panCoordinate;

return panCamera;
}

- (MGLMapCamera *)currentCameraWithEstimatedZoom:(double)zoom
- (MGLMapCamera *)cameraByZoomingToZoomLevel:(double)zoom aroundAnchorPoint:(CGPoint)anchorPoint
{
mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(self.contentInset);
mbgl::CameraOptions currentCameraOptions = _mbglMap->getCameraOptions(padding);
MGLMapCamera *camera;

mbgl::ScreenCoordinate anchor = mbgl::ScreenCoordinate { anchorPoint.x, anchorPoint.y };
currentCameraOptions.zoom = mbgl::util::clamp(zoom, self.minimumZoomLevel, self.maximumZoomLevel);
currentCameraOptions.anchor = anchor;
camera = [self cameraForCameraOptions:currentCameraOptions];

return camera;
}

- (MGLMapCamera *)currentCameraWithEstimatedDegrees:(CGFloat)degrees anchorPoint:(CGPoint)anchorPoint
- (MGLMapCamera *)cameraByRotatingToDirection:(CLLocationDirection)degrees aroundAnchorPoint:(CGPoint)anchorPoint
{
mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(self.contentInset);
mbgl::CameraOptions currentCameraOptions = _mbglMap->getCameraOptions(padding);
Expand All @@ -1790,34 +1781,19 @@ - (MGLMapCamera *)currentCameraWithEstimatedDegrees:(CGFloat)degrees anchorPoint
return camera;
}

- (MGLMapCamera *)currentCameraWithEstimatedPitch:(CGFloat)pitch anchorPoint:(CGPoint)anchorPoint
- (MGLMapCamera *)cameraByTiltingToPitch:(CGFloat)pitch
{
mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(self.contentInset);
mbgl::CameraOptions currentCameraOptions = _mbglMap->getCameraOptions(padding);

MGLMapCamera *camera;

mbgl::ScreenCoordinate anchor = mbgl::ScreenCoordinate { anchorPoint.x, anchorPoint.y };

currentCameraOptions.pitch = pitch * mbgl::util::DEG2RAD;
currentCameraOptions.anchor = anchor;
camera = [self cameraForCameraOptions:currentCameraOptions];

return camera;
}

- (double)currentCameraZoom
{
mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(self.contentInset);
mbgl::CameraOptions currentCameraOptions = _mbglMap->getCameraOptions(padding);
double zoom = 0.0;

if (currentCameraOptions.zoom) {
zoom = *currentCameraOptions.zoom;
}

return zoom;
}

- (CGPoint)anchorPointForGesture:(UIGestureRecognizer *)gesture {
if (self.userTrackingMode != MGLUserTrackingModeNone)
{
Expand Down

0 comments on commit cb1e02b

Please sign in to comment.