-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathExpressionHelper.java
More file actions
279 lines (234 loc) · 9.29 KB
/
ExpressionHelper.java
File metadata and controls
279 lines (234 loc) · 9.29 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
package nodebox.node;
import nodebox.graphics.Color;
import nodebox.util.waves.*;
import java.util.List;
import java.util.Random;
/**
* Class containing static method used in Expression.
*
* @see nodebox.node.Expression
*/
public class ExpressionHelper {
// TODO: Expression system is not thread-safe.
public static ProcessingContext currentContext;
public static Parameter currentParameter;
public static Random randomGenerator = new Random();
public static double random(Object seed, double... minmax) {
switch (minmax.length) {
case 0:
return random(seed);
case 1:
return random(seed, minmax[0]);
default: // Anything larger than 2
return random(seed, minmax[0], minmax[1]);
}
}
public static double random(Object seed) {
if (seed instanceof Number) {
Number number = (Number) seed;
randomGenerator.setSeed(number.longValue() * 100000000);
} else {
randomGenerator.setSeed(seed.hashCode());
}
return randomGenerator.nextDouble();
}
public static double random(Object seed, double max) {
return random(seed) * max;
}
public static double random(Object seed, double min, double max) {
return min + random(seed) * (max - min);
}
public static int randint(Object seed, int min, int max) {
if (seed instanceof Number) {
Number number = (Number) seed;
randomGenerator.setSeed(number.longValue() * 100000000);
} else {
randomGenerator.setSeed(seed.hashCode());
}
// nextInt's specified value is exclusive, whereas we want to include it, so add 1.
return min + randomGenerator.nextInt(max - min + 1);
}
public static int toInt(double v) {
return (int) v;
}
public static double toFloat(int v) {
return (double) v;
}
public static double clamp(double v, double min, double max) {
return min > v ? min : max < v ? max : v;
}
public static Color color(double... values) {
switch (values.length) {
case 0:
return new Color();
case 1:
return new Color(values[0], values[0], values[0]);
case 2:
return new Color(values[0], values[0], values[0], values[1]);
case 3:
return new Color(values[0], values[1], values[2]);
case 4:
return new Color(values[0], values[1], values[2], values[3]);
default:
return new Color();
}
}
public static Color color(double gray) {
return new Color(gray, gray, gray);
}
public static Color color(double gray, double alpha) {
return new Color(gray, gray, gray, alpha);
}
public static Color color(double red, double green, double blue) {
return new Color(red, green, blue);
}
public static Color color(double red, double green, double blue, double alpha) {
return new Color(red, green, blue, alpha);
}
public static Color hsb(double... values) {
switch (values.length) {
case 0:
return new Color();
case 1:
return new Color(values[0], values[0], values[0]);
case 2:
return new Color(values[0], values[1]);
case 3:
return Color.fromHSB(values[0], values[1], values[2]);
case 4:
return Color.fromHSB(values[0], values[1], values[2], values[3]);
default:
return new Color();
}
}
public static Color hsb(double gray) {
return new Color(gray, gray, gray);
}
public static Color hsb(double gray, double alpha) {
return new Color(gray, gray, gray, alpha);
}
public static Color hsb(double hue, double saturation, double brightness) {
return Color.fromHSB(hue, saturation, brightness);
}
public static Color hsb(double hue, double saturation, double brightness, double alpha) {
return Color.fromHSB(hue, saturation, brightness, alpha);
}
public static double wave(AbstractWave.Type type, double... values) {
double frame = currentContext.getFrame();
switch (values.length) {
case 0:
return wave(type, 0, 1, 60, frame);
case 1:
return wave(type, values[0], 1, 60, frame);
case 2:
return wave(type, values[0], values[1], 60, frame);
case 3:
return wave(type, values[0], values[2], values[3], frame);
case 4:
return wave(type, values[0], values[2], values[3], values[4]);
default:
return wave(type, 0, 1, 60, frame);
}
}
public static double wave() {
return wave(AbstractWave.Type.SINE, 0, 1, 60, currentContext.getFrame());
}
public static double wave(AbstractWave.Type type) {
return wave(type, 0, 1, 60, currentContext.getFrame());
}
public static double wave(AbstractWave.Type type, double max) {
return wave(type, 0, max, 60, currentContext.getFrame());
}
public static double wave(AbstractWave.Type type, double min, double max) {
return wave(type, min, max, 60, currentContext.getFrame());
}
public static double wave(AbstractWave.Type type, double min, double max, double speed) {
return wave(type, min, max, speed, currentContext.getFrame());
}
public static double wave(AbstractWave.Type type, double min, double max, double speed, double frame) {
float fmin = (float) min;
float fmax = (float) max;
float fspeed = (float) speed;
AbstractWave wave;
switch (type) {
case TRIANGLE:
wave = TriangleWave.from(fmin, fmax, fspeed);
break;
case SQUARE:
wave = SquareWave.from(fmin, fmax, fspeed);
break;
case SAWTOOTH:
wave = SawtoothWave.from(fmin, fmax, fspeed);
break;
case SINE:
default:
wave = SineWave.from(fmin, fmax, fspeed);
break;
}
return wave.getValueAt((float) frame);
}
public static double hold(double startFrame, double functionValue, double... values) {
double frame = currentContext.getFrame();
switch (values.length) {
case 1:
return hold(startFrame, functionValue, values[0], frame);
case 2:
return hold(startFrame, functionValue, values[0], values[1]);
case 0:
default:
return hold(startFrame, functionValue, 0, frame);
}
}
public static double hold(double startFrame, double functionValue) {
return hold(startFrame, functionValue, 0, currentContext.getFrame());
}
public static double hold(double startFrame, double functionValue, double defaultValue) {
return hold(startFrame, functionValue, defaultValue, currentContext.getFrame());
}
public static double hold(double startFrame, double functionValue, double defaultValue, double frame) {
return frame < startFrame ? defaultValue : functionValue;
}
public static double schedule(double startFrame, double endFrame, double functionValue, double... values) {
double frame = currentContext.getFrame();
switch (values.length) {
case 1:
return schedule(startFrame, endFrame, functionValue, values[0], frame);
case 2:
return schedule(startFrame, endFrame, functionValue, values[0], values[1]);
case 0:
default:
return schedule(startFrame, endFrame, functionValue, 0, frame);
}
}
public static double schedule(double startFrame, double endFrame, double functionValue) {
return schedule(startFrame, endFrame, functionValue, 0, currentContext.getFrame());
}
public static double schedule(double startFrame, double endFrame, double functionValue, double defaultValue) {
return schedule(startFrame, endFrame, functionValue, defaultValue, currentContext.getFrame());
}
public static double schedule(double startFrame, double endFrame, double functionValue, double defaultValue, double frame) {
return startFrame <= frame && frame < endFrame ? functionValue : defaultValue;
}
public static double timeloop(double speed, List<Number> values) {
return timeloop(speed, values, currentContext.getFrame());
}
public static double timeloop(double speed, List<Number> values, double frame) {
if (values.size() == 0) return 0;
int index = (int) Math.floor(frame / speed);
index = index % values.size();
if (index < 0) {
index = values.size() + index;
}
try {
return values.get(index).doubleValue();
} catch (ClassCastException e) {
return 0;
}
}
public static Object stamp(String key, Object defaultValue) {
if (currentContext == null) return defaultValue;
currentParameter.markStampExpression();
Object v = currentContext.get(key);
return v != null ? v : defaultValue;
}
}