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