fixed hat code validation.
authordbrady <dbrady@telltalegames.com>
Tue, 28 Jan 2014 15:28:20 -0800
changeset 8140 8372466c003c
parent 8139 ad675d62631b
child 8141 f3c8d96605cb
fixed hat code validation.
android-project/src/org/libsdl/app/SDLActivity.java
src/core/android/SDL_android.c
src/joystick/android/SDL_sysjoystick.c
src/joystick/android/SDL_sysjoystick_c.h
--- a/android-project/src/org/libsdl/app/SDLActivity.java	Wed Jan 29 00:29:14 2014 +0100
+++ b/android-project/src/org/libsdl/app/SDLActivity.java	Tue Jan 28 15:28:20 2014 -0800
@@ -262,6 +262,8 @@
     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();
@@ -923,6 +925,7 @@
         public int device_id;
         public String name;
         public ArrayList<InputDevice.MotionRange> axes;
+        public ArrayList<InputDevice.MotionRange> hats;
     }
     class RangeComparator implements Comparator<InputDevice.MotionRange>
     {
@@ -956,17 +959,25 @@
                     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);
                 }
             }
         }
@@ -1019,7 +1030,12 @@
                             /* 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:
--- a/src/core/android/SDL_android.c	Wed Jan 29 00:29:14 2014 +0100
+++ b/src/core/android/SDL_android.c	Tue Jan 28 15:28:20 2014 -0800
@@ -172,6 +172,14 @@
     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,
--- a/src/joystick/android/SDL_sysjoystick.c	Wed Jan 29 00:29:14 2014 +0100
+++ b/src/joystick/android/SDL_sysjoystick.c	Tue Jan 28 15:28:20 2014 -0800
@@ -226,6 +226,26 @@
     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)
--- a/src/joystick/android/SDL_sysjoystick_c.h	Wed Jan 29 00:29:14 2014 +0100
+++ b/src/joystick/android/SDL_sysjoystick_c.h	Tue Jan 28 15:28:20 2014 -0800
@@ -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);