1 import java.lang.reflect.*;
4 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
7 * ///\\\ ///\\\ ///\\\ ///\\\
8 * \\\/// \\\/// \\\/// \\\///
11 * EXPERTS ONLY!! EXPERTS ONLY!!
13 * Overlay UI that indicates pattern control, etc. This will be moved
14 * into the Processing library once it is stabilized and need not be
19 private final PFont titleFont = createFont("Myriad Pro", 10);
20 private final PFont itemFont = createFont("Lucida Grande", 11);
21 private final PFont knobFont = titleFont;
22 private final int w = 140;
23 private final int leftPos;
24 private final int leftTextPos;
25 private final int lineHeight = 20;
26 private final int sectionSpacing = 12;
27 private final int controlSpacing = 18;
28 private final int tempoHeight = 20;
29 private final int knobSize = 28;
30 private final float knobIndent = .4;
31 private final int knobSpacing = 6;
32 private final int knobLabelHeight = 14;
33 private final color lightBlue = #666699;
34 private final color lightGreen = #669966;
36 private final String[] patternNames;
37 private final String[] transitionNames;
38 private final String[] effectNames;
42 private int firstPatternY;
43 private int firstPatternKnobY;
44 private int firstTransitionY;
45 private int firstTransitionKnobY;
46 private int firstEffectY;
47 private int firstEffectKnobY;
51 private Method patternStateMethod;
52 private Method transitionStateMethod;
53 private Method effectStateMethod;
55 private final int NUM_PATTERN_KNOBS = 8;
56 private final int NUM_TRANSITION_KNOBS = 4;
57 private final int NUM_EFFECT_KNOBS = 4;
59 private int activeTransitionIndex = 0;
60 private int activeEffectIndex = 0;
62 public final VirtualPatternKnob[] patternKnobs;
63 public final VirtualTransitionKnob[] transitionKnobs;
64 public final VirtualEffectKnob[] effectKnobs;
68 leftTextPos = leftPos + 4;
69 logo = loadImage("logo-sm.png");
71 patternNames = classNameArray(patterns, "Pattern");
72 transitionNames = classNameArray(transitions, "Transition");
73 effectNames = classNameArray(effects, "Effect");
75 patternKnobs = new VirtualPatternKnob[NUM_PATTERN_KNOBS];
76 for (int i = 0; i < patternKnobs.length; ++i) {
77 patternKnobs[i] = new VirtualPatternKnob(i);
80 transitionKnobs = new VirtualTransitionKnob[NUM_TRANSITION_KNOBS];
81 for (int i = 0; i < transitionKnobs.length; ++i) {
82 transitionKnobs[i] = new VirtualTransitionKnob(i);
85 effectKnobs = new VirtualEffectKnob[NUM_EFFECT_KNOBS];
86 for (int i = 0; i < effectKnobs.length; ++i) {
87 effectKnobs[i] = new VirtualEffectKnob(i);
91 patternStateMethod = getClass().getMethod("getState", LXPattern.class);
92 effectStateMethod = getClass().getMethod("getState", LXEffect.class);
93 transitionStateMethod = getClass().getMethod("getState", LXTransition.class);
94 } catch (Exception x) {
95 throw new RuntimeException(x);
102 text("Tap 'u' to restore UI", width-4, height-6);
108 stroke(color(0, 0, 100));
109 // fill(color(0, 0, 50, 50)); // alpha is bad for perf
110 fill(color(0, 0, 30));
111 rect(leftPos-1, -1, w+2, height+2);
114 firstPatternY = yPos + lineHeight + 6;
115 yPos = drawObjectList(yPos, "PATTERN", patterns, patternNames, patternStateMethod);
116 yPos += controlSpacing;
117 firstPatternKnobY = yPos;
118 int xPos = leftTextPos;
119 for (int i = 0; i < NUM_PATTERN_KNOBS/2; ++i) {
120 drawKnob(xPos, yPos, knobSize, patternKnobs[i]);
121 drawKnob(xPos, yPos + knobSize + knobSpacing + knobLabelHeight, knobSize, patternKnobs[NUM_PATTERN_KNOBS/2 + i]);
122 xPos += knobSize + knobSpacing;
124 yPos += 2*(knobSize + knobLabelHeight) + knobSpacing;
126 yPos += sectionSpacing;
127 firstTransitionY = yPos + lineHeight + 6;
128 yPos = drawObjectList(yPos, "TRANSITION", transitions, transitionNames, transitionStateMethod);
129 yPos += controlSpacing;
130 firstTransitionKnobY = yPos;
132 for (int i = 0; i < transitionKnobs.length; ++i) {
133 drawKnob(xPos, yPos, knobSize, transitionKnobs[i]);
134 xPos += knobSize + knobSpacing;
136 yPos += knobSize + knobLabelHeight;
138 yPos += sectionSpacing;
139 firstEffectY = yPos + lineHeight + 6;
140 yPos = drawObjectList(yPos, "FX", effects, effectNames, effectStateMethod);
141 yPos += controlSpacing;
142 firstEffectKnobY = yPos;
144 for (int i = 0; i < effectKnobs.length; ++i) {
145 drawKnob(xPos, yPos, knobSize, effectKnobs[i]);
146 xPos += knobSize + knobSpacing;
148 yPos += knobSize + knobLabelHeight;
150 yPos += sectionSpacing;
151 yPos = drawObjectList(yPos, "TEMPO", null, null, null);
155 fill(tempoDown ? lightGreen : color(0, 0, 35 - 8*lx.tempo.rampf()));
156 rect(leftPos + 4, yPos, w - 8, tempoHeight);
159 text("" + ((int)(lx.tempo.bpmf() * 100) / 100.), leftPos + w/2., yPos + tempoHeight - 6);
165 text("Tap 'u' to hide UI", leftTextPos, height-6);
168 public LXParameter getOrNull(List<LXParameter> items, int index) {
169 if (index < items.size()) {
170 return items.get(index);
175 public void drawFPS() {
179 text("FPS: " + (((int)(frameRate * 10)) / 10.), 4, height-6);
182 private final int STATE_DEFAULT = 0;
183 private final int STATE_ACTIVE = 1;
184 private final int STATE_PENDING = 2;
186 public int getState(LXPattern p) {
187 if (p == lx.getPattern()) {
189 } else if (p == lx.getNextPattern()) {
190 return STATE_PENDING;
192 return STATE_DEFAULT;
195 public int getState(LXEffect e) {
197 return STATE_PENDING;
198 } else if (effects[activeEffectIndex] == e) {
201 return STATE_DEFAULT;
204 public int getState(LXTransition t) {
205 if (t == lx.getTransition()) {
206 return STATE_PENDING;
207 } else if (t == transitions[activeTransitionIndex]) {
210 return STATE_DEFAULT;
213 protected int drawObjectList(int yPos, String title, Object[] items, Method stateMethod) {
214 return drawObjectList(yPos, title, items, classNameArray(items, null), stateMethod);
217 private int drawObjectList(int yPos, String title, Object[] items, String[] names, Method stateMethod) {
222 text(title, leftTextPos, yPos += lineHeight);
227 for (int i = 0; i < items.length; ++i) {
229 int state = STATE_DEFAULT;
231 state = ((Integer) stateMethod.invoke(this, o)).intValue();
232 } catch (Exception x) {
233 throw new RuntimeException(x);
242 textColor = color(0, 0, 75 + 15*sin(millis()/200.));;
246 fill(even ? #666666 : #777777);
249 rect(leftPos, yPos+6, width, lineHeight);
251 text(names[i], leftTextPos, yPos += lineHeight);
258 private void drawKnob(int xPos, int yPos, int knobSize, LXParameter knob) {
262 final float knobValue = knob.getValuef();
263 String knobLabel = knob.getLabel();
264 if (knobLabel == null) {
266 } else if (knobLabel.length() > 4) {
267 knobLabel = knobLabel.substring(0, 4);
273 // For some reason this arc call really crushes drawing performance. Presumably
274 // because openGL is drawing it and when we overlap the second set of arcs it
275 // does a bunch of depth buffer intersection tests? Ellipse with a trapezoid cut out is faster
276 // arc(xPos + knobSize/2, yPos + knobSize/2, knobSize, knobSize, HALF_PI + knobIndent, HALF_PI + knobIndent + (TWO_PI-2*knobIndent));
277 ellipse(xPos + knobSize/2, yPos + knobSize/2, knobSize, knobSize);
279 float endArc = HALF_PI + knobIndent + (TWO_PI-2*knobIndent)*knobValue;
281 arc(xPos + knobSize/2, yPos + knobSize/2, knobSize, knobSize, HALF_PI + knobIndent, endArc);
283 // Mask notch out of knob
284 fill(color(0, 0, 30));
286 vertex(xPos + knobSize/2, yPos + knobSize/2.);
287 vertex(xPos + knobSize/2 - 6, yPos + knobSize);
288 vertex(xPos + knobSize/2 + 6, yPos + knobSize);
291 // Center circle of knob
293 ellipse(xPos + knobSize/2, yPos + knobSize/2, knobSize/2, knobSize/2);
296 rect(xPos, yPos + knobSize + 2, knobSize, knobLabelHeight - 2);
300 text(knobLabel, xPos + knobSize/2, yPos + knobSize + knobLabelHeight - 2);
304 private String[] classNameArray(Object[] objects, String suffix) {
305 if (objects == null) {
308 String[] names = new String[objects.length];
309 for (int i = 0; i < objects.length; ++i) {
310 names[i] = className(objects[i], suffix);
315 private String className(Object p, String suffix) {
316 String s = p.getClass().getName();
318 if ((li = s.lastIndexOf(".")) > 0) {
319 s = s.substring(li + 1);
321 if (s.indexOf("SugarCubes$") == 0) {
322 s = s.substring("SugarCubes$".length());
324 if ((suffix != null) && ((li = s.indexOf(suffix)) != -1)) {
325 s = s.substring(0, li);
330 class VirtualPatternKnob extends LXVirtualParameter {
331 private final int index;
333 VirtualPatternKnob(int index) {
337 public LXParameter getRealParameter() {
338 List<LXParameter> parameters = glucose.getPattern().getParameters();
339 if (index < parameters.size()) {
340 return parameters.get(index);
346 class VirtualTransitionKnob extends LXVirtualParameter {
347 private final int index;
349 VirtualTransitionKnob(int index) {
353 public LXParameter getRealParameter() {
354 List<LXParameter> parameters = transitions[activeTransitionIndex].getParameters();
355 if (index < parameters.size()) {
356 return parameters.get(index);
362 class VirtualEffectKnob extends LXVirtualParameter {
363 private final int index;
365 VirtualEffectKnob(int index) {
369 public LXParameter getRealParameter() {
370 List<LXParameter> parameters = effects[activeEffectIndex].getParameters();
371 if (index < parameters.size()) {
372 return parameters.get(index);
378 private int patternKnobIndex = -1;
379 private int transitionKnobIndex = -1;
380 private int effectKnobIndex = -1;
383 private int releaseEffect = -1;
384 private boolean tempoDown = false;
386 public void mousePressed() {
388 patternKnobIndex = transitionKnobIndex = effectKnobIndex = -1;
390 if (mouseY > tempoY) {
391 if (mouseY - tempoY < tempoHeight) {
395 } else if ((mouseY >= firstEffectKnobY) && (mouseY < firstEffectKnobY + knobSize + knobLabelHeight)) {
396 effectKnobIndex = (mouseX - leftTextPos) / (knobSize + knobSpacing);
397 } else if (mouseY > firstEffectY) {
398 int effectIndex = (mouseY - firstEffectY) / lineHeight;
399 if (effectIndex < effects.length) {
400 if (activeEffectIndex == effectIndex) {
401 effects[effectIndex].enable();
402 releaseEffect = effectIndex;
404 activeEffectIndex = effectIndex;
406 } else if ((mouseY >= firstTransitionKnobY) && (mouseY < firstTransitionKnobY + knobSize + knobLabelHeight)) {
407 transitionKnobIndex = (mouseX - leftTextPos) / (knobSize + knobSpacing);
408 } else if (mouseY > firstTransitionY) {
409 int transitionIndex = (mouseY - firstTransitionY) / lineHeight;
410 if (transitionIndex < transitions.length) {
411 activeTransitionIndex = transitionIndex;
413 } else if ((mouseY >= firstPatternKnobY) && (mouseY < firstPatternKnobY + 2*(knobSize+knobLabelHeight) + knobSpacing)) {
414 patternKnobIndex = (mouseX - leftTextPos) / (knobSize + knobSpacing);
415 if (mouseY >= firstPatternKnobY + knobSize + knobLabelHeight + knobSpacing) {
416 patternKnobIndex += NUM_PATTERN_KNOBS / 2;
418 } else if (mouseY > firstPatternY) {
419 int patternIndex = (mouseY - firstPatternY) / lineHeight;
420 if (patternIndex < patterns.length) {
421 patterns[patternIndex].setTransition(transitions[activeTransitionIndex]);
422 lx.goIndex(patternIndex);
427 public void mouseDragged() {
428 int dy = lastY - mouseY;
430 if (patternKnobIndex >= 0 && patternKnobIndex < NUM_PATTERN_KNOBS) {
431 LXParameter p = patternKnobs[patternKnobIndex];
432 p.setValue(constrain(p.getValuef() + dy*.01, 0, 1));
433 } else if (effectKnobIndex >= 0 && effectKnobIndex < NUM_EFFECT_KNOBS) {
434 LXParameter p = effectKnobs[effectKnobIndex];
435 p.setValue(constrain(p.getValuef() + dy*.01, 0, 1));
436 } else if (transitionKnobIndex >= 0 && transitionKnobIndex < NUM_TRANSITION_KNOBS) {
437 LXParameter p = transitionKnobs[transitionKnobIndex];
438 p.setValue(constrain(p.getValuef() + dy*.01, 0, 1));
442 public void mouseReleased() {
444 if (releaseEffect >= 0) {
445 effects[releaseEffect].trigger();
452 void mousePressed() {
453 if (mouseX > ui.leftPos) {
458 void mouseReleased() {
459 if (mouseX > ui.leftPos) {
464 void mouseDragged() {
465 if (mouseX > ui.leftPos) {