| 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; |
| 658 | itemColor = even ? #666666 : #777777; |
| 659 | } |
| 660 | pg.noStroke(); |
| 661 | pg.fill(itemColor); |
| 662 | pg.rect(0, yp, w, itemHeight); |
| 663 | pg.fill(labelColor); |
| 664 | pg.textFont(itemFont); |
| 665 | pg.textAlign(LEFT, TOP); |
| 666 | pg.text(item.getLabel(), 6, yp+4); |
| 667 | |
| 668 | yp += itemHeight; |
| 669 | even = !even; |
| 670 | } |
| 671 | if (hasScroll) { |
| 672 | pg.noStroke(); |
| 673 | pg.fill(color(0, 0, 100, 15)); |
| 674 | pg.rect(w-12, 0, 12, h); |
| 675 | pg.fill(#333333); |
| 676 | pg.rect(w-12, scrollYStart, 12, scrollYHeight); |
| 677 | } |
| 678 | |
| 679 | } |
| 680 | |
| 681 | private boolean scrolling = false; |
| 682 | private ScrollItem pressedItem = null; |
| 683 | |
| 684 | public void onMousePressed(float mx, float my) { |
| 685 | pressedItem = null; |
| 686 | if (hasScroll && mx >= w-12) { |
| 687 | if (my >= scrollYStart && my < (scrollYStart + scrollYHeight)) { |
| 688 | scrolling = true; |
| 689 | dAccum = 0; |
| 690 | } |
| 691 | } else { |
| 692 | int index = (int) my / itemHeight; |
| 693 | if (scrollOffset + index < items.size()) { |
| 694 | pressedItem = items.get(scrollOffset + index); |
| 695 | pressedItem.onMousePressed(); |
| 696 | pressedItem.select(); |
| 697 | redraw(); |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | public void onMouseReleased(float mx, float my) { |
| 703 | scrolling = false; |
| 704 | if (pressedItem != null) { |
| 705 | pressedItem.onMouseReleased(); |
| 706 | redraw(); |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | private float dAccum = 0; |
| 711 | public void onMouseDragged(float mx, float my, float dx, float dy) { |
| 712 | if (scrolling) { |
| 713 | dAccum += dy; |
| 714 | float scrollOne = h / items.size(); |
| 715 | int offset = (int) (dAccum / scrollOne); |
| 716 | if (offset != 0) { |
| 717 | dAccum -= offset * scrollOne; |
| 718 | setScrollOffset(scrollOffset + offset); |
| 719 | } |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | private float wAccum = 0; |
| 724 | public void onMouseWheel(float mx, float my, float delta) { |
| 725 | wAccum += delta; |
| 726 | int offset = (int) (wAccum / 5); |
| 727 | if (offset != 0) { |
| 728 | wAccum -= offset * 5; |
| 729 | setScrollOffset(scrollOffset + offset); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | public void setScrollOffset(int offset) { |
| 734 | scrollOffset = constrain(offset, 0, items.size() - numVisibleItems); |
| 735 | scrollYStart = (int) (scrollOffset * h / items.size()); |
| 736 | scrollYHeight = (int) (numVisibleItems * h / (float) items.size()); |
| 737 | redraw(); |
| 738 | } |
| 739 | |
| 740 | public UIScrollList setItems(List<ScrollItem> items) { |
| 741 | this.items = items; |
| 742 | numVisibleItems = (int) (h / itemHeight); |
| 743 | hasScroll = items.size() > numVisibleItems; |
| 744 | setScrollOffset(0); |
| 745 | redraw(); |
| 746 | return this; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | public interface ScrollItem { |
| 751 | public boolean isSelected(); |
| 752 | public boolean isPending(); |
| 753 | public String getLabel(); |
| 754 | public void select(); |
| 755 | public void onMousePressed(); |
| 756 | public void onMouseReleased(); |
| 757 | } |
| 758 | |
| 759 | public abstract class AbstractScrollItem implements ScrollItem { |
| 760 | public boolean isPending() { |
| 761 | return false; |
| 762 | } |
| 763 | public void select() {} |
| 764 | public void onMousePressed() {} |
| 765 | public void onMouseReleased() {} |
| 766 | } |
| 767 | |
| 768 | public class UIIntegerBox extends UIObject { |
| 769 | |
| 770 | private int minValue = 0; |
| 771 | private int maxValue = MAX_INT; |
| 772 | private int value = 0; |
| 773 | |
| 774 | UIIntegerBox(float x, float y, float w, float h) { |
| 775 | super(x, y, w, h); |
| 776 | } |
| 777 | |
| 778 | public UIIntegerBox setRange(int minValue, int maxValue) { |
| 779 | this.minValue = minValue; |
| 780 | this.maxValue = maxValue; |
| 781 | setValue(constrain(value, minValue, maxValue)); |
| 782 | return this; |
| 783 | } |
| 784 | |
| 785 | protected void onDraw(PGraphics pg) { |
| 786 | pg.stroke(#666666); |
| 787 | pg.fill(#222222); |
| 788 | pg.rect(0, 0, w, h); |
| 789 | pg.textAlign(CENTER, CENTER); |
| 790 | pg.textFont(defaultItemFont); |
| 791 | pg.fill(#999999); |
| 792 | pg.text("" + value, w/2, h/2); |
| 793 | } |
| 794 | |
| 795 | protected void onValueChange(int value) {} |
| 796 | |
| 797 | float dAccum = 0; |
| 798 | protected void onMousePressed(float mx, float my) { |
| 799 | dAccum = 0; |
| 800 | } |
| 801 | |
| 802 | protected void onMouseDragged(float mx, float my, float dx, float dy) { |
| 803 | dAccum -= dy; |
| 804 | int offset = (int) (dAccum / 5); |
| 805 | dAccum = dAccum - (offset * 5); |
| 806 | setValue(value + offset); |
| 807 | } |
| 808 | |
| 809 | public int getValue() { |
| 810 | return value; |
| 811 | } |
| 812 | |
| 813 | public UIIntegerBox setValue(int value) { |
| 814 | if (this.value != value) { |
| 815 | int range = (maxValue - minValue + 1); |
| 816 | while (value < minValue) { |
| 817 | value += range; |
| 818 | } |
| 819 | this.value = minValue + (value - minValue) % range; |
| 820 | this.onValueChange(this.value); |
| 821 | redraw(); |
| 822 | } |
| 823 | return this; |
| 824 | } |
| 825 | } |