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