More mapping cleanup, got rid of tons of mess code, clean definitions now
[SugarCubes.git] / _Internals.pde
CommitLineData
49815cc0 1/**
1ecdb44a
MS
2 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
3 *
4 * //\\ //\\ //\\ //\\
5 * ///\\\ ///\\\ ///\\\ ///\\\
6 * \\\/// \\\/// \\\/// \\\///
d12e46b6 7 * \\// \\// \\// \\//H
1ecdb44a
MS
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.*;
f3f5a876 17import glucose.model.*;
49815cc0
MS
18import heronarts.lx.*;
19import heronarts.lx.effect.*;
49815cc0 20import heronarts.lx.modulator.*;
b8bb2748 21import heronarts.lx.parameter.*;
f3f5a876 22import heronarts.lx.pattern.*;
9fa29818 23import heronarts.lx.transform.*;
49815cc0 24import heronarts.lx.transition.*;
4e6626a9
MS
25import heronarts.lx.ui.*;
26import heronarts.lx.ui.component.*;
27import heronarts.lx.ui.control.*;
49815cc0
MS
28import ddf.minim.*;
29import ddf.minim.analysis.*;
30import processing.opengl.*;
5d70e4d7 31import rwmidi.*;
24fc0330 32import java.lang.reflect.*;
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)
e18b4cb7
MS
38final float TRAILER_WIDTH = 192;
39final float TRAILER_DEPTH = 192;
87998ff3
MS
40final float TRAILER_HEIGHT = 33;
41
51d0d59a 42int targetFramerate = 60;
49815cc0 43int startMillis, lastMillis;
a898d79b
MS
44
45// Core engine variables
49815cc0 46GLucose glucose;
d12e46b6 47LX lx;
34490867 48Model model;
49815cc0 49LXPattern[] patterns;
24fc0330 50Effects effects;
a898d79b 51MappingTool mappingTool;
e037f60f 52GrizzlyOutput[] grizzlies;
e0794d3a 53PresetManager presetManager;
1f974cbc 54MidiEngine midiEngine;
a898d79b
MS
55
56// Display configuration mode
bf551144 57boolean mappingMode = false;
cc9fcf4b 58boolean debugMode = false;
7974acd6 59boolean simulationOn = true;
73678c57 60boolean diagnosticsOn = false;
34327c96 61LXPattern restoreToPattern = null;
4c640acc 62PImage logo;
a41f334c 63float[] hsb = new float[3];
d626bc9b 64
a898d79b 65// Handles to UI objects
d626bc9b 66UIPatternDeck uiPatternA;
a898d79b 67UICrossfader uiCrossfader;
a8d55ade 68UIMidi uiMidi;
d626bc9b
MS
69UIMapping uiMapping;
70UIDebugText uiDebugText;
fa4f822d 71UISpeed uiSpeed;
cc9fcf4b 72
34327c96
MS
73/**
74 * Engine construction and initialization.
75 */
d1dcc4b5
MS
76
77LXTransition _transition(GLucose glucose) {
78 return new DissolveTransition(glucose.lx).setDuration(1000);
79}
80
81LXPattern[] _leftPatterns(GLucose glucose) {
d626bc9b
MS
82 LXPattern[] patterns = patterns(glucose);
83 for (LXPattern p : patterns) {
d1dcc4b5 84 p.setTransition(_transition(glucose));
d626bc9b
MS
85 }
86 return patterns;
87}
88
d1dcc4b5
MS
89LXPattern[] _rightPatterns(GLucose glucose) {
90 LXPattern[] patterns = _leftPatterns(glucose);
91 LXPattern[] rightPatterns = new LXPattern[patterns.length+1];
92 int i = 0;
93 rightPatterns[i++] = new BlankPattern(glucose).setTransition(_transition(glucose));
94 for (LXPattern p : patterns) {
95 rightPatterns[i++] = p;
96 }
97 return rightPatterns;
98}
24fc0330
MS
99
100LXEffect[] _effectsArray(Effects effects) {
101 List<LXEffect> effectList = new ArrayList<LXEffect>();
102 for (Field f : effects.getClass().getDeclaredFields()) {
103 try {
104 Object val = f.get(effects);
105 if (val instanceof LXEffect) {
106 effectList.add((LXEffect)val);
107 }
108 } catch (IllegalAccessException iax) {}
109 }
110 return effectList.toArray(new LXEffect[]{});
111}
d1dcc4b5 112
34327c96
MS
113void logTime(String evt) {
114 int now = millis();
115 println(evt + ": " + (now - lastMillis) + "ms");
116 lastMillis = now;
117}
118
49815cc0
MS
119void setup() {
120 startMillis = lastMillis = millis();
121
122 // Initialize the Processing graphics environment
123 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
0e3c5542 124 frameRate(targetFramerate);
3f8be614 125 noSmooth();
49815cc0
MS
126 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
127 logTime("Created viewport");
128
129 // Create the GLucose engine to run the cubes
34490867 130 glucose = new GLucose(this, model = buildModel());
49815cc0 131 lx = glucose.lx;
cc9fcf4b 132 lx.enableKeyboardTempo();
49815cc0 133 logTime("Built GLucose engine");
2bb56822 134
49815cc0 135 // Set the patterns
42a424d7 136 LXEngine engine = lx.engine;
d1dcc4b5
MS
137 engine.setPatterns(patterns = _leftPatterns(glucose));
138 engine.addDeck(_rightPatterns(glucose));
49815cc0 139 logTime("Built patterns");
a898d79b
MS
140 glucose.setTransitions(transitions(glucose));
141 logTime("Built transitions");
24fc0330 142 glucose.lx.addEffects(_effectsArray(effects = new Effects()));
49815cc0 143 logTime("Built effects");
4214e9a2 144
e0794d3a
MS
145 // Preset manager
146 presetManager = new PresetManager();
147 logTime("Loaded presets");
4214e9a2 148
1f974cbc
MS
149 // MIDI devices
150 midiEngine = new MidiEngine();
1f974cbc
MS
151 logTime("Setup MIDI devices");
152
e73ef85d 153 // Build output driver
e037f60f 154 grizzlies = new GrizzlyOutput[]{};
34490867 155 try {
e037f60f 156 grizzlies = buildGrizzlies();
34490867
MS
157 for (LXOutput output : grizzlies) {
158 lx.addOutput(output);
159 }
160 } catch (Exception x) {
161 x.printStackTrace();
162 }
e037f60f
MS
163 logTime("Built Grizzly Outputs");
164
165 // Mapping tools
166 mappingTool = new MappingTool(glucose);
34490867 167
49815cc0 168 // Build overlay UI
e037f60f
MS
169 UILayer[] layers = new UILayer[] {
170 // Camera layer
171 new UICameraLayer(lx.ui)
e18b4cb7 172 .setCenter(model.cx, model.cy, model.cz)
e037f60f
MS
173 .setRadius(290).addComponent(new UICubesLayer()),
174
175 // Left controls
4e6626a9 176 uiPatternA = new UIPatternDeck(lx.ui, lx.engine.getDeck(GLucose.LEFT_DECK), "PATTERN A", 4, 4, 140, 324),
a8d55ade
MS
177 new UIBlendMode(4, 332, 140, 86),
178 new UIEffects(4, 422, 140, 144),
179 new UITempo(4, 570, 140, 50),
fa4f822d 180 uiSpeed = new UISpeed(4, 624, 140, 50),
a8d55ade 181
e037f60f 182 // Right controls
4e6626a9 183 new UIPatternDeck(lx.ui, lx.engine.getDeck(GLucose.RIGHT_DECK), "PATTERN B", width-144, 4, 140, 324),
d6ac1ee8 184 uiMidi = new UIMidi(midiEngine, width-144, 332, 140, 158),
e037f60f 185 new UIOutput(grizzlies, width-144, 494, 140, 106),
d626bc9b 186
e037f60f 187 // Crossfader
a8d55ade 188 uiCrossfader = new UICrossfader(width/2-90, height-90, 180, 86),
d626bc9b 189
e037f60f 190 // Overlays
a8d55ade 191 uiDebugText = new UIDebugText(148, height-138, width-304, 44),
4e6626a9 192 uiMapping = new UIMapping(mappingTool, 4, 4, 140, 324)
d626bc9b 193 };
e037f60f
MS
194 uiMapping.setVisible(false);
195 for (UILayer layer : layers) {
196 lx.ui.addLayer(layer);
4e6626a9 197 }
e037f60f 198 logTime("Built UI");
4c640acc
MS
199
200 // Load logo image
201 logo = loadImage("data/logo.png");
e037f60f 202 logTime("Loaded logo image");
4e6626a9 203
49815cc0 204 println("Total setup: " + (millis() - startMillis) + "ms");
e037f60f 205 println("Hit the 'o' key to toggle live output");
49815cc0
MS
206}
207
e18b4cb7
MS
208long simulationNanos = 0;
209
34327c96
MS
210/**
211 * Core render loop and drawing functionality.
212 */
49815cc0 213void draw() {
73678c57
MS
214 long drawStart = System.nanoTime();
215
4e6626a9 216 // Set background
0a9f99cc 217 background(40);
4e6626a9
MS
218
219 // Send colors
e037f60f 220 color[] sendColors = glucose.getColors();
73678c57 221 long gammaStart = System.nanoTime();
7974acd6
MS
222 // Gamma correction here. Apply a cubic to the brightness
223 // for better representation of dynamic range
224 for (int i = 0; i < sendColors.length; ++i) {
225 lx.RGBtoHSB(sendColors[i], hsb);
226 float b = hsb[2];
227 sendColors[i] = lx.hsb(360.*hsb[0], 100.*hsb[1], 100.*(b*b*b));
228 }
73678c57 229 long gammaNanos = System.nanoTime() - gammaStart;
4e6626a9 230
e037f60f 231 // Always draw FPS meter
4e6626a9 232 drawFPS();
4e6626a9
MS
233
234 // TODO(mcslee): fix
73678c57 235 long drawNanos = System.nanoTime() - drawStart;
e18b4cb7
MS
236 long uiNanos = 0;
237
73678c57 238 if (diagnosticsOn) {
e037f60f 239 drawDiagnostics(drawNanos, simulationNanos, uiNanos, gammaNanos);
4e6626a9
MS
240 }
241}
242
243class UICubesLayer extends UICameraComponent {
244 void onDraw(UI ui) {
245 color[] simulationColors = glucose.getColors();
246 String displayMode = uiCrossfader.getDisplayMode();
247 if (displayMode == "A") {
248 simulationColors = lx.engine.getDeck(GLucose.LEFT_DECK).getColors();
249 } else if (displayMode == "B") {
250 simulationColors = lx.engine.getDeck(GLucose.RIGHT_DECK).getColors();
251 }
4e6626a9
MS
252
253 long simulationStart = System.nanoTime();
254 if (simulationOn) {
255 drawSimulation(simulationColors);
256 }
e18b4cb7 257 simulationNanos = System.nanoTime() - simulationStart;
4e6626a9
MS
258
259 camera();
260 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
261 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
262 ((PGraphicsOpenGL)g).endGL();
263 strokeWeight(1);
e18b4cb7
MS
264 }
265
266 void drawSimulation(color[] simulationColors) {
267 translate(0, 30, 0);
268
269 noStroke();
270 fill(#141414);
271 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
272 fill(#070707);
273 stroke(#222222);
274 beginShape();
275 vertex(0, 0, 0);
276 vertex(TRAILER_WIDTH, 0, 0);
277 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
278 vertex(0, 0, TRAILER_DEPTH);
279 endShape();
280
281 // Draw the logo on the front of platform
282 pushMatrix();
283 translate(0, 0, -1);
284 float s = .07;
285 scale(s, -s, s);
286 image(logo, TRAILER_WIDTH/2/s-logo.width/2, TRAILER_HEIGHT/2/s-logo.height/2-2/s);
287 popMatrix();
288
289 noStroke();
290 for (Cube c : glucose.model.cubes) {
291 drawCube(c);
292 }
293
294 noFill();
295 strokeWeight(2);
296 beginShape(POINTS);
297 for (LXPoint p : glucose.model.points) {
298 stroke(simulationColors[p.index]);
299 vertex(p.x, p.y, p.z);
300 }
301 endShape();
302 }
303
304 void drawCube(Cube c) {
305 float in = .15;
306 noStroke();
307 fill(#393939);
308 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);
309 }
310
311 void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
312 pushMatrix();
313 translate(x, y, z);
314 rotate(rx / 180. * PI, -1, 0, 0);
315 rotate(ry / 180. * PI, 0, -1, 0);
316 rotate(rz / 180. * PI, 0, 0, -1);
317 for (int i = 0; i < 4; ++i) {
318 float wid = (i % 2 == 0) ? xd : zd;
319
320 beginShape();
321 vertex(0, 0);
322 vertex(wid, 0);
323 vertex(wid, yd);
324 vertex(wid - sw, yd);
325 vertex(wid - sw, sw);
326 vertex(0, sw);
327 endShape();
328 beginShape();
329 vertex(0, sw);
330 vertex(0, yd);
331 vertex(wid - sw, yd);
332 vertex(wid - sw, yd - sw);
333 vertex(sw, yd - sw);
334 vertex(sw, sw);
335 endShape();
336
337 translate(wid, 0, 0);
338 rotate(HALF_PI, 0, -1, 0);
339 }
340 popMatrix();
341 }
73678c57
MS
342}
343
e037f60f 344void drawDiagnostics(long drawNanos, long simulationNanos, long uiNanos, long gammaNanos) {
73678c57
MS
345 float ws = 4 / 1000000.;
346 int thirtyfps = 1000000000 / 30;
347 int sixtyfps = 1000000000 / 60;
348 int x = width - 138;
349 int y = height - 14;
350 int h = 10;
351 noFill();
352 stroke(#999999);
353 rect(x, y, thirtyfps * ws, h);
354 noStroke();
355 int xp = x;
356 float hv = 0;
e037f60f 357 for (long val : new long[] {lx.timer.drawNanos, simulationNanos, uiNanos, gammaNanos, lx.timer.outputNanos }) {
73678c57
MS
358 fill(lx.hsb(hv % 360, 100, 80));
359 rect(xp, y, val * ws, h-1);
360 hv += 140;
361 xp += val * ws;
362 }
363 noFill();
364 stroke(#333333);
365 line(x+sixtyfps*ws, y+1, x+sixtyfps*ws, y+h-1);
ef7118ad
MS
366
367 y = y - 14;
368 xp = x;
369 float tw = thirtyfps * ws;
370 noFill();
371 stroke(#999999);
372 rect(x, y, tw, h);
bae2197a 373 h = 5;
ef7118ad
MS
374 noStroke();
375 for (long val : new long[] {
376 lx.engine.timer.deckNanos,
bae2197a 377 lx.engine.timer.copyNanos,
ef7118ad
MS
378 lx.engine.timer.fxNanos}) {
379 float amt = val / (float) lx.timer.drawNanos;
380 fill(lx.hsb(hv % 360, 100, 80));
381 rect(xp, y, amt * tw, h-1);
382 hv += 140;
383 xp += amt * tw;
bae2197a
MS
384 }
385
386 xp = x;
387 y += h;
388 hv = 120;
389 for (long val : new long[] {
390 lx.engine.getDeck(0).timer.runNanos,
391 lx.engine.getDeck(1).timer.runNanos,
392 lx.engine.getDeck(1).getFaderTransition().timer.blendNanos}) {
393 float amt = val / (float) lx.timer.drawNanos;
394 fill(lx.hsb(hv % 360, 100, 80));
395 rect(xp, y, amt * tw, h-1);
396 hv += 140;
397 xp += amt * tw;
398 }
7974acd6
MS
399}
400
4e6626a9 401void drawFPS() {
d626bc9b
MS
402 // Always draw FPS meter
403 fill(#555555);
404 textSize(9);
405 textAlign(LEFT, BASELINE);
406 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
49815cc0
MS
407}
408
4c640acc 409
34327c96
MS
410/**
411 * Top-level keyboard event handling
412 */
49815cc0 413void keyPressed() {
bf551144 414 if (mappingMode) {
d626bc9b 415 mappingTool.keyPressed(uiMapping);
bf551144 416 }
3f8be614 417 switch (key) {
2b068dd5
MS
418 case '1':
419 case '2':
420 case '3':
421 case '4':
422 case '5':
423 case '6':
424 case '7':
425 case '8':
426 if (!midiEngine.isQwertyEnabled()) {
427 presetManager.select(midiEngine.getFocusedDeck(), key - '1');
428 }
429 break;
430
431 case '!':
432 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 0);
433 break;
434 case '@':
435 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 1);
436 break;
437 case '#':
438 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 2);
439 break;
440 case '$':
441 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 3);
442 break;
443 case '%':
444 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 4);
445 break;
446 case '^':
447 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 5);
448 break;
449 case '&':
450 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 6);
451 break;
452 case '*':
453 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 7);
454 break;
455
0e3c5542
MS
456 case '-':
457 case '_':
458 frameRate(--targetFramerate);
459 break;
460 case '=':
461 case '+':
462 frameRate(++targetFramerate);
75e1ddde
MS
463 break;
464 case 'b':
24fc0330 465 effects.boom.trigger();
75e1ddde 466 break;
cc9fcf4b 467 case 'd':
d6ac1ee8 468 if (!midiEngine.isQwertyEnabled()) {
a8d55ade
MS
469 debugMode = !debugMode;
470 println("Debug output: " + (debugMode ? "ON" : "OFF"));
471 }
554e38ff 472 break;
bf551144 473 case 'm':
d6ac1ee8 474 if (!midiEngine.isQwertyEnabled()) {
a8d55ade
MS
475 mappingMode = !mappingMode;
476 uiPatternA.setVisible(!mappingMode);
477 uiMapping.setVisible(mappingMode);
478 if (mappingMode) {
479 restoreToPattern = lx.getPattern();
480 lx.setPatterns(new LXPattern[] { mappingTool });
481 } else {
482 lx.setPatterns(patterns);
483 LXTransition pop = restoreToPattern.getTransition();
484 restoreToPattern.setTransition(null);
485 lx.goPattern(restoreToPattern);
486 restoreToPattern.setTransition(pop);
487 }
bf551144
MS
488 }
489 break;
19d16a16
MS
490 case 't':
491 if (!midiEngine.isQwertyEnabled()) {
492 lx.engine.setThreaded(!lx.engine.isThreaded());
493 }
494 break;
e037f60f 495 case 'o':
e73ef85d 496 case 'p':
e037f60f
MS
497 for (LXOutput output : grizzlies) {
498 output.enabled.toggle();
79ae8245 499 }
cc9fcf4b 500 break;
73678c57
MS
501 case 'q':
502 if (!midiEngine.isQwertyEnabled()) {
503 diagnosticsOn = !diagnosticsOn;
504 }
505 break;
7974acd6
MS
506 case 's':
507 if (!midiEngine.isQwertyEnabled()) {
508 simulationOn = !simulationOn;
509 }
510 break;
d626bc9b 511 }
0a9f99cc 512}
73687629 513