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