Skip to content

Commit 9471ca3

Browse files
committed
get tweak mode working again, remove debug msg, remove unneeded access levels (fixes processing#3435)
1 parent 585f854 commit 9471ca3

2 files changed

Lines changed: 59 additions & 54 deletions

File tree

java/src/processing/mode/java/JavaEditor.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2677,8 +2677,8 @@ protected void applyPreferences() {
26772677

26782678
// TWEAK MODE
26792679

2680-
static final String prefTweakPort = "tweak.port";
2681-
static final String prefTweakShowCode = "tweak.showcode";
2680+
static final String PREF_TWEAK_PORT = "tweak.port";
2681+
static final String PREF_TWEAK_SHOW_CODE = "tweak.showcode";
26822682

26832683
public String[] baseCode;
26842684
TweakClient tweakClient;
@@ -2881,17 +2881,17 @@ protected boolean automateSketch(Sketch sketch, SketchParser parser) {
28812881

28822882
// get port number from preferences.txt
28832883
int port;
2884-
String portStr = Preferences.get(prefTweakPort);
2884+
String portStr = Preferences.get(PREF_TWEAK_PORT);
28852885
if (portStr == null) {
2886-
Preferences.set(prefTweakPort, "auto");
2886+
Preferences.set(PREF_TWEAK_PORT, "auto");
28872887
portStr = "auto";
28882888
}
28892889

28902890
if (portStr.equals("auto")) {
28912891
// random port for udp (0xc000 - 0xffff)
28922892
port = (int)(Math.random()*0x3fff) + 0xc000;
28932893
} else {
2894-
port = Preferences.getInteger(prefTweakPort);
2894+
port = Preferences.getInteger(PREF_TWEAK_PORT);
28952895
}
28962896

28972897
// create the client that will send the new values to the sketch
@@ -2946,7 +2946,7 @@ protected boolean automateSketch(Sketch sketch, SketchParser parser) {
29462946
}
29472947

29482948
// add the server code that will receive the value change messages
2949-
header += TweakClient.getServerCode(port, numOfInts>0, numOfFloats>0);
2949+
// header += TweakClient.getServerCode(port, numOfInts>0, numOfFloats>0);
29502950
header += "TweakModeServer tweakmode_Server;\n";
29512951

29522952
header += "void tweakmode_initAllVars() {\n";
@@ -2975,15 +2975,18 @@ protected boolean automateSketch(Sketch sketch, SketchParser parser) {
29752975
setupEndPos = SketchParser.getSetupEnd(c);
29762976
c = replaceString(c, setupEndPos, setupEndPos, addToSetup);
29772977

2978-
code[0].setProgram(header + c);
2978+
// Server code defines a class, so it should go later in the sketch
2979+
String serverCode =
2980+
TweakClient.getServerCode(port, numOfInts>0, numOfFloats>0);
2981+
code[0].setProgram(header + c + serverCode);
29792982

29802983
// print out modified code
2981-
String showModCode = Preferences.get(prefTweakShowCode);
2984+
String showModCode = Preferences.get(PREF_TWEAK_SHOW_CODE);
29822985
if (showModCode == null) {
2983-
Preferences.setBoolean(prefTweakShowCode, false);
2986+
Preferences.setBoolean(PREF_TWEAK_SHOW_CODE, false);
29842987
}
29852988

2986-
if (Preferences.getBoolean(prefTweakShowCode)) {
2989+
if (Preferences.getBoolean(PREF_TWEAK_SHOW_CODE)) {
29872990
System.out.println("\nTweakMode modified code:\n");
29882991
for (int i=0; i<code.length; i++) {
29892992
System.out.println("tab " + i + "\n");

java/src/processing/mode/java/tweak/SketchParser.java

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -35,36 +35,37 @@ public class SketchParser {
3535

3636
String[] codeTabs;
3737
boolean requiresComment;
38-
ArrayList<ColorMode> colorModes;
38+
List<ColorMode> colorModes;
3939

4040
List<List<Range>> scientificNotations;
41-
41+
4242
Range setupFunction;
43-
43+
4444
List<List<Range>> commentBlocks;
4545
List<int[]> curlyScopes;
46-
46+
47+
4748
public SketchParser(String[] codeTabs, boolean requiresComment) {
4849
this.codeTabs = codeTabs;
4950
this.requiresComment = requiresComment;
50-
intVarCount=0;
51-
floatVarCount=0;
52-
51+
intVarCount = 0;
52+
floatVarCount = 0;
53+
5354
// get all comment blocks
5455
commentBlocks = new ArrayList<>();
5556
for (String code : codeTabs) {
5657
commentBlocks.add(getCommentBlocks(code));
5758
}
58-
59+
5960
// get setup function range (to ignore all numbers there)
6061
setupFunction = new Range(getSetupStart(codeTabs[0]), getSetupEnd(codeTabs[0]));
61-
62+
6263
// build curly scope for every character in the code
6364
curlyScopes = new ArrayList<>();
6465
for (String code : codeTabs) {
65-
curlyScopes.add(getCurlyScopes(code));
66+
curlyScopes.add(getCurlyScopes(code));
6667
}
67-
68+
6869
// get all scientific notation (to ignore them)
6970
scientificNotations = getAllScientificNotations();
7071

@@ -84,7 +85,7 @@ public SketchParser(String[] codeTabs, boolean requiresComment) {
8485
}
8586

8687

87-
public void addAllNumbers() {
88+
private void addAllNumbers() {
8889
allHandles = new ArrayList<>();
8990

9091
addAllDecimalNumbers();
@@ -123,7 +124,7 @@ private void addAllDecimalNumbers() {
123124
// ignore comments
124125
continue;
125126
}
126-
127+
127128
if (setupFunction.contains(start)) {
128129
// ignore numbers in setup
129130
continue;
@@ -215,7 +216,7 @@ private void addAllHexNumbers() {
215216
// ignore comments
216217
continue;
217218
}
218-
219+
219220
if (setupFunction.contains(start)) {
220221
// ignore number in setup
221222
continue;
@@ -276,7 +277,7 @@ private void addAllWebColorNumbers() {
276277
// ignore comments
277278
continue;
278279
}
279-
280+
280281
if (setupFunction.contains(start)) {
281282
// ignore number in setup
282283
continue;
@@ -380,7 +381,7 @@ private void createColorBoxes() {
380381
// ignore colors in a comment
381382
continue;
382383
}
383-
384+
384385
if (setupFunction.contains(m.start())) {
385386
// ignore number in setup
386387
continue;
@@ -464,7 +465,7 @@ private void createColorBoxesForLights() {
464465
// ignore colors in a comment
465466
continue;
466467
}
467-
468+
468469
if (setupFunction.contains(m.start())) {
469470
// ignore number in setup
470471
continue;
@@ -683,28 +684,28 @@ static private boolean isInsideString(int pos, String code) {
683684

684685
return false;
685686
}
686-
687+
687688
/**
688689
* Builds an int array for every tab that represents the scope depth at each character
689-
*
690+
*
690691
* @return
691692
*/
692693
static private int[] getCurlyScopes(String code)
693694
{
694695
List<Range> comments = getCommentBlocks(code);
695-
696+
696697
int[] scopes = new int[code.length()];
697698
int curlyScope = 0;
698699
boolean arrayAssignmentMaybeCommingFlag = false;
699700
int arrayAssignmentCurlyScope = 0;
700701
for (int pos=0; pos<code.length(); pos++) {
701702
scopes[pos] = curlyScope;
702-
703+
703704
if (isInRangeList(pos, comments)) {
704705
// we are inside a comment, ignore and move on
705706
continue;
706707
}
707-
708+
708709
if (code.charAt(pos) == '{') {
709710
if (arrayAssignmentMaybeCommingFlag ||
710711
arrayAssignmentCurlyScope>0) {
@@ -731,10 +732,10 @@ else if (!isWhiteSpace(code.charAt(pos))) {
731732
arrayAssignmentMaybeCommingFlag = false;
732733
}
733734
}
734-
735+
735736
return scopes;
736737
}
737-
738+
738739
static private boolean isWhiteSpace(char c) {
739740
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
740741
}
@@ -745,15 +746,15 @@ static private boolean isWhiteSpace(char c) {
745746
* @param codeTabIndex index of the code in codeTabs
746747
* @return
747748
* true if the position 'pos' is in global scope in the code 'codeTabs[codeTabIndex]'
748-
*
749+
*
749750
*/
750751
private boolean isGlobal(int pos, int codeTabIndex) {
751752
return (curlyScopes.get(codeTabIndex)[pos]==0);
752753
};
753754

754-
public static List<Range> getCommentBlocks(String code) {
755+
static private List<Range> getCommentBlocks(String code) {
755756
List<Range> commentBlocks = new ArrayList<Range>();
756-
757+
757758
int lastBlockStart=0;
758759
boolean lookForEnd = false;
759760
for (int pos=0; pos<code.length()-1; pos++) {
@@ -775,19 +776,19 @@ else if (code.charAt(pos) == '/' && code.charAt(pos+1) == '/') {
775776
commentBlocks.add(new Range(pos, getEndOfLine(pos, code)));
776777
}
777778
}
778-
779+
779780
}
780-
781+
781782
return commentBlocks;
782783
}
783-
784+
784785
private static boolean isInRangeList(int pos, List<Range> rangeList) {
785786
for (Range r : rangeList) {
786787
if (r.contains(pos)) {
787788
return true;
788789
}
789790
}
790-
791+
791792
return false;
792793
}
793794

@@ -826,7 +827,7 @@ else if (readObject) {
826827
}
827828

828829

829-
public static int getSetupStart(String code) {
830+
static public int getSetupStart(String code) {
830831
Pattern p = Pattern.compile("void[\\s\\t\\r\\n]*setup[\\s\\t]*\\(\\)[\\s\\t\\r\\n]*\\{");
831832
Matcher m = p.matcher(code);
832833

@@ -836,42 +837,43 @@ public static int getSetupStart(String code) {
836837

837838
return -1;
838839
}
839-
840-
public static int getSetupEnd(String code) {
840+
841+
842+
static public int getSetupEnd(String code) {
841843
List<Range> comments = getCommentBlocks(code);
842-
844+
843845
int setupStart = getSetupStart(code);
844846
if (setupStart == -1) {
845847
return -1;
846848
}
847-
849+
848850
System.out.println("setup start = " + setupStart);
849-
851+
850852
// count brackets to look for setup end
851853
int bracketCount=1;
852854
int pos = setupStart;
853855
while (bracketCount>0 && pos<code.length()) {
854-
856+
855857
if (isInRangeList(pos, comments)) {
856858
// in a comment, ignore and move on
857859
pos++;
858860
continue;
859861
}
860-
862+
861863
if (code.charAt(pos) == '{') {
862864
bracketCount++;
863865
}
864866
else if (code.charAt(pos) == '}') {
865867
bracketCount--;
866868
}
867-
869+
868870
pos++;
869871
}
870-
872+
871873
if (bracketCount == 0) {
872874
return pos-1;
873875
}
874-
876+
875877
return -1;
876878
}
877879

@@ -880,12 +882,12 @@ static class Range {
880882
int start;
881883
int end;
882884

883-
public Range(int s, int e) {
885+
Range(int s, int e) {
884886
start = s;
885887
end = e;
886888
}
887889

888-
public boolean contains(int v) {
890+
boolean contains(int v) {
889891
return v >= start && v < end;
890892
}
891893
}

0 commit comments

Comments
 (0)