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