Explorar o código

Added Gesture API support and implemented gestures support in PlatformQNX.cpp.

setaylor %!s(int64=13) %!d(string=hai) anos
pai
achega
23a3707f1c

+ 2 - 0
gameplay/gameplay.vcxproj

@@ -241,6 +241,7 @@
     <ClCompile Include="src\PhysicsSpringConstraint.cpp" />
     <ClCompile Include="src\Plane.cpp" />
     <ClCompile Include="src\PlatformAndroid.cpp" />
+    <ClCompile Include="src\PlatformLinux.cpp" />
     <ClCompile Include="src\PlatformQNX.cpp" />
     <ClCompile Include="src\PlatformWin32.cpp" />
     <ClCompile Include="src\Properties.cpp" />
@@ -308,6 +309,7 @@
     <ClInclude Include="src\Game.h" />
     <ClInclude Include="src\Gamepad.h" />
     <ClInclude Include="src\gameplay.h" />
+    <ClInclude Include="src\Gesture.h" />
     <ClInclude Include="src\Image.h" />
     <ClInclude Include="src\Joint.h" />
     <ClInclude Include="src\Joystick.h" />

+ 6 - 0
gameplay/gameplay.vcxproj.filters

@@ -783,6 +783,9 @@
     <ClCompile Include="src\lua\lua_ScriptTarget.cpp">
       <Filter>lua</Filter>
     </ClCompile>
+    <ClCompile Include="src\PlatformLinux.cpp">
+      <Filter>src</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="src\Animation.h">
@@ -1556,6 +1559,9 @@
     <ClInclude Include="src\lua\lua_ScriptTarget.h">
       <Filter>lua</Filter>
     </ClInclude>
+    <ClInclude Include="src\Gesture.h">
+      <Filter>src</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <None Include="src\gameplay-main-macosx.mm">

+ 25 - 0
gameplay/src/Game.cpp

@@ -441,6 +441,31 @@ bool Game::mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
     return false;
 }
 
+void Game::recognizeGesture(Gesture::GestureEvent evt)
+{
+    Platform::recognizeGesture(evt);
+}
+
+void Game::gestureSwipeEvent(int x, int y, int direction)
+{
+}
+
+void Game::gesturePinchEvent(int x, int y, float scale)
+{
+}
+
+void Game::gestureRotateEvent(int x, int y, float angle)
+{
+}
+
+void Game::gestureTapEvent(int x, int y)
+{
+}
+
+void Game::gestureTapDoubleEvent(int x, int y)
+{
+}
+
 void Game::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad)
 {
 }

+ 74 - 4
gameplay/src/Game.h

@@ -4,8 +4,10 @@
 #include <queue>
 
 #include "Keyboard.h"
-#include "Touch.h"
 #include "Mouse.h"
+#include "Touch.h"
+#include "Gesture.h"
+#include "Gamepad.h"
 #include "AudioController.h"
 #include "AnimationController.h"
 #include "PhysicsController.h"
@@ -14,7 +16,7 @@
 #include "Rectangle.h"
 #include "Vector4.h"
 #include "TimeListener.h"
-#include "Gamepad.h"
+
 
 namespace gameplay
 {
@@ -260,7 +262,7 @@ public:
     AudioListener* getAudioListener();
 
     /**
-     * Menu callback on menu events for platforms with special menu keys or gestures.
+     * Menu callback on menu events for platforms with special menu keys or special platform gestures.
      */
     virtual void menuEvent();
     
@@ -351,6 +353,74 @@ public:
      */
     inline bool isCursorVisible();
 
+    /**
+     * Requests the game to recognize the specified gesture events.
+     *
+     * This can be called multiple times to recognize more that one gesture.
+     * Call with Gesture::NONE to unrecognize all gestures. Once a gesture
+     * is recognized the specific gesture event methods will
+     * begin to be called.
+     *
+     * Registering for:
+     *
+     * Gesture::SWIPE calls gestureSwipeEvent(..)
+     * Gesture::PINCH calls gesturePinchEvent(..)
+     * Gesture::ROTATE calls gestureRotateEvent(..)
+     * Gesture::TAP calls gestureTapEvent(..)
+     * Gesture::TAP_DOUBLE calls gestureTapDoubleEvent(..)
+     *
+     * @param evt The gesture event to start recognizing for
+     */
+    void recognizeGesture(Gesture::GestureEvent evt);
+
+    /**
+     * Gesture callback on 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
+     */
+    virtual void gestureSwipeEvent(int x, int y, int direction);
+
+    /**
+     * Gesture callback on 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.
+     */
+    virtual void gesturePinchEvent(int x, int y, float scale);
+
+    /**
+     * Gesture callback on Gesture::ROTATE events.
+     *
+     * @param x The centroid x-coordinate of the rotate.
+     * @param y The centroid y-coordinate of the rotate.
+     * @param scale The scale of the pinch.
+     */
+    virtual void gestureRotateEvent(int x, int y, float angle);
+
+    /**
+     * Gesture callback on Gesture::TAP events.
+     *
+     * @param x The x-coordinate of the tap.
+     * @param y The y-coordinate of the tap.
+     */
+    virtual void gestureTapEvent(int x, int y);
+
+    /**
+     * Gesture callback on Gesture::TAP_DOUBLE events.
+     *
+     * @param x The x-coordinate of the double tap.
+     * @param y The y-coordinate of the double tap.
+     */
+    virtual void gestureTapDoubleEvent(int x, int y);
+
     /**
      * Gamepad callback on gamepad events. Override to receive Gamepad::CONNECTED_EVENT 
      * and Gamepad::DISCONNECTED_EVENT.
@@ -359,7 +429,7 @@ public:
      * @param gamepad the gamepad the event occurred on
      */
     virtual void gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad);
-    
+
     /**
      * Gets the number of gamepad's that can be used in the game. Includes gamepads not connected.
      * 

+ 39 - 0
gameplay/src/Gesture.h

@@ -0,0 +1,39 @@
+#ifndef GESTURE_H_
+#define GESTURE_H_
+
+namespace gameplay
+{
+
+/**
+ * Gesture event
+ */
+class Gesture
+{
+public:
+
+    enum GestureEvent
+    {
+        SWIPE   = 0,
+        PINCH,
+        ROTATE,
+        TAP,
+        TAP_DOUBLE,
+        NONE = -1
+    };
+
+    static const int SWIPE_DIRECTION_UP = 1 << 0;
+    static const int SWIPE_DIRECTION_DOWN = 1 << 1;
+    static const int SWIPE_DIRECTION_LEFT = 1 << 2;
+    static const int SWIPE_DIRECTION_RIGHT = 1 << 3;
+
+private:
+
+    /**
+     * Constructor. Used internally.
+     */
+    Gesture();
+};
+
+}
+
+#endif

+ 22 - 14
gameplay/src/Platform.h

@@ -1,10 +1,11 @@
 #ifndef PLATFORM_H_
 #define PLATFORM_H_
 
-#include "Touch.h"
+#include "Vector2.h"
 #include "Keyboard.h"
 #include "Mouse.h"
-#include "Vector2.h"
+#include "Touch.h"
+#include "Gesture.h"
 #include "Gamepad.h"
 
 namespace gameplay
@@ -97,6 +98,18 @@ public:
      */
     static void setVsync(bool enable);
 
+    /**
+     * Swaps the frame buffer on the device.
+     */
+    static void swapBuffers();
+
+    /**
+     * Sleeps synchronously for the given amount of time (in milliseconds).
+     *
+     * @param ms How long to sleep (in milliseconds).
+     */
+    static void sleep(long ms);
+
     /**
      * Set if multi-touch is enabled on the platform.
      *
@@ -165,11 +178,6 @@ public:
      * @param roll The accelerometer roll.
      */
     static void getAccelerometerValues(float* pitch, float* roll);
-
-    /**
-     * Swaps the frame buffer on the device.
-     */
-    static void swapBuffers();
     
     /**
      * Shows or hides the virtual keyboard (if supported).
@@ -178,6 +186,13 @@ public:
      */
     static void displayKeyboard(bool display);
 
+    /**
+     * Registers the platform for gesture recognition for the specified gesture event.
+     *
+     * @param evt The gesture event to recognize or Gesture::NONE to disable gesture recognition events.
+     */
+    static void recognizeGesture(Gesture::GestureEvent evt);
+
     /** 
      * Gets the number of gamepad devices connected to the Platform.
      *
@@ -317,13 +332,6 @@ public:
      * @see Mouse::MouseEvent
      */
     static bool mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheelDelta);
-
-    /**
-     * Sleeps synchronously for the given amount of time (in milliseconds).
-     *
-     * @param ms How long to sleep (in milliseconds).
-     */
-    static void sleep(long ms);
     
 private:
 

+ 13 - 8
gameplay/src/PlatformAndroid.cpp

@@ -878,6 +878,18 @@ void Platform::setVsync(bool enable)
     __vsync = enable;
 }
 
+
+void Platform::swapBuffers()
+{
+    if (__eglDisplay && __eglSurface)
+        eglSwapBuffers(__eglDisplay, __eglSurface);
+}
+
+void Platform::sleep(long ms)
+{
+    usleep(ms * 1000);
+}
+
 void Platform::setMultiTouch(bool enabled)
 {
     __multiTouch = enabled;
@@ -947,12 +959,6 @@ bool Platform::isCursorVisible()
     return false;
 }
 
-void Platform::swapBuffers()
-{
-    if (__eglDisplay && __eglSurface)
-        eglSwapBuffers(__eglDisplay, __eglSurface);
-}
-
 void Platform::displayKeyboard(bool display)
 {
     if (display)
@@ -995,9 +1001,8 @@ bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheel
     }
 }
 
-void Platform::sleep(long ms)
+void Platform::recognizeGesture(Gesture::GestureEvent evt)
 {
-    usleep(ms * 1000);
 }
 
 unsigned int Platform::getGamepadsConnected()

+ 13 - 8
gameplay/src/PlatformLinux.cpp

@@ -739,6 +739,16 @@ void Platform::setVsync(bool enable)
     __vsync = enable;
 }
 
+void Platform::swapBuffers()
+{
+    glXSwapBuffers(__display, __window);
+}
+
+void Platform::sleep(long ms)
+{
+    usleep(ms * 1000);
+}
+
 void Platform::setMultiTouch(bool enabled)
 {
     // not supported
@@ -796,11 +806,6 @@ bool Platform::isCursorVisible()
     return __cursorVisible;
 }
 
-void Platform::swapBuffers()
-{
-    glXSwapBuffers(__display, __window);
-}
-
 void Platform::displayKeyboard(bool display)
 {
     // not supported
@@ -840,9 +845,9 @@ bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheel
     }
 }
 
-void Platform::sleep(long ms)
-{
-    usleep(ms * 1000);
+void Platform::recognizeGesture(Gesture::GestureEvent evt)
+{
+        // Do nothing
 }
 
 unsigned int Platform::getGamepadsConnected()

+ 13 - 8
gameplay/src/PlatformMacOSX.mm

@@ -758,6 +758,17 @@ void Platform::setVsync(bool enable)
     __vsync = enable;
 }
 
+void Platform::swapBuffers()
+{
+    if (__view)
+        CGLFlushDrawable((CGLContextObj)[[__view openGLContext] CGLContextObj]);
+}
+
+void Platform::sleep(long ms)
+{
+    usleep(ms * 1000);
+}
+
 void Platform::setMultiTouch(bool enabled)
 {
 }
@@ -829,12 +840,6 @@ bool Platform::isCursorVisible()
     return __cursorVisible;
 }
 
-void Platform::swapBuffers()
-{
-    if (__view)
-        CGLFlushDrawable((CGLContextObj)[[__view openGLContext] CGLContextObj]);
-}
-
 void Platform::displayKeyboard(bool display)
 {
     // Do nothing.
@@ -874,9 +879,9 @@ bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheel
     }
 }
 
-void Platform::sleep(long ms)
+void Platform::recognizeGesture(Gesture::GestureEvent evt)
 {
-    usleep(ms * 1000);
+    // Do nothing
 }
 
 unsigned int Platform::getGamepadsConnected()

+ 108 - 11
gameplay/src/PlatformQNX.cpp

@@ -10,6 +10,12 @@
 #include <sys/keycodes.h>
 #include <screen/screen.h>
 #include <input/screen_helpers.h>
+#include <gestures/set.h>
+#include <gestures/swipe.h>
+#include <gestures/pinch.h>
+#include <gestures/rotate.h>
+#include <gestures/tap.h>
+#include <gestures/double_tap.h>
 #include <bps/bps.h>
 #include <bps/event.h>
 #include <bps/screen.h>
@@ -39,6 +45,7 @@ static bool __multiTouch = false;
 static float __pitch;
 static float __roll;
 static const char* __glExtensions;
+static struct gestures_set * __gesturesSet;
 PFNGLBINDVERTEXARRAYOESPROC glBindVertexArray = NULL;
 PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArrays = NULL;
 PFNGLGENVERTEXARRAYSOESPROC glGenVertexArrays = NULL;
@@ -435,6 +442,47 @@ EGLenum checkErrorEGL(const char* msg)
     return error;
 }
 
+void gesture_callback(gesture_base_t* gesture, mtouch_event_t* event, void* param, int async)
+{
+    switch (gesture->type)
+    {
+    case GESTURE_SWIPE:
+        {
+            gesture_swipe_t* swipe = (gesture_swipe_t*)gesture;
+            Game::getInstance()->gestureSwipeEvent(swipe->coords.x, swipe->coords.y, swipe->direction);
+            break;
+        }
+
+    case GESTURE_PINCH:
+        {
+            gesture_pinch_t* pinch = (gesture_pinch_t*)gesture;
+            float scale = 1.0f; // todo: try to map this the same across ios and android.
+            Game::getInstance()->gesturePinchEvent(pinch->centroid.x, pinch->centroid.y, scale);
+            break;
+        }
+
+    case GESTURE_ROTATE:
+        {
+            gesture_rotate_t* rotate = (gesture_rotate_t*)gesture;
+            Game::getInstance()->gestureRotateEvent(rotate->centroid.x, rotate->centroid.y, rotate->angle);
+            break;
+        }
+    case GESTURE_TAP:
+        {
+            gesture_tap_t* tap = (gesture_tap_t*)gesture;
+            Game::getInstance()->gestureTapEvent(tap->touch_coords.x, tap->touch_coords.y);
+            break;
+        }
+
+    case GESTURE_DOUBLE_TAP:
+        {
+            gesture_tap_t* double_tap = (gesture_tap_t*)gesture;
+            Game::getInstance()->gestureTapDoubleEvent(double_tap->touch_coords.x,double_tap->touch_coords.y);
+            break;
+        }
+    }
+}
+
 Platform::Platform(Game* game)
     : _game(game)
 {
@@ -491,15 +539,13 @@ Platform* Platform::create(Game* game, void* attachToWindow)
 
     bps_initialize();
 
+    // Initialize navigator and orientation
     static const int SENSOR_RATE = 25000;
     sensor_set_rate(SENSOR_TYPE_AZIMUTH_PITCH_ROLL, SENSOR_RATE);
     sensor_set_skip_duplicates(SENSOR_TYPE_AZIMUTH_PITCH_ROLL, true);
     sensor_request_events(SENSOR_TYPE_AZIMUTH_PITCH_ROLL);
-
     navigator_request_events(0);
     navigator_rotation_lock(true);
-
-    // Determine initial orientation angle.
     orientation_direction_t direction;
     orientation_get(&direction, &__orientationAngle);
 
@@ -1077,6 +1123,17 @@ void Platform::setVsync(bool enable)
     __vsync = enable;
 }
 
+void Platform::swapBuffers()
+{
+    if (__eglDisplay && __eglSurface)
+        eglSwapBuffers(__eglDisplay, __eglSurface);
+}
+
+void Platform::sleep(long ms)
+{
+    usleep(ms * 1000);
+}
+
 void Platform::setMultiTouch(bool enabled)
 {
     __multiTouch = enabled;
@@ -1156,12 +1213,6 @@ bool Platform::isCursorVisible()
     return false;
 }
 
-void Platform::swapBuffers()
-{
-    if (__eglDisplay && __eglSurface)
-        eglSwapBuffers(__eglDisplay, __eglSurface);
-}
-
 void Platform::displayKeyboard(bool display)
 {
     if (display)
@@ -1204,9 +1255,55 @@ bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheel
     }
 }
 
-void Platform::sleep(long ms)
+void Platform::recognizeGesture(Gesture::GestureEvent evt)
 {
-    usleep(ms * 1000);
+    if (evt != Gesture::NONE && __gesturesSet == NULL)
+    {
+       __gesturesSet = gestures_set_alloc();
+    }
+
+    switch(evt)
+    {
+    case Gesture::NONE:
+        {
+            if (__gesturesSet)
+            {
+                gestures_set_free(__gesturesSet);
+                __gesturesSet =  NULL;
+            }
+            break;
+        }
+
+    case Gesture::SWIPE:
+        {
+            swipe_gesture_alloc(NULL, gesture_callback, __gesturesSet);
+            break;
+        }
+
+    case Gesture::PINCH:
+        {
+            pinch_gesture_alloc(NULL, gesture_callback, __gesturesSet);
+            break;
+        }
+
+    case Gesture::ROTATE:
+        {
+            rotate_gesture_alloc(NULL, gesture_callback, __gesturesSet);
+            break;
+        }
+
+    case Gesture::TAP:
+        {
+            tap_gesture_alloc(NULL, gesture_callback, __gesturesSet);
+            break;
+        }
+
+    case Gesture::TAP_DOUBLE:
+        {
+            double_tap_gesture_alloc(NULL, gesture_callback, __gesturesSet);
+            break;
+        }
+    }
 }
 
 unsigned int Platform::getGamepadsConnected()

+ 15 - 10
gameplay/src/PlatformWin32.cpp

@@ -941,6 +941,17 @@ void Platform::setVsync(bool enable)
     __vsync = enable;
 }
 
+void Platform::swapBuffers()
+{
+    if (__hdc)
+        SwapBuffers(__hdc);
+}
+
+void Platform::sleep(long ms)
+{
+    Sleep(ms);
+}
+
 void Platform::setMultiTouch(bool enabled)
 {
     // not supported
@@ -1008,15 +1019,14 @@ bool Platform::isCursorVisible()
     return __cursorVisible;
 }
 
-void Platform::swapBuffers()
+void Platform::displayKeyboard(bool display)
 {
-    if (__hdc)
-        SwapBuffers(__hdc);
+    // Do nothing.
 }
 
-void Platform::displayKeyboard(bool display)
+void Platform::recognizeGesture(Gesture::GestureEvent evt)
 {
-    // Do nothing.
+    // Do nothing
 }
 
 unsigned int Platform::getGamepadsConnected()
@@ -1244,11 +1254,6 @@ bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheel
     }
 }
 
-void Platform::sleep(long ms)
-{
-    Sleep(ms);
-}
-
 }
 
 #endif

+ 12 - 9
gameplay/src/PlatformiOS.mm

@@ -886,6 +886,16 @@ void Platform::setVsync(bool enable)
     __vsync = enable;
 }
 
+void Platform::swapBuffers()
+{
+    if (__view)
+        [__view swapBuffers];
+}
+void Platform::sleep(long ms)
+{
+    usleep(ms * 1000);
+}
+
 void Platform::getAccelerometerValues(float* pitch, float* roll)
 {
     [__appDelegate getAccelerometerPitch:pitch roll:roll];
@@ -929,12 +939,6 @@ bool Platform::isMultiTouch()
     return __view.multipleTouchEnabled;
 }
 
-void Platform::swapBuffers()
-{
-    if (__view)
-        [__view swapBuffers];
-}
-
 void Platform::displayKeyboard(bool display) 
 {
     if(__view) 
@@ -984,9 +988,8 @@ bool Platform::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheel
     }
 }    
 
-void Platform::sleep(long ms)
-{
-    usleep(ms * 1000);
+void Platform::recognizeGesture(Gesture::GestureEvent evt)
+{
 }
 
 unsigned int Platform::getGamepadsConnected()

+ 0 - 1
gameplay/src/Touch.h

@@ -21,7 +21,6 @@ public:
         TOUCH_MOVE
     };
 
-
 private:
 
     /**

+ 4 - 3
gameplay/src/gameplay.h

@@ -3,11 +3,12 @@
 #include "Platform.h"
 #include "Game.h"
 #include "Keyboard.h"
-#include "Touch.h"
 #include "Mouse.h"
+#include "Touch.h"
+#include "Gesture.h"
+#include "Gamepad.h"
 #include "FileSystem.h"
 #include "Bundle.h"
-#include "Gamepad.h"
 
 // Math
 #include "Rectangle.h"
@@ -95,4 +96,4 @@
 #include "Layout.h"
 #include "AbsoluteLayout.h"
 #include "VerticalLayout.h"
-
+#include "FlowLayout.h"