-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrollbar.pde
73 lines (67 loc) · 1.52 KB
/
scrollbar.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class VScrollbar
{
int swidth, sheight;
int xpos, ypos;
float spos, newspos;
int sposMin, sposMax;
int loose;
boolean over;
boolean locked;
float ratio;
VScrollbar (int xp, int yp, int sw, int sh, int l) {
swidth = sw;
sheight = sh;
int heighttowidth = sh - sw;
ratio = (float)sh / (float)heighttowidth;
xpos = xp-swidth/2;
ypos = yp;
spos = ypos + sheight/2 - swidth/2;
newspos = spos;
sposMin = ypos;
sposMax = ypos + sheight - swidth;
loose = l;
}
void update() {
if(over()) {
over = true;
} else {
over = false;
}
if(mousePressed && over) {
locked = true;
}
if(!mousePressed) {
locked = false;
}
if(locked) {
newspos = constrain(mouseY-swidth/2, sposMin, sposMax);
}
if(abs(newspos - spos) > 1) {
spos = spos + (newspos-spos)/loose;
}
}
int constrain(int val, int minv, int maxv) {
return min(max(val, minv), maxv);
}
boolean over() {
if(mouseX > xpos && mouseX < xpos+swidth &&
mouseY > ypos && mouseY < ypos+sheight) {
return true;
} else {
return false;
}
}
void display() {
fill(255);
rect(xpos, ypos, swidth, sheight);
if(over || locked) {
fill(255, 255, 0);
} else {
fill(255, 0, 0);
}
rect(xpos, spos, swidth, swidth);
}
double getPos() {
return (spos-ypos) / (sheight-swidth);
}
}