-
Notifications
You must be signed in to change notification settings - Fork 2
/
VScrollBar.pde
114 lines (100 loc) · 2.74 KB
/
VScrollBar.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
public class VScrollBar extends Component {
private int x;
private int y;
private int barWidth; // Bar width
private int barHeight; // Bar height
private int sliderPos; // Vertical position of the slider
private int sliderHeight; // Slider height
private int scrollRange; // Vertical scrolling range
private boolean mouseOverBar;
private boolean mouseOverSlider;
private boolean mousePress;
public VScrollBar(int barWidth, int barHeight, int scrollRange) {
this.barWidth = barWidth;
this.barHeight = barHeight;
this.scrollRange = scrollRange;
this.sliderPos = 0;
this.mouseOverBar = false;
this.mouseOverSlider = false;
this.mousePress = false;
if(scrollRange <= barHeight)
this.sliderHeight = barHeight;
else
this.sliderHeight = (barHeight*barHeight)/scrollRange;
}
public void setup() {
}
public void update() {
this.x = (int) modelX(0, 0, 0);
this.y = (int) modelY(0, 0, 0);
if (mouseX > x && mouseX < x+barWidth &&
mouseY > y+sliderPos && mouseY < y+sliderPos+sliderHeight) {
mouseOverSlider = true;
mouseOverBar = true;
} else if (mouseX > x && mouseX < x+barWidth &&
mouseY > y && mouseY < y+barHeight) {
mouseOverSlider = false;
mouseOverBar = true;
} else {
mouseOverSlider = false;
mouseOverBar = false;
}
}
public int getHeight() {
return this.barHeight;
}
public int getWidth() {
return this.barWidth;
}
public void mouseEvent(int eventType) {
switch(eventType) {
case MOUSE_PRESSED:
if(mouseOverSlider)
mousePress = true;
else if(mouseOverBar) {
mousePress = true;
sliderPos = mouseY - y - sliderHeight/2;
}
break;
case MOUSE_RELEASED:
mousePress = false;
break;
case MOUSE_DRAGGED:
if(mousePress) {
if(mouseY > y && mouseY < y+barHeight)
sliderPos += mouseY - pmouseY;
}
break;
case MOUSE_WHEEL_UP:
sliderPos -= sliderHeight/5;
break;
case MOUSE_WHEEL_DOWN:
sliderPos += sliderHeight/5;
break;
}
if(sliderPos < 0)
sliderPos = 0;
else if(sliderPos+sliderHeight > barHeight)
sliderPos = barHeight-sliderHeight;
}
public void draw() {
noStroke();
// Draw bar
fill(204);
rect(0, 0, barWidth, barHeight);
// Draw slider
if (mouseOverSlider) {
fill(0, 0, 0);
} else {
fill(102, 102, 102);
}
rect(0, sliderPos, barWidth, sliderHeight);
}
public float getPos() {
float pct = sliderPos/(float) (barHeight - sliderHeight);
if(barHeight >= scrollRange)
return 0;
else
return (barHeight-scrollRange)*pct;
}
}