This commit is contained in:
drdev
2015-09-26 18:41:34 +00:00
parent 28316c0fe9
commit 8b2108f325
6 changed files with 59 additions and 16 deletions

View File

@@ -98,6 +98,17 @@ public final class FileUtil {
return dir.delete();
}
public static boolean deleteFile(String filename) {
try {
File file = new File(filename);
return file.delete();
}
catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static void copyFile(String sourceFilename, String destFilename) {
File source = new File(sourceFilename);
if (!source.exists()) { return; } //if source doesn't exist, nothing to copy

View File

@@ -31,9 +31,6 @@ public class Main extends AndroidApplication {
AndroidAdapter adapter = new AndroidAdapter(this.getContext());
//enforce orientation based on whether device is a tablet
adapter.setLandscapeMode(adapter.isTablet);
//establish assets directory
if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
Gdx.app.error("Forge", "Can't access external storage");
@@ -47,6 +44,16 @@ public class Main extends AndroidApplication {
return;
}
//enforce orientation based on whether device is a tablet and user preference
adapter.switchOrientationFile = assetsDir + "switch_orientation.ini";
boolean landscapeMode = adapter.isTablet == !FileUtil.doesFileExist(adapter.switchOrientationFile);
if (landscapeMode) {
Main.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else {
Main.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
initialize(Forge.getApp(new AndroidClipboard(), adapter, assetsDir));
}
@@ -87,6 +94,7 @@ public class Main extends AndroidApplication {
private class AndroidAdapter implements IDeviceAdapter {
private final boolean isTablet;
private final ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
private String switchOrientationFile;
private AndroidAdapter(Context context) {
isTablet = (context.getResources().getConfiguration().screenLayout
@@ -167,11 +175,12 @@ public class Main extends AndroidApplication {
@Override
public void setLandscapeMode(boolean landscapeMode) {
if (landscapeMode) {
Main.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//create file to indicate that portrait mode should be used for tablet or landscape should be used for phone
if (landscapeMode != isTablet) {
FileUtil.writeFile(switchOrientationFile, "1");
}
else {
Main.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
FileUtil.deleteFile(switchOrientationFile);
}
}
}

View File

@@ -3,6 +3,7 @@ package forge.app;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglClipboard;
@@ -21,11 +22,22 @@ public class Main {
FileUtil.ensureDirectoryExists(assetsDir);
}
new LwjglApplication(Forge.getApp(new LwjglClipboard(), new DesktopAdapter(),
assetsDir), "Forge", Utils.DEV_SCREEN_WIDTH, Utils.DEV_SCREEN_HEIGHT);
String switchOrientationFile = assetsDir + "switch_orientation.ini";
boolean landscapeMode = FileUtil.doesFileExist(switchOrientationFile);
int screenWidth = landscapeMode ? (int)(Utils.BASE_HEIGHT * 16 / 9) : (int)Utils.BASE_WIDTH;
int screenHeight = (int)Utils.BASE_HEIGHT;
new LwjglApplication(Forge.getApp(new LwjglClipboard(), new DesktopAdapter(switchOrientationFile),
assetsDir), "Forge", screenWidth, screenHeight);
}
private static class DesktopAdapter implements IDeviceAdapter {
private final String switchOrientationFile;
private DesktopAdapter(String switchOrientationFile0) {
switchOrientationFile = switchOrientationFile0;
}
//just assume desktop always connected to wifi
@Override
public boolean isConnectedToInternet() {
@@ -73,7 +85,13 @@ public class Main {
@Override
public void setLandscapeMode(boolean landscapeMode) {
//TODO: Consider supporting toggling this on desktop for testing
//create file to indicate that landscape mode should be used
if (landscapeMode) {
FileUtil.writeFile(switchOrientationFile, "1");
}
else {
FileUtil.deleteFile(switchOrientationFile);
}
}
}
}

View File

@@ -121,9 +121,16 @@ public class Forge implements ApplicationListener {
openScreen(HomeScreen.instance);
splashScreen = null;
if (isLandscapeMode()) { //open preferred new game screen by default if landscape mode
boolean isLandscapeMode = isLandscapeMode();
if (isLandscapeMode) { //open preferred new game screen by default if landscape mode
NewGameMenu.getPreferredScreen().open();
}
//update landscape mode preference if it doesn't match what the app loaded as
if (FModel.getPreferences().getPrefBoolean(FPref.UI_LANDSCAPE_MODE) != isLandscapeMode) {
FModel.getPreferences().setPref(FPref.UI_LANDSCAPE_MODE, isLandscapeMode);
FModel.getPreferences().save();
}
}
public static Clipboard getClipboard() {

View File

@@ -62,7 +62,9 @@ public class SettingsPage extends TabPage<SettingsScreen> {
@Override
public void select() {
super.select();
if (Forge.isLandscapeMode() != FModel.getPreferences().getPrefBoolean(FPref.UI_LANDSCAPE_MODE)) {
boolean landscapeMode = FModel.getPreferences().getPrefBoolean(FPref.UI_LANDSCAPE_MODE);
Forge.getDeviceAdapter().setLandscapeMode(landscapeMode); //ensure device able to save off ini file so landscape change takes effect
if (Forge.isLandscapeMode() != landscapeMode) {
FOptionPane.showConfirmDialog("You must restart Forge for this change to take effect.", "Restart Forge", "Restart", "Later", new Callback<Boolean>() {
@Override
public void run(Boolean result) {

View File

@@ -15,15 +15,11 @@ public class Utils {
private static final float AVG_FINGER_SIZE_CM = 1.1f;
//Swap commented out line below to specify average finger size and dev screen size
//Swap commented out line below to specify average finger size
private static final float ppcX = Gdx.graphics.getPpcX(), ppcY = Gdx.graphics.getPpcY();
public static final int DEV_SCREEN_WIDTH = DEV_SCREEN_LANDSCAPE ? (int)(BASE_HEIGHT * 16 / 9) : (int)BASE_WIDTH;
public static final int DEV_SCREEN_HEIGHT = (int)BASE_HEIGHT;
//private static final float ppcX = 169f / AVG_FINGER_SIZE_CM, ppcY = 237f / AVG_FINGER_SIZE_CM;
//public static final int DEV_SCREEN_WIDTH = 400, DEV_SCREEN_HEIGHT = 600;
//private static final float scaleX = 1.41f, scaleY = 1.25f;
//private static final float ppcX = Gdx.graphics.getPpcX() * scaleX, ppcY = Gdx.graphics.getPpcY() * scaleY;
//public static final int DEV_SCREEN_WIDTH = (int)(BASE_WIDTH * scaleX), DEV_SCREEN_HEIGHT = (int)(BASE_HEIGHT * scaleY);
//round to nearest int to reduce floating point display issues
//reduce if either would take up too large a percentage of the screen to prevent layouts not working