fixed hat code validation.
authordbrady <dbrady@telltalegames.com>
Tue, 28 Jan 2014 15:28:20 -0800
changeset 81408372466c003c
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
     1.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Wed Jan 29 00:29:14 2014 +0100
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Tue Jan 28 15:28:20 2014 -0800
     1.3 @@ -262,6 +262,8 @@
     1.4      public static native int onNativePadUp(int device_id, int keycode);
     1.5      public static native void onNativeJoy(int device_id, int axis,
     1.6                                            float value);
     1.7 +    public static native void onNativeHat(int device_id, int hat_id,
     1.8 +                                          int x, int y);
     1.9      public static native void onNativeKeyDown(int keycode);
    1.10      public static native void onNativeKeyUp(int keycode);
    1.11      public static native void onNativeKeyboardFocusLost();
    1.12 @@ -923,6 +925,7 @@
    1.13          public int device_id;
    1.14          public String name;
    1.15          public ArrayList<InputDevice.MotionRange> axes;
    1.16 +        public ArrayList<InputDevice.MotionRange> hats;
    1.17      }
    1.18      class RangeComparator implements Comparator<InputDevice.MotionRange>
    1.19      {
    1.20 @@ -956,17 +959,25 @@
    1.21                      joystick.device_id = deviceIds[i];
    1.22                      joystick.name = joystickDevice.getName();
    1.23                      joystick.axes = new ArrayList<InputDevice.MotionRange>();
    1.24 +                    joystick.hats = new ArrayList<InputDevice.MotionRange>();
    1.25                      
    1.26                      List<InputDevice.MotionRange> ranges = joystickDevice.getMotionRanges();
    1.27                      Collections.sort(ranges, new RangeComparator());
    1.28                      for (InputDevice.MotionRange range : ranges ) {
    1.29 -                        if ( (range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0 ) {
    1.30 -                            joystick.axes.add(range);
    1.31 -                         }
    1.32 +                        if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0 ) {
    1.33 +                            if (range.getAxis() == MotionEvent.AXIS_HAT_X ||
    1.34 +                                range.getAxis() == MotionEvent.AXIS_HAT_Y) {
    1.35 +                                joystick.hats.add(range);
    1.36 +                            }
    1.37 +                            else {
    1.38 +                                joystick.axes.add(range);
    1.39 +                            }
    1.40 +                        }
    1.41                      }
    1.42                      
    1.43                      mJoysticks.add(joystick);
    1.44 -                    SDLActivity.nativeAddJoystick(joystick.device_id, joystick.name, 0, -1, joystick.axes.size(), 0, 0);
    1.45 +                    SDLActivity.nativeAddJoystick(joystick.device_id, joystick.name, 0, -1, 
    1.46 +                                                  joystick.axes.size(), joystick.hats.size()/2, 0);
    1.47                  }
    1.48              }
    1.49          }
    1.50 @@ -1019,7 +1030,12 @@
    1.51                              /* Normalize the value to -1...1 */
    1.52                              float value = ( event.getAxisValue( range.getAxis(), actionPointerIndex) - range.getMin() ) / range.getRange() * 2.0f - 1.0f;
    1.53                              SDLActivity.onNativeJoy(joystick.device_id, i, value );
    1.54 -                        }                       
    1.55 +                        }          
    1.56 +                        for (int i = 0; i < joystick.hats.size(); i+=2) {
    1.57 +                            int hatX = Math.round(event.getAxisValue( joystick.hats.get(i).getAxis(), actionPointerIndex ) );
    1.58 +                            int hatY = Math.round(event.getAxisValue( joystick.hats.get(i+1).getAxis(), actionPointerIndex ) );
    1.59 +                            SDLActivity.onNativeHat(joystick.device_id, i/2, hatX, hatY );
    1.60 +                        }
    1.61                      }
    1.62                      break;
    1.63                  default:
     2.1 --- a/src/core/android/SDL_android.c	Wed Jan 29 00:29:14 2014 +0100
     2.2 +++ b/src/core/android/SDL_android.c	Tue Jan 28 15:28:20 2014 -0800
     2.3 @@ -172,6 +172,14 @@
     2.4      Android_OnJoy(device_id, axis, value);
     2.5  }
     2.6  
     2.7 +/* POV Hat */
     2.8 +void Java_org_libsdl_app_SDLActivity_onNativeHat(
     2.9 +                                    JNIEnv* env, jclass jcls,
    2.10 +                                    jint device_id, jint hat_id, jint x, jint y)
    2.11 +{
    2.12 +    Android_OnHat(device_id, hat_id, x, y);
    2.13 +}
    2.14 +
    2.15  
    2.16  int Java_org_libsdl_app_SDLActivity_nativeAddJoystick(
    2.17      JNIEnv* env, jclass jcls,
     3.1 --- a/src/joystick/android/SDL_sysjoystick.c	Wed Jan 29 00:29:14 2014 +0100
     3.2 +++ b/src/joystick/android/SDL_sysjoystick.c	Tue Jan 28 15:28:20 2014 -0800
     3.3 @@ -226,6 +226,26 @@
     3.4      return 0;
     3.5  }
     3.6  
     3.7 +int
     3.8 +Android_OnHat(int device_id, int hat_id, int x, int y)
     3.9 +{
    3.10 +    const Uint8 position_map[3][3] = {
    3.11 +        {SDL_HAT_LEFTUP, SDL_HAT_UP, SDL_HAT_RIGHTUP},
    3.12 +        {SDL_HAT_LEFT, SDL_HAT_CENTERED, SDL_HAT_RIGHT},
    3.13 +        {SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN}
    3.14 +    };
    3.15 +
    3.16 +    if (x >= -1 && x <=1 && y >= -1 && y <= 1) {
    3.17 +        SDL_joylist_item *item = JoystickByDeviceId(device_id);
    3.18 +        if (item && item->joystick) {
    3.19 +            SDL_PrivateJoystickHat(item->joystick, hat_id, position_map[y+1][x+1] );
    3.20 +        }
    3.21 +        return 0;
    3.22 +    }
    3.23 +
    3.24 +    return -1;
    3.25 +}
    3.26 +
    3.27  
    3.28  int
    3.29  Android_AddJoystick(int device_id, const char *name, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs)
     4.1 --- a/src/joystick/android/SDL_sysjoystick_c.h	Wed Jan 29 00:29:14 2014 +0100
     4.2 +++ b/src/joystick/android/SDL_sysjoystick_c.h	Tue Jan 28 15:28:20 2014 -0800
     4.3 @@ -27,6 +27,7 @@
     4.4  extern int Android_OnPadDown(int device_id, int keycode);
     4.5  extern int Android_OnPadUp(int device_id, int keycode);
     4.6  extern int Android_OnJoy(int device_id, int axisnum, float value);
     4.7 +extern int Android_OnHat(int device_id, int hat_id, int x, int y);
     4.8  extern int Android_AddJoystick(int device_id, const char *name, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs);
     4.9  extern int Android_RemoveJoystick(int device_id);
    4.10