Procházet zdrojové kódy

Refactoring for Platform gamepad events.

seanpaultaylor před 11 roky
rodič
revize
5fb8cdaa4a

+ 5 - 2
gameplay/src/Base.h

@@ -278,9 +278,12 @@ typedef GLuint FrameBufferHandle;
 /** Render buffer handle. */
 typedef GLuint RenderBufferHandle;
 
-/** Gamepad handle definitions vary by platform. */
+/** Gamepad handle */
+#ifdef __ANDROID__
+typedef unsigned int GamepadHandle;
+#else
 typedef unsigned long GamepadHandle;
-
+#endif
 }
 
 /**

+ 11 - 1
gameplay/src/Control.cpp

@@ -1078,7 +1078,17 @@ bool Control::mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
     return false;
 }
 
-bool Control::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex)
+bool Control::gamepadButtonEvent(Gamepad* gamepad)
+{
+    return false;
+}
+
+bool Control::gamepadTriggerEvent(Gamepad* gamepad, unsigned int index)
+{
+    return false;
+}
+
+bool Control::gamepadJoystickEvent(Gamepad* gamepad, unsigned int index)
 {
     return false;
 }

+ 19 - 5
gameplay/src/Control.h

@@ -1096,13 +1096,27 @@ protected:
     virtual bool mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta);
 
     /**
-     * Gamepad callback on gamepad events.
+     * Gamepad callback on gamepad button changes.
      *
-     * @param gamepad The gamepad whose state changed.
-     * @param evt The gamepad event that occurred.
-     * @param analogIndex If evt is JOYSTICK_EVENT or TRIGGER_EVENT, this will be the index of the corresponding control.
+     * @param gamepad The gamepad whose one or more buttons have changed.
      */
-    virtual bool gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex);
+    virtual bool gamepadButtonEvent(Gamepad* gamepad);
+
+    /**
+     * Gamepad callback on gamepad trigger changes.
+     *
+     * @param gamepad The gamepad whose one or more buttons have changed.
+     * @param index The index of the trigger that changed. 
+     */
+    virtual bool gamepadTriggerEvent(Gamepad* gamepad, unsigned int index);
+
+    /**
+     * Gamepad callback on gamepad analog joystick changes.
+     *
+     * @param gamepad The gamepad whose one or more buttons have changed.
+     * @param index The index of the joystick that changed.
+     */
+    virtual bool gamepadJoystickEvent(Gamepad* gamepad, unsigned int index);
 
     /**
      * Called each frame to update this control and its children.

+ 57 - 30
gameplay/src/Form.cpp

@@ -903,7 +903,7 @@ bool Form::pollGamepad(Gamepad* gamepad)
     return focusPressed || scrolling;
 }
 
-bool Form::gamepadEventInternal(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex)
+bool Form::gamepadButtonEventInternal(Gamepad* gamepad)
 {
     if (!__focusControl)
         return false;
@@ -911,56 +911,83 @@ bool Form::gamepadEventInternal(Gamepad::GamepadEvent evt, Gamepad* gamepad, uns
     bool selectButtonPressed = gamepad->isButtonDown(Gamepad::BUTTON_A) || gamepad->isButtonDown(Gamepad::BUTTON_X);
 
     // Fire press, release and click events to focused controls
-    switch (evt)
+    if (selectButtonPressed && __focusControl->_state != ACTIVE)
     {
-    case Gamepad::BUTTON_EVENT:
-        if (selectButtonPressed && __focusControl->_state != ACTIVE)
-        {
-            if (__activeControl[0])
-                __activeControl[0]->setDirty(DIRTY_STATE);
+        if (__activeControl[0])
+            __activeControl[0]->setDirty(DIRTY_STATE);
 
-            __activeControl[0] = __focusControl;
-            __focusControl->_state = ACTIVE;
-            __focusControl->notifyListeners(Control::Listener::PRESS);
-            return true;
-        }
-        else if (!selectButtonPressed && __focusControl->_state == ACTIVE)
-        {
-            if (__activeControl[0])
-                __activeControl[0]->setDirty(DIRTY_STATE);
+        __activeControl[0] = __focusControl;
+        __focusControl->_state = ACTIVE;
+        __focusControl->notifyListeners(Control::Listener::PRESS);
+        return true;
+    }
+    else if (!selectButtonPressed && __focusControl->_state == ACTIVE)
+    {
+        if (__activeControl[0])
+            __activeControl[0]->setDirty(DIRTY_STATE);
 
-            for (unsigned int i = 0; i < Touch::MAX_TOUCH_POINTS; ++i)
+        for (unsigned int i = 0; i < Touch::MAX_TOUCH_POINTS; ++i)
+        {
+            if (__activeControl[i] == __focusControl)
             {
-                if (__activeControl[i] == __focusControl)
-                {
-                    __activeControl[i] = NULL;
-                }
+                __activeControl[i] = NULL;
             }
-
-            __focusControl->_state = NORMAL;
-            __focusControl->notifyListeners(Control::Listener::RELEASE);
-            __focusControl->notifyListeners(Control::Listener::CLICK);
-            return true;
         }
-        break;
+
+        __focusControl->_state = NORMAL;
+        __focusControl->notifyListeners(Control::Listener::RELEASE);
+        __focusControl->notifyListeners(Control::Listener::CLICK);
+        return true;
     }
 
-    // Dispatch gamepad events to focused controls (or their parents)
-    Control * ctrl = __focusControl;
+    // Dispatch gamepad button events to focused controls (or their parents)
+    Control* ctrl = __focusControl;
     while (ctrl)
     {
         if (ctrl->isEnabled() && ctrl->isVisible())
         {
-            if (ctrl->gamepadEvent(evt, gamepad, analogIndex))
+            if (ctrl->gamepadButtonEvent(gamepad))
                 return true;
         }
 
         ctrl = ctrl->getParent();
     }
+    return false;
+}
+
+bool Form::gamepadTriggerEventInternal(Gamepad* gamepad, unsigned int index)
+{
+    // Dispatch gamepad trigger events to focused controls (or their parents)
+    Control* ctrl = __focusControl;
+    while (ctrl)
+    {
+        if (ctrl->isEnabled() && ctrl->isVisible())
+        {
+            if (ctrl->gamepadTriggerEvent(gamepad, index))
+                return true;
+        }
 
+        ctrl = ctrl->getParent();
+    }
     return false;
 }
 
+bool Form::gamepadJoystickEventInternal(Gamepad* gamepad, unsigned int index)
+{
+    // Dispatch gamepad joystick events to focused controls (or their parents)
+    Control* ctrl = __focusControl;
+    while (ctrl)
+    {
+        if (ctrl->isEnabled() && ctrl->isVisible())
+        {
+            if (ctrl->gamepadJoystickEvent(gamepad, index))
+                return true;
+        }
+
+        ctrl = ctrl->getParent();
+    }
+    return false;
+}
 void Form::resizeEventInternal(unsigned int width, unsigned int height)
 {
     for (size_t i = 0, size = __forms.size(); i < size; ++i)

+ 17 - 3
gameplay/src/Form.h

@@ -201,11 +201,25 @@ private:
     static bool mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheelDelta);
 
     /**
-     * Propagate gamepad events to enabled forms.
+     * Propagate gamepad button events to enabled forms.
      *
-     * @see Control::gamepadEvent
+     * @see Control::gamepadButtonEventInternal
      */
-    static bool gamepadEventInternal(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex);
+    static bool gamepadButtonEventInternal(Gamepad* gamepad);
+
+    /**
+     * Propagate gamepad trigger events to enabled forms.
+     *
+     * @see Control::gamepadTriggerEventInternal
+     */
+    static bool gamepadTriggerEventInternal(Gamepad* gamepad, unsigned int index);
+
+    /**
+     * Propagate gamepad button events to enabled forms.
+     *
+     * @see Control::gamepadJoystickEventInternal
+     */
+    static bool gamepadJoystickEventInternal(Gamepad* gamepad, unsigned int index);
 
     /**
      * Fired by the platform when the game window resizes.

+ 6 - 3
gameplay/src/Gamepad.cpp

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

+ 9 - 12
gameplay/src/Gamepad.h

@@ -32,17 +32,14 @@ public:
     enum GamepadEvent
     {
         CONNECTED_EVENT,
-        DISCONNECTED_EVENT,
-        BUTTON_EVENT,
-        JOYSTICK_EVENT,
-        TRIGGER_EVENT
+        DISCONNECTED_EVENT
     };
 
     /**
      * Gamepad buttons.
      */
     enum ButtonMapping
-    {
+    {        
         BUTTON_A,
         BUTTON_B,
         BUTTON_C,
@@ -192,9 +189,9 @@ private:
      * @param vendorString The vendor string/name.
      * @param productString The product string/name.
      */
-    Gamepad(GamepadHandle handle, 
-            unsigned int buttonCount, unsigned int joystickCount, unsigned int triggerCount,
-            unsigned int vendorId, unsigned int productId, const char* vendorString, const char* productString);
+    Gamepad(GamepadHandle handle, unsigned int buttonCount, unsigned int joystickCount, unsigned int triggerCount,
+            unsigned int vendorId, unsigned int productId, 
+            const char* vendorString, const char* productString);
 
     /**
      * Copy constructor.
@@ -208,9 +205,9 @@ private:
 
     static void updateInternal(float elapsedTime);
 
-    static Gamepad* add(GamepadHandle handle, 
-                        unsigned int buttonCount, unsigned int joystickCount, unsigned int triggerCount,
-                        unsigned int vendorId, unsigned int productId, const char* vendorString, const char* productString);
+    static Gamepad* add(GamepadHandle handle, unsigned int buttonCount, unsigned int joystickCount, unsigned int triggerCount,
+                        unsigned int vendorId, unsigned int productId, 
+                        const char* vendorString, const char* productString);
 
     static Gamepad* add(const char* formPath);
 
@@ -220,7 +217,7 @@ private:
 
     static unsigned int getGamepadCount();
 
-    static Gamepad* getGamepad(unsigned int index, bool preferPhysical = true);
+    static Gamepad* getGamepad(unsigned int index, bool preferPhysical);
 
     static Gamepad* getGamepad(GamepadHandle handle);
 

+ 30 - 10
gameplay/src/Platform.cpp

@@ -95,19 +95,9 @@ void Platform::resizeEventInternal(unsigned int width, unsigned int height)
         game->resizeEvent(width, height);
         game->getScriptController()->resizeEvent(width, height);
     }
-
     Form::resizeEventInternal(width, height);
 }
 
-void Platform::gamepadEventInternal(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex)
-{
-    if (!Form::gamepadEventInternal(evt, gamepad, analogIndex))
-    {
-        Game::getInstance()->gamepadEvent(evt, gamepad);
-        Game::getInstance()->getScriptController()->gamepadEvent(evt, gamepad);
-    }
-}
-
 void Platform::gamepadEventConnectedInternal(GamepadHandle handle,  unsigned int buttonCount, unsigned int joystickCount, unsigned int triggerCount,
                                              unsigned int vendorId, unsigned int productId, const char* vendorString, const char* productString)
 {
@@ -119,4 +109,34 @@ void Platform::gamepadEventDisconnectedInternal(GamepadHandle handle)
     Gamepad::remove(handle);
 }
 
+void Platform::gamepadButtonPressedEventInternal(GamepadHandle handle, Gamepad::ButtonMapping mapping)
+{
+    Gamepad* gamepad = Gamepad::getGamepad(handle);
+    unsigned int newButtons = gamepad->_buttons | (1 >> mapping);
+    gamepad->setButtons(newButtons);
+    Form::gamepadButtonEventInternal(gamepad);
+}
+
+void Platform::gamepadButtonReleasedEventInternal(GamepadHandle handle, Gamepad::ButtonMapping mapping)
+{
+    Gamepad* gamepad = Gamepad::getGamepad(handle);
+    unsigned int newButtons = gamepad->_buttons & ~(1 >> mapping);
+    gamepad->setButtons(newButtons);
+    Form::gamepadButtonEventInternal(gamepad);
+}
+
+void Platform::gamepadTriggerChangedEventInternal(GamepadHandle handle, unsigned int index, float value)
+{
+    Gamepad* gamepad = Gamepad::getGamepad(handle);
+    gamepad->setTriggerValue(index, value);
+    Form::gamepadTriggerEventInternal(gamepad, index);
+}
+
+void Platform::gamepadJoystickChangedEventInternal(GamepadHandle handle, unsigned int index, float x, float y)
+{
+    Gamepad* gamepad = Gamepad::getGamepad(handle);
+    gamepad->setJoystickValue(index, x, y);
+    Form::gamepadJoystickEventInternal(gamepad, index);
+}
+
 }

+ 30 - 7
gameplay/src/Platform.h

@@ -370,27 +370,50 @@ public:
      *
      * @script{ignore}
      */
-    static void gamepadEventInternal(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex = 0);
+    static void gamepadEventConnectedInternal(GamepadHandle handle, unsigned int buttonCount, unsigned int joystickCount, unsigned int triggerCount,
+                                              unsigned int vendorId, unsigned int productId,
+                                              const char* vendorString, const char* productString);
 
     /**
      * Internal method used only from static code in various platform implementation.
      *
      * @script{ignore}
      */
-    static void gamepadEventConnectedInternal(GamepadHandle handle, unsigned int buttonCount, unsigned int joystickCount, unsigned int triggerCount,
-                                              unsigned int vendorId, unsigned int productId,
-                                              const char* vendorString, const char* productString);
+    static void gamepadEventDisconnectedInternal(GamepadHandle handle);
 
     /**
      * Internal method used only from static code in various platform implementation.
      *
      * @script{ignore}
      */
-    static void gamepadEventDisconnectedInternal(GamepadHandle handle);
+    static void gamepadButtonPressedEventInternal(GamepadHandle handle, Gamepad::ButtonMapping mapping);
+
+    /**
+     * Internal method used only from static code in various platform implementation.
+     *
+     * @script{ignore}
+     */
+    static void gamepadButtonReleasedEventInternal(GamepadHandle handle, Gamepad::ButtonMapping button);
+
+    /**
+     * Internal method used only from static code in various platform implementation.
+     *
+     * @script{ignore}
+     */
+    static void gamepadTriggerChangedEventInternal(GamepadHandle handle, unsigned int index, float value);
+
+    /**
+    * Internal method used only from static code in various platform implementation.
+    *
+    * @script{ignore}
+    */
+    static void gamepadJoystickChangedEventInternal(GamepadHandle handle, unsigned int index, float x, float y);
 
     /**
-     * Internal method used by Gamepad that polls the platform for the updated Gamepad
-     * states such as joysticks, buttons and trigger values.
+     * Internal method used to poll the platform for the updated Gamepad
+     * states such as buttons, joytick and trigger values.
+     *
+     * Some platforms require to poll the gamepad system to get deltas. 
      *
      * @param gamepad The gamepad to be returned with the latest polled values populated.
      * @script{ignore}

+ 71 - 4
gameplay/src/PlatformAndroid.cpp

@@ -4,6 +4,7 @@
 #include "Platform.h"
 #include "FileSystem.h"
 #include "Game.h"
+#include "Gamepad.h"
 #include "Form.h"
 #include "ScriptController.h"
 #include <unistd.h>
@@ -1637,6 +1638,54 @@ std::string Platform::displayFileDialog(size_t mode, const char* title, const ch
     return "";
 }
 
+Gamepad::ButtonMapping getGamepadButtonMapping(jint keycode)
+{
+    switch (keycode)
+    {
+    case AKEYCODE_BUTTON_X:
+        return Gamepad::BUTTON_X;
+    case AKEYCODE_BUTTON_Y:
+        return Gamepad::BUTTON_Y;
+    case AKEYCODE_BUTTON_Z:
+        return Gamepad::BUTTON_Z;
+    case AKEYCODE_BUTTON_A:
+        return Gamepad::BUTTON_A;
+    case AKEYCODE_BUTTON_B:
+        return Gamepad::BUTTON_B;
+    case AKEYCODE_BUTTON_C:
+        return Gamepad::BUTTON_C;
+    case AKEYCODE_BUTTON_L1:
+        return Gamepad::BUTTON_L1;
+    case AKEYCODE_BUTTON_L2:
+        return Gamepad::BUTTON_L2;
+    case AKEYCODE_BUTTON_THUMBL:
+        return Gamepad::BUTTON_L3;
+    case AKEYCODE_BUTTON_R1:
+        return Gamepad::BUTTON_R1;
+    case AKEYCODE_BUTTON_R2:
+        return Gamepad::BUTTON_R2;
+    case AKEYCODE_BUTTON_THUMBR:
+        return Gamepad::BUTTON_R3;
+    case AKEYCODE_BUTTON_SELECT:
+        return Gamepad::BUTTON_MENU1;
+    case AKEYCODE_BUTTON_START:
+        return Gamepad::BUTTON_MENU2;
+    case AKEYCODE_BUTTON_MODE:
+        return Gamepad::BUTTON_MENU3;
+    case AKEYCODE_DPAD_UP:
+        return Gamepad::BUTTON_UP;
+    case AKEYCODE_DPAD_DOWN:
+        return Gamepad::BUTTON_DOWN;
+    case AKEYCODE_DPAD_LEFT:
+        return Gamepad::BUTTON_LEFT;
+    case AKEYCODE_DPAD_RIGHT:
+        return Gamepad::BUTTON_RIGHT;
+    default:
+        break;
+    }
+    return Gamepad::BUTTON_X;
+}
+
 }
 
 extern "C"
@@ -1644,14 +1693,32 @@ extern "C"
 
 JNIEXPORT void JNICALL Java_org_gameplay3d_GameNativeActivity_gamepadEventConnectedImpl(JNIEnv* env, jclass clazz, jint deviceId, jint buttonCount, jint joystickCount, jint triggerCount, jint vendorId, jint productId, jstring vendorIdStr, jstring productIdStr)
 {
-	// TODO
-	//gameplay::Platform::gamepadEventConnectedInternal(deviceId, buttonCount, joystickCount, triggerCount, vendorId, productId, "", "");
+	gameplay::Platform::gamepadEventConnectedInternal(deviceId, buttonCount, joystickCount, triggerCount, vendorId, productId, "", "");
 }
 
 JNIEXPORT void JNICALL Java_org_gameplay3d_GameNativeActivity_gamepadEventDisconnectedImpl(JNIEnv* env, jclass clazz, jint deviceId)
 {
-	// TODO
-	//gameplay::Platform::gamepadEventDisconnectedInternal(deviceId);
+	gameplay::Platform::gamepadEventDisconnectedInternal(deviceId);
+}
+
+JNIEXPORT void JNICALL Java_org_gameplay3d_GameNativeActivity_gamepadEventButtonPressedImpl(JNIEnv* env, jclass clazz, jint deviceId, jint keycode)
+{
+    gameplay::Platform::gamepadButtonPressedEventInternal(deviceId, gameplay::getGamepadButtonMapping(keycode));
+}
+
+JNIEXPORT void JNICALL Java_org_gameplay3d_GameNativeActivity_gamepadEventButtonReleasedImpl(JNIEnv* env, jclass clazz, jint deviceId, jint keycode)
+{
+    gameplay::Platform::gamepadButtonReleasedEventInternal(deviceId, gameplay::getGamepadButtonMapping(keycode));
+}
+
+JNIEXPORT void JNICALL Java_org_gameplay3d_GameNativeActivity_gamepadTriggerChangedEventImpl(JNIEnv* env, jclass clazz, jint deviceId, jint index, jfloat value)
+{
+    gameplay::Platform::gamepadTriggerChangedEventInternal(deviceId, index, value);
+}
+
+JNIEXPORT void JNICALL Java_org_gameplay3d_GameNativeActivity_gamepadJoystickChangedEventImpl(JNIEnv* env, jclass clazz, jint deviceId, jint index, jfloat x, jfloat y)
+{
+    gameplay::Platform::gamepadJoystickChangedEventInternal(deviceId, index, x, y);
 }
 
 }

+ 0 - 9
gameplay/src/ScriptController.cpp

@@ -885,13 +885,6 @@ void ScriptController::gestureDropEvent(int x, int y)
 {
 }
 
-void ScriptController::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex)
-{
-    std::vector<std::string>& list = _callbacks[GAMEPAD_EVENT];
-    for (size_t i = 0; i < list.size(); ++i)
-        executeFunction<void>(list[i].c_str(), "[Gamepad::GamepadEvent]<Gamepad>", evt, gamepad);
-}
-
 void ScriptController::executeFunctionHelper(int resultCount, const char* func, const char* args, va_list* list)
 {
 	if (!_lua)
@@ -1073,8 +1066,6 @@ ScriptController::ScriptCallback ScriptController::toCallback(const char* name)
         return ScriptController::GESTURE_PINCH_EVENT;
     else if (strcmp(name, "gestureTapEvent") == 0)
         return ScriptController::GESTURE_TAP_EVENT;
-    else if (strcmp(name, "gamepadEvent") == 0)
-        return ScriptController::GAMEPAD_EVENT;
     else
         return ScriptController::INVALID_CALLBACK;
 }

+ 1 - 9
gameplay/src/ScriptController.h

@@ -372,7 +372,7 @@ public:
      *
      * The 'callback' parameter must be one of the supported global callback
      * event functions. The following strings are accepted: initialize, finalize,
-     * update, render, resizeEvent, keyEvent, touchEvent, mouseEvent, gamepadEvent.
+     * update, render, resizeEvent, keyEvent, touchEvent, mouseEvent
      * Signatures for the registered script function must match that of the
      * corresponding signatures of these events on the Game class.
      *
@@ -925,14 +925,6 @@ private:
 	 */
 	void gestureDropEvent(int x, int y);
 
-    /**
-     * Script gamepad callback on gamepad events.
-     *
-     * @param evt The gamepad event that occurred.
-     * @param gamepad the gamepad the event occurred on
-     */
-    void gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex = 0);
-
     /**
      * Calls the specified Lua function using the given parameters.
      * 

+ 9 - 17
gameplay/src/Slider.cpp

@@ -252,26 +252,18 @@ bool Slider::mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
     return false;
 }
 
-bool Slider::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex)
+bool Slider::gamepadJoystickEvent(Gamepad* gamepad, unsigned int index)
 {
-    switch (evt)
+    // The right analog stick can be used to change a slider's value.
+    if (index == 1)
     {
-        case Gamepad::JOYSTICK_EVENT:
-        {
-            // The right analog stick can be used to change a slider's value.
-            if (analogIndex == 1)
-            {
-                Vector2 joy;
-                gamepad->getJoystickValues(analogIndex, &joy);
-                _gamepadValue = _value;
-                _delta = joy.x;
-                return true;
-            }
-            break;
-        }
+        Vector2 joy;
+        gamepad->getJoystickValues(index, &joy);
+        _gamepadValue = _value;
+        _delta = joy.x;
+        return true;
     }
-
-    return Label::gamepadEvent(evt, gamepad, analogIndex);
+    return Label::gamepadJoystickEvent(gamepad, index);
 }
 
 bool Slider::keyEvent(Keyboard::KeyEvent evt, int key)

+ 8 - 31
gameplay/src/Slider.h

@@ -180,47 +180,24 @@ protected:
     void initialize(const char* typeName, Theme::Style* style, Properties* properties);
 
     /**
-     * Touch callback on touch events.  Controls return true if they consume the touch event.
-     *
-     * @param evt The touch event that occurred.
-     * @param x The x position of the touch in pixels. Left edge is zero.
-     * @param y The y position of the touch in pixels. Top edge is zero.
-     * @param contactIndex The order of occurrence for multiple touch contacts starting at zero.
-     *
-     * @return Whether the touch event was consumed by the control.
-     *
-     * @see Touch::TouchEvent
+     * @see Control::KeyEvent
      */
-    bool touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
+    bool keyEvent(Keyboard::KeyEvent evt, int key);
 
     /**
-     * Mouse callback on mouse events.
-     *
-     * @param evt The mouse event that occurred.
-     * @param x The x position of the mouse in pixels. Left edge is zero.
-     * @param y The y position of the mouse in pixels. Top edge is zero.
-     * @param wheelDelta The number of mouse wheel ticks. Positive is up (forward), negative is down (backward).
-     *
-     * @return True if the mouse event is consumed or false if it is not consumed.
-     *
-     * @see Mouse::MouseEvent
+     * @see Control::TouchEvent
      */
-    bool mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta);
+    bool touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
 
     /**
-     * Gamepad callback on gamepad events.
-     *
-     * @see Control::gamepadEvent
+     * @see Control::MouseEvent
      */
-    bool gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex);
+    bool mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta);
 
     /**
-     * Keyboard callback on key events.
-     *
-     * @see Keyboard::KeyEvent
-     * @see Keyboard::Key
+     * @see Control::gamepadJoystickEvent
      */
-    bool keyEvent(Keyboard::KeyEvent evt, int key);
+    bool gamepadJoystickEvent(Gamepad* gamepad, unsigned int index);
 
     /**
      * @see Control::drawImages

+ 0 - 15
gameplay/src/lua/lua_GamepadGamepadEvent.cpp

@@ -8,9 +8,6 @@ static const char* enumStringEmpty = "";
 
 static const char* luaEnumString_GamepadGamepadEvent_CONNECTED_EVENT = "CONNECTED_EVENT";
 static const char* luaEnumString_GamepadGamepadEvent_DISCONNECTED_EVENT = "DISCONNECTED_EVENT";
-static const char* luaEnumString_GamepadGamepadEvent_BUTTON_EVENT = "BUTTON_EVENT";
-static const char* luaEnumString_GamepadGamepadEvent_JOYSTICK_EVENT = "JOYSTICK_EVENT";
-static const char* luaEnumString_GamepadGamepadEvent_TRIGGER_EVENT = "TRIGGER_EVENT";
 
 Gamepad::GamepadEvent lua_enumFromString_GamepadGamepadEvent(const char* s)
 {
@@ -18,12 +15,6 @@ Gamepad::GamepadEvent lua_enumFromString_GamepadGamepadEvent(const char* s)
         return Gamepad::CONNECTED_EVENT;
     if (strcmp(s, luaEnumString_GamepadGamepadEvent_DISCONNECTED_EVENT) == 0)
         return Gamepad::DISCONNECTED_EVENT;
-    if (strcmp(s, luaEnumString_GamepadGamepadEvent_BUTTON_EVENT) == 0)
-        return Gamepad::BUTTON_EVENT;
-    if (strcmp(s, luaEnumString_GamepadGamepadEvent_JOYSTICK_EVENT) == 0)
-        return Gamepad::JOYSTICK_EVENT;
-    if (strcmp(s, luaEnumString_GamepadGamepadEvent_TRIGGER_EVENT) == 0)
-        return Gamepad::TRIGGER_EVENT;
     return Gamepad::CONNECTED_EVENT;
 }
 
@@ -33,12 +24,6 @@ const char* lua_stringFromEnum_GamepadGamepadEvent(Gamepad::GamepadEvent e)
         return luaEnumString_GamepadGamepadEvent_CONNECTED_EVENT;
     if (e == Gamepad::DISCONNECTED_EVENT)
         return luaEnumString_GamepadGamepadEvent_DISCONNECTED_EVENT;
-    if (e == Gamepad::BUTTON_EVENT)
-        return luaEnumString_GamepadGamepadEvent_BUTTON_EVENT;
-    if (e == Gamepad::JOYSTICK_EVENT)
-        return luaEnumString_GamepadGamepadEvent_JOYSTICK_EVENT;
-    if (e == Gamepad::TRIGGER_EVENT)
-        return luaEnumString_GamepadGamepadEvent_TRIGGER_EVENT;
     return enumStringEmpty;
 }
 

+ 73 - 46
gameplay/src/org/gameplay3d/GameNativeActivity.java

@@ -10,6 +10,7 @@ import android.util.Log;
 import android.util.SparseArray;
 import android.view.InputDevice;
 import android.view.KeyEvent;
+import android.view.MotionEvent;
 import android.view.View;
 
 /**
@@ -22,97 +23,123 @@ import android.view.View;
  * android user-interface, life-cycle events for saving game state and custom plug-ins/extensions.
  */
 public class GameNativeActivity extends NativeActivity
-	implements InputManager.InputDeviceListener {
-	
-	static {
-		System.loadLibrary("gameplay");
-	}
-	
-	private static final String TAG = "GameNativeActivity";
-	
-	@Override
-	protected void onCreate(Bundle savedInstanceState) {
-		super.onCreate(savedInstanceState);
-		
-		_gamepadDevices = new SparseArray<InputDevice>();
-		_inputManager = (InputManager)getSystemService(Context.INPUT_SERVICE);
+    implements InputManager.InputDeviceListener {
+    
+    static {
+        System.loadLibrary("gameplay");
+    }
+    
+    private static final String TAG = "GameNativeActivity";
+    
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        
+        _gamepadDevices = new SparseArray<InputDevice>();
+        _inputManager = (InputManager)getSystemService(Context.INPUT_SERVICE);
 
-		View decorView = getWindow().getDecorView();
-		int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN;
-		if (Build.VERSION.SDK_INT >= 18) 
-			uiOptions ^= 0x00000800; //View.SYSTEM_UI_FLAG_IMMERSIVE;
-		decorView.setSystemUiVisibility(uiOptions);
-	}
-	
-	@Override
-	public void onSaveInstanceState(Bundle outState) {
-	    super.onSaveInstanceState(outState);
-	}
-	
-	@Override
+        View decorView = getWindow().getDecorView();
+        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN;
+        if (Build.VERSION.SDK_INT >= 18) 
+            uiOptions ^= 0x00000800; //View.SYSTEM_UI_FLAG_IMMERSIVE;
+        decorView.setSystemUiVisibility(uiOptions);
+    }
+    
+    @Override
+    public void onSaveInstanceState(Bundle outState) {
+        super.onSaveInstanceState(outState);
+    }
+    
+    @Override
     public void onConfigurationChanged(Configuration newConfig) {
-    	super.onConfigurationChanged(newConfig);
+        super.onConfigurationChanged(newConfig);
     } 
-	
+    
     @Override
     protected void onResume() {
-    	super.onResume();
+        super.onResume();
         _inputManager.registerInputDeviceListener(this, null);
         int[] ids = _inputManager.getInputDeviceIds();
         for (int i = 0; i < ids.length; i++) {
-        	getGamepadDevice(ids[i]);
+            getGamepadDevice(ids[i]);
         }
     }
     
     @Override
     protected void onPause() {
         _inputManager.unregisterInputDeviceListener(this);
-    	super.onPause();
+        super.onPause();
     }
     
     @Override
     public void onInputDeviceAdded(int deviceId) {
-    	Log.v(TAG, "Input Device added: " + deviceId);
+        Log.v(TAG, "Input Device added: " + deviceId);
         getGamepadDevice(deviceId);
     }
 
     @Override
     public void onInputDeviceRemoved(int deviceId) {
-    	Log.v(TAG, "Input Device removed: " + deviceId);
+        Log.v(TAG, "Input Device removed: " + deviceId);
         InputDevice device = _gamepadDevices.get(deviceId);
         if (device != null) {
-        	_gamepadDevices.remove(deviceId);
-        	//gamepadEventDisconnectedImpl(deviceId);
+            _gamepadDevices.remove(deviceId);
+            gamepadEventDisconnectedImpl(deviceId);
         }
     }
     
     @Override
     public void onInputDeviceChanged(int deviceId) {
-    	Log.v(TAG, "Input Device changed: " + deviceId);
+        Log.i(TAG, "Input Device changed: " + deviceId);
     }
     
     private InputDevice getGamepadDevice(int deviceId) {
-    	InputDevice device = _gamepadDevices.get(deviceId);
+        InputDevice device = _gamepadDevices.get(deviceId);
         if (device == null) {
             device = _inputManager.getInputDevice(deviceId);
             if (device == null)
-            	return null;
+                return null;
             if ((device.getSources() & InputDevice.SOURCE_GAMEPAD) != 0) {
-            	_gamepadDevices.put(deviceId, device);
-            	gamepadEventConnectedImpl(deviceId, 26, 2, 2, 0, 0, "", "");
+                _gamepadDevices.put(deviceId, device);
+                gamepadEventConnectedImpl(deviceId, 26, 2, 2, 0, 0, "", "");
             }
             if ((device.getSources() & InputDevice.SOURCE_JOYSTICK) != 0) {
-            	_gamepadDevices.put(deviceId, device);
-            	gamepadEventConnectedImpl(deviceId, 10, 0, 0, 0, 0, "", "");
+                _gamepadDevices.put(deviceId, device);
+                gamepadEventConnectedImpl(deviceId, 10, 0, 0, 0, 0, "", "");
             }
         }
         return device;
     }
-	
+    
+    @Override
+    public boolean dispatchKeyEvent(KeyEvent event) {
+        final int deviceId = event.getDeviceId();
+        final int keyCode = event.getKeyCode();
+        switch (event.getAction()) {
+            case KeyEvent.ACTION_DOWN:
+                gamepadButtonPressedEventImpl(deviceId, keyCode);
+                Log.i(TAG, "dispatchKeyEvent: " + deviceId + "," + keyCode);
+                return true;
+            case KeyEvent.ACTION_UP:
+                gamepadButtonReleasedEventImpl(deviceId, keyCode);
+                Log.i(TAG, "dispatchKeyEvent: " + deviceId + "," + keyCode);
+                return true;
+        }
+        return false;
+    }
+    
+    @Override
+    public boolean dispatchGenericMotionEvent(MotionEvent event) {
+        return false;
+    }
+    
     // JNI calls to PlatformAndroid.cpp
     private static native void gamepadEventConnectedImpl(int deviceId, int buttonCount, int joystickCount, int triggerCount, int vendorId, int productId, String vendorString, String productString);
     private static native void gamepadEventDisconnectedImpl(int deviceId);
+    private static native void gamepadButtonPressedEventImpl(int deviceId, int keycode);
+    private static native void gamepadButtonReleasedEventImpl(int deviceId, int keycode);
+    private static native void gamepadTriggerChangedEventImpl(int deviceId, int index, float value);
+    private static native void gamepadJoystickChangedEventImpl(int deviceId, int index, float x, float y);
     
-	private InputManager _inputManager;
-	private SparseArray<InputDevice> _gamepadDevices;
+    private InputManager _inputManager;
+    private SparseArray<InputDevice> _gamepadDevices;
 }