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 static int DOUBLE_CLICK_THRESHOLD = 300;
26 protected final List<UIObject> children = new ArrayList<UIObject>();
28 protected boolean needsRedraw = true;
29 protected boolean childNeedsRedraw = true;
31 protected float x=0, y=0, w=0, h=0;
33 public UIContainer parent = null;
35 protected boolean visible = true;
39 public UIObject(float x, float y, float w, float h) {
46 public boolean isVisible() {
50 public UIObject setVisible(boolean visible) {
51 if (visible != this.visible) {
52 this.visible = visible;
58 public final UIObject setPosition(float x, float y) {
65 public final UIObject setSize(float w, float h) {
72 public final UIObject addToContainer(UIContainer c) {
78 public final UIObject removeFromContainer(UIContainer c) {
79 c.children.remove(this);
84 public final UIObject redraw() {
86 UIObject p = this.parent;
88 p.childNeedsRedraw = true;
94 private final void _redraw() {
96 for (UIObject child : children) {
97 childNeedsRedraw = true;
102 public final void draw(PGraphics pg) {
110 if (childNeedsRedraw) {
111 childNeedsRedraw = false;
112 for (UIObject child : children) {
113 if (needsRedraw || child.needsRedraw || child.childNeedsRedraw) {
115 pg.translate(child.x, child.y);
123 public final boolean contains(float x, float y) {
125 (x >= this.x && x < (this.x + this.w)) &&
126 (y >= this.y && y < (this.y + this.h));
129 protected void onDraw(PGraphics pg) {}
130 protected void onMousePressed(float mx, float my) {}
131 protected void onMouseReleased(float mx, float my) {}
132 protected void onMouseDragged(float mx, float my, float dx, float dy) {}
133 protected void onMouseWheel(float mx, float my, float dx) {}
136 public class UIContainer extends UIObject {
138 private UIObject focusedChild = null;
140 public UIContainer() {}
142 public UIContainer(float x, float y, float w, float h) {
146 public UIContainer(UIObject[] children) {
147 for (UIObject child : children) {
148 child.addToContainer(this);
152 protected void onMousePressed(float mx, float my) {
153 for (int i = children.size() - 1; i >= 0; --i) {
154 UIObject child = children.get(i);
155 if (child.contains(mx, my)) {
156 child.onMousePressed(mx - child.x, my - child.y);
157 focusedChild = child;
163 protected void onMouseReleased(float mx, float my) {
164 if (focusedChild != null) {
165 focusedChild.onMouseReleased(mx - focusedChild.x, my - focusedChild.y);
170 protected void onMouseDragged(float mx, float my, float dx, float dy) {
171 if (focusedChild != null) {
172 focusedChild.onMouseDragged(mx - focusedChild.x, my - focusedChild.y, dx, dy);
176 protected void onMouseWheel(float mx, float my, float delta) {
177 for (UIObject child : children) {
178 if (child.contains(mx, my)) {
179 child.onMouseWheel(mx - child.x, mx - child.y, delta);
186 public class UIContext extends UIContainer {
188 final public PGraphics pg;
190 UIContext(float x, float y, float w, float h) {
192 pg = createGraphics((int)w, (int)h, JAVA2D);
200 if (needsRedraw || childNeedsRedraw) {
208 private float px, py;
209 private boolean dragging = false;
211 public boolean mousePressed(float mx, float my) {
215 if (contains(mx, my)) {
219 onMousePressed(mx - x, my - y);
225 public boolean mouseReleased(float mx, float my) {
230 onMouseReleased(mx - x, my - y);
234 public boolean mouseDragged(float mx, float my) {
241 onMouseDragged(mx - x, my - y, dx, dy);
249 public boolean mouseWheel(float mx, float my, float delta) {
253 if (contains(mx, my)) {
254 onMouseWheel(mx - x, my - y, delta);
261 public class UIWindow extends UIContext {
263 protected final static int titleHeight = 24;
265 public UIWindow(String label, float x, float y, float w, float h) {
267 new UILabel(6, 8, w-6, titleHeight-8) {
268 protected void onMouseDragged(float mx, float my, float dx, float dy) {
269 parent.x = constrain(parent.x + dx, 0, width - w);
270 parent.y = constrain(parent.y + dy, 0, height - h);
272 }.setLabel(label).setFont(defaultTitleFont).addToContainer(this);
275 protected void onDraw(PGraphics pg) {
279 pg.rect(0, 0, w-1, h-1);
283 public class UILabel extends UIObject {
285 private PFont font = defaultTitleFont;
286 private color fontColor = #CCCCCC;
287 private String label = "";
289 public UILabel(float x, float y, float w, float h) {
293 protected void onDraw(PGraphics pg) {
294 pg.textAlign(LEFT, TOP);
297 pg.text(label, 0, 0);
300 public UILabel setFont(PFont font) {
306 public UILabel setFontColor(color fontColor) {
307 this.fontColor = fontColor;
312 public UILabel setLabel(String label) {
319 public class UICheckbox extends UIButton {
321 private boolean firstDraw = true;
323 public UICheckbox(float x, float y, float w, float h) {
328 public void onDraw(PGraphics pg) {
329 pg.stroke(borderColor);
330 pg.fill(active ? activeColor : inactiveColor);
334 pg.textFont(defaultItemFont);
335 pg.textAlign(LEFT, CENTER);
336 pg.text(label, h + 4, h/2);
343 public class UIButton extends UIObject {
345 protected boolean active = false;
346 protected boolean isMomentary = false;
347 protected color borderColor = #666666;
348 protected color inactiveColor = #222222;
349 protected color activeColor = #669966;
350 protected color labelColor = #999999;
351 protected String label = "";
353 public UIButton(float x, float y, float w, float h) {
357 public UIButton setMomentary(boolean momentary) {
358 isMomentary = momentary;
362 protected void onDraw(PGraphics pg) {
363 pg.stroke(borderColor);
364 pg.fill(active ? activeColor : inactiveColor);
366 if (label != null && label.length() > 0) {
367 pg.fill(active ? #FFFFFF : labelColor);
368 pg.textFont(defaultItemFont);
369 pg.textAlign(CENTER);
370 pg.text(label, w/2, h-5);
374 protected void onMousePressed(float mx, float my) {
382 protected void onMouseReleased(float mx, float my) {
388 public boolean isActive() {
392 public UIButton setActive(boolean active) {
393 this.active = active;
399 public UIButton toggle() {
400 return setActive(!active);
403 protected void onToggle(boolean active) {}
405 public UIButton setBorderColor(color borderColor) {
406 if (this.borderColor != borderColor) {
407 this.borderColor = borderColor;
413 public UIButton setActiveColor(color activeColor) {
414 if (this.activeColor != activeColor) {
415 this.activeColor = activeColor;
423 public UIButton setInactiveColor(color inactiveColor) {
424 if (this.inactiveColor != inactiveColor) {
425 this.inactiveColor = inactiveColor;
433 public UIButton setLabelColor(color labelColor) {
434 if (this.labelColor != labelColor) {
435 this.labelColor = labelColor;
441 public UIButton setLabel(String label) {
442 if (!this.label.equals(label)) {
449 public void onMousePressed() {
454 public class UIToggleSet extends UIObject {
456 private String[] options;
457 private int[] boundaries;
458 private String value;
460 public UIToggleSet(float x, float y, float w, float h) {
464 public UIToggleSet setOptions(String[] options) {
465 this.options = options;
466 boundaries = new int[options.length];
468 for (String s : options) {
469 totalLength += s.length();
472 for (int i = 0; i < options.length; ++i) {
473 lengthSoFar += options[i].length();
474 boundaries[i] = (int) (lengthSoFar * w / totalLength);
481 public String getValue() {
485 public UIToggleSet setValue(String option) {
492 public void onDraw(PGraphics pg) {
496 for (int b : boundaries) {
497 pg.line(b, 1, b, h-1);
500 pg.textAlign(CENTER);
501 pg.textFont(defaultItemFont);
502 int leftBoundary = 0;
504 for (int i = 0; i < options.length; ++i) {
505 boolean isActive = options[i] == value;
508 pg.rect(leftBoundary + 1, 1, boundaries[i] - leftBoundary - 1, h-1);
510 pg.fill(isActive ? #FFFFFF : #999999);
511 pg.text(options[i], (leftBoundary + boundaries[i]) / 2., h-6);
512 leftBoundary = boundaries[i];
516 public void onMousePressed(float mx, float my) {
517 for (int i = 0; i < boundaries.length; ++i) {
518 if (mx < boundaries[i]) {
519 setValue(options[i]);
525 protected void onToggle(String option) {}
530 public abstract class UIParameterControl extends UIObject implements LXParameterListener {
531 protected LXParameter parameter = null;
533 protected UIParameterControl(float x, float y, float w, float h) {
537 public void onParameterChanged(LXParameter parameter) {
541 protected float getNormalized() {
542 if (parameter != null) {
543 if (parameter instanceof BasicParameter) {
544 return ((BasicParameter)parameter).getNormalizedf();
546 return parameter.getValuef();
551 protected UIParameterControl setNormalized(float value) {
552 if (parameter != null) {
553 if (parameter instanceof BasicParameter) {
554 ((BasicParameter)parameter).setNormalized(value);
556 parameter.setValue(value);
562 public UIParameterControl setParameter(LXParameter parameter) {
563 if (this.parameter != null) {
564 if (this.parameter instanceof LXListenableParameter) {
565 ((LXListenableParameter)this.parameter).removeListener(this);
568 this.parameter = parameter;
569 if (this.parameter != null) {
570 if (this.parameter instanceof LXListenableParameter) {
571 ((LXListenableParameter)this.parameter).addListener(this);
579 public class UIParameterKnob extends UIParameterControl {
580 private int knobSize = 28;
581 private final float knobIndent = .4;
582 private final int knobLabelHeight = 14;
583 private boolean showValue = false;
585 public UIParameterKnob(float x, float y) {
587 setSize(knobSize, knobSize + knobLabelHeight);
590 public UIParameterKnob(float x, float y, float w, float h) {
594 protected void onDraw(PGraphics pg) {
595 float knobValue = getNormalized();
597 pg.ellipseMode(CENTER);
601 pg.rect(0, 0, knobSize, knobSize);
603 // Full outer dark ring
605 pg.arc(knobSize/2, knobSize/2, knobSize, knobSize, HALF_PI + knobIndent, HALF_PI + knobIndent + (TWO_PI-2*knobIndent));
607 // Light ring indicating value
609 pg.arc(knobSize/2, knobSize/2, knobSize, knobSize, HALF_PI + knobIndent, HALF_PI + knobIndent + knobValue*(TWO_PI-2*knobIndent));
611 // Center circle of knob
613 pg.ellipse(knobSize/2, knobSize/2, knobSize/2, knobSize/2);
617 knobLabel = (parameter != null) ? ("" + parameter.getValue()) : null;
619 knobLabel = (parameter != null) ? parameter.getLabel() : null;
621 if (knobLabel == null) {
623 } else if (knobLabel.length() > 4) {
624 knobLabel = knobLabel.substring(0, 4);
627 pg.rect(0, knobSize + 2, knobSize, knobLabelHeight - 2);
629 pg.textAlign(CENTER);
630 pg.textFont(defaultTitleFont);
631 pg.text(knobLabel, knobSize/2, knobSize + knobLabelHeight - 2);
634 private long lastMousePress = 0;
635 public void onMousePressed(float mx, float my) {
636 super.onMousePressed(mx, my);
638 if (now - lastMousePress < DOUBLE_CLICK_THRESHOLD) {
642 lastMousePress = now;
648 public void onMouseReleased(float mx, float my) {
653 public void onMouseDragged(float mx, float my, float dx, float dy) {
654 float value = constrain(getNormalized() - dy / 100., 0, 1);
655 setNormalized(value);
659 public class UIParameterSlider extends UIParameterControl {
661 private static final float handleWidth = 12;
663 UIParameterSlider(float x, float y, float w, float h) {
667 protected void onDraw(PGraphics pg) {
672 pg.rect(4, h/2-2, w-8, 4);
675 pg.rect((int) (4 + parameter.getValuef() * (w-8-handleWidth)), 4, handleWidth, h-8);
678 private boolean editing = false;
679 private long lastClick = 0;
680 private float doubleClickMode = 0;
681 private float doubleClickX = 0;
682 protected void onMousePressed(float mx, float my) {
684 float handleLeft = 4 + getNormalized() * (w-8-handleWidth);
685 if (mx >= handleLeft && mx < handleLeft + handleWidth) {
688 if ((now - lastClick) < DOUBLE_CLICK_THRESHOLD && abs(mx - doubleClickX) < 3) {
689 setNormalized(doubleClickMode);
694 } else if (mx > w*.75) {
697 doubleClickMode = 0.5;
703 protected void onMouseReleased(float mx, float my) {
707 protected void onMouseDragged(float mx, float my, float dx, float dy) {
709 setNormalized(constrain((mx - handleWidth/2. - 4) / (w-8-handleWidth), 0, 1));
714 public class UIScrollList extends UIObject {
716 private List<ScrollItem> items = new ArrayList<ScrollItem>();
718 private PFont itemFont = defaultItemFont;
719 private int itemHeight = 20;
720 private color selectedColor = lightGreen;
721 private color pendingColor = lightBlue;
722 private int scrollOffset = 0;
723 private int numVisibleItems = 0;
725 private boolean hasScroll;
726 private float scrollYStart;
727 private float scrollYHeight;
729 public UIScrollList(float x, float y, float w, float h) {
733 protected void onDraw(PGraphics pg) {
736 for (int i = 0; i < numVisibleItems; ++i) {
737 if (i + scrollOffset >= items.size()) {
740 ScrollItem item = items.get(i + scrollOffset);
742 color labelColor = #FFFFFF;
743 if (item.isSelected()) {
744 itemColor = selectedColor;
745 } else if (item.isPending()) {
746 itemColor = pendingColor;
748 labelColor = #000000;
751 float factor = even ? .92 : 1.08;
752 itemColor = lx.scaleBrightness(itemColor, factor);
756 pg.rect(0, yp, w, itemHeight);
758 pg.textFont(itemFont);
759 pg.textAlign(LEFT, TOP);
760 pg.text(item.getLabel(), 6, yp+4);
768 pg.rect(w-12, 0, 12, h);
770 pg.rect(w-12, scrollYStart, 12, scrollYHeight);
775 private boolean scrolling = false;
776 private ScrollItem pressedItem = null;
778 public void onMousePressed(float mx, float my) {
780 if (hasScroll && mx >= w-12) {
781 if (my >= scrollYStart && my < (scrollYStart + scrollYHeight)) {
786 int index = (int) my / itemHeight;
787 if (scrollOffset + index < items.size()) {
788 pressedItem = items.get(scrollOffset + index);
789 pressedItem.onMousePressed();
795 public void onMouseReleased(float mx, float my) {
797 if (pressedItem != null) {
798 pressedItem.onMouseReleased();
803 private float dAccum = 0;
804 public void onMouseDragged(float mx, float my, float dx, float dy) {
807 float scrollOne = h / items.size();
808 int offset = (int) (dAccum / scrollOne);
810 dAccum -= offset * scrollOne;
811 setScrollOffset(scrollOffset + offset);
816 private float wAccum = 0;
817 public void onMouseWheel(float mx, float my, float delta) {
819 int offset = (int) (wAccum / 5);
821 wAccum -= offset * 5;
822 setScrollOffset(scrollOffset + offset);
826 public void setScrollOffset(int offset) {
827 scrollOffset = constrain(offset, 0, max(0, items.size() - numVisibleItems));
828 scrollYStart = round(scrollOffset * h / items.size());
829 scrollYHeight = round(numVisibleItems * h / items.size());
833 public UIScrollList setItems(List<ScrollItem> items) {
835 numVisibleItems = (int) (h / itemHeight);
836 hasScroll = items.size() > numVisibleItems;
843 public interface ScrollItem {
844 public boolean isSelected();
845 public boolean isPending();
846 public String getLabel();
847 public void onMousePressed();
848 public void onMouseReleased();
851 public abstract class AbstractScrollItem implements ScrollItem {
852 public boolean isPending() {
855 public void select() {}
856 public void onMousePressed() {}
857 public void onMouseReleased() {}
860 public class UIIntegerBox extends UIObject {
862 private int minValue = 0;
863 private int maxValue = MAX_INT;
864 private int value = 0;
866 UIIntegerBox(float x, float y, float w, float h) {
870 public UIIntegerBox setRange(int minValue, int maxValue) {
871 this.minValue = minValue;
872 this.maxValue = maxValue;
873 setValue(constrain(value, minValue, maxValue));
877 protected void onDraw(PGraphics pg) {
881 pg.textAlign(CENTER, CENTER);
882 pg.textFont(defaultItemFont);
884 pg.text("" + value, w/2, h/2);
887 protected void onValueChange(int value) {}
890 protected void onMousePressed(float mx, float my) {
894 protected void onMouseDragged(float mx, float my, float dx, float dy) {
896 int offset = (int) (dAccum / 5);
897 dAccum = dAccum - (offset * 5);
898 setValue(value + offset);
901 public int getValue() {
905 public UIIntegerBox setValue(int value) {
906 if (this.value != value) {
907 int range = (maxValue - minValue + 1);
908 while (value < minValue) {
911 this.value = minValue + (value - minValue) % range;
912 this.onValueChange(this.value);