| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 585f854 commit 9471ca3
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2677,8 +2677,8 @@ protected void applyPreferences() { | |||
| 2677 | 2677 | ||
| 2678 | 2678 | // TWEAK MODE | |
| 2679 | 2679 | ||
| 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"; | ||
| 2682 | 2682 | ||
| 2683 | 2683 | public String[] baseCode; | |
| 2684 | 2684 | TweakClient tweakClient; | |
@@ -2881,17 +2881,17 @@ protected boolean automateSketch(Sketch sketch, SketchParser parser) { | |||
| 2881 | 2881 | ||
| 2882 | 2882 | // get port number from preferences.txt | |
| 2883 | 2883 | int port; | |
| 2884 | - String portStr = Preferences.get(prefTweakPort); | ||
| 2884 | + String portStr = Preferences.get(PREF_TWEAK_PORT); | ||
| 2885 | 2885 | if (portStr == null) { | |
| 2886 | - Preferences.set(prefTweakPort, "auto"); | ||
| 2886 | + Preferences.set(PREF_TWEAK_PORT, "auto"); | ||
| 2887 | 2887 | portStr = "auto"; | |
| 2888 | 2888 | } | |
| 2889 | 2889 | ||
| 2890 | 2890 | if (portStr.equals("auto")) { | |
| 2891 | 2891 | // random port for udp (0xc000 - 0xffff) | |
| 2892 | 2892 | port = (int)(Math.random()*0x3fff) + 0xc000; | |
| 2893 | 2893 | } else { | |
| 2894 | - port = Preferences.getInteger(prefTweakPort); | ||
| 2894 | + port = Preferences.getInteger(PREF_TWEAK_PORT); | ||
| 2895 | 2895 | } | |
| 2896 | 2896 | ||
| 2897 | 2897 | // create the client that will send the new values to the sketch | |
@@ -2946,7 +2946,7 @@ protected boolean automateSketch(Sketch sketch, SketchParser parser) { | |||
| 2946 | 2946 | } | |
| 2947 | 2947 | ||
| 2948 | 2948 | // 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); | ||
| 2950 | 2950 | header += "TweakModeServer tweakmode_Server;\n"; | |
| 2951 | 2951 | ||
| 2952 | 2952 | header += "void tweakmode_initAllVars() {\n"; | |
@@ -2975,15 +2975,18 @@ protected boolean automateSketch(Sketch sketch, SketchParser parser) { | |||
| 2975 | 2975 | setupEndPos = SketchParser.getSetupEnd(c); | |
| 2976 | 2976 | c = replaceString(c, setupEndPos, setupEndPos, addToSetup); | |
| 2977 | 2977 | ||
| 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); | ||
| 2979 | 2982 | ||
| 2980 | 2983 | // print out modified code | |
| 2981 | - String showModCode = Preferences.get(prefTweakShowCode); | ||
| 2984 | + String showModCode = Preferences.get(PREF_TWEAK_SHOW_CODE); | ||
| 2982 | 2985 | if (showModCode == null) { | |
| 2983 | - Preferences.setBoolean(prefTweakShowCode, false); | ||
| 2986 | + Preferences.setBoolean(PREF_TWEAK_SHOW_CODE, false); | ||
| 2984 | 2987 | } | |
| 2985 | 2988 | ||
| 2986 | - if (Preferences.getBoolean(prefTweakShowCode)) { | ||
| 2989 | + if (Preferences.getBoolean(PREF_TWEAK_SHOW_CODE)) { | ||
| 2987 | 2990 | System.out.println("\nTweakMode modified code:\n"); | |
| 2988 | 2991 | for (int i=0; i<code.length; i++) { | |
| 2989 | 2992 | System.out.println("tab " + i + "\n"); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -35,36 +35,37 @@ public class SketchParser { | |||
| 35 | 35 | ||
| 36 | 36 | String[] codeTabs; | |
| 37 | 37 | boolean requiresComment; | |
| 38 | - ArrayList<ColorMode> colorModes; | ||
| 38 | + List<ColorMode> colorModes; | ||
| 39 | 39 | ||
| 40 | 40 | List<List<Range>> scientificNotations; | |
| 41 | - | ||
| 41 | + | ||
| 42 | 42 | Range setupFunction; | |
| 43 | - | ||
| 43 | + | ||
| 44 | 44 | List<List<Range>> commentBlocks; | |
| 45 | 45 | List<int[]> curlyScopes; | |
| 46 | - | ||
| 46 | + | ||
| 47 | + | ||
| 47 | 48 | public SketchParser(String[] codeTabs, boolean requiresComment) { | |
| 48 | 49 | this.codeTabs = codeTabs; | |
| 49 | 50 | this.requiresComment = requiresComment; | |
| 50 | - intVarCount=0; | ||
| 51 | - floatVarCount=0; | ||
| 52 | - | ||
| 51 | + intVarCount = 0; | ||
| 52 | + floatVarCount = 0; | ||
| 53 | + | ||
| 53 | 54 | // get all comment blocks | |
| 54 | 55 | commentBlocks = new ArrayList<>(); | |
| 55 | 56 | for (String code : codeTabs) { | |
| 56 | 57 | commentBlocks.add(getCommentBlocks(code)); | |
| 57 | 58 | } | |
| 58 | - | ||
| 59 | + | ||
| 59 | 60 | // get setup function range (to ignore all numbers there) | |
| 60 | 61 | setupFunction = new Range(getSetupStart(codeTabs[0]), getSetupEnd(codeTabs[0])); | |
| 61 | - | ||
| 62 | + | ||
| 62 | 63 | // build curly scope for every character in the code | |
| 63 | 64 | curlyScopes = new ArrayList<>(); | |
| 64 | 65 | for (String code : codeTabs) { | |
| 65 | - curlyScopes.add(getCurlyScopes(code)); | ||
| 66 | + curlyScopes.add(getCurlyScopes(code)); | ||
| 66 | 67 | } | |
| 67 | - | ||
| 68 | + | ||
| 68 | 69 | // get all scientific notation (to ignore them) | |
| 69 | 70 | scientificNotations = getAllScientificNotations(); | |
| 70 | 71 | ||
@@ -84,7 +85,7 @@ public SketchParser(String[] codeTabs, boolean requiresComment) { | |||
| 84 | 85 | } | |
| 85 | 86 | ||
| 86 | 87 | ||
| 87 | - public void addAllNumbers() { | ||
| 88 | + private void addAllNumbers() { | ||
| 88 | 89 | allHandles = new ArrayList<>(); | |
| 89 | 90 | ||
| 90 | 91 | addAllDecimalNumbers(); | |
@@ -123,7 +124,7 @@ private void addAllDecimalNumbers() { | |||
| 123 | 124 | // ignore comments | |
| 124 | 125 | continue; | |
| 125 | 126 | } | |
| 126 | - | ||
| 127 | + | ||
| 127 | 128 | if (setupFunction.contains(start)) { | |
| 128 | 129 | // ignore numbers in setup | |
| 129 | 130 | continue; | |
@@ -215,7 +216,7 @@ private void addAllHexNumbers() { | |||
| 215 | 216 | // ignore comments | |
| 216 | 217 | continue; | |
| 217 | 218 | } | |
| 218 | - | ||
| 219 | + | ||
| 219 | 220 | if (setupFunction.contains(start)) { | |
| 220 | 221 | // ignore number in setup | |
| 221 | 222 | continue; | |
@@ -276,7 +277,7 @@ private void addAllWebColorNumbers() { | |||
| 276 | 277 | // ignore comments | |
| 277 | 278 | continue; | |
| 278 | 279 | } | |
| 279 | - | ||
| 280 | + | ||
| 280 | 281 | if (setupFunction.contains(start)) { | |
| 281 | 282 | // ignore number in setup | |
| 282 | 283 | continue; | |
@@ -380,7 +381,7 @@ private void createColorBoxes() { | |||
| 380 | 381 | // ignore colors in a comment | |
| 381 | 382 | continue; | |
| 382 | 383 | } | |
| 383 | - | ||
| 384 | + | ||
| 384 | 385 | if (setupFunction.contains(m.start())) { | |
| 385 | 386 | // ignore number in setup | |
| 386 | 387 | continue; | |
@@ -464,7 +465,7 @@ private void createColorBoxesForLights() { | |||
| 464 | 465 | // ignore colors in a comment | |
| 465 | 466 | continue; | |
| 466 | 467 | } | |
| 467 | - | ||
| 468 | + | ||
| 468 | 469 | if (setupFunction.contains(m.start())) { | |
| 469 | 470 | // ignore number in setup | |
| 470 | 471 | continue; | |
@@ -683,28 +684,28 @@ static private boolean isInsideString(int pos, String code) { | |||
| 683 | 684 | ||
| 684 | 685 | return false; | |
| 685 | 686 | } | |
| 686 | - | ||
| 687 | + | ||
| 687 | 688 | /** | |
| 688 | 689 | * Builds an int array for every tab that represents the scope depth at each character | |
| 689 | - * | ||
| 690 | + * | ||
| 690 | 691 | * @return | |
| 691 | 692 | */ | |
| 692 | 693 | static private int[] getCurlyScopes(String code) | |
| 693 | 694 | { | |
| 694 | 695 | List<Range> comments = getCommentBlocks(code); | |
| 695 | - | ||
| 696 | + | ||
| 696 | 697 | int[] scopes = new int[code.length()]; | |
| 697 | 698 | int curlyScope = 0; | |
| 698 | 699 | boolean arrayAssignmentMaybeCommingFlag = false; | |
| 699 | 700 | int arrayAssignmentCurlyScope = 0; | |
| 700 | 701 | for (int pos=0; pos<code.length(); pos++) { | |
| 701 | 702 | scopes[pos] = curlyScope; | |
| 702 | - | ||
| 703 | + | ||
| 703 | 704 | if (isInRangeList(pos, comments)) { | |
| 704 | 705 | // we are inside a comment, ignore and move on | |
| 705 | 706 | continue; | |
| 706 | 707 | } | |
| 707 | - | ||
| 708 | + | ||
| 708 | 709 | if (code.charAt(pos) == '{') { | |
| 709 | 710 | if (arrayAssignmentMaybeCommingFlag || | |
| 710 | 711 | arrayAssignmentCurlyScope>0) { | |
@@ -731,10 +732,10 @@ else if (!isWhiteSpace(code.charAt(pos))) { | |||
| 731 | 732 | arrayAssignmentMaybeCommingFlag = false; | |
| 732 | 733 | } | |
| 733 | 734 | } | |
| 734 | - | ||
| 735 | + | ||
| 735 | 736 | return scopes; | |
| 736 | 737 | } | |
| 737 | - | ||
| 738 | + | ||
| 738 | 739 | static private boolean isWhiteSpace(char c) { | |
| 739 | 740 | return c == ' ' || c == '\t' || c == '\n' || c == '\r'; | |
| 740 | 741 | } | |
@@ -745,15 +746,15 @@ static private boolean isWhiteSpace(char c) { | |||
| 745 | 746 | * @param codeTabIndex index of the code in codeTabs | |
| 746 | 747 | * @return | |
| 747 | 748 | * true if the position 'pos' is in global scope in the code 'codeTabs[codeTabIndex]' | |
| 748 | - * | ||
| 749 | + * | ||
| 749 | 750 | */ | |
| 750 | 751 | private boolean isGlobal(int pos, int codeTabIndex) { | |
| 751 | 752 | return (curlyScopes.get(codeTabIndex)[pos]==0); | |
| 752 | 753 | }; | |
| 753 | 754 | ||
| 754 | - public static List<Range> getCommentBlocks(String code) { | ||
| 755 | + static private List<Range> getCommentBlocks(String code) { | ||
| 755 | 756 | List<Range> commentBlocks = new ArrayList<Range>(); | |
| 756 | - | ||
| 757 | + | ||
| 757 | 758 | int lastBlockStart=0; | |
| 758 | 759 | boolean lookForEnd = false; | |
| 759 | 760 | for (int pos=0; pos<code.length()-1; pos++) { | |
@@ -775,19 +776,19 @@ else if (code.charAt(pos) == '/' && code.charAt(pos+1) == '/') { | |||
| 775 | 776 | commentBlocks.add(new Range(pos, getEndOfLine(pos, code))); | |
| 776 | 777 | } | |
| 777 | 778 | } | |
| 778 | - | ||
| 779 | + | ||
| 779 | 780 | } | |
| 780 | - | ||
| 781 | + | ||
| 781 | 782 | return commentBlocks; | |
| 782 | 783 | } | |
| 783 | - | ||
| 784 | + | ||
| 784 | 785 | private static boolean isInRangeList(int pos, List<Range> rangeList) { | |
| 785 | 786 | for (Range r : rangeList) { | |
| 786 | 787 | if (r.contains(pos)) { | |
| 787 | 788 | return true; | |
| 788 | 789 | } | |
| 789 | 790 | } | |
| 790 | - | ||
| 791 | + | ||
| 791 | 792 | return false; | |
| 792 | 793 | } | |
| 793 | 794 | ||
@@ -826,7 +827,7 @@ else if (readObject) { | |||
| 826 | 827 | } | |
| 827 | 828 | ||
| 828 | 829 | ||
| 829 | - public static int getSetupStart(String code) { | ||
| 830 | + static public int getSetupStart(String code) { | ||
| 830 | 831 | Pattern p = Pattern.compile("void[\\s\\t\\r\\n]*setup[\\s\\t]*\\(\\)[\\s\\t\\r\\n]*\\{"); | |
| 831 | 832 | Matcher m = p.matcher(code); | |
| 832 | 833 | ||
@@ -836,42 +837,43 @@ public static int getSetupStart(String code) { | |||
| 836 | 837 | ||
| 837 | 838 | return -1; | |
| 838 | 839 | } | |
| 839 | - | ||
| 840 | - public static int getSetupEnd(String code) { | ||
| 840 | + | ||
| 841 | + | ||
| 842 | + static public int getSetupEnd(String code) { | ||
| 841 | 843 | List<Range> comments = getCommentBlocks(code); | |
| 842 | - | ||
| 844 | + | ||
| 843 | 845 | int setupStart = getSetupStart(code); | |
| 844 | 846 | if (setupStart == -1) { | |
| 845 | 847 | return -1; | |
| 846 | 848 | } | |
| 847 | - | ||
| 849 | + | ||
| 848 | 850 | System.out.println("setup start = " + setupStart); | |
| 849 | - | ||
| 851 | + | ||
| 850 | 852 | // count brackets to look for setup end | |
| 851 | 853 | int bracketCount=1; | |
| 852 | 854 | int pos = setupStart; | |
| 853 | 855 | while (bracketCount>0 && pos<code.length()) { | |
| 854 | - | ||
| 856 | + | ||
| 855 | 857 | if (isInRangeList(pos, comments)) { | |
| 856 | 858 | // in a comment, ignore and move on | |
| 857 | 859 | pos++; | |
| 858 | 860 | continue; | |
| 859 | 861 | } | |
| 860 | - | ||
| 862 | + | ||
| 861 | 863 | if (code.charAt(pos) == '{') { | |
| 862 | 864 | bracketCount++; | |
| 863 | 865 | } | |
| 864 | 866 | else if (code.charAt(pos) == '}') { | |
| 865 | 867 | bracketCount--; | |
| 866 | 868 | } | |
| 867 | - | ||
| 869 | + | ||
| 868 | 870 | pos++; | |
| 869 | 871 | } | |
| 870 | - | ||
| 872 | + | ||
| 871 | 873 | if (bracketCount == 0) { | |
| 872 | 874 | return pos-1; | |
| 873 | 875 | } | |
| 874 | - | ||
| 876 | + | ||
| 875 | 877 | return -1; | |
| 876 | 878 | } | |
| 877 | 879 | ||
@@ -880,12 +882,12 @@ static class Range { | |||
| 880 | 882 | int start; | |
| 881 | 883 | int end; | |
| 882 | 884 | ||
| 883 | - public Range(int s, int e) { | ||
| 885 | + Range(int s, int e) { | ||
| 884 | 886 | start = s; | |
| 885 | 887 | end = e; | |
| 886 | 888 | } | |
| 887 | 889 | ||
| 888 | - public boolean contains(int v) { | ||
| 890 | + boolean contains(int v) { | ||
| 889 | 891 | return v >= start && v < end; | |
| 890 | 892 | } | |
| 891 | 893 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments