|
| 1 | +package com.limelight; |
| 2 | + |
| 3 | +import android.app.AlertDialog; |
| 4 | +import android.widget.ArrayAdapter; |
| 5 | + |
| 6 | +import com.limelight.nvstream.NvConnection; |
| 7 | +import com.limelight.nvstream.input.KeyboardPacket; |
| 8 | + |
| 9 | +public class GameBackMenu { |
| 10 | + |
| 11 | + private final Game game; |
| 12 | + private final NvConnection conn; |
| 13 | + |
| 14 | + public GameBackMenu(Game game, NvConnection conn) { |
| 15 | + this.game = game; |
| 16 | + this.conn = conn; |
| 17 | + |
| 18 | + showMenuDialog("Back Menu", new MenuDialogOption[]{ |
| 19 | + new MenuDialogOption("Send special key(s)", () -> showSpecialKeysMenu()), |
| 20 | + new MenuDialogOption("Disconnect", () -> game.onBackPressed()), |
| 21 | + new MenuDialogOption("Cancel", null), |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + private void sendKeySequence(byte modifier, short[] keys) { |
| 26 | + |
| 27 | + for (short key : keys) |
| 28 | + conn.sendKeyboardInput(key, KeyboardPacket.KEY_DOWN, (byte) (modifier | KeyboardPacket.KEY_DOWN)); |
| 29 | + |
| 30 | + for (int pos = keys.length - 1; pos >= 0; pos--) |
| 31 | + conn.sendKeyboardInput(keys[pos], KeyboardPacket.KEY_UP, (byte) (modifier | KeyboardPacket.KEY_UP)); |
| 32 | + } |
| 33 | + |
| 34 | + private void showMenuDialog(String title, MenuDialogOption[] options) { |
| 35 | + AlertDialog.Builder builder = new AlertDialog.Builder(game); |
| 36 | + builder.setTitle(title); |
| 37 | + |
| 38 | + final ArrayAdapter<String> actions = |
| 39 | + new ArrayAdapter<String>(game, android.R.layout.simple_list_item_1); |
| 40 | + |
| 41 | + for (MenuDialogOption option : options) { |
| 42 | + actions.add(option.label); |
| 43 | + } |
| 44 | + |
| 45 | + builder.setAdapter(actions, (dialog, which) -> { |
| 46 | + String label = actions.getItem(which); |
| 47 | + for (MenuDialogOption option : options) { |
| 48 | + if (!label.equals(option.label)) { |
| 49 | + continue; |
| 50 | + } |
| 51 | + |
| 52 | + if (option.runnable != null) { |
| 53 | + option.runnable.run(); |
| 54 | + } |
| 55 | + break; |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + builder.show(); |
| 60 | + } |
| 61 | + |
| 62 | + private void showSpecialKeysMenu() { |
| 63 | + showMenuDialog(ACTIONS.SEND_SPECIAL_KEYS, new MenuDialogOption[]{ |
| 64 | + new MenuDialogOption("Send ESC", () -> sendKeySequence( |
| 65 | + (byte) 0, new short[]{0x18})), |
| 66 | + new MenuDialogOption("Send F11", () -> sendKeySequence( |
| 67 | + (byte) 0, new short[]{0x7a})), |
| 68 | + new MenuDialogOption("Send WIN (Open Start Menu)", () -> sendKeySequence( |
| 69 | + (byte) 0, new short[]{0x5B})), |
| 70 | + new MenuDialogOption("Send WIN + D (Switch to Desktop)", () -> sendKeySequence( |
| 71 | + (byte) 0, new short[]{0x5B, 0x44})), |
| 72 | + new MenuDialogOption("Send WIN + G (Open Xbox Game Bar)", () -> sendKeySequence( |
| 73 | + (byte) 0, new short[]{0x5B, 0x47})), |
| 74 | + /* |
| 75 | + TODO: Currently not working |
| 76 | + new MenuDialogOption("Send SHIFT + TAB (Open Steam Overlay)", () -> sendKeySequence( |
| 77 | + (byte) 0, new short[]{0xA0, 0x09})), |
| 78 | + */ |
| 79 | + new MenuDialogOption("Cancel", null), |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + private interface ACTIONS { |
| 84 | + String CONTINUE = "Continue"; |
| 85 | + String SEND_SPECIAL_KEYS = "Send special key(s)"; |
| 86 | + String DISCONNECT = "Disconnect"; |
| 87 | + } |
| 88 | + |
| 89 | + private class MenuDialogOption { |
| 90 | + private final String label; |
| 91 | + private final Runnable runnable; |
| 92 | + |
| 93 | + MenuDialogOption(String label, Runnable runnable) { |
| 94 | + this.label = label; |
| 95 | + this.runnable = runnable; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments