Преглед изворни кода

Adds gamepadEventInternal on Platform

setaylor пре 13 година
родитељ
комит
2060284f26

+ 2 - 2
gameplay/src/Gamepad.cpp

@@ -33,7 +33,7 @@ Gamepad::Gamepad(const char* formPath)
 }
 
 Gamepad::Gamepad(const char* id, GamepadHandle handle, unsigned int buttonCount, unsigned int joystickCount, unsigned int triggerCount,
-                 unsigned int vendorId, unsigned int productId, char* vendorString, char* productString)
+                 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)
 {
@@ -48,7 +48,7 @@ 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, char* vendorString, char* productString)
+    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);
 

+ 2 - 2
gameplay/src/Gamepad.h

@@ -198,7 +198,7 @@ private:
      * @param triggerCount the number of triggers on the gamepad.
      */
     Gamepad(const char* id, GamepadHandle handle, unsigned int buttonCount, unsigned int joystickCount, unsigned int triggerCount,
-            unsigned int vendorId, unsigned int productId, char* vendorString, char* productString);
+            unsigned int vendorId, unsigned int productId, const char* vendorString, const char* productString);
 
     /**
      * Copy constructor.
@@ -209,7 +209,7 @@ private:
      * 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, char* vendorString, char* productString);
+                        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.

+ 10 - 0
gameplay/src/Platform.h

@@ -268,6 +268,16 @@ public:
      */
     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.
+     *
+     * @param evt The mouse event that occurred.
+     * @param gamepad The gamepad that is either connected or disconnected from the platform.
+     *
+     * @see Gamepad::GamepadEvent
+     */
+    static void gamepadEventInternal(Gamepad::GamepadEvent evt, Gamepad* gamepad);
+
     /**
      * Opens an URL in an external browser, if available.
      *

+ 4 - 0
gameplay/src/PlatformAndroid.cpp

@@ -1252,6 +1252,10 @@ bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheel
     }
 }
 
+void Platform::gamepadEventInternal(Gamepad::GamepadEvent evt, Gamepad* gamepad)
+{
+}
+
 bool Platform::isGestureSupported(Gesture::GestureEvent evt)
 {
     // Pinch currently not implemented

+ 16 - 0
gameplay/src/PlatformBlackBerry.cpp

@@ -1521,6 +1521,22 @@ bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheel
     }
 }
 
+void Platform::gamepadEventInternal(Gamepad::GamepadEvent evt, Gamepad* gamepad)
+{
+    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);
+    }
+}
+
 bool Platform::isGestureSupported(Gesture::GestureEvent evt)
 {
     // All are supported no need to test the bitset

+ 4 - 0
gameplay/src/PlatformLinux.cpp

@@ -1118,6 +1118,10 @@ bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheel
         return Game::getInstance()->getScriptController()->mouseEvent(evt, x, y, wheelDelta);
     }
 }
+
+void Platform::gamepadEventInternal(Gamepad::GamepadEvent evt, Gamepad* gamepad)
+{
+}
 
 bool Platform::isGestureSupported(Gesture::GestureEvent evt)
 {

+ 16 - 0
gameplay/src/PlatformMacOSX.mm

@@ -1716,6 +1716,22 @@ bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheel
     return result;
 }
 
+void Platform::gamepadEventInternal(Gamepad::GamepadEvent evt, Gamepad* gamepad)
+{
+    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);
+    }
+}
+
 bool Platform::isGestureSupported(Gesture::GestureEvent evt)
 {
     // Swipe unsupported as it is considered moving mouse cursor

+ 4 - 0
gameplay/src/PlatformWindows.cpp

@@ -1278,6 +1278,10 @@ bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheel
     }
 }
 
+void Platform::gamepadEventInternal(Gamepad::GamepadEvent evt, Gamepad* gamepad)
+{
+}
+
 bool Platform::launchURL(const char* url)
 {
     if (url == NULL || *url == '\0')

+ 5 - 1
gameplay/src/PlatformiOS.mm

@@ -1388,7 +1388,11 @@ bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheel
     {
         return Game::getInstance()->getScriptController()->mouseEvent(evt, x, y, wheelDelta);
     }
-}    
+}
+
+void Platform::gamepadEventInternal(Gamepad::GamepadEvent evt, Gamepad* gamepad)
+{
+}
 
 bool Platform::isGestureSupported(Gesture::GestureEvent evt)
 {

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

@@ -2,6 +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"
@@ -21,6 +22,7 @@ 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},
@@ -196,6 +198,50 @@ 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.

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

@@ -9,6 +9,7 @@ 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);