diff --git a/java/src/processing/mode/java/JavaEditor.java b/java/src/processing/mode/java/JavaEditor.java index ac1fc2b75..6c596940f 100644 --- a/java/src/processing/mode/java/JavaEditor.java +++ b/java/src/processing/mode/java/JavaEditor.java @@ -2677,8 +2677,8 @@ public class JavaEditor extends Editor { // TWEAK MODE - static final String prefTweakPort = "tweak.port"; - static final String prefTweakShowCode = "tweak.showcode"; + static final String PREF_TWEAK_PORT = "tweak.port"; + static final String PREF_TWEAK_SHOW_CODE = "tweak.showcode"; public String[] baseCode; TweakClient tweakClient; @@ -2881,9 +2881,9 @@ public class JavaEditor extends Editor { // get port number from preferences.txt int port; - String portStr = Preferences.get(prefTweakPort); + String portStr = Preferences.get(PREF_TWEAK_PORT); if (portStr == null) { - Preferences.set(prefTweakPort, "auto"); + Preferences.set(PREF_TWEAK_PORT, "auto"); portStr = "auto"; } @@ -2891,7 +2891,7 @@ public class JavaEditor extends Editor { // random port for udp (0xc000 - 0xffff) port = (int)(Math.random()*0x3fff) + 0xc000; } else { - port = Preferences.getInteger(prefTweakPort); + port = Preferences.getInteger(PREF_TWEAK_PORT); } // create the client that will send the new values to the sketch @@ -2946,7 +2946,7 @@ public class JavaEditor extends Editor { } // add the server code that will receive the value change messages - header += TweakClient.getServerCode(port, numOfInts>0, numOfFloats>0); +// header += TweakClient.getServerCode(port, numOfInts>0, numOfFloats>0); header += "TweakModeServer tweakmode_Server;\n"; header += "void tweakmode_initAllVars() {\n"; @@ -2975,15 +2975,18 @@ public class JavaEditor extends Editor { setupEndPos = SketchParser.getSetupEnd(c); c = replaceString(c, setupEndPos, setupEndPos, addToSetup); - code[0].setProgram(header + c); + // 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(prefTweakShowCode); + String showModCode = Preferences.get(PREF_TWEAK_SHOW_CODE); if (showModCode == null) { - Preferences.setBoolean(prefTweakShowCode, false); + Preferences.setBoolean(PREF_TWEAK_SHOW_CODE, false); } - if (Preferences.getBoolean(prefTweakShowCode)) { + if (Preferences.getBoolean(PREF_TWEAK_SHOW_CODE)) { System.out.println("\nTweakMode modified code:\n"); for (int i=0; i colorModes; + List colorModes; List> scientificNotations; - + Range setupFunction; - + List> commentBlocks; List curlyScopes; - + + public SketchParser(String[] codeTabs, boolean requiresComment) { this.codeTabs = codeTabs; this.requiresComment = requiresComment; - intVarCount=0; - floatVarCount=0; - + intVarCount = 0; + floatVarCount = 0; + // get all comment blocks commentBlocks = new ArrayList<>(); for (String code : codeTabs) { commentBlocks.add(getCommentBlocks(code)); } - + // get setup function range (to ignore all numbers there) setupFunction = new Range(getSetupStart(codeTabs[0]), getSetupEnd(codeTabs[0])); - + // build curly scope for every character in the code curlyScopes = new ArrayList<>(); for (String code : codeTabs) { - curlyScopes.add(getCurlyScopes(code)); + curlyScopes.add(getCurlyScopes(code)); } - + // get all scientific notation (to ignore them) scientificNotations = getAllScientificNotations(); @@ -84,7 +85,7 @@ public class SketchParser { } - public void addAllNumbers() { + private void addAllNumbers() { allHandles = new ArrayList<>(); addAllDecimalNumbers(); @@ -123,7 +124,7 @@ public class SketchParser { // ignore comments continue; } - + if (setupFunction.contains(start)) { // ignore numbers in setup continue; @@ -215,7 +216,7 @@ public class SketchParser { // ignore comments continue; } - + if (setupFunction.contains(start)) { // ignore number in setup continue; @@ -276,7 +277,7 @@ public class SketchParser { // ignore comments continue; } - + if (setupFunction.contains(start)) { // ignore number in setup continue; @@ -380,7 +381,7 @@ public class SketchParser { // ignore colors in a comment continue; } - + if (setupFunction.contains(m.start())) { // ignore number in setup continue; @@ -464,7 +465,7 @@ public class SketchParser { // ignore colors in a comment continue; } - + if (setupFunction.contains(m.start())) { // ignore number in setup continue; @@ -683,28 +684,28 @@ public class SketchParser { return false; } - + /** * Builds an int array for every tab that represents the scope depth at each character - * + * * @return */ static private int[] getCurlyScopes(String code) { List comments = getCommentBlocks(code); - + int[] scopes = new int[code.length()]; int curlyScope = 0; boolean arrayAssignmentMaybeCommingFlag = false; int arrayAssignmentCurlyScope = 0; for (int pos=0; pos0) { @@ -731,10 +732,10 @@ public class SketchParser { arrayAssignmentMaybeCommingFlag = false; } } - + return scopes; } - + static private boolean isWhiteSpace(char c) { return c == ' ' || c == '\t' || c == '\n' || c == '\r'; } @@ -745,15 +746,15 @@ public class SketchParser { * @param codeTabIndex index of the code in codeTabs * @return * true if the position 'pos' is in global scope in the code 'codeTabs[codeTabIndex]' - * + * */ private boolean isGlobal(int pos, int codeTabIndex) { return (curlyScopes.get(codeTabIndex)[pos]==0); }; - public static List getCommentBlocks(String code) { + static private List getCommentBlocks(String code) { List commentBlocks = new ArrayList(); - + int lastBlockStart=0; boolean lookForEnd = false; for (int pos=0; pos rangeList) { for (Range r : rangeList) { if (r.contains(pos)) { return true; } } - + return false; } @@ -826,7 +827,7 @@ public class SketchParser { } - public static int getSetupStart(String code) { + static public int getSetupStart(String code) { Pattern p = Pattern.compile("void[\\s\\t\\r\\n]*setup[\\s\\t]*\\(\\)[\\s\\t\\r\\n]*\\{"); Matcher m = p.matcher(code); @@ -836,42 +837,43 @@ public class SketchParser { return -1; } - - public static int getSetupEnd(String code) { + + + static public int getSetupEnd(String code) { List comments = getCommentBlocks(code); - + int setupStart = getSetupStart(code); if (setupStart == -1) { return -1; } - + System.out.println("setup start = " + setupStart); - + // count brackets to look for setup end int bracketCount=1; int pos = setupStart; while (bracketCount>0 && pos= start && v < end; } }