forked from adlr/formulatepro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFPSquiggle.m
More file actions
167 lines (147 loc) · 5.16 KB
/
FPSquiggle.m
File metadata and controls
167 lines (147 loc) · 5.16 KB
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
//
// FPSquiggle.m
// FormulatePro
//
// Created by Andrew de los Reyes on 8/5/06.
// Copyright 2006 Andrew de los Reyes. All rights reserved.
//
#import "FPSquiggle.h"
#import "NSMutableDictionaryAdditions.h"
@implementation FPSquiggle
+ (NSString *)archivalClassName;
{
return @"Squiggle";
}
- (id)initWithGraphic:(FPGraphic *)graphic
{
self = [super initWithGraphic:graphic];
assert([graphic class] == [FPSquiggle class]);
if (self) {
FPSquiggle *gr = (FPSquiggle *)graphic;
self->_path = [gr->_path copy];
}
return self;
}
static NSString *pathArchiveKey = @"path";
- (id)initWithArchivalDictionary:(NSDictionary *)dict
inDocumentView:(FPDocumentView *)docView
{
self = [super initWithArchivalDictionary:dict
inDocumentView:docView];
if (self) {
NSBezierPath *path = [NSBezierPath bezierPath];
[path setLineWidth:1.0];
[path setLineJoinStyle:NSBevelLineJoinStyle];
NSArray *arr = [dict objectForKey:pathArchiveKey];
for (int i = 0; i < [arr count]; i++) {
NSPoint pt = NSPointFromString([arr objectAtIndex:i]);
if (0 == i)
[path moveToPoint:pt];
else
[path lineToPoint:pt];
}
_path = [path retain];
_bounds = [_path bounds];
}
return self;
}
- (NSDictionary *)archivalDictionary
{
NSMutableArray *arr = [NSMutableArray array];
for (int i = 0; i < [_path elementCount]; i++) {
NSPoint p[3];
NSBezierPathElement t;
t = [_path elementAtIndex:i associatedPoints:p];
if (0 == i)
assert(NSMoveToBezierPathElement == t);
else
assert(NSLineToBezierPathElement == t);
[arr addObject:NSStringFromPoint(p[0])];
}
NSMutableDictionary *ret =
[NSMutableDictionary
dictionaryWithDictionary:[super archivalDictionary]];
[ret setObject:arr forNonexistentKey:pathArchiveKey];
return ret;
}
- (id)initInDocumentView:(FPDocumentView *)docView
{
self = [super initInDocumentView:docView];
if (self) {
_path = nil;
}
return self;
}
- (void)draw:(BOOL)selected
{
NSBezierPath *tempPath = [[_path copy] autorelease];
NSAffineTransform *scaleTransform = [NSAffineTransform transform];
NSSize tempSize = [tempPath bounds].size;
[scaleTransform scaleXBy:(tempSize.width > 1.0e-6) ?
(_bounds.size.width/tempSize.width) : 1.0
yBy:(tempSize.height > 1.0e-6) ?
(_bounds.size.height/tempSize.height) : 1.0];
[tempPath transformUsingAffineTransform:scaleTransform];
NSAffineTransform *translateTransform = [NSAffineTransform transform];
[translateTransform
translateXBy:(_bounds.origin.x - [tempPath bounds].origin.x)
yBy:(_bounds.origin.y - [tempPath bounds].origin.y)];
[tempPath transformUsingAffineTransform:translateTransform];
[_strokeColor set];
[tempPath stroke];
}
// this function not tested ever
- (NSPoint)windowToPagePoint:(NSPoint)windowPoint
{
return [_docView convertPoint:
[[[_docView window] contentView] convertPoint:windowPoint
toView:_docView]
toPage:_page];
}
- (NSPoint)pageToWindowPoint:(NSPoint)pagePoint
{
return [[[_docView window] contentView]
convertPoint:[_docView convertPoint:pagePoint fromPage:_page]
fromView:_docView];
}
- (BOOL)placeWithEvent:(NSEvent *)theEvent
{
NSPoint point;
NSGraphicsContext *gc;
_page = [_docView pageForPointFromEvent:theEvent];
point = [_docView pagePointForPointFromEvent:theEvent page:_page];
_bounds.origin = point;
_bounds.size = NSMakeSize(0.0,0.0);
_path = [[NSBezierPath bezierPath] retain];
[_path moveToPoint:point];
gc = [NSGraphicsContext graphicsContextWithWindow:[_docView window]];
[NSGraphicsContext setCurrentContext:gc];
[NSBezierPath setDefaultLineWidth:[_docView scaleFactor]];
[_path setLineWidth:1.0];
[_path setLineJoinStyle:NSBevelLineJoinStyle];
[_strokeColor set];
for (;;) {
NSPoint new_point;
new_point = [_docView pagePointForPointFromEvent:theEvent page:_page];
[_path lineToPoint:new_point];
if (NSPointInRect([self pageToWindowPoint:point],
[[[_docView window] contentView] frame]) &&
NSPointInRect([self pageToWindowPoint:new_point],
[[[_docView window] contentView] frame])) {
[NSBezierPath strokeLineFromPoint:[self pageToWindowPoint:point]
toPoint:[self pageToWindowPoint:new_point]];
[gc flushGraphics];
}
// get ready for next iteration of the loop, or break out of loop
point = new_point;
theEvent = [[_docView window] nextEventMatchingMask:
(NSLeftMouseDraggedMask | NSLeftMouseUpMask)];
if ([theEvent type] == NSLeftMouseUp)
break;
}
_bounds = [_path bounds];
[NSGraphicsContext restoreGraphicsState];
[[[_docView window] contentView] setNeedsDisplay:YES];
return YES;
}
@end