mobile/ebitenmobile: Implement setScreenSize

This change also fixes the bug that Android froze when the view
size is changed.

Fixes #934
This commit is contained in:
Hajime Hoshi 2019-09-14 12:32:02 +09:00
parent 7547207e2d
commit c648b40e20
5 changed files with 22 additions and 7 deletions

View File

@ -257,6 +257,8 @@ const viewJava = `// Code generated by ebitenmobile. DO NOT EDIT.
package {{.JavaPkg}}.{{.PrefixLower}}; package {{.JavaPkg}}.{{.PrefixLower}};
import android.content.Context; import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log; import android.util.Log;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -305,14 +307,19 @@ public class EbitenView extends ViewGroup {
Ebitenmobileview.layout(widthInDp, heightInDp, new ViewRectSetter() { Ebitenmobileview.layout(widthInDp, heightInDp, new ViewRectSetter() {
@Override @Override
public void setViewRect(long xInDp, long yInDp, long widthInDp, long heightInDp) { public void setViewRect(long xInDp, long yInDp, long widthInDp, long heightInDp) {
int widthInPx = (int)Math.ceil(dpToPx(widthInDp)); final int widthInPx = (int)Math.ceil(dpToPx(widthInDp));
int heightInPx = (int)Math.ceil(dpToPx(heightInDp)); final int heightInPx = (int)Math.ceil(dpToPx(heightInDp));
int xInPx = (int)Math.ceil(dpToPx(xInDp)); final int xInPx = (int)Math.ceil(dpToPx(xInDp));
int yInPx = (int)Math.ceil(dpToPx(yInDp)); final int yInPx = (int)Math.ceil(dpToPx(yInDp));
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
ebitenSurfaceView_.layout(xInPx, yInPx, xInPx + widthInPx, yInPx + heightInPx); ebitenSurfaceView_.layout(xInPx, yInPx, xInPx + widthInPx, yInPx + heightInPx);
} }
}); });
} }
});
}
public void onPause() { public void onPause() {
if (initialized_) { if (initialized_) {

File diff suppressed because one or more lines are too long

View File

@ -49,7 +49,7 @@ func Layout(viewWidth, viewHeight int, viewRectSetter ViewRectSetter) {
start(theState.game.Update, w, h, scale) start(theState.game.Update, w, h, scale)
theState.running = true theState.running = true
} else { } else {
// TODO: Change the screen size setScreenSize(w, h, scale)
} }
if viewRectSetter != nil { if viewRectSetter != nil {

View File

@ -32,6 +32,9 @@ func update() error {
func start(f func(*ebiten.Image) error, width, height int, scale float64) { func start(f func(*ebiten.Image) error, width, height int, scale float64) {
} }
func setScreenSize(width, height int, scale float64) {
}
func updateTouchesOnAndroid(action int, id int, x, y int) { func updateTouchesOnAndroid(action int, id int, x, y int) {
} }

View File

@ -51,3 +51,8 @@ func start(f func(*ebiten.Image) error, width, height int, scale float64) {
// The last argument 'title' is not used on mobile platforms, so just pass an empty string. // The last argument 'title' is not used on mobile platforms, so just pass an empty string.
chError = ebiten.RunWithoutMainLoop(f, width, height, scale, "") chError = ebiten.RunWithoutMainLoop(f, width, height, scale, "")
} }
func setScreenSize(width, height int, scale float64) {
ebiten.SetScreenSize(width, height)
ebiten.SetScreenScale(scale)
}