Merge branch 'integration'
[SugarCubes.git] / TestPatterns.pde
1 abstract class TestPattern extends SCPattern {
2 public TestPattern(GLucose glucose) {
3 super(glucose);
4 setEligible(false);
5 }
6 }
7
8 /**
9 * Simplest demonstration of using the rotating master hue.
10 * All pixels are full-on the same color.
11 */
12 class TestHuePattern extends TestPattern {
13 public TestHuePattern(GLucose glucose) {
14 super(glucose);
15 }
16
17 public void run(int deltaMs) {
18 // Access the core master hue via this method call
19 float hv = lx.getBaseHuef();
20 for (int i = 0; i < colors.length; ++i) {
21 colors[i] = color(hv, 100, 100);
22 }
23 }
24 }
25
26 /**
27 * Test of a wave moving across the X axis.
28 */
29 class TestXPattern extends TestPattern {
30 private final SinLFO xPos = new SinLFO(0, model.xMax, 4000);
31 public TestXPattern(GLucose glucose) {
32 super(glucose);
33 addModulator(xPos).trigger();
34 }
35 public void run(int deltaMs) {
36 float hv = lx.getBaseHuef();
37 for (Point p : model.points) {
38 // This is a common technique for modulating brightness.
39 // You can use abs() to determine the distance between two
40 // values. The further away this point is from an exact
41 // point, the more we decrease its brightness
42 float bv = max(0, 100 - abs(p.fx - xPos.getValuef()));
43 colors[p.index] = color(hv, 100, bv);
44 }
45 }
46 }
47
48 /**
49 * Test of a wave on the Y axis.
50 */
51 class TestYPattern extends TestPattern {
52 private final SinLFO yPos = new SinLFO(0, model.yMax, 4000);
53 public TestYPattern(GLucose glucose) {
54 super(glucose);
55 addModulator(yPos).trigger();
56 }
57 public void run(int deltaMs) {
58 float hv = lx.getBaseHuef();
59 for (Point p : model.points) {
60 float bv = max(0, 100 - abs(p.fy - yPos.getValuef()));
61 colors[p.index] = color(hv, 100, bv);
62 }
63 }
64 }
65
66 /**
67 * Test of a wave on the Z axis.
68 */
69 class TestZPattern extends TestPattern {
70 private final SinLFO zPos = new SinLFO(0, model.zMax, 4000);
71 public TestZPattern(GLucose glucose) {
72 super(glucose);
73 addModulator(zPos).trigger();
74 }
75 public void run(int deltaMs) {
76 float hv = lx.getBaseHuef();
77 for (Point p : model.points) {
78 float bv = max(0, 100 - abs(p.fz - zPos.getValuef()));
79 colors[p.index] = color(hv, 100, bv);
80 }
81 }
82 }
83
84 /**
85 * This shows how to iterate over towers, enumerated in the model.
86 */
87 class TestTowerPattern extends TestPattern {
88 private final SawLFO towerIndex = new SawLFO(0, model.towers.size(), 1000*model.towers.size());
89
90 public TestTowerPattern(GLucose glucose) {
91 super(glucose);
92 addModulator(towerIndex).trigger();
93 }
94
95 public void run(int deltaMs) {
96 int ti = 0;
97 for (Tower t : model.towers) {
98 for (Point p : t.points) {
99 colors[p.index] = color(
100 lx.getBaseHuef(),
101 100,
102 max(0, 100 - 80*LXUtils.wrapdistf(ti, towerIndex.getValuef(), model.towers.size()))
103 );
104 }
105 ++ti;
106 }
107 }
108
109 }
110
111 /**
112 * This is a demonstration of how to use the projection library. A projection
113 * creates a mutation of the coordinates of all the points in the model, creating
114 * virtual x,y,z coordinates. In effect, this is like virtually rotating the entire
115 * art car. However, since in reality the car does not move, the result is that
116 * it appears that the object we are drawing on the car is actually moving.
117 *
118 * Keep in mind that what we are creating a projection of is the view coordinates.
119 * Depending on your intuition, some operations may feel backwards. For instance,
120 * if you translate the view to the right, it will make it seem that the object
121 * you are drawing has moved to the left. If you scale the view up 2x, objects
122 * drawn with the same absolute values will seem to be half the size.
123 *
124 * If this feels counterintuitive at first, don't worry. Just remember that you
125 * are moving the pixels, not the structure. We're dealing with a finite set
126 * of sparse, non-uniformly spaced pixels. Mutating the structure would move
127 * things to a space where there are no pixels in 99% of the cases.
128 */
129 class TestProjectionPattern extends TestPattern {
130
131 private final Projection projection;
132 private final SawLFO angle = new SawLFO(0, TWO_PI, 9000);
133 private final SinLFO yPos = new SinLFO(-20, 40, 5000);
134
135 public TestProjectionPattern(GLucose glucose) {
136 super(glucose);
137 projection = new Projection(model);
138 addModulator(angle).trigger();
139 addModulator(yPos).trigger();
140 }
141
142 public void run(int deltaMs) {
143 // For the same reasons described above, it may logically feel to you that
144 // some of these operations are in reverse order. Again, just keep in mind that
145 // the car itself is what's moving, not the object
146 projection.reset(model)
147
148 // Translate so the center of the car is the origin, offset by yPos
149 .translateCenter(model, 0, yPos.getValuef(), 0)
150
151 // Rotate around the origin (now the center of the car) about an X-vector
152 .rotate(angle.getValuef(), 1, 0, 0)
153
154 // Scale up the Y axis (objects will look smaller in that access)
155 .scale(1, 1.5, 1);
156
157 float hv = lx.getBaseHuef();
158 for (Coord c : projection) {
159 float d = sqrt(c.x*c.x + c.y*c.y + c.z*c.z); // distance from origin
160 // d = abs(d-60) + max(0, abs(c.z) - 20); // life saver / ring thing
161 d = max(0, abs(c.y) - 10 + .3*abs(c.z) + .08*abs(c.x)); // plane / spear thing
162 colors[c.index] = color(
163 (hv + .6*abs(c.x) + abs(c.z)) % 360,
164 100,
165 constrain(140 - 10*d, 0, 100)
166 );
167 }
168 }
169 }
170
171 class TestCubePattern extends TestPattern {
172
173 private SawLFO index = new SawLFO(0, Cube.POINTS_PER_CUBE, Cube.POINTS_PER_CUBE*60);
174
175 TestCubePattern(GLucose glucose) {
176 super(glucose);
177 addModulator(index).start();
178 }
179
180 public void run(int deltaMs) {
181 for (Cube c : model.cubes) {
182 int i = 0;
183 for (Point p : c.points) {
184 colors[p.index] = color(
185 lx.getBaseHuef(),
186 100,
187 max(0, 100 - 80.*abs(i - index.getValuef()))
188 );
189 ++i;
190 }
191 }
192 }
193 }
194
195 class MappingTool extends TestPattern {
196
197 private int cubeIndex = 0;
198 private int stripIndex = 0;
199 private int channelIndex = 0;
200
201 public final int MAPPING_MODE_ALL = 0;
202 public final int MAPPING_MODE_CHANNEL = 1;
203 public final int MAPPING_MODE_SINGLE_CUBE = 2;
204 public int mappingMode = MAPPING_MODE_ALL;
205
206 public final int CUBE_MODE_ALL = 0;
207 public final int CUBE_MODE_SINGLE_STRIP = 1;
208 public final int CUBE_MODE_STRIP_PATTERN = 2;
209 public int cubeMode = CUBE_MODE_ALL;
210
211 public boolean channelModeRed = true;
212 public boolean channelModeGreen = false;
213 public boolean channelModeBlue = false;
214
215 private final int numChannels;
216
217 private final PandaMapping[] pandaMappings;
218 private PandaMapping activeMapping;
219 private int mappingChannelIndex;
220
221 MappingTool(GLucose glucose, PandaMapping[] pandaMappings) {
222 super(glucose);
223 this.pandaMappings = pandaMappings;
224 numChannels = pandaMappings.length * PandaMapping.CHANNELS_PER_BOARD;
225 setChannel();
226 }
227
228 private void setChannel() {
229 mappingChannelIndex = channelIndex % PandaMapping.CHANNELS_PER_BOARD;
230 activeMapping = pandaMappings[channelIndex / PandaMapping.CHANNELS_PER_BOARD];
231 }
232
233 private int cubeInChannel(Cube c) {
234 int i = 1;
235 for (int index : activeMapping.channelList[mappingChannelIndex]) {
236 if (c == model.getCubeByRawIndex(index)) {
237 return i;
238 }
239 ++i;
240 }
241 return 0;
242 }
243
244 private void printInfo() {
245 println("Cube:" + cubeIndex + " Strip:" + (stripIndex+1));
246 }
247
248 public void cube(int delta) {
249 int len = model.cubes.size();
250 cubeIndex = (len + cubeIndex + delta) % len;
251 printInfo();
252 }
253
254 public void strip(int delta) {
255 int len = Cube.STRIPS_PER_CUBE;
256 stripIndex = (len + stripIndex + delta) % len;
257 printInfo();
258 }
259
260 public void run(int deltaMs) {
261 color off = color(0, 0, 0);
262 color c = off;
263 color r = #FF0000;
264 color g = #00FF00;
265 color b = #0000FF;
266 if (channelModeRed) c |= r;
267 if (channelModeGreen) c |= g;
268 if (channelModeBlue) c |= b;
269
270 int ci = 0;
271 for (Cube cube : model.cubes) {
272 boolean cubeOn = false;
273 int channelIndex = cubeInChannel(cube);
274 switch (mappingMode) {
275 case MAPPING_MODE_ALL: cubeOn = true; break;
276 case MAPPING_MODE_SINGLE_CUBE: cubeOn = (cubeIndex == ci); break;
277 case MAPPING_MODE_CHANNEL: cubeOn = (channelIndex > 0); break;
278 }
279 if (cubeOn) {
280 if (mappingMode == MAPPING_MODE_CHANNEL) {
281 color cc = off;
282 switch (channelIndex) {
283 case 1: cc = r; break;
284 case 2: cc = r|g; break;
285 case 3: cc = g; break;
286 case 4: cc = b; break;
287 case 5: cc = r|b; break;
288 }
289 setColor(cube, cc);
290 } else if (cubeMode == CUBE_MODE_STRIP_PATTERN) {
291 int si = 0;
292 color sc = off;
293 for (Strip strip : cube.strips) {
294 int faceI = si / Face.STRIPS_PER_FACE;
295 switch (faceI) {
296 case 0: sc = r; break;
297 case 1: sc = g; break;
298 case 2: sc = b; break;
299 case 3: sc = r|g|b; break;
300 }
301 if (si % Face.STRIPS_PER_FACE == 2) {
302 sc = r|g;
303 }
304 setColor(strip, sc);
305 ++si;
306 }
307 } else if (cubeMode == CUBE_MODE_SINGLE_STRIP) {
308 setColor(cube, off);
309 setColor(cube.strips.get(stripIndex), c);
310 } else {
311 setColor(cube, c);
312 }
313 } else {
314 setColor(cube, off);
315 }
316 ++ci;
317 }
318
319 }
320
321 public void incCube() {
322 cubeIndex = (cubeIndex + 1) % model.cubes.size();
323 }
324
325 public void decCube() {
326 --cubeIndex;
327 if (cubeIndex < 0) {
328 cubeIndex += model.cubes.size();
329 }
330 }
331
332 public void incChannel() {
333 channelIndex = (channelIndex + 1) % numChannels;
334 setChannel();
335 }
336
337 public void decChannel() {
338 --channelIndex;
339 if (channelIndex < 0) {
340 channelIndex += numChannels;
341 }
342 setChannel();
343 }
344
345 public void incStrip() {
346 int stripsPerCube = Cube.FACES_PER_CUBE * Face.STRIPS_PER_FACE;
347 stripIndex = (stripIndex + 1) % stripsPerCube;
348 }
349
350 public void decStrip() {
351 int stripsPerCube = Cube.FACES_PER_CUBE * Face.STRIPS_PER_FACE;
352 --stripIndex;
353 if (stripIndex < 0) {
354 stripIndex += stripsPerCube;
355 }
356 }
357
358 public void keyPressed() {
359 switch (keyCode) {
360 case UP: if (mappingMode == MAPPING_MODE_CHANNEL) incChannel(); else incCube(); break;
361 case DOWN: if (mappingMode == MAPPING_MODE_CHANNEL) decChannel(); else decCube(); break;
362 case LEFT: decStrip(); break;
363 case RIGHT: incStrip(); break;
364 }
365 switch (key) {
366 case 'r': channelModeRed = !channelModeRed; break;
367 case 'g': channelModeGreen = !channelModeGreen; break;
368 case 'b': channelModeBlue = !channelModeBlue; break;
369 }
370 }
371 }