Improved precision of the Dpad detection

This commit is contained in:
SerpentSpirale 2021-07-20 14:32:24 +02:00
parent 00e60ee3bd
commit 97a8a11f80

View File

@ -38,68 +38,68 @@ public class GamepadDpad {
this.parentPad = parentPad; this.parentPad = parentPad;
} }
public int update(InputEvent event) { public void update(KeyEvent event){
if (!isDpadEvent(event)) {
return -1; //TODO check if the event is valid
if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
pressedDirection = LEFT;
} else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
pressedDirection = RIGHT;
} else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
pressedDirection = UP;
} else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
pressedDirection = DOWN;
} else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER) {
pressedDirection = CENTER;
} }
// If the input event is a MotionEvent, check its hat axis values. setDummyEventKeyCode(pressedDirection);
if (event instanceof MotionEvent) { parentPad.sendButton(dummyEvent);
}
// Use the hat axis value to find the D-pad direction public void update(MotionEvent event){
MotionEvent motionEvent = (MotionEvent) event; //TODO check if the event is valid
float xaxis = motionEvent.getAxisValue(MotionEvent.AXIS_HAT_X);
float yaxis = motionEvent.getAxisValue(MotionEvent.AXIS_HAT_Y);
// Check if the AXIS_HAT_X value is -1 or 1, and set the D-pad // Use the hat axis value to find the D-pad direction
// LEFT and RIGHT direction accordingly. float xaxis = event.getAxisValue(MotionEvent.AXIS_HAT_X);
if (Float.compare(xaxis, -1.0f) == 0) { float yaxis = event.getAxisValue(MotionEvent.AXIS_HAT_Y);
pressedDirection = LEFT;
} else if (Float.compare(xaxis, 1.0f) == 0) { // Check if the AXIS_HAT_X value is -1 or 1, and set the D-pad
pressedDirection = RIGHT; // LEFT and RIGHT direction accordingly.
} if (Float.compare(xaxis, -1.0f) == 0) {
// Check if the AXIS_HAT_Y value is -1 or 1, and set the D-pad pressedDirection = LEFT;
// UP and DOWN direction accordingly. } else if (Float.compare(xaxis, 1.0f) == 0) {
else if (Float.compare(yaxis, -1.0f) == 0) { pressedDirection = RIGHT;
pressedDirection = UP; }
} else if (Float.compare(yaxis, 1.0f) == 0) { // Check if the AXIS_HAT_Y value is -1 or 1, and set the D-pad
pressedDirection = DOWN; // UP and DOWN direction accordingly.
}else { else if (Float.compare(yaxis, -1.0f) == 0) {
pressedDirection = CENTER; pressedDirection = UP;
} } else if (Float.compare(yaxis, 1.0f) == 0) {
pressedDirection = DOWN;
}else {
pressedDirection = CENTER;
} }
// If the input event is a KeyEvent, check its key code. setDummyEventKeyCode(pressedDirection);
else if (event instanceof KeyEvent) { parentPad.sendButton(dummyEvent);
}
// Use the key code to find the D-pad direction.
KeyEvent keyEvent = (KeyEvent) event;
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
pressedDirection = LEFT;
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
pressedDirection = RIGHT;
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
pressedDirection = UP;
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
pressedDirection = DOWN;
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER) {
pressedDirection = CENTER;
}
}
private void setDummyEventKeyCode(int fakeKeycode){
try { try {
eventCodeField.setInt(dummyEvent, pressedDirection); eventCodeField.setInt(dummyEvent, fakeKeycode);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
e.printStackTrace(); e.printStackTrace();
} }
parentPad.sendButton(dummyEvent);
return pressedDirection;
} }
public static boolean isDpadEvent(InputEvent event) { public static boolean isDpadEvent(MotionEvent event) {
// Check that input comes from a device with directional pads. // Check that input comes from a device with directional pads.
// And... also the joystick since it declares sometimes as a joystick. // And... also the joystick since it declares sometimes as a joystick.
return (event.getSource() & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK && return (event.getSource() & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK;
((event.getSource() & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD) && (event.getDevice().getKeyboardType() == KEYBOARD_TYPE_NON_ALPHABETIC); }
public static boolean isDpadEvent(KeyEvent event){
return ((event.getSource() & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD) && (event.getDevice().getKeyboardType() == KEYBOARD_TYPE_NON_ALPHABETIC);
} }
} }