Skip to content

Commit 9566318

Browse files
authored
Update Scrollbar.pde
Substitute the variable mousePressed for another that will lock scrollbars only after clicking on them.
1 parent 33db64d commit 9566318

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

Topics/GUI/Scrollbar/Scrollbar.pde

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,60 @@
11
/**
2-
* Scrollbar.
3-
*
4-
* Move the scrollbars left and right to change the positions of the images.
2+
* Scrollbar.
3+
*
4+
* Move the scrollbars left and right to change the positions of the images.
55
*/
66

7+
//True if a mouse button was pressed while no other button was.
8+
boolean firstMousePress = false;
79
HScrollbar hs1, hs2; // Two scrollbars
810
PImage img1, img2; // Two images to load
911

1012
void setup() {
1113
size(640, 360);
1214
noStroke();
13-
15+
1416
hs1 = new HScrollbar(0, height/2-8, width, 16, 16);
1517
hs2 = new HScrollbar(0, height/2+8, width, 16, 16);
16-
18+
1719
// Load images
1820
img1 = loadImage("seedTop.jpg");
1921
img2 = loadImage("seedBottom.jpg");
2022
}
2123

2224
void draw() {
2325
background(255);
24-
26+
2527
// Get the position of the img1 scrollbar
26-
// and convert to a value to display the img1 image
28+
// and convert to a value to display the img1 image
2729
float img1Pos = hs1.getPos()-width/2;
2830
fill(255);
2931
image(img1, width/2-img1.width/2 + img1Pos*1.5, 0);
30-
32+
3133
// Get the position of the img2 scrollbar
3234
// and convert to a value to display the img2 image
3335
float img2Pos = hs2.getPos()-width/2;
3436
fill(255);
3537
image(img2, width/2-img2.width/2 + img2Pos*1.5, height/2);
36-
38+
3739
hs1.update();
3840
hs2.update();
3941
hs1.display();
4042
hs2.display();
41-
43+
4244
stroke(0);
4345
line(0, height/2, width, height/2);
46+
47+
//After it has been used in the sketch, set it back to false
48+
if (firstMousePress) {
49+
firstMousePress = false;
50+
}
4451
}
4552

53+
void mousePressed() {
54+
if (!firstMousePress) {
55+
firstMousePress = true;
56+
}
57+
}
4658

4759
class HScrollbar {
4860
int swidth, sheight; // width and height of bar
@@ -74,7 +86,7 @@ class HScrollbar {
7486
} else {
7587
over = false;
7688
}
77-
if (mousePressed && over) {
89+
if (firstMousePress && over) {
7890
locked = true;
7991
}
8092
if (!mousePressed) {
@@ -94,7 +106,7 @@ class HScrollbar {
94106

95107
boolean overEvent() {
96108
if (mouseX > xpos && mouseX < xpos+swidth &&
97-
mouseY > ypos && mouseY < ypos+sheight) {
109+
mouseY > ypos && mouseY < ypos+sheight) {
98110
return true;
99111
} else {
100112
return false;

0 commit comments

Comments
 (0)