Add framerate UI and control on -/+ keys
[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 = 30;
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 void setup() {
58 startMillis = lastMillis = millis();
59
60 // Initialize the Processing graphics environment
61 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
62 frameRate(targetFramerate);
63 noSmooth();
64 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
65 logTime("Created viewport");
66
67 // Create the GLucose engine to run the cubes
68 glucose = new GLucose(this, new SCMapping());
69 lx = glucose.lx;
70 lx.enableKeyboardTempo();
71 logTime("Built GLucose engine");
72
73 // Set the patterns
74 glucose.lx.setPatterns(patterns = patterns(glucose));
75 logTime("Built patterns");
76 glucose.lx.addEffects(effects = effects(glucose));
77 logTime("Built effects");
78 glucose.setTransitions(transitions = transitions(glucose));
79 logTime("Built transitions");
80
81 // Build output driver
82 int[][] frontChannels = glucose.mapping.buildFrontChannelList();
83 int[][] rearChannels = glucose.mapping.buildRearChannelList();
84 int[][] flippedRGB = glucose.mapping.buildFlippedRGBList();
85 mappingTool = new MappingTool(glucose, frontChannels, rearChannels);
86 pandaFront = new PandaDriver(new NetAddress("192.168.1.28", 9001), glucose.model, frontChannels, flippedRGB);
87 pandaRear = new PandaDriver(new NetAddress("192.168.1.29", 9001), glucose.model, rearChannels, flippedRGB);
88 logTime("Build PandaDriver");
89
90 // Build overlay UI
91 ui = controlUI = new ControlUI();
92 mappingUI = new MappingUI(mappingTool);
93 logTime("Built overlay UI");
94
95 // MIDI devices
96 for (MidiInputDevice d : RWMidi.getInputDevices()) {
97 d.createInput(this);
98 }
99 SCMidiDevices.initializeStandardDevices(glucose);
100 logTime("Setup MIDI devices");
101
102 println("Total setup: " + (millis() - startMillis) + "ms");
103 println("Hit the 'p' key to toggle Panda Board output");
104 }
105
106 void controllerChangeReceived(rwmidi.Controller cc) {
107 if (debugMode) {
108 println("CC: " + cc.toString());
109 }
110 }
111
112 void noteOnReceived(Note note) {
113 if (debugMode) {
114 println("Note On: " + note.toString());
115 }
116 }
117
118 void noteOffReceived(Note note) {
119 if (debugMode) {
120 println("Note Off: " + note.toString());
121 }
122 }
123
124 void logTime(String evt) {
125 int now = millis();
126 println(evt + ": " + (now - lastMillis) + "ms");
127 lastMillis = now;
128 }
129
130 void draw() {
131 // The glucose engine deals with the core simulation here, we don't need
132 // to do anything specific. This method just needs to exist.
133
134 // TODO(mcslee): move into GLucose engine
135 if (pandaBoardsEnabled) {
136 color[] colors = glucose.getColors();
137 pandaFront.send(colors);
138 pandaRear.send(colors);
139 }
140 }
141
142 void drawUI() {
143 if (uiOn) {
144 ui.draw();
145 } else {
146 ui.drawHelpTip();
147 }
148 ui.drawFPS();
149 }
150
151 boolean uiOn = true;
152 int restoreToIndex = -1;
153
154 void keyPressed() {
155 if (mappingMode) {
156 mappingTool.keyPressed();
157 }
158 switch (key) {
159 case '-':
160 case '_':
161 frameRate(--targetFramerate);
162 break;
163 case '=':
164 case '+':
165 frameRate(++targetFramerate);
166 break;
167 case 'd':
168 debugMode = !debugMode;
169 println("Debug output: " + (debugMode ? "ON" : "OFF"));
170 case 'm':
171 mappingMode = !mappingMode;
172 if (mappingMode) {
173 LXPattern pattern = lx.getPattern();
174 for (int i = 0; i < patterns.length; ++i) {
175 if (pattern == patterns[i]) {
176 restoreToIndex = i;
177 break;
178 }
179 }
180 ui = mappingUI;
181 lx.setPatterns(new LXPattern[] { mappingTool });
182 } else {
183 ui = controlUI;
184 lx.setPatterns(patterns);
185 lx.goIndex(restoreToIndex);
186 }
187 break;
188 case 'p':
189 pandaBoardsEnabled = !pandaBoardsEnabled;
190 println("PandaBoard Output: " + (pandaBoardsEnabled ? "ON" : "OFF"));
191 break;
192 case 'u':
193 uiOn = !uiOn;
194 break;
195 }
196 }
197
198