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;
179 patternNames = classNameArray(patterns, "Pattern");
180 transitionNames = classNameArray(transitions, "Transition");
181 effectNames = classNameArray(effects, "Effect");
184 patternStateMethod = getClass().getMethod("getState", LXPattern.class);
185 effectStateMethod = getClass().getMethod("getState", LXEffect.class);
186 transitionStateMethod = getClass().getMethod("getState", LXTransition.class);
187 } catch (Exception x) {
188 throw new RuntimeException(x);
193 drawLogoAndBackground();
195 firstPatternY = yPos + lineHeight + 6;
196 yPos = drawObjectList(yPos, "PATTERN", patterns, patternNames, patternStateMethod);
197 yPos += controlSpacing;
198 firstPatternKnobY = yPos;
199 int xPos = leftTextPos;
200 for (int i = 0; i < glucose.NUM_PATTERN_KNOBS/2; ++i) {
201 drawKnob(xPos, yPos, knobSize, glucose.patternKnobs.get(i));
202 drawKnob(xPos, yPos + knobSize + knobSpacing + knobLabelHeight, knobSize, glucose.patternKnobs.get(glucose.NUM_PATTERN_KNOBS/2 + i));
203 xPos += knobSize + knobSpacing;
205 yPos += 2*(knobSize + knobLabelHeight) + knobSpacing;
207 yPos += sectionSpacing;
208 firstTransitionY = yPos + lineHeight + 6;
209 yPos = drawObjectList(yPos, "TRANSITION", transitions, transitionNames, transitionStateMethod);
210 yPos += controlSpacing;
211 firstTransitionKnobY = yPos;
213 for (VirtualTransitionKnob knob : glucose.transitionKnobs) {
214 drawKnob(xPos, yPos, knobSize, knob);
215 xPos += knobSize + knobSpacing;
217 yPos += knobSize + knobLabelHeight;
219 yPos += sectionSpacing;
220 firstEffectY = yPos + lineHeight + 6;
221 yPos = drawObjectList(yPos, "FX", effects, effectNames, effectStateMethod);
222 yPos += controlSpacing;
223 firstEffectKnobY = yPos;
225 for (VirtualEffectKnob knob : glucose.effectKnobs) {
226 drawKnob(xPos, yPos, knobSize, knob);
227 xPos += knobSize + knobSpacing;
229 yPos += knobSize + knobLabelHeight;
231 yPos += sectionSpacing;
232 yPos = drawObjectList(yPos, "TEMPO", null, null, null);
236 fill(tempoDown ? lightGreen : color(0, 0, 35 - 8*lx.tempo.rampf()));
237 rect(leftPos + 4, yPos, w - 8, tempoHeight);
240 text("" + ((int)(lx.tempo.bpmf() * 100) / 100.), leftPos + w/2., yPos + tempoHeight - 6);
243 drawToggleTip("Tap 'u' to hide");
246 public LXParameter getOrNull(List<LXParameter> items, int index) {
247 if (index < items.size()) {
248 return items.get(index);
253 public int getState(LXPattern p) {
254 if (p == lx.getPattern()) {
256 } else if (p == lx.getNextPattern()) {
257 return STATE_PENDING;
259 return STATE_DEFAULT;
262 public int getState(LXEffect e) {
264 return STATE_PENDING;
265 } else if (e == glucose.getSelectedEffect()) {
268 return STATE_DEFAULT;
271 public int getState(LXTransition t) {
272 if (t == lx.getTransition()) {
273 return STATE_PENDING;
274 } else if (t == glucose.getSelectedTransition()) {
277 return STATE_DEFAULT;
280 private void drawKnob(int xPos, int yPos, int knobSize, LXParameter knob) {
281 final float knobValue = knob.getValuef();
282 String knobLabel = knob.getLabel();
283 if (knobLabel == null) {
285 } else if (knobLabel.length() > 4) {
286 knobLabel = knobLabel.substring(0, 4);
292 // For some reason this arc call really crushes drawing performance. Presumably
293 // because openGL is drawing it and when we overlap the second set of arcs it
294 // does a bunch of depth buffer intersection tests? Ellipse with a trapezoid cut out is faster
295 // arc(xPos + knobSize/2, yPos + knobSize/2, knobSize, knobSize, HALF_PI + knobIndent, HALF_PI + knobIndent + (TWO_PI-2*knobIndent));
296 ellipse(xPos + knobSize/2, yPos + knobSize/2, knobSize, knobSize);
298 float endArc = HALF_PI + knobIndent + (TWO_PI-2*knobIndent)*knobValue;
300 arc(xPos + knobSize/2, yPos + knobSize/2, knobSize, knobSize, HALF_PI + knobIndent, endArc);
302 // Mask notch out of knob
303 fill(color(0, 0, 30));
305 vertex(xPos + knobSize/2, yPos + knobSize/2.);
306 vertex(xPos + knobSize/2 - 6, yPos + knobSize);
307 vertex(xPos + knobSize/2 + 6, yPos + knobSize);
310 // Center circle of knob
312 ellipse(xPos + knobSize/2, yPos + knobSize/2, knobSize/2, knobSize/2);
315 rect(xPos, yPos + knobSize + 2, knobSize, knobLabelHeight - 2);
319 text(knobLabel, xPos + knobSize/2, yPos + knobSize + knobLabelHeight - 2);
322 private int patternKnobIndex = -1;
323 private int transitionKnobIndex = -1;
324 private int effectKnobIndex = -1;
327 private int releaseEffect = -1;
328 private boolean tempoDown = false;
330 public void mousePressed() {
332 patternKnobIndex = transitionKnobIndex = effectKnobIndex = -1;
334 if (mouseY > tempoY) {
335 if (mouseY - tempoY < tempoHeight) {
339 } else if ((mouseY >= firstEffectKnobY) && (mouseY < firstEffectKnobY + knobSize + knobLabelHeight)) {
340 effectKnobIndex = (mouseX - leftTextPos) / (knobSize + knobSpacing);
341 } else if (mouseY > firstEffectY) {
342 int effectIndex = objectClickIndex(firstEffectY);
343 if (effectIndex < effects.length) {
344 if (effects[effectIndex] == glucose.getSelectedEffect()) {
345 effects[effectIndex].enable();
346 releaseEffect = effectIndex;
348 glucose.setSelectedEffect(effectIndex);
350 } else if ((mouseY >= firstTransitionKnobY) && (mouseY < firstTransitionKnobY + knobSize + knobLabelHeight)) {
351 transitionKnobIndex = (mouseX - leftTextPos) / (knobSize + knobSpacing);
352 } else if (mouseY > firstTransitionY) {
353 int transitionIndex = objectClickIndex(firstTransitionY);
354 if (transitionIndex < transitions.length) {
355 glucose.setSelectedTransition(transitionIndex);
357 } else if ((mouseY >= firstPatternKnobY) && (mouseY < firstPatternKnobY + 2*(knobSize+knobLabelHeight) + knobSpacing)) {
358 patternKnobIndex = (mouseX - leftTextPos) / (knobSize + knobSpacing);
359 if (mouseY >= firstPatternKnobY + knobSize + knobLabelHeight + knobSpacing) {
360 patternKnobIndex += glucose.NUM_PATTERN_KNOBS / 2;
362 } else if (mouseY > firstPatternY) {
363 int patternIndex = objectClickIndex(firstPatternY);
364 if (patternIndex < patterns.length) {
365 lx.goIndex(patternIndex);
370 public void mouseDragged() {
371 int dy = lastY - mouseY;
373 if (patternKnobIndex >= 0 && patternKnobIndex < glucose.NUM_PATTERN_KNOBS) {
374 LXParameter p = glucose.patternKnobs.get(patternKnobIndex);
375 p.setValue(constrain(p.getValuef() + dy*.01, 0, 1));
376 } else if (effectKnobIndex >= 0 && effectKnobIndex < glucose.NUM_EFFECT_KNOBS) {
377 LXParameter p = glucose.effectKnobs.get(effectKnobIndex);
378 p.setValue(constrain(p.getValuef() + dy*.01, 0, 1));
379 } else if (transitionKnobIndex >= 0 && transitionKnobIndex < glucose.NUM_TRANSITION_KNOBS) {
380 LXParameter p = glucose.transitionKnobs.get(transitionKnobIndex);
381 p.setValue(constrain(p.getValuef() + dy*.01, 0, 1));
385 public void mouseReleased() {
387 if (releaseEffect >= 0) {
388 effects[releaseEffect].trigger();
396 * UI for control of mapping.
398 class MappingUI extends OverlayUI {
400 private MappingTool mappingTool;
402 private final String MAPPING_MODE_ALL = "All On";
403 private final String MAPPING_MODE_CHANNEL = "Channel";
404 private final String MAPPING_MODE_SINGLE_CUBE = "Single Cube";
406 private final String[] mappingModes = {
408 MAPPING_MODE_CHANNEL,
409 MAPPING_MODE_SINGLE_CUBE
411 private final Method mappingModeStateMethod;
413 private final String CUBE_MODE_ALL = "All Strips";
414 private final String CUBE_MODE_SINGLE_STRIP = "Single Strip";
415 private final String CUBE_MODE_STRIP_PATTERN = "Strip Pattern";
416 private final String[] cubeModes = {
418 CUBE_MODE_SINGLE_STRIP,
419 CUBE_MODE_STRIP_PATTERN
421 private final Method cubeModeStateMethod;
423 private final String CHANNEL_MODE_RED = "Red";
424 private final String CHANNEL_MODE_GREEN = "Green";
425 private final String CHANNEL_MODE_BLUE = "Blue";
426 private final String[] channelModes = {
431 private final Method channelModeStateMethod;
433 private int firstMappingY;
434 private int firstCubeY;
435 private int firstChannelY;
436 private int channelFieldY;
437 private int cubeFieldY;
438 private int stripFieldY;
440 private boolean dragCube;
441 private boolean dragStrip;
442 private boolean dragChannel;
444 MappingUI(MappingTool mappingTool) {
445 this.mappingTool = mappingTool;
447 mappingModeStateMethod = getClass().getMethod("getMappingState", Object.class);
448 channelModeStateMethod = getClass().getMethod("getChannelState", Object.class);
449 cubeModeStateMethod = getClass().getMethod("getCubeState", Object.class);
450 } catch (Exception x) {
451 throw new RuntimeException(x);
455 public int getMappingState(Object mappingMode) {
456 boolean active = false;
457 if (mappingMode == MAPPING_MODE_ALL) {
458 active = mappingTool.mappingMode == mappingTool.MAPPING_MODE_ALL;
459 } else if (mappingMode == MAPPING_MODE_CHANNEL) {
460 active = mappingTool.mappingMode == mappingTool.MAPPING_MODE_CHANNEL;
461 } else if (mappingMode == MAPPING_MODE_SINGLE_CUBE) {
462 active = mappingTool.mappingMode == mappingTool.MAPPING_MODE_SINGLE_CUBE;
464 return active ? STATE_ACTIVE : STATE_DEFAULT;
467 public int getChannelState(Object channelMode) {
468 boolean active = false;
469 if (channelMode == CHANNEL_MODE_RED) {
470 active = mappingTool.channelModeRed;
471 } else if (channelMode == CHANNEL_MODE_GREEN) {
472 active = mappingTool.channelModeGreen;
473 } else if (channelMode == CHANNEL_MODE_BLUE) {
474 active = mappingTool.channelModeBlue;
476 return active ? STATE_ACTIVE : STATE_DEFAULT;
479 public int getCubeState(Object cubeMode) {
480 boolean active = false;
481 if (cubeMode == CUBE_MODE_ALL) {
482 active = mappingTool.cubeMode == mappingTool.CUBE_MODE_ALL;
483 } else if (cubeMode == CUBE_MODE_SINGLE_STRIP) {
484 active = mappingTool.cubeMode == mappingTool.CUBE_MODE_SINGLE_STRIP;
485 } else if (cubeMode == CUBE_MODE_STRIP_PATTERN) {
486 active = mappingTool.cubeMode == mappingTool.CUBE_MODE_STRIP_PATTERN;
488 return active ? STATE_ACTIVE : STATE_DEFAULT;
492 drawLogoAndBackground();
494 firstMappingY = yPos + lineHeight + 6;
495 yPos = drawObjectList(yPos, "MAPPING MODE", mappingModes, mappingModes, mappingModeStateMethod);
496 yPos += sectionSpacing;
498 firstCubeY = yPos + lineHeight + 6;
499 yPos = drawObjectList(yPos, "CUBE MODE", cubeModes, cubeModes, cubeModeStateMethod);
500 yPos += sectionSpacing;
502 firstChannelY = yPos + lineHeight + 6;
503 yPos = drawObjectList(yPos, "CHANNELS", channelModes, channelModes, channelModeStateMethod);
504 yPos += sectionSpacing;
506 channelFieldY = yPos + lineHeight + 6;
507 yPos = drawValueField(yPos, "CHANNEL ID", mappingTool.channelIndex + 1);
508 yPos += sectionSpacing;
510 cubeFieldY = yPos + lineHeight + 6;
511 yPos = drawValueField(yPos, "CUBE ID", glucose.model.getRawIndexForCube(mappingTool.cubeIndex));
512 yPos += sectionSpacing;
514 stripFieldY = yPos + lineHeight + 6;
515 yPos = drawValueField(yPos, "STRIP ID", mappingTool.stripIndex + 1);
517 drawToggleTip("Tap 'm' to return");
520 private int drawValueField(int yPos, String label, int value) {
525 text(label, leftTextPos, yPos);
528 rect(leftTextPos, yPos, w-8, lineHeight);
534 text("" + value, leftTextPos + (w-8)/2, yPos - 5);
541 public void mousePressed() {
542 dragCube = dragStrip = dragChannel = false;
544 if (mouseY >= stripFieldY) {
545 if (mouseY < stripFieldY + lineHeight) {
548 } else if (mouseY >= cubeFieldY) {
549 if (mouseY < cubeFieldY + lineHeight) {
552 } else if (mouseY >= channelFieldY) {
553 if (mouseY < channelFieldY + lineHeight) {
556 } else if (mouseY >= firstChannelY) {
557 int index = objectClickIndex(firstChannelY);
559 case 0: mappingTool.channelModeRed = !mappingTool.channelModeRed; break;
560 case 1: mappingTool.channelModeGreen = !mappingTool.channelModeGreen; break;
561 case 2: mappingTool.channelModeBlue = !mappingTool.channelModeBlue; break;
563 } else if (mouseY >= firstCubeY) {
564 int index = objectClickIndex(firstCubeY);
566 case 0: mappingTool.cubeMode = mappingTool.CUBE_MODE_ALL; break;
567 case 1: mappingTool.cubeMode = mappingTool.CUBE_MODE_SINGLE_STRIP; break;
568 case 2: mappingTool.cubeMode = mappingTool.CUBE_MODE_STRIP_PATTERN; break;
570 } else if (mouseY >= firstMappingY) {
571 int index = objectClickIndex(firstMappingY);
573 case 0: mappingTool.mappingMode = mappingTool.MAPPING_MODE_ALL; break;
574 case 1: mappingTool.mappingMode = mappingTool.MAPPING_MODE_CHANNEL; break;
575 case 2: mappingTool.mappingMode = mappingTool.MAPPING_MODE_SINGLE_CUBE; break;
580 public void mouseReleased() {
583 public void mouseDragged() {
584 final int DRAG_THRESHOLD = 5;
585 int dy = lastY - mouseY;
586 if (abs(dy) >= DRAG_THRESHOLD) {
590 mappingTool.decCube();
592 mappingTool.incCube();
594 } else if (dragStrip) {
596 mappingTool.decStrip();
598 mappingTool.incStrip();
600 } else if (dragChannel) {
602 mappingTool.decChannel();
604 mappingTool.incChannel();
614 void mousePressed() {
615 if (mouseX > ui.leftPos) {
620 void mouseReleased() {
621 if (mouseX > ui.leftPos) {
626 void mouseDragged() {
627 if (mouseX > ui.leftPos) {