diff --git a/core/src/main/java/com/agifans/agile/Agile.java b/core/src/main/java/com/agifans/agile/Agile.java index ab716b8..475e123 100644 --- a/core/src/main/java/com/agifans/agile/Agile.java +++ b/core/src/main/java/com/agifans/agile/Agile.java @@ -1,5 +1,7 @@ package com.agifans.agile; +import java.util.Map; + import com.agifans.agile.config.AppConfigItem; import com.agifans.agile.ui.DialogHandler; import com.badlogic.gdx.Game; @@ -28,9 +30,9 @@ public class Agile extends Game { private DialogHandler dialogHandler; /** - * Command line args. Mainly applicable to desktop. + * For desktop, contains command line args. For HTML5, the hash and/or query parameters. */ - private String[] args; + private Map args; /** * AGILE's saved preferences. @@ -44,7 +46,7 @@ public class Agile extends Game { * @param dialogHandler * @param args */ - public Agile(AgileRunner agileRunner, DialogHandler dialogHandler, String... args) { + public Agile(AgileRunner agileRunner, DialogHandler dialogHandler, Map args) { this.agileRunner = agileRunner; this.dialogHandler = dialogHandler; this.args = args; @@ -56,15 +58,30 @@ public void create() { homeScreen = new HomeScreen(this, dialogHandler); gameScreen = new GameScreen(this, agileRunner, dialogHandler); - if ((args != null) && (args.length > 0)) { - AppConfigItem appConfigItem = new AppConfigItem(); - appConfigItem.setFilePath(args[0]); - if ((args[0].toLowerCase().endsWith(".zip"))) { - appConfigItem.setFileType("ZIP"); + AppConfigItem appConfigItem = null; + + if ((args != null) && (args.size() > 0)) { + if (args.containsKey("id")) { + appConfigItem = homeScreen.getAppConfigItemByGameId(args.get("id")); + } + if (args.containsKey("uri")) { + appConfigItem = homeScreen.getAppConfigItemByGameUri(args.get("uri")); } - GameScreen machineScreen = getGameScreen(); - machineScreen.initGame(appConfigItem); - setScreen(machineScreen); + if (args.containsKey("path")) { + String filePath = args.get("path"); + appConfigItem = new AppConfigItem(); + appConfigItem.setFilePath(filePath); + if ((filePath.toLowerCase().endsWith(".zip"))) { + appConfigItem.setFileType("ZIP"); + } else { + appConfigItem.setFileType("DIR"); + } + } + } + + if (appConfigItem != null) { + gameScreen.initGame(appConfigItem, false); + setScreen(gameScreen); } else { setScreen(homeScreen); } diff --git a/core/src/main/java/com/agifans/agile/GameScreen.java b/core/src/main/java/com/agifans/agile/GameScreen.java index 726317c..2b89c14 100644 --- a/core/src/main/java/com/agifans/agile/GameScreen.java +++ b/core/src/main/java/com/agifans/agile/GameScreen.java @@ -83,6 +83,11 @@ public class GameScreen implements Screen { */ private AppConfigItem appConfigItem; + /** + * Whether or not the game was started by a user interaction. + */ + private boolean startedByUser; + /** * Constructor for GameScreen. * @@ -187,7 +192,7 @@ public void show() { if (agileRunner.hasTouchScreen()) { gameScreenInputProcessor.setJoystickAlignment(JoystickAlignment.RIGHT); - if (!Gdx.graphics.isFullscreen()) { + if (!Gdx.graphics.isFullscreen() && startedByUser) { gameScreenInputProcessor.switchIntoFullScreen(); } } @@ -548,9 +553,11 @@ public void hide() { * that was selected on the HomeScreen. * * @param appConfigItem The configuration for the app that was selected on the HomeScreen. + * @param startedByUser true if the game is being started by a user interaction; otherwise false. */ - public void initGame(AppConfigItem appConfigItem) { + public void initGame(AppConfigItem appConfigItem, boolean startedByUser) { this.appConfigItem = appConfigItem; + this.startedByUser = startedByUser; } /** diff --git a/core/src/main/java/com/agifans/agile/HomeScreen.java b/core/src/main/java/com/agifans/agile/HomeScreen.java index 1cb8f1c..1cb9afe 100644 --- a/core/src/main/java/com/agifans/agile/HomeScreen.java +++ b/core/src/main/java/com/agifans/agile/HomeScreen.java @@ -575,6 +575,40 @@ public Button buildAppButton(AppConfigItem appConfigItem) { return button; } + /** + * If there is a game in the AppConfigItem Map that has the given game ID, then + * that is returned; otherwise returns false. + * + * @param gameId The ID of the game to get the AppConfigItem for, if it exists. + * + * @return + */ + public AppConfigItem getAppConfigItemByGameId(String gameId) { + for (AppConfigItem appConfigItem : appConfigMap.values()) { + if (appConfigItem.getGameId().equalsIgnoreCase(gameId)) { + return appConfigItem; + } + } + return null; + } + + /** + * If there is a game in the AppConfigItem Map that has the given URI, then + * that is returned; otherwise returns false. + * + * @param gameId The URI of the game to get the AppConfigItem for, if it exists. + * + * @return + */ + public AppConfigItem getAppConfigItemByGameUri(String gameUri) { + for (AppConfigItem appConfigItem : appConfigMap.values()) { + if (appConfigItem.getFilePath().equalsIgnoreCase(gameUri)) { + return appConfigItem; + } + } + return null; + } + /** * Converts the given Map of AppConfigItems to an AppConfig instance. * @@ -687,7 +721,7 @@ public void clicked(InputEvent event, float x, float y) { importGame(appConfigItem); } else { GameScreen gameScreen = agile.getGameScreen(); - gameScreen.initGame(appConfigItem); + gameScreen.initGame(appConfigItem, true); agile.setScreen(gameScreen); } } @@ -891,7 +925,7 @@ private void runGame() { closeImmediately(); GameScreen gameScreen = agile.getGameScreen(); - gameScreen.initGame(gameToRun); + gameScreen.initGame(gameToRun, true); agile.setScreen(gameScreen); } diff --git a/html/src/main/java/com/agifans/agile/gwt/GwtAgileRunner.java b/html/src/main/java/com/agifans/agile/gwt/GwtAgileRunner.java index d58f706..81e3be3 100644 --- a/html/src/main/java/com/agifans/agile/gwt/GwtAgileRunner.java +++ b/html/src/main/java/com/agifans/agile/gwt/GwtAgileRunner.java @@ -82,7 +82,6 @@ public void start(AppConfigItem appConfigItem) { } private static native void updateURLWithoutReloading(String newUrl) /*-{ - console.log("Setting URL to : " + newUrl); $wnd.history.pushState(newUrl, "", newUrl); }-*/; diff --git a/html/src/main/java/com/agifans/agile/gwt/GwtLauncher.java b/html/src/main/java/com/agifans/agile/gwt/GwtLauncher.java index e6426e3..d978558 100644 --- a/html/src/main/java/com/agifans/agile/gwt/GwtLauncher.java +++ b/html/src/main/java/com/agifans/agile/gwt/GwtLauncher.java @@ -6,7 +6,12 @@ import com.badlogic.gdx.backends.gwt.preloader.Preloader; import com.google.gwt.core.client.GWT; import com.google.gwt.dom.client.Style; +import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.Panel; + +import java.util.HashMap; +import java.util.Map; + import com.agifans.agile.Agile; /** Launches the GWT application. */ @@ -26,11 +31,27 @@ public GwtApplicationConfiguration getConfig () { @Override public ApplicationListener createApplicationListener () { + Map argsMap = new HashMap<>(); + + // HTML5 version supports /id/ and /uri/ hash values, e.g. #/id/kq2 + String hash = Window.Location.getHash().toLowerCase(); + + if ((hash != null) && (hash.length() > 0)) { + if (hash.startsWith("#/id/") && !hash.endsWith("/")) { + String gameId = hash.substring(hash.lastIndexOf('/') + 1); + argsMap.put("id", gameId); + } + if (hash.startsWith("#/uri/") && !hash.endsWith("/")) { + String gameUri = hash.substring(hash.lastIndexOf('/') + 1); + argsMap.put("uri", gameUri); + } + } + GwtDialogHandler gwtDialogHandler = new GwtDialogHandler(); GwtAgileRunner gwtAgileRunner = new GwtAgileRunner( new GwtUserInput(), new GwtWavePlayer(), new GwtSavedGameStore(), new GwtPixelData(), new GwtVariableData()); - return new Agile(gwtAgileRunner, gwtDialogHandler); + return new Agile(gwtAgileRunner, gwtDialogHandler, argsMap); } @Override