mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
Merge pull request #152 from processing/smooth_preproc_fix
This commit is contained in:
@@ -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());
|
||||
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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" };
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
Reference in New Issue
Block a user