Detect if calling special methods on the PApplet or not.

This commit is contained in:
A Pottinger
2021-10-10 11:52:40 -07:00
parent 5d56e43a7d
commit 8e9c864031
7 changed files with 191 additions and 10 deletions
@@ -326,7 +326,22 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
String methodName = ctx.getChild(0).getText();
// Check if calling on something other than this.
//System.out.println(ctx.getParent());
boolean impliedThis = ctx.getParent().getChildCount() == 1;
boolean usesThis;
if (impliedThis) {
usesThis = true;
} else {
String statmentTarget = ctx.getParent().getChild(0).getText();
boolean explicitThis = statmentTarget.equals("this");
boolean explicitSuper = statmentTarget.equals("super");
usesThis = explicitThis || explicitSuper;
}
// If not using this or super, no rewrite as the user is calling their own
// declaration or instance of PGraphics.
if (!usesThis) {
return;
}
// If referring to the applet, check for rewrites.
if (SIZE_METHOD_NAME.equals(methodName) || FULLSCREEN_METHOD_NAME.equals(methodName)) {
@@ -663,7 +678,7 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
}
if (thisRequiresRewrite) {
delete(ctx.start, ctx.stop);
delete(ctx.getParent().start, ctx.getParent().stop);
insertAfter(ctx.stop, "/* size commented out by preprocessor */");
sizeRequiresRewrite = true;
}
@@ -682,8 +697,8 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
pixelDensity = argsContext.getChild(0).getText();
delete(ctx.start, ctx.stop);
insertAfter(ctx.stop, "/* pixelDensity commented out by preprocessor */");
delete(ctx.getParent().start, ctx.getParent().stop);
insertAfter(ctx.getParent().stop, "/* pixelDensity commented out by preprocessor */");
pixelDensityRequiresRewrite = true;
}
@@ -698,8 +713,11 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
return; // User override of noSmooth.
}
delete(ctx.start, ctx.stop);
insertAfter(ctx.stop, "/* noSmooth commented out by preprocessor */");
delete(ctx.getParent().start, ctx.getParent().stop);
insertAfter(
ctx.getParent().stop,
"/* noSmooth commented out by preprocessor */"
);
noSmoothRequiresRewrite = true;
}
@@ -720,8 +738,11 @@ public class PdeParseTreeListener extends ProcessingBaseListener {
smoothParam = "";
}
delete(ctx.start, ctx.stop);
insertAfter(ctx.stop, "/* smooth commented out by preprocessor */");
delete(ctx.getParent().start, ctx.getParent().stop);
insertAfter(
ctx.getParent().stop,
"/* smooth commented out by preprocessor */"
);
smoothRequiresRewrite = true;
}
@@ -375,6 +375,11 @@ public class ParserTests {
expectGood("smoothnoparam");
}
@Test
public void testSmoothThis() {
expectGood("smoothnoparamthis");
}
@Test
public void testSmoothWithParam() {
expectGood("smoothparam");
@@ -390,9 +395,14 @@ public class ParserTests {
expectGood("colorimport");
}
/*@Test
@Test
public void testPGraphicsStandalone() {
expectGood("pgraphics");
}*/
}
@Test
public void testSizeThis() {
expectGood("sizethis");
}
}
+37
View File
@@ -0,0 +1,37 @@
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 pgraphics extends PApplet {
PGraphics gfx;
public void setup() {
gfx = createGraphics(width, height);
gfx.smooth();
}
public void draw() {
}
static public void main(String[] passedArgs) {
String[] appletArgs = new String[] { "pgraphics" };
if (passedArgs != null) {
PApplet.main(concat(appletArgs, passedArgs));
} else {
PApplet.main(appletArgs);
}
}
}
+45
View File
@@ -0,0 +1,45 @@
import processing.core.*;
import processing.data.*;
import processing.event.*;
import processing.opengl.*;
import processing.pdf.*;
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 sizethis extends PApplet {
public void setup() {
/* size commented out by preprocessor */;
}
public void draw() {
// Draw something good here
line(0, 0, width/2, height);
// Exit the program
println("Finished.");
exit();
}
public void settings() { size(400,400,PDF,"filename.pdf"); }
static public void main(String[] passedArgs) {
String[] appletArgs = new String[] { "sizethis" };
if (passedArgs != null) {
PApplet.main(concat(appletArgs, passedArgs));
} else {
PApplet.main(appletArgs);
}
}
}
+14
View File
@@ -0,0 +1,14 @@
import processing.pdf.*;
void setup() {
this.size(400, 400, PDF, "filename.pdf");
}
void draw() {
// Draw something good here
line(0, 0, width/2, height);
// Exit the program
println("Finished.");
exit();
}
@@ -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 smoothnoparamthis 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[] { "smoothnoparamthis" };
if (passedArgs != null) {
PApplet.main(concat(appletArgs, passedArgs));
} else {
PApplet.main(appletArgs);
}
}
}
+12
View File
@@ -0,0 +1,12 @@
void setup(){
size(300,300, P2D);
this.smooth();
}
void draw(){
background(0);
fill(255,0,0);
ellipse(100,100,100,100);
fill(0,255,0);
ellipse(150,150,100,100);
}