Browse Source

Exposed gesture events to script.

sgrenier 12 years ago
parent
commit
db77222bc7

+ 21 - 0
gameplay/src/Platform.cpp

@@ -42,6 +42,27 @@ bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheel
     }
 }
 
+void Platform::gestureSwipeEventInternal(int x, int y, int direction)
+{
+    // TODO: Add support to Form for gestures
+    Game::getInstance()->gestureSwipeEvent(x, y, direction);
+    Game::getInstance()->getScriptController()->gestureSwipeEvent(x, y, direction);
+}
+
+void Platform::gesturePinchEventInternal(int x, int y, float scale)
+{
+    // TODO: Add support to Form for gestures
+    Game::getInstance()->gesturePinchEvent(x, y, scale);
+    Game::getInstance()->getScriptController()->gesturePinchEvent(x, y, scale);
+}
+
+void Platform::gestureTapEventInternal(int x, int y)
+{
+    // TODO: Add support to Form for gestures
+    Game::getInstance()->gestureTapEvent(x, y);
+    Game::getInstance()->getScriptController()->gestureTapEvent(x, y);
+}
+
 void Platform::resizeEventInternal(unsigned int width, unsigned int height)
 {
     // Update the width and height of the game

+ 23 - 2
gameplay/src/Platform.h

@@ -276,14 +276,14 @@ private:
 
 public:
 
-   /**
+    /**
      * Internal method used only from static code in various platform implementation.
      *
      * @script{ignore}
      */
     static void touchEventInternal(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
 
-   /**
+    /**
      * Internal method used only from static code in various platform implementation.
      *
      * @script{ignore}
@@ -297,6 +297,27 @@ public:
      */
     static bool mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheelDelta);
 
+    /**
+     * Internal method used only from static code in various platform implementation.
+     *
+     * @script{ignore}
+     */
+    static void gestureSwipeEventInternal(int x, int y, int direction);
+
+    /**
+     * Internal method used only from static code in various platform implementation.
+     *
+     * @script{ignore}
+     */
+    static void gesturePinchEventInternal(int x, int y, float scale);
+
+    /**
+     * Internal method used only from static code in various platform implementation.
+     *
+     * @script{ignore}
+     */
+    static void gestureTapEventInternal(int x, int y);
+
     /**
      * Internal method used only from static code in various platform implementation.
      *

+ 5 - 5
gameplay/src/PlatformAndroid.cpp

@@ -744,14 +744,14 @@ static int32_t engine_handle_input(struct android_app* app, AInputEvent* event)
                                 else if (deltaY < 0)
                                     direction = gameplay::Gesture::SWIPE_DIRECTION_UP;
                             }
-                            gameplay::Game::getInstance()->gestureSwipeEvent(x, y, direction);
+                            gameplay::Platform::gestureSwipeEventInternal(x, y, direction);
                             __pointer0.pressed = false;
                             gestureDetected = true;
                         }
                         else if(__gestureEventsProcessed.test(Gesture::GESTURE_TAP) &&
                                gameplay::Game::getInstance()->getAbsoluteTime() - __pointer0.time < GESTURE_TAP_DURATION_MAX)
                         {
-                            gameplay::Game::getInstance()->gestureTapEvent(x, y);
+                            gameplay::Platform::gestureTapEventInternal(x, y);
                             __pointer0.pressed = false;
                             gestureDetected = true;
                         }
@@ -810,7 +810,7 @@ static int32_t engine_handle_input(struct android_app* app, AInputEvent* event)
                             gameplay::Game::getInstance()->getAbsoluteTime() - __pointer1.time < GESTURE_SWIPE_DURATION_MAX && 
                             (abs(deltaX) > GESTURE_SWIPE_DISTANCE_MIN || abs(deltaY) > GESTURE_SWIPE_DISTANCE_MIN) )
                         {
-                            int direction;
+                            int direction = 0;
                             if (deltaX > 0)
                                 direction |= gameplay::Gesture::SWIPE_DIRECTION_RIGHT;
                             else if (deltaX < 0)
@@ -821,14 +821,14 @@ static int32_t engine_handle_input(struct android_app* app, AInputEvent* event)
                             else if (deltaY < 0)
                                 direction |= gameplay::Gesture::SWIPE_DIRECTION_UP;
 
-                            gameplay::Game::getInstance()->gestureSwipeEvent(x, y, direction);
+                            gameplay::Platform::gestureSwipeEventInternal(x, y, direction);
                             __pointer1.pressed = false;
                             gestureDetected = true;
                         }
                         else if(__gestureEventsProcessed.test(Gesture::GESTURE_TAP) &&
                                gameplay::Game::getInstance()->getAbsoluteTime() - __pointer1.time < GESTURE_TAP_DURATION_MAX)
                         {
-                            gameplay::Game::getInstance()->gestureTapEvent(x, y);
+                            gameplay::Platform::gestureTapEventInternal(x, y);
                             __pointer1.pressed = false;
                             gestureDetected = true;
                         }

+ 13 - 13
gameplay/src/PlatformBlackBerry.cpp

@@ -457,7 +457,7 @@ void gesture_callback(gesture_base_t* gesture, mtouch_event_t* event, void* para
                 gesture_swipe_t* swipe = (gesture_swipe_t*)gesture;
                 if (!__gestureSwipeRecognized)
                 {
-                    Game::getInstance()->gestureSwipeEvent(swipe->coords.x, swipe->coords.y, swipe->direction);
+                    Platform::gestureSwipeEventInternal(swipe->coords.x, swipe->coords.y, swipe->direction);
                     __gestureSwipeRecognized = true;
                 }
 
@@ -465,17 +465,17 @@ void gesture_callback(gesture_base_t* gesture, mtouch_event_t* event, void* para
             break;
         }
 
-    case GESTURE_PINCH:
-        {
-            if ( __gestureEventsProcessed.test(Gesture::GESTURE_PINCH) )
-            {
-                gesture_pinch_t* pinch = (gesture_pinch_t*)gesture;
-                float dist_x = (float)pinch->last_distance.x - (float)pinch->distance.x;
-                float dist_y = (float)pinch->last_distance.y - (float)pinch->distance.y;
-                float scale = sqrt( (dist_x * dist_x) + (dist_y * dist_y) );
-                Game::getInstance()->gesturePinchEvent(pinch->centroid.x, pinch->centroid.y, scale);
-            }
-            break;
+    case GESTURE_PINCH:
+        {
+            if ( __gestureEventsProcessed.test(Gesture::GESTURE_PINCH) )
+            {
+                gesture_pinch_t* pinch = (gesture_pinch_t*)gesture;
+                float dist_x = (float)pinch->last_distance.x - (float)pinch->distance.x;
+                float dist_y = (float)pinch->last_distance.y - (float)pinch->distance.y;
+                float scale = sqrt( (dist_x * dist_x) + (dist_y * dist_y) );
+                Game::getInstance()->gesturePinchEvent(pinch->centroid.x, pinch->centroid.y, scale);
+            }
+            break;
         }
 
     case GESTURE_TAP:
@@ -483,7 +483,7 @@ void gesture_callback(gesture_base_t* gesture, mtouch_event_t* event, void* para
             if ( __gestureEventsProcessed.test(Gesture::GESTURE_TAP) )
             {
                 gesture_tap_t* tap = (gesture_tap_t*)gesture;
-                Game::getInstance()->gestureTapEvent(tap->touch_coords.x, tap->touch_coords.y);
+                Platform::gestureTapEventInternal(tap->touch_coords.x, tap->touch_coords.y);
             }
             break;
         }

+ 1 - 1
gameplay/src/PlatformMacOSX.mm

@@ -1569,7 +1569,7 @@ int getUnicode(int key)
     yavg /= [touches count];
     
     [gameLock lock];
-    _game->gesturePinchEvent((int)xavg, (int)yavg, [event magnification]);
+    gameplay::Platform::gesturePinchEventInternal((int)xavg, (int)yavg, [event magnification]);
     [gameLock unlock];
 }
 

+ 3 - 3
gameplay/src/PlatformiOS.mm

@@ -711,14 +711,14 @@ int getUnicode(int key);
 - (void)handleTapGesture:(UITapGestureRecognizer*)sender
 {
     CGPoint location = [sender locationInView:self];
-    game->gestureTapEvent(location.x, location.y);
+    gameplay::Platform::gestureTapEventInternal(location.x, location.y);
 }
 
 - (void)handlePinchGesture:(UIPinchGestureRecognizer*)sender
 {
     CGFloat factor = [sender scale];
     CGPoint location = [sender locationInView:self];
-    game->gesturePinchEvent(location.x, location.y, factor);
+    gameplay::Platform::gesturePinchEventInternal(location.x, location.y, factor);
 }
 
 - (void)handleSwipeGesture:(UISwipeGestureRecognizer*)sender
@@ -740,7 +740,7 @@ int getUnicode(int key);
             gameplayDirection = Gesture::SWIPE_DIRECTION_DOWN;
             break;
     }
-    game->gestureSwipeEvent(location.x, location.y, gameplayDirection);
+    gameplay::Platform::gestureSwipeEventInternal(location.x, location.y, gameplayDirection);
 }
 
 @end

+ 28 - 1
gameplay/src/ScriptController.cpp

@@ -99,7 +99,7 @@ static bool getNestedVariable(lua_State* lua, const char* name)
     {
         start = end;
         end = strchr(start, '.');
-        if (end == '\0' || end == NULL)
+        if (end == NULL || *end == '\0')
         {
             // push the last variable
             lua_pushstring(lua, start);
@@ -839,6 +839,27 @@ bool ScriptController::mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheel
     return false;
 }
 
+void ScriptController::gestureSwipeEvent(int x, int y, int direction)
+{
+    std::vector<std::string>& list = _callbacks[GESTURE_SWIPE_EVENT];
+    for (size_t i = 0; i < list.size(); ++i)
+        executeFunction<void>(list[i].c_str(), "iii", x, y, direction);
+}
+
+void ScriptController::gesturePinchEvent(int x, int y, float scale)
+{
+    std::vector<std::string>& list = _callbacks[GESTURE_PINCH_EVENT];
+    for (size_t i = 0; i < list.size(); ++i)
+        executeFunction<void>(list[i].c_str(), "iif", x, y, scale);
+}
+
+void ScriptController::gestureTapEvent(int x, int y)
+{
+    std::vector<std::string>& list = _callbacks[GESTURE_TAP_EVENT];
+    for (size_t i = 0; i < list.size(); ++i)
+        executeFunction<void>(list[i].c_str(), "ii", x, y);
+}
+
 void ScriptController::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex)
 {
     std::vector<std::string>& list = _callbacks[GAMEPAD_EVENT];
@@ -1021,6 +1042,12 @@ ScriptController::ScriptCallback ScriptController::toCallback(const char* name)
         return ScriptController::TOUCH_EVENT;
     else if (strcmp(name, "mouseEvent") == 0)
         return ScriptController::MOUSE_EVENT;
+    else if (strcmp(name, "gestureSwipeEvent") == 0)
+        return ScriptController::GESTURE_SWIPE_EVENT;
+    else if (strcmp(name, "gesturePinchEvent") == 0)
+        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

+ 34 - 0
gameplay/src/ScriptController.h

@@ -770,6 +770,9 @@ private:
         KEY_EVENT,
         MOUSE_EVENT,
         TOUCH_EVENT,
+        GESTURE_SWIPE_EVENT,
+        GESTURE_PINCH_EVENT,
+        GESTURE_TAP_EVENT,
         GAMEPAD_EVENT,
         CALLBACK_COUNT,
         INVALID_CALLBACK = CALLBACK_COUNT
@@ -867,6 +870,37 @@ private:
      */
     bool mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta);
 
+    /**
+     * Script callback for Gesture::SWIPE events.
+     *
+     * @param x The x-coordinate of the start of the swipe.
+     * @param y The y-coordinate of the start of the swipe.
+     * @param direction The direction of the swipe
+     *
+     * @see Gesture::SWIPE_DIRECTION_UP
+     * @see Gesture::SWIPE_DIRECTION_DOWN
+     * @see Gesture::SWIPE_DIRECTION_LEFT
+     * @see Gesture::SWIPE_DIRECTION_RIGHT
+     */
+    void gestureSwipeEvent(int x, int y, int direction);
+
+    /**
+     * Script callback for Gesture::PINCH events.
+     *
+     * @param x The centroid x-coordinate of the pinch.
+     * @param y The centroid y-coordinate of the pinch.
+     * @param scale The scale of the pinch.
+     */
+    void gesturePinchEvent(int x, int y, float scale);
+
+    /**
+     * Script callback for Gesture::TAP events.
+     *
+     * @param x The x-coordinate of the tap.
+     * @param y The y-coordinate of the tap.
+     */
+    void gestureTapEvent(int x, int y);
+
     /**
      * Script gamepad callback on gamepad events.
      *