Skip to content

Commit

Permalink
Added support to run a game specified in the URL. Skips showing the h…
Browse files Browse the repository at this point in the history
…ome screen.
  • Loading branch information
lanceewing committed Mar 26, 2024
1 parent ab36966 commit c6a2264
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 17 deletions.
39 changes: 28 additions & 11 deletions core/src/main/java/com/agifans/agile/Agile.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<String, String> args;

/**
* AGILE's saved preferences.
Expand All @@ -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<String, String> args) {
this.agileRunner = agileRunner;
this.dialogHandler = dialogHandler;
this.args = args;
Expand All @@ -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);
}
Expand Down
11 changes: 9 additions & 2 deletions core/src/main/java/com/agifans/agile/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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;
}

/**
Expand Down
38 changes: 36 additions & 2 deletions core/src/main/java/com/agifans/agile/HomeScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -891,7 +925,7 @@ private void runGame() {
closeImmediately();

GameScreen gameScreen = agile.getGameScreen();
gameScreen.initGame(gameToRun);
gameScreen.initGame(gameToRun, true);
agile.setScreen(gameScreen);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}-*/;

Expand Down
23 changes: 22 additions & 1 deletion html/src/main/java/com/agifans/agile/gwt/GwtLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -26,11 +31,27 @@ public GwtApplicationConfiguration getConfig () {

@Override
public ApplicationListener createApplicationListener () {
Map<String, String> 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
Expand Down

0 comments on commit c6a2264

Please sign in to comment.