An implementation of InputMapper for gamepads
| 27 | |
| 28 | /** An implementation of InputMapper for gamepads */ |
| 29 | public class GamepadInputMapper extends ControllerAdapter implements InputMapper { |
| 30 | private enum AxisValue { |
| 31 | LESS, |
| 32 | ZERO, |
| 33 | MORE |
| 34 | } |
| 35 | |
| 36 | private enum KeyState { |
| 37 | RELEASED, |
| 38 | PRESSED, |
| 39 | JUST_PRESSED |
| 40 | } |
| 41 | |
| 42 | public interface Listener { |
| 43 | /** |
| 44 | * Returns true if the event has been handled. In this case the internal state of the input |
| 45 | * mapper won't be updated |
| 46 | */ |
| 47 | boolean onButtonPressed(int buttonCode, boolean pressed); |
| 48 | } |
| 49 | |
| 50 | private Controller mController; |
| 51 | |
| 52 | private final HashMap<VirtualKey, KeyState> mPressedKeys = new HashMap<>(); |
| 53 | |
| 54 | private final HashMap<VirtualKey, Integer> mButtonCodes = new HashMap<>(); |
| 55 | |
| 56 | // Maps well-known gamepad buttons to our virtual keys |
| 57 | private final IntMap<VirtualKey> mVirtualKeyForButton = new IntMap<>(); |
| 58 | |
| 59 | private Listener mListener; |
| 60 | |
| 61 | GamepadInputMapper(Controller controller) { |
| 62 | mButtonCodes.put(VirtualKey.TRIGGER, 1); |
| 63 | mButtonCodes.put(VirtualKey.BACK, 2); |
| 64 | mButtonCodes.put(VirtualKey.PAUSE, 3); |
| 65 | setController(controller); |
| 66 | } |
| 67 | |
| 68 | public Controller getController() { |
| 69 | return mController; |
| 70 | } |
| 71 | |
| 72 | void setController(Controller controller) { |
| 73 | mController = controller; |
| 74 | if (controller != null) { |
| 75 | mController.addListener(this); |
| 76 | updateVirtualKeyForButton(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | public int getButtonCodeForVirtualKey(VirtualKey key) { |
| 81 | return mButtonCodes.get(key); |
| 82 | } |
| 83 | |
| 84 | public void setButtonCodeForVirtualKey(VirtualKey key, int code) { |
| 85 | mButtonCodes.put(key, code); |
| 86 | } |
nothing calls this directly
no outgoing calls
no test coverage detected