2 * Overlay UI that indicates pattern control, etc. This will be moved
3 * into the Processing library once it is stabilized and need not be
8 private final PFont titleFont = createFont("Myriad Pro", 10);
9 private final PFont itemFont = createFont("Lucida Grande", 11);
10 private final PFont knobFont = titleFont;
11 private final int w = 140;
12 private final int leftPos;
13 private final int leftTextPos;
14 private final int lineHeight = 20;
15 private final int sectionSpacing = 12;
16 private final int controlSpacing = 18;
17 private final int tempoHeight = 20;
18 private final int knobSize = 28;
19 private final float knobIndent = .4;
20 private final int knobSpacing = 6;
21 private final int knobLabelHeight = 14;
22 private final color lightBlue = #666699;
23 private final color lightGreen = #669966;
25 private final String[] patternNames;
26 private final String[] transitionNames;
27 private final String[] effectNames;
31 private int firstPatternY;
32 private int firstPatternKnobY;
33 private int firstTransitionY;
34 private int firstTransitionKnobY;
35 private int firstEffectY;
36 private int firstEffectKnobY;
40 private Method patternStateMethod;
41 private Method transitionStateMethod;
42 private Method effectStateMethod;
44 private final int NUM_PATTERN_KNOBS = 8;
45 private final int NUM_TRANSITION_KNOBS = 4;
46 private final int NUM_EFFECT_KNOBS = 4;
48 private int activeTransitionIndex = 0;
49 private int activeEffectIndex = 0;
51 public final VirtualPatternKnob[] patternKnobs;
52 public final VirtualTransitionKnob[] transitionKnobs;
53 public final VirtualEffectKnob[] effectKnobs;
57 leftTextPos = leftPos + 4;
58 logo = loadImage("logo-sm.png");
60 patternNames = classNameArray(patterns, "Pattern");
61 transitionNames = classNameArray(transitions, "Transition");
62 effectNames = classNameArray(effects, "Effect");
64 patternKnobs = new VirtualPatternKnob[NUM_PATTERN_KNOBS];
65 for (int i = 0; i < patternKnobs.length; ++i) {
66 patternKnobs[i] = new VirtualPatternKnob(i);
69 transitionKnobs = new VirtualTransitionKnob[NUM_TRANSITION_KNOBS];
70 for (int i = 0; i < transitionKnobs.length; ++i) {
71 transitionKnobs[i] = new VirtualTransitionKnob(i);
74 effectKnobs = new VirtualEffectKnob[NUM_EFFECT_KNOBS];
75 for (int i = 0; i < effectKnobs.length; ++i) {
76 effectKnobs[i] = new VirtualEffectKnob(i);
80 patternStateMethod = getClass().getMethod("getState", LXPattern.class);
81 effectStateMethod = getClass().getMethod("getState", LXEffect.class);
82 transitionStateMethod = getClass().getMethod("getState", LXTransition.class);
83 } catch (Exception x) {
84 throw new RuntimeException(x);
91 text("Tap 'u' to restore UI", width-4, height-6);
97 stroke(color(0, 0, 100));
98 // fill(color(0, 0, 50, 50)); // alpha is bad for perf
99 fill(color(0, 0, 30));
100 rect(leftPos-1, -1, w+2, height+2);
103 firstPatternY = yPos + lineHeight + 6;
104 yPos = drawObjectList(yPos, "PATTERN", patterns, patternNames, patternStateMethod);
105 yPos += controlSpacing;
106 firstPatternKnobY = yPos;
107 int xPos = leftTextPos;
108 for (int i = 0; i < NUM_PATTERN_KNOBS/2; ++i) {
109 drawKnob(xPos, yPos, knobSize, patternKnobs[i]);
110 drawKnob(xPos, yPos + knobSize + knobSpacing + knobLabelHeight, knobSize, patternKnobs[NUM_PATTERN_KNOBS/2 + i]);
111 xPos += knobSize + knobSpacing;
113 yPos += 2*(knobSize + knobLabelHeight) + knobSpacing;
115 yPos += sectionSpacing;
116 firstTransitionY = yPos + lineHeight + 6;
117 yPos = drawObjectList(yPos, "TRANSITION", transitions, transitionNames, transitionStateMethod);
118 yPos += controlSpacing;
119 firstTransitionKnobY = yPos;
121 for (int i = 0; i < transitionKnobs.length; ++i) {
122 drawKnob(xPos, yPos, knobSize, transitionKnobs[i]);
123 xPos += knobSize + knobSpacing;
125 yPos += knobSize + knobLabelHeight;
127 yPos += sectionSpacing;
128 firstEffectY = yPos + lineHeight + 6;
129 yPos = drawObjectList(yPos, "FX", effects, effectNames, effectStateMethod);
130 yPos += controlSpacing;
131 firstEffectKnobY = yPos;
133 for (int i = 0; i < effectKnobs.length; ++i) {
134 drawKnob(xPos, yPos, knobSize, effectKnobs[i]);
135 xPos += knobSize + knobSpacing;
137 yPos += knobSize + knobLabelHeight;
139 yPos += sectionSpacing;
140 yPos = drawObjectList(yPos, "TEMPO", null, null, null);
144 fill(tempoDown ? lightGreen : color(0, 0, 35 - 8*lx.tempo.rampf()));
145 rect(leftPos + 4, yPos, w - 8, tempoHeight);
148 text("" + ((int)(lx.tempo.bpmf() * 100) / 100.), leftPos + w/2., yPos + tempoHeight - 6);
154 text("Tap 'u' to hide UI", leftTextPos, height-6);
157 public LXParameter getOrNull(List<LXParameter> items, int index) {
158 if (index < items.size()) {
159 return items.get(index);
164 public void drawFPS() {
168 text("FPS: " + (((int)(frameRate * 10)) / 10.), 4, height-6);
171 private final int STATE_DEFAULT = 0;
172 private final int STATE_ACTIVE = 1;
173 private final int STATE_PENDING = 2;
175 public int getState(LXPattern p) {
176 if (p == lx.getPattern()) {
178 } else if (p == lx.getNextPattern()) {
179 return STATE_PENDING;
181 return STATE_DEFAULT;
184 public int getState(LXEffect e) {
186 return STATE_PENDING;
187 } else if (effects[activeEffectIndex] == e) {
190 return STATE_DEFAULT;
193 public int getState(LXTransition t) {
194 if (t == lx.getTransition()) {
195 return STATE_PENDING;
196 } else if (t == transitions[activeTransitionIndex]) {
199 return STATE_DEFAULT;
202 protected int drawObjectList(int yPos, String title, Object[] items, Method stateMethod) {
203 return drawObjectList(yPos, title, items, classNameArray(items, null), stateMethod);
206 private int drawObjectList(int yPos, String title, Object[] items, String[] names, Method stateMethod) {
211 text(title, leftTextPos, yPos += lineHeight);
216 for (int i = 0; i < items.length; ++i) {
218 int state = STATE_DEFAULT;
220 state = ((Integer) stateMethod.invoke(this, o)).intValue();
221 } catch (Exception x) {
222 throw new RuntimeException(x);
231 textColor = color(0, 0, 75 + 15*sin(millis()/200.));;
235 fill(even ? #666666 : #777777);
238 rect(leftPos, yPos+6, width, lineHeight);
240 text(names[i], leftTextPos, yPos += lineHeight);
247 private void drawKnob(int xPos, int yPos, int knobSize, LXParameter knob) {
251 final float knobValue = knob.getValuef();
252 String knobLabel = knob.getLabel();
253 if (knobLabel == null) {
255 } else if (knobLabel.length() > 4) {
256 knobLabel = knobLabel.substring(0, 4);
262 // For some reason this arc call really crushes drawing performance. Presumably
263 // because openGL is drawing it and when we overlap the second set of arcs it
264 // does a bunch of depth buffer intersection tests? Ellipse with a trapezoid cut out is faster
265 // arc(xPos + knobSize/2, yPos + knobSize/2, knobSize, knobSize, HALF_PI + knobIndent, HALF_PI + knobIndent + (TWO_PI-2*knobIndent));
266 ellipse(xPos + knobSize/2, yPos + knobSize/2, knobSize, knobSize);
268 float endArc = HALF_PI + knobIndent + (TWO_PI-2*knobIndent)*knobValue;
270 arc(xPos + knobSize/2, yPos + knobSize/2, knobSize, knobSize, HALF_PI + knobIndent, endArc);
272 // Mask notch out of knob
273 fill(color(0, 0, 30));
275 vertex(xPos + knobSize/2, yPos + knobSize/2.);
276 vertex(xPos + knobSize/2 - 6, yPos + knobSize);
277 vertex(xPos + knobSize/2 + 6, yPos + knobSize);
280 // Center circle of knob
282 ellipse(xPos + knobSize/2, yPos + knobSize/2, knobSize/2, knobSize/2);
285 rect(xPos, yPos + knobSize + 2, knobSize, knobLabelHeight - 2);
289 text(knobLabel, xPos + knobSize/2, yPos + knobSize + knobLabelHeight - 2);
293 private String[] classNameArray(Object[] objects, String suffix) {
294 if (objects == null) {
297 String[] names = new String[objects.length];
298 for (int i = 0; i < objects.length; ++i) {
299 names[i] = className(objects[i], suffix);
304 private String className(Object p, String suffix) {
305 String s = p.getClass().getName();
307 if ((li = s.lastIndexOf(".")) > 0) {
308 s = s.substring(li + 1);
310 if (s.indexOf("SugarCubes$") == 0) {
311 s = s.substring("SugarCubes$".length());
313 if ((suffix != null) && ((li = s.indexOf(suffix)) != -1)) {
314 s = s.substring(0, li);
319 class VirtualPatternKnob extends LXVirtualParameter {
320 private final int index;
322 VirtualPatternKnob(int index) {
326 public LXParameter getRealParameter() {
327 List<LXParameter> parameters = glucose.getPattern().getParameters();
328 if (index < parameters.size()) {
329 return parameters.get(index);
335 class VirtualTransitionKnob extends LXVirtualParameter {
336 private final int index;
338 VirtualTransitionKnob(int index) {
342 public LXParameter getRealParameter() {
343 List<LXParameter> parameters = transitions[activeTransitionIndex].getParameters();
344 if (index < parameters.size()) {
345 return parameters.get(index);
351 class VirtualEffectKnob extends LXVirtualParameter {
352 private final int index;
354 VirtualEffectKnob(int index) {
358 public LXParameter getRealParameter() {
359 List<LXParameter> parameters = effects[activeEffectIndex].getParameters();
360 if (index < parameters.size()) {
361 return parameters.get(index);
367 private int patternKnobIndex = -1;
368 private int transitionKnobIndex = -1;
369 private int effectKnobIndex = -1;
372 private int releaseEffect = -1;
373 private boolean tempoDown = false;
375 public void mousePressed() {
377 patternKnobIndex = transitionKnobIndex = effectKnobIndex = -1;
379 if (mouseY > tempoY) {
380 if (mouseY - tempoY < tempoHeight) {
384 } else if ((mouseY >= firstEffectKnobY) && (mouseY < firstEffectKnobY + knobSize + knobLabelHeight)) {
385 effectKnobIndex = (mouseX - leftTextPos) / (knobSize + knobSpacing);
386 } else if (mouseY > firstEffectY) {
387 int effectIndex = (mouseY - firstEffectY) / lineHeight;
388 if (effectIndex < effects.length) {
389 if (activeEffectIndex == effectIndex) {
390 effects[effectIndex].enable();
391 releaseEffect = effectIndex;
393 activeEffectIndex = effectIndex;
395 } else if ((mouseY >= firstTransitionKnobY) && (mouseY < firstTransitionKnobY + knobSize + knobLabelHeight)) {
396 transitionKnobIndex = (mouseX - leftTextPos) / (knobSize + knobSpacing);
397 } else if (mouseY > firstTransitionY) {
398 int transitionIndex = (mouseY - firstTransitionY) / lineHeight;
399 if (transitionIndex < transitions.length) {
400 activeTransitionIndex = transitionIndex;
402 } else if ((mouseY >= firstPatternKnobY) && (mouseY < firstPatternKnobY + 2*(knobSize+knobLabelHeight) + knobSpacing)) {
403 patternKnobIndex = (mouseX - leftTextPos) / (knobSize + knobSpacing);
404 if (mouseY >= firstPatternKnobY + knobSize + knobLabelHeight + knobSpacing) {
405 patternKnobIndex += NUM_PATTERN_KNOBS / 2;
407 } else if (mouseY > firstPatternY) {
408 int patternIndex = (mouseY - firstPatternY) / lineHeight;
409 if (patternIndex < patterns.length) {
410 patterns[patternIndex].setTransition(transitions[activeTransitionIndex]);
411 lx.goIndex(patternIndex);
416 public void mouseDragged() {
417 int dy = lastY - mouseY;
419 if (patternKnobIndex >= 0 && patternKnobIndex < NUM_PATTERN_KNOBS) {
420 LXParameter p = patternKnobs[patternKnobIndex];
421 p.setValue(constrain(p.getValuef() + dy*.01, 0, 1));
422 } else if (effectKnobIndex >= 0 && effectKnobIndex < NUM_EFFECT_KNOBS) {
423 LXParameter p = effectKnobs[effectKnobIndex];
424 p.setValue(constrain(p.getValuef() + dy*.01, 0, 1));
425 } else if (transitionKnobIndex >= 0 && transitionKnobIndex < NUM_TRANSITION_KNOBS) {
426 LXParameter p = transitionKnobs[transitionKnobIndex];
427 p.setValue(constrain(p.getValuef() + dy*.01, 0, 1));
431 public void mouseReleased() {
433 if (releaseEffect >= 0) {
434 effects[releaseEffect].trigger();
441 void mousePressed() {
442 if (mouseX > ui.leftPos) {
447 void mouseReleased() {
448 if (mouseX > ui.leftPos) {
453 void mouseDragged() {
454 if (mouseX > ui.leftPos) {