Skip to content

Commit

Permalink
game menu: add option to run with game focus
Browse files Browse the repository at this point in the history
Changes:
 * Add withGameFocus option to game menu option to run the given
   runnable after alert dialog has been closed and the game has
   focus again.
 * use game focus option for on screen keyboard and toogle mouse
  • Loading branch information
Karim Mreisi committed Mar 28, 2023
1 parent 25db6aa commit 84dc834
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
41 changes: 36 additions & 5 deletions app/src/main/java/com/limelight/GameMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,23 @@
*/
public class GameMenu {

private static final long TEST_GAME_FOCUS_DELAY = 10;
private static final long KEY_UP_DELAY = 25;

public static class MenuOption {
private final String label;
private final boolean withGameFocus;
private final Runnable runnable;

public MenuOption(String label, Runnable runnable) {
public MenuOption(String label, boolean withGameFocus, Runnable runnable) {
this.label = label;
this.withGameFocus = withGameFocus;
this.runnable = runnable;
}

public MenuOption(String label, Runnable runnable) {
this(label, false, runnable);
}
}

private final Game game;
Expand Down Expand Up @@ -85,6 +92,32 @@ private void sendKeys(short[] keys) {
}), KEY_UP_DELAY);
}

private void runWithGameFocus(Runnable runnable) {
// Ensure that the Game activity is still active (not finished)
if (game.isFinishing()) {
return;
}
// Check if the game window has focus again, if not try again after delay
if (!game.hasWindowFocus()) {
new Handler().postDelayed(() -> runWithGameFocus(runnable), TEST_GAME_FOCUS_DELAY);
return;
}
// Game Activity has focus, run runnable
runnable.run();
}

private void run(MenuOption option) {
if (option.runnable == null) {
return;
}

if (option.withGameFocus) {
runWithGameFocus(option.runnable);
} else {
option.runnable.run();
}
}

private void showMenuDialog(String title, MenuOption[] options) {
AlertDialog.Builder builder = new AlertDialog.Builder(game);
builder.setTitle(title);
Expand All @@ -103,9 +136,7 @@ private void showMenuDialog(String title, MenuOption[] options) {
continue;
}

if (option.runnable != null) {
option.runnable.run();
}
run(option);
break;
}
});
Expand Down Expand Up @@ -136,7 +167,7 @@ private void showSpecialKeysMenu() {
private void showMenu() {
List<MenuOption> options = new ArrayList<>();

options.add(new MenuOption(getString(R.string.game_menu_toggle_keyboard),
options.add(new MenuOption(getString(R.string.game_menu_toggle_keyboard), true,
() -> game.toggleKeyboard()));

if (device != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1920,7 +1920,7 @@ public List<GameMenu.MenuOption> getGameMenuOptions() {
List<GameMenu.MenuOption> options = new ArrayList<>();
options.add(new GameMenu.MenuOption(activityContext.getString(mouseEmulationActive ?
R.string.game_menu_toggle_mouse_off : R.string.game_menu_toggle_mouse_on),
() -> toggleMouseEmulation()));
true, () -> toggleMouseEmulation()));

return options;
}
Expand Down

0 comments on commit 84dc834

Please sign in to comment.