Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated RMPath to add line cap/join and shadow options #169

Merged
merged 1 commit into from
Jun 25, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 9 additions & 27 deletions MapView/Map/RMPath.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,7 @@
@class RMMapContents;
@class RMMapView;

@interface RMPath : RMMapLayer <RMMovingMapLayer> {
RMProjectedPoint projectedLocation;

UIColor *lineColor;
UIColor *fillColor;
float lineWidth;
BOOL scaleLineWidth;

CGFloat *_lineDashLengths;
CGFloat *_scaledLineDashLengths;
size_t _lineDashCount;
CGFloat lineDashPhase;
BOOL scaleLineDash;

BOOL enableDragging;
BOOL enableRotation;

CGMutablePathRef path;

RMMapContents *mapContents;

CGRect originalContentsRect;
BOOL redrawPending;
}
@interface RMPath : RMMapLayer <RMMovingMapLayer>

- (id) initWithContents: (RMMapContents*)aContents;
- (id) initForMap: (RMMapView*)map;
Expand All @@ -71,11 +48,16 @@
- (void) addLineToLatLong: (RMLatLong) point;
- (void) closePath;

@property (nonatomic, assign) CGLineCap lineCap;
@property (nonatomic, assign) CGLineJoin lineJoin;
@property (nonatomic, assign) float lineWidth;
@property (nonatomic, assign) BOOL scaleLineWidth;
@property (nonatomic, readwrite, assign) NSArray *lineDashLengths;
@property CGFloat lineDashPhase;
@property BOOL scaleLineDash;
@property (nonatomic, assign) CGFloat shadowBlur;
@property (nonatomic, assign) CGSize shadowOffset;
@property (nonatomic, retain) UIColor *shadowColor;
@property (nonatomic, assign) NSArray *lineDashLengths;
@property (nonatomic, assign) CGFloat lineDashPhase;
@property (nonatomic, assign) BOOL scaleLineDash;
@property (nonatomic, assign) RMProjectedPoint projectedLocation;
@property (assign) BOOL enableDragging;
@property (assign) BOOL enableRotation;
Expand Down
78 changes: 71 additions & 7 deletions MapView/Map/RMPath.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,28 @@
#import "RMProjection.h"
#import "RMNotifications.h"

@interface RMPath ()
@interface RMPath () {
RMProjectedPoint projectedLocation;

CGFloat *_lineDashLengths;
CGFloat *_scaledLineDashLengths;
size_t _lineDashCount;
CGFloat lineDashPhase;

CGMutablePathRef path;

RMMapContents *mapContents;

CGRect originalContentsRect;
BOOL redrawPending;
}

- (void)addPointToXY:(RMProjectedPoint) point withDrawing:(BOOL)isDrawing;
- (void)recalculateGeometry;
@end

@implementation RMPath
@synthesize lineWidth, lineColor, fillColor, scaleLineWidth, lineDashPhase, lineDashLengths, scaleLineDash, projectedLocation, enableDragging, enableRotation;
@synthesize lineCap, lineJoin, lineWidth, lineColor, fillColor, scaleLineWidth, shadowBlur, shadowOffset, shadowColor, lineDashPhase, lineDashLengths, scaleLineDash, projectedLocation, enableDragging, enableRotation;
@dynamic CGPath, projectedBounds;

- (id) initWithContents: (RMMapContents*)aContents {
Expand All @@ -51,6 +66,8 @@ - (id) initWithContents: (RMMapContents*)aContents {

// Defaults
lineWidth = 4.0;
lineCap = kCGLineCapRound;
lineJoin = kCGLineJoinRound;
scaleLineWidth = NO;
enableDragging = YES;
enableRotation = YES;
Expand All @@ -60,6 +77,9 @@ - (id) initWithContents: (RMMapContents*)aContents {
_lineDashLengths = NULL;
_scaledLineDashLengths = NULL;
lineDashPhase = 0.0;
shadowBlur = 0.0;
shadowOffset = CGSizeMake(0, 0);
self.shadowColor = [UIColor clearColor];

self.masksToBounds = YES;

Expand Down Expand Up @@ -88,8 +108,10 @@ - (id) initForMap: (RMMapView*)map withCoordinates:(const CLLocationCoordinate2D
-(void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
CGPathRelease(path);
self.lineColor = nil;
self.fillColor = nil;
[lineColor release];
[fillColor release];
[shadowColor release];
if ( _lineDashLengths ) free(_lineDashLengths);
[super dealloc];
}

Expand Down Expand Up @@ -169,6 +191,20 @@ - (void) setLineDashLengths:(NSArray *)lengths {
}
}

-(void)setLineCap:(CGLineCap)theLineCap {
if ( theLineCap != lineCap ) {
lineCap = theLineCap;
[self setNeedsDisplay];
}
}

-(void)setLineJoin:(CGLineJoin)theLineJoin {
if ( theLineJoin != lineJoin ) {
lineJoin = theLineJoin;
[self setNeedsDisplay];
}
}

- (void)setLineColor:(UIColor *)aLineColor {
if (lineColor != aLineColor) {
[lineColor release];
Expand All @@ -185,6 +221,29 @@ - (void)setFillColor:(UIColor *)aFillColor {
}
}

-(void)setShadowBlur:(CGFloat)theShadowBlur {
if ( shadowBlur != theShadowBlur ) {
shadowBlur = theShadowBlur;
[self setNeedsDisplay];
}
}

-(void)setShadowOffset:(CGSize)theShadowOffset {
if ( !CGSizeEqualToSize(shadowOffset, theShadowOffset) ) {
shadowOffset = theShadowOffset;
[self setNeedsDisplay];
}
}

-(void)setShadowColor:(UIColor *)theShadowColor {
if ( ![shadowColor isEqual:theShadowColor] ) {
[theShadowColor retain];
[shadowColor release];
shadowColor = theShadowColor;
[self setNeedsDisplay];
}
}

- (void)moveBy: (CGSize) delta {
if(enableDragging){
[super moveBy:delta];
Expand Down Expand Up @@ -217,7 +276,6 @@ - (void)addPointToXY:(RMProjectedPoint) point withDrawing:(BOOL)isDrawing {

- (void)recalculateGeometry {
RMMercatorToScreenProjection *projection = [mapContents mercatorToScreenProjection];
const float outset = 100.0f;

float scaledLineWidth = lineWidth;
if ( !scaleLineWidth ) {
Expand All @@ -240,6 +298,8 @@ - (void)recalculateGeometry {
CGRect screenBounds = [mapContents screenBounds];
CGPoint myPosition = [projection projectXYPoint: projectedLocation];
CGRect clippedBounds = pixelBounds;
CGFloat outset = MAX(screenBounds.size.width, screenBounds.size.height);

clippedBounds.origin.x += myPosition.x; clippedBounds.origin.y += myPosition.y;
clippedBounds = CGRectIntersection(clippedBounds, CGRectInset(screenBounds, -outset, -outset));
clippedBounds.origin.x -= myPosition.x; clippedBounds.origin.y -= myPosition.y;
Expand Down Expand Up @@ -311,8 +371,8 @@ - (void)drawInContext:(CGContextRef)theContext {
CGContextAddPath(theContext, path);

CGContextSetLineWidth(theContext, scaledLineWidth);
CGContextSetLineCap(theContext, kCGLineCapRound);
CGContextSetLineJoin(theContext, kCGLineJoinRound);
CGContextSetLineCap(theContext, lineCap);
CGContextSetLineJoin(theContext, lineJoin);
if(_lineDashLengths){
CGContextSetLineDash(theContext, lineDashPhase, dashLengths, _lineDashCount);
}
Expand All @@ -321,6 +381,10 @@ - (void)drawInContext:(CGContextRef)theContext {
CGContextSetStrokeColorWithColor(theContext, [lineColor CGColor]);
}

if ( ![shadowColor isEqual:[UIColor clearColor]] ) {
CGContextSetShadowWithColor(theContext, shadowOffset, shadowBlur, [shadowColor CGColor]);
}

if ( fillColor ) {
CGContextSetFillColorWithColor(theContext, [fillColor CGColor]);
}
Expand Down
9 changes: 5 additions & 4 deletions samples/MapTestbed/Classes/MapTestbedAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ - (void)performTest
testPath = [[RMPath alloc] initWithContents:mapContents];
[testPath setLineColor:[UIColor greenColor]];
[testPath setFillColor:[UIColor clearColor]];
[testPath setLineWidth:40.0f];
[testPath setDrawingMode:kCGPathStroke];
[testPath setLineWidth:4.0f];
[testPath setShadowColor:[UIColor colorWithWhite:0.0 alpha:0.8]];
[testPath setShadowBlur:4.0];
[testPath setShadowOffset:CGSizeMake(0, 4)];
[testPath addLineToLatLong:one];
[testPath addLineToLatLong:two];
[testPath addLineToLatLong:three];
Expand Down Expand Up @@ -108,8 +110,7 @@ - (void)performTest
testRegion = [[RMPath alloc] initWithContents:mapContents];
[testRegion setFillColor:[UIColor colorWithRed: 0.1 green:0.1 blue: 0.8 alpha: 0.5 ]];
[testRegion setLineColor:[UIColor blueColor]];
[testRegion setLineWidth:20.0f];
[testRegion setDrawingMode:kCGPathFillStroke];
[testRegion setLineWidth:2.0f];
[testRegion addLineToLatLong:r1];
[testRegion addLineToLatLong:r2];
[testRegion addLineToLatLong:r3];
Expand Down