MCPcopy Create free account
hub / github.com/agateau/pixelwheels / KeyMapper

Class KeyMapper

core/src/com/agateau/ui/KeyMapper.java:30–134  ·  view source on GitHub ↗

Implementation of InputMapper for keyboards

Source from the content-addressed store, hash-verified

28
29/** Implementation of InputMapper for keyboards */
30public class KeyMapper implements InputMapper {
31 // The UI instance can map multiple keycodes to the same VirtualKey, so the value type of this
32 // map is an array of int.
33 private final HashMap<VirtualKey, Integer[]> mKeysForVirtualKey = new HashMap<>();
34
35 /** Create a KeyMapper to use when navigating UIs */
36 public static KeyMapper createUiInstance() {
37 KeyMapper mapper = new KeyMapper();
38 mapper.setKey(VirtualKey.LEFT, Input.Keys.LEFT);
39 mapper.setKey(VirtualKey.RIGHT, Input.Keys.RIGHT);
40 mapper.setKey(VirtualKey.UP, Input.Keys.UP);
41 mapper.setKey(VirtualKey.DOWN, Input.Keys.DOWN);
42 mapper.setKey(VirtualKey.TRIGGER, Input.Keys.SPACE);
43 mapper.setKey(VirtualKey.BACK, Input.Keys.ESCAPE);
44 mapper.setKey(VirtualKey.PAUSE, Input.Keys.ESCAPE);
45 mapper.setKey(VirtualKey.SCREENSHOT, Input.Keys.F9);
46
47 mapper.addKey(VirtualKey.TRIGGER, Input.Keys.ENTER);
48 if (!PlatformUtils.isDesktop()) {
49 // Do not use CENTER or BACK on Desktop, it causes invalid enum value errors with lwjgl3
50 mapper.addKey(VirtualKey.TRIGGER, Input.Keys.CENTER);
51 mapper.addKey(VirtualKey.BACK, Input.Keys.BACK);
52 }
53 return mapper;
54 }
55
56 /**
57 * Create a KeyMapper used by a player during actual play, not to navigate UIs (except when
58 * picking 2nd-player specific settings)
59 */
60 public static KeyMapper createGameInstance(int playerIdx) {
61 KeyMapper mapper = new KeyMapper();
62 for (VirtualKey vkey : VirtualKey.values()) {
63 int key = DefaultKeys.getDefaultKey(playerIdx, vkey);
64 mapper.mKeysForVirtualKey.put(vkey, new Integer[] {key});
65 }
66 return mapper;
67 }
68
69 private KeyMapper() {}
70
71 public void setKey(VirtualKey vkey, int key) {
72 mKeysForVirtualKey.put(vkey, new Integer[] {key});
73 }
74
75 public void addKey(VirtualKey vkey, int key) {
76 Integer[] keys = mKeysForVirtualKey.get(vkey);
77 if (keys == null) {
78 keys = new Integer[] {key};
79 } else {
80 keys = addToIntegerArray(keys, key);
81 }
82 mKeysForVirtualKey.put(vkey, keys);
83 }
84
85 public int getKey(VirtualKey virtualKey) {
86 return mKeysForVirtualKey.get(virtualKey)[0];
87 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected