Clean up new debug UI for unused/duplicated cubes a bit
[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
MS
43
44int startMillis, lastMillis;
45GLucose glucose;
46HeronLX lx;
bf551144 47MappingTool mappingTool;
49815cc0
MS
48LXPattern[] patterns;
49LXTransition[] transitions;
50LXEffect[] effects;
51OverlayUI ui;
bf551144
MS
52ControlUI controlUI;
53MappingUI mappingUI;
79ae8245 54PandaDriver[] pandaBoards;
bf551144 55boolean mappingMode = false;
cc9fcf4b 56boolean debugMode = false;
554e38ff 57DebugUI debugUI;
cc9fcf4b 58
0a9f99cc 59// Camera variables
bda5421d 60float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
0a9f99cc 61
49815cc0
MS
62void setup() {
63 startMillis = lastMillis = millis();
64
65 // Initialize the Processing graphics environment
66 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
0e3c5542 67 frameRate(targetFramerate);
3f8be614 68 noSmooth();
49815cc0
MS
69 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
70 logTime("Created viewport");
71
72 // Create the GLucose engine to run the cubes
186bc4d3 73 glucose = new GLucose(this, buildModel());
49815cc0 74 lx = glucose.lx;
cc9fcf4b 75 lx.enableKeyboardTempo();
49815cc0
MS
76 logTime("Built GLucose engine");
77
78 // Set the patterns
79 glucose.lx.setPatterns(patterns = patterns(glucose));
80 logTime("Built patterns");
81 glucose.lx.addEffects(effects = effects(glucose));
82 logTime("Built effects");
cc9fcf4b 83 glucose.setTransitions(transitions = transitions(glucose));
49815cc0 84 logTime("Built transitions");
e73ef85d
MS
85
86 // Build output driver
186bc4d3 87 PandaMapping[] pandaMappings = buildPandaList();
45f43cc2
MS
88 pandaBoards = new PandaDriver[pandaMappings.length];
89 int pbi = 0;
90 for (PandaMapping pm : pandaMappings) {
44b8de9c 91 pandaBoards[pbi++] = new PandaDriver(pm.ip, glucose.model, pm);
45f43cc2
MS
92 }
93 mappingTool = new MappingTool(glucose, pandaMappings);
94 logTime("Built PandaDriver");
49815cc0
MS
95
96 // Build overlay UI
bf551144
MS
97 ui = controlUI = new ControlUI();
98 mappingUI = new MappingUI(mappingTool);
45f43cc2 99 debugUI = new DebugUI(pandaMappings);
49815cc0 100 logTime("Built overlay UI");
1ecdb44a 101
49815cc0 102 // MIDI devices
cc9fcf4b
MS
103 for (MidiInputDevice d : RWMidi.getInputDevices()) {
104 d.createInput(this);
105 }
106 SCMidiDevices.initializeStandardDevices(glucose);
3f8be614 107 logTime("Setup MIDI devices");
554e38ff 108
0a9f99cc 109 // Setup camera
e0cea600 110 midX = TRAILER_WIDTH/2. + 20;
0a9f99cc 111 midY = glucose.model.yMax/2;
e0cea600
MS
112 midZ = TRAILER_DEPTH/2.;
113 eyeR = -290;
0a9f99cc
MS
114 eyeA = .15;
115 eyeY = midY + 20;
116 eyeX = midX + eyeR*sin(eyeA);
117 eyeZ = midZ + eyeR*cos(eyeA);
bda5421d
MS
118 addMouseWheelListener(new java.awt.event.MouseWheelListener() {
119 public void mouseWheelMoved(java.awt.event.MouseWheelEvent mwe) {
120 mouseWheel(mwe.getWheelRotation());
121 }});
122
49815cc0 123 println("Total setup: " + (millis() - startMillis) + "ms");
e73ef85d 124 println("Hit the 'p' key to toggle Panda Board output");
49815cc0
MS
125}
126
e28f168c
AG
127boolean[] noteState = new boolean[16];
128
cc9fcf4b
MS
129void controllerChangeReceived(rwmidi.Controller cc) {
130 if (debugMode) {
131 println("CC: " + cc.toString());
132 }
e28f168c
AG
133 //println(cc.getInput().getName());
134 int c = cc.getCC();
135 if(c==1){
136 for(int i=0; i<16; i++){
137 if(noteState[i] && i<8) { LXParameter p = glucose.patternKnobs.get(i); p.setValue(cc.getValue()/127.0); }
138 else if(noteState[i] && i<16) { try { LXParameter p = gparams.get(i-8); p.setValue(cc.getValue()/127.0); } catch(Exception e) {} }
139 }
140 }
141 if(c==2){
142 for(int i=0; i<16; i++){
143 //sif(noteState[i] && i<8) { println( gplay.Sliders ); }
144 //else if(noteState[i] && i<16) { try { LXParameter p = gparams.get(i-8); p.setValue(cc.getValue()/127.0); } catch(Exception e) {} }
145 }
146 }
147
148
149 //if(c>=16 || c<16+8){
150 // LXParameter p = gparams.get(c-16);
151 // p.setValue(c/127.0);
152 //}
cc9fcf4b
MS
153}
154
e28f168c 155
cc9fcf4b
MS
156void noteOnReceived(Note note) {
157 if (debugMode) {
158 println("Note On: " + note.toString());
159 }
e28f168c
AG
160 int pitch = note.getPitch();
161 if(pitch>=36 && pitch <36+16){
162 noteState[pitch-36]=true;
163 }
cc9fcf4b
MS
164}
165
166void noteOffReceived(Note note) {
167 if (debugMode) {
168 println("Note Off: " + note.toString());
169 }
e28f168c
AG
170 int pitch = note.getPitch();
171 if(pitch>=36 && pitch <36+16){
172 noteState[pitch-36]=false;
173 }
cc9fcf4b
MS
174}
175
e28f168c 176
49815cc0
MS
177void logTime(String evt) {
178 int now = millis();
179 println(evt + ": " + (now - lastMillis) + "ms");
180 lastMillis = now;
181}
182
183void draw() {
0a9f99cc
MS
184 // Draws the simulation and the 2D UI overlay
185 background(40);
186 color[] colors = glucose.getColors();
554e38ff
MS
187 if (debugMode) {
188 debugUI.maskColors(colors);
189 }
51d0d59a 190
0a9f99cc
MS
191 camera(
192 eyeX, eyeY, eyeZ,
193 midX, midY, midZ,
194 0, -1, 0
195 );
51d0d59a 196
51d0d59a
MS
197 noStroke();
198 fill(#141414);
87998ff3 199 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
51d0d59a
MS
200 fill(#070707);
201 stroke(#222222);
0a9f99cc 202 beginShape();
51d0d59a 203 vertex(0, 0, 0);
87998ff3
MS
204 vertex(TRAILER_WIDTH, 0, 0);
205 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
206 vertex(0, 0, TRAILER_DEPTH);
51d0d59a 207 endShape();
0a9f99cc 208
51d0d59a 209 noStroke();
92c06c97 210 drawBassBox(glucose.model.bassBox);
e76480d4
MS
211 for (Speaker s : glucose.model.speakers) {
212 drawSpeaker(s);
213 }
51d0d59a
MS
214 for (Cube c : glucose.model.cubes) {
215 drawCube(c);
216 }
217
0a9f99cc
MS
218 noFill();
219 strokeWeight(2);
220 beginShape(POINTS);
221 for (Point p : glucose.model.points) {
222 stroke(colors[p.index]);
223 vertex(p.fx, p.fy, p.fz);
224 }
225 endShape();
226
227 // 2D Overlay
228 camera();
e0cea600 229 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
0a9f99cc
MS
230 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
231 ((PGraphicsOpenGL)g).endGL();
232 strokeWeight(1);
233 drawUI();
e73ef85d 234
554e38ff
MS
235 if (debugMode) {
236 debugUI.draw();
237 }
a797d019 238
6702151a
MS
239 // Gamma correction here. Apply a cubic to the brightness
240 // for better representation of dynamic range
241 for (int i = 0; i < colors.length; ++i) {
242 float b = brightness(colors[i]) / 100.f;
243 colors[i] = color(
244 hue(colors[i]),
245 saturation(colors[i]),
246 (b*b*b) * 100.
247 );
248 }
e7f14d4d 249
e73ef85d 250 // TODO(mcslee): move into GLucose engine
79ae8245 251 for (PandaDriver p : pandaBoards) {
b58e5a1d 252 p.send(colors);
e73ef85d 253 }
49815cc0
MS
254}
255
92c06c97 256void drawBassBox(BassBox b) {
e28f168c 257 /*
92c06c97 258 float in = .15;
e76480d4
MS
259
260 noStroke();
261 fill(#191919);
262 pushMatrix();
263 translate(b.x + BassBox.EDGE_WIDTH/2., b.y + BassBox.EDGE_HEIGHT/2, b.z + BassBox.EDGE_DEPTH/2.);
264 box(BassBox.EDGE_WIDTH-20*in, BassBox.EDGE_HEIGHT-20*in, BassBox.EDGE_DEPTH-20*in);
265 popMatrix();
266
267 noStroke();
268 fill(#393939);
92c06c97
MS
269 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);
270
39011e7e
MS
271 pushMatrix();
272 translate(b.x+(Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT-in, b.z + BassBox.EDGE_DEPTH/2.);
273 float lastOffset = 0;
274 for (float offset : BoothFloor.STRIP_OFFSETS) {
275 translate(offset - lastOffset, 0, 0);
276 box(Cube.CHANNEL_WIDTH-in, 0, BassBox.EDGE_DEPTH - 2*in);
277 lastOffset = offset;
278 }
279 popMatrix();
280
92c06c97
MS
281 pushMatrix();
282 translate(b.x + (Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT/2., b.z + in);
283 for (int j = 0; j < 2; ++j) {
284 pushMatrix();
285 for (int i = 0; i < BassBox.NUM_FRONT_STRUTS; ++i) {
286 translate(BassBox.FRONT_STRUT_SPACING, 0, 0);
287 box(Cube.CHANNEL_WIDTH-in, BassBox.EDGE_HEIGHT - in*2, 0);
288 }
289 popMatrix();
290 translate(0, 0, BassBox.EDGE_DEPTH - 2*in);
291 }
292 popMatrix();
293
294 pushMatrix();
295 translate(b.x + in, b.y + BassBox.EDGE_HEIGHT/2., b.z + BassBox.SIDE_STRUT_SPACING + (Cube.CHANNEL_WIDTH-in)/2.);
296 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
297 translate(BassBox.EDGE_WIDTH-2*in, 0, 0);
298 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
e28f168c 299 popMatrix();*/
e76480d4 300
92c06c97
MS
301}
302
51d0d59a 303void drawCube(Cube c) {
254d34c0 304 float in = .15;
e76480d4
MS
305 noStroke();
306 fill(#393939);
254d34c0 307 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
308}
309
e76480d4
MS
310void drawSpeaker(Speaker s) {
311 float in = .15;
312
313 noStroke();
314 fill(#191919);
315 pushMatrix();
316 translate(s.x, s.y, s.z);
317 rotate(s.ry / 180. * PI, 0, -1, 0);
318 translate(Speaker.EDGE_WIDTH/2., Speaker.EDGE_HEIGHT/2., Speaker.EDGE_DEPTH/2.);
319 box(Speaker.EDGE_WIDTH-20*in, Speaker.EDGE_HEIGHT-20*in, Speaker.EDGE_DEPTH-20*in);
254fbb68
MS
320 translate(0, Speaker.EDGE_HEIGHT/2. + Speaker.EDGE_HEIGHT*.8/2, 0);
321
322 fill(#222222);
323 box(Speaker.EDGE_WIDTH*.6, Speaker.EDGE_HEIGHT*.8, Speaker.EDGE_DEPTH*.75);
e76480d4
MS
324 popMatrix();
325
e76480d4
MS
326 noStroke();
327 fill(#393939);
328 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);
329}
330
331
51d0d59a
MS
332void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
333 pushMatrix();
334 translate(x, y, z);
e0cea600 335 rotate(rx / 180. * PI, -1, 0, 0);
51d0d59a 336 rotate(ry / 180. * PI, 0, -1, 0);
e0cea600 337 rotate(rz / 180. * PI, 0, 0, -1);
51d0d59a
MS
338 for (int i = 0; i < 4; ++i) {
339 float wid = (i % 2 == 0) ? xd : zd;
340
341 beginShape();
342 vertex(0, 0);
343 vertex(wid, 0);
344 vertex(wid, yd);
345 vertex(wid - sw, yd);
346 vertex(wid - sw, sw);
347 vertex(0, sw);
348 endShape();
349 beginShape();
350 vertex(0, sw);
351 vertex(0, yd);
352 vertex(wid - sw, yd);
353 vertex(wid - sw, yd - sw);
354 vertex(sw, yd - sw);
355 vertex(sw, sw);
356 endShape();
357
358 translate(wid, 0, 0);
359 rotate(HALF_PI, 0, -1, 0);
360 }
361 popMatrix();
362}
363
49815cc0
MS
364void drawUI() {
365 if (uiOn) {
366 ui.draw();
367 } else {
368 ui.drawHelpTip();
369 }
370 ui.drawFPS();
e28f168c 371 ui.drawDanText();
49815cc0
MS
372}
373
374boolean uiOn = true;
bf551144
MS
375int restoreToIndex = -1;
376
e28f168c 377boolean doDual = false;
49815cc0 378void keyPressed() {
bf551144
MS
379 if (mappingMode) {
380 mappingTool.keyPressed();
381 }
3f8be614 382 switch (key) {
e28f168c
AG
383 case 'w':
384 doDual = !doDual;
385 break;
0e3c5542
MS
386 case '-':
387 case '_':
388 frameRate(--targetFramerate);
389 break;
390 case '=':
391 case '+':
392 frameRate(++targetFramerate);
393 break;
cc9fcf4b
MS
394 case 'd':
395 debugMode = !debugMode;
396 println("Debug output: " + (debugMode ? "ON" : "OFF"));
554e38ff 397 break;
bf551144
MS
398 case 'm':
399 mappingMode = !mappingMode;
400 if (mappingMode) {
401 LXPattern pattern = lx.getPattern();
402 for (int i = 0; i < patterns.length; ++i) {
403 if (pattern == patterns[i]) {
404 restoreToIndex = i;
405 break;
406 }
407 }
408 ui = mappingUI;
409 lx.setPatterns(new LXPattern[] { mappingTool });
410 } else {
411 ui = controlUI;
412 lx.setPatterns(patterns);
413 lx.goIndex(restoreToIndex);
414 }
415 break;
e73ef85d 416 case 'p':
79ae8245
MS
417 for (PandaDriver p : pandaBoards) {
418 p.toggle();
419 }
cc9fcf4b 420 break;
3f8be614
MS
421 case 'u':
422 uiOn = !uiOn;
423 break;
49815cc0
MS
424 }
425}
426
0a9f99cc 427int mx, my;
0a9f99cc 428void mousePressed() {
79ae8245
MS
429 ui.mousePressed();
430 if (mouseX < ui.leftPos) {
554e38ff
MS
431 if (debugMode) {
432 debugUI.mousePressed();
433 }
0a9f99cc
MS
434 mx = mouseX;
435 my = mouseY;
436 }
437}
438
439void mouseDragged() {
440 if (mouseX > ui.leftPos) {
441 ui.mouseDragged();
442 } else {
443 int dx = mouseX - mx;
444 int dy = mouseY - my;
445 mx = mouseX;
446 my = mouseY;
447 eyeA += dx*.003;
448 eyeX = midX + eyeR*sin(eyeA);
449 eyeZ = midZ + eyeR*cos(eyeA);
450 eyeY += dy;
451 }
452}
453
454void mouseReleased() {
79ae8245 455 ui.mouseReleased();
0a9f99cc 456}
bda5421d
MS
457
458void mouseWheel(int delta) {
0ba6ac44
MS
459 if (mouseX > ui.leftPos) {
460 ui.mouseWheel(delta);
461 } else {
462 eyeR = constrain(eyeR - delta, -500, -80);
463 eyeX = midX + eyeR*sin(eyeA);
464 eyeZ = midZ + eyeR*cos(eyeA);
465 }
bda5421d 466}