Add Moga controller support.

This commit is contained in:
Cameron Gutman 2013-11-12 03:10:23 -05:00
parent aa4e07ea2f
commit ae65bbe3ca

View File

@ -339,70 +339,84 @@ public class Game extends Activity implements OnGenericMotionListener, OnTouchLi
return false; return false;
} }
if ((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { if ((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
float LS_X = event.getAxisValue(MotionEvent.AXIS_X); float LS_X = event.getAxisValue(MotionEvent.AXIS_X);
float LS_Y = event.getAxisValue(MotionEvent.AXIS_Y); float LS_Y = event.getAxisValue(MotionEvent.AXIS_Y);
float RS_X, RS_Y, L2, R2; float RS_X, RS_Y, L2, R2;
InputDevice.MotionRange l2Range = dev.getMotionRange(MotionEvent.AXIS_LTRIGGER); InputDevice.MotionRange leftTriggerRange = dev.getMotionRange(MotionEvent.AXIS_LTRIGGER);
InputDevice.MotionRange r2Range = dev.getMotionRange(MotionEvent.AXIS_RTRIGGER); InputDevice.MotionRange rightTriggerRange = dev.getMotionRange(MotionEvent.AXIS_RTRIGGER);
if (l2Range != null && r2Range != null) if (leftTriggerRange != null && rightTriggerRange != null)
{ {
// Ouya controller // Ouya controller
RS_X = event.getAxisValue(MotionEvent.AXIS_Z); L2 = event.getAxisValue(MotionEvent.AXIS_LTRIGGER);
RS_Y = event.getAxisValue(MotionEvent.AXIS_RZ); R2 = event.getAxisValue(MotionEvent.AXIS_RTRIGGER);
L2 = event.getAxisValue(MotionEvent.AXIS_LTRIGGER); RS_X = event.getAxisValue(MotionEvent.AXIS_Z);
R2 = event.getAxisValue(MotionEvent.AXIS_RTRIGGER); RS_Y = event.getAxisValue(MotionEvent.AXIS_RZ);
} }
else else
{ {
// Xbox controller InputDevice.MotionRange brakeRange = dev.getMotionRange(MotionEvent.AXIS_BRAKE);
RS_X = event.getAxisValue(MotionEvent.AXIS_RX); InputDevice.MotionRange gasRange = dev.getMotionRange(MotionEvent.AXIS_GAS);
RS_Y = event.getAxisValue(MotionEvent.AXIS_RY); if (brakeRange != null && gasRange != null)
L2 = (event.getAxisValue(MotionEvent.AXIS_Z) + 1) / 2; {
R2 = (event.getAxisValue(MotionEvent.AXIS_RZ) + 1) / 2; // Moga controller
} RS_X = event.getAxisValue(MotionEvent.AXIS_Z);
RS_Y = event.getAxisValue(MotionEvent.AXIS_RZ);
L2 = event.getAxisValue(MotionEvent.AXIS_BRAKE);
R2 = event.getAxisValue(MotionEvent.AXIS_GAS);
}
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;
}
}
InputDevice.MotionRange hatXRange = dev.getMotionRange(MotionEvent.AXIS_HAT_X);
InputDevice.MotionRange hatYRange = dev.getMotionRange(MotionEvent.AXIS_HAT_Y);
if (hatXRange != null && hatYRange != null)
{
// Xbox controller D-pad
float hatX, hatY;
hatX = event.getAxisValue(MotionEvent.AXIS_HAT_X); InputDevice.MotionRange hatXRange = dev.getMotionRange(MotionEvent.AXIS_HAT_X);
hatY = event.getAxisValue(MotionEvent.AXIS_HAT_Y); InputDevice.MotionRange hatYRange = dev.getMotionRange(MotionEvent.AXIS_HAT_Y);
if (hatXRange != null && hatYRange != null)
{
// Xbox controller D-pad
float hatX, hatY;
inputMap &= ~(NvControllerPacket.LEFT_FLAG | NvControllerPacket.RIGHT_FLAG); hatX = event.getAxisValue(MotionEvent.AXIS_HAT_X);
inputMap &= ~(NvControllerPacket.UP_FLAG | NvControllerPacket.DOWN_FLAG); hatY = event.getAxisValue(MotionEvent.AXIS_HAT_Y);
if (hatX < -0.5) {
inputMap |= NvControllerPacket.LEFT_FLAG;
}
if (hatX > 0.5) {
inputMap |= NvControllerPacket.RIGHT_FLAG;
}
if (hatY < -0.5) {
inputMap |= NvControllerPacket.UP_FLAG;
}
if (hatY > 0.5) {
inputMap |= NvControllerPacket.DOWN_FLAG;
}
}
leftStickX = (short)Math.round(LS_X * 0x7FFF); inputMap &= ~(NvControllerPacket.LEFT_FLAG | NvControllerPacket.RIGHT_FLAG);
leftStickY = (short)Math.round(-LS_Y * 0x7FFF); inputMap &= ~(NvControllerPacket.UP_FLAG | NvControllerPacket.DOWN_FLAG);
if (hatX < -0.5) {
inputMap |= NvControllerPacket.LEFT_FLAG;
}
if (hatX > 0.5) {
inputMap |= NvControllerPacket.RIGHT_FLAG;
}
if (hatY < -0.5) {
inputMap |= NvControllerPacket.UP_FLAG;
}
if (hatY > 0.5) {
inputMap |= NvControllerPacket.DOWN_FLAG;
}
}
rightStickX = (short)Math.round(RS_X * 0x7FFF); leftStickX = (short)Math.round(LS_X * 0x7FFF);
rightStickY = (short)Math.round(-RS_Y * 0x7FFF); leftStickY = (short)Math.round(-LS_Y * 0x7FFF);
leftTrigger = (byte)Math.round(L2 * 0xFF); rightStickX = (short)Math.round(RS_X * 0x7FFF);
rightTrigger = (byte)Math.round(R2 * 0xFF); rightStickY = (short)Math.round(-RS_Y * 0x7FFF);
sendControllerInputPacket(); leftTrigger = (byte)Math.round(L2 * 0xFF);
return true; rightTrigger = (byte)Math.round(R2 * 0xFF);
sendControllerInputPacket();
return true;
} }
else if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) else if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0)
{ {
// Send a mouse move update (if neccessary) // Send a mouse move update (if neccessary)
updateMousePosition((int)event.getX(), (int)event.getY()); updateMousePosition((int)event.getX(), (int)event.getY());