Skip to content

Commit aae0c14

Browse files
committed
Implement size() and fullscree() methods
As done in the main repo, fullscreen() and size() are now called from the settings() method. Signed-off-by: Umair Khan <[email protected]>
1 parent 0938465 commit aae0c14

File tree

2 files changed

+129
-81
lines changed

2 files changed

+129
-81
lines changed

core/src/processing/core/PApplet.java

Lines changed: 127 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,14 @@ static public class RendererChangeException extends RuntimeException { }
417417

418418
static final String ERROR_MIN_MAX =
419419
"Cannot use min() or max() on an empty array.";
420+
421+
boolean insideSettings;
422+
423+
String renderer = JAVA2D;
424+
425+
int smooth = 1; // default smoothing (whatever that means for the renderer)
426+
427+
boolean fullScreen = false;
420428

421429

422430
//////////////////////////////////////////////////////////////
@@ -448,6 +456,10 @@ public void onCreate(Bundle savedInstanceState) {
448456
getWindowManager().getDefaultDisplay().getMetrics(dm);
449457
displayWidth = dm.widthPixels;
450458
displayHeight = dm.heightPixels;
459+
460+
//Setting the default height and width to be fullscreen
461+
width = displayWidth;
462+
height = displayHeight;
451463
// println("density is " + dm.density);
452464
// println("densityDpi is " + dm.densityDpi);
453465
if (DEBUG) println("display metrics: " + dm);
@@ -464,6 +476,8 @@ public void onCreate(Bundle savedInstanceState) {
464476
// RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
465477
//lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
466478
// layout.setGravity(RelativeLayout.CENTER_IN_PARENT);
479+
480+
handleSettings();
467481

468482
int sw = sketchWidth();
469483
int sh = sketchHeight();
@@ -638,6 +652,41 @@ protected void onPause() {
638652
//}
639653
// surfaceView.onPause();
640654
}
655+
656+
657+
/**
658+
* @param method "size" or "fullScreen"
659+
* @param args parameters passed to the function so we can show the user
660+
* @return true if safely inside the settings() method
661+
*/
662+
boolean insideSettings(String method, Object... args) {
663+
if (insideSettings) {
664+
return true;
665+
}
666+
final String url = "https://processing.org/reference/" + method + "_.html";
667+
if (!external) { // post a warning for users of Eclipse and other IDEs
668+
StringList argList = new StringList(args);
669+
System.err.println("When not using the PDE, " + method + "() can only be used inside settings().");
670+
System.err.println("Remove the " + method + "() method from setup(), and add the following:");
671+
System.err.println("public void settings() {");
672+
System.err.println(" " + method + "(" + argList.join(", ") + ");");
673+
System.err.println("}");
674+
}
675+
throw new IllegalStateException(method + "() cannot be used here, see " + url);
676+
}
677+
678+
679+
void handleSettings() {
680+
insideSettings = true;
681+
//Do stuff
682+
settings();
683+
insideSettings = false;
684+
}
685+
686+
687+
public void settings() {
688+
//It'll be empty. Will be overridden by user's sketch class.
689+
}
641690

642691

643692
/**
@@ -1040,17 +1089,23 @@ public int sketchQuality() {
10401089

10411090

10421091
public int sketchWidth() {
1043-
return displayWidth;
1092+
if (fullScreen) {
1093+
return displayWidth;
1094+
}
1095+
return width;
10441096
}
10451097

10461098

10471099
public int sketchHeight() {
1048-
return displayHeight;
1100+
if (fullScreen) {
1101+
return displayHeight;
1102+
}
1103+
return height;
10491104
}
10501105

10511106

10521107
public String sketchRenderer() {
1053-
return JAVA2D;
1108+
return renderer;
10541109
}
10551110

10561111

@@ -1465,6 +1520,54 @@ public void draw() {
14651520
// layout.invalidate();
14661521
// }
14671522
// }
1523+
1524+
1525+
/**
1526+
* Create a full-screen sketch using the default renderer.
1527+
*/
1528+
public void fullScreen() {
1529+
if (!fullScreen) {
1530+
if (insideSettings("fullScreen")) {
1531+
this.fullScreen = true;
1532+
}
1533+
}
1534+
}
1535+
1536+
1537+
public void fullScreen(int display) {
1538+
//Display index doesn't make sense in Android.
1539+
//Should we throw some error in log ?
1540+
if (!fullScreen /*|| display != this.display*/) {
1541+
if (insideSettings("fullScreen", display)) {
1542+
this.fullScreen = true;
1543+
// this.display = display;
1544+
}
1545+
}
1546+
}
1547+
1548+
1549+
public void fullScreen(String renderer) {
1550+
if (!fullScreen ||
1551+
!renderer.equals(this.renderer)) {
1552+
if (insideSettings("fullScreen", renderer)) {
1553+
this.fullScreen = true;
1554+
this.renderer = renderer;
1555+
}
1556+
}
1557+
}
1558+
1559+
1560+
public void fullScreen(String renderer, int display) {
1561+
if (!fullScreen ||
1562+
!renderer.equals(this.renderer) /*||
1563+
display != this.display*/) {
1564+
if (insideSettings("fullScreen", renderer, display)) {
1565+
this.fullScreen = true;
1566+
this.renderer = renderer;
1567+
// this.display = display;
1568+
}
1569+
}
1570+
}
14681571

14691572

14701573
/**
@@ -1477,12 +1580,24 @@ public void draw() {
14771580
* previous renderer and simply resize it.
14781581
*/
14791582
public void size(int iwidth, int iheight) {
1480-
size(iwidth, iheight, P2D, null);
1583+
if (iwidth != this.width || iheight != this.height) {
1584+
if (insideSettings("size", iwidth, iheight)) {
1585+
this.width = iwidth;
1586+
this.height = iheight;
1587+
}
1588+
}
14811589
}
14821590

14831591

14841592
public void size(int iwidth, int iheight, String irenderer) {
1485-
size(iwidth, iheight, irenderer, null);
1593+
if (iwidth != this.width || iheight != this.height ||
1594+
!this.renderer.equals(irenderer)) {
1595+
if (insideSettings("size", iwidth, iheight, irenderer)) {
1596+
this.width = iwidth;
1597+
this.height = iheight;
1598+
this.renderer = irenderer;
1599+
}
1600+
}
14861601
}
14871602

14881603

@@ -1519,82 +1634,15 @@ public void renderer(String name) {
15191634
*/
15201635
public void size(final int iwidth, final int iheight,
15211636
final String irenderer, final String ipath) {
1522-
System.out.println("This size() method is ignored on Android.");
1523-
System.out.println("See http://wiki.processing.org/w/Android for more information.");
1524-
1525-
/*
1526-
// Looper.prepare();
1527-
// Run this from the EDT, just cuz it's AWT stuff (or maybe later Swing)
1528-
// new Handler().post(new Runnable() {
1529-
handler.post(new Runnable() {
1530-
public void run() {
1531-
println("Handler is on thread " + Thread.currentThread().getName());
1532-
// // Set the preferred size so that the layout managers can handle it
1533-
//// setPreferredSize(new Dimension(iwidth, iheight));
1534-
//// setSize(iwidth, iheight);
1535-
// g.setSize(iwidth, iheight);
1536-
// }
1537-
// });
1538-
1539-
// ensure that this is an absolute path
1540-
// if (ipath != null) ipath = savePath(ipath);
1541-
// no path renderers supported yet
1542-
1543-
println("I'm the ole thread " + Thread.currentThread().getName());
1544-
String currentRenderer = g.getClass().getName();
1545-
if (currentRenderer.equals(irenderer)) {
1546-
println("resizing renderer inside size(....)");
1547-
// Avoid infinite loop of throwing exception to reset renderer
1548-
resizeRenderer(iwidth, iheight);
1549-
//redraw(); // will only be called insize draw()
1550-
1551-
} else { // renderer is being changed
1552-
println("changing renderer inside size(....)");
1553-
1554-
// otherwise ok to fall through and create renderer below
1555-
// the renderer is changing, so need to create a new object
1556-
// g = makeGraphics(iwidth, iheight, irenderer, ipath, true);
1557-
// width = iwidth;
1558-
// height = iheight;
1559-
// if ()
1560-
1561-
// Remove the old view from the layout
1562-
layout.removeView(surfaceView);
1563-
1564-
if (irenderer.equals(A2D)) {
1565-
surfaceView = new SketchSurfaceView2D(PApplet.this, iwidth, iheight);
1566-
} else if (irenderer.equals(A3D)) {
1567-
surfaceView = new SketchSurfaceView3D(PApplet.this, iwidth, iheight);
1637+
if (iwidth != this.width || iheight != this.height ||
1638+
!this.renderer.equals(irenderer)) {
1639+
if (insideSettings("size", iwidth, iheight, irenderer,
1640+
ipath)) {
1641+
this.width = iwidth;
1642+
this.height = iheight;
1643+
this.renderer = irenderer;
15681644
}
1569-
g = ((SketchSurfaceView) surfaceView).getGraphics();
1570-
1571-
// these don't seem like a good idea
1572-
// width = screenWidth;
1573-
// height = screenHeight;
1574-
1575-
// println("getting window");
1576-
// Window window = getWindow();
1577-
// window.setContentView(surfaceView); // set full screen
1578-
// layout.removeAllViews();
1579-
layout.addView(surfaceView);
1580-
1581-
// fire resize event to make sure the applet is the proper size
1582-
// setSize(iwidth, iheight);
1583-
// this is the function that will run if the user does their own
1584-
// size() command inside setup, so set defaultSize to false.
1585-
// defaultSize = false;
1586-
1587-
// throw an exception so that setup() is called again
1588-
// but with a properly sized render
1589-
// this is for opengl, which needs a valid, properly sized
1590-
// display before calling anything inside setup().
1591-
// throw new RendererChangeException();
1592-
println("interrupting animation thread");
1593-
thread.interrupt();
15941645
}
1595-
}
1596-
});
1597-
*/
15981646
}
15991647

16001648

src/processing/mode/android/AndroidPreprocessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ protected int writeImports(final PrintWriter out,
204204
return 2 + super.writeImports(out, programImports, codeFolderImports);
205205
}
206206

207-
207+
/*
208208
protected void writeFooter(PrintWriter out, String className) {
209209
if (mode == Mode.STATIC) {
210210
// close off draw() definition
@@ -233,7 +233,7 @@ protected void writeFooter(PrintWriter out, String className) {
233233
out.println("}");
234234
}
235235
}
236-
236+
*/
237237

238238
// As of revision 0215 (2.0b7-ish), the default imports are now identical
239239
// between desktop and Android (to avoid unintended incompatibilities).

0 commit comments

Comments
 (0)