diff options
author | Minh Nguyễn <mxn@1ec5.org> | 2015-12-01 15:34:15 -0800 |
---|---|---|
committer | Minh Nguyễn <mxn@1ec5.org> | 2015-12-13 17:26:53 -0800 |
commit | 004d04075ad01253215ea7fcd7cd49bb61aabe7d (patch) | |
tree | 6e57b456d84e9c6cef63f757e901c00755bcfe91 /platform/osx/sdk | |
parent | f542d7b2a73ca63e2ab0099e998bd62bd57da9fc (diff) | |
download | qtlocation-mapboxgl-004d04075ad01253215ea7fcd7cd49bb61aabe7d.tar.gz |
[osx] Annotation cursors
Also added an option to osxapp for assigning a random cursor to all the annotations each time an annotation is added or removed.
Diffstat (limited to 'platform/osx/sdk')
-rw-r--r-- | platform/osx/sdk/MGLMapView.mm | 89 |
1 files changed, 66 insertions, 23 deletions
diff --git a/platform/osx/sdk/MGLMapView.mm b/platform/osx/sdk/MGLMapView.mm index 81d5edfdd0..5cee56cd7d 100644 --- a/platform/osx/sdk/MGLMapView.mm +++ b/platform/osx/sdk/MGLMapView.mm @@ -111,6 +111,7 @@ public: std::shared_ptr<mbgl::SQLiteCache> _mbglFileCache; mbgl::DefaultFileSource *_mbglFileSource; + NSPanGestureRecognizer *_panGestureRecognizer; NSMagnificationGestureRecognizer *_magnificationGestureRecognizer; NSRotationGestureRecognizer *_rotationGestureRecognizer; double _scaleAtBeginningOfGesture; @@ -122,6 +123,8 @@ public: MGLAnnotationID _lastSelectedAnnotationID; NSSize _unionedAnnotationImageSize; std::vector<MGLAnnotationID> _annotationsNearbyLastClick; + BOOL _wantsToolTipRects; + BOOL _wantsCursorRects; BOOL _delegateHasAlphasForShapeAnnotations; BOOL _delegateHasStrokeColorsForShapeAnnotations; BOOL _delegateHasFillColorsForShapeAnnotations; @@ -294,9 +297,9 @@ public: _rotateEnabled = YES; _pitchEnabled = YES; - NSPanGestureRecognizer *panGestureRecognizer = [[NSPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)]; - panGestureRecognizer.delaysKeyEvents = YES; - [self addGestureRecognizer:panGestureRecognizer]; + _panGestureRecognizer = [[NSPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)]; + _panGestureRecognizer.delaysKeyEvents = YES; + [self addGestureRecognizer:_panGestureRecognizer]; NSClickGestureRecognizer *clickGestureRecognizer = [[NSClickGestureRecognizer alloc] initWithTarget:self action:@selector(handleClickGesture:)]; clickGestureRecognizer.delaysPrimaryMouseButtonEvents = NO; @@ -591,7 +594,7 @@ public: [self updateZoomControls]; [self updateCompass]; [self updateAnnotationCallouts]; - [self updateToolTips]; + [self updateAnnotationTrackingAreas]; if ([self.delegate respondsToSelector:@selector(mapView:regionDidChangeAnimated:)]) { BOOL animated = change == mbgl::MapChangeRegionDidChangeAnimated; @@ -830,7 +833,7 @@ public: _mbglMap->cancelTransitions(); if (gestureRecognizer.state == NSGestureRecognizerStateBegan) { - [[NSCursor closedHandCursor] push]; + [self.window invalidateCursorRectsForView:self]; _mbglMap->setGestureInProgress(true); } else if (gestureRecognizer.state == NSGestureRecognizerStateChanged) { delta.y *= -1; @@ -839,7 +842,7 @@ public: } else if (gestureRecognizer.state == NSGestureRecognizerStateEnded || gestureRecognizer.state == NSGestureRecognizerStateCancelled) { _mbglMap->setGestureInProgress(false); - [[NSCursor arrowCursor] pop]; + [self.window invalidateCursorRectsForView:self]; } } } @@ -1128,6 +1131,10 @@ public: NSString *symbolName = [MGLAnnotationSpritePrefix stringByAppendingString:annotationImage.reuseIdentifier]; points.emplace_back(MGLLatLngFromLocationCoordinate2D(annotation.coordinate), symbolName ? [symbolName UTF8String] : ""); + + if (annotation.toolTip.length) { + _wantsToolTipRects = YES; + } } } @@ -1152,7 +1159,7 @@ public: } } - [self updateToolTips]; + [self updateAnnotationTrackingAreas]; } - (void)installAnnotationImage:(MGLAnnotationImage *)annotationImage { @@ -1180,6 +1187,10 @@ public: // Union this slop area with any existing slop areas. _unionedAnnotationImageSize = NSMakeSize(MAX(_unionedAnnotationImageSize.width, size.width), MAX(_unionedAnnotationImageSize.height, size.height)); + + if (annotationImage.cursor) { + _wantsCursorRects = YES; + } } - (void)removeAnnotation:(id <MGLAnnotation>)annotation { @@ -1216,7 +1227,7 @@ public: _mbglMap->removeAnnotations(annotationIDsToRemove); - [self updateToolTips]; + [self updateAnnotationTrackingAreas]; } - (id <MGLAnnotation>)selectedAnnotation { @@ -1518,25 +1529,33 @@ public: [self removeAnnotations:overlays]; } -#pragma mark Tooltips +#pragma mark Tooltips and cursors -- (void)updateToolTips { - [self removeAllToolTips]; - - std::vector<MGLAnnotationID> annotationIDs = [self annotationIDsInRect:self.bounds]; - for (MGLAnnotationID annotationID : annotationIDs) { - MGLAnnotationImage *annotationImage = [self imageOfAnnotationWithID:annotationID]; - id <MGLAnnotation> annotation = [self annotationWithID:annotationID]; - if (annotation.toolTip.length) { - NSImage *image = annotationImage.image; - NSRect annotationRect = [self frameOfImage:image - centeredAtCoordinate:annotation.coordinate]; - annotationRect = NSOffsetRect(image.alignmentRect, annotationRect.origin.x, annotationRect.origin.y); - if (!NSIsEmptyRect(annotationRect)) { - [self addToolTipRect:annotationRect owner:self userData:(void *)(NSUInteger)annotationID]; +- (void)updateAnnotationTrackingAreas { + if (_wantsToolTipRects) { + [self removeAllToolTips]; + std::vector<MGLAnnotationID> annotationIDs = [self annotationIDsInRect:self.bounds]; + for (MGLAnnotationID annotationID : annotationIDs) { + MGLAnnotationImage *annotationImage = [self imageOfAnnotationWithID:annotationID]; + id <MGLAnnotation> annotation = [self annotationWithID:annotationID]; + if (annotation.toolTip.length) { + NSImage *image = annotationImage.image; + NSRect annotationRect = [self frameOfImage:image + centeredAtCoordinate:annotation.coordinate]; + annotationRect = NSOffsetRect(image.alignmentRect, annotationRect.origin.x, annotationRect.origin.y); + if (!NSIsEmptyRect(annotationRect)) { + [self addToolTipRect:annotationRect owner:self userData:(void *)(NSUInteger)annotationID]; + } + } + if (annotationImage.cursor) { + _wantsCursorRects = YES; } } } + + if (_wantsCursorRects) { + [self.window invalidateCursorRectsForView:self]; + } } - (NSString *)view:(__unused NSView *)view stringForToolTip:(__unused NSToolTipTag)tag point:(__unused NSPoint)point userData:(void *)data { @@ -1548,6 +1567,30 @@ public: return annotation.toolTip; } +- (void)resetCursorRects { + if (_panGestureRecognizer.state == NSGestureRecognizerStateBegan + || _panGestureRecognizer.state == NSGestureRecognizerStateChanged) { + [self addCursorRect:self.bounds cursor:[NSCursor closedHandCursor]]; + return; + } + if (!_wantsCursorRects) { + return; + } + + std::vector<MGLAnnotationID> annotationIDs = [self annotationIDsInRect:self.bounds]; + for (MGLAnnotationID annotationID : annotationIDs) { + id <MGLAnnotation> annotation = [self annotationWithID:annotationID]; + MGLAnnotationImage *annotationImage = [self imageOfAnnotationWithID:annotationID]; + if (annotationImage.cursor) { + NSImage *image = annotationImage.image; + NSRect annotationRect = [self frameOfImage:image + centeredAtCoordinate:annotation.coordinate]; + annotationRect = NSOffsetRect(image.alignmentRect, annotationRect.origin.x, annotationRect.origin.y); + [self addCursorRect:annotationRect cursor:annotationImage.cursor]; + } + } +} + #pragma mark Interface Builder methods - (void)prepareForInterfaceBuilder { |