Browse Source

Changes to Gamepad API

setaylor 13 years ago
parent
commit
8069cdcd2a

+ 4 - 7
gameplay/src/Game.cpp

@@ -183,14 +183,11 @@ void Game::shutdown()
 		}
 		_scriptController->finalize();
 
-        std::vector<Gamepad*>* gamepads = Gamepad::getGamepads();
-        if (gamepads)
+        unsigned int gamepadCount = Gamepad::getGamepadCount();
+        for (unsigned int i = 0; i < gamepadCount; i++)
         {
-            for (size_t i = 0, count = gamepads->size(); i < count; ++i)
-            {
-                SAFE_DELETE((*gamepads)[i]);
-            }
-            gamepads->clear();
+            Gamepad* gamepad = Gamepad::getGamepad(i);
+            SAFE_DELETE(gamepad);
         }
 
         _animationController->finalize();

+ 19 - 0
gameplay/src/Game.h

@@ -440,6 +440,25 @@ public:
      */
     virtual void gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad);
 
+    /**
+     * Gets the current number of gamepads currently connected to the system.
+     *
+     * @return The number of gamepads currently connected to the system.
+     */
+    inline unsigned int getGamepadCount() const;
+
+    /**
+     * Gets the gamepad at the specified index. 
+     *
+     * The gamepad index can change when connnected and disconnected so you
+     * cannot relie on this other than iterating through them all to display
+     * them or poll them.
+     *
+     * @param index The index of the gamepad to retrieve.
+     * @return The gamepad at the specified index.
+     */
+    inline Gamepad* getGamepad(unsigned int index) const;
+
     /**
      * Sets multi-touch is to be enabled/disabled. Default is disabled.
      *

+ 10 - 0
gameplay/src/Game.inl

@@ -116,6 +116,16 @@ inline void Game::getAccelerometerValues(float* pitch, float* roll)
     Platform::getAccelerometerValues(pitch, roll);
 }
 
+inline unsigned int Game::getGamepadCount() const
+{
+    return Gamepad::getGamepadCount();
+}
+
+inline Gamepad* Game::getGamepad(unsigned int index) const
+{
+    return Gamepad::getGamepad(index);
+}
+
 inline void Game::displayKeyboard(bool display)
 {
     Platform::displayKeyboard(display);

+ 13 - 27
gameplay/src/Gamepad.cpp

@@ -9,7 +9,7 @@ namespace gameplay
 static std::vector<Gamepad*> __gamepads;
 
 Gamepad::Gamepad(const char* formPath)
-    : _id(""), _handle(0), _vendorId(0), _productId(0), _buttonCount(0), _joystickCount(0), _triggerCount(0), _form(NULL)
+    : _handle(0), _vendorId(0), _productId(0), _buttonCount(0), _joystickCount(0), _triggerCount(0), _form(NULL), _buttons(0)
 {
     GP_ASSERT(formPath);
     _form = Form::create(formPath);
@@ -32,10 +32,10 @@ Gamepad::Gamepad(const char* formPath)
     bindGamepadControls(_form);
 }
 
-Gamepad::Gamepad(const char* id, GamepadHandle handle, unsigned int buttonCount, unsigned int joystickCount, unsigned int triggerCount,
+Gamepad::Gamepad(GamepadHandle handle, unsigned int buttonCount, unsigned int joystickCount, unsigned int triggerCount,
                  unsigned int vendorId, unsigned int productId, const char* vendorString, const char* productString)
-    : _id(id), _handle(handle), _vendorId(vendorId), _productId(productId), _vendorString(vendorString), _productString(productString),
-      _buttonCount(buttonCount), _joystickCount(joystickCount), _triggerCount(triggerCount), _form(NULL)
+    : _handle(handle), _vendorId(vendorId), _productId(productId), _vendorString(vendorString), _productString(productString),
+      _buttonCount(buttonCount), _joystickCount(joystickCount), _triggerCount(triggerCount), _form(NULL), _buttons(0)
 {
 }
 
@@ -47,10 +47,11 @@ Gamepad::~Gamepad()
     }
 }
 
-Gamepad* Gamepad::add(const char* id, GamepadHandle handle, unsigned int buttonCount, unsigned int joystickCount, unsigned int triggerCount,
-    unsigned int vendorId, unsigned int productId, const char* vendorString, const char* productString)
+Gamepad* 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)
 {
-    Gamepad* gamepad = new Gamepad(id, handle, buttonCount, joystickCount, triggerCount, vendorId, productId, vendorString, productString);
+    Gamepad* gamepad = new Gamepad(handle, buttonCount, joystickCount, triggerCount, vendorId, productId, vendorString, productString);
 
     __gamepads.push_back(gamepad);
     Game::getInstance()->gamepadEvent(CONNECTED_EVENT, gamepad);
@@ -127,28 +128,18 @@ void Gamepad::bindGamepadControls(Container* container)
             Button* button = (Button*)control;
             _uiButtons[button->getDataBinding()] = button;
             _buttonCount++;
-        }   
+        }
     }
 }
 
-Gamepad* Gamepad::getGamepad(GamepadHandle handle)
+unsigned int Gamepad::getGamepadCount()
 {
-    std::vector<Gamepad*>::const_iterator it;
-    for (it = __gamepads.begin(); it != __gamepads.end(); it++)
-    {
-        Gamepad* gamepad = *it;
-        if (!gamepad->isVirtual() && gamepad->_handle == handle)
-        {
-            return gamepad;
-        }
-    }
-
-    return NULL;
+    return __gamepads.size();
 }
 
-std::vector<Gamepad*>* Gamepad::getGamepads()
+Gamepad* Gamepad::getGamepad(unsigned int index)
 {
-    return &__gamepads;
+    return __gamepads[index];
 }
 
 Gamepad::ButtonMapping Gamepad::getButtonMappingFromString(const char* string)
@@ -198,11 +189,6 @@ Gamepad::ButtonMapping Gamepad::getButtonMappingFromString(const char* string)
     return BUTTON_A;
 }
 
-const char* Gamepad::getId() const
-{
-    return _id.c_str();
-}
-
 const unsigned int Gamepad::getVendorId() const
 {
     return _vendorId;

+ 43 - 54
gameplay/src/Gamepad.h

@@ -58,48 +58,6 @@ public:
         BUTTON_RIGHT
     };
 
-    /**
-     * Get all connected gamepads.
-     *
-     * @return A vector of all connected gamepads.
-     */
-    static std::vector<Gamepad*>* getGamepads();
-
-    /**
-     * Get this gamepad's ID as a string.
-     *
-     * @return This gamepad's ID as a string.
-     */
-    const char* getId() const;
-
-    /**
-     * Get this gamepad's vendor ID.
-     *
-     * @return This gamepad's vendor ID.
-     */
-    const unsigned int getVendorId() const;
-
-    /**
-     * Get this gamepad's product ID.
-     *
-     * @return This gamepad's product ID.
-     */
-    const unsigned int getProductId() const;
-
-    /**
-     * Get this gamepad's vendor name.
-     *
-     * @return This gamepad's vendor name.
-     */
-    const char* getVendorString() const;
-
-    /**
-     * Get this gamepad's product name.
-     *
-     * @return This gamepad's product name.
-     */
-    const char* getProductString() const;
-
     /**
      * Gets the number of buttons on this gamepad.
      *
@@ -148,6 +106,34 @@ public:
      */
     float getTriggerValue(unsigned int triggerId) const;
 
+   /**
+     * Get this gamepad's vendor ID.
+     *
+     * @return This gamepad's vendor ID.
+     */
+    const unsigned int getVendorId() const;
+
+    /**
+     * Get this gamepad's product ID.
+     *
+     * @return This gamepad's product ID.
+     */
+    const unsigned int getProductId() const;
+
+    /**
+     * Get this gamepad's vendor name.
+     *
+     * @return This gamepad's vendor name.
+     */
+    const char* getVendorString() const;
+
+    /**
+     * Get this gamepad's product name.
+     *
+     * @return This gamepad's product name.
+     */
+    const char* getProductString() const;
+
     /**
      * Returns whether the gamepad is currently represented with a UI form or not.
      *
@@ -191,36 +177,39 @@ private:
     /**
      * Constructs a physical gamepad.
      *
-     * @param id The gamepad's id.
      * @param handle The gamepad handle
      * @param buttonCount the number of buttons on the gamepad. 
      * @param joystickCount the number of joysticks on the gamepad.
      * @param triggerCount the number of triggers on the gamepad.
+     * @param vendorId The vendor id
+     * @param productId The product id
+     * @param vendorString The vendor string/name.
+     * @param productString The product string/name.
      */
-    Gamepad(const char* id, 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.
      */
     Gamepad(const Gamepad& copy);
 
-    /**
-     * Used by platforms to add gamepads to the set available to games.
-     */
-    static Gamepad* add(const char* id, 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);
 
-    /**
-     * Used by Game::loadGamepads() to add virtual gamepads from a game's config file.
-     */
     static Gamepad* add(const char* formPath);
 
     static void remove(GamepadHandle handle);
 
     static void remove(Gamepad* gamepad);
 
-    static Gamepad* getGamepad(GamepadHandle handle);
+    static unsigned int getGamepadCount();
+
+    static Gamepad* getGamepad(unsigned int index);
 
     static ButtonMapping getButtonMappingFromString(const char* string);
 

+ 27 - 4
gameplay/src/Platform.h

@@ -238,6 +238,7 @@ public:
      * @param contactIndex An integer to identify this contact point within the currently active touch set.
      *
      * @see Touch::TouchEvent
+     * @script{ignore}
      */
     static void touchEventInternal(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
 
@@ -250,6 +251,7 @@ public:
      * 
      * @see Keyboard::KeyEvent
      * @see Keyboard::Key
+     * @script{ignore}
      */
     static void keyEventInternal(Keyboard::KeyEvent evt, int key);
 
@@ -265,18 +267,39 @@ public:
      * @return True if the mouse event is consumed or false if it is not consumed.
      *
      * @see Mouse::MouseEvent
+     * @script{ignore}
      */
     static bool mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheelDelta);
 
     /**
-     * Gamepad callback on gamepad events. This is called only from platform when gamepad are connectd and disconnected.
+     * Gamepad callback from platform on gamepad events when a gamepad is connected.
      *
-     * @param evt The mouse event that occurred.
-     * @param gamepad The gamepad that is either connected or disconnected from the platform.
+     * @param handle The gamepad handle
+     * @param buttonCount The number of buttons
+     * @param joystickCount The number of joysticks
+     * @param triggerCount The number of triggers
+     * @param vendorId The vendor id
+     * @param productId The product id
+     * @param vendorString The vendor string/name.
+     * @param productString The product string/name.
+     *
+     * @see Gamepad::GamepadEvent
+     * @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);
+
+    /**
+     * Gamepad callback from platform on gamepad events when a gamepad is disconnected.
+     *
+     * @param handle The gamepad handle
      *
      * @see Gamepad::GamepadEvent
+     * @script{ignore}
      */
-    static void gamepadEventInternal(Gamepad::GamepadEvent evt, Gamepad* gamepad);
+    static void gamepadEventDisconnectedInternal(GamepadHandle handle);
 
     /**
      * Opens an URL in an external browser, if available.

+ 8 - 1
gameplay/src/PlatformAndroid.cpp

@@ -1252,8 +1252,15 @@ bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheel
     }
 }
 
-void Platform::gamepadEventInternal(Gamepad::GamepadEvent evt, Gamepad* 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)
 {
+    Gamepad::add(GamepadHandle handle, buttonCount, joystickCount, triggerCount, vendorId, productId, vendorString, productString);
+}
+
+void Platform::gamepadEventDisconnectedInternal(GamepadHandle handle)
+{
+    Gamepad::remove(handle);
 }
 
 bool Platform::isGestureSupported(Gesture::GestureEvent evt)

+ 21 - 30
gameplay/src/PlatformBlackBerry.cpp

@@ -521,8 +521,9 @@ static const int __PIDs[] = {
 
 static const unsigned int __knownGamepads = 3;
 
-void loadGamepad(GamepadHandle handle, int* buttonCount, int* joystickCount, int* productId, int* vendorId, char* id, char* productString, char* vendorString)
+void queryGamepad(GamepadHandle handle, int* buttonCount, int* joystickCount, int* productId, int* vendorId, char* productString, char* vendorString)
 {
+    char id[128];
     screen_get_device_property_iv(handle, SCREEN_PROPERTY_BUTTON_COUNT, buttonCount);
     screen_get_device_property_cv(handle, SCREEN_PROPERTY_ID_STRING, 128, id);
     screen_get_device_property_cv(handle, SCREEN_PROPERTY_PRODUCT, 64, productString);
@@ -626,10 +627,6 @@ void Platform::pollGamepadState(Gamepad* gamepad)
         gamepad->_triggers[i] = value;
     }
 }
-#else
-void Platform::getGamepadButtonValues(GamepadHandle handle, unsigned int* out) { }
-void Platform::getGamepadJoystickValues(GamepadHandle handle, unsigned int joystickIndex, Vector2* outValue) { }
-void Platform::getGamepadTriggerValue(GamepadHandle handle, unsigned int triggerIndex, float* out) { }
 #endif
 
 Platform::Platform(Game* game)
@@ -975,13 +972,15 @@ Platform* Platform::create(Game* game, void* attachToWindow)
         glIsVertexArray = (PFNGLISVERTEXARRAYOESPROC)eglGetProcAddress("glIsVertexArrayOES");
     }
 
-    // Discover gamepad devices.
+ #ifdef BLACKBERRY_USE_GAMEPAD
+    // Discover initial gamepad devices.
     int count;
     screen_get_context_property_iv(__screenContext, SCREEN_PROPERTY_DEVICE_COUNT, &count);
     screenDevs = (screen_device_t*)calloc(count, sizeof(screen_device_t));
     screen_get_context_property_pv(__screenContext, SCREEN_PROPERTY_DEVICES, (void**)screenDevs);
 
-	for (int i = 0; i < count; i++) {
+	for (int i = 0; i < count; i++) 
+    {
 	    int type;
         screen_get_device_property_iv(screenDevs[i], SCREEN_PROPERTY_TYPE, &type);
 
@@ -991,21 +990,19 @@ Platform* Platform::create(Game* game, void* attachToWindow)
             int joystickCount = 0;
             int productId;
             int vendorId;
-            char id[128];
             char productString[64];
             char vendorString[64];
-            loadGamepad(screenDevs[i], &buttonCount, &joystickCount, &productId, &vendorId, id, productString, vendorString);
-            Gamepad::add(id, screenDevs[i], buttonCount, joystickCount, 0, vendorId, productId, vendorString, productString);
+            queryGamepad(screenDevs[i], &buttonCount, &joystickCount, &productId, &vendorId, productString, vendorString);
+            Platform::gamepadEventConnectedInternal(screenDevs[i], buttonCount, joystickCount, 0, vendorId, productId, vendorString, productString);
         }
 	}
 	free(screenDevs);
+#endif
 
     return platform;
 
 error:
 
-    // TODO: cleanup
-
     return NULL;
 }
 
@@ -1223,7 +1220,7 @@ int Platform::enterMessagePump()
                         }
                         break;
                     }
-#ifdef __BB10__
+#ifdef BLACKBERRY_USE_GAMEPAD
                     case SCREEN_EVENT_DEVICE:
                     {
                         // A device was attached or removed.
@@ -1243,16 +1240,15 @@ int Platform::enterMessagePump()
                                 int joystickCount = 0;
                                 int productId;
                                 int vendorId;
-                                char id[128];
                                 char productString[64];
                                 char vendorString[64];
-                                loadGamepad(device, &buttonCount, &joystickCount, &productId, &vendorId, id, productString, vendorString);
-                                Gamepad::add(id, device, buttonCount, joystickCount, 0, vendorId, productId, vendorString, productString);
+                                loadGamepad(device, &buttonCount, &joystickCount, &productId, &vendorId, productString, vendorString);
+                                Platform::gamepadEventConnectedInternal(device, buttonCount, joystickCount, 0, vendorId, productId, vendorString, productString);
                             }
                         }
                         else
                         {
-                            Gamepad::remove(device);
+                            Platform::gamepadEventDisconnectedInternal(device);
                         }
 
                         break;
@@ -1521,20 +1517,15 @@ bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheel
     }
 }
 
-void Platform::gamepadEventInternal(Gamepad::GamepadEvent evt, Gamepad* 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)
 {
-    if (evt == Gamepad::CONNECTED_EVENT)
-    {
-        Gamepad::add(gamepad->_id.c_str(), 
-                     gamepad->_handle, 
-                     gamepad->_buttonCount, gamepad->_joystickCount, gamepad->_triggerCount,
-                     gamepad->_vendorId, gamepad->_productId, 
-                     gamepad->_vendorString.c_str(), gamepad->_productString.c_str());
-    }
-    else if (evt == Gamepad::DISCONNECTED_EVENT) 
-    {
-        Gamepad::remove(gamepad);
-    }
+    Gamepad::add(GamepadHandle handle, buttonCount, joystickCount, triggerCount, vendorId, productId, vendorString, productString);
+}
+
+void Platform::gamepadEventDisconnectedInternal(GamepadHandle handle)
+{
+    Gamepad::remove(handle);
 }
 
 bool Platform::isGestureSupported(Gesture::GestureEvent evt)

+ 9 - 2
gameplay/src/PlatformLinux.cpp

@@ -1119,8 +1119,15 @@ bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheel
     }
 }
 
-void Platform::gamepadEventInternal(Gamepad::GamepadEvent evt, Gamepad* 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)
+{
+    Gamepad::add(handle, buttonCount, joystickCount, triggerCount, vendorId, productId, vendorString, productString);
+}
+
+void Platform::gamepadEventDisconnectedInternal(GamepadHandle handle)
+{
+    Gamepad::remove(handle);
 }
 
 bool Platform::isGestureSupported(Gesture::GestureEvent evt)

+ 8 - 17
gameplay/src/PlatformMacOSX.mm

@@ -613,13 +613,10 @@ double getMachTimeInMilliseconds()
             break;
         }
     }
-
 }
 
-
 @end
 
-
 @interface View : NSOpenGLView <NSWindowDelegate>
 {
 @public
@@ -1716,20 +1713,15 @@ bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheel
     return result;
 }
 
-void Platform::gamepadEventInternal(Gamepad::GamepadEvent evt, Gamepad* 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)
 {
-    if (evt == Gamepad::CONNECTED_EVENT)
-    {
-        Gamepad::add(gamepad->_id.c_str(), 
-                     gamepad->_handle, 
-                     gamepad->_buttonCount, gamepad->_joystickCount, gamepad->_triggerCount,
-                     gamepad->_vendorId, gamepad->_productId, 
-                     gamepad->_vendorString.c_str(), gamepad->_productString.c_str());
-    }
-    else if (evt == Gamepad::DISCONNECTED_EVENT) 
-    {
-        Gamepad::remove(gamepad);
-    }
+    Gamepad::add(handle, buttonCount, joystickCount, triggerCount, vendorId, productId, vendorString, productString);
+}
+
+void Platform::gamepadEventDisconnectedInternal(GamepadHandle handle)
+{
+    Gamepad::remove(handle);
 }
 
 bool Platform::isGestureSupported(Gesture::GestureEvent evt)
@@ -1770,7 +1762,6 @@ void Platform::pollGamepadState(Gamepad* gamepad)
 
 }
 
-
 OSXGamepad* gamepadForLocationID(NSNumber* locationID)
 {
     OSXGamepad* fgamepad = NULL;

+ 21 - 12
gameplay/src/PlatformWindows.cpp

@@ -47,6 +47,7 @@ static const unsigned int XINPUT_TRIGGER_COUNT = 2;
 
 #ifdef USE_XINPUT
 static XINPUT_STATE __xInputState;
+static bool __connectedXInput[4];
 
 static float normalizeXInputJoystickAxis(int axisValue, int deadZone)
 {
@@ -910,11 +911,13 @@ Platform* Platform::create(Game* game, void* attachToWindow)
     {
         if (XInputGetState(i, &__xInputState) == NO_ERROR)
         {
-            // Gamepad is connected.
-            char id[9];
-            sprintf(id, "XInput %d", i);
-            Gamepad::add(id, i, XINPUT_BUTTON_COUNT, XINPUT_JOYSTICK_COUNT, XINPUT_TRIGGER_COUNT,
-                         0, 0, "Unknown", "XInput Gamepad");
+            if (!__connectedXInput[i])
+            {
+                // Gamepad is connected.
+                Platform::gamepadEventConnectedInternal(i, XINPUT_BUTTON_COUNT, XINPUT_JOYSTICK_COUNT, XINPUT_TRIGGER_COUNT, 0, 0, "Microsoft", "XBox360 Controller");
+                __connectedXInput[i] = true;
+            }
+
         }
     }
 #endif
@@ -973,13 +976,11 @@ int Platform::enterMessagePump()
             // Check for connected XInput gamepads.
             for (DWORD i = 0; i < XUSER_MAX_COUNT; i++)
             {
-                if (XInputGetState(i, &__xInputState) == NO_ERROR && !Gamepad::getGamepad(i))
+                if (XInputGetState(i, &__xInputState) == NO_ERROR && !__connectedXInput[i])
                 {
                     // Gamepad was just connected.
-                    char id[9];
-                    sprintf(id, "XInput %d", i);
-                    Gamepad::add(id, i, XINPUT_BUTTON_COUNT, XINPUT_JOYSTICK_COUNT, XINPUT_TRIGGER_COUNT,
-                                    0, 0, "Unknown", "XInput Gamepad");
+                    Platform::gamepadEventConnectedInternal(i, XINPUT_BUTTON_COUNT, XINPUT_JOYSTICK_COUNT, XINPUT_TRIGGER_COUNT, 0, 0, "Microsoft", "XBox360 Controller");
+                    __connectedXInput[i] = true;
                 }
             }
 #endif
@@ -1234,7 +1235,8 @@ void Platform::pollGamepadState(Gamepad* gamepad)
     else
     {
         // Gamepad was disconnected.
-        Gamepad::remove(gamepad);
+        Platform::gamepadEventDisconnectedInternal(gamepad->_handle);
+        __connectedXInput[gamepad->_handle] = false;
     }
 }
 #else
@@ -1278,8 +1280,15 @@ bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheel
     }
 }
 
-void Platform::gamepadEventInternal(Gamepad::GamepadEvent evt, Gamepad* 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)
+{
+    Gamepad::add(handle, buttonCount, joystickCount, triggerCount, vendorId, productId, vendorString, productString);
+}
+
+void Platform::gamepadEventDisconnectedInternal(GamepadHandle handle)
 {
+    Gamepad::remove(handle);
 }
 
 bool Platform::launchURL(const char* url)

+ 8 - 1
gameplay/src/PlatformiOS.mm

@@ -1390,8 +1390,15 @@ bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheel
     }
 }
 
-void Platform::gamepadEventInternal(Gamepad::GamepadEvent evt, Gamepad* 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)
 {
+    Gamepad::add(handle, buttonCount, joystickCount, triggerCount, vendorId, productId, vendorString, productString);
+}
+
+void Platform::gamepadEventDisconnectedInternal(GamepadHandle handle)
+{
+    Gamepad::remove(handle);
 }
 
 bool Platform::isGestureSupported(Gesture::GestureEvent evt)

+ 85 - 0
gameplay/src/lua/lua_Game.cpp

@@ -40,6 +40,8 @@ void luaRegister_Game()
         {"getAudioListener", lua_Game_getAudioListener},
         {"getConfig", lua_Game_getConfig},
         {"getFrameRate", lua_Game_getFrameRate},
+        {"getGamepad", lua_Game_getGamepad},
+        {"getGamepadCount", lua_Game_getGamepadCount},
         {"getHeight", lua_Game_getHeight},
         {"getPhysicsController", lua_Game_getPhysicsController},
         {"getScriptController", lua_Game_getScriptController},
@@ -867,6 +869,89 @@ int lua_Game_getFrameRate(lua_State* state)
     return 0;
 }
 
+int lua_Game_getGamepad(lua_State* state)
+{
+    // Get the number of parameters.
+    int paramCount = lua_gettop(state);
+
+    // Attempt to match the parameters to a valid binding.
+    switch (paramCount)
+    {
+        case 2:
+        {
+            if ((lua_type(state, 1) == LUA_TUSERDATA) &&
+                lua_type(state, 2) == LUA_TNUMBER)
+            {
+                // Get parameter 1 off the stack.
+                unsigned int param1 = (unsigned int)luaL_checkunsigned(state, 2);
+
+                Game* instance = getInstance(state);
+                void* returnPtr = (void*)instance->getGamepad(param1);
+                if (returnPtr)
+                {
+                    ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
+                    object->instance = returnPtr;
+                    object->owns = false;
+                    luaL_getmetatable(state, "Gamepad");
+                    lua_setmetatable(state, -2);
+                }
+                else
+                {
+                    lua_pushnil(state);
+                }
+
+                return 1;
+            }
+
+            lua_pushstring(state, "lua_Game_getGamepad - Failed to match the given parameters to a valid function signature.");
+            lua_error(state);
+            break;
+        }
+        default:
+        {
+            lua_pushstring(state, "Invalid number of parameters (expected 2).");
+            lua_error(state);
+            break;
+        }
+    }
+    return 0;
+}
+
+int lua_Game_getGamepadCount(lua_State* state)
+{
+    // Get the number of parameters.
+    int paramCount = lua_gettop(state);
+
+    // Attempt to match the parameters to a valid binding.
+    switch (paramCount)
+    {
+        case 1:
+        {
+            if ((lua_type(state, 1) == LUA_TUSERDATA))
+            {
+                Game* instance = getInstance(state);
+                unsigned int result = instance->getGamepadCount();
+
+                // Push the return value onto the stack.
+                lua_pushunsigned(state, result);
+
+                return 1;
+            }
+
+            lua_pushstring(state, "lua_Game_getGamepadCount - Failed to match the given parameters to a valid function signature.");
+            lua_error(state);
+            break;
+        }
+        default:
+        {
+            lua_pushstring(state, "Invalid number of parameters (expected 1).");
+            lua_error(state);
+            break;
+        }
+    }
+    return 0;
+}
+
 int lua_Game_getHeight(lua_State* state)
 {
     // Get the number of parameters.

+ 2 - 0
gameplay/src/lua/lua_Game.h

@@ -23,6 +23,8 @@ int lua_Game_getAudioController(lua_State* state);
 int lua_Game_getAudioListener(lua_State* state);
 int lua_Game_getConfig(lua_State* state);
 int lua_Game_getFrameRate(lua_State* state);
+int lua_Game_getGamepad(lua_State* state);
+int lua_Game_getGamepadCount(lua_State* state);
 int lua_Game_getHeight(lua_State* state);
 int lua_Game_getPhysicsController(lua_State* state);
 int lua_Game_getScriptController(lua_State* state);

+ 1 - 78
gameplay/src/lua/lua_Gamepad.cpp

@@ -17,7 +17,6 @@ void luaRegister_Gamepad()
         {"draw", lua_Gamepad_draw},
         {"getButtonCount", lua_Gamepad_getButtonCount},
         {"getForm", lua_Gamepad_getForm},
-        {"getId", lua_Gamepad_getId},
         {"getJoystickCount", lua_Gamepad_getJoystickCount},
         {"getJoystickValues", lua_Gamepad_getJoystickValues},
         {"getProductId", lua_Gamepad_getProductId},
@@ -31,11 +30,7 @@ void luaRegister_Gamepad()
         {"update", lua_Gamepad_update},
         {NULL, NULL}
     };
-    const luaL_Reg lua_statics[] = 
-    {
-        {"getGamepads", lua_Gamepad_static_getGamepads},
-        {NULL, NULL}
-    };
+    const luaL_Reg* lua_statics = NULL;
     std::vector<std::string> scopePath;
 
     ScriptUtil::registerClass("Gamepad", lua_members, NULL, NULL, lua_statics, scopePath);
@@ -159,41 +154,6 @@ int lua_Gamepad_getForm(lua_State* state)
     return 0;
 }
 
-int lua_Gamepad_getId(lua_State* state)
-{
-    // Get the number of parameters.
-    int paramCount = lua_gettop(state);
-
-    // Attempt to match the parameters to a valid binding.
-    switch (paramCount)
-    {
-        case 1:
-        {
-            if ((lua_type(state, 1) == LUA_TUSERDATA))
-            {
-                Gamepad* instance = getInstance(state);
-                const char* result = instance->getId();
-
-                // Push the return value onto the stack.
-                lua_pushstring(state, result);
-
-                return 1;
-            }
-
-            lua_pushstring(state, "lua_Gamepad_getId - Failed to match the given parameters to a valid function signature.");
-            lua_error(state);
-            break;
-        }
-        default:
-        {
-            lua_pushstring(state, "Invalid number of parameters (expected 1).");
-            lua_error(state);
-            break;
-        }
-    }
-    return 0;
-}
-
 int lua_Gamepad_getJoystickCount(lua_State* state)
 {
     // Get the number of parameters.
@@ -563,43 +523,6 @@ int lua_Gamepad_isVirtual(lua_State* state)
     return 0;
 }
 
-int lua_Gamepad_static_getGamepads(lua_State* state)
-{
-    // Get the number of parameters.
-    int paramCount = lua_gettop(state);
-
-    // Attempt to match the parameters to a valid binding.
-    switch (paramCount)
-    {
-        case 0:
-        {
-            void* returnPtr = (void*)Gamepad::getGamepads();
-            if (returnPtr)
-            {
-                ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
-                object->instance = returnPtr;
-                object->owns = false;
-                luaL_getmetatable(state, "Gamepad");
-                lua_setmetatable(state, -2);
-            }
-            else
-            {
-                lua_pushnil(state);
-            }
-
-            return 1;
-            break;
-        }
-        default:
-        {
-            lua_pushstring(state, "Invalid number of parameters (expected 0).");
-            lua_error(state);
-            break;
-        }
-    }
-    return 0;
-}
-
 int lua_Gamepad_update(lua_State* state)
 {
     // Get the number of parameters.

+ 0 - 2
gameplay/src/lua/lua_Gamepad.h

@@ -8,7 +8,6 @@ namespace gameplay
 int lua_Gamepad_draw(lua_State* state);
 int lua_Gamepad_getButtonCount(lua_State* state);
 int lua_Gamepad_getForm(lua_State* state);
-int lua_Gamepad_getId(lua_State* state);
 int lua_Gamepad_getJoystickCount(lua_State* state);
 int lua_Gamepad_getJoystickValues(lua_State* state);
 int lua_Gamepad_getProductId(lua_State* state);
@@ -19,7 +18,6 @@ int lua_Gamepad_getVendorId(lua_State* state);
 int lua_Gamepad_getVendorString(lua_State* state);
 int lua_Gamepad_isButtonDown(lua_State* state);
 int lua_Gamepad_isVirtual(lua_State* state);
-int lua_Gamepad_static_getGamepads(lua_State* state);
 int lua_Gamepad_update(lua_State* state);
 
 void luaRegister_Gamepad();

+ 0 - 185
gameplay/src/lua/lua_Platform.cpp

@@ -2,11 +2,7 @@
 #include "ScriptController.h"
 #include "lua_Platform.h"
 #include "Platform.h"
-#include "lua_GamepadGamepadEvent.h"
 #include "lua_GestureGestureEvent.h"
-#include "lua_KeyboardKeyEvent.h"
-#include "lua_MouseMouseEvent.h"
-#include "lua_TouchTouchEvent.h"
 
 namespace gameplay
 {
@@ -22,7 +18,6 @@ void luaRegister_Platform()
     {
         {"canExit", lua_Platform_static_canExit},
         {"displayKeyboard", lua_Platform_static_displayKeyboard},
-        {"gamepadEventInternal", lua_Platform_static_gamepadEventInternal},
         {"getAbsoluteTime", lua_Platform_static_getAbsoluteTime},
         {"getAccelerometerValues", lua_Platform_static_getAccelerometerValues},
         {"getDisplayHeight", lua_Platform_static_getDisplayHeight},
@@ -34,9 +29,7 @@ void luaRegister_Platform()
         {"isMouseCaptured", lua_Platform_static_isMouseCaptured},
         {"isMultiTouch", lua_Platform_static_isMultiTouch},
         {"isVsync", lua_Platform_static_isVsync},
-        {"keyEventInternal", lua_Platform_static_keyEventInternal},
         {"launchURL", lua_Platform_static_launchURL},
-        {"mouseEventInternal", lua_Platform_static_mouseEventInternal},
         {"pollGamepadState", lua_Platform_static_pollGamepadState},
         {"registerGesture", lua_Platform_static_registerGesture},
         {"setAbsoluteTime", lua_Platform_static_setAbsoluteTime},
@@ -47,7 +40,6 @@ void luaRegister_Platform()
         {"signalShutdown", lua_Platform_static_signalShutdown},
         {"sleep", lua_Platform_static_sleep},
         {"swapBuffers", lua_Platform_static_swapBuffers},
-        {"touchEventInternal", lua_Platform_static_touchEventInternal},
         {"unregisterGesture", lua_Platform_static_unregisterGesture},
         {NULL, NULL}
     };
@@ -198,50 +190,6 @@ int lua_Platform_static_displayKeyboard(lua_State* state)
     return 0;
 }
 
-int lua_Platform_static_gamepadEventInternal(lua_State* state)
-{
-    // Get the number of parameters.
-    int paramCount = lua_gettop(state);
-
-    // Attempt to match the parameters to a valid binding.
-    switch (paramCount)
-    {
-        case 2:
-        {
-            if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL) &&
-                (lua_type(state, 2) == LUA_TUSERDATA || lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TNIL))
-            {
-                // Get parameter 1 off the stack.
-                Gamepad::GamepadEvent param1 = (Gamepad::GamepadEvent)lua_enumFromString_GamepadGamepadEvent(luaL_checkstring(state, 1));
-
-                // Get parameter 2 off the stack.
-                bool param2Valid;
-                ScriptUtil::LuaArray<Gamepad> param2 = ScriptUtil::getObjectPointer<Gamepad>(2, "Gamepad", false, &param2Valid);
-                if (!param2Valid)
-                {
-                    lua_pushstring(state, "Failed to convert parameter 2 to type 'Gamepad'.");
-                    lua_error(state);
-                }
-
-                Platform::gamepadEventInternal(param1, param2);
-                
-                return 0;
-            }
-
-            lua_pushstring(state, "lua_Platform_static_gamepadEventInternal - Failed to match the given parameters to a valid function signature.");
-            lua_error(state);
-            break;
-        }
-        default:
-        {
-            lua_pushstring(state, "Invalid number of parameters (expected 2).");
-            lua_error(state);
-            break;
-        }
-    }
-    return 0;
-}
-
 int lua_Platform_static_getAbsoluteTime(lua_State* state)
 {
     // Get the number of parameters.
@@ -578,44 +526,6 @@ int lua_Platform_static_isVsync(lua_State* state)
     return 0;
 }
 
-int lua_Platform_static_keyEventInternal(lua_State* state)
-{
-    // Get the number of parameters.
-    int paramCount = lua_gettop(state);
-
-    // Attempt to match the parameters to a valid binding.
-    switch (paramCount)
-    {
-        case 2:
-        {
-            if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL) &&
-                lua_type(state, 2) == LUA_TNUMBER)
-            {
-                // Get parameter 1 off the stack.
-                Keyboard::KeyEvent param1 = (Keyboard::KeyEvent)lua_enumFromString_KeyboardKeyEvent(luaL_checkstring(state, 1));
-
-                // Get parameter 2 off the stack.
-                int param2 = (int)luaL_checkint(state, 2);
-
-                Platform::keyEventInternal(param1, param2);
-                
-                return 0;
-            }
-
-            lua_pushstring(state, "lua_Platform_static_keyEventInternal - Failed to match the given parameters to a valid function signature.");
-            lua_error(state);
-            break;
-        }
-        default:
-        {
-            lua_pushstring(state, "Invalid number of parameters (expected 2).");
-            lua_error(state);
-            break;
-        }
-    }
-    return 0;
-}
-
 int lua_Platform_static_launchURL(lua_State* state)
 {
     // Get the number of parameters.
@@ -653,55 +563,6 @@ int lua_Platform_static_launchURL(lua_State* state)
     return 0;
 }
 
-int lua_Platform_static_mouseEventInternal(lua_State* state)
-{
-    // Get the number of parameters.
-    int paramCount = lua_gettop(state);
-
-    // Attempt to match the parameters to a valid binding.
-    switch (paramCount)
-    {
-        case 4:
-        {
-            if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL) &&
-                lua_type(state, 2) == LUA_TNUMBER &&
-                lua_type(state, 3) == LUA_TNUMBER &&
-                lua_type(state, 4) == LUA_TNUMBER)
-            {
-                // Get parameter 1 off the stack.
-                Mouse::MouseEvent param1 = (Mouse::MouseEvent)lua_enumFromString_MouseMouseEvent(luaL_checkstring(state, 1));
-
-                // Get parameter 2 off the stack.
-                int param2 = (int)luaL_checkint(state, 2);
-
-                // Get parameter 3 off the stack.
-                int param3 = (int)luaL_checkint(state, 3);
-
-                // Get parameter 4 off the stack.
-                int param4 = (int)luaL_checkint(state, 4);
-
-                bool result = Platform::mouseEventInternal(param1, param2, param3, param4);
-
-                // Push the return value onto the stack.
-                lua_pushboolean(state, result);
-
-                return 1;
-            }
-
-            lua_pushstring(state, "lua_Platform_static_mouseEventInternal - Failed to match the given parameters to a valid function signature.");
-            lua_error(state);
-            break;
-        }
-        default:
-        {
-            lua_pushstring(state, "Invalid number of parameters (expected 4).");
-            lua_error(state);
-            break;
-        }
-    }
-    return 0;
-}
-
 int lua_Platform_static_pollGamepadState(lua_State* state)
 {
     // Get the number of parameters.
@@ -1030,52 +891,6 @@ int lua_Platform_static_swapBuffers(lua_State* state)
     return 0;
 }
 
-int lua_Platform_static_touchEventInternal(lua_State* state)
-{
-    // Get the number of parameters.
-    int paramCount = lua_gettop(state);
-
-    // Attempt to match the parameters to a valid binding.
-    switch (paramCount)
-    {
-        case 4:
-        {
-            if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL) &&
-                lua_type(state, 2) == LUA_TNUMBER &&
-                lua_type(state, 3) == LUA_TNUMBER &&
-                lua_type(state, 4) == LUA_TNUMBER)
-            {
-                // Get parameter 1 off the stack.
-                Touch::TouchEvent param1 = (Touch::TouchEvent)lua_enumFromString_TouchTouchEvent(luaL_checkstring(state, 1));
-
-                // Get parameter 2 off the stack.
-                int param2 = (int)luaL_checkint(state, 2);
-
-                // Get parameter 3 off the stack.
-                int param3 = (int)luaL_checkint(state, 3);
-
-                // Get parameter 4 off the stack.
-                unsigned int param4 = (unsigned int)luaL_checkunsigned(state, 4);
-
-                Platform::touchEventInternal(param1, param2, param3, param4);
-                
-                return 0;
-            }
-
-            lua_pushstring(state, "lua_Platform_static_touchEventInternal - Failed to match the given parameters to a valid function signature.");
-            lua_error(state);
-            break;
-        }
-        default:
-        {
-            lua_pushstring(state, "Invalid number of parameters (expected 4).");
-            lua_error(state);
-            break;
-        }
-    }
-    return 0;
-}
-
 int lua_Platform_static_unregisterGesture(lua_State* state)
 {
     // Get the number of parameters.

+ 0 - 4
gameplay/src/lua/lua_Platform.h

@@ -9,7 +9,6 @@ int lua_Platform__gc(lua_State* state);
 int lua_Platform_enterMessagePump(lua_State* state);
 int lua_Platform_static_canExit(lua_State* state);
 int lua_Platform_static_displayKeyboard(lua_State* state);
-int lua_Platform_static_gamepadEventInternal(lua_State* state);
 int lua_Platform_static_getAbsoluteTime(lua_State* state);
 int lua_Platform_static_getAccelerometerValues(lua_State* state);
 int lua_Platform_static_getDisplayHeight(lua_State* state);
@@ -21,9 +20,7 @@ int lua_Platform_static_isGestureSupported(lua_State* state);
 int lua_Platform_static_isMouseCaptured(lua_State* state);
 int lua_Platform_static_isMultiTouch(lua_State* state);
 int lua_Platform_static_isVsync(lua_State* state);
-int lua_Platform_static_keyEventInternal(lua_State* state);
 int lua_Platform_static_launchURL(lua_State* state);
-int lua_Platform_static_mouseEventInternal(lua_State* state);
 int lua_Platform_static_pollGamepadState(lua_State* state);
 int lua_Platform_static_registerGesture(lua_State* state);
 int lua_Platform_static_setAbsoluteTime(lua_State* state);
@@ -34,7 +31,6 @@ int lua_Platform_static_setVsync(lua_State* state);
 int lua_Platform_static_signalShutdown(lua_State* state);
 int lua_Platform_static_sleep(lua_State* state);
 int lua_Platform_static_swapBuffers(lua_State* state);
-int lua_Platform_static_touchEventInternal(lua_State* state);
 int lua_Platform_static_unregisterGesture(lua_State* state);
 
 void luaRegister_Platform();