@@ -18,7 +18,7 @@ public class Handle {
1818 public int newEndChar ;
1919 public int line ;
2020 int tabIndex ;
21- int decimalPlaces ; // number of digits after the decimal point
21+ int decimalPlaces ; // number of digits after the decimal point
2222 float incValue ;
2323
2424 java .lang .Number value , newValue ;
@@ -33,10 +33,11 @@ public class Handle {
3333 HProgressBar progBar = null ;
3434 String textFormat ;
3535
36- int oscPort ;
36+ // the client that sends the changes
37+ UDPTweakClient tweakClient ;
3738
38- public Handle (String t , String n , int vi , String v , int ti , int l , int sc , int ec , int dp )
39- {
39+ public Handle (String t , String n , int vi , String v , int ti , int l , int sc ,
40+ int ec , int dp ) {
4041 type = t ;
4142 name = n ;
4243 varIndex = vi ;
@@ -47,27 +48,26 @@ public Handle(String t, String n, int vi, String v, int ti, int l, int sc, int e
4748 endChar = ec ;
4849 decimalPlaces = dp ;
4950
50- incValue = (float )( 1 / Math .pow (10 , decimalPlaces ));
51+ incValue = (float ) ( 1 / Math .pow (10 , decimalPlaces ));
5152
5253 if (type == "int" ) {
5354 value = newValue = Integer .parseInt (strValue );
5455 strNewValue = strValue ;
5556 textFormat = "%d" ;
56- }
57- else if ( type == "hex" ) {
58- Long val = Long . parseLong ( strValue . substring ( 2 , strValue . length ()), 16 );
57+ } else if ( type == "hex" ) {
58+ Long val = Long . parseLong ( strValue . substring ( 2 , strValue . length ()),
59+ 16 );
5960 value = newValue = val .intValue ();
6061 strNewValue = strValue ;
6162 textFormat = "0x%x" ;
62- }
63- else if ( type == "webcolor" ) {
64- Long val = Long . parseLong ( strValue . substring ( 1 , strValue . length ()), 16 );
63+ } else if ( type == "webcolor" ) {
64+ Long val = Long . parseLong ( strValue . substring ( 1 , strValue . length ()),
65+ 16 );
6566 val = val | 0xff000000 ;
6667 value = newValue = val .intValue ();
6768 strNewValue = strValue ;
6869 textFormat = "#%06x" ;
69- }
70- else if (type == "float" ) {
70+ } else if (type == "float" ) {
7171 value = newValue = Float .parseFloat (strValue );
7272 strNewValue = strValue ;
7373 textFormat = "%.0" + decimalPlaces + "f" ;
@@ -77,8 +77,7 @@ else if (type == "float") {
7777 newEndChar = endChar ;
7878 }
7979
80- public void initInterface (int x , int y , int width , int height )
81- {
80+ public void initInterface (int x , int y , int width , int height ) {
8281 this .x = x ;
8382 this .y = y ;
8483 this .width = width ;
@@ -88,13 +87,11 @@ public void initInterface(int x, int y, int width, int height)
8887 progBar = new HProgressBar (height , width );
8988 }
9089
91- public void setCenterX (int mx )
92- {
90+ public void setCenterX (int mx ) {
9391 xLast = xCurrent = xCenter = mx ;
9492 }
9593
96- public void setCurrentX (int mx )
97- {
94+ public void setCurrentX (int mx ) {
9895 xLast = xCurrent ;
9996 xCurrent = mx ;
10097
@@ -103,92 +100,80 @@ public void setCurrentX(int mx)
103100 updateValue ();
104101 }
105102
106- public void resetProgress ()
107- {
103+ public void resetProgress () {
108104 progBar .setPos (0 );
109105 }
110106
111- public void updateValue ()
112- {
107+ public void updateValue () {
113108 float change = getChange ();
114109
115110 if (type == "int" ) {
116- if (newValue .intValue () + (int )change > Integer .MAX_VALUE ||
117- newValue .intValue () + (int )change < Integer .MIN_VALUE ) {
111+ if (newValue .intValue () + (int ) change > Integer .MAX_VALUE
112+ || newValue .intValue () + (int ) change < Integer .MIN_VALUE ) {
118113 change = 0 ;
119114 return ;
120115 }
121- setValue (newValue .intValue () + (int )change );
122- }
123- else if (type == "hex" ) {
124- setValue (newValue .intValue () + (int )change );
125- }
126- else if (type == "webcolor" ) {
127- setValue (newValue .intValue () + (int )change );
128- }
129- else if (type == "float" ) {
116+ setValue (newValue .intValue () + (int ) change );
117+ } else if (type == "hex" ) {
118+ setValue (newValue .intValue () + (int ) change );
119+ } else if (type == "webcolor" ) {
120+ setValue (newValue .intValue () + (int ) change );
121+ } else if (type == "float" ) {
130122 setValue (newValue .floatValue () + change );
131123 }
132124
133125 updateColorBox ();
134126 }
135127
136- public void setValue (Number value )
137- {
128+ public void setValue (Number value ) {
138129 if (type == "int" ) {
139130 newValue = value .intValue ();
140- strNewValue = String .format (Locale .US ,textFormat , newValue . intValue ());
141- }
142- else if (type == "hex" ) {
131+ strNewValue = String .format (Locale .US , textFormat ,
132+ newValue . intValue ());
133+ } else if (type == "hex" ) {
143134 newValue = value .intValue ();
144- strNewValue = String .format (Locale .US ,textFormat , newValue . intValue ());
145- }
146- else if (type == "webcolor" ) {
135+ strNewValue = String .format (Locale .US , textFormat ,
136+ newValue . intValue ());
137+ } else if (type == "webcolor" ) {
147138 newValue = value .intValue ();
148139 // keep only RGB
149140 int val = (newValue .intValue () & 0xffffff );
150- strNewValue = String .format (Locale .US ,textFormat , val );
151- }
152- else if (type == "float" ) {
141+ strNewValue = String .format (Locale .US , textFormat , val );
142+ } else if (type == "float" ) {
153143 BigDecimal bd = new BigDecimal (value .floatValue ());
154144 bd = bd .setScale (decimalPlaces , BigDecimal .ROUND_HALF_UP );
155145 newValue = bd .floatValue ();
156- strNewValue = String .format (Locale .US ,textFormat , newValue .floatValue ());
146+ strNewValue = String .format (Locale .US , textFormat ,
147+ newValue .floatValue ());
157148 }
158149
159150 // send new data to the server in the sketch
160- oscSendNewValue ();
151+ sendNewValue ();
161152 }
162153
163- public void updateColorBox ()
164- {
165- if (colorBox != null )
166- {
154+ public void updateColorBox () {
155+ if (colorBox != null ) {
167156 colorBox .colorChanged ();
168157 }
169158 }
170159
171- private float getChange ()
172- {
160+ private float getChange () {
173161 int pixels = xCurrent - xLast ;
174- return pixels * incValue ;
162+ return pixels * incValue ;
175163 }
176164
177- public void setPos (int nx , int ny )
178- {
165+ public void setPos (int nx , int ny ) {
179166 x = nx ;
180167 y = ny ;
181168 }
182169
183- public void setWidth (int w )
184- {
170+ public void setWidth (int w ) {
185171 width = w ;
186172
187173 progBar .setWidth (w );
188174 }
189175
190- public void draw (Graphics2D g2d , boolean hasFocus )
191- {
176+ public void draw (Graphics2D g2d , boolean hasFocus ) {
192177 AffineTransform prevTrans = g2d .getTransform ();
193178 g2d .translate (x , y );
194179
@@ -198,92 +183,80 @@ public void draw(Graphics2D g2d, boolean hasFocus)
198183
199184 if (hasFocus ) {
200185 if (progBar != null ) {
201- g2d .translate (width / 2 , 2 );
186+ g2d .translate (width / 2 , 2 );
202187 progBar .draw (g2d );
203188 }
204189 }
205190
206191 g2d .setTransform (prevTrans );
207192 }
208193
209- public boolean pick (int mx , int my )
210- {
194+ public boolean pick (int mx , int my ) {
211195 return pickText (mx , my );
212196 }
213197
214- public boolean pickText (int mx , int my )
215- {
216- if (mx >x -2 && mx <x +width +2 && my >y -height && my <y ) {
198+ public boolean pickText (int mx , int my ) {
199+ if (mx > x - 2 && mx < x + width + 2 && my > y - height && my < y ) {
217200 return true ;
218201 }
219202
220203 return false ;
221204 }
222205
223- public boolean valueChanged ()
224- {
206+ public boolean valueChanged () {
225207 if (type == "int" ) {
226208 return (value .intValue () != newValue .intValue ());
227- }
228- else if (type == "hex" ) {
209+ } else if (type == "hex" ) {
229210 return (value .intValue () != newValue .intValue ());
230- }
231- else if (type == "webcolor" ) {
211+ } else if (type == "webcolor" ) {
232212 return (value .intValue () != newValue .intValue ());
233- }
234- else {
213+ } else {
235214 return (value .floatValue () != newValue .floatValue ());
236215 }
237216 }
238217
239- public void setColorBox (ColorControlBox box )
240- {
218+ public void setColorBox (ColorControlBox box ) {
241219 colorBox = box ;
242220 }
243221
244- public void setOscPort (int port )
245- {
246- oscPort = port ;
222+ public void setTweakClient (UDPTweakClient client ) {
223+ tweakClient = client ;
247224 }
248225
249- public void oscSendNewValue ()
250- {
226+ public void sendNewValue () {
251227 int index = varIndex ;
252228 try {
253229 if (type == "int" ) {
254- OSCSender .sendInt (index , newValue .intValue (), oscPort );
255- }
256- else if ( type == "hex" ) {
257- OSCSender . sendInt ( index , newValue . intValue (), oscPort );
258- }
259- else if (type == "webcolor " ) {
260- OSCSender . sendInt (index , newValue .intValue (), oscPort );
230+ tweakClient .sendInt (index , newValue .intValue ());
231+ } else if ( type == "hex" ) {
232+ tweakClient . sendInt ( index , newValue . intValue ());
233+ } else if ( type == "webcolor" ) {
234+ tweakClient . sendInt ( index , newValue . intValue ());
235+ } else if (type == "float " ) {
236+ tweakClient . sendFloat (index , newValue .floatValue () );
261237 }
262- else if (type == "float" ) {
263- OSCSender .sendFloat (index , newValue .floatValue (), oscPort );
264- }
265- } catch (Exception e ) { System .out .println ("error sending OSC message!" ); }
238+ } catch (Exception e ) {
239+ System .out .println ("error sending new value!" );
240+ }
266241 }
267242
268- public String toString ()
269- {
270- return type + " " + name + " = " + strValue +
271- " (tab: " + tabIndex + ", line: " + line +
272- ", start: " + startChar + ", end: " + endChar + ")" ;
243+ public String toString () {
244+ return type + " " + name + " = " + strValue + " (tab: " + tabIndex
245+ + ", line: " + line + ", start: " + startChar + ", end: "
246+ + endChar + ")" ;
273247 }
274248}
275249
276250/*
277251 * Used for sorting the handles by order of occurrence inside each tab
278252 */
279253class HandleComparator implements Comparator <Handle > {
280- public int compare (Handle handle1 , Handle handle2 ) {
281- int tab = handle1 .tabIndex - handle2 .tabIndex ;
282- if (tab == 0 ) {
283- return handle1 .startChar - handle2 .startChar ;
284- }
285- else {
286- return tab ;
287- }
288- }
254+ public int compare (Handle handle1 , Handle handle2 ) {
255+ int tab = handle1 .tabIndex - handle2 .tabIndex ;
256+ if (tab == 0 ) {
257+ return handle1 .startChar - handle2 .startChar ;
258+ } else {
259+ return tab ;
260+ }
261+ }
289262}
0 commit comments