-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHorizontalScrollBar.pde
118 lines (111 loc) · 3.17 KB
/
HorizontalScrollBar.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
115
116
117
118
/**
* Implement a simple horizontal scrollbar. Code adapted from the Scrollbar sketch in the GUI examples
* included with the processing PDE.
*
* The code is setup to implement a horizontal scroll-bar with a slider inside.
*
* Anthony Brown Jan 2017
*/
class HorizontalScrollBar {
int scrollBarWidth, scrollBarHeight; // width and height of bar
int sliderWidth, sliderHeight; // Width and height of slider
float xPositionBar, yPositionBar; // x and y position of bar
float sliderPosition, newSliderPosition; // x position of slider
float sliderPositionMinimum, sliderPositionMaximum; // max and min values of slider
boolean over; // is the mouse over the slider?
boolean locked;
float sliderPositionNormalization;
/**
* Constructor.
*
* @param xp
* X-position of scroll-bar.
* @param yp
* Y-position of scroll-bar.
* @param sw
* Width of scroll-bar.
* @param sh
* Height of scroll-bar.
*/
HorizontalScrollBar (float xp, float yp, int sw, int sh) {
scrollBarWidth = sw;
scrollBarHeight = sh;
sliderWidth = scrollBarHeight;
sliderHeight = scrollBarHeight;
sliderPositionNormalization = 1.0 / (float) (scrollBarWidth - sliderWidth);
xPositionBar = xp;
yPositionBar = yp-scrollBarHeight/2;
sliderPosition = xPositionBar;
newSliderPosition = sliderPosition;
sliderPositionMinimum = sliderPosition;
sliderPositionMaximum = xPositionBar + scrollBarWidth - scrollBarHeight;
}
/**
* Update the status of the scroll-bar.
*/
void update() {
if (overEvent()) {
over = true;
} else {
over = false;
}
if (mousePressed && over) {
locked = true;
}
if (!mousePressed) {
locked = false;
}
if (locked) {
newSliderPosition = constrain(mouseX, sliderPositionMinimum, sliderPositionMaximum);
}
if (abs(newSliderPosition - sliderPosition) > 1) {
sliderPosition = sliderPosition + (newSliderPosition-sliderPosition);
}
}
/**
* Constrain a value between an upper and lower limit.
*
* @param val
* Input value.
* @param minv
* Minimum allowed value.
* @param maxv
* Maximum allowed value.
*/
float constrain(float val, float minv, float maxv) {
return min(max(val, minv), maxv);
}
/**
* Check if mouse is over SCROLLBAR (not the slider).
*/
boolean overEvent() {
if (mouseX > xPositionBar && mouseX < xPositionBar+scrollBarWidth &&
mouseY > yPositionBar && mouseY < yPositionBar+scrollBarHeight) {
return true;
} else {
return false;
}
}
/**
* Display the scrollbar and slider.
*/
void display() {
noStroke();
fill(204);
rect(xPositionBar, yPositionBar, scrollBarWidth, scrollBarHeight);
if (over || locked) {
fill(#4477AA);
} else {
fill(102, 102, 102);
}
rect(sliderPosition, yPositionBar, scrollBarHeight, scrollBarHeight);
}
/**
* Get the normalized position of the slider within the scrollbar.
* A position 0 means the slider is on the left while 1 means the slider
* is on the right.
*/
float getPos() {
return (sliderPosition-xPositionBar) * sliderPositionNormalization;
}
}