Basic changes to support new layout
[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
0a9f99cc 37int targetFramerate = 45;
49815cc0
MS
38
39int startMillis, lastMillis;
40GLucose glucose;
41HeronLX lx;
bf551144 42MappingTool mappingTool;
49815cc0
MS
43LXPattern[] patterns;
44LXTransition[] transitions;
45LXEffect[] effects;
46OverlayUI ui;
bf551144
MS
47ControlUI controlUI;
48MappingUI mappingUI;
e73ef85d
MS
49PandaDriver pandaFront;
50PandaDriver pandaRear;
bf551144 51boolean mappingMode = false;
e73ef85d
MS
52
53boolean pandaBoardsEnabled = false;
49815cc0 54
cc9fcf4b 55boolean debugMode = false;
554e38ff 56DebugUI debugUI;
cc9fcf4b 57
0a9f99cc 58// Camera variables
bda5421d 59float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
0a9f99cc 60
49815cc0
MS
61void setup() {
62 startMillis = lastMillis = millis();
63
64 // Initialize the Processing graphics environment
65 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
0e3c5542 66 frameRate(targetFramerate);
3f8be614 67 noSmooth();
49815cc0
MS
68 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
69 logTime("Created viewport");
70
71 // Create the GLucose engine to run the cubes
1ecdb44a 72 glucose = new GLucose(this, new SCMapping());
49815cc0 73 lx = glucose.lx;
cc9fcf4b 74 lx.enableKeyboardTempo();
49815cc0
MS
75 logTime("Built GLucose engine");
76
77 // Set the patterns
78 glucose.lx.setPatterns(patterns = patterns(glucose));
79 logTime("Built patterns");
80 glucose.lx.addEffects(effects = effects(glucose));
81 logTime("Built effects");
cc9fcf4b 82 glucose.setTransitions(transitions = transitions(glucose));
49815cc0 83 logTime("Built transitions");
e73ef85d
MS
84
85 // Build output driver
86 int[][] frontChannels = glucose.mapping.buildFrontChannelList();
87 int[][] rearChannels = glucose.mapping.buildRearChannelList();
88 int[][] flippedRGB = glucose.mapping.buildFlippedRGBList();
2bae07c9 89 mappingTool = new MappingTool(glucose, frontChannels, rearChannels);
e73ef85d
MS
90 pandaFront = new PandaDriver(new NetAddress("192.168.1.28", 9001), glucose.model, frontChannels, flippedRGB);
91 pandaRear = new PandaDriver(new NetAddress("192.168.1.29", 9001), glucose.model, rearChannels, flippedRGB);
92 logTime("Build PandaDriver");
49815cc0
MS
93
94 // Build overlay UI
bf551144
MS
95 ui = controlUI = new ControlUI();
96 mappingUI = new MappingUI(mappingTool);
554e38ff 97 debugUI = new DebugUI(frontChannels, rearChannels);
49815cc0 98 logTime("Built overlay UI");
1ecdb44a 99
49815cc0 100 // MIDI devices
cc9fcf4b
MS
101 for (MidiInputDevice d : RWMidi.getInputDevices()) {
102 d.createInput(this);
103 }
104 SCMidiDevices.initializeStandardDevices(glucose);
3f8be614 105 logTime("Setup MIDI devices");
554e38ff 106
0a9f99cc
MS
107 // Setup camera
108 midX = glucose.model.xMax/2 + 20;
109 midY = glucose.model.yMax/2;
110 midZ = glucose.model.zMax/2;
bda5421d 111 eyeR = -270;
0a9f99cc
MS
112 eyeA = .15;
113 eyeY = midY + 20;
114 eyeX = midX + eyeR*sin(eyeA);
115 eyeZ = midZ + eyeR*cos(eyeA);
bda5421d
MS
116 addMouseWheelListener(new java.awt.event.MouseWheelListener() {
117 public void mouseWheelMoved(java.awt.event.MouseWheelEvent mwe) {
118 mouseWheel(mwe.getWheelRotation());
119 }});
120
0a9f99cc 121
49815cc0 122 println("Total setup: " + (millis() - startMillis) + "ms");
e73ef85d 123 println("Hit the 'p' key to toggle Panda Board output");
49815cc0
MS
124}
125
cc9fcf4b
MS
126void controllerChangeReceived(rwmidi.Controller cc) {
127 if (debugMode) {
128 println("CC: " + cc.toString());
129 }
130}
131
132void noteOnReceived(Note note) {
133 if (debugMode) {
134 println("Note On: " + note.toString());
135 }
136}
137
138void noteOffReceived(Note note) {
139 if (debugMode) {
140 println("Note Off: " + note.toString());
141 }
142}
143
49815cc0
MS
144void logTime(String evt) {
145 int now = millis();
146 println(evt + ": " + (now - lastMillis) + "ms");
147 lastMillis = now;
148}
149
150void draw() {
0a9f99cc
MS
151 // Draws the simulation and the 2D UI overlay
152 background(40);
153 color[] colors = glucose.getColors();
554e38ff
MS
154 if (debugMode) {
155 debugUI.maskColors(colors);
156 }
157
0a9f99cc
MS
158 camera(
159 eyeX, eyeY, eyeZ,
160 midX, midY, midZ,
161 0, -1, 0
162 );
163 stroke(#333333);
164 fill(#292929);
a797d019 165 float yFloor = -2;
0a9f99cc
MS
166 beginShape();
167 vertex(0, yFloor, 0);
168 vertex(glucose.model.xMax, yFloor, 0);
169 vertex(glucose.model.xMax, yFloor, glucose.model.zMax);
170 vertex(0, yFloor, glucose.model.zMax);
171 endShape(CLOSE);
172
173 noFill();
174 strokeWeight(2);
175 beginShape(POINTS);
176 for (Point p : glucose.model.points) {
177 stroke(colors[p.index]);
178 vertex(p.fx, p.fy, p.fz);
a797d019 179 // println(p.fx + ":" + p.fy + ":" + p.fz);
0a9f99cc
MS
180 }
181 endShape();
182
183 // 2D Overlay
184 camera();
185 javax.media.opengl.GL gl= ((PGraphicsOpenGL)g).beginGL();
186 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
187 ((PGraphicsOpenGL)g).endGL();
188 strokeWeight(1);
189 drawUI();
e73ef85d 190
554e38ff
MS
191 if (debugMode) {
192 debugUI.draw();
193 }
a797d019 194
e73ef85d
MS
195 // TODO(mcslee): move into GLucose engine
196 if (pandaBoardsEnabled) {
e73ef85d
MS
197 pandaFront.send(colors);
198 pandaRear.send(colors);
199 }
49815cc0
MS
200}
201
202void drawUI() {
203 if (uiOn) {
204 ui.draw();
205 } else {
206 ui.drawHelpTip();
207 }
208 ui.drawFPS();
209}
210
211boolean uiOn = true;
bf551144
MS
212int restoreToIndex = -1;
213
49815cc0 214void keyPressed() {
bf551144
MS
215 if (mappingMode) {
216 mappingTool.keyPressed();
217 }
3f8be614 218 switch (key) {
0e3c5542
MS
219 case '-':
220 case '_':
221 frameRate(--targetFramerate);
222 break;
223 case '=':
224 case '+':
225 frameRate(++targetFramerate);
226 break;
cc9fcf4b
MS
227 case 'd':
228 debugMode = !debugMode;
229 println("Debug output: " + (debugMode ? "ON" : "OFF"));
554e38ff 230 break;
bf551144
MS
231 case 'm':
232 mappingMode = !mappingMode;
233 if (mappingMode) {
234 LXPattern pattern = lx.getPattern();
235 for (int i = 0; i < patterns.length; ++i) {
236 if (pattern == patterns[i]) {
237 restoreToIndex = i;
238 break;
239 }
240 }
241 ui = mappingUI;
242 lx.setPatterns(new LXPattern[] { mappingTool });
243 } else {
244 ui = controlUI;
245 lx.setPatterns(patterns);
246 lx.goIndex(restoreToIndex);
247 }
248 break;
e73ef85d
MS
249 case 'p':
250 pandaBoardsEnabled = !pandaBoardsEnabled;
251 println("PandaBoard Output: " + (pandaBoardsEnabled ? "ON" : "OFF"));
cc9fcf4b 252 break;
3f8be614
MS
253 case 'u':
254 uiOn = !uiOn;
255 break;
49815cc0
MS
256 }
257}
258
0a9f99cc
MS
259int mx, my;
260
261void mousePressed() {
262 if (mouseX > ui.leftPos) {
263 ui.mousePressed();
264 } else {
554e38ff
MS
265 if (debugMode) {
266 debugUI.mousePressed();
267 }
0a9f99cc
MS
268 mx = mouseX;
269 my = mouseY;
270 }
271}
272
273void mouseDragged() {
274 if (mouseX > ui.leftPos) {
275 ui.mouseDragged();
276 } else {
277 int dx = mouseX - mx;
278 int dy = mouseY - my;
279 mx = mouseX;
280 my = mouseY;
281 eyeA += dx*.003;
282 eyeX = midX + eyeR*sin(eyeA);
283 eyeZ = midZ + eyeR*cos(eyeA);
284 eyeY += dy;
285 }
286}
287
288void mouseReleased() {
289 if (mouseX > ui.leftPos) {
290 ui.mouseReleased();
291 }
292}
bda5421d
MS
293
294void mouseWheel(int delta) {
295 eyeR = constrain(eyeR - delta, -500, -80);
296 eyeX = midX + eyeR*sin(eyeA);
297 eyeZ = midZ + eyeR*cos(eyeA);
298}
49815cc0 299