Add Xbox 360 controller support.

This commit is contained in:
Cameron Gutman 2013-09-30 20:18:21 -04:00
parent cd42ae04ff
commit 4f3de0ba7e

View File

@ -177,23 +177,34 @@ public class Game extends Activity {
public boolean onGenericMotionEvent(MotionEvent event) { public boolean onGenericMotionEvent(MotionEvent event) {
InputDevice dev = event.getDevice(); InputDevice dev = event.getDevice();
if ((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { if (dev == null) {
//Get all the axis for the event System.err.println("Unknown device");
float LS_X = event.getAxisValue(MotionEvent.AXIS_X); return false;
float LS_Y = event.getAxisValue(MotionEvent.AXIS_Y);
float RS_X = event.getAxisValue(MotionEvent.AXIS_Z);
float RS_Y = event.getAxisValue(MotionEvent.AXIS_RZ);
float LS_X_DEADZONE = dev.getMotionRange(MotionEvent.AXIS_X).getFlat();
float LS_Y_DEADZONE = dev.getMotionRange(MotionEvent.AXIS_Y).getFlat();
if (LS_X * LS_X + LS_Y * LS_Y < LS_X_DEADZONE * LS_Y_DEADZONE) {
LS_X = LS_Y = 0.0f;
} }
float RS_X_DEADZONE = dev.getMotionRange(MotionEvent.AXIS_Z).getFlat(); if ((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
float RS_Y_DEADZONE = dev.getMotionRange(MotionEvent.AXIS_RZ).getFlat(); float LS_X = event.getAxisValue(MotionEvent.AXIS_X);
if (RS_X * RS_X + RS_Y * RS_Y < RS_X_DEADZONE * RS_Y_DEADZONE) { float LS_Y = event.getAxisValue(MotionEvent.AXIS_Y);
RS_X = RS_Y = 0.0f;
float RS_X, RS_Y, L2, R2;
InputDevice.MotionRange l2Range = dev.getMotionRange(MotionEvent.AXIS_LTRIGGER);
InputDevice.MotionRange r2Range = dev.getMotionRange(MotionEvent.AXIS_RTRIGGER);
if (l2Range != null && r2Range != null)
{
// Ouya controller
RS_X = event.getAxisValue(MotionEvent.AXIS_Z);
RS_Y = event.getAxisValue(MotionEvent.AXIS_RZ);
L2 = event.getAxisValue(MotionEvent.AXIS_LTRIGGER);
R2 = event.getAxisValue(MotionEvent.AXIS_RTRIGGER);
}
else
{
// Xbox controller
RS_X = event.getAxisValue(MotionEvent.AXIS_RX);
RS_Y = event.getAxisValue(MotionEvent.AXIS_RY);
L2 = (event.getAxisValue(MotionEvent.AXIS_Z) + 1) / 2;
R2 = (event.getAxisValue(MotionEvent.AXIS_RZ) + 1) / 2;
} }
leftStickX = (short)Math.round(LS_X * 0x7FFF); leftStickX = (short)Math.round(LS_X * 0x7FFF);
@ -202,20 +213,6 @@ public class Game extends Activity {
rightStickX = (short)Math.round(RS_X * 0x7FFF); rightStickX = (short)Math.round(RS_X * 0x7FFF);
rightStickY = (short)Math.round(-RS_Y * 0x7FFF); rightStickY = (short)Math.round(-RS_Y * 0x7FFF);
float L2 = event.getAxisValue(MotionEvent.AXIS_LTRIGGER);
float L2_DEADZONE = dev.getMotionRange(MotionEvent.AXIS_LTRIGGER).getFlat();
if (L2 < L2_DEADZONE) {
L2 = 0.0f;
}
float R2 = event.getAxisValue(MotionEvent.AXIS_RTRIGGER);
float R2_DEADZONE = dev.getMotionRange(MotionEvent.AXIS_RTRIGGER).getFlat();
if (R2 < R2_DEADZONE) {
R2 = 0.0f;
}
leftTrigger = (byte)Math.round(L2 * 0xFF); leftTrigger = (byte)Math.round(L2 * 0xFF);
rightTrigger = (byte)Math.round(R2 * 0xFF); rightTrigger = (byte)Math.round(R2 * 0xFF);