From d4afe8697f8ed4f9d1ab6e3d626818172705a407 Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Tue, 23 Apr 2019 15:27:13 -0400 Subject: [PATCH] v0.7.2c: added a simple window restoring function to scene resets --- .../ShatteredPixelDungeon.java | 23 ++++++++++++++- .../scenes/PixelScene.java | 29 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ShatteredPixelDungeon.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ShatteredPixelDungeon.java index 94b8280a3..e42f8d706 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ShatteredPixelDungeon.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/ShatteredPixelDungeon.java @@ -215,6 +215,14 @@ public class ShatteredPixelDungeon extends Game { } + @Override + protected void onSaveInstanceState(Bundle outState) { + if (scene instanceof PixelScene){ + ((PixelScene) scene).saveWindows(); + } + super.onSaveInstanceState(outState); + } + @Override public void onWindowFocusChanged( boolean hasFocus ) { super.onWindowFocusChanged( hasFocus ); @@ -236,9 +244,22 @@ public class ShatteredPixelDungeon extends Game { PixelScene.noFade = true; switchScene( c, callback ); } - + + @Override + protected void switchScene() { + super.switchScene(); + if (scene instanceof PixelScene){ + ((PixelScene) scene).restoreWindows(); + } + } + @Override public void onSurfaceChanged( GL10 gl, int width, int height ) { + + if (scene instanceof PixelScene && + (height != Game.height || width != Game.width)) { + ((PixelScene) scene).saveWindows(); + } super.onSurfaceChanged( gl, width, height ); diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/PixelScene.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/PixelScene.java index 40b4bd652..ad8da616c 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/PixelScene.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/PixelScene.java @@ -26,6 +26,7 @@ import com.shatteredpixel.shatteredpixeldungeon.Badges; import com.shatteredpixel.shatteredpixeldungeon.SPDSettings; import com.shatteredpixel.shatteredpixeldungeon.effects.BadgeBanner; import com.shatteredpixel.shatteredpixeldungeon.ui.RenderedTextMultiline; +import com.shatteredpixel.shatteredpixeldungeon.ui.Window; import com.watabou.glwrap.Blending; import com.watabou.input.Touchscreen; import com.watabou.noosa.BitmapText; @@ -34,12 +35,15 @@ import com.watabou.noosa.BitmapTextMultiline; import com.watabou.noosa.Camera; import com.watabou.noosa.ColorBlock; import com.watabou.noosa.Game; +import com.watabou.noosa.Gizmo; import com.watabou.noosa.RenderedText; import com.watabou.noosa.Scene; import com.watabou.noosa.Visual; import com.watabou.noosa.ui.Component; import com.watabou.utils.BitmapCache; +import java.util.ArrayList; + public class PixelScene extends Scene { // Minimum virtual display size for portrait orientation @@ -129,6 +133,31 @@ public class PixelScene extends Scene { font2x.texture.filter(Texture.LINEAR, Texture.NEAREST);*/ } } + + //FIXME this system currently only works for a subset of windows + private static ArrayList> savedWindows = new ArrayList<>(); + + public void saveWindows(){ + savedWindows.clear(); + for (Gizmo g : members){ + if (g instanceof Window){ + savedWindows.add((Class) g.getClass()); + } + } + } + + public void restoreWindows(){ + if (!savedWindows.isEmpty()){ + for (Class w : savedWindows){ + try{ + add(w.newInstance()); + } catch (Exception e){ + //window has no public zero-arg constructor, just eat the exception + } + } + savedWindows.clear(); + } + } @Override public void destroy() {