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+
77Handle [] handles;
88
9+ // True if a mouse button has just been pressed while no other button was.
10+ boolean firstMousePress = false ;
11+
912void setup () {
1013 size (640 , 360 );
1114 int num = height / 15 ;
@@ -18,24 +21,36 @@ void setup() {
1821
1922void 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
3752class 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
116130boolean 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+ }
0 commit comments