forked from dev2dev/BezierBuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BezierView.m
277 lines (225 loc) · 9.17 KB
/
BezierView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//
// BezierView.m
// BezierBuilder
//
// Created by Dave DeLong on 7/7/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "BezierView.h"
#import "BezierPoint.h"
#import "NSBezierPathCodeBuilder.h"
float NSDistanceFromPointToPoint(NSPoint point1, NSPoint point2) {
float dx2 = (point1.x - point2.x) * (point1.x - point2.x);
float dy2 = (point1.y - point2.y) * (point1.y - point2.y);
return sqrt(dx2 + dy2);
}
NSPoint NSInterpolatePoints(NSPoint point1, NSPoint point2, float amount) {
return NSMakePoint(point1.x * (1-amount) + point2.x * amount,
point1.y * (1-amount) + point2.y * amount);
}
NSPoint NSNormalizedPoint(NSPoint point) {
float magnitude = sqrtf(point.x * point.x + point.y * point.y);
return NSMakePoint(point.x / magnitude, point.y / magnitude);
}
NSPoint NSScaledPoint(NSPoint point, float scale) {
return NSMakePoint(point.x * scale, point.y * scale);
}
@implementation BezierView
@synthesize delegate, bezierPoints;
- (void) awakeFromNib {
bezierPoints = [[NSMutableArray alloc] init];
editingPoint = NSMakePoint(-1, -1);
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(frameChanged:)
name:NSViewFrameDidChangeNotification
object:self];
}
- (BOOL)acceptsFirstResponder {
return YES;
}
- (void)frameChanged:(NSNotification *)note {
[[self delegate] elementsDidChangeInBezierView:self];
}
#define NEAR_THRESHOLD 10
- (NSPoint) locationOfPathElementNearPoint:(NSPoint)aPoint {
NSPoint closest = NSMakePoint(-1, -1);
float distance = FLT_MAX;
for (NSUInteger i = 0; i < [bezierPoints count]; ++i) {
BezierPoint *p = [bezierPoints objectAtIndex:i];
float tDistance = 0;
if (i > 0) {
tDistance = NSDistanceFromPointToPoint(aPoint, [p controlPoint2]);
if (tDistance <= distance && tDistance <= NEAR_THRESHOLD) {
distance = tDistance;
closest = NSMakePoint(i, 2);
}
tDistance = NSDistanceFromPointToPoint(aPoint, [p controlPoint1]);
if (tDistance <= distance && tDistance <= NEAR_THRESHOLD) {
distance = tDistance;
closest = NSMakePoint(i, 1);
}
}
tDistance = NSDistanceFromPointToPoint(aPoint, [p mainPoint]);
if (tDistance <= distance && tDistance <= NEAR_THRESHOLD) {
distance = tDistance;
closest = NSMakePoint(i, 0);
}
}
return closest;
}
#define CONTROL_OFFSET 20
- (void) updateEditingPointWithPoint:(NSPoint)p withEvent:(NSEvent *)event {
BOOL isShiftDown = ([event modifierFlags] & NSShiftKeyMask) > 0;
BOOL isCommandDown = ([event modifierFlags] & NSCommandKeyMask) > 0;
BezierPoint *point = [bezierPoints objectAtIndex:editingPoint.x];
if (editingPoint.y == -1) {
[point setMainPoint:p];
if (editingPoint.x > 0) {
NSPoint c1 = [point controlPoint1];
BezierPoint *prevPoint = [bezierPoints objectAtIndex:editingPoint.x - 1];
NSPoint prevOrigin = [prevPoint mainPoint];
NSPoint trajectory = NSPointSubtractPoint(c1, prevOrigin);
NSPoint thingToPointBackAt = NSPointAddToPoint(c1, NSScaledPoint(trajectory, 0.5));
NSPoint newC2 = NSInterpolatePoints(thingToPointBackAt, p, 0.33);
[point setControlPoint2:newC2];
}
}
else if (editingPoint.y == 0) {
NSPoint diff = NSPointSubtractPoint(p, [point mainPoint]);
[point setMainPoint:p];
if (!isCommandDown) {
NSPoint newPoint = NSPointAddToPoint(diff, [point controlPoint2]);
[point setControlPoint2:newPoint];
}
if (!isCommandDown && editingPoint.x < [bezierPoints count] - 1) {
// Update c1 of the next point too
BezierPoint *nextPoint = [bezierPoints objectAtIndex:editingPoint.x + 1];
NSPoint newPoint = NSPointAddToPoint(diff, [nextPoint controlPoint1]);
[nextPoint setControlPoint1:newPoint];
}
} else if (editingPoint.y == 1) {
[point setControlPoint1:p];
if (!isCommandDown && editingPoint.x > 1) {
// Update the previous c2 to keep it in line
BezierPoint *prevPoint = [bezierPoints objectAtIndex:editingPoint.x - 1];
float controlLength = NSDistanceFromPointToPoint([prevPoint mainPoint], [prevPoint controlPoint2]);
if (isShiftDown) {
controlLength = NSDistanceFromPointToPoint([prevPoint mainPoint], p);
}
NSPoint trajectory = NSPointSubtractPoint([prevPoint mainPoint], p);
NSPoint delta = NSScaledPoint(NSNormalizedPoint(trajectory), controlLength);
NSPoint newPoint = NSPointAddToPoint([prevPoint mainPoint], delta);
[prevPoint setControlPoint2:newPoint];
}
} else if (editingPoint.y == 2) {
[point setControlPoint2:p];
if (!isCommandDown && editingPoint.x < [bezierPoints count] - 1) {
// Update the next c1 to keep it in line
BezierPoint *nextPoint = [bezierPoints objectAtIndex:editingPoint.x + 1];
float controlLength = NSDistanceFromPointToPoint([point mainPoint], [nextPoint controlPoint1]);
if (isShiftDown) {
controlLength = NSDistanceFromPointToPoint([point mainPoint], p);
}
NSPoint trajectory = NSPointSubtractPoint([point mainPoint], p);
NSPoint delta = NSScaledPoint(NSNormalizedPoint(trajectory), controlLength);
NSPoint newPoint = NSPointAddToPoint([point mainPoint], delta);
[nextPoint setControlPoint1:newPoint];
}
}
}
- (void)mouseDown:(NSEvent *)theEvent {
NSPoint event_location = [theEvent locationInWindow];
NSPoint local_point = [self convertPoint:event_location fromView:nil];
editingPoint = [self locationOfPathElementNearPoint:local_point];
if (editingPoint.x < 0) {
// It's a new point
CGPoint control1 = local_point;
CGPoint control2 = local_point;
NSUInteger pointCount = [bezierPoints count];
BezierPoint *lastPoint = [bezierPoints lastObject];
if (pointCount == 1) {
// This is the first curve segment, so extrapolating from the
// trajectory of the previous segment makes no sense
NSPoint prevMain = [lastPoint mainPoint];
control1 = NSInterpolatePoints(prevMain, local_point, 0.3);
NSPoint trajectory = NSPointSubtractPoint(control1, prevMain);
NSPoint thingToPointBackAt = NSPointAddToPoint(control1, NSScaledPoint(trajectory, 0.5));
control2 = NSInterpolatePoints(thingToPointBackAt, local_point, 0.33);
}
else if (pointCount > 1) {
NSPoint prevC2 = [lastPoint controlPoint2];
NSPoint prevMain = [lastPoint mainPoint];
NSPoint trajectory = NSPointSubtractPoint(prevMain, prevC2);
control1 = NSPointAddToPoint(prevMain, trajectory);
NSPoint thingToPointBackAt = NSPointAddToPoint(control1, NSScaledPoint(trajectory, 0.5));
control2 = NSInterpolatePoints(thingToPointBackAt, local_point, 0.33);
}
BezierPoint *newPoint = [[BezierPoint alloc] init];
[newPoint setMainPoint:local_point];
[newPoint setControlPoint1:control1];
[newPoint setControlPoint2:control2];
[bezierPoints addObject:newPoint];
[newPoint release];
[[self delegate] elementsDidChangeInBezierView:self];
//setting y to -1 means that all the control points will be dragged
editingPoint = NSMakePoint([bezierPoints count]-1, -1);
}
}
- (void)mouseDragged:(NSEvent *)theEvent {
NSPoint event_location = [theEvent locationInWindow];
NSPoint local_point = [self convertPoint:event_location fromView:nil];
[self updateEditingPointWithPoint:local_point withEvent:theEvent];
[[self delegate] elementsDidChangeInBezierView:self];
}
- (void)mouseUp:(NSEvent *)theEvent {
NSPoint event_location = [theEvent locationInWindow];
NSPoint local_point = [self convertPoint:event_location fromView:nil];
[self updateEditingPointWithPoint:local_point withEvent:theEvent];
[[self delegate] elementsDidChangeInBezierView:self];
}
- (void)deleteBackward:(id)sender {
if ([bezierPoints count] > 0) {
[bezierPoints removeLastObject];
[[self delegate] elementsDidChangeInBezierView:self];
}
}
- (void)keyDown:(NSEvent *)theEvent {
[self interpretKeyEvents:[NSArray arrayWithObject:theEvent]];
}
#define HANDLE_WIDTH 5
#define HANDLE_HEIGHT 5
- (void)drawRect:(NSRect)dirtyRect {
[[[NSColor redColor] colorWithAlphaComponent:0.6] set];
NSBezierPath * extra = [[NSBezierPath alloc] init];
for (NSUInteger i = 0; i < [bezierPoints count]; ++i) {
BezierPoint *bezierPoint = [bezierPoints objectAtIndex:i];
NSRect r;
if (i > 0) {
[extra moveToPoint:[[bezierPoints objectAtIndex:i-1] mainPoint]];
[extra lineToPoint:[bezierPoint controlPoint1]];
[extra moveToPoint:[bezierPoint controlPoint2]];
[extra lineToPoint:[bezierPoint mainPoint]];
r = NSMakeRect([bezierPoint controlPoint1].x, [bezierPoint controlPoint1].y, HANDLE_WIDTH, HANDLE_HEIGHT);
r = NSOffsetRect(r, -HANDLE_WIDTH/2, -HANDLE_HEIGHT/2);
[extra appendBezierPathWithOvalInRect:r];
r = NSMakeRect([bezierPoint controlPoint2].x, [bezierPoint controlPoint2].y, HANDLE_WIDTH, HANDLE_HEIGHT);
r = NSOffsetRect(r, -HANDLE_WIDTH/2, -HANDLE_HEIGHT/2);
[extra appendBezierPathWithOvalInRect:r];
}
r = NSMakeRect([bezierPoint mainPoint].x, [bezierPoint mainPoint].y, HANDLE_WIDTH, HANDLE_HEIGHT);
r = NSOffsetRect(r, -HANDLE_WIDTH/2, -HANDLE_HEIGHT/2);
[extra appendBezierPathWithOvalInRect:r];
}
[extra stroke];
[extra release];
[[NSColor blackColor] set];
NSBezierPathCodeBuilder *builder = [[NSBezierPathCodeBuilder alloc] init];
[builder setBezierPoints:bezierPoints];
NSBezierPath * path = [builder objectForBezierPoints];
[path stroke];
}
- (void) dealloc {
[bezierPoints release];
[super dealloc];
}
@end