-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBracket.m
More file actions
executable file
·282 lines (247 loc) · 6.17 KB
/
Copy pathBracket.m
File metadata and controls
executable file
·282 lines (247 loc) · 6.17 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
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
278
279
280
281
282
// ##############################################################
// Bracket.m
// Magic Number Machine
//
// Created by Matt Gallagher on Sun May 25 2003.
// Copyright (c) 2003 Matt Gallagher. All rights reserved.
// ##############################################################
#import "Bracket.h"
#import "ExpressionSymbols.h"
//
// About Bracket
//
// A basic node which exists to wrap a sub-tree.
//
@implementation Bracket
- (instancetype)init
{
self = [super init];
if (self) {
}
return self;
}
//
// initWithParent
//
// Same as inherited except initialises "closed" to no.
//
- (instancetype)initWithParent:(Expression*)newParent andManager:(DataManager*)newManager
{
self = [super initWithParent:newParent andManager:newManager];
if (self)
{
closed = NO;
}
return self;
}
//
// initWithCoder
//
// Part of the NSCoder protocol. Required for copy and paste.
//
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
closed = [coder decodeBoolForKey:@"MEClosed"];
return self;
}
//
// encodeWithCoder
//
// Part of the NSCoder protocol. Required for copy and paste.
//
- (void)encodeWithCoder:(NSCoder *)coder
{
[super encodeWithCoder:coder];
[coder encodeBool:closed forKey:@"MEClosed"];
}
//
// binaryOpPressed
//
// Default behaviour if bracket closed, otherwise spilts the child first.
//
- (void)binaryOpPressed:(int)op
{
if (closed == YES)
[super binaryOpPressed:op];
else
// Split the child into a binary operation.
[self replaceChild:child withBinOp:op];
}
//
// closeBracketPressed
//
// Closes this node's sub-tree (and causes the right bracket to be drawn)
//
- (void)closeBracketPressed
{
if (closed == YES)
{
[super closeBracketPressed];
return;
}
closed = YES;
pathValidAt = -1;
[self inputPoint];
[self valueChanged];
}
//
// deleteDigit
//
// Same as inherited except opens the sub-tree if this node is closed.
//
- (void)deleteDigit
{
if (closed == YES)
{
closed = NO;
[self valueChanged];
}
else
{
[super deleteDigit];
}
}
//
// equalsPressed
//
// When equals is pressed we want to close all our bracketed groups (it looks prettier)
//
// This behaviour is the only reason for this function to exist.
//
- (void)equalsPressed
{
[super equalsPressed];
if (closed == NO)
{
closed = YES;
[self valueChanged];
}
}
//
// getExpressionString
//
// Ouputs this node as a string.
//
- (NSString*)getExpressionString
{
NSString *resultString = @"(";
if (child != nil)
resultString = [resultString stringByAppendingString:[child getExpressionString]];
if (closed == YES)
{
resultString = [resultString stringByAppendingString:@")"];
}
return resultString;
}
//
// pathAtLevel
//
// Drawing this node is pretty simple: it draws the child node and places a left bracket to
// the left of the child and a right bracket to the right if this node is closed.
//
- (NSBezierPath*)pathAtLevel:(int)level
{
NSBezierPath *copy;
if (pathValidAt != level)
{
NSBezierPath *rightBracket;
NSAffineTransform *transform;
double scale = [Expression scaleWithLevel:level];
NSRect boundsRect;
NSBezierPath *childPath = [NSBezierPath bezierPath];
double bracketHeight;
double bracketWidth;
double bracketBaseline;
expressionPath = [NSBezierPath bezierPath];
// Get the left bracket
[expressionPath appendBezierPath:[ExpressionSymbols leftBracketPath]];
transform = [NSAffineTransform transform];
[transform scaleBy:scale];
[expressionPath transformUsingAffineTransform:transform];
boundsRect = [expressionPath bounds];
bracketWidth = boundsRect.origin.x + boundsRect.size.width;
bracketHeight = boundsRect.size.height;
bracketBaseline = boundsRect.origin.y;
if (child != nil)
{
// Get the bracket contents
childPath = [child pathAtLevel:level];
// Transform the op to the right of the child's value
transform = [NSAffineTransform transform];
[transform translateXBy:bracketWidth yBy:0];
[childPath transformUsingAffineTransform:transform];
}
if (closed)
{
// Get the right bracket
rightBracket = [ExpressionSymbols rightBracketPath];
transform = [NSAffineTransform transform];
[transform scaleBy:scale];
[rightBracket transformUsingAffineTransform:transform];
transform = [NSAffineTransform transform];
if (child != nil)
{
// Position the right bracket after the bracket contents
boundsRect = [childPath bounds];
[transform
translateXBy:boundsRect.origin.x + boundsRect.size.width + (scale * 2.0) yBy:0
];
}
else
{
// Position the right bracket after the left bracket
transform = [NSAffineTransform transform];
[transform translateXBy:bracketWidth yBy:0];
}
// The left and right bracket now get joined together
[rightBracket transformUsingAffineTransform:transform];
[expressionPath appendBezierPath:rightBracket];
}
if (child != nil)
{
// Scale the brackets so that they are the same height as the value that they contain
boundsRect = [childPath bounds];
transform = [NSAffineTransform transform];
[transform
translateXBy:0.0
yBy:((boundsRect.origin.y < 0.0) ? boundsRect.origin.y : 0.0) - (3.0 * scale)
];
if ((boundsRect.size.height + (6.0 * scale)) / bracketHeight > 1.0)
[transform scaleXBy:1.0 yBy:(boundsRect.size.height + (6.0 * scale)) / bracketHeight];
[transform translateXBy:0.0 yBy:-bracketBaseline];
[expressionPath transformUsingAffineTransform:transform];
[expressionPath appendBezierPath:childPath];
}
if (![expressionPath isEmpty])
naturalBounds = [expressionPath bounds];
else
naturalBounds = NSMakeRect(0.0, 0.0, 0.0, 0.0);
if (![childPath isEmpty])
childNaturalBounds = [childPath bounds];
else
childNaturalBounds = NSMakeRect(0.0, 0.0, 0.0, 0.0);
pathValidAt = level;
}
copy = [NSBezierPath bezierPath];
[copy appendBezierPath:expressionPath];
return copy;
}
//
// postOpPressed
//
// Not certain what this is doing. I'm sure it has a point, I just can't remember it.
//
- (void)postOpPressed:(int)op
{
if (closed == YES)
{
[super postOpPressed:op];
}
else
{
// Create a postOp at the child node and set it as the input point
[self replaceChild:child withPostOp:op];
}
}
@end