Skip to content

Commit c78456e

Browse files
committed
Initial commit of project
1 parent ebe776e commit c78456e

20 files changed

+6271
-0
lines changed

BezierBuilder-Info.plist

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>English</string>
7+
<key>CFBundleDocumentTypes</key>
8+
<array>
9+
<dict>
10+
<key>CFBundleTypeExtensions</key>
11+
<array>
12+
<string>????</string>
13+
</array>
14+
<key>CFBundleTypeIconFile</key>
15+
<string></string>
16+
<key>CFBundleTypeName</key>
17+
<string>DocumentType</string>
18+
<key>CFBundleTypeOSTypes</key>
19+
<array>
20+
<string>????</string>
21+
</array>
22+
<key>CFBundleTypeRole</key>
23+
<string>Editor</string>
24+
<key>NSDocumentClass</key>
25+
<string>MyDocument</string>
26+
</dict>
27+
</array>
28+
<key>CFBundleExecutable</key>
29+
<string>${EXECUTABLE_NAME}</string>
30+
<key>CFBundleIconFile</key>
31+
<string></string>
32+
<key>CFBundleIdentifier</key>
33+
<string>com.yourcompany.${PRODUCT_NAME:rfc1034identifier}</string>
34+
<key>CFBundleInfoDictionaryVersion</key>
35+
<string>6.0</string>
36+
<key>CFBundleName</key>
37+
<string>${PRODUCT_NAME}</string>
38+
<key>CFBundlePackageType</key>
39+
<string>APPL</string>
40+
<key>CFBundleSignature</key>
41+
<string>????</string>
42+
<key>CFBundleVersion</key>
43+
<string>1</string>
44+
<key>CFBundleShortVersionString</key>
45+
<string>1.0</string>
46+
<key>LSMinimumSystemVersion</key>
47+
<string>${MACOSX_DEPLOYMENT_TARGET}</string>
48+
<key>NSMainNibFile</key>
49+
<string>MainMenu</string>
50+
<key>NSPrincipalClass</key>
51+
<string>NSApplication</string>
52+
</dict>
53+
</plist>

BezierBuilder.xcodeproj/project.pbxproj

Lines changed: 351 additions & 0 deletions
Large diffs are not rendered by default.

BezierBuilder_Prefix.pch

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// Prefix header for all source files of the 'BezierBuilder' target in the 'BezierBuilder' project
3+
//
4+
5+
#ifdef __OBJC__
6+
#import <Cocoa/Cocoa.h>
7+
8+
static NSPoint NSPointAddToPoint(NSPoint p1, NSPoint p2) {
9+
return NSMakePoint(p1.x+p2.x, p1.y+p2.y);
10+
}
11+
12+
static NSPoint NSPointSubtractPoint(NSPoint p1, NSPoint p2) {
13+
return NSMakePoint(p1.x-p2.x, p1.y-p2.y);
14+
}
15+
#endif

BezierPoint.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// BezierPoint.h
3+
// BezierBuilder
4+
//
5+
// Created by Dave DeLong on 2/1/11.
6+
// Copyright 2011 __MyCompanyName__. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
12+
@interface BezierPoint : NSObject {
13+
14+
}
15+
16+
@property CGPoint mainPoint;
17+
@property CGPoint controlPoint1;
18+
@property CGPoint controlPoint2;
19+
20+
@end

BezierPoint.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// BezierPoint.m
3+
// BezierBuilder
4+
//
5+
// Created by Dave DeLong on 2/1/11.
6+
// Copyright 2011 __MyCompanyName__. All rights reserved.
7+
//
8+
9+
#import "BezierPoint.h"
10+
11+
12+
@implementation BezierPoint
13+
@synthesize mainPoint;
14+
@synthesize controlPoint1, controlPoint2;
15+
16+
@end

BezierView.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// BezierView.h
3+
// BezierBuilder
4+
//
5+
// Created by Dave DeLong on 7/7/09.
6+
// Copyright 2009 __MyCompanyName__. All rights reserved.
7+
//
8+
9+
#import <Cocoa/Cocoa.h>
10+
11+
@class BezierPoint;
12+
13+
@protocol BezierViewDelegate
14+
15+
- (void) didChangeElementAtIndex:(NSPoint)index byDelta:(NSPoint)point;
16+
- (void) didAddPoints:(NSPointArray)points;
17+
- (NSBezierPath *) path;
18+
19+
@end
20+
21+
22+
@interface BezierView : NSView {
23+
IBOutlet id<BezierViewDelegate> delegate;
24+
NSPoint editingPoint;
25+
NSPoint previousPoint;
26+
27+
BOOL isShiftDown;
28+
}
29+
30+
@property (nonatomic, assign) id<BezierViewDelegate> delegate;
31+
32+
@end

BezierView.m

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
//
2+
// BezierView.m
3+
// BezierBuilder
4+
//
5+
// Created by Dave DeLong on 7/7/09.
6+
// Copyright 2009 __MyCompanyName__. All rights reserved.
7+
//
8+
9+
#import "BezierView.h"
10+
11+
float NSDistanceFromPointToPoint(NSPoint point1, NSPoint point2) {
12+
float dx2 = (point1.x - point2.x) * (point1.x - point2.x);
13+
float dy2 = (point1.y - point2.y) * (point1.y - point2.y);
14+
return sqrt(dx2 + dy2);
15+
}
16+
17+
@implementation BezierView
18+
19+
@synthesize delegate;
20+
21+
- (BOOL)acceptsFirstResponder {
22+
return YES;
23+
}
24+
25+
- (void) flagsChanged:(NSEvent *)event {
26+
isShiftDown = (([event modifierFlags] & NSShiftKeyMask) > 0);
27+
}
28+
29+
- (NSPoint) locationOfPathElementNearPoint:(NSPoint)aPoint {
30+
NSBezierPath * path = [delegate path];
31+
NSPoint closest = NSMakePoint(-1, -1);
32+
33+
int elemCount = [path elementCount];
34+
float distance = FLT_MAX;
35+
for (int i = 0; i < elemCount; ++i) {
36+
NSPoint points[3];
37+
NSBezierPathElement element = [path elementAtIndex:(elemCount - 1) associatedPoints:points];
38+
float tDistance = 0;
39+
switch (element) {
40+
case NSCurveToBezierPathElement:
41+
tDistance = NSDistanceFromPointToPoint(aPoint, points[2]);
42+
NSLog(@"tDist %d,2: %f", i, tDistance);
43+
if (tDistance <= distance && tDistance <= 5) {
44+
distance = tDistance;
45+
closest = NSMakePoint(i, 2);
46+
}
47+
tDistance = NSDistanceFromPointToPoint(aPoint, points[1]);
48+
NSLog(@"tDist %d,1: %f", i, tDistance);
49+
if (tDistance <= distance && tDistance <= 5) {
50+
distance = tDistance;
51+
closest = NSMakePoint(i, 1);
52+
}
53+
default:
54+
tDistance = NSDistanceFromPointToPoint(aPoint, points[0]);
55+
NSLog(@"tDist %d,0: %f", i, tDistance);
56+
if (tDistance <= distance && tDistance <= 5) {
57+
distance = tDistance;
58+
closest = NSMakePoint(i, 0);
59+
}
60+
}
61+
}
62+
return closest;
63+
}
64+
65+
- (NSPointArray) pointArrayAtPoint:(NSPoint)centerPoint {
66+
NSPoint * points = malloc(3 * sizeof(NSPoint));
67+
points[0] = centerPoint;
68+
points[1] = NSMakePoint(MAX(centerPoint.x+20, 0), centerPoint.y);
69+
points[2] = NSMakePoint(MIN(centerPoint.x-20, [self frame].size.width), centerPoint.y);
70+
return points;
71+
}
72+
73+
- (void)mouseDown:(NSEvent *)theEvent {
74+
NSPoint event_location = [theEvent locationInWindow];
75+
NSPoint local_point = [self convertPoint:event_location fromView:nil];
76+
editingPoint = [self locationOfPathElementNearPoint:local_point];
77+
previousPoint = local_point;
78+
if (editingPoint.y <= 0 && isShiftDown) { editingPoint.y = -1; }
79+
if (editingPoint.x < 0) {
80+
NSPoint * points = [self pointArrayAtPoint:local_point];
81+
[[self delegate] didAddPoints:points];
82+
free(points);
83+
//setting y to -1 means that all the control points will be dragged
84+
editingPoint = NSMakePoint([[[self delegate] path] elementCount]-1, -1);
85+
}
86+
}
87+
88+
- (void)mouseDragged:(NSEvent *)theEvent {
89+
NSPoint event_location = [theEvent locationInWindow];
90+
NSPoint local_point = [self convertPoint:event_location fromView:nil];
91+
[[self delegate] didChangeElementAtIndex:editingPoint byDelta:NSPointSubtractPoint(local_point, previousPoint)];
92+
previousPoint = local_point;
93+
}
94+
95+
- (void)mouseUp:(NSEvent *)theEvent {
96+
NSPoint event_location = [theEvent locationInWindow];
97+
NSPoint local_point = [self convertPoint:event_location fromView:nil];
98+
[[self delegate] didChangeElementAtIndex:editingPoint byDelta:NSPointSubtractPoint(local_point, previousPoint)];
99+
previousPoint = local_point;
100+
}
101+
102+
- (void)drawRect:(NSRect)dirtyRect {
103+
NSBezierPath * path = [delegate path];
104+
[path stroke];
105+
106+
[[NSColor redColor] set];
107+
NSBezierPath * extra = [[NSBezierPath alloc] init];
108+
int elemCount = [path elementCount];
109+
for (int i = 0; i < elemCount; ++i) {
110+
NSPoint points[3];
111+
NSBezierPathElement element = [path elementAtIndex:i associatedPoints:points];
112+
NSRect rect;
113+
switch (element) {
114+
case NSCurveToBezierPathElement:
115+
[extra moveToPoint:points[1]];
116+
[extra lineToPoint:points[0]];
117+
[extra lineToPoint:points[2]];
118+
rect = NSMakeRect(points[1].x-2, points[1].y-2, 5, 5);
119+
[extra appendBezierPathWithOvalInRect:rect];
120+
rect = NSMakeRect(points[2].x-2, points[2].y-2, 5, 5);
121+
[extra appendBezierPathWithOvalInRect:rect];
122+
default:
123+
rect = NSMakeRect(points[0].x-2, points[0].y-2, 5, 5);
124+
[extra appendBezierPathWithOvalInRect:rect];
125+
}
126+
}
127+
[extra stroke];
128+
[extra release];
129+
}
130+
131+
@end

CGPathRefCodeBuilder.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// CGPathRefCodeBuilder.h
3+
// BezierBuilder
4+
//
5+
// Created by Dave DeLong on 2/1/11.
6+
// Copyright 2011 __MyCompanyName__. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "CodeBuilder.h"
11+
12+
@interface CGPathRefCodeBuilder : CodeBuilder {
13+
14+
}
15+
16+
@end

CGPathRefCodeBuilder.m

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// CGPathRefCodeBuilder.m
3+
// BezierBuilder
4+
//
5+
// Created by Dave DeLong on 2/1/11.
6+
// Copyright 2011 __MyCompanyName__. All rights reserved.
7+
//
8+
9+
#import "CGPathRefCodeBuilder.h"
10+
#import "BezierPoint.h"
11+
12+
13+
@implementation CGPathRefCodeBuilder
14+
15+
+ (NSString *) codeForBezierPoints:(NSArray *)points {
16+
NSMutableArray *lines = [NSMutableArray array];
17+
18+
[lines addObject:@"CGMutablePathRef path = CGPathCreateMutable();"];
19+
for (NSUInteger i = 0; i < [points count]; ++i) {
20+
BezierPoint *point = [points objectAtIndex:i];
21+
if (i == 0) {
22+
[lines addObject:[NSString stringWithFormat:@"CGPathMoveToPoint(path, NULL, %f, %f);", [point mainPoint].x, [point mainPoint].y]];
23+
} else {
24+
[lines addObject:[NSString stringWithFormat:@"CGPathAddCurveToPoint(path, NULL, %f, %f, %f, %f, %f, %f);",
25+
[point controlPoint1].x, [point controlPoint1].y,
26+
[point controlPoint2].x, [point controlPoint2].y,
27+
[point mainPoint].x, [point mainPoint].y]];
28+
}
29+
}
30+
31+
[lines addObject:@"CGContextAddPath(<#CGContextRef#>, path);"];
32+
[lines addObject:@"CGContextStrokePath(<#CGContextRef#>);"];
33+
[lines addObject:@"CFRelease(path);"];
34+
35+
return [lines componentsJoinedByString:@"\n"];
36+
}
37+
38+
@end

CodeBuilder.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// CodeBuilder.h
3+
// BezierBuilder
4+
//
5+
// Created by Dave DeLong on 2/1/11.
6+
// Copyright 2011 __MyCompanyName__. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
12+
@interface CodeBuilder : NSObject {
13+
14+
}
15+
16+
+ (NSString *) codeForBezierPoints:(NSArray *)points;
17+
+ (id) objectForBezierPoints:(NSArray *)points;
18+
19+
@end

CodeBuilder.m

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// CodeBuilder.m
3+
// BezierBuilder
4+
//
5+
// Created by Dave DeLong on 2/1/11.
6+
// Copyright 2011 __MyCompanyName__. All rights reserved.
7+
//
8+
9+
#import "CodeBuilder.h"
10+
11+
12+
@implementation CodeBuilder
13+
14+
+ (NSString *) codeForBezierPoints:(NSArray *)points {
15+
NSLog(@"SUBCLASSES MUST OVERRIDE");
16+
return nil;
17+
}
18+
19+
+ (id) objectForBezierPoints:(NSArray *)points {
20+
NSLog(@"SUBCLASSES MUST OVERRIDE");
21+
return nil;
22+
}
23+
24+
@end

0 commit comments

Comments
 (0)