Skip to content

Commit 84dc834

Browse files
author
Karim Mreisi
committed
game menu: add option to run with game focus
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
1 parent 25db6aa commit 84dc834

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

app/src/main/java/com/limelight/GameMenu.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,23 @@
1919
*/
2020
public class GameMenu {
2121

22+
private static final long TEST_GAME_FOCUS_DELAY = 10;
2223
private static final long KEY_UP_DELAY = 25;
2324

2425
public static class MenuOption {
2526
private final String label;
27+
private final boolean withGameFocus;
2628
private final Runnable runnable;
2729

28-
public MenuOption(String label, Runnable runnable) {
30+
public MenuOption(String label, boolean withGameFocus, Runnable runnable) {
2931
this.label = label;
32+
this.withGameFocus = withGameFocus;
3033
this.runnable = runnable;
3134
}
35+
36+
public MenuOption(String label, Runnable runnable) {
37+
this(label, false, runnable);
38+
}
3239
}
3340

3441
private final Game game;
@@ -85,6 +92,32 @@ private void sendKeys(short[] keys) {
8592
}), KEY_UP_DELAY);
8693
}
8794

95+
private void runWithGameFocus(Runnable runnable) {
96+
// Ensure that the Game activity is still active (not finished)
97+
if (game.isFinishing()) {
98+
return;
99+
}
100+
// Check if the game window has focus again, if not try again after delay
101+
if (!game.hasWindowFocus()) {
102+
new Handler().postDelayed(() -> runWithGameFocus(runnable), TEST_GAME_FOCUS_DELAY);
103+
return;
104+
}
105+
// Game Activity has focus, run runnable
106+
runnable.run();
107+
}
108+
109+
private void run(MenuOption option) {
110+
if (option.runnable == null) {
111+
return;
112+
}
113+
114+
if (option.withGameFocus) {
115+
runWithGameFocus(option.runnable);
116+
} else {
117+
option.runnable.run();
118+
}
119+
}
120+
88121
private void showMenuDialog(String title, MenuOption[] options) {
89122
AlertDialog.Builder builder = new AlertDialog.Builder(game);
90123
builder.setTitle(title);
@@ -103,9 +136,7 @@ private void showMenuDialog(String title, MenuOption[] options) {
103136
continue;
104137
}
105138

106-
if (option.runnable != null) {
107-
option.runnable.run();
108-
}
139+
run(option);
109140
break;
110141
}
111142
});
@@ -136,7 +167,7 @@ private void showSpecialKeysMenu() {
136167
private void showMenu() {
137168
List<MenuOption> options = new ArrayList<>();
138169

139-
options.add(new MenuOption(getString(R.string.game_menu_toggle_keyboard),
170+
options.add(new MenuOption(getString(R.string.game_menu_toggle_keyboard), true,
140171
() -> game.toggleKeyboard()));
141172

142173
if (device != null) {

app/src/main/java/com/limelight/binding/input/ControllerHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1920,7 +1920,7 @@ public List<GameMenu.MenuOption> getGameMenuOptions() {
19201920
List<GameMenu.MenuOption> options = new ArrayList<>();
19211921
options.add(new GameMenu.MenuOption(activityContext.getString(mouseEmulationActive ?
19221922
R.string.game_menu_toggle_mouse_off : R.string.game_menu_toggle_mouse_on),
1923-
() -> toggleMouseEmulation()));
1923+
true, () -> toggleMouseEmulation()));
19241924

19251925
return options;
19261926
}

0 commit comments

Comments
 (0)