Browse Source

Android gamepad support

seanpaultaylor 11 years ago
parent
commit
9a23c3f73d
3 changed files with 30 additions and 18 deletions
  1. 2 2
      gameplay/src/Form.cpp
  2. 3 6
      gameplay/src/Gamepad.cpp
  3. 25 10
      gameplay/src/PlatformAndroid.cpp

+ 2 - 2
gameplay/src/Form.cpp

@@ -10,8 +10,8 @@
 #include "CheckBox.h"
 #include "Scene.h"
 
-// Scroll speed when using a DPad -- max scroll speed when using a joystick.
-static const float GAMEPAD_SCROLL_SPEED = 500.0f;
+// Scroll speed when using a joystick.
+static const float GAMEPAD_SCROLL_SPEED = 600.0f;
 // Distance a joystick must be pushed in order to trigger focus-change and/or scrolling.
 static const float JOYSTICK_THRESHOLD = 0.75f;
 // If the DPad or joystick is held down, this is the initial delay in milliseconds between focus changes.

+ 3 - 6
gameplay/src/Gamepad.cpp

@@ -389,8 +389,7 @@ void Gamepad::setButtons(unsigned int buttons)
     if (buttons != _buttons)
     {
         _buttons = buttons;
-        if (isVirtual())
-            Form::gamepadButtonEventInternal(this);
+        Form::gamepadButtonEventInternal(this);
     }
 }
 
@@ -399,8 +398,7 @@ void Gamepad::setJoystickValue(unsigned int index, float x, float y)
     if (_joysticks[index].x != x || _joysticks[index].y != y)
     {
         _joysticks[index].set(x, y);
-        if (isVirtual())
-            Form::gamepadJoystickEventInternal(this, index);
+        Form::gamepadJoystickEventInternal(this, index);
     }
 }
 
@@ -409,8 +407,7 @@ void Gamepad::setTriggerValue(unsigned int index, float value)
     if (_triggers[index] != value)
     {
         _triggers[index] = value;
-        if (isVirtual())
-            Form::gamepadTriggerEventInternal(this, index);
+        Form::gamepadTriggerEventInternal(this, index);
     }
 }
 

+ 25 - 10
gameplay/src/PlatformAndroid.cpp

@@ -752,6 +752,13 @@ Gamepad::ButtonMapping getGamepadButtonMapping(jint keycode)
     return Gamepad::BUTTON_MENU4;
 }
 
+static float clampFuzz(float value, float fuzz)
+{
+    if (std::fabs(value) <= fuzz)
+        return 0.0f;
+    return value;
+}
+
 // Process the next input event.
 static int32_t engine_handle_input(struct android_app* app, AInputEvent* event)
 {
@@ -768,8 +775,8 @@ static int32_t engine_handle_input(struct android_app* app, AInputEvent* event)
         int y;
         
         if (source & AINPUT_SOURCE_JOYSTICK)
-        {            
-            // DPAD (axis hats)
+        {
+            // DPAD handling (axis hats)
             float xaxis = AMotionEvent_getAxisValue(event, AMOTION_EVENT_AXIS_HAT_X, 0);
             float yaxis = AMotionEvent_getAxisValue(event, AMOTION_EVENT_AXIS_HAT_Y, 0);        
             if (xaxis == -1.0f)
@@ -785,7 +792,7 @@ static int32_t engine_handle_input(struct android_app* app, AInputEvent* event)
                 gameplay::Platform::gamepadButtonReleasedEventInternal(deviceId, gameplay::Gamepad::BUTTON_LEFT);
                 gameplay::Platform::gamepadButtonReleasedEventInternal(deviceId, gameplay::Gamepad::BUTTON_RIGHT);
             }
-            
+
             if(yaxis == -1.0f)
             {
                 gameplay::Platform::gamepadButtonPressedEventInternal(deviceId, gameplay::Gamepad::BUTTON_UP);
@@ -798,14 +805,22 @@ static int32_t engine_handle_input(struct android_app* app, AInputEvent* event)
             {
                 gameplay::Platform::gamepadButtonReleasedEventInternal(deviceId, gameplay::Gamepad::BUTTON_UP);
                 gameplay::Platform::gamepadButtonReleasedEventInternal(deviceId, gameplay::Gamepad::BUTTON_DOWN);
-            }         
-            
-            /* TRIGGERS
+            }
+
+            // Trigger handling
             float leftTrigger = AMotionEvent_getAxisValue(event, AMOTION_EVENT_AXIS_BRAKE, 0);
-                gameplay::Platform::gamepadTriggerChangedEventInternal(deviceId, 0, leftTrigger);
-            float rightTrigger = AMotionEvent_getAxisValue(event, AMOTION_EVENT_AXIS_GAS, 1);
-                gameplay::Platform::gamepadTriggerChangedEventInternal(deviceId, 0, rightTrigger);
-            */
+            gameplay::Platform::gamepadTriggerChangedEventInternal(deviceId, 0, leftTrigger);
+            float rightTrigger = AMotionEvent_getAxisValue(event, AMOTION_EVENT_AXIS_GAS, 0);
+            gameplay::Platform::gamepadTriggerChangedEventInternal(deviceId, 1, rightTrigger);
+
+            // jJoystick handling
+            float fuzz = 0.15f;
+            float x = AMotionEvent_getAxisValue(event, AMOTION_EVENT_AXIS_X, 0);
+            float y = AMotionEvent_getAxisValue(event, AMOTION_EVENT_AXIS_Y, 0);
+            gameplay::Platform::gamepadJoystickChangedEventInternal(deviceId, 0, clampFuzz(x, fuzz), clampFuzz(y, fuzz));
+            float z = AMotionEvent_getAxisValue(event, AMOTION_EVENT_AXIS_Z, 0);
+            float rz = AMotionEvent_getAxisValue(event, AMOTION_EVENT_AXIS_RZ, 0);
+            gameplay::Platform::gamepadJoystickChangedEventInternal(deviceId, 1, clampFuzz(z, fuzz), clampFuzz(rz, fuzz));
         }
         else
         {