Apat and added acos to spherycolor, not included in color yet but working
[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;
2bb56822 28 for (LXPoint 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;
2bb56822 49 for (LXPoint 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;
2bb56822 68 for (LXPoint 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;
2bb56822 78 for (LXPoint 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
254fbb68 87class TestStripPattern extends TestPattern {
e105ef7c
MS
88
89 SinLFO d = new SinLFO(4, 40, 4000);
90
254fbb68
MS
91 public TestStripPattern(GLucose glucose) {
92 super(glucose);
e105ef7c 93 addModulator(d).trigger();
254fbb68
MS
94 }
95
34327c96 96 public void run(double deltaMs) {
254fbb68 97 for (Strip s : model.strips) {
2bb56822 98 for (LXPoint p : s.points) {
19d16a16 99 colors[p.index] = lx.hsb(
254fbb68
MS
100 lx.getBaseHuef(),
101 100,
e105ef7c 102 max(0, 100 - d.getValuef()*dist(p.x, p.y, s.cx, s.cy))
254fbb68
MS
103 );
104 }
105 }
106 }
107}
108
7ef61b89
MS
109/**
110 * Simplest demonstration of using the rotating master hue.
111 * All pixels are full-on the same color.
112 */
6c1a9e53 113class TestHuePattern extends TestPattern {
49815cc0
MS
114 public TestHuePattern(GLucose glucose) {
115 super(glucose);
116 }
7ef61b89 117
34327c96 118 public void run(double deltaMs) {
7ef61b89
MS
119 // Access the core master hue via this method call
120 float hv = lx.getBaseHuef();
49815cc0 121 for (int i = 0; i < colors.length; ++i) {
19d16a16 122 colors[i] = lx.hsb(hv, 100, 100);
49815cc0
MS
123 }
124 }
125}
126
7ef61b89
MS
127/**
128 * Test of a wave moving across the X axis.
129 */
6c1a9e53 130class TestXPattern extends TestPattern {
7ef61b89 131 private final SinLFO xPos = new SinLFO(0, model.xMax, 4000);
49815cc0
MS
132 public TestXPattern(GLucose glucose) {
133 super(glucose);
134 addModulator(xPos).trigger();
135 }
34327c96 136 public void run(double deltaMs) {
7ef61b89 137 float hv = lx.getBaseHuef();
2bb56822 138 for (LXPoint p : model.points) {
7ef61b89
MS
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
190d91c2 143 float bv = max(0, 100 - abs(p.x - xPos.getValuef()));
19d16a16 144 colors[p.index] = lx.hsb(hv, 100, bv);
49815cc0
MS
145 }
146 }
147}
148
7ef61b89
MS
149/**
150 * Test of a wave on the Y axis.
151 */
6c1a9e53 152class TestYPattern extends TestPattern {
7ef61b89 153 private final SinLFO yPos = new SinLFO(0, model.yMax, 4000);
49815cc0
MS
154 public TestYPattern(GLucose glucose) {
155 super(glucose);
156 addModulator(yPos).trigger();
157 }
34327c96 158 public void run(double deltaMs) {
7ef61b89 159 float hv = lx.getBaseHuef();
2bb56822 160 for (LXPoint p : model.points) {
190d91c2 161 float bv = max(0, 100 - abs(p.y - yPos.getValuef()));
19d16a16 162 colors[p.index] = lx.hsb(hv, 100, bv);
49815cc0
MS
163 }
164 }
165}
166
7ef61b89
MS
167/**
168 * Test of a wave on the Z axis.
169 */
6c1a9e53 170class TestZPattern extends TestPattern {
7ef61b89 171 private final SinLFO zPos = new SinLFO(0, model.zMax, 4000);
49815cc0
MS
172 public TestZPattern(GLucose glucose) {
173 super(glucose);
174 addModulator(zPos).trigger();
175 }
34327c96 176 public void run(double deltaMs) {
7ef61b89 177 float hv = lx.getBaseHuef();
2bb56822 178 for (LXPoint p : model.points) {
190d91c2 179 float bv = max(0, 100 - abs(p.z - zPos.getValuef()));
19d16a16 180 colors[p.index] = lx.hsb(hv, 100, bv);
49815cc0
MS
181 }
182 }
183}
f3f5a876 184
186bc4d3
MS
185/**
186 * This shows how to iterate over towers, enumerated in the model.
187 */
6c1a9e53 188class TestTowerPattern extends TestPattern {
186bc4d3
MS
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();
186bc4d3
MS
194 }
195
34327c96 196 public void run(double deltaMs) {
186bc4d3
MS
197 int ti = 0;
198 for (Tower t : model.towers) {
2bb56822 199 for (LXPoint p : t.points) {
19d16a16 200 colors[p.index] = lx.hsb(
186bc4d3
MS
201 lx.getBaseHuef(),
202 100,
92341dd9 203 max(0, 100 - 80*LXUtils.wrapdistf(ti, towerIndex.getValuef(), model.towers.size()))
186bc4d3
MS
204 );
205 }
206 ++ti;
207 }
208 }
209
210}
211
7ef61b89
MS
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 */
6c1a9e53 230class TestProjectionPattern extends TestPattern {
f3f5a876 231
9fa29818 232 private final LXProjection projection;
7ef61b89
MS
233 private final SawLFO angle = new SawLFO(0, TWO_PI, 9000);
234 private final SinLFO yPos = new SinLFO(-20, 40, 5000);
f3f5a876 235
7ef61b89 236 public TestProjectionPattern(GLucose glucose) {
f3f5a876 237 super(glucose);
9fa29818 238 projection = new LXProjection(model);
f3f5a876
MS
239 addModulator(angle).trigger();
240 addModulator(yPos).trigger();
241 }
242
34327c96 243 public void run(double deltaMs) {
7ef61b89
MS
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
2bb56822 247 projection.reset()
7ef61b89
MS
248
249 // Translate so the center of the car is the origin, offset by yPos
2bb56822 250 .translateCenter(0, yPos.getValuef(), 0)
7ef61b89
MS
251
252 // Rotate around the origin (now the center of the car) about an X-vector
f3f5a876 253 .rotate(angle.getValuef(), 1, 0, 0)
7ef61b89
MS
254
255 // Scale up the Y axis (objects will look smaller in that access)
f3f5a876
MS
256 .scale(1, 1.5, 1);
257
7ef61b89 258 float hv = lx.getBaseHuef();
9fa29818 259 for (LXVector c : projection) {
f3f5a876
MS
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
e105ef7c 262 d = max(0, abs(c.y) - 10 + .1*abs(c.z) + .02*abs(c.x)); // plane / spear thing
19d16a16 263 colors[c.index] = lx.hsb(
7ef61b89 264 (hv + .6*abs(c.x) + abs(c.z)) % 360,
f3f5a876 265 100,
e105ef7c 266 constrain(140 - 40*d, 0, 100)
f3f5a876
MS
267 );
268 }
269 }
270}
bf551144 271
6c1a9e53 272class TestCubePattern extends TestPattern {
254d34c0 273
cdb88d5d 274 private SawLFO index = new SawLFO(0, Cube.POINTS_PER_CUBE, Cube.POINTS_PER_CUBE*60);
254d34c0
MS
275
276 TestCubePattern(GLucose glucose) {
277 super(glucose);
278 addModulator(index).start();
279 }
280
34327c96 281 public void run(double deltaMs) {
254d34c0
MS
282 for (Cube c : model.cubes) {
283 int i = 0;
2bb56822 284 for (LXPoint p : c.points) {
19d16a16 285 colors[p.index] = lx.hsb(
254d34c0
MS
286 lx.getBaseHuef(),
287 100,
288 max(0, 100 - 80.*abs(i - index.getValuef()))
289 );
290 ++i;
291 }
292 }
293 }
294}
295
6c1a9e53 296class MappingTool extends TestPattern {
bf551144
MS
297
298 private int cubeIndex = 0;
299 private int stripIndex = 0;
2bae07c9 300 private int channelIndex = 0;
bf551144 301
2bae07c9
MS
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;
bf551144
MS
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
45f43cc2 316 private final int numChannels;
2bae07c9 317
45f43cc2 318 private final PandaMapping[] pandaMappings;
84086fa3
MS
319 private PandaMapping activePanda;
320 private ChannelMapping activeChannel;
2bae07c9 321
45f43cc2 322 MappingTool(GLucose glucose, PandaMapping[] pandaMappings) {
bf551144 323 super(glucose);
45f43cc2 324 this.pandaMappings = pandaMappings;
1685dc84 325 numChannels = pandaMappings.length * PandaMapping.CHANNELS_PER_BOARD;
2bae07c9
MS
326 setChannel();
327 }
d626bc9b
MS
328
329 public int numChannels() {
330 return numChannels;
331 }
2bae07c9
MS
332
333 private void setChannel() {
84086fa3
MS
334 activePanda = pandaMappings[channelIndex / PandaMapping.CHANNELS_PER_BOARD];
335 activeChannel = activePanda.channelList[channelIndex % PandaMapping.CHANNELS_PER_BOARD];
2bae07c9
MS
336 }
337
84086fa3
MS
338 private int indexOfCubeInChannel(Cube c) {
339 if (activeChannel.mode == ChannelMapping.MODE_CUBES) {
340 int i = 1;
341 for (int index : activeChannel.objectIndices) {
9f823de4 342 if ((index >= 0) && (c == model.getCubeByRawIndex(index))) {
84086fa3
MS
343 return i;
344 }
345 ++i;
2bae07c9 346 }
2bae07c9
MS
347 }
348 return 0;
bf551144
MS
349 }
350
351 private void printInfo() {
352 println("Cube:" + cubeIndex + " Strip:" + (stripIndex+1));
353 }
354
355 public void cube(int delta) {
356 int len = model.cubes.size();
357 cubeIndex = (len + cubeIndex + delta) % len;
358 printInfo();
359 }
360
361 public void strip(int delta) {
45f43cc2 362 int len = Cube.STRIPS_PER_CUBE;
bf551144
MS
363 stripIndex = (len + stripIndex + delta) % len;
364 printInfo();
365 }
366
34327c96 367 public void run(double deltaMs) {
19d16a16 368 color off = #000000;
bf551144
MS
369 color c = off;
370 color r = #FF0000;
371 color g = #00FF00;
372 color b = #0000FF;
373 if (channelModeRed) c |= r;
374 if (channelModeGreen) c |= g;
375 if (channelModeBlue) c |= b;
376
377 int ci = 0;
378 for (Cube cube : model.cubes) {
2bae07c9 379 boolean cubeOn = false;
84086fa3 380 int indexOfCubeInChannel = indexOfCubeInChannel(cube);
2bae07c9
MS
381 switch (mappingMode) {
382 case MAPPING_MODE_ALL: cubeOn = true; break;
383 case MAPPING_MODE_SINGLE_CUBE: cubeOn = (cubeIndex == ci); break;
d626bc9b 384 case MAPPING_MODE_CHANNEL: cubeOn = (indexOfCubeInChannel > 0); break;
2bae07c9
MS
385 }
386 if (cubeOn) {
387 if (mappingMode == MAPPING_MODE_CHANNEL) {
388 color cc = off;
84086fa3 389 switch (indexOfCubeInChannel) {
2bae07c9
MS
390 case 1: cc = r; break;
391 case 2: cc = r|g; break;
392 case 3: cc = g; break;
393 case 4: cc = b; break;
394 case 5: cc = r|b; break;
395 }
396 setColor(cube, cc);
397 } else if (cubeMode == CUBE_MODE_STRIP_PATTERN) {
bf551144
MS
398 int si = 0;
399 color sc = off;
400 for (Strip strip : cube.strips) {
a797d019
MS
401 int faceI = si / Face.STRIPS_PER_FACE;
402 switch (faceI) {
bf551144
MS
403 case 0: sc = r; break;
404 case 1: sc = g; break;
405 case 2: sc = b; break;
406 case 3: sc = r|g|b; break;
407 }
a797d019 408 if (si % Face.STRIPS_PER_FACE == 2) {
bf551144
MS
409 sc = r|g;
410 }
411 setColor(strip, sc);
412 ++si;
413 }
414 } else if (cubeMode == CUBE_MODE_SINGLE_STRIP) {
415 setColor(cube, off);
416 setColor(cube.strips.get(stripIndex), c);
417 } else {
418 setColor(cube, c);
419 }
420 } else {
421 setColor(cube, off);
422 }
423 ++ci;
424 }
d626bc9b
MS
425 }
426
427 public void setCube(int index) {
428 cubeIndex = index % model.cubes.size();
bf551144
MS
429 }
430
431 public void incCube() {
432 cubeIndex = (cubeIndex + 1) % model.cubes.size();
433 }
434
435 public void decCube() {
436 --cubeIndex;
437 if (cubeIndex < 0) {
438 cubeIndex += model.cubes.size();
439 }
440 }
d626bc9b
MS
441
442 public void setChannel(int index) {
443 channelIndex = index % numChannels;
444 setChannel();
445 }
2bae07c9
MS
446
447 public void incChannel() {
45f43cc2 448 channelIndex = (channelIndex + 1) % numChannels;
2bae07c9
MS
449 setChannel();
450 }
451
452 public void decChannel() {
9f823de4 453 channelIndex = (channelIndex + numChannels - 1) % numChannels;
2bae07c9
MS
454 setChannel();
455 }
bf551144 456
d626bc9b
MS
457 public void setStrip(int index) {
458 stripIndex = index % Cube.STRIPS_PER_CUBE;
459 }
460
bf551144 461 public void incStrip() {
92c06c97 462 stripIndex = (stripIndex + 1) % Cube.STRIPS_PER_CUBE;
bf551144
MS
463 }
464
465 public void decStrip() {
9f823de4 466 stripIndex = (stripIndex + Cube.STRIPS_PER_CUBE - 1) % Cube.STRIPS_PER_CUBE;
bf551144
MS
467 }
468
d626bc9b 469 public void keyPressed(UIMapping uiMapping) {
bf551144 470 switch (keyCode) {
2bae07c9
MS
471 case UP: if (mappingMode == MAPPING_MODE_CHANNEL) incChannel(); else incCube(); break;
472 case DOWN: if (mappingMode == MAPPING_MODE_CHANNEL) decChannel(); else decCube(); break;
bf551144
MS
473 case LEFT: decStrip(); break;
474 case RIGHT: incStrip(); break;
475 }
476 switch (key) {
477 case 'r': channelModeRed = !channelModeRed; break;
478 case 'g': channelModeGreen = !channelModeGreen; break;
479 case 'b': channelModeBlue = !channelModeBlue; break;
480 }
d626bc9b
MS
481 uiMapping.setChannelID(channelIndex+1);
482 uiMapping.setCubeID(cubeIndex+1);
483 uiMapping.setStripID(stripIndex+1);
484 uiMapping.redraw();
bf551144 485 }
d626bc9b 486
bf551144 487}