Overhaul UI code to make it more flexible and less redrawing, little hierarchy framework
[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 class 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) {
19 colors[p.index] = color(h % 360, 100, b);
20 b = max(0, b - 10);
21 }
22 h += 70;
23 }
24 }
25 }
26
27 }
28
29 class 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
48 class 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
77 class TestStripPattern extends TestPattern {
78
79 SinLFO d = new SinLFO(4, 40, 4000);
80
81 public TestStripPattern(GLucose glucose) {
82 super(glucose);
83 addModulator(d).trigger();
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,
92 max(0, 100 - d.getValuef()*dist(p.x, p.y, s.cx, s.cy))
93 );
94 }
95 }
96 }
97 }
98
99 /**
100 * Simplest demonstration of using the rotating master hue.
101 * All pixels are full-on the same color.
102 */
103 class TestHuePattern extends TestPattern {
104 public TestHuePattern(GLucose glucose) {
105 super(glucose);
106 }
107
108 public void run(int deltaMs) {
109 // Access the core master hue via this method call
110 float hv = lx.getBaseHuef();
111 for (int i = 0; i < colors.length; ++i) {
112 colors[i] = color(hv, 100, 100);
113 }
114 }
115 }
116
117 /**
118 * Test of a wave moving across the X axis.
119 */
120 class TestXPattern extends TestPattern {
121 private final SinLFO xPos = new SinLFO(0, model.xMax, 4000);
122 public TestXPattern(GLucose glucose) {
123 super(glucose);
124 addModulator(xPos).trigger();
125 }
126 public void run(int deltaMs) {
127 float hv = lx.getBaseHuef();
128 for (Point p : model.points) {
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);
135 }
136 }
137 }
138
139 /**
140 * Test of a wave on the Y axis.
141 */
142 class TestYPattern extends TestPattern {
143 private final SinLFO yPos = new SinLFO(0, model.yMax, 4000);
144 public TestYPattern(GLucose glucose) {
145 super(glucose);
146 addModulator(yPos).trigger();
147 }
148 public void run(int deltaMs) {
149 float hv = lx.getBaseHuef();
150 for (Point p : model.points) {
151 float bv = max(0, 100 - abs(p.fy - yPos.getValuef()));
152 colors[p.index] = color(hv, 100, bv);
153 }
154 }
155 }
156
157 /**
158 * Test of a wave on the Z axis.
159 */
160 class TestZPattern extends TestPattern {
161 private final SinLFO zPos = new SinLFO(0, model.zMax, 4000);
162 public TestZPattern(GLucose glucose) {
163 super(glucose);
164 addModulator(zPos).trigger();
165 }
166 public void run(int deltaMs) {
167 float hv = lx.getBaseHuef();
168 for (Point p : model.points) {
169 float bv = max(0, 100 - abs(p.fz - zPos.getValuef()));
170 colors[p.index] = color(hv, 100, bv);
171 }
172 }
173 }
174
175 /**
176 * This shows how to iterate over towers, enumerated in the model.
177 */
178 class TestTowerPattern extends TestPattern {
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();
184 }
185
186 public void run(int deltaMs) {
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,
193 max(0, 100 - 80*LXUtils.wrapdistf(ti, towerIndex.getValuef(), model.towers.size()))
194 );
195 }
196 ++ti;
197 }
198 }
199
200 }
201
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 */
220 class TestProjectionPattern extends TestPattern {
221
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);
225
226 public TestProjectionPattern(GLucose glucose) {
227 super(glucose);
228 projection = new Projection(model);
229 addModulator(angle).trigger();
230 addModulator(yPos).trigger();
231 }
232
233 public void run(int deltaMs) {
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
237 projection.reset(model)
238
239 // Translate so the center of the car is the origin, offset by yPos
240 .translateCenter(model, 0, yPos.getValuef(), 0)
241
242 // Rotate around the origin (now the center of the car) about an X-vector
243 .rotate(angle.getValuef(), 1, 0, 0)
244
245 // Scale up the Y axis (objects will look smaller in that access)
246 .scale(1, 1.5, 1);
247
248 float hv = lx.getBaseHuef();
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
252 d = max(0, abs(c.y) - 10 + .1*abs(c.z) + .02*abs(c.x)); // plane / spear thing
253 colors[c.index] = color(
254 (hv + .6*abs(c.x) + abs(c.z)) % 360,
255 100,
256 constrain(140 - 40*d, 0, 100)
257 );
258 }
259 }
260 }
261
262 class TestCubePattern extends TestPattern {
263
264 private SawLFO index = new SawLFO(0, Cube.POINTS_PER_CUBE, Cube.POINTS_PER_CUBE*60);
265
266 TestCubePattern(GLucose glucose) {
267 super(glucose);
268 addModulator(index).start();
269 }
270
271 public void run(int deltaMs) {
272 for (Cube c : model.cubes) {
273 int i = 0;
274 for (Point p : c.points) {
275 colors[p.index] = color(
276 lx.getBaseHuef(),
277 100,
278 max(0, 100 - 80.*abs(i - index.getValuef()))
279 );
280 ++i;
281 }
282 }
283 }
284 }
285
286 class MappingTool extends TestPattern {
287
288 private int cubeIndex = 0;
289 private int stripIndex = 0;
290 private int channelIndex = 0;
291
292 public final int MAPPING_MODE_ALL = 0;
293 public final int MAPPING_MODE_CHANNEL = 1;
294 public final int MAPPING_MODE_SINGLE_CUBE = 2;
295 public int mappingMode = MAPPING_MODE_ALL;
296
297 public final int CUBE_MODE_ALL = 0;
298 public final int CUBE_MODE_SINGLE_STRIP = 1;
299 public final int CUBE_MODE_STRIP_PATTERN = 2;
300 public int cubeMode = CUBE_MODE_ALL;
301
302 public boolean channelModeRed = true;
303 public boolean channelModeGreen = false;
304 public boolean channelModeBlue = false;
305
306 private final int numChannels;
307
308 private final PandaMapping[] pandaMappings;
309 private PandaMapping activePanda;
310 private ChannelMapping activeChannel;
311
312 MappingTool(GLucose glucose, PandaMapping[] pandaMappings) {
313 super(glucose);
314 this.pandaMappings = pandaMappings;
315 numChannels = pandaMappings.length * PandaMapping.CHANNELS_PER_BOARD;
316 setChannel();
317 }
318
319 public int numChannels() {
320 return numChannels;
321 }
322
323 private void setChannel() {
324 activePanda = pandaMappings[channelIndex / PandaMapping.CHANNELS_PER_BOARD];
325 activeChannel = activePanda.channelList[channelIndex % PandaMapping.CHANNELS_PER_BOARD];
326 }
327
328 private int indexOfCubeInChannel(Cube c) {
329 if (activeChannel.mode == ChannelMapping.MODE_CUBES) {
330 int i = 1;
331 for (int index : activeChannel.objectIndices) {
332 if ((index >= 0) && (c == model.getCubeByRawIndex(index))) {
333 return i;
334 }
335 ++i;
336 }
337 }
338 return 0;
339 }
340
341 private void printInfo() {
342 println("Cube:" + cubeIndex + " Strip:" + (stripIndex+1));
343 }
344
345 public void cube(int delta) {
346 int len = model.cubes.size();
347 cubeIndex = (len + cubeIndex + delta) % len;
348 printInfo();
349 }
350
351 public void strip(int delta) {
352 int len = Cube.STRIPS_PER_CUBE;
353 stripIndex = (len + stripIndex + delta) % len;
354 printInfo();
355 }
356
357 public void run(int deltaMs) {
358 color off = color(0, 0, 0);
359 color c = off;
360 color r = #FF0000;
361 color g = #00FF00;
362 color b = #0000FF;
363 if (channelModeRed) c |= r;
364 if (channelModeGreen) c |= g;
365 if (channelModeBlue) c |= b;
366
367 int ci = 0;
368 for (Cube cube : model.cubes) {
369 boolean cubeOn = false;
370 int indexOfCubeInChannel = indexOfCubeInChannel(cube);
371 switch (mappingMode) {
372 case MAPPING_MODE_ALL: cubeOn = true; break;
373 case MAPPING_MODE_SINGLE_CUBE: cubeOn = (cubeIndex == ci); break;
374 case MAPPING_MODE_CHANNEL: cubeOn = (indexOfCubeInChannel > 0); break;
375 }
376 if (cubeOn) {
377 if (mappingMode == MAPPING_MODE_CHANNEL) {
378 color cc = off;
379 switch (indexOfCubeInChannel) {
380 case 1: cc = r; break;
381 case 2: cc = r|g; break;
382 case 3: cc = g; break;
383 case 4: cc = b; break;
384 case 5: cc = r|b; break;
385 }
386 setColor(cube, cc);
387 } else if (cubeMode == CUBE_MODE_STRIP_PATTERN) {
388 int si = 0;
389 color sc = off;
390 for (Strip strip : cube.strips) {
391 int faceI = si / Face.STRIPS_PER_FACE;
392 switch (faceI) {
393 case 0: sc = r; break;
394 case 1: sc = g; break;
395 case 2: sc = b; break;
396 case 3: sc = r|g|b; break;
397 }
398 if (si % Face.STRIPS_PER_FACE == 2) {
399 sc = r|g;
400 }
401 setColor(strip, sc);
402 ++si;
403 }
404 } else if (cubeMode == CUBE_MODE_SINGLE_STRIP) {
405 setColor(cube, off);
406 setColor(cube.strips.get(stripIndex), c);
407 } else {
408 setColor(cube, c);
409 }
410 } else {
411 setColor(cube, off);
412 }
413 ++ci;
414 }
415 }
416
417 public void setCube(int index) {
418 cubeIndex = index % model.cubes.size();
419 }
420
421 public void incCube() {
422 cubeIndex = (cubeIndex + 1) % model.cubes.size();
423 }
424
425 public void decCube() {
426 --cubeIndex;
427 if (cubeIndex < 0) {
428 cubeIndex += model.cubes.size();
429 }
430 }
431
432 public void setChannel(int index) {
433 channelIndex = index % numChannels;
434 setChannel();
435 }
436
437 public void incChannel() {
438 channelIndex = (channelIndex + 1) % numChannels;
439 setChannel();
440 }
441
442 public void decChannel() {
443 channelIndex = (channelIndex + numChannels - 1) % numChannels;
444 setChannel();
445 }
446
447 public void setStrip(int index) {
448 stripIndex = index % Cube.STRIPS_PER_CUBE;
449 }
450
451 public void incStrip() {
452 stripIndex = (stripIndex + 1) % Cube.STRIPS_PER_CUBE;
453 }
454
455 public void decStrip() {
456 stripIndex = (stripIndex + Cube.STRIPS_PER_CUBE - 1) % Cube.STRIPS_PER_CUBE;
457 }
458
459 public void keyPressed(UIMapping uiMapping) {
460 switch (keyCode) {
461 case UP: if (mappingMode == MAPPING_MODE_CHANNEL) incChannel(); else incCube(); break;
462 case DOWN: if (mappingMode == MAPPING_MODE_CHANNEL) decChannel(); else decCube(); break;
463 case LEFT: decStrip(); break;
464 case RIGHT: incStrip(); break;
465 }
466 switch (key) {
467 case 'r': channelModeRed = !channelModeRed; break;
468 case 'g': channelModeGreen = !channelModeGreen; break;
469 case 'b': channelModeBlue = !channelModeBlue; break;
470 }
471 uiMapping.setChannelID(channelIndex+1);
472 uiMapping.setCubeID(cubeIndex+1);
473 uiMapping.setStripID(stripIndex+1);
474 uiMapping.redraw();
475 }
476
477 }