-
Notifications
You must be signed in to change notification settings - Fork 0
/
HScrollbar.pde
110 lines (100 loc) · 2.94 KB
/
HScrollbar.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
class HScrollbar {
float barWidth; //Bar's width in pixels
float barHeight; //Bar's height in pixels
float xPosition; //Bar's x position in pixels
float yPosition; //Bar's y position in pixels
float sliderPosition, newSliderPosition; //Position of slider
float sliderPositionMin, sliderPositionMax; //Max and min values of slider
boolean mouseOver; //Is the mouse over the slider?
boolean locked; //Is the mouse clicking and dragging the slider now?
/**
* @brief Creates a new horizontal scrollbar
*
* @param x The x position of the top left corner of the bar in pixels
* @param y The y position of the top left corner of the bar in pixels
* @param w The width of the bar in pixels
* @param h The height of the bar in pixels
*/
HScrollbar (float x, float y, float w, float h) {
barWidth = w;
barHeight = h;
xPosition = x;
yPosition = y;
sliderPosition = xPosition + barWidth/2 - barHeight/2;
newSliderPosition = sliderPosition;
sliderPositionMin = xPosition;
sliderPositionMax = xPosition + barWidth - barHeight;
}
/**
* @brief Updates the state of the scrollbar according to the mouse movement
*/
void update() {
if (isMouseOver()) {
mouseOver = true;
}
else {
mouseOver = false;
}
if (mousePressed && mouseOver) {
locked = true;
}
if (!mousePressed) {
locked = false;
}
if (locked) {
newSliderPosition = constrain(mouseX - barHeight/2, sliderPositionMin, sliderPositionMax);
}
if (abs(newSliderPosition - sliderPosition) > 1) {
sliderPosition = sliderPosition + (newSliderPosition - sliderPosition);
}
}
/**
* @brief Clamps the value into the interval
*
* @param val The value to be clamped
* @param minVal Smallest value possible
* @param maxVal Largest value possible
*
* @return val clamped into the interval [minVal, maxVal]
*/
float constrain(float val, float minVal, float maxVal) {
return min(max(val, minVal), maxVal);
}
/**
* @brief Gets whether the mouse is hovering the scrollbar
*
* @return Whether the mouse is hovering the scrollbar
*/
boolean isMouseOver() {
if (mouseX > xPosition && mouseX < xPosition+barWidth &&
mouseY > yPosition && mouseY < yPosition+barHeight) {
return true;
}
else {
return false;
}
}
/**
* @brief Draws the scrollbar in its current state
*/
void display() {
noStroke();
fill(204);
rect(xPosition, yPosition, barWidth, barHeight);
if (mouseOver || locked) {
fill(0, 0, 0);
}
else {
fill(102, 102, 102);
}
rect(sliderPosition, yPosition, barHeight, barHeight);
}
/**
* @brief Gets the slider position
*
* @return The slider position in the interval [0,1] corresponding to [leftmost position, rightmost position]
*/
float getPos() {
return (sliderPosition - xPosition)/(barWidth - barHeight);
}
}