mirror of
https://github.com/AngelAuraMC/Amethyst-Android.git
synced 2025-09-17 00:29:50 -04:00
Add GamepadDpad.java
This commit is contained in:
parent
d48ab790a0
commit
b9b792623f
@ -0,0 +1,77 @@
|
||||
package net.kdt.pojavlaunch.customcontrols.gamepad;
|
||||
|
||||
import android.view.InputDevice;
|
||||
import android.view.InputEvent;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.MotionEvent;
|
||||
|
||||
/*
|
||||
Code from the android documentation
|
||||
*/
|
||||
|
||||
public class GamepadDpad {
|
||||
final static int UP = 0;
|
||||
final static int LEFT = 1;
|
||||
final static int RIGHT = 2;
|
||||
final static int DOWN = 3;
|
||||
final static int CENTER = 4;
|
||||
|
||||
int pressedDirection = -1;
|
||||
|
||||
public int getDirectionPressed(InputEvent event) {
|
||||
if (!isDpadEvent(event)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// If the input event is a MotionEvent, check its hat axis values.
|
||||
if (event instanceof MotionEvent) {
|
||||
|
||||
// Use the hat axis value to find the D-pad direction
|
||||
MotionEvent motionEvent = (MotionEvent) event;
|
||||
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
|
||||
// LEFT and RIGHT direction accordingly.
|
||||
if (Float.compare(xaxis, -1.0f) == 0) {
|
||||
pressedDirection = GamepadDpad.LEFT;
|
||||
} else if (Float.compare(xaxis, 1.0f) == 0) {
|
||||
pressedDirection = GamepadDpad.RIGHT;
|
||||
}
|
||||
// Check if the AXIS_HAT_Y value is -1 or 1, and set the D-pad
|
||||
// UP and DOWN direction accordingly.
|
||||
else if (Float.compare(yaxis, -1.0f) == 0) {
|
||||
pressedDirection = GamepadDpad.UP;
|
||||
} else if (Float.compare(yaxis, 1.0f) == 0) {
|
||||
pressedDirection = GamepadDpad.DOWN;
|
||||
}
|
||||
}
|
||||
|
||||
// If the input event is a KeyEvent, check its key code.
|
||||
else if (event instanceof KeyEvent) {
|
||||
|
||||
// Use the key code to find the D-pad direction.
|
||||
KeyEvent keyEvent = (KeyEvent) event;
|
||||
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
|
||||
pressedDirection = GamepadDpad.LEFT;
|
||||
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
|
||||
pressedDirection = GamepadDpad.RIGHT;
|
||||
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
|
||||
pressedDirection = GamepadDpad.UP;
|
||||
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
|
||||
pressedDirection = GamepadDpad.DOWN;
|
||||
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER) {
|
||||
pressedDirection = GamepadDpad.CENTER;
|
||||
}
|
||||
}
|
||||
return pressedDirection;
|
||||
}
|
||||
|
||||
public static boolean isDpadEvent(InputEvent event) {
|
||||
// Check that input comes from a device with directional pads.
|
||||
// And... also the joystick since it declares sometimes as a joystick.
|
||||
|
||||
return (event.getSource() & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
|
||||
|| (event.getSource() & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user