Skip to content

Commit 125bf3c

Browse files
authored
Merge pull request processing#9 from trikaphundo/gui-examples-corrections
Gui examples corrections
2 parents 6458c2a + 9566318 commit 125bf3c

File tree

2 files changed

+63
-37
lines changed

2 files changed

+63
-37
lines changed

Topics/GUI/Handles/Handles.pde

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
/**
2-
* Handles.
3-
*
4-
* Click and drag the white boxes to change their position.
2+
* Handles.
3+
*
4+
* Click and drag the white boxes to change their position.
55
*/
6-
6+
77
Handle[] handles;
88

9+
//True if a mouse button has just been pressed while no other button was.
10+
boolean firstMousePress = false;
11+
912
void setup() {
1013
size(640, 360);
1114
int num = height/15;
@@ -18,24 +21,36 @@ void setup() {
1821

1922
void draw() {
2023
background(153);
21-
24+
2225
for (int i = 0; i < handles.length; i++) {
2326
handles[i].update();
2427
handles[i].display();
2528
}
26-
29+
2730
fill(0);
2831
rect(0, 0, width/2, height);
32+
33+
//After it has been used in the sketch, set it back to false
34+
if (firstMousePress) {
35+
firstMousePress = false;
36+
}
2937
}
3038

31-
void mouseReleased() {
39+
40+
void mousePressed() {
41+
if (!firstMousePress) {
42+
firstMousePress = true;
43+
}
44+
}
45+
46+
void mouseReleased() {
3247
for (int i = 0; i < handles.length; i++) {
3348
handles[i].releaseEvent();
3449
}
3550
}
3651

3752
class Handle {
38-
53+
3954
int x, y;
4055
int boxx, boxy;
4156
int stretch;
@@ -45,7 +60,7 @@ class Handle {
4560
boolean locked = false;
4661
boolean otherslocked = false;
4762
Handle[] others;
48-
63+
4964
Handle(int ix, int iy, int il, int is, Handle[] o) {
5065
x = ix;
5166
y = iy;
@@ -55,51 +70,51 @@ class Handle {
5570
boxy = y - size/2;
5671
others = o;
5772
}
58-
73+
5974
void update() {
6075
boxx = x+stretch;
6176
boxy = y - size/2;
62-
77+
6378
for (int i=0; i<others.length; i++) {
6479
if (others[i].locked == true) {
6580
otherslocked = true;
6681
break;
6782
} else {
6883
otherslocked = false;
69-
}
84+
}
7085
}
71-
86+
7287
if (otherslocked == false) {
7388
overEvent();
7489
pressEvent();
7590
}
76-
91+
7792
if (press) {
7893
stretch = lock(mouseX-width/2-size/2, 0, width/2-size-1);
7994
}
8095
}
81-
96+
8297
void overEvent() {
8398
if (overRect(boxx, boxy, size, size)) {
8499
over = true;
85100
} else {
86101
over = false;
87102
}
88103
}
89-
104+
90105
void pressEvent() {
91-
if (over && mousePressed || locked) {
106+
if (over && firstMousePress || locked) {
92107
press = true;
93108
locked = true;
94109
} else {
95110
press = false;
96111
}
97112
}
98-
113+
99114
void releaseEvent() {
100115
locked = false;
101116
}
102-
117+
103118
void display() {
104119
line(x, y, x+stretch, y);
105120
fill(255);
@@ -109,19 +124,18 @@ class Handle {
109124
line(boxx, boxy, boxx+size, boxy+size);
110125
line(boxx, boxy+size, boxx+size, boxy);
111126
}
112-
113127
}
114128
}
115129

116130
boolean overRect(int x, int y, int width, int height) {
117-
if (mouseX >= x && mouseX <= x+width &&
118-
mouseY >= y && mouseY <= y+height) {
131+
if (mouseX >= x && mouseX <= x+width &&
132+
mouseY >= y && mouseY <= y+height) {
119133
return true;
120134
} else {
121135
return false;
122136
}
123137
}
124138

125-
int lock(int val, int minv, int maxv) {
126-
return min(max(val, minv), maxv);
127-
}
139+
int lock(int val, int minv, int maxv) {
140+
return min(max(val, minv), maxv);
141+
}

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)