Some cleanups and listen to deck for transition changes
[SugarCubes.git] / _Internals.pde
CommitLineData
49815cc0 1/**
1ecdb44a
MS
2 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
3 *
4 * //\\ //\\ //\\ //\\
5 * ///\\\ ///\\\ ///\\\ ///\\\
6 * \\\/// \\\/// \\\/// \\\///
7 * \\// \\// \\// \\//
8 *
9 * EXPERTS ONLY!! EXPERTS ONLY!!
10 *
49815cc0
MS
11 * If you are an artist, you may ignore this file! It just sets
12 * up the framework to run the patterns. Should not need modification
13 * for general animation work.
14 */
15
16import glucose.*;
17import glucose.control.*;
3f8be614 18import glucose.effect.*;
f3f5a876 19import glucose.model.*;
49815cc0 20import glucose.pattern.*;
f3f5a876 21import glucose.transform.*;
49815cc0 22import glucose.transition.*;
49815cc0 23import heronarts.lx.*;
3f8be614 24import heronarts.lx.control.*;
49815cc0 25import heronarts.lx.effect.*;
49815cc0 26import heronarts.lx.modulator.*;
f3f5a876 27import heronarts.lx.pattern.*;
49815cc0
MS
28import heronarts.lx.transition.*;
29import ddf.minim.*;
30import ddf.minim.analysis.*;
31import processing.opengl.*;
5d70e4d7 32import rwmidi.*;
49815cc0
MS
33
34final int VIEWPORT_WIDTH = 900;
35final int VIEWPORT_HEIGHT = 700;
0e3c5542 36
92c06c97 37// The trailer is measured from the outside of the black metal (but not including the higher welded part on the front)
87998ff3
MS
38final float TRAILER_WIDTH = 240;
39final float TRAILER_DEPTH = 97;
40final float TRAILER_HEIGHT = 33;
41
51d0d59a 42int targetFramerate = 60;
49815cc0 43int startMillis, lastMillis;
a898d79b
MS
44
45// Core engine variables
49815cc0
MS
46GLucose glucose;
47HeronLX lx;
48LXPattern[] patterns;
a898d79b 49MappingTool mappingTool;
79ae8245 50PandaDriver[] pandaBoards;
a898d79b
MS
51
52// Display configuration mode
bf551144 53boolean mappingMode = false;
cc9fcf4b 54boolean debugMode = false;
554e38ff 55DebugUI debugUI;
d626bc9b 56
a898d79b 57// Handles to UI objects
d626bc9b
MS
58UIContext[] overlays;
59UIPatternDeck uiPatternA;
a898d79b 60UICrossfader uiCrossfader;
d626bc9b
MS
61UIMapping uiMapping;
62UIDebugText uiDebugText;
cc9fcf4b 63
0a9f99cc 64// Camera variables
bda5421d 65float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
0a9f99cc 66
d626bc9b
MS
67LXPattern[] _patterns(GLucose glucose) {
68 LXPattern[] patterns = patterns(glucose);
69 for (LXPattern p : patterns) {
70 p.setTransition(new DissolveTransition(glucose.lx).setDuration(1000));
71 }
72 return patterns;
73}
74
49815cc0
MS
75void setup() {
76 startMillis = lastMillis = millis();
77
78 // Initialize the Processing graphics environment
79 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
0e3c5542 80 frameRate(targetFramerate);
3f8be614 81 noSmooth();
49815cc0
MS
82 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
83 logTime("Created viewport");
84
85 // Create the GLucose engine to run the cubes
186bc4d3 86 glucose = new GLucose(this, buildModel());
49815cc0 87 lx = glucose.lx;
cc9fcf4b 88 lx.enableKeyboardTempo();
49815cc0
MS
89 logTime("Built GLucose engine");
90
91 // Set the patterns
d626bc9b 92 Engine engine = lx.engine;
d626bc9b
MS
93 engine.setPatterns(patterns = _patterns(glucose));
94 engine.addDeck(_patterns(glucose));
49815cc0 95 logTime("Built patterns");
a898d79b
MS
96 glucose.setTransitions(transitions(glucose));
97 logTime("Built transitions");
98 glucose.lx.addEffects(effects(glucose));
49815cc0 99 logTime("Built effects");
e73ef85d
MS
100
101 // Build output driver
186bc4d3 102 PandaMapping[] pandaMappings = buildPandaList();
45f43cc2
MS
103 pandaBoards = new PandaDriver[pandaMappings.length];
104 int pbi = 0;
105 for (PandaMapping pm : pandaMappings) {
44b8de9c 106 pandaBoards[pbi++] = new PandaDriver(pm.ip, glucose.model, pm);
45f43cc2
MS
107 }
108 mappingTool = new MappingTool(glucose, pandaMappings);
109 logTime("Built PandaDriver");
49815cc0
MS
110
111 // Build overlay UI
45f43cc2 112 debugUI = new DebugUI(pandaMappings);
d626bc9b
MS
113 overlays = new UIContext[] {
114 uiPatternA = new UIPatternDeck(lx.engine.getDeck(0), "PATTERN A", 4, 4, 140, 344),
a898d79b 115 uiCrossfader = new UICrossfader(4, 352, 140, 212),
d626bc9b
MS
116
117 new UIPatternDeck(lx.engine.getDeck(1), "PATTERN B", width-144, 4, 140, 344),
118 new UIEffects(width-144, 352, 140, 144),
119 new UITempo(width-144, 498, 140, 50),
1f42cce7 120 new UIOutput(width-144, 552, 140, 106),
d626bc9b
MS
121
122 uiDebugText = new UIDebugText(4, height-64, width-8, 44),
123 uiMapping = new UIMapping(mappingTool, 4, 4, 140, 344),
124 };
125 uiMapping.setVisible(false);
49815cc0 126 logTime("Built overlay UI");
1ecdb44a 127
49815cc0 128 // MIDI devices
cc9fcf4b
MS
129 for (MidiInputDevice d : RWMidi.getInputDevices()) {
130 d.createInput(this);
131 }
132 SCMidiDevices.initializeStandardDevices(glucose);
3f8be614 133 logTime("Setup MIDI devices");
554e38ff 134
0a9f99cc 135 // Setup camera
d626bc9b 136 midX = TRAILER_WIDTH/2.;
0a9f99cc 137 midY = glucose.model.yMax/2;
e0cea600
MS
138 midZ = TRAILER_DEPTH/2.;
139 eyeR = -290;
0a9f99cc 140 eyeA = .15;
d626bc9b 141 eyeY = midY + 70;
0a9f99cc
MS
142 eyeX = midX + eyeR*sin(eyeA);
143 eyeZ = midZ + eyeR*cos(eyeA);
bda5421d
MS
144 addMouseWheelListener(new java.awt.event.MouseWheelListener() {
145 public void mouseWheelMoved(java.awt.event.MouseWheelEvent mwe) {
146 mouseWheel(mwe.getWheelRotation());
147 }});
148
49815cc0 149 println("Total setup: " + (millis() - startMillis) + "ms");
e73ef85d 150 println("Hit the 'p' key to toggle Panda Board output");
49815cc0
MS
151}
152
e28f168c 153
cc9fcf4b
MS
154void controllerChangeReceived(rwmidi.Controller cc) {
155 if (debugMode) {
156 println("CC: " + cc.toString());
157 }
158}
159
160void noteOnReceived(Note note) {
161 if (debugMode) {
162 println("Note On: " + note.toString());
163 }
164}
165
166void noteOffReceived(Note note) {
167 if (debugMode) {
168 println("Note Off: " + note.toString());
169 }
170}
171
49815cc0
MS
172void logTime(String evt) {
173 int now = millis();
174 println(evt + ": " + (now - lastMillis) + "ms");
175 lastMillis = now;
176}
177
178void draw() {
0a9f99cc
MS
179 // Draws the simulation and the 2D UI overlay
180 background(40);
a898d79b
MS
181 color[] colors = glucose.getColors();
182
183 String displayMode = uiCrossfader.getDisplayMode();
d626bc9b
MS
184 if (displayMode == "A") {
185 colors = lx.engine.getDeck(0).getColors();
186 } else if (displayMode == "B") {
187 colors = lx.engine.getDeck(1).getColors();
188 }
554e38ff
MS
189 if (debugMode) {
190 debugUI.maskColors(colors);
191 }
51d0d59a 192
0a9f99cc
MS
193 camera(
194 eyeX, eyeY, eyeZ,
195 midX, midY, midZ,
196 0, -1, 0
197 );
51d0d59a 198
d626bc9b
MS
199 translate(0, 10, 0);
200
51d0d59a
MS
201 noStroke();
202 fill(#141414);
87998ff3 203 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
51d0d59a
MS
204 fill(#070707);
205 stroke(#222222);
0a9f99cc 206 beginShape();
51d0d59a 207 vertex(0, 0, 0);
87998ff3
MS
208 vertex(TRAILER_WIDTH, 0, 0);
209 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
210 vertex(0, 0, TRAILER_DEPTH);
51d0d59a 211 endShape();
0a9f99cc 212
51d0d59a 213 noStroke();
ab77005f
MS
214// drawBassBox(glucose.model.bassBox);
215// for (Speaker s : glucose.model.speakers) {
216// drawSpeaker(s);
217// }
51d0d59a
MS
218 for (Cube c : glucose.model.cubes) {
219 drawCube(c);
220 }
221
0a9f99cc
MS
222 noFill();
223 strokeWeight(2);
224 beginShape(POINTS);
922def98
MS
225 // TODO(mcslee): restore when bassBox/speakers are right again
226 // for (Point p : glucose.model.points) {
227 for (Cube cube : glucose.model.cubes) {
228 for (Point p : cube.points) {
229 stroke(colors[p.index]);
230 vertex(p.fx, p.fy, p.fz);
231 }
0a9f99cc
MS
232 }
233 endShape();
234
d626bc9b 235 // 2D Overlay UI
0a9f99cc 236 drawUI();
d626bc9b
MS
237
238 // Send output colors
239 color[] sendColors = glucose.getColors();
554e38ff 240 if (debugMode) {
d626bc9b 241 debugUI.maskColors(colors);
554e38ff 242 }
a797d019 243
6702151a
MS
244 // Gamma correction here. Apply a cubic to the brightness
245 // for better representation of dynamic range
246 for (int i = 0; i < colors.length; ++i) {
247 float b = brightness(colors[i]) / 100.f;
248 colors[i] = color(
249 hue(colors[i]),
250 saturation(colors[i]),
251 (b*b*b) * 100.
252 );
253 }
e7f14d4d 254
e73ef85d 255 // TODO(mcslee): move into GLucose engine
79ae8245 256 for (PandaDriver p : pandaBoards) {
b58e5a1d 257 p.send(colors);
e73ef85d 258 }
49815cc0
MS
259}
260
92c06c97
MS
261void drawBassBox(BassBox b) {
262 float in = .15;
e76480d4
MS
263
264 noStroke();
265 fill(#191919);
266 pushMatrix();
267 translate(b.x + BassBox.EDGE_WIDTH/2., b.y + BassBox.EDGE_HEIGHT/2, b.z + BassBox.EDGE_DEPTH/2.);
268 box(BassBox.EDGE_WIDTH-20*in, BassBox.EDGE_HEIGHT-20*in, BassBox.EDGE_DEPTH-20*in);
269 popMatrix();
270
271 noStroke();
272 fill(#393939);
92c06c97
MS
273 drawBox(b.x+in, b.y+in, b.z+in, 0, 0, 0, BassBox.EDGE_WIDTH-in*2, BassBox.EDGE_HEIGHT-in*2, BassBox.EDGE_DEPTH-in*2, Cube.CHANNEL_WIDTH-in);
274
39011e7e
MS
275 pushMatrix();
276 translate(b.x+(Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT-in, b.z + BassBox.EDGE_DEPTH/2.);
277 float lastOffset = 0;
278 for (float offset : BoothFloor.STRIP_OFFSETS) {
279 translate(offset - lastOffset, 0, 0);
280 box(Cube.CHANNEL_WIDTH-in, 0, BassBox.EDGE_DEPTH - 2*in);
281 lastOffset = offset;
282 }
283 popMatrix();
284
92c06c97
MS
285 pushMatrix();
286 translate(b.x + (Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT/2., b.z + in);
287 for (int j = 0; j < 2; ++j) {
288 pushMatrix();
289 for (int i = 0; i < BassBox.NUM_FRONT_STRUTS; ++i) {
290 translate(BassBox.FRONT_STRUT_SPACING, 0, 0);
291 box(Cube.CHANNEL_WIDTH-in, BassBox.EDGE_HEIGHT - in*2, 0);
292 }
293 popMatrix();
294 translate(0, 0, BassBox.EDGE_DEPTH - 2*in);
295 }
296 popMatrix();
297
298 pushMatrix();
299 translate(b.x + in, b.y + BassBox.EDGE_HEIGHT/2., b.z + BassBox.SIDE_STRUT_SPACING + (Cube.CHANNEL_WIDTH-in)/2.);
300 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
301 translate(BassBox.EDGE_WIDTH-2*in, 0, 0);
302 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
ab77005f 303 popMatrix();
e76480d4 304
92c06c97
MS
305}
306
51d0d59a 307void drawCube(Cube c) {
254d34c0 308 float in = .15;
e76480d4
MS
309 noStroke();
310 fill(#393939);
254d34c0 311 drawBox(c.x+in, c.y+in, c.z+in, c.rx, c.ry, c.rz, Cube.EDGE_WIDTH-in*2, Cube.EDGE_HEIGHT-in*2, Cube.EDGE_WIDTH-in*2, Cube.CHANNEL_WIDTH-in);
51d0d59a
MS
312}
313
e76480d4
MS
314void drawSpeaker(Speaker s) {
315 float in = .15;
316
317 noStroke();
318 fill(#191919);
319 pushMatrix();
320 translate(s.x, s.y, s.z);
321 rotate(s.ry / 180. * PI, 0, -1, 0);
322 translate(Speaker.EDGE_WIDTH/2., Speaker.EDGE_HEIGHT/2., Speaker.EDGE_DEPTH/2.);
323 box(Speaker.EDGE_WIDTH-20*in, Speaker.EDGE_HEIGHT-20*in, Speaker.EDGE_DEPTH-20*in);
254fbb68
MS
324 translate(0, Speaker.EDGE_HEIGHT/2. + Speaker.EDGE_HEIGHT*.8/2, 0);
325
326 fill(#222222);
327 box(Speaker.EDGE_WIDTH*.6, Speaker.EDGE_HEIGHT*.8, Speaker.EDGE_DEPTH*.75);
e76480d4
MS
328 popMatrix();
329
e76480d4
MS
330 noStroke();
331 fill(#393939);
332 drawBox(s.x+in, s.y+in, s.z+in, 0, s.ry, 0, Speaker.EDGE_WIDTH-in*2, Speaker.EDGE_HEIGHT-in*2, Speaker.EDGE_DEPTH-in*2, Cube.CHANNEL_WIDTH-in);
ab77005f 333}
e76480d4 334
51d0d59a
MS
335void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
336 pushMatrix();
337 translate(x, y, z);
e0cea600 338 rotate(rx / 180. * PI, -1, 0, 0);
51d0d59a 339 rotate(ry / 180. * PI, 0, -1, 0);
e0cea600 340 rotate(rz / 180. * PI, 0, 0, -1);
51d0d59a
MS
341 for (int i = 0; i < 4; ++i) {
342 float wid = (i % 2 == 0) ? xd : zd;
343
344 beginShape();
345 vertex(0, 0);
346 vertex(wid, 0);
347 vertex(wid, yd);
348 vertex(wid - sw, yd);
349 vertex(wid - sw, sw);
350 vertex(0, sw);
351 endShape();
352 beginShape();
353 vertex(0, sw);
354 vertex(0, yd);
355 vertex(wid - sw, yd);
356 vertex(wid - sw, yd - sw);
357 vertex(sw, yd - sw);
358 vertex(sw, sw);
359 endShape();
360
361 translate(wid, 0, 0);
362 rotate(HALF_PI, 0, -1, 0);
363 }
364 popMatrix();
365}
366
49815cc0 367void drawUI() {
d626bc9b
MS
368 camera();
369 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
370 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
371 ((PGraphicsOpenGL)g).endGL();
372 strokeWeight(1);
373
49815cc0 374 if (uiOn) {
d626bc9b
MS
375 for (UIContext context : overlays) {
376 context.draw();
377 }
378 }
379
380 // Always draw FPS meter
381 fill(#555555);
382 textSize(9);
383 textAlign(LEFT, BASELINE);
384 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
385
386 if (debugMode) {
387 debugUI.draw();
49815cc0 388 }
49815cc0
MS
389}
390
391boolean uiOn = true;
d626bc9b 392LXPattern restoreToPattern = null;
ab77005f 393
49815cc0 394void keyPressed() {
bf551144 395 if (mappingMode) {
d626bc9b 396 mappingTool.keyPressed(uiMapping);
bf551144 397 }
3f8be614 398 switch (key) {
0e3c5542
MS
399 case '-':
400 case '_':
401 frameRate(--targetFramerate);
402 break;
403 case '=':
404 case '+':
405 frameRate(++targetFramerate);
406 break;
cc9fcf4b
MS
407 case 'd':
408 debugMode = !debugMode;
409 println("Debug output: " + (debugMode ? "ON" : "OFF"));
554e38ff 410 break;
bf551144
MS
411 case 'm':
412 mappingMode = !mappingMode;
d626bc9b
MS
413 uiPatternA.setVisible(!mappingMode);
414 uiMapping.setVisible(mappingMode);
bf551144 415 if (mappingMode) {
d626bc9b 416 restoreToPattern = lx.getPattern();
bf551144
MS
417 lx.setPatterns(new LXPattern[] { mappingTool });
418 } else {
bf551144 419 lx.setPatterns(patterns);
a898d79b
MS
420 LXTransition pop = restoreToPattern.getTransition();
421 restoreToPattern.setTransition(null);
d626bc9b 422 lx.goPattern(restoreToPattern);
a898d79b 423 restoreToPattern.setTransition(pop);
bf551144
MS
424 }
425 break;
e73ef85d 426 case 'p':
79ae8245
MS
427 for (PandaDriver p : pandaBoards) {
428 p.toggle();
429 }
cc9fcf4b 430 break;
3f8be614
MS
431 case 'u':
432 uiOn = !uiOn;
433 break;
49815cc0
MS
434 }
435}
436
0a9f99cc 437int mx, my;
0a9f99cc 438void mousePressed() {
d626bc9b
MS
439 boolean debugged = false;
440 if (debugMode) {
441 debugged = debugUI.mousePressed();
442 }
443 if (!debugged) {
444 for (UIContext context : overlays) {
445 context.mousePressed(mouseX, mouseY);
446 }
0a9f99cc 447 }
d626bc9b
MS
448 mx = mouseX;
449 my = mouseY;
0a9f99cc
MS
450}
451
452void mouseDragged() {
d626bc9b
MS
453 boolean dragged = false;
454 for (UIContext context : overlays) {
455 dragged |= context.mouseDragged(mouseX, mouseY);
456 }
457 if (!dragged) {
0a9f99cc
MS
458 int dx = mouseX - mx;
459 int dy = mouseY - my;
460 mx = mouseX;
461 my = mouseY;
462 eyeA += dx*.003;
463 eyeX = midX + eyeR*sin(eyeA);
464 eyeZ = midZ + eyeR*cos(eyeA);
465 eyeY += dy;
466 }
467}
468
469void mouseReleased() {
d626bc9b
MS
470 for (UIContext context : overlays) {
471 context.mouseReleased(mouseX, mouseY);
472 }
473
474 // ui.mouseReleased();
0a9f99cc 475}
bda5421d
MS
476
477void mouseWheel(int delta) {
d626bc9b
MS
478 boolean wheeled = false;
479 for (UIContext context : overlays) {
480 wheeled |= context.mouseWheel(mouseX, mouseY, delta);
481 }
482
483 if (!wheeled) {
0ba6ac44
MS
484 eyeR = constrain(eyeR - delta, -500, -80);
485 eyeX = midX + eyeR*sin(eyeA);
486 eyeZ = midZ + eyeR*cos(eyeA);
487 }
bda5421d 488}