diff --git a/java/src/processing/mode/java/JavaEditor.java b/java/src/processing/mode/java/JavaEditor.java index e7d01835f..3da31ad88 100644 --- a/java/src/processing/mode/java/JavaEditor.java +++ b/java/src/processing/mode/java/JavaEditor.java @@ -2739,7 +2739,6 @@ public class JavaEditor extends Editor { * Replace all numbers with variables and add code to initialize * these variables and handle update messages. */ - //public boolean automateSketch(Sketch sketch, ArrayList handles[]) protected boolean automateSketch(Sketch sketch, List> handles) { SketchCode[] code = sketch.getCode(); @@ -2751,8 +2750,8 @@ public class JavaEditor extends Editor { return false; } - int setupStartPos = SketchParser.getSetupStart(baseCode[0]); - if (setupStartPos < 0) { + int setupEndPos = SketchParser.getSetupEnd(baseCode[0]); + if (setupEndPos < 0) { return false; } @@ -2849,8 +2848,8 @@ public class JavaEditor extends Editor { " tweakmode_initAllVars();\n"+ " tweakmode_initCommunication();\n\n"; - setupStartPos = SketchParser.getSetupStart(c); - c = replaceString(c, setupStartPos, setupStartPos, addToSetup); + setupEndPos = SketchParser.getSetupEnd(c); + c = replaceString(c, setupEndPos, setupEndPos, addToSetup); code[0].setProgram(header + c); diff --git a/java/src/processing/mode/java/tweak/SketchParser.java b/java/src/processing/mode/java/tweak/SketchParser.java index 4f6eca38f..876d4840d 100644 --- a/java/src/processing/mode/java/tweak/SketchParser.java +++ b/java/src/processing/mode/java/tweak/SketchParser.java @@ -38,17 +38,22 @@ public class SketchParser { ArrayList colorModes; List> scientificNotations; - - + + Range setupFunction; + public SketchParser(String[] codeTabs, boolean requiresComment) { this.codeTabs = codeTabs; this.requiresComment = requiresComment; intVarCount=0; floatVarCount=0; - + + // get setup function range (to ignore all numbers there) + setupFunction = new Range(getSetupStart(codeTabs[0]), getSetupEnd(codeTabs[0])); + + // get all scientific notation (to ignore them) scientificNotations = getAllScientificNotations(); - // find, add, and sort all tweakable numbers in the sketch + // find, add, and sort all tweak-able numbers in the sketch addAllNumbers(); // handle colors @@ -65,11 +70,11 @@ public class SketchParser { public void addAllNumbers() { - //allHandles = new ArrayList[codeTabs.length]; // moved inside addAllDecimalNumbers + allHandles = new ArrayList<>(); + addAllDecimalNumbers(); addAllHexNumbers(); addAllWebColorNumbers(); - //for (int i=0; i handle : allHandles) { //Collections.sort(allHandles[i], new HandleComparator()); Collections.sort(handle, new HandleComparator()); @@ -83,14 +88,11 @@ public class SketchParser { * list of all numbers in the sketch (excluding hexadecimals) */ private void addAllDecimalNumbers() { - allHandles = new ArrayList<>(); - // for every number found: // save its type (int/float), name, value and position in code. Pattern p = Pattern.compile("[\\[\\{<>(),\\t\\s\\+\\-\\/\\*^%!|&=?:~]\\d+\\.?\\d*"); for (int i = 0; i < codeTabs.length; i++) { - //allHandles[i] = new ArrayList(); List handles = new ArrayList(); allHandles.add(handles); @@ -106,6 +108,11 @@ public class SketchParser { // ignore comments continue; } + + if (setupFunction.contains(start)) { + // ignore numbers in setup + continue; + } if (requiresComment) { // only add numbers that have the "// tweak" comment in their line @@ -193,6 +200,11 @@ public class SketchParser { // ignore comments continue; } + + if (setupFunction.contains(start)) { + // ignore number in setup + continue; + } if (requiresComment) { // only add numbers that have the "// tweak" comment in their line @@ -249,6 +261,11 @@ public class SketchParser { // ignore comments continue; } + + if (setupFunction.contains(start)) { + // ignore number in setup + continue; + } if (requiresComment) { // only add numbers that have the "// tweak" comment in their line @@ -326,7 +343,6 @@ public class SketchParser { Pattern p = Pattern.compile("color\\(|color\\s\\(|fill[\\(\\s]|stroke[\\(\\s]|background[\\(\\s]|tint[\\(\\s]"); for (int i = 0; i < codeTabs.length; i++) { - //colorBoxes[i] = new ArrayList(); List colorBox = new ArrayList(); colorBoxes.add(colorBox); @@ -348,6 +364,11 @@ public class SketchParser { // ignore colors in a comment continue; } + + if (setupFunction.contains(m.start())) { + // ignore number in setup + continue; + } // look for handles inside the parenthesis for (Handle handle : allHandles.get(i)) { @@ -360,7 +381,7 @@ public class SketchParser { if (colorHandles.size() > 0) { /* make sure there is no other stuff between '()' like variables. - * substract all handle values from string inside parenthesis and + * subtract all handle values from string inside parenthesis and * check there is no garbage left */ String insidePar = tab.substring(openPar+1, closePar); @@ -427,6 +448,11 @@ public class SketchParser { // ignore colors in a comment continue; } + + if (setupFunction.contains(m.start())) { + // ignore number in setup + continue; + } // put 'colorParamsEnd' after three parameters inside the parenthesis or at the close int colorParamsEnd = openPar; @@ -450,7 +476,7 @@ public class SketchParser { if (colorHandles.size() > 0) { /* make sure there is no other stuff between '()' like variables. - * substract all handle values from string inside parenthesis and + * subtract all handle values from string inside parenthesis and * check there is no garbage left */ String insidePar = tab.substring(openPar+1, colorParamsEnd); @@ -546,14 +572,10 @@ public class SketchParser { List> notations = new ArrayList<>(); Pattern p = Pattern.compile("[+\\-]?(?:0|[1-9]\\d*)(?:\\.\\d*)?[eE][+\\-]?\\d+"); - //for (int i = 0; i < codeTabs.length; i++) { for (String code : codeTabs) { List notation = new ArrayList(); - //notations[i] = new ArrayList(); - //Matcher m = p.matcher(codeTabs[i]); Matcher m = p.matcher(code); while (m.find()) { - //notations[i].add(new Range(m.start(), m.end())); notation.add(new Range(m.start(), m.end())); } notations.add(notation); @@ -772,11 +794,37 @@ public class SketchParser { return -1; } - - -// private String replaceString(String str, int start, int end, String put) { -// return str.substring(0, start) + put + str.substring(end, str.length()); -// } + + static public int getSetupEnd(String code) { + int setupStart = getSetupStart(code); + if (setupStart == -1) { + return -1; + } + + // count brackets to look for setup end + int bracketCount=1; + int pos = setupStart; + while (bracketCount>0 && pos