Merge branch 'master' of https://github.com/sugarcubes/SugarCubes into realmapping
[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 activePanda;
241 private ChannelMapping activeChannel;
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 activePanda = pandaMappings[channelIndex / PandaMapping.CHANNELS_PER_BOARD];
252 activeChannel = activePanda.channelList[channelIndex % PandaMapping.CHANNELS_PER_BOARD];
253 }
254
255 private int indexOfCubeInChannel(Cube c) {
256 if (activeChannel.mode == ChannelMapping.MODE_CUBES) {
257 int i = 1;
258 for (int index : activeChannel.objectIndices) {
259 if ((index >= 0) && (c == model.getCubeByRawIndex(index))) {
260 return i;
261 }
262 ++i;
263 }
264 }
265 return 0;
266 }
267
268 private void printInfo() {
269 println("Cube:" + cubeIndex + " Strip:" + (stripIndex+1));
270 }
271
272 public void cube(int delta) {
273 int len = model.cubes.size();
274 cubeIndex = (len + cubeIndex + delta) % len;
275 printInfo();
276 }
277
278 public void strip(int delta) {
279 int len = Cube.STRIPS_PER_CUBE;
280 stripIndex = (len + stripIndex + delta) % len;
281 printInfo();
282 }
283
284 public void run(int deltaMs) {
285 color off = color(0, 0, 0);
286 color c = off;
287 color r = #FF0000;
288 color g = #00FF00;
289 color b = #0000FF;
290 if (channelModeRed) c |= r;
291 if (channelModeGreen) c |= g;
292 if (channelModeBlue) c |= b;
293
294 int ci = 0;
295 for (Cube cube : model.cubes) {
296 boolean cubeOn = false;
297 int indexOfCubeInChannel = indexOfCubeInChannel(cube);
298 switch (mappingMode) {
299 case MAPPING_MODE_ALL: cubeOn = true; break;
300 case MAPPING_MODE_SINGLE_CUBE: cubeOn = (cubeIndex == ci); break;
301 case MAPPING_MODE_CHANNEL: cubeOn = (channelIndex > 0); break;
302 }
303 if (cubeOn) {
304 if (mappingMode == MAPPING_MODE_CHANNEL) {
305 color cc = off;
306 switch (indexOfCubeInChannel) {
307 case 1: cc = r; break;
308 case 2: cc = r|g; break;
309 case 3: cc = g; break;
310 case 4: cc = b; break;
311 case 5: cc = r|b; break;
312 }
313 setColor(cube, cc);
314 } else if (cubeMode == CUBE_MODE_STRIP_PATTERN) {
315 int si = 0;
316 color sc = off;
317 for (Strip strip : cube.strips) {
318 int faceI = si / Face.STRIPS_PER_FACE;
319 switch (faceI) {
320 case 0: sc = r; break;
321 case 1: sc = g; break;
322 case 2: sc = b; break;
323 case 3: sc = r|g|b; break;
324 }
325 if (si % Face.STRIPS_PER_FACE == 2) {
326 sc = r|g;
327 }
328 setColor(strip, sc);
329 ++si;
330 }
331 } else if (cubeMode == CUBE_MODE_SINGLE_STRIP) {
332 setColor(cube, off);
333 setColor(cube.strips.get(stripIndex), c);
334 } else {
335 setColor(cube, c);
336 }
337 } else {
338 setColor(cube, off);
339 }
340 ++ci;
341 }
342
343 }
344
345 public void incCube() {
346 cubeIndex = (cubeIndex + 1) % model.cubes.size();
347 }
348
349 public void decCube() {
350 --cubeIndex;
351 if (cubeIndex < 0) {
352 cubeIndex += model.cubes.size();
353 }
354 }
355
356 public void incChannel() {
357 channelIndex = (channelIndex + 1) % numChannels;
358 setChannel();
359 }
360
361 public void decChannel() {
362 channelIndex = (channelIndex + numChannels - 1) % numChannels;
363 setChannel();
364 }
365
366 public void incStrip() {
367 stripIndex = (stripIndex + 1) % Cube.STRIPS_PER_CUBE;
368 }
369
370 public void decStrip() {
371 stripIndex = (stripIndex + Cube.STRIPS_PER_CUBE - 1) % Cube.STRIPS_PER_CUBE;
372 }
373
374 public void keyPressed() {
375 switch (keyCode) {
376 case UP: if (mappingMode == MAPPING_MODE_CHANNEL) incChannel(); else incCube(); break;
377 case DOWN: if (mappingMode == MAPPING_MODE_CHANNEL) decChannel(); else decCube(); break;
378 case LEFT: decStrip(); break;
379 case RIGHT: incStrip(); break;
380 }
381 switch (key) {
382 case 'r': channelModeRed = !channelModeRed; break;
383 case 'g': channelModeGreen = !channelModeGreen; break;
384 case 'b': channelModeBlue = !channelModeBlue; break;
385 }
386 }
387 }