Skip to content

Commit a1218e8

Browse files
committed
Add support for top-left and bottom-left coordinate systems.
1 parent 3136340 commit a1218e8

File tree

8 files changed

+332
-803
lines changed

8 files changed

+332
-803
lines changed

BezierView.m

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,22 @@ @implementation BezierView
3838
- (void) awakeFromNib {
3939
bezierPoints = [[NSMutableArray alloc] init];
4040
editingPoint = NSMakePoint(-1, -1);
41+
42+
[[NSNotificationCenter defaultCenter] addObserver:self
43+
selector:@selector(frameChanged:)
44+
name:NSViewFrameDidChangeNotification
45+
object:self];
4146
}
4247

4348
- (BOOL)acceptsFirstResponder {
4449
return YES;
4550
}
4651

52+
- (void)frameChanged:(NSNotification *)note {
53+
[[self delegate] elementsDidChangeInBezierView:self];
54+
}
55+
56+
4757
#define NEAR_THRESHOLD 10
4858

4959
- (NSPoint) locationOfPathElementNearPoint:(NSPoint)aPoint {
@@ -177,7 +187,9 @@ - (void)mouseDown:(NSEvent *)theEvent {
177187
NSPoint trajectory = NSPointSubtractPoint(prevMain, prevC2);
178188
control1 = NSPointAddToPoint(prevMain, trajectory);
179189

180-
control2 = NSInterpolatePoints(control1, local_point, 0.5);
190+
NSPoint thingToPointBackAt = NSPointAddToPoint(control1, NSScaledPoint(trajectory, 0.5));
191+
192+
control2 = NSInterpolatePoints(thingToPointBackAt, local_point, 0.33);
181193
}
182194

183195
BezierPoint *newPoint = [[BezierPoint alloc] init];
@@ -251,7 +263,9 @@ - (void)drawRect:(NSRect)dirtyRect {
251263
[extra release];
252264

253265
[[NSColor blackColor] set];
254-
NSBezierPath * path = [NSBezierPathCodeBuilder objectForBezierPoints:bezierPoints];
266+
NSBezierPathCodeBuilder *builder = [[NSBezierPathCodeBuilder alloc] init];
267+
[builder setBezierPoints:bezierPoints];
268+
NSBezierPath * path = [builder objectForBezierPoints];
255269
[path stroke];
256270
}
257271

CGPathRefCodeBuilder.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
@implementation CGPathRefCodeBuilder
1414

15-
+ (NSString *) codeForBezierPoints:(NSArray *)points {
15+
- (NSString *) codeForBezierPoints {
16+
NSArray *points = [self effectiveBezierPoints];
1617
NSMutableArray *lines = [NSMutableArray array];
1718

1819
[lines addObject:@"CGMutablePathRef path = CGPathCreateMutable();"];

CodeBuilder.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,21 @@
1010

1111

1212
@interface CodeBuilder : NSObject {
13-
13+
NSArray *bezierPoints;
1414
}
1515

16-
+ (NSString *) codeForBezierPoints:(NSArray *)points;
17-
+ (id) objectForBezierPoints:(NSArray *)points;
16+
// If yOrigin == 0, a bottom-left coordinate system is used.
17+
// If yOrigin != 0, a top-left coordinate system is used, and bezier
18+
// points (which always use bottom-left are converted before generating
19+
// code.
20+
@property (assign) CGFloat yOrigin;
21+
22+
@property (copy) NSArray *bezierPoints;
23+
24+
- (NSString *) codeForBezierPoints;
25+
- (id) objectForBezierPoints;
26+
27+
// Bezier Points converted for the yOrigin
28+
- (NSArray *)effectiveBezierPoints;
1829

1930
@end

CodeBuilder.m

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,51 @@
77
//
88

99
#import "CodeBuilder.h"
10+
#import "BezierPoint.h"
1011

1112

1213
@implementation CodeBuilder
14+
@synthesize yOrigin, bezierPoints;
1315

14-
+ (NSString *) codeForBezierPoints:(NSArray *)points {
16+
- (NSArray *)effectiveBezierPoints {
17+
if (yOrigin == 0.0) {
18+
return bezierPoints;
19+
}
20+
else {
21+
NSMutableArray *effectivePoints = [NSMutableArray array];
22+
for (BezierPoint *point in bezierPoints) {
23+
CGPoint mainPoint = [point mainPoint];
24+
CGPoint control1 = [point controlPoint1];
25+
CGPoint control2 = [point controlPoint2];
26+
27+
CGPoint newMain = CGPointMake(mainPoint.x, yOrigin - mainPoint.y);
28+
CGPoint newC1 = CGPointMake(control1.x, yOrigin - control1.y);
29+
CGPoint newC2 = CGPointMake(control2.x, yOrigin - control2.y);
30+
31+
BezierPoint *newPoint = [[BezierPoint alloc] init];
32+
[newPoint setMainPoint:newMain];
33+
[newPoint setControlPoint1:newC1];
34+
[newPoint setControlPoint2:newC2];
35+
[effectivePoints addObject:newPoint];
36+
[newPoint release];
37+
}
38+
return effectivePoints;
39+
}
40+
}
41+
42+
- (NSString *) codeForBezierPoints {
1543
NSLog(@"SUBCLASSES MUST OVERRIDE");
1644
return nil;
1745
}
1846

19-
+ (id) objectForBezierPoints:(NSArray *)points {
47+
- (id) objectForBezierPoints {
2048
NSLog(@"SUBCLASSES MUST OVERRIDE");
2149
return nil;
2250
}
2351

52+
- (void)dealloc {
53+
[bezierPoints release];
54+
[super dealloc];
55+
}
56+
2457
@end

0 commit comments

Comments
 (0)