-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstant.m
More file actions
executable file
·222 lines (193 loc) · 5.41 KB
/
Copy pathConstant.m
File metadata and controls
executable file
·222 lines (193 loc) · 5.41 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
// ##############################################################
// Constant.m
// Magic Number Machine
//
// Created by Matt Gallagher on Wed May 14 2003.
// Copyright (c) 2003 Matt Gallagher. All rights reserved.
// ##############################################################
#import "Constant.h"
#import "ExpressionSymbols.h"
#import "BigCFloat.h"
#import "DataManager.h"
#import "OpEnumerations.h"
#import "PreOp.h"
//
// About Constant
//
// This class exists to handle the two mathematical constants that can be entered
// as though they are numbers: pi and i.
//
// Most of the behaviour in this class comes from the fact that a Constant cannot
// have a child node, so a new node must be created every time one is required.
//
// Since this class handles a lot of the behaviour for numbers entered into an
// expression, it also forms the basis for Value (the user entered value class).
//
// Modified: 25 May 2015 - Mike: Now support all constant symbols defined in the
// constants drawer.
@implementation Constant
//
// initWithParent
//
// Initialises this class with the constant set to either pi or i
//
- (instancetype)initWithParent:(Expression*)newParent manager:(DataManager*)newManager andConstant:(int)newConstant
{
self = [super initWithParent:newParent andManager:newManager];
if (self)
{
constant = newConstant;
value = [ExpressionSymbols getValueForConstant:constant];
negative = NO;
}
return self;
}
//
// initWithCoder
//
// Required for the NSCoder protocol, which is used by copy and paste.
//
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
constant = [coder decodeIntForKey:@"MEConstant"];
value = [coder decodeObjectForKey:@"MEValue"];
return self;
}
//
// encodeWithCoder
//
// Required for the NSCoder protocol, which is used by copy and paste.
//
- (void)encodeWithCoder:(NSCoder *)coder
{
[super encodeWithCoder:coder];
[coder encodeInt:(int)constant forKey:@"MEConstant"];
[coder encodeObject:value forKey:@"MEValue"];
}
//
// appendDigit
//
// Since this node can't be edited, we need a new node that can be.
//
- (void)appendDigit:(int)digit
{
if (digit == '-')
{
BigCFloat *minusOne = [BigCFloat bigFloatWithInt:-1 radix:[manager getRadix]];
[value multiplyBy:minusOne];
negative = !negative;
[self valueChanged];
return;
}
// Behave as though we already have a child and spawn off another node
[self binaryOpPressed:'.'];
[[manager getInputPoint] appendDigit:digit];
}
//
// bracketPressed
//
// Since this node can't be edited, we need a new node that can be.
//
- (void)bracketPressed
{
// Behave as though we already have a child and spawn off another node
[self binaryOpPressed:'.'];
[[manager getInputPoint] bracketPressed];
}
//
// constantPressed
//
// Since this node can't be edited, we need a new node that can be.
//
- (void)constantPressed:(enum ConstType)newConstant
{
// Behave as though we already have a child and spawn off another node
[self binaryOpPressed:'.'];
[[manager getInputPoint] constantPressed:(int)newConstant];
}
//
// expressionInserted
//
// Since this node can't be edited, we need a new node that can be.
//
- (void)expressionInserted:(Expression*)newExpression
{
// Behave as though we already have a child and spawn off another node
[self binaryOpPressed:'.'];
[[manager getInputPoint] expressionInserted:newExpression];
}
//
// getExpressionString
//
// Creates an output string for this node.
//
- (NSString*)getExpressionString
{
NSString *resultString = negative ? @"-" : @"";
resultString = [resultString stringByAppendingString:[ExpressionSymbols getNameForConstant:constant]];
return resultString;
}
//
// pathAtLevel
//
// Creates a path containing either π or i
//
- (NSBezierPath*)pathAtLevel:(int)level
{
NSBezierPath *copy;
if (pathValidAt != level)
{
expressionPath = [NSBezierPath bezierPath];
[expressionPath appendBezierPath:[ExpressionSymbols makeSymbolForConstant:constant]];
if (negative)
{
NSBezierPath *minusPath = [NSBezierPath bezierPath];
NSAffineTransform *signTransform = [NSAffineTransform transform];
NSRect boundsRect;
[minusPath appendBezierPath:[ExpressionSymbols minusPath]];
boundsRect = [minusPath bounds];
[signTransform translateXBy:boundsRect.origin.x + boundsRect.size.width + 4.0 yBy:0];
[expressionPath transformUsingAffineTransform:signTransform];
[expressionPath appendBezierPath:minusPath];
}
if (level >= 2)
{
NSAffineTransform *transform = [NSAffineTransform transform];
double scale = [Expression scaleWithLevel:level];
[transform scaleBy:scale];
[expressionPath transformUsingAffineTransform:transform];
}
if (![expressionPath isEmpty])
naturalBounds = [expressionPath bounds];
else
naturalBounds = NSMakeRect(0.0, 0.0, 0.0, 0.0);
pathValidAt = level;
}
copy = [NSBezierPath bezierPath];
[copy appendBezierPath:expressionPath];
return copy;
}
//
// preOpPressed
//
// Since this node can't be edited, we need a new node that can be.
//
- (void)preOpPressed:(int)newOp
{
// Can't change the child so spawn another factor
[self binaryOpPressed:'.'];
[[manager getInputPoint] preOpPressed:newOp];
}
//
// valueInserted
//
// Since this node can't be edited, we need a new node that can be.
//
- (void)valueInserted:(BigCFloat*)newValue
{
// Behave as though we already have a child (ie can't accept a new child)
[self binaryOpPressed:'.'];
[[manager getInputPoint] valueInserted:newValue];
}
@end