forked from yangsu/Synaptic-Wall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearSlider.pde
43 lines (37 loc) · 1.42 KB
/
LinearSlider.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class LinearSlider extends Slider {
public LinearSlider(float x, float y, float size, float val, float min, float max, int id, Controllable target) {
this(x, y, size, SLIDER_BAR_WIDTH, val, min, max, id, target);
}
public LinearSlider(float x, float y, float size, float thickness, float val, float min, float max, int id, Controllable target) {
super(x, y, size, thickness, x, x + size, val, min, max, id, target);
}
public void drawBackground() {
pushStyle();
fill(SLIDER_BG_COLOR);
rect(fLoc.x, fLoc.y, fSize, fThickness);
popStyle();
}
public void drawForeground() {
pushStyle();
fill((fHover) ? HIGHLIGHT_COLOR : SLIDER_BAR_COLOR);
rect(fLoc.x, fLoc.y, fSlider - fBegin, fThickness);
stroke(CP_TEXT_COLOR);
strokeWeight(1);
String s = fLabel + " : " + nf(fValue, 1, 2);
float offset = textWidth(s)/2;
text(s, (fBegin + fEnd)/2 - offset, fLoc.y - SLIDER_LABEL_OFFSET);
popStyle();
}
public boolean isInBounds(float x, float y) {
return x >= fBegin && x <= fEnd && y >= fLoc.y && y <= fLoc.y + fThickness;
}
public void updateSlider(float x, float y) {
// TODO: add custom constrain function for correct snapping behavior
fSlider = constrain(x, fBegin, fEnd);
fValue = map(fSlider, fBegin, fEnd, fMin, fMax);
if (fTarget != null) fTarget.onEvent(fID, fValue);
}
public void setVisible(boolean visible) {
fVisible = true;
}
}