Refactoring Gestures Class
[platform/core/uifw/dali-core.git] / dali / internal / event / events / long-press-gesture / long-press-gesture-recognizer.cpp
index 6e0b24d..e4a9e02 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/common/thread-local-storage.h>
@@ -39,29 +39,35 @@ namespace
 {
 // TODO: Set these according to DPI
 const float MAXIMUM_MOTION_ALLOWED = 60.0f;
-// TODO: Set this time according to system setting (vconf)
-const unsigned long LONG_PRESS_TIME = 500u;
+
 } // unnamed namespace
 
-LongPressGestureRecognizer::LongPressGestureRecognizer(Observer& observer, Vector2 screenSize, const LongPressGestureRequest& request )
-: GestureRecognizer( screenSize, Gesture::LongPress ),
+LongPressGestureRecognizer::LongPressGestureRecognizer(Observer& observer, Vector2 screenSize, const LongPressGestureRequest& request, uint32_t minimumHoldingTime )
+: GestureRecognizer( screenSize, Dali::Gesture::LongPress ),
   mObserver( observer ),
   mState( Clear ),
   mMinimumTouchesRequired( request.minTouches ),
   mMaximumTouchesRequired( request.maxTouches ),
   mTouchTime( 0 ),
-  mTimerId( 0 )
+  mTimerId( 0 ),
+  mMinimumHoldingTime( minimumHoldingTime )
 {
 }
 
 LongPressGestureRecognizer::~LongPressGestureRecognizer()
 {
+  if( mTimerId != 0 && ThreadLocalStorage::Created() )
+  {
+    Dali::Integration::PlatformAbstraction& platformAbstraction = ThreadLocalStorage::Get().GetPlatformAbstraction();
+    platformAbstraction.CancelTimer(mTimerId);
+  }
 }
 
 void LongPressGestureRecognizer::SendEvent(const Integration::TouchEvent& event)
 {
   unsigned int pointCount( event.GetPointCount() );
   Dali::Integration::PlatformAbstraction& platformAbstraction = ThreadLocalStorage::Get().GetPlatformAbstraction();
+  GestureRecognizerPtr ptr(this); // To keep us from being destroyed during the life-time of this method
 
   switch (mState)
   {
@@ -77,11 +83,15 @@ void LongPressGestureRecognizer::SendEvent(const Integration::TouchEvent& event)
 
         mTouchTime = event.time;
 
-        mTimerId = platformAbstraction.StartTimer(GetSystemValue(), MakeCallback( this, &LongPressGestureRecognizer::TimerCallback));
+        if( mTimerId != 0 )
+        {
+          platformAbstraction.CancelTimer(mTimerId);
+        }
+        mTimerId = platformAbstraction.StartTimer( mMinimumHoldingTime, MakeCallback( this, &LongPressGestureRecognizer::TimerCallback ) );
 
         // A long press gesture may be possible, tell Core about this and change state to Touched.
         mState = Touched;
-        EmitGesture( Gesture::Possible );
+        EmitGesture( Dali::Gesture::Possible );
       }
 
       break;
@@ -93,9 +103,10 @@ void LongPressGestureRecognizer::SendEvent(const Integration::TouchEvent& event)
       if (pointCount > mMaximumTouchesRequired)
       {
         // A long press did not occur, tell Core that it was cancelled and change state to Failed.
-        EmitGesture( Gesture::Cancelled );
+        EmitGesture( Dali::Gesture::Cancelled );
         mTouchPositions.clear();
         platformAbstraction.CancelTimer(mTimerId);
+        mTimerId = 0;
         mState = Failed;
         break;
       }
@@ -119,9 +130,10 @@ void LongPressGestureRecognizer::SendEvent(const Integration::TouchEvent& event)
           case PointState::INTERRUPTED:
           {
             // System has interrupted us, long press is not possible, inform Core
-            EmitGesture( Gesture::Cancelled );
+            EmitGesture( Dali::Gesture::Cancelled );
             mTouchPositions.clear();
             platformAbstraction.CancelTimer(mTimerId);
+            mTimerId = 0;
             mState = ( pointCount == 1 ) ? Clear : Failed; // Change state to Clear if only one point, Failed otherwise.
             endLoop = true;
             break;
@@ -135,8 +147,9 @@ void LongPressGestureRecognizer::SendEvent(const Integration::TouchEvent& event)
             if (distanceSquared > ( MAXIMUM_MOTION_ALLOWED * MAXIMUM_MOTION_ALLOWED ) )
             {
               // We have moved more than the allowable motion for a long press gesture. Inform Core and change state to Failed.
-              EmitGesture( Gesture::Cancelled );
+              EmitGesture( Dali::Gesture::Cancelled );
               platformAbstraction.CancelTimer(mTimerId);
+              mTimerId = 0;
               mState = Failed;
               endLoop = true;
             }
@@ -167,7 +180,7 @@ void LongPressGestureRecognizer::SendEvent(const Integration::TouchEvent& event)
           if(mState == Finished)
           {
             // When the last touch point is lifted, we should inform the Core that the Long press has finished.
-            EmitGesture(Gesture::Finished);
+            EmitGesture(Dali::Gesture::Finished);
           }
           mTouchPositions.clear();
           mState = Clear; // Reset state to clear when last touch point is lifted.
@@ -186,12 +199,20 @@ void LongPressGestureRecognizer::Update(const GestureRequest& request)
   mMaximumTouchesRequired = longPress.maxTouches;
 }
 
+void LongPressGestureRecognizer::SetMinimumHoldingTime( uint32_t time )
+{
+  mMinimumHoldingTime = time;
+}
+
+
 bool LongPressGestureRecognizer::TimerCallback()
 {
-  EmitGesture(Gesture::Started);
+  EmitGesture(Dali::Gesture::Started);
 
   mState = Finished;
 
+  mTimerId = 0;
+
   return false;
 }
 
@@ -200,8 +221,8 @@ void LongPressGestureRecognizer::EmitGesture(Gesture::State state)
   unsigned int touchPoints ( static_cast<unsigned int>( mTouchPositions.size() ) );
 
   // We should tell Core about the Possible and Cancelled states regardless of whether we have satisfied long press requirements.
-  if ( (state == Gesture::Possible) ||
-       (state == Gesture::Cancelled) ||
+  if ( (state == Dali::Gesture::Possible) ||
+       (state == Dali::Gesture::Cancelled) ||
        (touchPoints >= mMinimumTouchesRequired) )
   {
     LongPressGestureEvent longPress( state );
@@ -215,9 +236,9 @@ void LongPressGestureRecognizer::EmitGesture(Gesture::State state)
     longPress.point /= static_cast<float>( touchPoints );
 
     longPress.time = mTouchTime;
-    if ( state != Gesture::Possible )
+    if ( state != Dali::Gesture::Possible )
     {
-      longPress.time += GetSystemValue();
+      longPress.time += mMinimumHoldingTime;
     }
 
     if( mScene )
@@ -230,11 +251,6 @@ void LongPressGestureRecognizer::EmitGesture(Gesture::State state)
   }
 }
 
-int LongPressGestureRecognizer::GetSystemValue()
-{
-  return LONG_PRESS_TIME;
-}
-
 } // namespace Internal
 
 } // namespace Dali