Add framerate UI and control on -/+ keys
[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
MS
36
37int targetFramerate = 30;
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
49815cc0
MS
57void setup() {
58 startMillis = lastMillis = millis();
59
60 // Initialize the Processing graphics environment
61 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
0e3c5542 62 frameRate(targetFramerate);
3f8be614 63 noSmooth();
49815cc0
MS
64 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
65 logTime("Created viewport");
66
67 // Create the GLucose engine to run the cubes
1ecdb44a 68 glucose = new GLucose(this, new SCMapping());
49815cc0 69 lx = glucose.lx;
cc9fcf4b 70 lx.enableKeyboardTempo();
49815cc0
MS
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");
cc9fcf4b 78 glucose.setTransitions(transitions = transitions(glucose));
49815cc0 79 logTime("Built transitions");
e73ef85d
MS
80
81 // Build output driver
82 int[][] frontChannels = glucose.mapping.buildFrontChannelList();
83 int[][] rearChannels = glucose.mapping.buildRearChannelList();
84 int[][] flippedRGB = glucose.mapping.buildFlippedRGBList();
2bae07c9 85 mappingTool = new MappingTool(glucose, frontChannels, rearChannels);
e73ef85d
MS
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");
49815cc0
MS
89
90 // Build overlay UI
bf551144
MS
91 ui = controlUI = new ControlUI();
92 mappingUI = new MappingUI(mappingTool);
49815cc0 93 logTime("Built overlay UI");
1ecdb44a 94
49815cc0 95 // MIDI devices
cc9fcf4b
MS
96 for (MidiInputDevice d : RWMidi.getInputDevices()) {
97 d.createInput(this);
98 }
99 SCMidiDevices.initializeStandardDevices(glucose);
3f8be614 100 logTime("Setup MIDI devices");
49815cc0
MS
101
102 println("Total setup: " + (millis() - startMillis) + "ms");
e73ef85d 103 println("Hit the 'p' key to toggle Panda Board output");
49815cc0
MS
104}
105
cc9fcf4b
MS
106void controllerChangeReceived(rwmidi.Controller cc) {
107 if (debugMode) {
108 println("CC: " + cc.toString());
109 }
110}
111
112void noteOnReceived(Note note) {
113 if (debugMode) {
114 println("Note On: " + note.toString());
115 }
116}
117
118void noteOffReceived(Note note) {
119 if (debugMode) {
120 println("Note Off: " + note.toString());
121 }
122}
123
49815cc0
MS
124void logTime(String evt) {
125 int now = millis();
126 println(evt + ": " + (now - lastMillis) + "ms");
127 lastMillis = now;
128}
129
130void 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.
e73ef85d
MS
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 }
49815cc0
MS
140}
141
142void drawUI() {
143 if (uiOn) {
144 ui.draw();
145 } else {
146 ui.drawHelpTip();
147 }
148 ui.drawFPS();
149}
150
151boolean uiOn = true;
bf551144
MS
152int restoreToIndex = -1;
153
49815cc0 154void keyPressed() {
bf551144
MS
155 if (mappingMode) {
156 mappingTool.keyPressed();
157 }
3f8be614 158 switch (key) {
0e3c5542
MS
159 case '-':
160 case '_':
161 frameRate(--targetFramerate);
162 break;
163 case '=':
164 case '+':
165 frameRate(++targetFramerate);
166 break;
cc9fcf4b
MS
167 case 'd':
168 debugMode = !debugMode;
169 println("Debug output: " + (debugMode ? "ON" : "OFF"));
bf551144
MS
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;
e73ef85d
MS
188 case 'p':
189 pandaBoardsEnabled = !pandaBoardsEnabled;
190 println("PandaBoard Output: " + (pandaBoardsEnabled ? "ON" : "OFF"));
cc9fcf4b 191 break;
3f8be614
MS
192 case 'u':
193 uiOn = !uiOn;
194 break;
49815cc0
MS
195 }
196}
197
198