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_TRANSITION_KNOBS = 4;
45 private final int NUM_EFFECT_KNOBS = 4;
47 private int activeTransitionIndex = 0;
48 private int activeEffectIndex = 0;
50 private final VirtualTransitionKnob[] transitionKnobs;
51 private final VirtualEffectKnob[] effectKnobs;
55 leftTextPos = leftPos + 4;
56 logo = loadImage("logo-sm.png");
58 patternNames = classNameArray(patterns, "Pattern");
59 transitionNames = classNameArray(transitions, "Transition");
60 effectNames = classNameArray(effects, "Effect");
62 transitionKnobs = new VirtualTransitionKnob[NUM_TRANSITION_KNOBS];
63 for (int i = 0; i < transitionKnobs.length; ++i) {
64 transitionKnobs[i] = new VirtualTransitionKnob(i);
67 effectKnobs = new VirtualEffectKnob[NUM_EFFECT_KNOBS];
68 for (int i = 0; i < effectKnobs.length; ++i) {
69 effectKnobs[i] = new VirtualEffectKnob(i);
73 patternStateMethod = getClass().getMethod("getState", LXPattern.class);
74 effectStateMethod = getClass().getMethod("getState", LXEffect.class);
75 transitionStateMethod = getClass().getMethod("getState", LXTransition.class);
76 } catch (Exception x) {
77 throw new RuntimeException(x);
84 text("Tap 'u' to restore UI", width-4, height-6);
90 stroke(color(0, 0, 100));
91 // fill(color(0, 0, 50, 50)); // alpha is bad for perf
92 fill(color(0, 0, 30));
93 rect(leftPos-1, -1, w+2, height+2);
96 firstPatternY = yPos + lineHeight + 6;
97 yPos = drawObjectList(yPos, "PATTERN", patterns, patternNames, patternStateMethod);
98 yPos += controlSpacing;
99 firstPatternKnobY = yPos;
100 int xPos = leftTextPos;
101 for (int i = 0; i < glucose.NUM_PATTERN_KNOBS/2; ++i) {
102 drawKnob(xPos, yPos, knobSize, glucose.patternKnobs[i]);
103 drawKnob(xPos, yPos + knobSize + knobSpacing + knobLabelHeight, knobSize, glucose.patternKnobs[glucose.NUM_PATTERN_KNOBS/2 + i]);
104 xPos += knobSize + knobSpacing;
106 yPos += 2*(knobSize + knobLabelHeight) + knobSpacing;
108 yPos += sectionSpacing;
109 firstTransitionY = yPos + lineHeight + 6;
110 yPos = drawObjectList(yPos, "TRANSITION", transitions, transitionNames, transitionStateMethod);
111 yPos += controlSpacing;
112 firstTransitionKnobY = yPos;
114 for (int i = 0; i < transitionKnobs.length; ++i) {
115 drawKnob(xPos, yPos, knobSize, transitionKnobs[i]);
116 xPos += knobSize + knobSpacing;
118 yPos += knobSize + knobLabelHeight;
120 yPos += sectionSpacing;
121 firstEffectY = yPos + lineHeight + 6;
122 yPos = drawObjectList(yPos, "FX", effects, effectNames, effectStateMethod);
123 yPos += controlSpacing;
124 firstEffectKnobY = yPos;
126 for (int i = 0; i < effectKnobs.length; ++i) {
127 drawKnob(xPos, yPos, knobSize, effectKnobs[i]);
128 xPos += knobSize + knobSpacing;
130 yPos += knobSize + knobLabelHeight;
132 yPos += sectionSpacing;
133 yPos = drawObjectList(yPos, "TEMPO", null, null, null);
137 fill(tempoDown ? lightGreen : color(0, 0, 35 - 8*lx.tempo.rampf()));
138 rect(leftPos + 4, yPos, w - 8, tempoHeight);
141 text("" + ((int)(lx.tempo.bpmf() * 100) / 100.), leftPos + w/2., yPos + tempoHeight - 6);
147 text("Tap 'u' to hide UI (~+3FPS)", leftTextPos, height-6);
150 public LXParameter getOrNull(List<LXParameter> items, int index) {
151 if (index < items.size()) {
152 return items.get(index);
157 public void drawFPS() {
161 text("FPS: " + (((int)(frameRate * 10)) / 10.), 4, height-6);
164 private final int STATE_DEFAULT = 0;
165 private final int STATE_ACTIVE = 1;
166 private final int STATE_PENDING = 2;
168 public int getState(LXPattern p) {
169 if (p == lx.getPattern()) {
171 } else if (p == lx.getNextPattern()) {
172 return STATE_PENDING;
174 return STATE_DEFAULT;
177 public int getState(LXEffect e) {
179 return STATE_PENDING;
180 } else if (effects[activeEffectIndex] == e) {
183 return STATE_DEFAULT;
186 public int getState(LXTransition t) {
187 if (t == lx.getTransition()) {
188 return STATE_PENDING;
189 } else if (t == transitions[activeTransitionIndex]) {
192 return STATE_DEFAULT;
195 protected int drawObjectList(int yPos, String title, Object[] items, Method stateMethod) {
196 return drawObjectList(yPos, title, items, classNameArray(items, null), stateMethod);
199 private int drawObjectList(int yPos, String title, Object[] items, String[] names, Method stateMethod) {
204 text(title, leftTextPos, yPos += lineHeight);
209 for (int i = 0; i < items.length; ++i) {
211 int state = STATE_DEFAULT;
213 state = ((Integer) stateMethod.invoke(this, o)).intValue();
214 } catch (Exception x) {
215 throw new RuntimeException(x);
224 textColor = color(0, 0, 75 + 15*sin(millis()/200.));;
228 fill(even ? #666666 : #777777);
231 rect(leftPos, yPos+6, width, lineHeight);
233 text(names[i], leftTextPos, yPos += lineHeight);
240 private void drawKnob(int xPos, int yPos, int knobSize, LXParameter knob) {
244 final float knobValue = knob.getValuef();
245 String knobLabel = knob.getLabel();
246 if (knobLabel == null) {
248 } else if (knobLabel.length() > 4) {
249 knobLabel = knobLabel.substring(0, 4);
254 // For some reason this arc call really crushes drawing performance. Presumably
255 // because openGL is drawing it and when we overlap the second set of arcs it
256 // does a bunch of depth buffer intersection tests? Ellipse with a trapezoid cut out is faster
257 // arc(xPos + knobSize/2, yPos + knobSize/2, knobSize, knobSize, HALF_PI + knobIndent, HALF_PI + knobIndent + (TWO_PI-2*knobIndent));
258 ellipse(xPos + knobSize/2, yPos + knobSize/2, knobSize, knobSize);
260 float endArc = HALF_PI + knobIndent + (TWO_PI-2*knobIndent)*knobValue;
262 arc(xPos + knobSize/2, yPos + knobSize/2, knobSize, knobSize, HALF_PI + knobIndent, endArc);
264 // Center circle of knob
266 ellipse(xPos + knobSize/2, yPos + knobSize/2, knobSize/2, knobSize/2);
268 // Mask notch out of knob
269 fill(color(0, 0, 30));
271 vertex(xPos + knobSize/2 - 3, yPos + knobSize - 8);
272 vertex(xPos + knobSize/2 - 5, yPos + knobSize);
273 vertex(xPos + knobSize/2 + 5, yPos + knobSize);
274 vertex(xPos + knobSize/2 + 3, yPos + knobSize - 8);
278 rect(xPos, yPos + knobSize + 2, knobSize, knobLabelHeight - 2);
282 text(knobLabel, xPos + knobSize/2, yPos + knobSize + knobLabelHeight - 2);
286 private String[] classNameArray(Object[] objects, String suffix) {
287 if (objects == null) {
290 String[] names = new String[objects.length];
291 for (int i = 0; i < objects.length; ++i) {
292 names[i] = className(objects[i], suffix);
297 private String className(Object p, String suffix) {
298 String s = p.getClass().getName();
300 if ((li = s.lastIndexOf(".")) > 0) {
301 s = s.substring(li + 1);
303 if (s.indexOf("SugarCubes$") == 0) {
304 s = s.substring("SugarCubes$".length());
306 if ((suffix != null) && ((li = s.indexOf(suffix)) != -1)) {
307 s = s.substring(0, li);
312 class VirtualTransitionKnob extends LXVirtualParameter {
313 private final int index;
315 VirtualTransitionKnob(int index) {
319 public LXParameter getRealParameter() {
320 List<LXParameter> parameters = transitions[activeTransitionIndex].getParameters();
321 if (index < parameters.size()) {
322 return parameters.get(index);
328 class VirtualEffectKnob extends LXVirtualParameter {
329 private final int index;
331 VirtualEffectKnob(int index) {
335 public LXParameter getRealParameter() {
336 List<LXParameter> parameters = effects[activeEffectIndex].getParameters();
337 if (index < parameters.size()) {
338 return parameters.get(index);
344 private int patternKnobIndex = -1;
345 private int transitionKnobIndex = -1;
346 private int effectKnobIndex = -1;
349 private int releaseEffect = -1;
350 private boolean tempoDown = false;
352 public void mousePressed() {
354 patternKnobIndex = transitionKnobIndex = effectKnobIndex = -1;
356 if (mouseY > tempoY) {
357 if (mouseY - tempoY < tempoHeight) {
361 } else if ((mouseY >= firstEffectKnobY) && (mouseY < firstEffectKnobY + knobSize + knobLabelHeight)) {
362 effectKnobIndex = (mouseX - leftTextPos) / (knobSize + knobSpacing);
363 } else if (mouseY > firstEffectY) {
364 int effectIndex = (mouseY - firstEffectY) / lineHeight;
365 if (effectIndex < effects.length) {
366 if (activeEffectIndex == effectIndex) {
367 effects[effectIndex].enable();
368 releaseEffect = effectIndex;
370 activeEffectIndex = effectIndex;
372 } else if ((mouseY >= firstTransitionKnobY) && (mouseY < firstTransitionKnobY + knobSize + knobLabelHeight)) {
373 transitionKnobIndex = (mouseX - leftTextPos) / (knobSize + knobSpacing);
374 } else if (mouseY > firstTransitionY) {
375 int transitionIndex = (mouseY - firstTransitionY) / lineHeight;
376 if (transitionIndex < transitions.length) {
377 activeTransitionIndex = transitionIndex;
379 } else if ((mouseY >= firstPatternKnobY) && (mouseY < firstPatternKnobY + 2*(knobSize+knobLabelHeight) + knobSpacing)) {
380 patternKnobIndex = (mouseX - leftTextPos) / (knobSize + knobSpacing);
381 if (mouseY >= firstPatternKnobY + knobSize + knobLabelHeight + knobSpacing) {
382 patternKnobIndex += glucose.NUM_PATTERN_KNOBS / 2;
384 } else if (mouseY > firstPatternY) {
385 int patternIndex = (mouseY - firstPatternY) / lineHeight;
386 if (patternIndex < patterns.length) {
387 patterns[patternIndex].setTransition(transitions[activeTransitionIndex]);
388 lx.goIndex(patternIndex);
393 public void mouseDragged() {
394 int dy = lastY - mouseY;
396 if (patternKnobIndex >= 0 && patternKnobIndex < glucose.NUM_PATTERN_KNOBS) {
397 LXParameter p = glucose.patternKnobs[patternKnobIndex];
398 p.setValue(constrain(p.getValuef() + dy*.01, 0, 1));
399 } else if (effectKnobIndex >= 0 && effectKnobIndex < NUM_EFFECT_KNOBS) {
400 LXParameter p = effectKnobs[effectKnobIndex];
401 p.setValue(constrain(p.getValuef() + dy*.01, 0, 1));
402 } else if (transitionKnobIndex >= 0 && transitionKnobIndex < NUM_TRANSITION_KNOBS) {
403 LXParameter p = transitionKnobs[transitionKnobIndex];
404 p.setValue(constrain(p.getValuef() + dy*.01, 0, 1));
408 public void mouseReleased() {
410 if (releaseEffect >= 0) {
411 effects[releaseEffect].trigger();
418 void mousePressed() {
419 if (mouseX > ui.leftPos) {
424 void mouseReleased() {
425 if (mouseX > ui.leftPos) {
430 void mouseDragged() {
431 if (mouseX > ui.leftPos) {