1
1
/**
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.
5
5
*/
6
-
6
+
7
7
Handle [] handles;
8
8
9
+ // True if a mouse button has just been pressed while no other button was.
10
+ boolean firstMousePress = false ;
11
+
9
12
void setup () {
10
13
size (640 , 360 );
11
14
int num = height / 15 ;
@@ -18,24 +21,36 @@ void setup() {
18
21
19
22
void draw () {
20
23
background (153 );
21
-
24
+
22
25
for (int i = 0 ; i < handles. length; i++ ) {
23
26
handles[i]. update();
24
27
handles[i]. display();
25
28
}
26
-
29
+
27
30
fill (0 );
28
31
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
+ }
29
37
}
30
38
31
- void mouseReleased () {
39
+
40
+ void mousePressed () {
41
+ if (! firstMousePress) {
42
+ firstMousePress = true ;
43
+ }
44
+ }
45
+
46
+ void mouseReleased () {
32
47
for (int i = 0 ; i < handles. length; i++ ) {
33
48
handles[i]. releaseEvent();
34
49
}
35
50
}
36
51
37
52
class Handle {
38
-
53
+
39
54
int x, y;
40
55
int boxx, boxy;
41
56
int stretch;
@@ -45,7 +60,7 @@ class Handle {
45
60
boolean locked = false ;
46
61
boolean otherslocked = false ;
47
62
Handle [] others;
48
-
63
+
49
64
Handle (int ix , int iy , int il , int is , Handle [] o ) {
50
65
x = ix;
51
66
y = iy;
@@ -55,51 +70,51 @@ class Handle {
55
70
boxy = y - size/ 2 ;
56
71
others = o;
57
72
}
58
-
73
+
59
74
void update () {
60
75
boxx = x+ stretch;
61
76
boxy = y - size/ 2 ;
62
-
77
+
63
78
for (int i= 0 ; i< others. length; i++ ) {
64
79
if (others[i]. locked == true ) {
65
80
otherslocked = true ;
66
81
break ;
67
82
} else {
68
83
otherslocked = false ;
69
- }
84
+ }
70
85
}
71
-
86
+
72
87
if (otherslocked == false ) {
73
88
overEvent();
74
89
pressEvent();
75
90
}
76
-
91
+
77
92
if (press) {
78
93
stretch = lock(mouseX - width / 2 - size/ 2 , 0 , width / 2 - size- 1 );
79
94
}
80
95
}
81
-
96
+
82
97
void overEvent () {
83
98
if (overRect(boxx, boxy, size, size)) {
84
99
over = true ;
85
100
} else {
86
101
over = false ;
87
102
}
88
103
}
89
-
104
+
90
105
void pressEvent () {
91
- if (over && mousePressed || locked) {
106
+ if (over && firstMousePress || locked) {
92
107
press = true ;
93
108
locked = true ;
94
109
} else {
95
110
press = false ;
96
111
}
97
112
}
98
-
113
+
99
114
void releaseEvent () {
100
115
locked = false ;
101
116
}
102
-
117
+
103
118
void display () {
104
119
line (x, y, x+ stretch, y);
105
120
fill (255 );
@@ -109,19 +124,18 @@ class Handle {
109
124
line (boxx, boxy, boxx+ size, boxy+ size);
110
125
line (boxx, boxy+ size, boxx+ size, boxy);
111
126
}
112
-
113
127
}
114
128
}
115
129
116
130
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 ) {
119
133
return true ;
120
134
} else {
121
135
return false ;
122
136
}
123
137
}
124
138
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
+ }
0 commit comments