use of processing.mode.java.tweak.TweakClient in project processing by processing.
the class JavaEditor method automateSketch.
/*
private void removeSpacesFromCode() {
SketchCode[] code = sketch.getCode();
for (int i=0; i<code.length; i++) {
String c = code[i].getProgram();
//c = c.substring(SPACE_AMOUNT, c.length() - SPACE_AMOUNT);
code[i].setProgram(c);
// TODO Wild Hack: set document to null so the text editor will refresh
// the program contents when the document tab is being clicked
code[i].setDocument(null);
}
// this will update the current code
setCode(sketch.getCurrentCode());
}
*/
/**
* Replace all numbers with variables and add code to initialize
* these variables and handle update messages.
*/
protected boolean automateSketch(Sketch sketch, SketchParser parser) {
SketchCode[] code = sketch.getCode();
List<List<Handle>> handles = parser.allHandles;
if (code.length < 1) {
return false;
}
if (handles.size() == 0) {
return false;
}
int afterSizePos = SketchParser.getAfterSizePos(baseCode[0]);
if (afterSizePos < 0) {
return false;
}
// get port number from preferences.txt
int port;
String portStr = Preferences.get(PREF_TWEAK_PORT);
if (portStr == null) {
Preferences.set(PREF_TWEAK_PORT, "auto");
portStr = "auto";
}
if (portStr.equals("auto")) {
// random port for udp (0xc000 - 0xffff)
port = (int) (Math.random() * 0x3fff) + 0xc000;
} else {
port = Preferences.getInteger(PREF_TWEAK_PORT);
}
// create the client that will send the new values to the sketch
tweakClient = new TweakClient(port);
// update handles with a reference to the client object
for (int tab = 0; tab < code.length; tab++) {
for (Handle h : handles.get(tab)) {
h.setTweakClient(tweakClient);
}
}
// loop through all tabs in the current sketch
for (int tab = 0; tab < code.length; tab++) {
int charInc = 0;
String c = baseCode[tab];
for (Handle n : handles.get(tab)) {
// replace number value with a variable
c = replaceString(c, n.startChar + charInc, n.endChar + charInc, n.name);
charInc += n.name.length() - n.strValue.length();
}
code[tab].setProgram(c);
}
// add the main header to the code in the first tab
String c = code[0].getProgram();
// header contains variable declaration, initialization,
// and OSC listener function
String header;
header = "\n\n" + "/*************************/\n" + "/* MODIFIED BY TWEAKMODE */\n" + "/*************************/\n" + "\n\n";
// add needed OSC imports and the global OSC object
header += "import java.net.*;\n";
header += "import java.io.*;\n";
header += "import java.nio.*;\n\n";
// write a declaration for int and float arrays
int numOfInts = howManyInts(handles);
int numOfFloats = howManyFloats(handles);
if (numOfInts > 0) {
header += "int[] tweakmode_int = new int[" + numOfInts + "];\n";
}
if (numOfFloats > 0) {
header += "float[] tweakmode_float = new float[" + numOfFloats + "];\n\n";
}
// add the server code that will receive the value change messages
// header += TweakClient.getServerCode(port, numOfInts>0, numOfFloats>0);
header += "TweakModeServer tweakmode_Server;\n";
header += "void tweakmode_initAllVars() {\n";
//for (int i=0; i<handles.length; i++) {
for (List<Handle> list : handles) {
//for (Handle n : handles[i]) {
for (Handle n : list) {
header += " " + n.name + " = " + n.strValue + ";\n";
}
}
header += "}\n\n";
header += "void tweakmode_initCommunication() {\n";
header += " tweakmode_Server = new TweakModeServer();\n";
header += " tweakmode_Server.setup();\n";
header += " tweakmode_Server.start();\n";
header += "}\n";
header += "\n\n\n\n\n";
// add call to our initAllVars and initOSC functions
// from the setup() function.
String addToSetup = "\n\n\n" + " /* TWEAKMODE */\n" + " tweakmode_initAllVars();\n" + " tweakmode_initCommunication();\n" + " /* TWEAKMODE */\n\n";
afterSizePos = SketchParser.getAfterSizePos(c);
c = replaceString(c, afterSizePos, afterSizePos, addToSetup);
// Server code defines a class, so it should go later in the sketch
String serverCode = TweakClient.getServerCode(port, numOfInts > 0, numOfFloats > 0);
code[0].setProgram(header + c + serverCode);
// print out modified code
String showModCode = Preferences.get(PREF_TWEAK_SHOW_CODE);
if (showModCode == null) {
Preferences.setBoolean(PREF_TWEAK_SHOW_CODE, false);
}
if (Preferences.getBoolean(PREF_TWEAK_SHOW_CODE)) {
System.out.println("\nTweakMode modified code:\n");
for (int i = 0; i < code.length; i++) {
System.out.println("tab " + i + "\n");
System.out.println("=======================================================\n");
System.out.println(code[i].getProgram());
}
}
return true;
}
Aggregations