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}};
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewGroup;
@ -305,11 +307,16 @@ public class EbitenView extends ViewGroup {
Ebitenmobileview.layout(widthInDp, heightInDp, new ViewRectSetter() {
@Override
public void setViewRect(long xInDp, long yInDp, long widthInDp, long heightInDp) {
int widthInPx = (int)Math.ceil(dpToPx(widthInDp));
int heightInPx = (int)Math.ceil(dpToPx(heightInDp));
int xInPx = (int)Math.ceil(dpToPx(xInDp));
int yInPx = (int)Math.ceil(dpToPx(yInDp));
ebitenSurfaceView_.layout(xInPx, yInPx, xInPx + widthInPx, yInPx + heightInPx);
final int widthInPx = (int)Math.ceil(dpToPx(widthInDp));
final int heightInPx = (int)Math.ceil(dpToPx(heightInDp));
final int xInPx = (int)Math.ceil(dpToPx(xInDp));
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);
}
});
}
});
}

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)
theState.running = true
} else {
// TODO: Change the screen size
setScreenSize(w, h, scale)
}
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 setScreenSize(width, height int, scale float64) {
}
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.
chError = ebiten.RunWithoutMainLoop(f, width, height, scale, "")
}
func setScreenSize(width, height int, scale float64) {
ebiten.SetScreenSize(width, height)
ebiten.SetScreenScale(scale)
}