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