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.
This commit is contained in:
Gal Sasson
2014-08-20 11:25:51 -04:00
parent 421a60055c
commit 70cf524342
2 changed files with 102 additions and 132 deletions

View File

@@ -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 && mx<x+width+2 && my>y-height && my<y) {
public boolean pickText(int mx, int my) {
if (mx > 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<Handle> {
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;
}
}
}

View File

@@ -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<code.length; tab++) {
@@ -2096,31 +2114,6 @@ public class DebugEditor extends JavaEditor implements ActionListener {
header += "TweakModeServer tweakmode_Server;\n";
/* add the class for the OSC event handler that will respond to our messages */
// header += "public class TweakMode_OscHandler {\n" +
// " public void oscEvent(OscMessage msg) {\n" +
// " String type = msg.addrPattern();\n";
// if (numOfInts > 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<handles.length; i++) {
for (Handle n : handles[i])
@@ -2133,7 +2126,6 @@ public class DebugEditor extends JavaEditor implements ActionListener {
header += " tweakmode_Server = new TweakModeServer();\n";
header += " tweakmode_Server.setup();\n";
header += " tweakmode_Server.start();\n";
// header += " tweakmode_oscP5 = new OscP5(tweakmode_oscHandler,"+oscPort+");\n";
header += "}\n";
header += "\n\n\n\n\n";
@@ -2149,14 +2141,20 @@ public class DebugEditor extends JavaEditor implements ActionListener {
code[0].setProgram(header + c);
/* print out modified code */
// if (tweakMode.dumpModifiedCode) {
System.out.println("\nModified code:\n");
String showModCode = Preferences.get(prefTweakShowCode);
if (showModCode == null) {
Preferences.setBoolean(prefTweakShowCode, false);
}
if (Preferences.getBoolean(prefTweakShowCode)) {
System.out.println("\nTweakMode modified code:\n");
for (int i=0; i<code.length; i++)
{
System.out.println("file " + i + "\n=========");
System.out.println("tab " + i + "\n");
System.out.println("=======================================================\n");
System.out.println(code[i].getProgram());
}
// }
}
return true;
}