Skip to content

Commit

Permalink
Updated RMPath to add line cap/join and shadow options
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeltyson committed Mar 21, 2012
1 parent e3657ac commit 0070fcf
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 38 deletions.
36 changes: 9 additions & 27 deletions MapView/Map/RMPath.h
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
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
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

0 comments on commit 0070fcf

Please sign in to comment.