Skip to content
Snippets Groups Projects
Commit 99b6c65c authored by Matyáš Latner's avatar Matyáš Latner
Browse files

#19 Saving and restoring last screen

 * save last screen
 * restore last screen after start
 * reset saved screen on Android for app shutdown from task manager
parent d604be48
Branches
Tags
No related merge requests found
...@@ -13,6 +13,6 @@ public class AndroidLauncher extends AndroidApplication { ...@@ -13,6 +13,6 @@ public class AndroidLauncher extends AndroidApplication {
protected void onCreate (Bundle savedInstanceState) { protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new Tablexia(BuildConfig.DEBUG, getResources().getConfiguration().locale, BuildConfig.VERSION_NAME, BuildConfig.APPLICATION_ID), config); initialize(new Tablexia(BuildConfig.DEBUG, getResources().getConfiguration().locale, BuildConfig.VERSION_NAME, BuildConfig.APPLICATION_ID, savedInstanceState == null), config);
} }
} }
...@@ -2,8 +2,6 @@ package cz.nic.tablexia; ...@@ -2,8 +2,6 @@ package cz.nic.tablexia;
import com.badlogic.gdx.Application; import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.reflect.ClassReflection;
import com.badlogic.gdx.utils.reflect.ReflectionException;
import net.engio.mbassy.listener.Handler; import net.engio.mbassy.listener.Handler;
...@@ -20,33 +18,45 @@ import cz.nic.tablexia.loader.zip.ZipAssetLoader; ...@@ -20,33 +18,45 @@ import cz.nic.tablexia.loader.zip.ZipAssetLoader;
import cz.nic.tablexia.menu.MainMenuContainer; import cz.nic.tablexia.menu.MainMenuContainer;
import cz.nic.tablexia.screen.AbstractTablexiaScreen; import cz.nic.tablexia.screen.AbstractTablexiaScreen;
import cz.nic.tablexia.screen.ScreenDefinition; import cz.nic.tablexia.screen.ScreenDefinition;
import cz.nic.tablexia.screen.gamemenu.GameMenuScreen;
import cz.nic.tablexia.screen.loader.LoaderScreen; import cz.nic.tablexia.screen.loader.LoaderScreen;
import cz.nic.tablexia.util.Log; import cz.nic.tablexia.util.Log;
import cz.nic.tablexia.util.Utility;
public class Tablexia extends TablexiaApplication { public class Tablexia extends TablexiaApplication {
private boolean loadingComplete = false; private boolean loadingComplete = false;
private MainMenuContainer mainMenuContainer; private MainMenuContainer mainMenuContainer;
private ZipAssetLoader zipAssetLoader; private ZipAssetLoader zipAssetLoader;
private boolean resetState;
public Tablexia(boolean debug, Locale systemLocale, String versionName, String applicationId) { public Tablexia(boolean debug, Locale systemLocale, String versionName, String applicationId, boolean resetState) {
TablexiaSettings.init(debug, systemLocale, versionName, applicationId); TablexiaSettings.init(debug, systemLocale, versionName, applicationId);
this.resetState = resetState;
} }
public Tablexia(String buildTypeKey, Locale systemLocale, String versionName, String applicationId) { public Tablexia(String buildTypeKey, Locale systemLocale, String versionName, String applicationId, boolean resetState) {
TablexiaSettings.init(buildTypeKey, systemLocale, versionName, applicationId); TablexiaSettings.init(buildTypeKey, systemLocale, versionName, applicationId);
this.resetState = resetState;
} }
private void loadingComplete() { private void loadingComplete() {
if (!loadingComplete) { if (!loadingComplete) {
loadingComplete = true; loadingComplete = true;
setScreenIfIsDifferent(new GameMenuScreen(), ScreenTransaction.MOVE_LEFT); showLastOrInitialScreen();
ApplicationBus.getInstance().publishAsync(new ApplicationLoadingCompleteEvent()); ApplicationBus.getInstance().publishAsync(new ApplicationLoadingCompleteEvent());
} }
} }
private void prepareMainMenu() { private void showLastOrInitialScreen() {
AbstractTablexiaScreen<?> lastScreen = TablexiaSettings.getInstance().getCurrentScreen();
if (lastScreen != null) {
setScreenIfIsDifferent(lastScreen, ScreenTransaction.FADE);
} else {
setScreenIfIsDifferent(Utility.getScreenForScreenClass(TablexiaSettings.INITIAL_SCREEN.getScreenClass()), ScreenTransaction.MOVE_LEFT);
}
}
private void prepareMainMenu() {
mainMenuContainer = new MainMenuContainer(getStage().getWidth(), getStage().getHeight()); mainMenuContainer = new MainMenuContainer(getStage().getWidth(), getStage().getHeight());
getStage().addActor(mainMenuContainer); getStage().addActor(mainMenuContainer);
} }
...@@ -105,7 +115,7 @@ public class Tablexia extends TablexiaApplication { ...@@ -105,7 +115,7 @@ public class Tablexia extends TablexiaApplication {
public void create () { public void create () {
super.create(); super.create();
TablexiaSettings.getInstance().loadPreferences(); TablexiaSettings.getInstance().loadPreferences(resetState);
Log.setLoglevel(TablexiaSettings.getInstance().getLogLevel()); Log.setLoglevel(TablexiaSettings.getInstance().getLogLevel());
// init event bus handlers // init event bus handlers
...@@ -194,12 +204,7 @@ public class Tablexia extends TablexiaApplication { ...@@ -194,12 +204,7 @@ public class Tablexia extends TablexiaApplication {
@Override @Override
public void run() { public void run() {
try { setScreenIfIsDifferent(Utility.getScreenForScreenClass(screenClass), changeScreenEvent.getScreenTransaction());
AbstractTablexiaScreen<?> screen = (AbstractTablexiaScreen<?>) ClassReflection.getConstructors(screenClass)[0].newInstance(new Object[]{});
setScreenIfIsDifferent(screen, changeScreenEvent.getScreenTransaction());
} catch (ReflectionException e) {
Log.err(getClass(), "Cannot change screen! (Do you have only one parameter less constructor in screen?)", e);
}
} }
}); });
} }
......
...@@ -167,8 +167,10 @@ public abstract class TablexiaApplication implements ApplicationListener { ...@@ -167,8 +167,10 @@ public abstract class TablexiaApplication implements ApplicationListener {
* @param screenTransaction screen transaction type * @param screenTransaction screen transaction type
*/ */
public void setScreenIfIsDifferent(AbstractTablexiaScreen<?> newScreen, ScreenTransaction screenTransaction) { public void setScreenIfIsDifferent(AbstractTablexiaScreen<?> newScreen, ScreenTransaction screenTransaction) {
if (getScreen() == null || getScreen().getClass() != newScreen.getClass()) { if (newScreen != null) {
setScreen(newScreen, screenTransaction); if (getScreen() == null || getScreen().getClass() != newScreen.getClass()) {
setScreen(newScreen, screenTransaction);
}
} }
} }
...@@ -223,6 +225,7 @@ public abstract class TablexiaApplication implements ApplicationListener { ...@@ -223,6 +225,7 @@ public abstract class TablexiaApplication implements ApplicationListener {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private void processNewScreen(AbstractTablexiaScreen<?> newScreen) { private void processNewScreen(AbstractTablexiaScreen<?> newScreen) {
if (newScreen != null) { if (newScreen != null) {
TablexiaSettings.getInstance().setCurrentScreen(newScreen);
inputMultiplexer.addProcessor(newScreen.getInputProcessor()); inputMultiplexer.addProcessor(newScreen.getInputProcessor());
newScreen.show(); newScreen.show();
newScreen.resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); newScreen.resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
......
...@@ -2,21 +2,30 @@ package cz.nic.tablexia; ...@@ -2,21 +2,30 @@ package cz.nic.tablexia;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences; import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.utils.reflect.ClassReflection;
import com.badlogic.gdx.utils.reflect.ReflectionException;
import java.util.Locale; import java.util.Locale;
import cz.nic.tablexia.bus.ApplicationBus; import cz.nic.tablexia.bus.ApplicationBus;
import cz.nic.tablexia.bus.event.LocaleChangedEvent; import cz.nic.tablexia.bus.event.LocaleChangedEvent;
import cz.nic.tablexia.loader.application.ApplicationTextManager; import cz.nic.tablexia.loader.application.ApplicationTextManager;
import cz.nic.tablexia.screen.AbstractTablexiaScreen;
import cz.nic.tablexia.screen.ScreenDefinition;
import cz.nic.tablexia.util.Log; import cz.nic.tablexia.util.Log;
import cz.nic.tablexia.util.Utility;
public class TablexiaSettings { public class TablexiaSettings {
private static final int DEFAULT_SCREEN_WIDTH = 1000; private static final int DEFAULT_SCREEN_WIDTH = 1000;
private static final double MAXIMUM_RATIO = 9.0 / 16.0; private static final double MAXIMUM_RATIO = 9.0 / 16.0;
private static final int MIN_SCREEN_HEIGHT = (int) (DEFAULT_SCREEN_WIDTH * MAXIMUM_RATIO); private static final int MIN_SCREEN_HEIGHT = (int) (DEFAULT_SCREEN_WIDTH * MAXIMUM_RATIO);
private static final boolean DEBUG_SHOW_BOUNDING_BOXES = true; private static final boolean DEBUG_SHOW_BOUNDING_BOXES = true;
public static final ScreenDefinition INITIAL_SCREEN = ScreenDefinition.GAME_MENU;
public static final String LOCALE_KEY = "locale"; public static final String LOCALE_KEY = "locale";
public static final String CURRENT_SCREEN_KEY = "current_screen";
private static final String IDE_BUILD_APPLICATION_ID = "cz.nic.tablexia.debug"; private static final String IDE_BUILD_APPLICATION_ID = "cz.nic.tablexia.debug";
private static final String IDE_BUILD_VERSION_NAME = "IDE-BUILD"; private static final String IDE_BUILD_VERSION_NAME = "IDE-BUILD";
...@@ -162,9 +171,13 @@ public class TablexiaSettings { ...@@ -162,9 +171,13 @@ public class TablexiaSettings {
//////////////////////////// LIBGDX PREFERENCES //////////////////////////// LIBGDX PREFERENCES
public void loadPreferences() { public void loadPreferences(boolean resetState) {
preferences = Gdx.app.getPreferences(getApplicationId()); preferences = Gdx.app.getPreferences(getApplicationId());
selectedLocale = LocaleDefinition.getLocaleDefinitionForKey(preferences.getString(LOCALE_KEY)); selectedLocale = LocaleDefinition.getLocaleDefinitionForKey(preferences.getString(LOCALE_KEY));
if (resetState) {
preferences.remove(CURRENT_SCREEN_KEY);
preferences.flush();
}
} }
...@@ -208,6 +221,25 @@ public class TablexiaSettings { ...@@ -208,6 +221,25 @@ public class TablexiaSettings {
return selectedLocale.getLocale(); return selectedLocale.getLocale();
} }
public void setCurrentScreen(AbstractTablexiaScreen screen) {
if (screen.hasState()) {
preferences.putString(CURRENT_SCREEN_KEY, screen.getClass().getName());
preferences.flush();
}
}
public AbstractTablexiaScreen<?> getCurrentScreen() {
String screenClassName = preferences.getString(CURRENT_SCREEN_KEY, null);
if (screenClassName != null) {
try {
return Utility.getScreenForScreenClass(ClassReflection.forName(screenClassName));
} catch (ReflectionException e) {
Log.err(getClass(), "Cannot load screen class: " + screenClassName, e);
}
}
return null;
}
//////////////////////////// SCREEN SIZE //////////////////////////// SCREEN SIZE
......
...@@ -44,6 +44,7 @@ public abstract class AbstractTablexiaScreen<T> extends ScreenAdapter { ...@@ -44,6 +44,7 @@ public abstract class AbstractTablexiaScreen<T> extends ScreenAdapter {
private boolean loadingStarted; private boolean loadingStarted;
private boolean loadAsync; private boolean loadAsync;
private boolean paused; private boolean paused;
private boolean hasState;
private static class TextManager extends TablexiaDataManager<I18NBundle> { private static class TextManager extends TablexiaDataManager<I18NBundle> {
...@@ -111,21 +112,25 @@ public abstract class AbstractTablexiaScreen<T> extends ScreenAdapter { ...@@ -111,21 +112,25 @@ public abstract class AbstractTablexiaScreen<T> extends ScreenAdapter {
public AbstractTablexiaScreen() { public AbstractTablexiaScreen() {
this(true, TablexiaAssetManager.StorageType.EXTERNAL); this(true, true, TablexiaAssetManager.StorageType.EXTERNAL);
} }
public AbstractTablexiaScreen(boolean loadAsync, TablexiaAssetManager.StorageType storageType) { public AbstractTablexiaScreen(boolean hasState, boolean loadAsync, TablexiaAssetManager.StorageType storageType) {
stage = prepareStage(); stage = prepareStage();
this.hasState = hasState;
this.loadAsync = loadAsync; this.loadAsync = loadAsync;
textureManager = new TablexiaTextureManager(storageType); textureManager = new TablexiaTextureManager(storageType);
soundManager = new TablexiaSoundManager(storageType); soundManager = new TablexiaSoundManager(storageType);
textManager = new TextManager(); textManager = new TextManager();
dataManager = new DataManager(); dataManager = new DataManager();
loadingComplete = false; loadingComplete = false;
loadingStarted = false; loadingStarted = false;
} }
public boolean hasState() {
return hasState;
}
//////////////////////////// LOADING LISTENER //////////////////////////// LOADING LISTENER
......
...@@ -29,8 +29,12 @@ public enum ScreenDefinition { ...@@ -29,8 +29,12 @@ public enum ScreenDefinition {
this.screenClass = screenClass; this.screenClass = screenClass;
this.mainMenuDefinition = mainMenuDefinition; this.mainMenuDefinition = mainMenuDefinition;
} }
public MainMenuDefinition getMainMenuDefinition() { public Class<? extends AbstractTablexiaScreen<?>> getScreenClass() {
return screenClass;
}
public MainMenuDefinition getMainMenuDefinition() {
return mainMenuDefinition; return mainMenuDefinition;
} }
......
...@@ -35,7 +35,7 @@ public class LoaderScreen extends AbstractTablexiaScreen<Void> { ...@@ -35,7 +35,7 @@ public class LoaderScreen extends AbstractTablexiaScreen<Void> {
} }
public LoaderScreen() { public LoaderScreen() {
super(false, TablexiaAssetManager.StorageType.INTERNAL); super(false, false, TablexiaAssetManager.StorageType.INTERNAL);
} }
@Override @Override
......
package cz.nic.tablexia.util;
import com.badlogic.gdx.utils.reflect.ClassReflection;
import com.badlogic.gdx.utils.reflect.ReflectionException;
import cz.nic.tablexia.screen.AbstractTablexiaScreen;
public class Utility {
public static AbstractTablexiaScreen<?> getScreenForScreenClass(Class<? extends AbstractTablexiaScreen<?>> screenClass) {
try {
return (AbstractTablexiaScreen<?>) ClassReflection.getConstructors(screenClass)[0].newInstance(new Object[]{});
} catch (ReflectionException e) {
Log.err(Utility.class, "Cannot instantiate screen class: " + screenClass + " ! (Do you have only one parameter less constructor in screen class?)", e);
}
return null;
}
}
...@@ -44,7 +44,7 @@ public class DesktopLauncher { ...@@ -44,7 +44,7 @@ public class DesktopLauncher {
config.addIcon(DESKTOP_ICON_128, Files.FileType.Internal); config.addIcon(DESKTOP_ICON_128, Files.FileType.Internal);
} }
new LwjglApplication(new Tablexia(buildType, Locale.getDefault(), versionName, applicationId), config); new LwjglApplication(new Tablexia(buildType, Locale.getDefault(), versionName, applicationId, false), config);
} }
private static String loadAttributeFromManifest(String attributeName) { private static String loadAttributeFromManifest(String attributeName) {
......
...@@ -14,7 +14,7 @@ public class IOSLauncher extends IOSApplication.Delegate { ...@@ -14,7 +14,7 @@ public class IOSLauncher extends IOSApplication.Delegate {
@Override @Override
protected IOSApplication createApplication() { protected IOSApplication createApplication() {
IOSApplicationConfiguration config = new IOSApplicationConfiguration(); IOSApplicationConfiguration config = new IOSApplicationConfiguration();
return new IOSApplication(new Tablexia(null, Locale.getDefault(), null, null), config); return new IOSApplication(new Tablexia(null, Locale.getDefault(), null, null, false), config);
} }
public static void main(String[] argv) { public static void main(String[] argv) {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment