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
17 abstract class OverlayUI {
18 protected final PFont titleFont = createFont("Myriad Pro", 10);
19 protected final color titleColor = #AAAAAA;
20 protected final PFont itemFont = createFont("Lucida Grande", 11);
21 protected final PFont knobFont = titleFont;
22 protected final int w = 140;
23 protected final int leftPos;
24 protected final int leftTextPos;
25 protected final int lineHeight = 20;
26 protected final int sectionSpacing = 12;
27 protected final int controlSpacing = 18;
28 protected final int tempoHeight = 20;
29 protected final int knobSize = 28;
30 protected final float knobIndent = .4;
31 protected final int knobSpacing = 6;
32 protected final int knobLabelHeight = 14;
33 protected final color lightBlue = #666699;
34 protected final color lightGreen = #669966;
38 protected final int STATE_DEFAULT = 0;
39 protected final int STATE_ACTIVE = 1;
40 protected final int STATE_PENDING = 2;
42 protected OverlayUI() {
44 leftTextPos = leftPos + 4;
45 logo = loadImage("logo-sm.png");
48 protected void drawLogoAndBackground() {
50 stroke(color(0, 0, 100));
51 // fill(color(0, 0, 50, 50)); // alpha is bad for perf
52 fill(color(0, 0, 30));
53 rect(leftPos-1, -1, w+2, height+2);
56 protected void drawToggleTip(String s) {
60 text(s, leftTextPos, height-6);
63 protected void drawHelpTip() {
66 text("Tap 'u' to restore UI", width-4, height-6);
69 public void drawFPS() {
73 text("FPS: " + (((int)(frameRate * 10)) / 10.), 4, height-6);
76 protected int drawObjectList(int yPos, String title, Object[] items, Method stateMethod) {
77 return drawObjectList(yPos, title, items, classNameArray(items, null), stateMethod);
80 protected int drawObjectList(int yPos, String title, Object[] items, String[] names, Method stateMethod) {
85 text(title, leftTextPos, yPos += lineHeight);
90 for (int i = 0; i < items.length; ++i) {
92 int state = STATE_DEFAULT;
94 state = ((Integer) stateMethod.invoke(this, o)).intValue();
95 } catch (Exception x) {
96 throw new RuntimeException(x);
105 textColor = color(0, 0, 75 + 15*sin(millis()/200.));;
109 fill(even ? #666666 : #777777);
112 rect(leftPos, yPos+6, width, lineHeight);
114 text(names[i], leftTextPos, yPos += lineHeight);
121 protected String[] classNameArray(Object[] objects, String suffix) {
122 if (objects == null) {
125 String[] names = new String[objects.length];
126 for (int i = 0; i < objects.length; ++i) {
127 names[i] = className(objects[i], suffix);
132 protected String className(Object p, String suffix) {
133 String s = p.getClass().getName();
135 if ((li = s.lastIndexOf(".")) > 0) {
136 s = s.substring(li + 1);
138 if (s.indexOf("SugarCubes$") == 0) {
139 s = s.substring("SugarCubes$".length());
141 if ((suffix != null) && ((li = s.indexOf(suffix)) != -1)) {
142 s = s.substring(0, li);
147 protected int objectClickIndex(int firstItemY) {
148 return (mouseY - firstItemY) / lineHeight;
151 abstract public void draw();
152 abstract public void mousePressed();
153 abstract public void mouseDragged();
154 abstract public void mouseReleased();
158 * UI for control of patterns, transitions, effects.
160 class ControlUI extends OverlayUI {
161 private final String[] patternNames;
162 private final String[] transitionNames;
163 private final String[] effectNames;
165 private int firstPatternY;
166 private int firstPatternKnobY;
167 private int firstTransitionY;
168 private int firstTransitionKnobY;
169 private int firstEffectY;
170 private int firstEffectKnobY;
174 private Method patternStateMethod;
175 private Method transitionStateMethod;
176 private Method effectStateMethod;
178 private final int NUM_PATTERN_KNOBS = 8;
179 private final int NUM_TRANSITION_KNOBS = 4;
180 private final int NUM_EFFECT_KNOBS = 4;
182 private int activeTransitionIndex = 0;
183 private int activeEffectIndex = 0;
185 public final VirtualPatternKnob[] patternKnobs;
186 public final VirtualTransitionKnob[] transitionKnobs;
187 public final VirtualEffectKnob[] effectKnobs;
190 patternNames = classNameArray(patterns, "Pattern");
191 transitionNames = classNameArray(transitions, "Transition");
192 effectNames = classNameArray(effects, "Effect");
194 patternKnobs = new VirtualPatternKnob[NUM_PATTERN_KNOBS];
195 for (int i = 0; i < patternKnobs.length; ++i) {
196 patternKnobs[i] = new VirtualPatternKnob(i);
199 transitionKnobs = new VirtualTransitionKnob[NUM_TRANSITION_KNOBS];
200 for (int i = 0; i < transitionKnobs.length; ++i) {
201 transitionKnobs[i] = new VirtualTransitionKnob(i);
204 effectKnobs = new VirtualEffectKnob[NUM_EFFECT_KNOBS];
205 for (int i = 0; i < effectKnobs.length; ++i) {
206 effectKnobs[i] = new VirtualEffectKnob(i);
210 patternStateMethod = getClass().getMethod("getState", LXPattern.class);
211 effectStateMethod = getClass().getMethod("getState", LXEffect.class);
212 transitionStateMethod = getClass().getMethod("getState", LXTransition.class);
213 } catch (Exception x) {
214 throw new RuntimeException(x);
219 drawLogoAndBackground();
221 firstPatternY = yPos + lineHeight + 6;
222 yPos = drawObjectList(yPos, "PATTERN", patterns, patternNames, patternStateMethod);
223 yPos += controlSpacing;
224 firstPatternKnobY = yPos;
225 int xPos = leftTextPos;
226 for (int i = 0; i < NUM_PATTERN_KNOBS/2; ++i) {
227 drawKnob(xPos, yPos, knobSize, patternKnobs[i]);
228 drawKnob(xPos, yPos + knobSize + knobSpacing + knobLabelHeight, knobSize, patternKnobs[NUM_PATTERN_KNOBS/2 + i]);
229 xPos += knobSize + knobSpacing;
231 yPos += 2*(knobSize + knobLabelHeight) + knobSpacing;
233 yPos += sectionSpacing;
234 firstTransitionY = yPos + lineHeight + 6;
235 yPos = drawObjectList(yPos, "TRANSITION", transitions, transitionNames, transitionStateMethod);
236 yPos += controlSpacing;
237 firstTransitionKnobY = yPos;
239 for (int i = 0; i < transitionKnobs.length; ++i) {
240 drawKnob(xPos, yPos, knobSize, transitionKnobs[i]);
241 xPos += knobSize + knobSpacing;
243 yPos += knobSize + knobLabelHeight;
245 yPos += sectionSpacing;
246 firstEffectY = yPos + lineHeight + 6;
247 yPos = drawObjectList(yPos, "FX", effects, effectNames, effectStateMethod);
248 yPos += controlSpacing;
249 firstEffectKnobY = yPos;
251 for (int i = 0; i < effectKnobs.length; ++i) {
252 drawKnob(xPos, yPos, knobSize, effectKnobs[i]);
253 xPos += knobSize + knobSpacing;
255 yPos += knobSize + knobLabelHeight;
257 yPos += sectionSpacing;
258 yPos = drawObjectList(yPos, "TEMPO", null, null, null);
262 fill(tempoDown ? lightGreen : color(0, 0, 35 - 8*lx.tempo.rampf()));
263 rect(leftPos + 4, yPos, w - 8, tempoHeight);
266 text("" + ((int)(lx.tempo.bpmf() * 100) / 100.), leftPos + w/2., yPos + tempoHeight - 6);
269 drawToggleTip("Tap 'u' to hide");
272 public LXParameter getOrNull(List<LXParameter> items, int index) {
273 if (index < items.size()) {
274 return items.get(index);
279 public int getState(LXPattern p) {
280 if (p == lx.getPattern()) {
282 } else if (p == lx.getNextPattern()) {
283 return STATE_PENDING;
285 return STATE_DEFAULT;
288 public int getState(LXEffect e) {
290 return STATE_PENDING;
291 } else if (effects[activeEffectIndex] == e) {
294 return STATE_DEFAULT;
297 public int getState(LXTransition t) {
298 if (t == lx.getTransition()) {
299 return STATE_PENDING;
300 } else if (t == transitions[activeTransitionIndex]) {
303 return STATE_DEFAULT;
306 private void drawKnob(int xPos, int yPos, int knobSize, LXParameter knob) {
307 final float knobValue = knob.getValuef();
308 String knobLabel = knob.getLabel();
309 if (knobLabel == null) {
311 } else if (knobLabel.length() > 4) {
312 knobLabel = knobLabel.substring(0, 4);
318 // For some reason this arc call really crushes drawing performance. Presumably
319 // because openGL is drawing it and when we overlap the second set of arcs it
320 // does a bunch of depth buffer intersection tests? Ellipse with a trapezoid cut out is faster
321 // arc(xPos + knobSize/2, yPos + knobSize/2, knobSize, knobSize, HALF_PI + knobIndent, HALF_PI + knobIndent + (TWO_PI-2*knobIndent));
322 ellipse(xPos + knobSize/2, yPos + knobSize/2, knobSize, knobSize);
324 float endArc = HALF_PI + knobIndent + (TWO_PI-2*knobIndent)*knobValue;
326 arc(xPos + knobSize/2, yPos + knobSize/2, knobSize, knobSize, HALF_PI + knobIndent, endArc);
328 // Mask notch out of knob
329 fill(color(0, 0, 30));
331 vertex(xPos + knobSize/2, yPos + knobSize/2.);
332 vertex(xPos + knobSize/2 - 6, yPos + knobSize);
333 vertex(xPos + knobSize/2 + 6, yPos + knobSize);
336 // Center circle of knob
338 ellipse(xPos + knobSize/2, yPos + knobSize/2, knobSize/2, knobSize/2);
341 rect(xPos, yPos + knobSize + 2, knobSize, knobLabelHeight - 2);
345 text(knobLabel, xPos + knobSize/2, yPos + knobSize + knobLabelHeight - 2);
348 class VirtualPatternKnob extends LXVirtualParameter {
349 private final int index;
351 VirtualPatternKnob(int index) {
355 public LXParameter getRealParameter() {
356 List<LXParameter> parameters = glucose.getPattern().getParameters();
357 if (index < parameters.size()) {
358 return parameters.get(index);
364 class VirtualTransitionKnob extends LXVirtualParameter {
365 private final int index;
367 VirtualTransitionKnob(int index) {
371 public LXParameter getRealParameter() {
372 List<LXParameter> parameters = transitions[activeTransitionIndex].getParameters();
373 if (index < parameters.size()) {
374 return parameters.get(index);
380 class VirtualEffectKnob extends LXVirtualParameter {
381 private final int index;
383 VirtualEffectKnob(int index) {
387 public LXParameter getRealParameter() {
388 List<LXParameter> parameters = effects[activeEffectIndex].getParameters();
389 if (index < parameters.size()) {
390 return parameters.get(index);
396 private int patternKnobIndex = -1;
397 private int transitionKnobIndex = -1;
398 private int effectKnobIndex = -1;
401 private int releaseEffect = -1;
402 private boolean tempoDown = false;
404 public void mousePressed() {
406 patternKnobIndex = transitionKnobIndex = effectKnobIndex = -1;
408 if (mouseY > tempoY) {
409 if (mouseY - tempoY < tempoHeight) {
413 } else if ((mouseY >= firstEffectKnobY) && (mouseY < firstEffectKnobY + knobSize + knobLabelHeight)) {
414 effectKnobIndex = (mouseX - leftTextPos) / (knobSize + knobSpacing);
415 } else if (mouseY > firstEffectY) {
416 int effectIndex = objectClickIndex(firstEffectY);
417 if (effectIndex < effects.length) {
418 if (activeEffectIndex == effectIndex) {
419 effects[effectIndex].enable();
420 releaseEffect = effectIndex;
422 activeEffectIndex = effectIndex;
424 } else if ((mouseY >= firstTransitionKnobY) && (mouseY < firstTransitionKnobY + knobSize + knobLabelHeight)) {
425 transitionKnobIndex = (mouseX - leftTextPos) / (knobSize + knobSpacing);
426 } else if (mouseY > firstTransitionY) {
427 int transitionIndex = objectClickIndex(firstTransitionY);
428 if (transitionIndex < transitions.length) {
429 activeTransitionIndex = transitionIndex;
431 } else if ((mouseY >= firstPatternKnobY) && (mouseY < firstPatternKnobY + 2*(knobSize+knobLabelHeight) + knobSpacing)) {
432 patternKnobIndex = (mouseX - leftTextPos) / (knobSize + knobSpacing);
433 if (mouseY >= firstPatternKnobY + knobSize + knobLabelHeight + knobSpacing) {
434 patternKnobIndex += NUM_PATTERN_KNOBS / 2;
436 } else if (mouseY > firstPatternY) {
437 int patternIndex = objectClickIndex(firstPatternY);
438 if (patternIndex < patterns.length) {
439 patterns[patternIndex].setTransition(transitions[activeTransitionIndex]);
440 lx.goIndex(patternIndex);
445 public void mouseDragged() {
446 int dy = lastY - mouseY;
448 if (patternKnobIndex >= 0 && patternKnobIndex < NUM_PATTERN_KNOBS) {
449 LXParameter p = patternKnobs[patternKnobIndex];
450 p.setValue(constrain(p.getValuef() + dy*.01, 0, 1));
451 } else if (effectKnobIndex >= 0 && effectKnobIndex < NUM_EFFECT_KNOBS) {
452 LXParameter p = effectKnobs[effectKnobIndex];
453 p.setValue(constrain(p.getValuef() + dy*.01, 0, 1));
454 } else if (transitionKnobIndex >= 0 && transitionKnobIndex < NUM_TRANSITION_KNOBS) {
455 LXParameter p = transitionKnobs[transitionKnobIndex];
456 p.setValue(constrain(p.getValuef() + dy*.01, 0, 1));
460 public void mouseReleased() {
462 if (releaseEffect >= 0) {
463 effects[releaseEffect].trigger();
471 * UI for control of mapping.
473 class MappingUI extends OverlayUI {
475 private MappingTool mappingTool;
477 private final String MAPPING_MODE_ALL = "All On";
478 private final String MAPPING_MODE_SINGLE_CUBE = "Single Cube";
479 private final String[] mappingModes = {
481 MAPPING_MODE_SINGLE_CUBE,
483 private final Method mappingModeStateMethod;
485 private final String CUBE_MODE_ALL = "All Strips";
486 private final String CUBE_MODE_SINGLE_STRIP = "Single Strip";
487 private final String CUBE_MODE_STRIP_PATTERN = "Strip Pattern";
488 private final String[] cubeModes = {
490 CUBE_MODE_SINGLE_STRIP,
491 CUBE_MODE_STRIP_PATTERN
493 private final Method cubeModeStateMethod;
495 private final String CHANNEL_MODE_RED = "Red";
496 private final String CHANNEL_MODE_GREEN = "Green";
497 private final String CHANNEL_MODE_BLUE = "Blue";
498 private final String[] channelModes = {
503 private final Method channelModeStateMethod;
505 private int firstMappingY;
506 private int firstCubeY;
507 private int firstChannelY;
508 private int cubeFieldY;
509 private int stripFieldY;
511 private boolean dragCube;
512 private boolean dragStrip;
514 MappingUI(MappingTool mappingTool) {
515 this.mappingTool = mappingTool;
517 mappingModeStateMethod = getClass().getMethod("getMappingState", Object.class);
518 channelModeStateMethod = getClass().getMethod("getChannelState", Object.class);
519 cubeModeStateMethod = getClass().getMethod("getCubeState", Object.class);
520 } catch (Exception x) {
521 throw new RuntimeException(x);
525 public int getMappingState(Object mappingMode) {
526 boolean active = (mappingMode == MAPPING_MODE_SINGLE_CUBE) == mappingTool.mappingModeSingleCube;
527 return active ? STATE_ACTIVE : STATE_DEFAULT;
530 public int getChannelState(Object channelMode) {
531 boolean active = false;
532 if (channelMode == CHANNEL_MODE_RED) {
533 active = mappingTool.channelModeRed;
534 } else if (channelMode == CHANNEL_MODE_GREEN) {
535 active = mappingTool.channelModeGreen;
536 } else if (channelMode == CHANNEL_MODE_BLUE) {
537 active = mappingTool.channelModeBlue;
539 return active ? STATE_ACTIVE : STATE_DEFAULT;
542 public int getCubeState(Object cubeMode) {
543 boolean active = false;
544 if (cubeMode == CUBE_MODE_ALL) {
545 active = mappingTool.cubeMode == mappingTool.CUBE_MODE_ALL;
546 } else if (cubeMode == CUBE_MODE_SINGLE_STRIP) {
547 active = mappingTool.cubeMode == mappingTool.CUBE_MODE_SINGLE_STRIP;
548 } else if (cubeMode == CUBE_MODE_STRIP_PATTERN) {
549 active = mappingTool.cubeMode == mappingTool.CUBE_MODE_STRIP_PATTERN;
551 return active ? STATE_ACTIVE : STATE_DEFAULT;
555 drawLogoAndBackground();
557 firstMappingY = yPos + lineHeight + 6;
558 yPos = drawObjectList(yPos, "MAPPING MODE", mappingModes, mappingModes, mappingModeStateMethod);
559 yPos += sectionSpacing;
561 firstCubeY = yPos + lineHeight + 6;
562 yPos = drawObjectList(yPos, "CUBE MODE", cubeModes, cubeModes, cubeModeStateMethod);
563 yPos += sectionSpacing;
565 firstChannelY = yPos + lineHeight + 6;
566 yPos = drawObjectList(yPos, "CHANNELS", channelModes, channelModes, channelModeStateMethod);
567 yPos += sectionSpacing;
569 cubeFieldY = yPos + lineHeight + 6;
570 yPos = drawValueField(yPos, "CUBE ID", glucose.model.getRawIndexForCube(mappingTool.cubeIndex));
571 yPos += sectionSpacing;
573 stripFieldY = yPos + lineHeight + 6;
574 yPos = drawValueField(yPos, "STRIP ID", mappingTool.stripIndex + 1);
576 drawToggleTip("Tap 'm' to return");
579 private int drawValueField(int yPos, String label, int value) {
584 text(label, leftTextPos, yPos);
587 rect(leftTextPos, yPos, w-8, lineHeight);
593 text("" + value, leftTextPos + (w-8)/2, yPos - 5);
600 public void mousePressed() {
601 dragCube = dragStrip = false;
603 if (mouseY >= stripFieldY) {
604 if (mouseY < stripFieldY + lineHeight) {
607 } else if (mouseY >= cubeFieldY) {
608 if (mouseY < cubeFieldY + lineHeight) {
611 } else if (mouseY >= firstChannelY) {
612 int index = objectClickIndex(firstChannelY);
614 case 0: mappingTool.channelModeRed = !mappingTool.channelModeRed; break;
615 case 1: mappingTool.channelModeGreen = !mappingTool.channelModeGreen; break;
616 case 2: mappingTool.channelModeBlue = !mappingTool.channelModeBlue; break;
618 } else if (mouseY >= firstCubeY) {
619 int index = objectClickIndex(firstCubeY);
621 case 0: mappingTool.cubeMode = mappingTool.CUBE_MODE_ALL; break;
622 case 1: mappingTool.cubeMode = mappingTool.CUBE_MODE_SINGLE_STRIP; break;
623 case 2: mappingTool.cubeMode = mappingTool.CUBE_MODE_STRIP_PATTERN; break;
625 } else if (mouseY >= firstMappingY) {
626 int index = objectClickIndex(firstMappingY);
628 mappingTool.mappingModeSingleCube = (index > 0);
633 public void mouseReleased() {
636 public void mouseDragged() {
637 final int DRAG_THRESHOLD = 5;
638 int dy = lastY - mouseY;
639 if (abs(dy) >= DRAG_THRESHOLD) {
643 mappingTool.decCube();
645 mappingTool.incCube();
647 } else if (dragStrip) {
649 mappingTool.decStrip();
651 mappingTool.incStrip();
661 void mousePressed() {
662 if (mouseX > ui.leftPos) {
667 void mouseReleased() {
668 if (mouseX > ui.leftPos) {
673 void mouseDragged() {
674 if (mouseX > ui.leftPos) {