Add mouse scroll to zoom
[SugarCubes.git] / _Internals.pde
1 /**
2 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
3 *
4 * //\\ //\\ //\\ //\\
5 * ///\\\ ///\\\ ///\\\ ///\\\
6 * \\\/// \\\/// \\\/// \\\///
7 * \\// \\// \\// \\//
8 *
9 * EXPERTS ONLY!! EXPERTS ONLY!!
10 *
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
16 import glucose.*;
17 import glucose.control.*;
18 import glucose.effect.*;
19 import glucose.model.*;
20 import glucose.pattern.*;
21 import glucose.transform.*;
22 import glucose.transition.*;
23 import heronarts.lx.*;
24 import heronarts.lx.control.*;
25 import heronarts.lx.effect.*;
26 import heronarts.lx.modulator.*;
27 import heronarts.lx.pattern.*;
28 import heronarts.lx.transition.*;
29 import ddf.minim.*;
30 import ddf.minim.analysis.*;
31 import processing.opengl.*;
32 import rwmidi.*;
33
34 final int VIEWPORT_WIDTH = 900;
35 final int VIEWPORT_HEIGHT = 700;
36
37 int targetFramerate = 45;
38
39 int startMillis, lastMillis;
40 GLucose glucose;
41 HeronLX lx;
42 MappingTool mappingTool;
43 LXPattern[] patterns;
44 LXTransition[] transitions;
45 LXEffect[] effects;
46 OverlayUI ui;
47 ControlUI controlUI;
48 MappingUI mappingUI;
49 PandaDriver pandaFront;
50 PandaDriver pandaRear;
51 boolean mappingMode = false;
52
53 boolean pandaBoardsEnabled = false;
54
55 boolean debugMode = false;
56
57 // Camera variables
58 float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
59
60 void setup() {
61 startMillis = lastMillis = millis();
62
63 // Initialize the Processing graphics environment
64 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
65 frameRate(targetFramerate);
66 noSmooth();
67 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
68 logTime("Created viewport");
69
70 // Create the GLucose engine to run the cubes
71 glucose = new GLucose(this, new SCMapping());
72 lx = glucose.lx;
73 lx.enableKeyboardTempo();
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");
81 glucose.setTransitions(transitions = transitions(glucose));
82 logTime("Built transitions");
83
84 // Build output driver
85 int[][] frontChannels = glucose.mapping.buildFrontChannelList();
86 int[][] rearChannels = glucose.mapping.buildRearChannelList();
87 int[][] flippedRGB = glucose.mapping.buildFlippedRGBList();
88 mappingTool = new MappingTool(glucose, frontChannels, rearChannels);
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");
92
93 // Build overlay UI
94 ui = controlUI = new ControlUI();
95 mappingUI = new MappingUI(mappingTool);
96 logTime("Built overlay UI");
97
98 // MIDI devices
99 for (MidiInputDevice d : RWMidi.getInputDevices()) {
100 d.createInput(this);
101 }
102 SCMidiDevices.initializeStandardDevices(glucose);
103 logTime("Setup MIDI devices");
104
105 // Setup camera
106 midX = glucose.model.xMax/2 + 20;
107 midY = glucose.model.yMax/2;
108 midZ = glucose.model.zMax/2;
109 eyeR = -270;
110 eyeA = .15;
111 eyeY = midY + 20;
112 eyeX = midX + eyeR*sin(eyeA);
113 eyeZ = midZ + eyeR*cos(eyeA);
114 addMouseWheelListener(new java.awt.event.MouseWheelListener() {
115 public void mouseWheelMoved(java.awt.event.MouseWheelEvent mwe) {
116 mouseWheel(mwe.getWheelRotation());
117 }});
118
119
120 println("Total setup: " + (millis() - startMillis) + "ms");
121 println("Hit the 'p' key to toggle Panda Board output");
122 }
123
124 void controllerChangeReceived(rwmidi.Controller cc) {
125 if (debugMode) {
126 println("CC: " + cc.toString());
127 }
128 }
129
130 void noteOnReceived(Note note) {
131 if (debugMode) {
132 println("Note On: " + note.toString());
133 }
134 }
135
136 void noteOffReceived(Note note) {
137 if (debugMode) {
138 println("Note Off: " + note.toString());
139 }
140 }
141
142 void logTime(String evt) {
143 int now = millis();
144 println(evt + ": " + (now - lastMillis) + "ms");
145 lastMillis = now;
146 }
147
148 void draw() {
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();
183
184 // TODO(mcslee): move into GLucose engine
185 if (pandaBoardsEnabled) {
186 pandaFront.send(colors);
187 pandaRear.send(colors);
188 }
189 }
190
191 void drawUI() {
192 if (uiOn) {
193 ui.draw();
194 } else {
195 ui.drawHelpTip();
196 }
197 ui.drawFPS();
198 }
199
200 boolean uiOn = true;
201 int restoreToIndex = -1;
202
203 void keyPressed() {
204 if (mappingMode) {
205 mappingTool.keyPressed();
206 }
207 switch (key) {
208 case '-':
209 case '_':
210 frameRate(--targetFramerate);
211 break;
212 case '=':
213 case '+':
214 frameRate(++targetFramerate);
215 break;
216 case 'd':
217 debugMode = !debugMode;
218 println("Debug output: " + (debugMode ? "ON" : "OFF"));
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;
237 case 'p':
238 pandaBoardsEnabled = !pandaBoardsEnabled;
239 println("PandaBoard Output: " + (pandaBoardsEnabled ? "ON" : "OFF"));
240 break;
241 case 'u':
242 uiOn = !uiOn;
243 break;
244 }
245 }
246
247 int mx, my;
248
249 void mousePressed() {
250 if (mouseX > ui.leftPos) {
251 ui.mousePressed();
252 } else {
253 mx = mouseX;
254 my = mouseY;
255 }
256 }
257
258 void 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
273 void mouseReleased() {
274 if (mouseX > ui.leftPos) {
275 ui.mouseReleased();
276 }
277 }
278
279 void mouseWheel(int delta) {
280 eyeR = constrain(eyeR - delta, -500, -80);
281 eyeX = midX + eyeR*sin(eyeA);
282 eyeZ = midZ + eyeR*cos(eyeA);
283 }
284