2 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
5 * ///\\\ ///\\\ ///\\\ ///\\\
6 * \\\/// \\\/// \\\/// \\\///
9 * EXPERTS ONLY!! EXPERTS ONLY!!
11 * Little UI framework in progress to handle mouse events, layout,
15 final color lightGreen = #669966;
16 final color lightBlue = #666699;
17 final color bgGray = #444444;
18 final color defaultTextColor = #999999;
19 final PFont defaultItemFont = createFont("Lucida Grande", 11);
20 final PFont defaultTitleFont = createFont("Myriad Pro", 10);
22 public abstract class UIObject {
24 protected final List<UIObject> children = new ArrayList<UIObject>();
26 protected boolean needsRedraw = true;
27 protected boolean childNeedsRedraw = true;
29 protected float x=0, y=0, w=0, h=0;
31 public UIContainer parent = null;
33 protected boolean visible = true;
37 public UIObject(float x, float y, float w, float h) {
44 public boolean isVisible() {
48 public UIObject setVisible(boolean visible) {
49 if (visible != this.visible) {
50 this.visible = visible;
56 public final UIObject setPosition(float x, float y) {
63 public final UIObject setSize(float w, float h) {
70 public final UIObject addToContainer(UIContainer c) {
76 public final UIObject removeFromContainer(UIContainer c) {
77 c.children.remove(this);
82 public final UIObject redraw() {
84 UIObject p = this.parent;
86 p.childNeedsRedraw = true;
92 private final void _redraw() {
94 for (UIObject child : children) {
95 childNeedsRedraw = true;
100 public final void draw(PGraphics pg) {
108 if (childNeedsRedraw) {
109 childNeedsRedraw = false;
110 for (UIObject child : children) {
111 if (needsRedraw || child.needsRedraw || child.childNeedsRedraw) {
113 pg.translate(child.x, child.y);
121 public final boolean contains(float x, float y) {
123 (x >= this.x && x < (this.x + this.w)) &&
124 (y >= this.y && y < (this.y + this.h));
127 protected void onDraw(PGraphics pg) {}
128 protected void onMousePressed(float mx, float my) {}
129 protected void onMouseReleased(float mx, float my) {}
130 protected void onMouseDragged(float mx, float my, float dx, float dy) {}
131 protected void onMouseWheel(float mx, float my, float dx) {}
134 public class UIContainer extends UIObject {
136 private UIObject focusedChild = null;
138 public UIContainer() {}
140 public UIContainer(float x, float y, float w, float h) {
144 public UIContainer(UIObject[] children) {
145 for (UIObject child : children) {
146 child.addToContainer(this);
150 protected void onMousePressed(float mx, float my) {
151 for (int i = children.size() - 1; i >= 0; --i) {
152 UIObject child = children.get(i);
153 if (child.contains(mx, my)) {
154 child.onMousePressed(mx - child.x, my - child.y);
155 focusedChild = child;
161 protected void onMouseReleased(float mx, float my) {
162 if (focusedChild != null) {
163 focusedChild.onMouseReleased(mx - focusedChild.x, my - focusedChild.y);
168 protected void onMouseDragged(float mx, float my, float dx, float dy) {
169 if (focusedChild != null) {
170 focusedChild.onMouseDragged(mx - focusedChild.x, my - focusedChild.y, dx, dy);
174 protected void onMouseWheel(float mx, float my, float delta) {
175 for (UIObject child : children) {
176 if (child.contains(mx, my)) {
177 child.onMouseWheel(mx - child.x, mx - child.y, delta);
184 public class UIContext extends UIContainer {
186 final public PGraphics pg;
188 UIContext(float x, float y, float w, float h) {
190 pg = createGraphics((int)w, (int)h, JAVA2D);
198 if (needsRedraw || childNeedsRedraw) {
206 private float px, py;
207 private boolean dragging = false;
209 public boolean mousePressed(float mx, float my) {
213 if (contains(mx, my)) {
217 onMousePressed(mx - x, my - y);
223 public boolean mouseReleased(float mx, float my) {
228 onMouseReleased(mx - x, my - y);
232 public boolean mouseDragged(float mx, float my) {
239 onMouseDragged(mx - x, my - y, dx, dy);
247 public boolean mouseWheel(float mx, float my, float delta) {
251 if (contains(mx, my)) {
252 onMouseWheel(mx - x, my - y, delta);
259 public class UIWindow extends UIContext {
261 protected final static int titleHeight = 24;
263 public UIWindow(String label, float x, float y, float w, float h) {
265 new UILabel(6, 8, w-6, titleHeight-8) {
266 protected void onMouseDragged(float mx, float my, float dx, float dy) {
267 parent.x = constrain(parent.x + dx, 0, width - w);
268 parent.y = constrain(parent.y + dy, 0, height - h);
270 }.setLabel(label).setFont(defaultTitleFont).addToContainer(this);
273 protected void onDraw(PGraphics pg) {
277 pg.rect(0, 0, w-1, h-1);
281 public class UILabel extends UIObject {
283 private PFont font = defaultTitleFont;
284 private color fontColor = #CCCCCC;
285 private String label = "";
287 public UILabel(float x, float y, float w, float h) {
291 protected void onDraw(PGraphics pg) {
292 pg.textAlign(LEFT, TOP);
295 pg.text(label, 0, 0);
298 public UILabel setFont(PFont font) {
304 public UILabel setFontColor(color fontColor) {
305 this.fontColor = fontColor;
310 public UILabel setLabel(String label) {
317 public class UICheckbox extends UIButton {
319 private boolean firstDraw = true;
321 public UICheckbox(float x, float y, float w, float h) {
326 public void onDraw(PGraphics pg) {
327 pg.stroke(borderColor);
328 pg.fill(active ? activeColor : inactiveColor);
332 pg.textFont(defaultItemFont);
333 pg.textAlign(LEFT, CENTER);
334 pg.text(label, h + 4, h/2);
341 public class UIButton extends UIObject {
343 protected boolean active = false;
344 protected boolean isMomentary = false;
345 protected color borderColor = #666666;
346 protected color inactiveColor = #222222;
347 protected color activeColor = #669966;
348 protected color labelColor = #999999;
349 protected String label = "";
351 public UIButton(float x, float y, float w, float h) {
355 public UIButton setMomentary(boolean momentary) {
356 isMomentary = momentary;
360 protected void onDraw(PGraphics pg) {
361 pg.stroke(borderColor);
362 pg.fill(active ? activeColor : inactiveColor);
364 if (label != null && label.length() > 0) {
365 pg.fill(active ? #FFFFFF : labelColor);
366 pg.textFont(defaultItemFont);
367 pg.textAlign(CENTER);
368 pg.text(label, w/2, h-5);
372 protected void onMousePressed(float mx, float my) {
380 protected void onMouseReleased(float mx, float my) {
386 public boolean isActive() {
390 public UIButton setActive(boolean active) {
391 this.active = active;
397 public UIButton toggle() {
398 return setActive(!active);
401 protected void onToggle(boolean active) {}
403 public UIButton setBorderColor(color borderColor) {
404 if (this.borderColor != borderColor) {
405 this.borderColor = borderColor;
411 public UIButton setActiveColor(color activeColor) {
412 if (this.activeColor != activeColor) {
413 this.activeColor = activeColor;
421 public UIButton setInactiveColor(color inactiveColor) {
422 if (this.inactiveColor != inactiveColor) {
423 this.inactiveColor = inactiveColor;
431 public UIButton setLabelColor(color labelColor) {
432 if (this.labelColor != labelColor) {
433 this.labelColor = labelColor;
439 public UIButton setLabel(String label) {
440 if (!this.label.equals(label)) {
447 public void onMousePressed() {
452 public class UIToggleSet extends UIObject {
454 private String[] options;
455 private int[] boundaries;
456 private String value;
458 public UIToggleSet(float x, float y, float w, float h) {
462 public UIToggleSet setOptions(String[] options) {
463 this.options = options;
464 boundaries = new int[options.length];
466 for (String s : options) {
467 totalLength += s.length();
470 for (int i = 0; i < options.length; ++i) {
471 lengthSoFar += options[i].length();
472 boundaries[i] = (int) (lengthSoFar * w / totalLength);
479 public String getValue() {
483 public UIToggleSet setValue(String option) {
490 public void onDraw(PGraphics pg) {
494 for (int b : boundaries) {
495 pg.line(b, 1, b, h-1);
498 pg.textAlign(CENTER);
499 pg.textFont(defaultItemFont);
500 int leftBoundary = 0;
502 for (int i = 0; i < options.length; ++i) {
503 boolean isActive = options[i] == value;
506 pg.rect(leftBoundary + 1, 1, boundaries[i] - leftBoundary - 1, h-1);
508 pg.fill(isActive ? #FFFFFF : #999999);
509 pg.text(options[i], (leftBoundary + boundaries[i]) / 2., h-6);
510 leftBoundary = boundaries[i];
514 public void onMousePressed(float mx, float my) {
515 for (int i = 0; i < boundaries.length; ++i) {
516 if (mx < boundaries[i]) {
517 setValue(options[i]);
523 protected void onToggle(String option) {}
528 public abstract class UIParameterControl extends UIObject implements LXParameter.Listener {
529 protected LXParameter parameter = null;
531 protected UIParameterControl(float x, float y, float w, float h) {
535 public void onParameterChanged(LXParameter parameter) {
539 public UIParameterControl setParameter(LXParameter parameter) {
540 if (this.parameter != null) {
541 if (this.parameter instanceof LXListenableParameter) {
542 ((LXListenableParameter)this.parameter).removeListener(this);
545 this.parameter = parameter;
546 if (this.parameter != null) {
547 if (this.parameter instanceof LXListenableParameter) {
548 ((LXListenableParameter)this.parameter).addListener(this);
556 public class UIParameterKnob extends UIParameterControl {
557 private int knobSize = 28;
558 private final float knobIndent = .4;
559 private final int knobLabelHeight = 14;
561 public UIParameterKnob(float x, float y) {
563 setSize(knobSize, knobSize + knobLabelHeight);
566 public UIParameterKnob(float x, float y, float w, float h) {
570 protected void onDraw(PGraphics pg) {
571 float knobValue = (parameter != null) ? parameter.getValuef() : 0;
573 pg.ellipseMode(CENTER);
577 pg.rect(0, 0, knobSize, knobSize);
579 // Full outer dark ring
581 pg.arc(knobSize/2, knobSize/2, knobSize, knobSize, HALF_PI + knobIndent, HALF_PI + knobIndent + (TWO_PI-2*knobIndent));
583 // Light ring indicating value
585 pg.arc(knobSize/2, knobSize/2, knobSize, knobSize, HALF_PI + knobIndent, HALF_PI + knobIndent + knobValue*(TWO_PI-2*knobIndent));
587 // Center circle of knob
589 pg.ellipse(knobSize/2, knobSize/2, knobSize/2, knobSize/2);
591 String knobLabel = (parameter != null) ? parameter.getLabel() : null;
592 if (knobLabel == null) {
594 } else if (knobLabel.length() > 4) {
595 knobLabel = knobLabel.substring(0, 4);
598 pg.rect(0, knobSize + 2, knobSize, knobLabelHeight - 2);
600 pg.textAlign(CENTER);
601 pg.textFont(defaultTitleFont);
602 pg.text(knobLabel, knobSize/2, knobSize + knobLabelHeight - 2);
605 public void onMouseDragged(float mx, float my, float dx, float dy) {
606 if (parameter != null) {
607 float value = constrain(parameter.getValuef() - dy / 100., 0, 1);
608 parameter.setValue(value);
613 public class UIParameterSlider extends UIParameterControl {
615 private static final float handleWidth = 12;
617 UIParameterSlider(float x, float y, float w, float h) {
621 protected void onDraw(PGraphics pg) {
626 pg.rect(4, h/2-2, w-8, 4);
629 pg.rect((int) (4 + parameter.getValuef() * (w-8-handleWidth)), 4, handleWidth, h-8);
632 private boolean editing = false;
633 private long lastClick = 0;
634 private float doubleClickMode = 0;
635 private float doubleClickX = 0;
636 protected void onMousePressed(float mx, float my) {
638 float handleLeft = 4 + parameter.getValuef() * (w-8-handleWidth);
639 if (mx >= handleLeft && mx < handleLeft + handleWidth) {
642 if ((now - lastClick) < 300 && abs(mx - doubleClickX) < 3) {
643 parameter.setValue(doubleClickMode);
648 } else if (mx > w*.75) {
651 doubleClickMode = 0.5;
657 protected void onMouseReleased(float mx, float my) {
661 protected void onMouseDragged(float mx, float my, float dx, float dy) {
663 parameter.setValue(constrain((mx - handleWidth/2. - 4) / (w-8-handleWidth), 0, 1));
668 public class UIScrollList extends UIObject {
670 private List<ScrollItem> items = new ArrayList<ScrollItem>();
672 private PFont itemFont = defaultItemFont;
673 private int itemHeight = 20;
674 private color selectedColor = lightGreen;
675 private color pendingColor = lightBlue;
676 private int scrollOffset = 0;
677 private int numVisibleItems = 0;
679 private boolean hasScroll;
680 private float scrollYStart;
681 private float scrollYHeight;
683 public UIScrollList(float x, float y, float w, float h) {
687 protected void onDraw(PGraphics pg) {
690 for (int i = 0; i < numVisibleItems; ++i) {
691 if (i + scrollOffset >= items.size()) {
694 ScrollItem item = items.get(i + scrollOffset);
696 color labelColor = #FFFFFF;
697 if (item.isSelected()) {
698 itemColor = selectedColor;
699 } else if (item.isPending()) {
700 itemColor = pendingColor;
702 labelColor = #000000;
705 float factor = even ? .92 : 1.08;
706 itemColor = lx.scaleBrightness(itemColor, factor);
710 pg.rect(0, yp, w, itemHeight);
712 pg.textFont(itemFont);
713 pg.textAlign(LEFT, TOP);
714 pg.text(item.getLabel(), 6, yp+4);
722 pg.rect(w-12, 0, 12, h);
724 pg.rect(w-12, scrollYStart, 12, scrollYHeight);
729 private boolean scrolling = false;
730 private ScrollItem pressedItem = null;
732 public void onMousePressed(float mx, float my) {
734 if (hasScroll && mx >= w-12) {
735 if (my >= scrollYStart && my < (scrollYStart + scrollYHeight)) {
740 int index = (int) my / itemHeight;
741 if (scrollOffset + index < items.size()) {
742 pressedItem = items.get(scrollOffset + index);
743 pressedItem.onMousePressed();
749 public void onMouseReleased(float mx, float my) {
751 if (pressedItem != null) {
752 pressedItem.onMouseReleased();
757 private float dAccum = 0;
758 public void onMouseDragged(float mx, float my, float dx, float dy) {
761 float scrollOne = h / items.size();
762 int offset = (int) (dAccum / scrollOne);
764 dAccum -= offset * scrollOne;
765 setScrollOffset(scrollOffset + offset);
770 private float wAccum = 0;
771 public void onMouseWheel(float mx, float my, float delta) {
773 int offset = (int) (wAccum / 5);
775 wAccum -= offset * 5;
776 setScrollOffset(scrollOffset + offset);
780 public void setScrollOffset(int offset) {
781 scrollOffset = constrain(offset, 0, max(0, items.size() - numVisibleItems));
782 scrollYStart = round(scrollOffset * h / items.size());
783 scrollYHeight = round(numVisibleItems * h / items.size());
787 public UIScrollList setItems(List<ScrollItem> items) {
789 numVisibleItems = (int) (h / itemHeight);
790 hasScroll = items.size() > numVisibleItems;
797 public interface ScrollItem {
798 public boolean isSelected();
799 public boolean isPending();
800 public String getLabel();
801 public void onMousePressed();
802 public void onMouseReleased();
805 public abstract class AbstractScrollItem implements ScrollItem {
806 public boolean isPending() {
809 public void select() {}
810 public void onMousePressed() {}
811 public void onMouseReleased() {}
814 public class UIIntegerBox extends UIObject {
816 private int minValue = 0;
817 private int maxValue = MAX_INT;
818 private int value = 0;
820 UIIntegerBox(float x, float y, float w, float h) {
824 public UIIntegerBox setRange(int minValue, int maxValue) {
825 this.minValue = minValue;
826 this.maxValue = maxValue;
827 setValue(constrain(value, minValue, maxValue));
831 protected void onDraw(PGraphics pg) {
835 pg.textAlign(CENTER, CENTER);
836 pg.textFont(defaultItemFont);
838 pg.text("" + value, w/2, h/2);
841 protected void onValueChange(int value) {}
844 protected void onMousePressed(float mx, float my) {
848 protected void onMouseDragged(float mx, float my, float dx, float dy) {
850 int offset = (int) (dAccum / 5);
851 dAccum = dAccum - (offset * 5);
852 setValue(value + offset);
855 public int getValue() {
859 public UIIntegerBox setValue(int value) {
860 if (this.value != value) {
861 int range = (maxValue - minValue + 1);
862 while (value < minValue) {
865 this.value = minValue + (value - minValue) % range;
866 this.onValueChange(this.value);