Skip to content

Commit

Permalink
fixed hat code validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrady committed Jan 28, 2014
1 parent 68f2fe6 commit 109fe0e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
26 changes: 21 additions & 5 deletions android-project/src/org/libsdl/app/SDLActivity.java
Expand Up @@ -262,6 +262,8 @@ boolean sendCommand(int command, Object data) {
public static native int onNativePadUp(int device_id, int keycode);
public static native void onNativeJoy(int device_id, int axis,
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 onNativeKeyUp(int keycode);
public static native void onNativeKeyboardFocusLost();
Expand Down Expand Up @@ -923,6 +925,7 @@ class SDLJoystick {
public int device_id;
public String name;
public ArrayList<InputDevice.MotionRange> axes;
public ArrayList<InputDevice.MotionRange> hats;
}
class RangeComparator implements Comparator<InputDevice.MotionRange>
{
Expand Down Expand Up @@ -956,17 +959,25 @@ public void pollInputDevices() {
joystick.device_id = deviceIds[i];
joystick.name = joystickDevice.getName();
joystick.axes = new ArrayList<InputDevice.MotionRange>();
joystick.hats = new ArrayList<InputDevice.MotionRange>();

List<InputDevice.MotionRange> ranges = joystickDevice.getMotionRanges();
Collections.sort(ranges, new RangeComparator());
for (InputDevice.MotionRange range : ranges ) {
if ( (range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0 ) {
joystick.axes.add(range);
}
if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0 ) {
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);
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);
}
}
}
Expand Down Expand Up @@ -1019,7 +1030,12 @@ public boolean handleMotionEvent(MotionEvent event) {
/* Normalize the value to -1...1 */
float value = ( event.getAxisValue( range.getAxis(), actionPointerIndex) - range.getMin() ) / range.getRange() * 2.0f - 1.0f;
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;
default:
Expand Down
8 changes: 8 additions & 0 deletions src/core/android/SDL_android.c
Expand Up @@ -172,6 +172,14 @@ void Java_org_libsdl_app_SDLActivity_onNativeJoy(
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(
JNIEnv* env, jclass jcls,
Expand Down
20 changes: 20 additions & 0 deletions src/joystick/android/SDL_sysjoystick.c
Expand Up @@ -226,6 +226,26 @@ Android_OnJoy(int device_id, int axis, float value)
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
Android_AddJoystick(int device_id, const char *name, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs)
Expand Down
1 change: 1 addition & 0 deletions src/joystick/android/SDL_sysjoystick_c.h
Expand Up @@ -27,6 +27,7 @@
extern int Android_OnPadDown(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_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_RemoveJoystick(int device_id);

Expand Down

0 comments on commit 109fe0e

Please sign in to comment.