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