Skip to content

Commit 615ec0e

Browse files
committed
added layout() to embed sketch into a view layout
1 parent 323d421 commit 615ec0e

File tree

3 files changed

+70
-28
lines changed

3 files changed

+70
-28
lines changed

core/src/processing/core/PApplet.java

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import android.content.res.AssetManager;
4040
import android.graphics.*;
4141
import android.net.Uri;
42+
import android.os.Build;
4243
import android.os.Bundle;
4344
import android.support.v4.content.ContextCompat;
4445
import android.view.LayoutInflater;
@@ -47,6 +48,7 @@
4748
import android.view.SurfaceView;
4849
import android.view.View;
4950
import android.view.ViewGroup;
51+
import android.view.ViewTreeObserver;
5052
import android.view.inputmethod.InputMethodManager;
5153
import processing.a2d.PGraphicsAndroid2D;
5254
import processing.android.AppComponent;
@@ -73,7 +75,8 @@ public class PApplet extends Object implements PConstants {
7375
static final public boolean DEBUG = false;
7476

7577
// Convenience public constant holding the SDK version, akin to platform in Java mode
76-
static final public int SDK = android.os.Build.VERSION.SDK_INT;
78+
static final public int SDK = Build.VERSION.SDK_INT;
79+
7780
// static final public int SDK = Build.VERSION_CODES.ICE_CREAM_SANDWICH; // Forcing older SDK for testing
7881

7982
/**
@@ -108,8 +111,8 @@ public class PApplet extends Object implements PConstants {
108111
// static final boolean THREAD_DEBUG = false;
109112

110113
/** Default width and height for applet when not specified */
111-
static public final int DEFAULT_WIDTH = 100;
112-
static public final int DEFAULT_HEIGHT = 100;
114+
static public final int DEFAULT_WIDTH = -1;
115+
static public final int DEFAULT_HEIGHT = -1;
113116

114117
/**
115118
* Minimum dimensions for the window holding an applet.
@@ -490,10 +493,18 @@ public void initSurface(LayoutInflater inflater, ViewGroup container,
490493

491494
handleSettings();
492495

493-
if (fullScreen && parentLayout == -1) {
494-
// Setting the default height and width to be fullscreen
495-
width = displayWidth;
496-
height = displayHeight;
496+
if (parentLayout == -1) {
497+
if (fullScreen || width == -1 || height == -1) {
498+
// Either sketch explicitly set to full-screen mode, or not
499+
// size/fullScreen provided, so sketch uses the entire display
500+
width = displayWidth;
501+
height = displayHeight;
502+
}
503+
} else {
504+
// Dummy weight and height to initialize the PGraphics, will be resized
505+
// when the view associated to the parent layout is created
506+
width = 100;
507+
height = 100;
497508
}
498509

499510
String rendererName = sketchRenderer();
@@ -514,8 +525,27 @@ public void initSurface(LayoutInflater inflater, ViewGroup container,
514525
setFullScreenVisibility();
515526
surface.initView(width, height);
516527
} else {
517-
surface.initView(inflater, container, savedInstanceState,
518-
fullScreen, width, height);
528+
surface.initView(inflater, container, savedInstanceState);
529+
530+
/*
531+
final View parent = surface.getRootView();
532+
parent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
533+
@SuppressWarnings("deprecation")
534+
@Override
535+
public void onGlobalLayout() {
536+
int availableWidth = parent.getMeasuredWidth();
537+
int availableHeight = parent.getMeasuredHeight();
538+
if (availableHeight > 0 && availableWidth > 0) {
539+
System.err.println(availableWidth + " " + availableHeight);
540+
if (SDK < Build.VERSION_CODES.JELLY_BEAN) {
541+
parent.getViewTreeObserver().removeGlobalOnLayoutListener(this);
542+
} else {
543+
parent.getViewTreeObserver().removeOnGlobalLayoutListener(this);
544+
}
545+
}
546+
}
547+
});
548+
*/
519549
}
520550

521551
finished = false; // just for clarity
@@ -626,7 +656,7 @@ public void onPermissionsGranted() {
626656

627657

628658
/**
629-
* @param method "size" or "fullScreen"
659+
* @param method "size", "fullScreen", or "layout"
630660
* @param args parameters passed to the function so we can show the user
631661
* @return true if safely inside the settings() method
632662
*/
@@ -1280,6 +1310,26 @@ public void size(int iwidth, int iheight, String irenderer) {
12801310
}
12811311

12821312

1313+
public void layout(int ilayout) {
1314+
if (ilayout != this.parentLayout) {
1315+
if (insideSettings("layout", ilayout)) {
1316+
this.parentLayout = ilayout;
1317+
}
1318+
}
1319+
}
1320+
1321+
1322+
public void layout(int ilayout, String irenderer) {
1323+
if (ilayout != this.parentLayout ||
1324+
!this.renderer.equals(irenderer)) {
1325+
if (insideSettings("layout", ilayout, irenderer)) {
1326+
this.parentLayout = ilayout;
1327+
this.renderer = irenderer;
1328+
}
1329+
}
1330+
}
1331+
1332+
12831333
//. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12841334

12851335

core/src/processing/core/PSurface.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ public interface PSurface {
6161

6262
public void initView(int sketchWidth, int sketchHeight);
6363
public void initView(LayoutInflater inflater, ViewGroup container,
64-
Bundle savedInstanceState,
65-
boolean sketchFullScreen,
66-
int sketchWidth, int sketchHeight);
64+
Bundle savedInstanceState);
6765

6866
public void startActivity(Intent intent);
6967

core/src/processing/core/PSurfaceNone.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,7 @@ public void initView(int sketchWidth, int sketchHeight) {
194194

195195
@Override
196196
public void initView(LayoutInflater inflater, ViewGroup container,
197-
Bundle savedInstanceState,
198-
boolean sketchFullScreen,
199-
int sketchWidth, int sketchHeight) {
197+
Bundle savedInstanceState) {
200198
// https://www.bignerdranch.com/blog/understanding-androids-layoutinflater-inflate/
201199
ViewGroup rootView = (ViewGroup)inflater.inflate(sketch.parentLayout, container, false);
202200
RelativeLayout.LayoutParams gp =
@@ -205,18 +203,14 @@ public void initView(LayoutInflater inflater, ViewGroup container,
205203
gp.addRule(RelativeLayout.CENTER_IN_PARENT);
206204

207205
View view = getSurfaceView();
208-
if (sketch.fullScreen) {
209-
LinearLayout.LayoutParams vp;
210-
vp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
211-
LayoutParams.MATCH_PARENT);
212-
vp.weight = 1.0f;
213-
view.setLayoutParams(vp);
214-
rootView.addView(view, gp);
215-
} else {
216-
LinearLayout layout = new LinearLayout(activity);
217-
layout.addView(view, sketchWidth, sketchHeight);
218-
rootView.addView(layout, gp);
219-
}
206+
LinearLayout.LayoutParams vp;
207+
vp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
208+
LayoutParams.MATCH_PARENT);
209+
vp.weight = 1.0f;
210+
vp.setMargins(0, 0, 0, 0);
211+
view.setLayoutParams(vp);
212+
view.setPadding(0,0,0,0);
213+
rootView.addView(view, gp);
220214

221215
rootView.setBackgroundColor(sketch.sketchWindowColor());
222216
setRootView(rootView);

0 commit comments

Comments
 (0)