From 70cf524342af09bd1ff39aa4e7ec6ac6b0265014 Mon Sep 17 00:00:00 2001 From: Gal Sasson Date: Wed, 20 Aug 2014 11:25:51 -0400 Subject: [PATCH] add tweak.port parameter for tweak mode communication and some cleanups - tweak.port can be a number or 'auto', in this case a port number will be randomly generated at run time in the range of 0xc000 - 0xffff. --- pdex/src/galsasson/mode/tweak/Handle.java | 172 ++++++++---------- .../mode/experimental/DebugEditor.java | 62 +++---- 2 files changed, 102 insertions(+), 132 deletions(-) diff --git a/pdex/src/galsasson/mode/tweak/Handle.java b/pdex/src/galsasson/mode/tweak/Handle.java index cef24117b..a57e3de46 100644 --- a/pdex/src/galsasson/mode/tweak/Handle.java +++ b/pdex/src/galsasson/mode/tweak/Handle.java @@ -18,7 +18,7 @@ public class Handle { public int newEndChar; public int line; int tabIndex; - int decimalPlaces; // number of digits after the decimal point + int decimalPlaces; // number of digits after the decimal point float incValue; java.lang.Number value, newValue; @@ -36,8 +36,8 @@ public class Handle { // the client that sends the changes UDPTweakClient tweakClient; - public Handle(String t, String n, int vi, String v, int ti, int l, int sc, int ec, int dp) - { + public Handle(String t, String n, int vi, String v, int ti, int l, int sc, + int ec, int dp) { type = t; name = n; varIndex = vi; @@ -48,27 +48,26 @@ public class Handle { endChar = ec; decimalPlaces = dp; - incValue = (float)(1/Math.pow(10, decimalPlaces)); + incValue = (float) (1 / Math.pow(10, decimalPlaces)); if (type == "int") { value = newValue = Integer.parseInt(strValue); strNewValue = strValue; textFormat = "%d"; - } - else if (type == "hex") { - Long val = Long.parseLong(strValue.substring(2, strValue.length()), 16); + } else if (type == "hex") { + Long val = Long.parseLong(strValue.substring(2, strValue.length()), + 16); value = newValue = val.intValue(); strNewValue = strValue; textFormat = "0x%x"; - } - else if (type == "webcolor") { - Long val = Long.parseLong(strValue.substring(1, strValue.length()), 16); + } else if (type == "webcolor") { + Long val = Long.parseLong(strValue.substring(1, strValue.length()), + 16); val = val | 0xff000000; value = newValue = val.intValue(); strNewValue = strValue; textFormat = "#%06x"; - } - else if (type == "float") { + } else if (type == "float") { value = newValue = Float.parseFloat(strValue); strNewValue = strValue; textFormat = "%.0" + decimalPlaces + "f"; @@ -78,8 +77,7 @@ public class Handle { newEndChar = endChar; } - public void initInterface(int x, int y, int width, int height) - { + public void initInterface(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; @@ -89,13 +87,11 @@ public class Handle { progBar = new HProgressBar(height, width); } - public void setCenterX(int mx) - { + public void setCenterX(int mx) { xLast = xCurrent = xCenter = mx; } - public void setCurrentX(int mx) - { + public void setCurrentX(int mx) { xLast = xCurrent; xCurrent = mx; @@ -104,92 +100,80 @@ public class Handle { updateValue(); } - public void resetProgress() - { + public void resetProgress() { progBar.setPos(0); } - public void updateValue() - { + public void updateValue() { float change = getChange(); if (type == "int") { - if (newValue.intValue() + (int)change > Integer.MAX_VALUE || - newValue.intValue() + (int)change < Integer.MIN_VALUE) { + if (newValue.intValue() + (int) change > Integer.MAX_VALUE + || newValue.intValue() + (int) change < Integer.MIN_VALUE) { change = 0; return; } - setValue(newValue.intValue() + (int)change); - } - else if (type == "hex") { - setValue(newValue.intValue() + (int)change); - } - else if (type == "webcolor") { - setValue(newValue.intValue() + (int)change); - } - else if (type == "float") { + setValue(newValue.intValue() + (int) change); + } else if (type == "hex") { + setValue(newValue.intValue() + (int) change); + } else if (type == "webcolor") { + setValue(newValue.intValue() + (int) change); + } else if (type == "float") { setValue(newValue.floatValue() + change); } updateColorBox(); } - public void setValue(Number value) - { + public void setValue(Number value) { if (type == "int") { newValue = value.intValue(); - strNewValue = String.format(Locale.US,textFormat, newValue.intValue()); - } - else if (type == "hex") { + strNewValue = String.format(Locale.US, textFormat, + newValue.intValue()); + } else if (type == "hex") { newValue = value.intValue(); - strNewValue = String.format(Locale.US,textFormat, newValue.intValue()); - } - else if (type == "webcolor") { + strNewValue = String.format(Locale.US, textFormat, + newValue.intValue()); + } else if (type == "webcolor") { newValue = value.intValue(); // keep only RGB int val = (newValue.intValue() & 0xffffff); - strNewValue = String.format(Locale.US,textFormat, val); - } - else if (type == "float") { + strNewValue = String.format(Locale.US, textFormat, val); + } else if (type == "float") { BigDecimal bd = new BigDecimal(value.floatValue()); bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP); newValue = bd.floatValue(); - strNewValue = String.format(Locale.US,textFormat, newValue.floatValue()); + strNewValue = String.format(Locale.US, textFormat, + newValue.floatValue()); } // send new data to the server in the sketch - oscSendNewValue(); + sendNewValue(); } - public void updateColorBox() - { - if (colorBox != null) - { + public void updateColorBox() { + if (colorBox != null) { colorBox.colorChanged(); } } - private float getChange() - { + private float getChange() { int pixels = xCurrent - xLast; - return pixels*incValue; + return pixels * incValue; } - public void setPos(int nx, int ny) - { + public void setPos(int nx, int ny) { x = nx; y = ny; } - public void setWidth(int w) - { + public void setWidth(int w) { width = w; progBar.setWidth(w); } - public void draw(Graphics2D g2d, boolean hasFocus) - { + public void draw(Graphics2D g2d, boolean hasFocus) { AffineTransform prevTrans = g2d.getTransform(); g2d.translate(x, y); @@ -199,7 +183,7 @@ public class Handle { if (hasFocus) { if (progBar != null) { - g2d.translate(width/2, 2); + g2d.translate(width / 2, 2); progBar.draw(g2d); } } @@ -207,70 +191,59 @@ public class Handle { g2d.setTransform(prevTrans); } - public boolean pick(int mx, int my) - { + public boolean pick(int mx, int my) { return pickText(mx, my); } - public boolean pickText(int mx, int my) - { - if (mx>x-2 && mxy-height && my x - 2 && mx < x + width + 2 && my > y - height && my < y) { return true; } return false; } - public boolean valueChanged() - { + public boolean valueChanged() { if (type == "int") { return (value.intValue() != newValue.intValue()); - } - else if (type == "hex") { + } else if (type == "hex") { return (value.intValue() != newValue.intValue()); - } - else if (type == "webcolor") { + } else if (type == "webcolor") { return (value.intValue() != newValue.intValue()); - } - else { + } else { return (value.floatValue() != newValue.floatValue()); } } - public void setColorBox(ColorControlBox box) - { + public void setColorBox(ColorControlBox box) { colorBox = box; } - public void setTweakClient(UDPTweakClient client) - { + public void setTweakClient(UDPTweakClient client) { tweakClient = client; } - public void oscSendNewValue() - { + public void sendNewValue() { int index = varIndex; try { if (type == "int") { tweakClient.sendInt(index, newValue.intValue()); - } - else if (type == "hex") { + } else if (type == "hex") { tweakClient.sendInt(index, newValue.intValue()); - } - else if (type == "webcolor") { + } else if (type == "webcolor") { tweakClient.sendInt(index, newValue.intValue()); - } - else if (type == "float") { + } else if (type == "float") { tweakClient.sendFloat(index, newValue.floatValue()); } - } catch (Exception e) { System.out.println("error sending OSC message!"); } + } catch (Exception e) { + System.out.println("error sending new value!"); + } } - public String toString() - { - return type + " " + name + " = " + strValue + - " (tab: " + tabIndex + ", line: " + line + - ", start: " + startChar + ", end: " + endChar + ")"; + public String toString() { + return type + " " + name + " = " + strValue + " (tab: " + tabIndex + + ", line: " + line + ", start: " + startChar + ", end: " + + endChar + ")"; } } @@ -278,13 +251,12 @@ public class Handle { * Used for sorting the handles by order of occurrence inside each tab */ class HandleComparator implements Comparator { - public int compare(Handle handle1, Handle handle2) { - int tab = handle1.tabIndex - handle2.tabIndex; - if (tab == 0) { - return handle1.startChar - handle2.startChar; - } - else { - return tab; - } - } + public int compare(Handle handle1, Handle handle2) { + int tab = handle1.tabIndex - handle2.tabIndex; + if (tab == 0) { + return handle1.startChar - handle2.startChar; + } else { + return tab; + } + } } diff --git a/pdex/src/processing/mode/experimental/DebugEditor.java b/pdex/src/processing/mode/experimental/DebugEditor.java index 05467655e..824733b33 100755 --- a/pdex/src/processing/mode/experimental/DebugEditor.java +++ b/pdex/src/processing/mode/experimental/DebugEditor.java @@ -72,6 +72,7 @@ import processing.app.Base; import processing.app.EditorState; import processing.app.EditorToolbar; import processing.app.Mode; +import processing.app.Preferences; import processing.app.Sketch; import processing.app.SketchCode; import processing.app.Toolkit; @@ -1804,6 +1805,9 @@ public class DebugEditor extends JavaEditor implements ActionListener { */ //protected JCheckBoxMenuItem enableTweakCB; + public static final String prefTweakPort = "tweak.port"; + public static final String prefTweakShowCode = "tweak.showcode"; + String[] baseCode; final static int SPACE_AMOUNT = 0; @@ -2036,9 +2040,23 @@ public class DebugEditor extends JavaEditor implements ActionListener { return false; } + // get port number from preferences.txt + int port; + String portStr = Preferences.get(prefTweakPort); + if (portStr == null) { + Preferences.set(prefTweakPort, "auto"); + portStr = "auto"; + } + + if (portStr.equals("auto")) { + // random port for udp (0xc000 - 0xffff) + port = (int)(Math.random()*0x3fff) + 0xc000; + } + else { + port = Preferences.getInteger(prefTweakPort); + } + /* create the client that will send the new values to the sketch */ - // random port for udp (0xff0 - 0xfff0) - int port = (int)(Math.random()*0xf000) + 0xff0; tweakClient = new UDPTweakClient(port); // update handles with a reference to the client object for (int tab=0; tab 0) { -// header += " if (type.contains(\"/tm_change_int\")) {\n" + -// " int index = msg.get(0).intValue();\n" + -// " int value = msg.get(1).intValue();\n" + -// " tweakmode_int[index] = value;\n" + -// " }\n"; -// if (numOfFloats > 0) { -// header += " else "; -// } -// } -// if (numOfFloats > 0) { -// header += "if (type.contains(\"/tm_change_float\")) {\n" + -// " int index = msg.get(0).intValue();\n" + -// " float value = msg.get(1).floatValue();\n" + -// " tweakmode_float[index] = value;\n" + -// " }\n"; -// } -// header += " }\n" + -// "}\n"; -// header += "TweakMode_OscHandler tweakmode_oscHandler = new TweakMode_OscHandler();\n"; - header += "void tweakmode_initAllVars() {\n"; for (int i=0; i