Move TouchPoint to Devel API
[platform/core/uifw/dali-core.git] / dali / internal / event / events / pinch-gesture / pinch-gesture-recognizer.cpp
index 23b24d8..a552ca7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -21,7 +21,7 @@
 // EXTERNAL INCLUDES
 #include <cmath>
 
-#include <dali/public-api/events/touch-point.h>
+#include <dali/devel-api/events/touch-point.h>
 #include <dali/public-api/math/vector2.h>
 
 #include <dali/internal/event/events/pinch-gesture/pinch-gesture-event.h>
@@ -39,9 +39,6 @@ namespace Internal
 
 namespace
 {
-const unsigned int MINIMUM_TOUCH_EVENTS_REQUIRED = 4;
-const unsigned int MINIMUM_TOUCH_EVENTS_REQUIRED_AFTER_START = 4;
-
 const float MINIMUM_DISTANCE_IN_MILLIINCH = 45.0f; // This value is used for measuring minimum pinch distance in pixel.
 const float MINIMUM_DISTANCE_IN_PIXEL = 10.0f; // This value is for devices that do not provide a valid dpi value. (assumes 220dpi)
 
@@ -68,13 +65,15 @@ inline float GetDefaultMinimumPinchDistance( const Vector2& dpi )
 
 } // unnamed namespace
 
-PinchGestureRecognizer::PinchGestureRecognizer( Observer& observer, Vector2 screenSize, Vector2 screenDpi, float minimumPinchDistance )
+PinchGestureRecognizer::PinchGestureRecognizer( Observer& observer, Vector2 screenSize, Vector2 screenDpi, float minimumPinchDistance, uint32_t minimumTouchEvents, uint32_t minimumTouchEventsAfterStart )
 : GestureRecognizer( screenSize, Gesture::Pinch ),
   mObserver( observer ),
   mState( Clear ),
   mTouchEvents(),
   mDefaultMinimumDistanceDelta( GetDefaultMinimumPinchDistance( screenDpi ) ),
-  mStartingDistance( 0.0f )
+  mStartingDistance( 0.0f ),
+  mMinimumTouchEvents( minimumTouchEvents ),
+  mMinimumTouchEventsAfterStart( minimumTouchEventsAfterStart )
 {
   SetMinimumPinchDistance( minimumPinchDistance );
 }
@@ -91,6 +90,7 @@ void PinchGestureRecognizer::SetMinimumPinchDistance(float value)
 void PinchGestureRecognizer::SendEvent(const Integration::TouchEvent& event)
 {
   int pointCount = event.GetPointCount();
+  GestureRecognizerPtr ptr(this); // To keep us from being destroyed during the life-time of this method
 
   switch (mState)
   {
@@ -118,7 +118,7 @@ void PinchGestureRecognizer::SendEvent(const Integration::TouchEvent& event)
         const Integration::Point& currentPoint1 = event.points[0];
         const Integration::Point& currentPoint2 = event.points[1];
 
-        if (currentPoint1.GetState() == PointState::UP || currentPoint2.GetState() == PointState::UP)
+        if (currentPoint1.GetState() == PointState::UP || currentPoint2.GetState() == PointState::UP || currentPoint1.GetState() == PointState::INTERRUPTED)
         {
           // One of our touch points has an Up event so change our state back to Clear.
           mState = Clear;
@@ -129,7 +129,7 @@ void PinchGestureRecognizer::SendEvent(const Integration::TouchEvent& event)
           mTouchEvents.push_back(event);
 
           // We can only determine a pinch after a certain number of touch points have been collected.
-          if (mTouchEvents.size() >= MINIMUM_TOUCH_EVENTS_REQUIRED)
+          if( mTouchEvents.size() >= mMinimumTouchEvents )
           {
             const Integration::Point& firstPoint1 = mTouchEvents[0].points[0];
             const Integration::Point& firstPoint2 = mTouchEvents[0].points[1];
@@ -142,7 +142,7 @@ void PinchGestureRecognizer::SendEvent(const Integration::TouchEvent& event)
             if (fabsf(distanceChanged) > mMinimumDistanceDelta)
             {
               // Remove the first few events from the vector otherwise values are exaggerated
-              mTouchEvents.erase(mTouchEvents.begin(), mTouchEvents.end() - MINIMUM_TOUCH_EVENTS_REQUIRED_AFTER_START);
+              mTouchEvents.erase( mTouchEvents.begin(), mTouchEvents.end() - mMinimumTouchEvents );
 
               if ( !mTouchEvents.empty() )
               {
@@ -171,7 +171,15 @@ void PinchGestureRecognizer::SendEvent(const Integration::TouchEvent& event)
 
     case Started:
     {
-      if (pointCount != 2)
+      if(event.points[0].GetState() == PointState::INTERRUPTED)
+      {
+        // System interruption occurred, pinch should be cancelled
+        mTouchEvents.clear();
+        SendPinch(Gesture::Cancelled, event);
+        mState = Clear;
+        mTouchEvents.clear();
+      }
+      else if (pointCount != 2)
       {
         // Send pinch finished event
         SendPinch(Gesture::Finished, event);
@@ -197,7 +205,7 @@ void PinchGestureRecognizer::SendEvent(const Integration::TouchEvent& event)
         {
           mTouchEvents.push_back(event);
 
-          if (mTouchEvents.size() >= MINIMUM_TOUCH_EVENTS_REQUIRED_AFTER_START)
+          if( mTouchEvents.size() >= mMinimumTouchEventsAfterStart )
           {
             // Send pinch continuing
             SendPinch(Gesture::Continuing, event);
@@ -267,6 +275,16 @@ void PinchGestureRecognizer::SendPinch(Gesture::State state, const Integration::
   }
 }
 
+void PinchGestureRecognizer::SetMinimumTouchEvents( uint32_t value )
+{
+  mMinimumTouchEvents = value;
+}
+
+void PinchGestureRecognizer::SetMinimumTouchEventsAfterStart( uint32_t value )
+{
+  mMinimumTouchEventsAfterStart = value;
+}
+
 } // namespace Internal
 
 } // namespace Dali