fixed hat code validation.

This commit is contained in:
dbrady 2014-01-28 15:28:20 -08:00
parent 68f2fe6499
commit 109fe0e0dc
4 changed files with 50 additions and 5 deletions

View File

@ -262,6 +262,8 @@ public class SDLActivity extends Activity {
public static native int onNativePadUp(int device_id, int keycode); public static native int onNativePadUp(int device_id, int keycode);
public static native void onNativeJoy(int device_id, int axis, public static native void onNativeJoy(int device_id, int axis,
float value); float value);
public static native void onNativeHat(int device_id, int hat_id,
int x, int y);
public static native void onNativeKeyDown(int keycode); public static native void onNativeKeyDown(int keycode);
public static native void onNativeKeyUp(int keycode); public static native void onNativeKeyUp(int keycode);
public static native void onNativeKeyboardFocusLost(); public static native void onNativeKeyboardFocusLost();
@ -923,6 +925,7 @@ class SDLJoystickHandler_API12 extends SDLJoystickHandler {
public int device_id; public int device_id;
public String name; public String name;
public ArrayList<InputDevice.MotionRange> axes; public ArrayList<InputDevice.MotionRange> axes;
public ArrayList<InputDevice.MotionRange> hats;
} }
class RangeComparator implements Comparator<InputDevice.MotionRange> class RangeComparator implements Comparator<InputDevice.MotionRange>
{ {
@ -956,17 +959,25 @@ class SDLJoystickHandler_API12 extends SDLJoystickHandler {
joystick.device_id = deviceIds[i]; joystick.device_id = deviceIds[i];
joystick.name = joystickDevice.getName(); joystick.name = joystickDevice.getName();
joystick.axes = new ArrayList<InputDevice.MotionRange>(); joystick.axes = new ArrayList<InputDevice.MotionRange>();
joystick.hats = new ArrayList<InputDevice.MotionRange>();
List<InputDevice.MotionRange> ranges = joystickDevice.getMotionRanges(); List<InputDevice.MotionRange> ranges = joystickDevice.getMotionRanges();
Collections.sort(ranges, new RangeComparator()); Collections.sort(ranges, new RangeComparator());
for (InputDevice.MotionRange range : ranges ) { for (InputDevice.MotionRange range : ranges ) {
if ( (range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0 ) { if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0 ) {
joystick.axes.add(range); if (range.getAxis() == MotionEvent.AXIS_HAT_X ||
} range.getAxis() == MotionEvent.AXIS_HAT_Y) {
joystick.hats.add(range);
}
else {
joystick.axes.add(range);
}
}
} }
mJoysticks.add(joystick); mJoysticks.add(joystick);
SDLActivity.nativeAddJoystick(joystick.device_id, joystick.name, 0, -1, joystick.axes.size(), 0, 0); SDLActivity.nativeAddJoystick(joystick.device_id, joystick.name, 0, -1,
joystick.axes.size(), joystick.hats.size()/2, 0);
} }
} }
} }
@ -1019,7 +1030,12 @@ class SDLJoystickHandler_API12 extends SDLJoystickHandler {
/* Normalize the value to -1...1 */ /* Normalize the value to -1...1 */
float value = ( event.getAxisValue( range.getAxis(), actionPointerIndex) - range.getMin() ) / range.getRange() * 2.0f - 1.0f; float value = ( event.getAxisValue( range.getAxis(), actionPointerIndex) - range.getMin() ) / range.getRange() * 2.0f - 1.0f;
SDLActivity.onNativeJoy(joystick.device_id, i, value ); SDLActivity.onNativeJoy(joystick.device_id, i, value );
} }
for (int i = 0; i < joystick.hats.size(); i+=2) {
int hatX = Math.round(event.getAxisValue( joystick.hats.get(i).getAxis(), actionPointerIndex ) );
int hatY = Math.round(event.getAxisValue( joystick.hats.get(i+1).getAxis(), actionPointerIndex ) );
SDLActivity.onNativeHat(joystick.device_id, i/2, hatX, hatY );
}
} }
break; break;
default: default:

View File

@ -172,6 +172,14 @@ void Java_org_libsdl_app_SDLActivity_onNativeJoy(
Android_OnJoy(device_id, axis, value); Android_OnJoy(device_id, axis, value);
} }
/* POV Hat */
void Java_org_libsdl_app_SDLActivity_onNativeHat(
JNIEnv* env, jclass jcls,
jint device_id, jint hat_id, jint x, jint y)
{
Android_OnHat(device_id, hat_id, x, y);
}
int Java_org_libsdl_app_SDLActivity_nativeAddJoystick( int Java_org_libsdl_app_SDLActivity_nativeAddJoystick(
JNIEnv* env, jclass jcls, JNIEnv* env, jclass jcls,

View File

@ -226,6 +226,26 @@ Android_OnJoy(int device_id, int axis, float value)
return 0; return 0;
} }
int
Android_OnHat(int device_id, int hat_id, int x, int y)
{
const Uint8 position_map[3][3] = {
{SDL_HAT_LEFTUP, SDL_HAT_UP, SDL_HAT_RIGHTUP},
{SDL_HAT_LEFT, SDL_HAT_CENTERED, SDL_HAT_RIGHT},
{SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN}
};
if (x >= -1 && x <=1 && y >= -1 && y <= 1) {
SDL_joylist_item *item = JoystickByDeviceId(device_id);
if (item && item->joystick) {
SDL_PrivateJoystickHat(item->joystick, hat_id, position_map[y+1][x+1] );
}
return 0;
}
return -1;
}
int int
Android_AddJoystick(int device_id, const char *name, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs) Android_AddJoystick(int device_id, const char *name, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs)

View File

@ -27,6 +27,7 @@
extern int Android_OnPadDown(int device_id, int keycode); extern int Android_OnPadDown(int device_id, int keycode);
extern int Android_OnPadUp(int device_id, int keycode); extern int Android_OnPadUp(int device_id, int keycode);
extern int Android_OnJoy(int device_id, int axisnum, float value); extern int Android_OnJoy(int device_id, int axisnum, float value);
extern int Android_OnHat(int device_id, int hat_id, int x, int y);
extern int Android_AddJoystick(int device_id, const char *name, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs); extern int Android_AddJoystick(int device_id, const char *name, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs);
extern int Android_RemoveJoystick(int device_id); extern int Android_RemoveJoystick(int device_id);