Commit | Line | Data |
---|---|---|
d626bc9b MS |
1 | /** |
2 | * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND | |
3 | * | |
4 | * //\\ //\\ //\\ //\\ | |
5 | * ///\\\ ///\\\ ///\\\ ///\\\ | |
6 | * \\\/// \\\/// \\\/// \\\/// | |
7 | * \\// \\// \\// \\// | |
8 | * | |
9 | * EXPERTS ONLY!! EXPERTS ONLY!! | |
10 | * | |
11 | * Little UI framework in progress to handle mouse events, layout, | |
12 | * redrawing, etc. | |
13 | */ | |
14 | ||
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); | |
21 | ||
22 | public abstract class UIObject { | |
23 | ||
24 | protected final List<UIObject> children = new ArrayList<UIObject>(); | |
25 | ||
26 | protected boolean needsRedraw = true; | |
27 | protected boolean childNeedsRedraw = true; | |
28 | ||
29 | protected float x=0, y=0, w=0, h=0; | |
30 | ||
31 | public UIContainer parent = null; | |
32 | ||
33 | protected boolean visible = true; | |
34 | ||
35 | public UIObject() {} | |
36 | ||
37 | public UIObject(float x, float y, float w, float h) { | |
38 | this.x = x; | |
39 | this.y = y; | |
40 | this.w = w; | |
41 | this.h = h; | |
42 | } | |
43 | ||
44 | public boolean isVisible() { | |
45 | return visible; | |
46 | } | |
47 | ||
48 | public UIObject setVisible(boolean visible) { | |
49 | if (visible != this.visible) { | |
50 | this.visible = visible; | |
51 | redraw(); | |
52 | } | |
53 | return this; | |
54 | } | |
55 | ||
56 | public final UIObject setPosition(float x, float y) { | |
57 | this.x = x; | |
58 | this.y = y; | |
59 | redraw(); | |
60 | return this; | |
61 | } | |
62 | ||
63 | public final UIObject setSize(float w, float h) { | |
64 | this.w = w; | |
65 | this.h = h; | |
66 | redraw(); | |
67 | return this; | |
68 | } | |
69 | ||
70 | public final UIObject addToContainer(UIContainer c) { | |
71 | c.children.add(this); | |
72 | this.parent = c; | |
73 | return this; | |
74 | } | |
75 | ||
76 | public final UIObject removeFromContainer(UIContainer c) { | |
77 | c.children.remove(this); | |
78 | this.parent = null; | |
79 | return this; | |
80 | } | |
81 | ||
82 | public final UIObject redraw() { | |
83 | _redraw(); | |
84 | UIObject p = this.parent; | |
85 | while (p != null) { | |
86 | p.childNeedsRedraw = true; | |
87 | p = p.parent; | |
88 | } | |
89 | return this; | |
90 | } | |
91 | ||
92 | private final void _redraw() { | |
93 | needsRedraw = true; | |
94 | for (UIObject child : children) { | |
95 | childNeedsRedraw = true; | |
96 | child._redraw(); | |
97 | } | |
98 | } | |
99 | ||
100 | public final void draw(PGraphics pg) { | |
101 | if (!visible) { | |
102 | return; | |
103 | } | |
104 | if (needsRedraw) { | |
105 | needsRedraw = false; | |
106 | onDraw(pg); | |
107 | } | |
108 | if (childNeedsRedraw) { | |
109 | childNeedsRedraw = false; | |
110 | for (UIObject child : children) { | |
111 | if (needsRedraw || child.needsRedraw || child.childNeedsRedraw) { | |
112 | pg.pushMatrix(); | |
113 | pg.translate(child.x, child.y); | |
114 | child.draw(pg); | |
115 | pg.popMatrix(); | |
116 | } | |
117 | } | |
118 | } | |
119 | } | |
120 | ||
121 | public final boolean contains(float x, float y) { | |
122 | return | |
123 | (x >= this.x && x < (this.x + this.w)) && | |
124 | (y >= this.y && y < (this.y + this.h)); | |
125 | } | |
126 | ||
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) {} | |
132 | } | |
133 | ||
134 | public class UIContainer extends UIObject { | |
135 | ||
136 | private UIObject focusedChild = null; | |
137 | ||
138 | public UIContainer() {} | |
139 | ||
140 | public UIContainer(float x, float y, float w, float h) { | |
141 | super(x, y, w, h); | |
142 | } | |
143 | ||
144 | public UIContainer(UIObject[] children) { | |
145 | for (UIObject child : children) { | |
146 | child.addToContainer(this); | |
147 | } | |
148 | } | |
149 | ||
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; | |
156 | break; | |
157 | } | |
158 | } | |
159 | } | |
160 | ||
161 | protected void onMouseReleased(float mx, float my) { | |
162 | if (focusedChild != null) { | |
163 | focusedChild.onMouseReleased(mx - focusedChild.x, my - focusedChild.y); | |
164 | } | |
165 | focusedChild = null; | |
166 | } | |
167 | ||
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); | |
171 | } | |
172 | } | |
173 | ||
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); | |
178 | } | |
179 | } | |
180 | } | |
181 | ||
182 | } | |
183 | ||
184 | public class UIContext extends UIContainer { | |
185 | ||
186 | final public PGraphics pg; | |
187 | ||
188 | UIContext(float x, float y, float w, float h) { | |
189 | super(x, y, w, h); | |
190 | pg = createGraphics((int)w, (int)h, JAVA2D); | |
191 | pg.smooth(); | |
192 | } | |
193 | ||
194 | public void draw() { | |
195 | if (!visible) { | |
196 | return; | |
197 | } | |
198 | if (needsRedraw || childNeedsRedraw) { | |
199 | pg.beginDraw(); | |
200 | draw(pg); | |
201 | pg.endDraw(); | |
202 | } | |
203 | image(pg, x, y); | |
204 | } | |
205 | ||
206 | private float px, py; | |
207 | private boolean dragging = false; | |
208 | ||
209 | public boolean mousePressed(float mx, float my) { | |
210 | if (!visible) { | |
211 | return false; | |
212 | } | |
213 | if (contains(mx, my)) { | |
214 | dragging = true; | |
215 | px = mx; | |
216 | py = my; | |
217 | onMousePressed(mx - x, my - y); | |
218 | return true; | |
219 | } | |
220 | return false; | |
221 | } | |
222 | ||
223 | public boolean mouseReleased(float mx, float my) { | |
224 | if (!visible) { | |
225 | return false; | |
226 | } | |
227 | dragging = false; | |
228 | onMouseReleased(mx - x, my - y); | |
229 | return true; | |
230 | } | |
231 | ||
232 | public boolean mouseDragged(float mx, float my) { | |
233 | if (!visible) { | |
234 | return false; | |
235 | } | |
236 | if (dragging) { | |
237 | float dx = mx - px; | |
238 | float dy = my - py; | |
239 | onMouseDragged(mx - x, my - y, dx, dy); | |
240 | px = mx; | |
241 | py = my; | |
242 | return true; | |
243 | } | |
244 | return false; | |
245 | } | |
246 | ||
247 | public boolean mouseWheel(float mx, float my, float delta) { | |
248 | if (!visible) { | |
249 | return false; | |
250 | } | |
251 | if (contains(mx, my)) { | |
252 | onMouseWheel(mx - x, my - y, delta); | |
253 | return true; | |
254 | } | |
255 | return false; | |
256 | } | |
257 | } | |
258 | ||
259 | public class UIWindow extends UIContext { | |
260 | ||
261 | protected final static int titleHeight = 24; | |
262 | ||
263 | public UIWindow(String label, float x, float y, float w, float h) { | |
264 | super(x, y, w, 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); | |
269 | } | |
270 | }.setLabel(label).setFont(defaultTitleFont).addToContainer(this); | |
271 | } | |
272 | ||
273 | protected void onDraw(PGraphics pg) { | |
274 | pg.noStroke(); | |
275 | pg.fill(#444444); | |
276 | pg.stroke(#292929); | |
277 | pg.rect(0, 0, w-1, h-1); | |
278 | } | |
279 | } | |
280 | ||
281 | public class UILabel extends UIObject { | |
282 | ||
283 | private PFont font = defaultTitleFont; | |
284 | private color fontColor = #CCCCCC; | |
285 | private String label = ""; | |
286 | ||
287 | public UILabel(float x, float y, float w, float h) { | |
288 | super(x, y, w, h); | |
289 | } | |
290 | ||
291 | protected void onDraw(PGraphics pg) { | |
292 | pg.textAlign(LEFT, TOP); | |
293 | pg.textFont(font); | |
294 | pg.fill(fontColor); | |
295 | pg.text(label, 0, 0); | |
296 | } | |
297 | ||
298 | public UILabel setFont(PFont font) { | |
299 | this.font = font; | |
300 | redraw(); | |
301 | return this; | |
302 | } | |
303 | ||
304 | public UILabel setFontColor(color fontColor) { | |
305 | this.fontColor = fontColor; | |
306 | redraw(); | |
307 | return this; | |
308 | } | |
309 | ||
310 | public UILabel setLabel(String label) { | |
311 | this.label = label; | |
312 | redraw(); | |
313 | return this; | |
314 | } | |
315 | } | |
316 | ||
317 | public class UIButton extends UIObject { | |
318 | ||
319 | private boolean active = false; | |
320 | private boolean isMomentary = false; | |
321 | private color borderColor = #666666; | |
322 | private color inactiveColor = #222222; | |
323 | private color activeColor = #669966; | |
324 | private color labelColor = #999999; | |
325 | private String label = ""; | |
326 | ||
327 | public UIButton(float x, float y, float w, float h) { | |
328 | super(x, y, w, h); | |
329 | } | |
330 | ||
331 | public UIButton setMomentary(boolean momentary) { | |
332 | isMomentary = momentary; | |
333 | return this; | |
334 | } | |
335 | ||
336 | protected void onDraw(PGraphics pg) { | |
337 | pg.stroke(borderColor); | |
338 | pg.fill(active ? activeColor : inactiveColor); | |
339 | pg.rect(0, 0, w, h); | |
340 | if (label != null && label.length() > 0) { | |
341 | pg.fill(active ? #FFFFFF : labelColor); | |
342 | pg.textFont(defaultItemFont); | |
343 | pg.textAlign(CENTER); | |
344 | pg.text(label, w/2, h-5); | |
345 | } | |
346 | } | |
347 | ||
348 | protected void onMousePressed(float mx, float my) { | |
349 | if (isMomentary) { | |
350 | setActive(true); | |
351 | } else { | |
352 | setActive(!active); | |
353 | } | |
354 | } | |
355 | ||
356 | protected void onMouseReleased(float mx, float my) { | |
357 | if (isMomentary) { | |
358 | setActive(false); | |
359 | } | |
360 | } | |
361 | ||
362 | public UIButton setActive(boolean active) { | |
363 | this.active = active; | |
364 | onToggle(active); | |
365 | redraw(); | |
366 | return this; | |
367 | } | |
368 | ||
369 | public UIButton toggle() { | |
370 | return setActive(!active); | |
371 | } | |
372 | ||
373 | protected void onToggle(boolean active) {} | |
374 | ||
375 | public UIButton setBorderColor(color borderColor) { | |
376 | if (this.borderColor != borderColor) { | |
377 | this.borderColor = borderColor; | |
378 | redraw(); | |
379 | } | |
380 | return this; | |
381 | } | |
382 | ||
383 | public UIButton setActiveColor(color activeColor) { | |
384 | if (this.activeColor != activeColor) { | |
385 | this.activeColor = activeColor; | |
386 | if (active) { | |
387 | redraw(); | |
388 | } | |
389 | } | |
390 | return this; | |
391 | } | |
392 | ||
393 | public UIButton setInactiveColor(color inactiveColor) { | |
394 | if (this.inactiveColor != inactiveColor) { | |
395 | this.inactiveColor = inactiveColor; | |
396 | if (!active) { | |
397 | redraw(); | |
398 | } | |
399 | } | |
400 | return this; | |
401 | } | |
402 | ||
403 | public UIButton setLabelColor(color labelColor) { | |
404 | if (this.labelColor != labelColor) { | |
405 | this.labelColor = labelColor; | |
406 | redraw(); | |
407 | } | |
408 | return this; | |
409 | } | |
410 | ||
411 | public UIButton setLabel(String label) { | |
412 | if (!this.label.equals(label)) { | |
413 | this.label = label; | |
414 | redraw(); | |
415 | } | |
416 | return this; | |
417 | } | |
418 | ||
419 | public void onMousePressed() { | |
420 | setActive(!active); | |
421 | } | |
422 | } | |
423 | ||
424 | public class UIToggleSet extends UIObject { | |
425 | ||
426 | private String[] options; | |
427 | private int[] boundaries; | |
428 | private String value; | |
429 | ||
430 | public UIToggleSet(float x, float y, float w, float h) { | |
431 | super(x, y, w, h); | |
432 | } | |
433 | ||
434 | public UIToggleSet setOptions(String[] options) { | |
435 | this.options = options; | |
436 | boundaries = new int[options.length]; | |
437 | int totalLength = 0; | |
438 | for (String s : options) { | |
439 | totalLength += s.length(); | |
440 | } | |
441 | int lengthSoFar = 0; | |
442 | for (int i = 0; i < options.length; ++i) { | |
443 | lengthSoFar += options[i].length(); | |
444 | boundaries[i] = (int) (lengthSoFar * w / totalLength); | |
445 | } | |
446 | value = options[0]; | |
447 | redraw(); | |
448 | return this; | |
449 | } | |
450 | ||
451 | public String getValue() { | |
452 | return value; | |
453 | } | |
454 | ||
455 | public UIToggleSet setValue(String option) { | |
456 | value = option; | |
457 | onToggle(value); | |
458 | redraw(); | |
459 | return this; | |
460 | } | |
461 | ||
462 | public void onDraw(PGraphics pg) { | |
463 | pg.stroke(#666666); | |
464 | pg.fill(#222222); | |
465 | pg.rect(0, 0, w, h); | |
466 | for (int b : boundaries) { | |
467 | pg.line(b, 1, b, h-1); | |
468 | } | |
469 | pg.noStroke(); | |
470 | pg.textAlign(CENTER); | |
471 | pg.textFont(defaultItemFont); | |
472 | int leftBoundary = 0; | |
473 | ||
474 | for (int i = 0; i < options.length; ++i) { | |
475 | boolean isActive = options[i] == value; | |
476 | if (isActive) { | |
477 | pg.fill(lightGreen); | |
478 | pg.rect(leftBoundary + 1, 1, boundaries[i] - leftBoundary - 1, h-1); | |
479 | } | |
480 | pg.fill(isActive ? #FFFFFF : #999999); | |
481 | pg.text(options[i], (leftBoundary + boundaries[i]) / 2., h-6); | |
482 | leftBoundary = boundaries[i]; | |
483 | } | |
484 | } | |
485 | ||
486 | public void onMousePressed(float mx, float my) { | |
487 | for (int i = 0; i < boundaries.length; ++i) { | |
488 | if (mx < boundaries[i]) { | |
489 | setValue(options[i]); | |
490 | break; | |
491 | } | |
492 | } | |
493 | } | |
494 | ||
495 | protected void onToggle(String option) {} | |
496 | ||
497 | } | |
498 | ||
499 | ||
500 | public abstract class UIParameterControl extends UIObject implements LXParameter.Listener { | |
501 | protected LXParameter parameter = null; | |
502 | ||
503 | protected UIParameterControl(float x, float y, float w, float h) { | |
504 | super(x, y, w, h); | |
505 | } | |
506 | ||
507 | public void onParameterChanged(LXParameter parameter) { | |
508 | redraw(); | |
509 | } | |
510 | ||
511 | public UIParameterControl setParameter(LXParameter parameter) { | |
512 | if (this.parameter != null) { | |
513 | if (this.parameter instanceof LXListenableParameter) { | |
514 | ((LXListenableParameter)this.parameter).removeListener(this); | |
515 | } | |
516 | } | |
517 | this.parameter = parameter; | |
518 | if (this.parameter != null) { | |
519 | if (this.parameter instanceof LXListenableParameter) { | |
520 | ((LXListenableParameter)this.parameter).addListener(this); | |
521 | } | |
522 | } | |
523 | redraw(); | |
524 | return this; | |
525 | } | |
526 | } | |
527 | ||
528 | public class UIParameterKnob extends UIParameterControl { | |
529 | private int knobSize = 28; | |
530 | private final float knobIndent = .4; | |
531 | private final int knobLabelHeight = 14; | |
532 | ||
533 | public UIParameterKnob(float x, float y) { | |
534 | this(x, y, 0, 0); | |
535 | setSize(knobSize, knobSize + knobLabelHeight); | |
536 | } | |
537 | ||
538 | public UIParameterKnob(float x, float y, float w, float h) { | |
539 | super(x, y, w, h); | |
540 | } | |
541 | ||
542 | protected void onDraw(PGraphics pg) { | |
543 | float knobValue = (parameter != null) ? parameter.getValuef() : 0; | |
544 | ||
545 | pg.ellipseMode(CENTER); | |
546 | pg.noStroke(); | |
547 | ||
548 | pg.fill(bgGray); | |
549 | pg.rect(0, 0, knobSize, knobSize); | |
550 | ||
551 | // Full outer dark ring | |
552 | pg.fill(#222222); | |
553 | pg.arc(knobSize/2, knobSize/2, knobSize, knobSize, HALF_PI + knobIndent, HALF_PI + knobIndent + (TWO_PI-2*knobIndent)); | |
554 | ||
555 | // Light ring indicating value | |
556 | pg.fill(lightGreen); | |
557 | pg.arc(knobSize/2, knobSize/2, knobSize, knobSize, HALF_PI + knobIndent, HALF_PI + knobIndent + knobValue*(TWO_PI-2*knobIndent)); | |
558 | ||
559 | // Center circle of knob | |
560 | pg.fill(#333333); | |
561 | pg.ellipse(knobSize/2, knobSize/2, knobSize/2, knobSize/2); | |
562 | ||
563 | String knobLabel = (parameter != null) ? parameter.getLabel() : null; | |
564 | if (knobLabel == null) { | |
565 | knobLabel = "-"; | |
566 | } else if (knobLabel.length() > 4) { | |
567 | knobLabel = knobLabel.substring(0, 4); | |
568 | } | |
569 | pg.fill(#000000); | |
570 | pg.rect(0, knobSize + 2, knobSize, knobLabelHeight - 2); | |
571 | pg.fill(#999999); | |
572 | pg.textAlign(CENTER); | |
573 | pg.textFont(defaultTitleFont); | |
574 | pg.text(knobLabel, knobSize/2, knobSize + knobLabelHeight - 2); | |
575 | } | |
576 | ||
577 | public void onMouseDragged(float mx, float my, float dx, float dy) { | |
578 | if (parameter != null) { | |
579 | float value = constrain(parameter.getValuef() - dy / 100., 0, 1); | |
580 | parameter.setValue(value); | |
581 | } | |
582 | } | |
583 | } | |
584 | ||
585 | public class UIParameterSlider extends UIParameterControl { | |
586 | ||
587 | private static final float handleWidth = 12; | |
588 | ||
589 | UIParameterSlider(float x, float y, float w, float h) { | |
590 | super(x, y, w, h); | |
591 | } | |
592 | ||
593 | protected void onDraw(PGraphics pg) { | |
594 | pg.noStroke(); | |
595 | pg.fill(#333333); | |
596 | pg.rect(0, 0, w, h); | |
597 | pg.fill(#222222); | |
598 | pg.rect(4, h/2-2, w-8, 4); | |
599 | pg.fill(#666666); | |
600 | pg.stroke(#222222); | |
601 | pg.rect((int) (4 + parameter.getValuef() * (w-8-handleWidth)), 4, handleWidth, h-8); | |
602 | } | |
603 | ||
604 | private boolean editing = false; | |
605 | protected void onMousePressed(float mx, float my) { | |
606 | float handleLeft = 4 + parameter.getValuef() * (w-8-handleWidth); | |
607 | if (mx >= handleLeft && mx < handleLeft + handleWidth) { | |
608 | editing = true; | |
609 | } | |
610 | } | |
611 | ||
612 | protected void onMouseReleased(float mx, float my) { | |
613 | editing = false; | |
614 | } | |
615 | ||
616 | protected void onMouseDragged(float mx, float my, float dx, float dy) { | |
617 | if (editing) { | |
618 | parameter.setValue(constrain((mx - handleWidth/2. - 4) / (w-8-handleWidth), 0, 1)); | |
619 | } | |
620 | } | |
621 | } | |
622 | ||
623 | public class UIScrollList extends UIObject { | |
624 | ||
625 | private List<ScrollItem> items = new ArrayList<ScrollItem>(); | |
626 | ||
627 | private PFont itemFont = defaultItemFont; | |
628 | private int itemHeight = 20; | |
629 | private color selectedColor = lightGreen; | |
630 | private color pendingColor = lightBlue; | |
631 | private int scrollOffset = 0; | |
632 | private int numVisibleItems = 0; | |
633 | ||
634 | private boolean hasScroll; | |
635 | private float scrollYStart; | |
636 | private float scrollYHeight; | |
637 | ||
638 | public UIScrollList(float x, float y, float w, float h) { | |
639 | super(x, y, w, h); | |
640 | } | |
641 | ||
642 | protected void onDraw(PGraphics pg) { | |
643 | int yp = 0; | |
644 | boolean even = true; | |
645 | for (int i = 0; i < numVisibleItems; ++i) { | |
646 | if (i + scrollOffset >= items.size()) { | |
647 | break; | |
648 | } | |
649 | ScrollItem item = items.get(i + scrollOffset); | |
650 | color itemColor; | |
651 | color labelColor = #FFFFFF; | |
652 | if (item.isSelected()) { | |
653 | itemColor = selectedColor; | |
654 | } else if (item.isPending()) { | |
655 | itemColor = pendingColor; | |
656 | } else { | |
657 | labelColor = #000000; | |
1f42cce7 | 658 | itemColor = #707070; |
d626bc9b | 659 | } |
1f42cce7 MS |
660 | float factor = even ? .92 : 1.08; |
661 | itemColor = color(hue(itemColor), saturation(itemColor), min(100, factor*brightness(itemColor))); | |
662 | ||
d626bc9b MS |
663 | pg.noStroke(); |
664 | pg.fill(itemColor); | |
665 | pg.rect(0, yp, w, itemHeight); | |
666 | pg.fill(labelColor); | |
667 | pg.textFont(itemFont); | |
668 | pg.textAlign(LEFT, TOP); | |
669 | pg.text(item.getLabel(), 6, yp+4); | |
670 | ||
671 | yp += itemHeight; | |
672 | even = !even; | |
673 | } | |
674 | if (hasScroll) { | |
675 | pg.noStroke(); | |
676 | pg.fill(color(0, 0, 100, 15)); | |
677 | pg.rect(w-12, 0, 12, h); | |
678 | pg.fill(#333333); | |
679 | pg.rect(w-12, scrollYStart, 12, scrollYHeight); | |
680 | } | |
681 | ||
682 | } | |
683 | ||
684 | private boolean scrolling = false; | |
685 | private ScrollItem pressedItem = null; | |
686 | ||
687 | public void onMousePressed(float mx, float my) { | |
688 | pressedItem = null; | |
689 | if (hasScroll && mx >= w-12) { | |
690 | if (my >= scrollYStart && my < (scrollYStart + scrollYHeight)) { | |
691 | scrolling = true; | |
692 | dAccum = 0; | |
693 | } | |
694 | } else { | |
695 | int index = (int) my / itemHeight; | |
696 | if (scrollOffset + index < items.size()) { | |
697 | pressedItem = items.get(scrollOffset + index); | |
698 | pressedItem.onMousePressed(); | |
d626bc9b MS |
699 | redraw(); |
700 | } | |
701 | } | |
702 | } | |
703 | ||
704 | public void onMouseReleased(float mx, float my) { | |
705 | scrolling = false; | |
706 | if (pressedItem != null) { | |
707 | pressedItem.onMouseReleased(); | |
708 | redraw(); | |
709 | } | |
710 | } | |
711 | ||
712 | private float dAccum = 0; | |
713 | public void onMouseDragged(float mx, float my, float dx, float dy) { | |
714 | if (scrolling) { | |
715 | dAccum += dy; | |
716 | float scrollOne = h / items.size(); | |
717 | int offset = (int) (dAccum / scrollOne); | |
718 | if (offset != 0) { | |
719 | dAccum -= offset * scrollOne; | |
720 | setScrollOffset(scrollOffset + offset); | |
721 | } | |
722 | } | |
723 | } | |
724 | ||
725 | private float wAccum = 0; | |
726 | public void onMouseWheel(float mx, float my, float delta) { | |
727 | wAccum += delta; | |
728 | int offset = (int) (wAccum / 5); | |
729 | if (offset != 0) { | |
730 | wAccum -= offset * 5; | |
731 | setScrollOffset(scrollOffset + offset); | |
732 | } | |
733 | } | |
734 | ||
735 | public void setScrollOffset(int offset) { | |
736 | scrollOffset = constrain(offset, 0, items.size() - numVisibleItems); | |
737 | scrollYStart = (int) (scrollOffset * h / items.size()); | |
738 | scrollYHeight = (int) (numVisibleItems * h / (float) items.size()); | |
739 | redraw(); | |
740 | } | |
741 | ||
742 | public UIScrollList setItems(List<ScrollItem> items) { | |
743 | this.items = items; | |
744 | numVisibleItems = (int) (h / itemHeight); | |
745 | hasScroll = items.size() > numVisibleItems; | |
746 | setScrollOffset(0); | |
747 | redraw(); | |
748 | return this; | |
749 | } | |
750 | } | |
751 | ||
752 | public interface ScrollItem { | |
753 | public boolean isSelected(); | |
754 | public boolean isPending(); | |
755 | public String getLabel(); | |
d626bc9b MS |
756 | public void onMousePressed(); |
757 | public void onMouseReleased(); | |
758 | } | |
759 | ||
760 | public abstract class AbstractScrollItem implements ScrollItem { | |
761 | public boolean isPending() { | |
762 | return false; | |
763 | } | |
764 | public void select() {} | |
765 | public void onMousePressed() {} | |
766 | public void onMouseReleased() {} | |
767 | } | |
768 | ||
769 | public class UIIntegerBox extends UIObject { | |
770 | ||
771 | private int minValue = 0; | |
772 | private int maxValue = MAX_INT; | |
773 | private int value = 0; | |
774 | ||
775 | UIIntegerBox(float x, float y, float w, float h) { | |
776 | super(x, y, w, h); | |
777 | } | |
778 | ||
779 | public UIIntegerBox setRange(int minValue, int maxValue) { | |
780 | this.minValue = minValue; | |
781 | this.maxValue = maxValue; | |
782 | setValue(constrain(value, minValue, maxValue)); | |
783 | return this; | |
784 | } | |
785 | ||
786 | protected void onDraw(PGraphics pg) { | |
78a44a1b | 787 | pg.stroke(#666666); |
d626bc9b MS |
788 | pg.fill(#222222); |
789 | pg.rect(0, 0, w, h); | |
790 | pg.textAlign(CENTER, CENTER); | |
791 | pg.textFont(defaultItemFont); | |
792 | pg.fill(#999999); | |
793 | pg.text("" + value, w/2, h/2); | |
794 | } | |
795 | ||
796 | protected void onValueChange(int value) {} | |
797 | ||
798 | float dAccum = 0; | |
799 | protected void onMousePressed(float mx, float my) { | |
800 | dAccum = 0; | |
801 | } | |
802 | ||
803 | protected void onMouseDragged(float mx, float my, float dx, float dy) { | |
804 | dAccum -= dy; | |
805 | int offset = (int) (dAccum / 5); | |
806 | dAccum = dAccum - (offset * 5); | |
807 | setValue(value + offset); | |
808 | } | |
809 | ||
810 | public int getValue() { | |
811 | return value; | |
812 | } | |
813 | ||
814 | public UIIntegerBox setValue(int value) { | |
815 | if (this.value != value) { | |
816 | int range = (maxValue - minValue + 1); | |
817 | while (value < minValue) { | |
818 | value += range; | |
819 | } | |
820 | this.value = minValue + (value - minValue) % range; | |
821 | this.onValueChange(this.value); | |
822 | redraw(); | |
823 | } | |
824 | return this; | |
825 | } | |
826 | } |