@@ -153,13 +153,86 @@ public static enum Mode {
153153// private boolean foundMain;
154154 private String advClassName = "" ;
155155 protected Mode mode ;
156- HashMap <String , Object > foundMethods ;
156+ Set <String > foundMethods ;
157+
158+ // protected String sizeStatement;
159+ // protected String sketchWidth;
160+ // protected String sketchHeight;
161+ // protected String sketchRenderer;
162+ // protected String sketchOutputPath;
163+
164+ /*
165+ static class SizeInfo {
166+ String statement;
167+ String width;
168+ String height;
169+ String renderer;
170+ String path;
171+ String display;
172+
173+ // SizeInfo(String statement, String width, String height,
174+ // String renderer, String outputPath, String display) {
175+ // this.statement = statement;
176+ // }
177+
178+ boolean hasOldSyntax() {
179+ if (width.equals("screenWidth") ||
180+ width.equals("screenHeight") ||
181+ height.equals("screenHeight") ||
182+ height.equals("screenWidth")) {
183+ final String message =
184+ "The screenWidth and screenHeight variables are named\n" +
185+ "displayWidth and displayHeight in Processing 3.\n" +
186+ "Or you can use the fullScreen() method instead of size().";
187+ Base.showWarning("Time for a quick update", message, null);
188+ return true;
189+ }
190+ if (width.equals("screen.width") ||
191+ width.equals("screen.height") ||
192+ height.equals("screen.height") ||
193+ height.equals("screen.width")) {
194+ final String message =
195+ "The screen.width and screen.height variables are named\n" +
196+ "displayWidth and displayHeight in Processing 3.\n" +
197+ "Or you can use the fullScreen() method instead of size().";
198+ Base.showWarning("Time for a quick update", message, null);
199+ return true;
200+ }
201+ return false;
202+ }
203+
204+ boolean hasBadSize() {
205+ if (!width.equals("displayWidth") &&
206+ !width.equals("displayHeight") &&
207+ PApplet.parseInt(width, -1) == -1) {
208+ return true;
209+ }
210+ if (!height.equals("displayWidth") &&
211+ !height.equals("displayHeight") &&
212+ PApplet.parseInt(height, -1) == -1) {
213+ return true;
214+ }
215+ return false;
216+ }
217+
218+
219+ void checkEmpty() {
220+ if (renderer != null) {
221+ if (renderer.length() == 0) { // if empty, set null
222+ renderer = null;
223+ }
224+ }
225+ if (path != null) {
226+ if (path.length() == 0) {
227+ path = null;
228+ }
229+ }
230+ }
231+ }
232+ */
233+
234+ SizeInfo sizeInfo ;
157235
158- protected String sizeStatement ;
159- protected String sketchWidth ;
160- protected String sketchHeight ;
161- protected String sketchRenderer ;
162- protected String sketchOutputPath ;
163236
164237 /**
165238 * Regular expression for parsing the size() method. This should match
@@ -172,6 +245,13 @@ public static enum Mode {
172245// "(?:^|\\s|;)size\\s*\\(\\s*([^\\s,]+)\\s*,\\s*([^\\s,\\)]+)\\s*,?\\s*([^\\)]*)\\s*\\)\\s*\\;";
173246 static private final String SIZE_CONTENTS_REGEX =
174247 "(?:^|\\ s|;)size\\ s*\\ (([^\\ )]+)\\ )\\ s*\\ ;" ;
248+ static private final String FULL_SCREEN_CONTENTS_REGEX =
249+ "(?:^|\\ s|;)fullScreen\\ s*\\ (([^\\ )]+)\\ )\\ s*\\ ;" ;
250+ /** Test whether there's a void somewhere (the program has functions). */
251+ static private final String VOID_REGEX =
252+ "(?:^|\\ s|;)void\\ s" ;
253+ static private final String VOID_SETUP_REGEX =
254+ "(?:^|\\ s|;)void\\ ssetup\\ s*\\ (" ;
175255
176256
177257 static private final Pattern PUBLIC_CLASS =
@@ -201,17 +281,18 @@ public PdePreprocessor(final String sketchName, final int tabSize) {
201281 }
202282
203283
204- public String [] initSketchSize (String code , boolean sizeWarning ) throws SketchException {
205- String [] info = parseSketchSize (code , sizeWarning );
206- //PApplet.printArray(info);
207- if (info != null ) {
208- sizeStatement = info [0 ];
209- sketchWidth = info [1 ];
210- sketchHeight = info [2 ];
211- sketchRenderer = info [3 ];
212- sketchOutputPath = info [4 ];
213- }
214- return info ;
284+ public SizeInfo initSketchSize (String code , boolean sizeWarning ) throws SketchException {
285+ // String[] info = parseSketchSize(code, sizeWarning);
286+ // if (info != null) {
287+ // sizeStatement = info[0];
288+ // sketchWidth = info[1];
289+ // sketchHeight = info[2];
290+ // sketchRenderer = info[3];
291+ // sketchOutputPath = info[4];
292+ // }
293+ // return info;
294+ sizeInfo = parseSketchSize (code , sizeWarning );
295+ return sizeInfo ;
215296 }
216297
217298
@@ -259,7 +340,7 @@ static private StringList breakCommas(String contents) {
259340 * @param fussy true if it should show an error message if bad size()
260341 * @return null if there was an error, otherwise an array (might contain some/all nulls)
261342 */
262- static public String [] parseSketchSize (String code , boolean fussy ) {
343+ static public SizeInfo parseSketchSize (String code , boolean fussy ) {
263344 // This matches against any uses of the size() function, whether numbers
264345 // or variables or whatever. This way, no warning is shown if size() isn't
265346 // actually used in the applet, which is the case especially for anyone
@@ -269,47 +350,71 @@ static public String[] parseSketchSize(String code, boolean fussy) {
269350// String[] matches = PApplet.match(scrubbed, SIZE_REGEX);
270351// String[] matches = PApplet.match(scrubComments(code), SIZE_REGEX);
271352
353+ /*
354+ 1. no size() or fullScreen() method at all
355+ will use the non-overridden settings() method in PApplet
356+ 2. size() or fullScreen() found inside setup() (static mode sketch or otherwise)
357+ make sure that it uses numbers (or displayWidth/Height), copy into settings
358+ 3. size() or fullScreen() already in settings()
359+ don't mess with the sketch, don't insert any defaults
360+
361+ really only need to deal with situation #2.. nothing to be done for 1 and 3
362+ */
363+ // if static mode sketch, all we need is regex
364+ // easy proxy for static in this case is whether [^\s]void\s is present
365+
366+ String searchArea = scrubComments (code );
367+ String [] setupMatch = PApplet .match (searchArea , VOID_SETUP_REGEX );
368+ if (setupMatch != null ) {
369+ String found = setupMatch [0 ];
370+ int start = searchArea .indexOf (found ) + found .length ();
371+ int openBrace = searchArea .indexOf ("{" , start );
372+ char [] c = searchArea .toCharArray ();
373+ int depth = 0 ;
374+ int closeBrace = -1 ;
375+ StringBuilder sb = new StringBuilder ();
376+ for (int i = openBrace ; i < c .length ; i ++) {
377+ if (c [i ] == '{' ) {
378+ depth ++;
379+ } else if (c [i ] == '}' ) {
380+ depth --;
381+ if (depth == 0 ) {
382+ closeBrace = ++i ;
383+ break ;
384+ }
385+ } else {
386+ sb .append (c [i ]);
387+ }
388+ }
389+ if (closeBrace == -1 ) {
390+ // throw new SketchException("Found a { that's missing a matching }");
391+ return null ;
392+ }
393+ searchArea = sb .toString ();
394+ }
395+
272396 // Get everything inside the parens for the size() method
273- String [] contents = PApplet .match (scrubComments ( code ) , SIZE_CONTENTS_REGEX );
397+ String [] contents = PApplet .match (searchArea , SIZE_CONTENTS_REGEX );
274398 if (contents != null ) {
275399 //String[] matches = split on commas, but not commas inside quotes
276400
277401 StringList args = breakCommas (contents [1 ]);
278- String width = args . get ( 0 ). trim ();
279- String height = args . get ( 1 ). trim () ;
280- String renderer = ( args .size () >= 3 ) ? args . get ( 2 ) : null ;
281- String path = ( args .size () >= 4 ) ? args . get ( 3 ) : null ;
282-
283- boolean badSize = false ;
402+ SizeInfo info = new SizeInfo ();
403+ info . statement = contents [ 0 ] ;
404+ info . width = args .get ( 0 ). trim () ;
405+ info . height = args .get ( 1 ). trim () ;
406+ info . renderer = ( args . size () >= 3 ) ? args . get ( 2 ). trim () : null ;
407+ info . path = ( args . size () >= 4 ) ? args . get ( 3 ). trim () : null ;
284408
285409 // Trying to remember why we wanted to allow people to use displayWidth
286410 // as the height or displayHeight as the width, but maybe it's for
287411 // making a square sketch window? Not going to
288412
289- if (width .equals ("screenWidth" ) ||
290- width .equals ("screenHeight" ) ||
291- height .equals ("screenHeight" ) ||
292- height .equals ("screenWidth" )) {
293- final String message =
294- "The screenWidth and screenHeight variables\n " +
295- "are named displayWidth and displayHeight\n " +
296- "in this release of Processing." ;
297- Base .showWarning ("Time for a quick update" , message , null );
413+ if (info .hasOldSyntax ()) {
298414 return null ;
299415 }
300416
301- if (!width .equals ("displayWidth" ) &&
302- !width .equals ("displayHeight" ) &&
303- PApplet .parseInt (width , -1 ) == -1 ) {
304- badSize = true ;
305- }
306- if (!height .equals ("displayWidth" ) &&
307- !height .equals ("displayHeight" ) &&
308- PApplet .parseInt (height , -1 ) == -1 ) {
309- badSize = true ;
310- }
311-
312- if (badSize && fussy ) {
417+ if (info .hasBadSize () && fussy ) {
313418 // found a reference to size, but it didn't seem to contain numbers
314419 final String message =
315420 "The size of this applet could not automatically\n " +
@@ -321,25 +426,13 @@ static public String[] parseSketchSize(String code, boolean fussy) {
321426 return null ;
322427 }
323428
324- // Remove additional space 'round the renderer
325- if (renderer != null ) {
326- renderer = renderer .trim ();
327- if (renderer .length () == 0 ) { // if empty, set null
328- renderer = null ;
329- }
330- }
331-
332- // Same for the file name
333- if (path != null ) {
334- path = path .trim ();
335- if (path .length () == 0 ) {
336- path = null ;
337- }
338- }
339- return new String [] { contents [0 ], width , height , renderer , path };
429+ info .checkEmpty ();
430+ return info ;
431+ //return new String[] { contents[0], width, height, renderer, path };
340432 }
341433 // not an error, just no size() specified
342- return new String [] { null , null , null , null , null };
434+ //return new String[] { null, null, null, null, null };
435+ return new SizeInfo ();
343436 }
344437
345438
@@ -405,12 +498,12 @@ static public String scrubComments(String what) {
405498
406499
407500 public void addMethod (String methodName ) {
408- foundMethods .put (methodName , new Object () );
501+ foundMethods .add (methodName );
409502 }
410503
411504
412505 public boolean hasMethod (String methodName ) {
413- return foundMethods .containsKey (methodName );
506+ return foundMethods .contains (methodName );
414507 }
415508
416509
@@ -571,7 +664,7 @@ public PreprocessorResult write(Writer out, String program,
571664
572665 // need to reset whether or not this has a main()
573666// foundMain = false;
574- foundMethods = new HashMap <String , Object >();
667+ foundMethods = new HashSet <String >();
575668
576669 // http://processing.org/bugs/bugzilla/5.html
577670 if (!program .endsWith ("\n " )) {
@@ -941,8 +1034,8 @@ protected void writeFooter(PrintWriter out, String className) {
9411034 if ((mode == Mode .STATIC ) || (mode == Mode .ACTIVE )) {
9421035 // doesn't remove the oriiginal size() method, but calling size()
9431036 // again in setup() is harmless.
944- if (!hasMethod ("settings" ) && sizeStatement != null ) {
945- out .println (indent + "public void settings() { " + sizeStatement + " }" );
1037+ if (!hasMethod ("settings" ) && sizeInfo . statement != null ) {
1038+ out .println (indent + "public void settings() { " + sizeInfo . statement + " }" );
9461039// out.println(indent + "public void settings() {");
9471040// out.println(indent + indent + sizeStatement);
9481041// out.println(indent + "}");
0 commit comments