Created
November 30, 2011 22:34
-
-
Save snown/1411457 to your computer and use it in GitHub Desktop.
Derive and Construct a new NSBezierPath
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)resetDerivedPathDictionary:(NSMutableDictionary *)aDerivedPathDictionary { | |
if ([aDerivedPathDictionary count] > 0) { | |
[aDerivedPathDictionary removeAllObjects]; | |
} | |
[aDerivedPathDictionary setObject:[NSMutableArray array] forKey:WRADerivedPathInstructionsKey]; | |
} | |
- (void)addLast:(NSInteger)numberOfElements elementsFromBezierPath:(NSBezierPath *)aBezierPath toDerivedPathDictionary:(NSMutableDictionary *)aDerivedPathDictionary atIndex:(NSUInteger)index { | |
NSLog(@"%s [Line %d]", __PRETTY_FUNCTION__, __LINE__); | |
NSInteger elementCount = [aBezierPath elementCount]; | |
NSMutableArray *instructionSet = [NSMutableArray array]; | |
for (NSInteger elementWalkCount = (elementCount - numberOfElements); elementWalkCount<elementCount; elementWalkCount++) { | |
NSBezierPathElement element = [aBezierPath elementAtIndex:elementWalkCount]; | |
NSMutableArray *pointValueArray = [NSMutableArray array]; | |
NSDictionary *instruction = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInteger:element], @"Element", pointValueArray, @"Points", nil]; | |
if (element == NSCurveToBezierPathElement) { | |
// NSLog(@"%s [Line %d] - Found Curve", __PRETTY_FUNCTION__, __LINE__); | |
NSPoint points[3]; | |
[aBezierPath elementAtIndex:elementWalkCount associatedPoints:points]; | |
for (int walkCount = 0; walkCount<3; walkCount++) { | |
[pointValueArray addObject:[NSValue valueWithPoint:points[walkCount]]]; | |
} | |
// NSLog(@"%s [Line %d] - Curve Points:\n%@", __PRETTY_FUNCTION__, __LINE__); | |
} else { | |
NSPoint points[1]; | |
[aBezierPath elementAtIndex:elementWalkCount associatedPoints:points]; | |
[pointValueArray addObject:[NSValue valueWithPoint:points[0]]]; | |
} | |
[instructionSet addObject:instruction]; | |
} | |
if (index >= elementCount) { | |
[(NSMutableArray *)[aDerivedPathDictionary objectForKey:WRADerivedPathInstructionsKey] addObjectsFromArray:instructionSet]; | |
} else { | |
[(NSMutableArray *)[aDerivedPathDictionary objectForKey:WRADerivedPathInstructionsKey] insertObjects:instructionSet atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(index, [instructionSet count])]]; | |
} | |
} | |
- (void)addLast:(NSInteger)numberOfElements elementsFromBezierPath:(NSBezierPath *)aBezierPath toDerivedPathDictionary:(NSMutableDictionary *)aDerivedPathDictionary { | |
[self addLast:numberOfElements elementsFromBezierPath:aBezierPath toDerivedPathDictionary:aDerivedPathDictionary atIndex:[(NSMutableArray *)[aDerivedPathDictionary objectForKey:WRADerivedPathInstructionsKey] count]]; | |
} | |
- (void)prependLast:(NSInteger)numberOfElements elementsFromBezierPath:(NSBezierPath *)aBezierPath toDerivedPathDictionary:(NSMutableDictionary *)aDerivedPathDictionary { | |
[self addLast:numberOfElements elementsFromBezierPath:aBezierPath toDerivedPathDictionary:aDerivedPathDictionary atIndex:0]; | |
} | |
- (void)drawDerivedPath:(NSMutableDictionary *)aDerivedPathDictionary { | |
NSLog(@"%s [Line %d]", __PRETTY_FUNCTION__, __LINE__); | |
[NSGraphicsContext saveGraphicsState]; | |
NSBezierPath *constructedPath = [NSBezierPath bezierPath]; | |
NSMutableString *bezierElementDescription = [NSMutableString string]; | |
NSPoint startPoint = [(NSValue *)[aDerivedPathDictionary objectForKey:WRADerivedPathStartingPointKey] pointValue]; | |
[bezierElementDescription appendFormat:@"Starting at Point = X: %f Y: %f\n", startPoint.x, startPoint.y]; | |
[constructedPath moveToPoint:startPoint]; | |
for (NSDictionary *instruction in (NSArray *)[aDerivedPathDictionary objectForKey:WRADerivedPathInstructionsKey]) { | |
NSBezierPathElement element = [(NSNumber *)[instruction objectForKey:@"Element"] unsignedIntegerValue]; | |
NSArray *points = (NSArray *)[instruction objectForKey:@"Points"]; | |
if ([points count] > 0) { | |
if (element == NSMoveToBezierPathElement) { | |
NSPoint movePoint = [(NSValue *)[points objectAtIndex:0] pointValue]; | |
[bezierElementDescription appendFormat:@"Move To Point: X: %f Y:%f\n", movePoint.x, movePoint.y]; | |
[constructedPath moveToPoint:movePoint]; | |
} else if (element == NSLineToBezierPathElement) { | |
NSPoint linePoint = [(NSValue *)[points objectAtIndex:0] pointValue]; | |
[bezierElementDescription appendFormat:@"Line To Point: X: %f Y:%f\n", linePoint.x, linePoint.y]; | |
[constructedPath lineToPoint:linePoint]; | |
} else if (element == NSCurveToBezierPathElement) { | |
NSPoint controlPoint1 = [(NSValue *)[points objectAtIndex:0] pointValue]; | |
NSPoint controlPoint2 = [(NSValue *)[points objectAtIndex:1] pointValue]; | |
NSPoint endPoint = [(NSValue *)[points objectAtIndex:2] pointValue]; | |
[bezierElementDescription appendFormat:@"Curve To Point: X: %f Y:%f\n", endPoint.x, endPoint.y]; | |
[bezierElementDescription appendFormat:@"\tControl Point 1: X: %f Y:%f\n", controlPoint1.x, controlPoint1.y]; | |
[bezierElementDescription appendFormat:@"\tControl Point 2: X: %f Y:%f\n", controlPoint2.x, controlPoint2.y]; | |
[constructedPath curveToPoint:endPoint controlPoint1:controlPoint1 controlPoint2:controlPoint2]; | |
} else if (element == NSClosePathBezierPathElement) { | |
NSPoint closePoint = [(NSValue *)[points objectAtIndex:0] pointValue]; | |
[bezierElementDescription appendFormat:@"Close at Point: X: %f Y:%f\n", closePoint.x, closePoint.y]; | |
[constructedPath closePath]; | |
} | |
} | |
} | |
NSLog(@"%s [Line %d]\n%@", __PRETTY_FUNCTION__, __LINE__, bezierElementDescription); | |
[[NSColor redColor] set]; | |
[constructedPath stroke]; | |
[NSGraphicsContext restoreGraphicsState]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment