diff --git a/java/src/processing/mode/java/preproc/PdeParseTreeListener.java b/java/src/processing/mode/java/preproc/PdeParseTreeListener.java index ff6410f5a..f1520c43e 100644 --- a/java/src/processing/mode/java/preproc/PdeParseTreeListener.java +++ b/java/src/processing/mode/java/preproc/PdeParseTreeListener.java @@ -51,6 +51,8 @@ import processing.mode.java.preproc.PdePreprocessor.Mode; public class PdeParseTreeListener extends ProcessingBaseListener { private static final String SIZE_METHOD_NAME = "size"; + private static final String SMOOTH_METHOD_NAME = "smooth"; + private static final String NO_SMOOTH_METHOD_NAME = "noSmooth"; private static final String PIXEL_DENSITY_METHOD_NAME = "pixelDensity"; private static final String FULLSCREEN_METHOD_NAME = "fullScreen"; @@ -73,12 +75,15 @@ public class PdeParseTreeListener extends ProcessingBaseListener { private String sketchWidth; private String sketchHeight; private String pixelDensity; + private String smoothParam; private String sketchRenderer = null; private String sketchOutputFilename = null; private boolean sizeRequiresRewrite = false; private boolean pixelDensityRequiresRewrite = false; private boolean sizeIsFullscreen = false; + private boolean noSmoothRequiresRewrite = false; + private boolean smoothRequiresRewrite = false; private RewriteResult headerResult; private RewriteResult footerResult; @@ -310,10 +315,12 @@ public class PdeParseTreeListener extends ProcessingBaseListener { if (SIZE_METHOD_NAME.equals(methodName) || FULLSCREEN_METHOD_NAME.equals(methodName)) { handleSizeCall(ctx); - } - - if (PIXEL_DENSITY_METHOD_NAME.equals(methodName)) { + } else if (PIXEL_DENSITY_METHOD_NAME.equals(methodName)) { handlePixelDensityCall(ctx); + } else if (NO_SMOOTH_METHOD_NAME.equals(methodName)) { + handleNoSmoothCall(ctx); + } else if (SMOOTH_METHOD_NAME.equals(methodName)) { + handleSmoothCall(ctx); } } @@ -585,22 +592,9 @@ public class PdeParseTreeListener extends ProcessingBaseListener { * @param ctx The context of the call. */ protected void handleSizeCall(ParserRuleContext ctx) { - ParserRuleContext testCtx = ctx.getParent() - .getParent() - .getParent() - .getParent(); - - boolean isInGlobal = - testCtx instanceof ProcessingParser.StaticProcessingSketchContext; - - boolean isInSetup; - if (!isInGlobal) { - ParserRuleContext methodDeclaration = testCtx.getParent() - .getParent(); - - isInSetup = isMethodSetup(methodDeclaration); - } else { - isInSetup = false; + // Check that this is the size call for processing and not a user defined size method. + if (!calledFromGlobalOrSetup(ctx)) { + return; } ParseTree argsContext = ctx.getChild(2); @@ -610,41 +604,45 @@ public class PdeParseTreeListener extends ProcessingBaseListener { boolean isSize = ctx.getChild(0).getText().equals(SIZE_METHOD_NAME); boolean isFullscreen = ctx.getChild(0).getText().equals(FULLSCREEN_METHOD_NAME); - if (isInGlobal || isInSetup) { + if (isSize && argsContext.getChildCount() > 2) { thisRequiresRewrite = true; - if (isSize && argsContext.getChildCount() > 2) { - sketchWidth = argsContext.getChild(0).getText(); - if (PApplet.parseInt(sketchWidth, -1) == -1 && - !sketchWidth.equals("displayWidth")) { - thisRequiresRewrite = false; - } - - sketchHeight = argsContext.getChild(2).getText(); - if (PApplet.parseInt(sketchHeight, -1) == -1 && - !sketchHeight.equals("displayHeight")) { - thisRequiresRewrite = false; - } - - if (argsContext.getChildCount() > 3) { - sketchRenderer = argsContext.getChild(4).getText(); - } - - if (argsContext.getChildCount() > 5) { - sketchOutputFilename = argsContext.getChild(6).getText(); - } + sketchWidth = argsContext.getChild(0).getText(); + boolean invalidWidth = PApplet.parseInt(sketchWidth, -1) == -1; + invalidWidth = invalidWidth && !sketchWidth.equals("displayWidth"); + if (invalidWidth) { + thisRequiresRewrite = false; } - if (isFullscreen) { - sketchWidth = "displayWidth"; - sketchWidth = "displayHeight"; + sketchHeight = argsContext.getChild(2).getText(); + boolean invalidHeight = PApplet.parseInt(sketchHeight, -1) == -1; + invalidHeight = invalidHeight && !sketchHeight.equals("displayHeight"); + if (invalidHeight) { + thisRequiresRewrite = false; + } - thisRequiresRewrite = true; - sizeIsFullscreen = true; + if (argsContext.getChildCount() > 3) { + sketchRenderer = argsContext.getChild(4).getText(); + } - if (argsContext.getChildCount() > 0) { - sketchRenderer = argsContext.getChild(0).getText(); - } + if (argsContext.getChildCount() > 5) { + sketchOutputFilename = argsContext.getChild(6).getText(); + } + + if (argsContext.getChildCount() > 7) { + thisRequiresRewrite = false; // Uesr may have overloaded size. + } + } + + if (isFullscreen) { + sketchWidth = "displayWidth"; + sketchWidth = "displayHeight"; + + thisRequiresRewrite = true; + sizeIsFullscreen = true; + + if (argsContext.getChildCount() > 0) { + sketchRenderer = argsContext.getChild(0).getText(); } } @@ -656,32 +654,77 @@ public class PdeParseTreeListener extends ProcessingBaseListener { } protected void handlePixelDensityCall(ParserRuleContext ctx) { - ParserRuleContext testCtx = ctx.getParent() + // Check that this is a call for processing and not a user defined method. + if (!calledFromGlobalOrSetup(ctx)) { + return; + } + + ParseTree argsContext = ctx.getChild(2); + if (argsContext.getChildCount() == 0 || argsContext.getChildCount() > 3) { + return; // User override of pixel density. + } + + pixelDensity = argsContext.getChild(0).getText(); + + delete(ctx.start, ctx.stop); + insertAfter(ctx.stop, "/* pixelDensity commented out by preprocessor */"); + pixelDensityRequiresRewrite = true; + } + + protected void handleNoSmoothCall(ParserRuleContext ctx) { + // Check that this is a call for processing and not a user defined method. + if (!calledFromGlobalOrSetup(ctx)) { + return; + } + + ParseTree argsContext = ctx.getChild(2); + if (argsContext.getChildCount() > 0) { + return; // User override of noSmooth. + } + + delete(ctx.start, ctx.stop); + insertAfter(ctx.stop, "/* noSmooth commented out by preprocessor */"); + noSmoothRequiresRewrite = true; + } + + protected void handleSmoothCall(ParserRuleContext ctx) { + // Check that this is a call for processing and not a user defined size method. + if (!calledFromGlobalOrSetup(ctx)) { + return; + } + + ParseTree argsContext = ctx.getChild(2); + if (argsContext.getChildCount() > 2) { + return; // User may have overloaded smooth; + } + + if (argsContext.getChildCount() > 0) { + smoothParam = argsContext.getChild(0).getText(); + } else { + smoothParam = ""; + } + + delete(ctx.start, ctx.stop); + insertAfter(ctx.stop, "/* smooth commented out by preprocessor */"); + smoothRequiresRewrite = true; + } + + protected boolean calledFromGlobalOrSetup(ParserRuleContext callContext) { + ParserRuleContext outerContext = callContext.getParent() .getParent() .getParent() .getParent(); - boolean isInGlobal = - testCtx instanceof ProcessingParser.StaticProcessingSketchContext; - - boolean isInSetup; - if (!isInGlobal) { - ParserRuleContext methodDeclaration = testCtx.getParent() - .getParent(); - - isInSetup = isMethodSetup(methodDeclaration); - } else { - isInSetup = false; + // Check static context first (global) + if (outerContext instanceof ProcessingParser.StaticProcessingSketchContext) { + return true; } - ParseTree argsContext = ctx.getChild(2); + // Otherwise check if method called form setup + ParserRuleContext methodDeclaration = outerContext.getParent() + .getParent(); - if (isInGlobal || isInSetup) { - pixelDensity = argsContext.getChild(0).getText(); - delete(ctx.start, ctx.stop); - insertAfter(ctx.stop, "/* pixelDensity commented out by preprocessor */"); - pixelDensityRequiresRewrite = true; - } + return isMethodSetup(methodDeclaration); } /** @@ -1010,10 +1053,16 @@ public class PdeParseTreeListener extends ProcessingBaseListener { protected void writeExtraFieldsAndMethods(PrintWriterWithEditGen classBodyWriter, RewriteResultBuilder resultBuilder) { - if (!sizeRequiresRewrite && !pixelDensityRequiresRewrite) { + // First check that a settings method is required at all + boolean noRewriteRequired = !sizeRequiresRewrite; + noRewriteRequired = noRewriteRequired && !pixelDensityRequiresRewrite; + noRewriteRequired = noRewriteRequired && !noSmoothRequiresRewrite; + noRewriteRequired = noRewriteRequired && !smoothRequiresRewrite; + if (noRewriteRequired) { return; } + // If needed, add the components via a string joiner. String settingsOuterTemplate = indent1 + "public void settings() { %s }"; StringJoiner settingsInner = new StringJoiner("\n"); @@ -1048,6 +1097,13 @@ public class PdeParseTreeListener extends ProcessingBaseListener { settingsInner.add(String.format("pixelDensity(%s);", pixelDensity)); } + if (noSmoothRequiresRewrite) { + settingsInner.add("noSmooth();"); + } + + if (smoothRequiresRewrite) { + settingsInner.add(String.format("smooth(%s);", smoothParam)); + } String newCode = String.format(settingsOuterTemplate, settingsInner.toString()); diff --git a/java/test/processing/mode/java/ParserTests.java b/java/test/processing/mode/java/ParserTests.java index cb94cfcd1..34e8add92 100644 --- a/java/test/processing/mode/java/ParserTests.java +++ b/java/test/processing/mode/java/ParserTests.java @@ -365,4 +365,24 @@ public class ParserTests { expectGood("colorreturn"); } + @Test + public void testNoSmooth() { + expectGood("nosmooth"); + } + + @Test + public void testSmooth() { + expectGood("smoothnoparam"); + } + + @Test + public void testSmoothWithParam() { + expectGood("smoothparam"); + } + + @Test + public void testSmoothWithParamStatic() { + expectGood("smoothparamstatic"); + } + } diff --git a/java/test/resources/bug315g.expected b/java/test/resources/bug315g.expected index 54de9127e..e771f41b6 100644 --- a/java/test/resources/bug315g.expected +++ b/java/test/resources/bug315g.expected @@ -16,7 +16,7 @@ public class bug315g extends PApplet { public void setup() { /* size commented out by preprocessor */; -smooth(); +/* smooth commented out by preprocessor */; int y; y = 60; int d; @@ -25,7 +25,8 @@ ellipse(75, y, d, d); noLoop(); } - public void settings() { size(480,120); } + public void settings() { size(480,120); +smooth();} static public void main(String[] passedArgs) { String[] appletArgs = new String[] { "bug315g" }; diff --git a/java/test/resources/nosmooth.expected b/java/test/resources/nosmooth.expected new file mode 100644 index 000000000..6770b50fd --- /dev/null +++ b/java/test/resources/nosmooth.expected @@ -0,0 +1,42 @@ +import processing.core.*; +import processing.data.*; +import processing.event.*; +import processing.opengl.*; + +import java.util.HashMap; +import java.util.ArrayList; +import java.io.File; +import java.io.BufferedReader; +import java.io.PrintWriter; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.IOException; + +public class nosmooth extends PApplet { + + public void setup(){ + /* size commented out by preprocessor */; + /* noSmooth commented out by preprocessor */; +} + + public void draw(){ + background(0); + fill(255,0,0); + ellipse(100,100,100,100); + fill(0,255,0); + ellipse(150,150,100,100); +} + + + public void settings() { size(300, 300, P2D); +noSmooth(); } + + static public void main(String[] passedArgs) { + String[] appletArgs = new String[] { "nosmooth" }; + if (passedArgs != null) { + PApplet.main(concat(appletArgs, passedArgs)); + } else { + PApplet.main(appletArgs); + } + } +} diff --git a/java/test/resources/nosmooth.pde b/java/test/resources/nosmooth.pde new file mode 100644 index 000000000..02f069f69 --- /dev/null +++ b/java/test/resources/nosmooth.pde @@ -0,0 +1,12 @@ +void setup(){ + size(300,300, P2D); + noSmooth(); +} + +void draw(){ + background(0); + fill(255,0,0); + ellipse(100,100,100,100); + fill(0,255,0); + ellipse(150,150,100,100); +} diff --git a/java/test/resources/smoothnoparam.expected b/java/test/resources/smoothnoparam.expected new file mode 100644 index 000000000..2d87ddbea --- /dev/null +++ b/java/test/resources/smoothnoparam.expected @@ -0,0 +1,42 @@ +import processing.core.*; +import processing.data.*; +import processing.event.*; +import processing.opengl.*; + +import java.util.HashMap; +import java.util.ArrayList; +import java.io.File; +import java.io.BufferedReader; +import java.io.PrintWriter; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.IOException; + +public class smoothnoparam extends PApplet { + + public void setup(){ + /* size commented out by preprocessor */; + /* smooth commented out by preprocessor */; +} + + public void draw(){ + background(0); + fill(255,0,0); + ellipse(100,100,100,100); + fill(0,255,0); + ellipse(150,150,100,100); +} + + + public void settings() { size(300, 300, P2D); +smooth(); } + + static public void main(String[] passedArgs) { + String[] appletArgs = new String[] { "smoothnoparam" }; + if (passedArgs != null) { + PApplet.main(concat(appletArgs, passedArgs)); + } else { + PApplet.main(appletArgs); + } + } +} diff --git a/java/test/resources/smoothnoparam.pde b/java/test/resources/smoothnoparam.pde new file mode 100644 index 000000000..0873171ae --- /dev/null +++ b/java/test/resources/smoothnoparam.pde @@ -0,0 +1,12 @@ +void setup(){ + size(300,300, P2D); + smooth(); +} + +void draw(){ + background(0); + fill(255,0,0); + ellipse(100,100,100,100); + fill(0,255,0); + ellipse(150,150,100,100); +} diff --git a/java/test/resources/smoothparam.expected b/java/test/resources/smoothparam.expected new file mode 100644 index 000000000..99543096a --- /dev/null +++ b/java/test/resources/smoothparam.expected @@ -0,0 +1,42 @@ +import processing.core.*; +import processing.data.*; +import processing.event.*; +import processing.opengl.*; + +import java.util.HashMap; +import java.util.ArrayList; +import java.io.File; +import java.io.BufferedReader; +import java.io.PrintWriter; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.IOException; + +public class smoothparam extends PApplet { + + public void setup(){ + /* size commented out by preprocessor */; + /* smooth commented out by preprocessor */; +} + + public void draw(){ + background(0); + fill(255,0,0); + ellipse(100,100,100,100); + fill(0,255,0); + ellipse(150,150,100,100); +} + + + public void settings() { size(300, 300, P2D); +smooth(4); } + + static public void main(String[] passedArgs) { + String[] appletArgs = new String[] { "smoothparam" }; + if (passedArgs != null) { + PApplet.main(concat(appletArgs, passedArgs)); + } else { + PApplet.main(appletArgs); + } + } +} diff --git a/java/test/resources/smoothparam.pde b/java/test/resources/smoothparam.pde new file mode 100644 index 000000000..8e0851e4a --- /dev/null +++ b/java/test/resources/smoothparam.pde @@ -0,0 +1,12 @@ +void setup(){ + size(300,300, P2D); + smooth(4); +} + +void draw(){ + background(0); + fill(255,0,0); + ellipse(100,100,100,100); + fill(0,255,0); + ellipse(150,150,100,100); +} diff --git a/java/test/resources/smoothparamstatic.expected b/java/test/resources/smoothparamstatic.expected new file mode 100644 index 000000000..4a8cb13b1 --- /dev/null +++ b/java/test/resources/smoothparamstatic.expected @@ -0,0 +1,40 @@ +import processing.core.*; +import processing.data.*; +import processing.event.*; +import processing.opengl.*; + +import java.util.HashMap; +import java.util.ArrayList; +import java.io.File; +import java.io.BufferedReader; +import java.io.PrintWriter; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.IOException; + +public class smoothparamstatic extends PApplet { + + public void setup() { +/* size commented out by preprocessor */; +/* smooth commented out by preprocessor */; +background(0); +fill(255,0,0); +ellipse(100,100,100,100); +fill(0,255,0); +ellipse(150,150,100,100); + + noLoop(); + } + + public void settings() { size(300, 300, P2D); +smooth(4); } + + static public void main(String[] passedArgs) { + String[] appletArgs = new String[] { "smoothparamstatic" }; + if (passedArgs != null) { + PApplet.main(concat(appletArgs, passedArgs)); + } else { + PApplet.main(appletArgs); + } + } +} diff --git a/java/test/resources/smoothparamstatic.pde b/java/test/resources/smoothparamstatic.pde new file mode 100644 index 000000000..1de819f5b --- /dev/null +++ b/java/test/resources/smoothparamstatic.pde @@ -0,0 +1,7 @@ +size(300,300, P2D); +smooth(4); +background(0); +fill(255,0,0); +ellipse(100,100,100,100); +fill(0,255,0); +ellipse(150,150,100,100);