Reset mGestureStackDepth when panning is interrupted by ScrollTo
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / controls / scrollable / scroll-view / scroll-view-impl.cpp
index 9cabbf0..0d37399 100644 (file)
 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-overshoot-indicator-impl.h>
 #include <dali/integration-api/debug.h>
 
-#define ENABLED_SCROLL_STATE_LOGGING
+//#define ENABLED_SCROLL_STATE_LOGGING
 
 #ifdef ENABLED_SCROLL_STATE_LOGGING
-#define DALI_LOG_SCROLL_STATE(format, args...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, "%s:%d " format, __PRETTY_FUNCTION__, __LINE__, ## args)
+#define DALI_LOG_SCROLL_STATE(format, args...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, "%s:%d " format "\n", __PRETTY_FUNCTION__, __LINE__, ## args)
 #else
 #define DALI_LOG_SCROLL_STATE(format, args...)
 #endif
@@ -623,7 +623,9 @@ ScrollView::ScrollView()
   mWrapMode(false),
   mAxisAutoLock(false),
   mAlterChild(false),
-  mDefaultMaxOvershoot(true)
+  mDefaultMaxOvershoot(true),
+  mCanScrollHorizontal(true),
+  mCanScrollVertical(true)
 {
   SetRequiresMouseWheelEvents(true);
 }
@@ -939,8 +941,8 @@ void ScrollView::UpdatePropertyDomain(const Vector3& size)
   if(mRulerX->IsEnabled())
   {
     const Toolkit::RulerDomain& rulerDomain = mRulerX->GetDomain();
-    if( fabsf(min.x - rulerDomain.min) > Math::MACHINE_EPSILON_10000
-        || fabsf(max.x - rulerDomain.max) > Math::MACHINE_EPSILON_10000 )
+    if( fabsf(min.x - rulerDomain.min) > Math::MACHINE_EPSILON_100
+        || fabsf(max.x - rulerDomain.max) > Math::MACHINE_EPSILON_100 )
     {
       domainChanged = true;
       min.x = rulerDomain.min;
@@ -954,17 +956,26 @@ void ScrollView::UpdatePropertyDomain(const Vector3& size)
         mScrollPrePosition.x = Clamp(mScrollPrePosition.x, -(max.x - size.x), -min.x);
       }
     }
-    if( (fabsf(rulerDomain.max - rulerDomain.min) - size.x) > Math::MACHINE_EPSILON_10000 )
+    if( (fabsf(rulerDomain.max - rulerDomain.min) - size.x) > Math::MACHINE_EPSILON_100 )
     {
       canScrollHorizontal = true;
     }
   }
+  else if( fabs(min.x) > Math::MACHINE_EPSILON_100
+           || fabs(max.x) > Math::MACHINE_EPSILON_100 )
+  {
+    // need to reset to 0
+    domainChanged = true;
+    min.x = 0.0f;
+    max.x = 0.0f;
+    canScrollHorizontal = false;
+  }
 
   if(mRulerY->IsEnabled())
   {
     const Toolkit::RulerDomain& rulerDomain = mRulerY->GetDomain();
-    if( fabsf(min.y - rulerDomain.min) > Math::MACHINE_EPSILON_10000
-        || fabsf(max.y - rulerDomain.max) > Math::MACHINE_EPSILON_10000 )
+    if( fabsf(min.y - rulerDomain.min) > Math::MACHINE_EPSILON_100
+        || fabsf(max.y - rulerDomain.max) > Math::MACHINE_EPSILON_100 )
     {
       domainChanged = true;
       min.y = rulerDomain.min;
@@ -978,18 +989,30 @@ void ScrollView::UpdatePropertyDomain(const Vector3& size)
         mScrollPrePosition.y = Clamp(mScrollPrePosition.y, -(max.y - size.y), -min.y);
       }
     }
-    if( (fabsf(rulerDomain.max - rulerDomain.min) - size.y) > Math::MACHINE_EPSILON_10000 )
+    if( (fabsf(rulerDomain.max - rulerDomain.min) - size.y) > Math::MACHINE_EPSILON_100 )
     {
       canScrollVertical = true;
     }
   }
+  else if( fabs(min.y) > Math::MACHINE_EPSILON_100
+           || fabs(max.y) > Math::MACHINE_EPSILON_100 )
+  {
+    // need to reset to 0
+    domainChanged = true;
+    min.y = 0.0f;
+    max.y = 0.0f;
+    canScrollHorizontal = false;
+  }
+
   // avoid setting properties if possible, otherwise this will cause an entire update as well as triggering constraints using each property we update
-  if( self.GetProperty<bool>(mPropertyCanScrollVertical) != canScrollVertical )
+  if( mCanScrollVertical != canScrollVertical )
   {
+    mCanScrollVertical = canScrollVertical;
     self.SetProperty(mPropertyCanScrollVertical, canScrollVertical);
   }
-  if( self.GetProperty<bool>(mPropertyCanScrollHorizontal) != canScrollHorizontal )
+  if( mCanScrollHorizontal != canScrollHorizontal )
   {
+    mCanScrollHorizontal = canScrollHorizontal;
     self.SetProperty(mPropertyCanScrollHorizontal, canScrollHorizontal);
   }
   if( scrollPositionChanged )
@@ -1000,8 +1023,8 @@ void ScrollView::UpdatePropertyDomain(const Vector3& size)
   {
     mMinScroll = min;
     mMaxScroll = max;
-    self.SetProperty(mPropertyPositionMin, min );
-    self.SetProperty(mPropertyPositionMax, max );
+    self.SetProperty(mPropertyPositionMin, mMinScroll );
+    self.SetProperty(mPropertyPositionMax, mMaxScroll );
   }
 }
 
@@ -1028,7 +1051,7 @@ void ScrollView::SetScrollSensitive(bool sensitive)
   Actor self = Self();
   PanGestureDetector panGesture( GetPanGestureDetector() );
 
-  DALI_LOG_SCROLL_STATE("[0x%X] sensitive[%d]", this, int(sensitive));
+  DALI_LOG_SCROLL_STATE("[0x%X] sensitive: before:[%d] setting[%d]", this, int(mSensitive), int(sensitive));
 
   if((!mSensitive) && (sensitive))
   {
@@ -1038,8 +1061,7 @@ void ScrollView::SetScrollSensitive(bool sensitive)
   else if((mSensitive) && (!sensitive))
   {
     // while the scroll view is panning, the state needs to be reset.
-    bool isPanning = self.GetProperty<bool>( mPropertyPanning );
-    if ( isPanning )
+    if ( mPanning )
     {
       PanGesture cancelGesture( Gesture::Cancelled );
       OnPan( cancelGesture );
@@ -1250,14 +1272,19 @@ void ScrollView::TransformTo(const Vector3& position, const Vector3& scale, floa
 void ScrollView::TransformTo(const Vector3& position, const Vector3& scale, float rotation, float duration,
                              DirectionBias horizontalBias, DirectionBias verticalBias)
 {
+  Actor self( Self() );
+
   // Guard against destruction during signal emission
   // Note that Emit() methods are called indirectly e.g. from within ScrollView::AnimateTo()
   Toolkit::ScrollView handle( GetOwner() );
 
+  DALI_LOG_SCROLL_STATE("[0x%X] pos[%.2f,%.2f], scale[%.2f,%.2f], rot[%.2f], duration[%.2f] bias[%d, %d]",
+    this, position.x, position.y, scale.x, scale.y, rotation, duration, int(horizontalBias), int(verticalBias));
+
   Vector3 currentScrollPosition = GetCurrentScrollPosition();
-  Self().SetProperty( mPropertyScrollStartPagePosition, currentScrollPosition );
+  self.SetProperty( mPropertyScrollStartPagePosition, currentScrollPosition );
 
-  if(mScrolling) // are we interrupting a current scroll?
+  if( mScrolling ) // are we interrupting a current scroll?
   {
     // set mScrolling to false, in case user has code that interrogates mScrolling Getter() in complete.
     mScrolling = false;
@@ -1265,8 +1292,23 @@ void ScrollView::TransformTo(const Vector3& position, const Vector3& scale, floa
     mScrollCompletedSignalV2.Emit( currentScrollPosition );
   }
 
-  Self().SetProperty(mPropertyScrolling, true);
+  if( mPanning ) // are we interrupting a current pan?
+  {
+    DALI_LOG_SCROLL_STATE("[0x%X] Interrupting Pan, set to false", this );
+    mPanning = false;
+    mGestureStackDepth = 0;
+    self.SetProperty( mPropertyPanning, false );
+
+    if( mScrollMainInternalPrePositionConstraint )
+    {
+      self.RemoveConstraint(mScrollMainInternalPrePositionConstraint);
+    }
+  }
+
+  self.SetProperty(mPropertyScrolling, true);
   mScrolling = true;
+
+  DALI_LOG_SCROLL_STATE("[0x%X] mScrollStartedSignalV2 1 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
   mScrollStartedSignalV2.Emit( currentScrollPosition );
   bool animating = AnimateTo(-position,
                              Vector3::ONE * duration,
@@ -1283,11 +1325,20 @@ void ScrollView::TransformTo(const Vector3& position, const Vector3& scale, floa
   if(!animating)
   {
     // if not animating, then this pan has completed right now.
-    Self().SetProperty(mPropertyScrolling, false);
+    self.SetProperty(mPropertyScrolling, false);
     mScrolling = false;
-    DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignalV2 2 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
+
+    // If we have no duration, then in the next update frame, we will be at the position specified as we just set.
+    // In this scenario, we cannot return the currentScrollPosition as this is out-of-date and should instead return the requested final position
+    Vector3 completedPosition( currentScrollPosition );
+    if( duration <= Math::MACHINE_EPSILON_10 )
+    {
+      completedPosition = position;
+    }
+
+    DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignalV2 2 [%.2f, %.2f]", this, completedPosition.x, completedPosition.y);
     SetScrollUpdateNotification(false);
-    mScrollCompletedSignalV2.Emit( currentScrollPosition );
+    mScrollCompletedSignalV2.Emit( completedPosition );
   }
 }
 
@@ -1304,6 +1355,9 @@ void ScrollView::ScrollTo(const Vector3& position, float duration)
 void ScrollView::ScrollTo(const Vector3& position, float duration,
                           DirectionBias horizontalBias, DirectionBias verticalBias)
 {
+  DALI_LOG_SCROLL_STATE("[0x%X] position[%.2f, %.2f] duration[%.2f]",
+    this, position.x, position.y, duration, int(horizontalBias), int(verticalBias));
+
   TransformTo(position, mScrollPostScale, mScrollPostRotation, duration, horizontalBias, verticalBias);
 }
 
@@ -1761,14 +1815,14 @@ bool ScrollView::AnimateTo(const Vector3& position, const Vector3& positionDurat
 
       if(mRulerX->IsEnabled())
       {
-        float dir = VectorInDomain(-mScrollPostPosition.x, -mScrollTargetPosition.x, rulerDomainX.min, rulerDomainX.max, horizontalBias);
-        mScrollTargetPosition.x = mScrollPostPosition.x + -dir;
+        float dir = VectorInDomain(-mScrollPrePosition.x, -mScrollTargetPosition.x, rulerDomainX.min, rulerDomainX.max, horizontalBias);
+        mScrollTargetPosition.x = mScrollPrePosition.x + -dir;
       }
 
       if(mRulerY->IsEnabled())
       {
-        float dir = VectorInDomain(-mScrollPostPosition.y, -mScrollTargetPosition.y, rulerDomainY.min, rulerDomainY.max, verticalBias);
-        mScrollTargetPosition.y = mScrollPostPosition.y + -dir;
+        float dir = VectorInDomain(-mScrollPrePosition.y, -mScrollTargetPosition.y, rulerDomainY.min, rulerDomainY.max, verticalBias);
+        mScrollTargetPosition.y = mScrollPrePosition.y + -dir;
       }
     }
 
@@ -1781,6 +1835,8 @@ bool ScrollView::AnimateTo(const Vector3& position, const Vector3& positionDurat
     {
       self.SetProperty(mPropertyPrePosition, mScrollTargetPosition);
       mScrollPrePosition = mScrollTargetPosition;
+      mScrollPostPosition = mScrollTargetPosition;
+      WrapPosition(mScrollPostPosition);
     }
   }
 
@@ -1815,6 +1871,7 @@ bool ScrollView::AnimateTo(const Vector3& position, const Vector3& positionDurat
   snapEvent.rotation = rotation;
   snapEvent.duration = totalDuration;
 
+  DALI_LOG_SCROLL_STATE("[0x%X] mSnapStartedSignalV2 [%.2f, %.2f]", this, snapEvent.position.x, snapEvent.position.y);
   mSnapStartedSignalV2.Emit( snapEvent );
 
   return (mScrollStateFlags & SCROLL_ANIMATION_FLAGS) != 0;
@@ -1849,6 +1906,15 @@ void ScrollView::RemoveOverlay(Actor actor)
   mInternalActor.Remove( actor );
 }
 
+void ScrollView::SetOvershootEffectColor( const Vector4& color )
+{
+  mOvershootEffectColor = color;
+  if( mOvershootIndicator )
+  {
+    mOvershootIndicator->SetOvershootEffectColor( color );
+  }
+}
+
 void ScrollView::SetScrollingDirection( Radian direction, Radian threshold )
 {
   PanGestureDetector panGesture( GetPanGestureDetector() );
@@ -2304,6 +2370,13 @@ void ScrollView::OnScrollAnimationFinished( Animation& source )
       scrollingFinished = true;
     }
     mInternalXAnimation.Reset();
+    // wrap pre scroll x position and set it
+    if( mWrapMode )
+    {
+      const RulerDomain rulerDomain = mRulerX->GetDomain();
+      mScrollPrePosition.x = -WrapInDomain(-mScrollPrePosition.x, rulerDomain.min, rulerDomain.max);
+      handle.SetProperty(mPropertyPrePosition, mScrollPrePosition);
+    }
     SnapInternalXTo(mScrollPostPosition.x);
   }
 
@@ -2314,6 +2387,13 @@ void ScrollView::OnScrollAnimationFinished( Animation& source )
       scrollingFinished = true;
     }
     mInternalYAnimation.Reset();
+    if( mWrapMode )
+    {
+      // wrap pre scroll y position and set it
+      const RulerDomain rulerDomain = mRulerY->GetDomain();
+      mScrollPrePosition.y = -WrapInDomain(-mScrollPrePosition.y, rulerDomain.min, rulerDomain.max);
+      handle.SetProperty(mPropertyPrePosition, mScrollPrePosition);
+    }
     SnapInternalYTo(mScrollPostPosition.y);
   }
 
@@ -2463,6 +2543,8 @@ void ScrollView::OnPan(PanGesture gesture)
 
   if(!mSensitive)
   {
+    DALI_LOG_SCROLL_STATE("[0x%X] Pan Ignored, Insensitive", this);
+
     // If another callback on the same original signal disables sensitivity,
     // this callback will still be called, so we must suppress it.
     return;
@@ -2473,6 +2555,7 @@ void ScrollView::OnPan(PanGesture gesture)
   {
     case Gesture::Started:
     {
+      DALI_LOG_SCROLL_STATE("[0x%X] Pan Started", this);
       UpdateLocalScrollProperties();
       GestureStarted();
       mPanning = true;
@@ -2485,21 +2568,45 @@ void ScrollView::OnPan(PanGesture gesture)
 
     case Gesture::Continuing:
     {
-      GestureContinuing(gesture.screenDisplacement, Vector2::ZERO, 0.0f);
+      if ( mPanning )
+      {
+        DALI_LOG_SCROLL_STATE("[0x%X] Pan Continuing", this);
+        GestureContinuing(gesture.screenDisplacement, Vector2::ZERO, 0.0f);
+      }
+      else
+      {
+        // If we do not think we are panning, then we should not do anything here
+        return;
+      }
       break;
     }
 
     case Gesture::Finished:
     case Gesture::Cancelled:
     {
-      UpdateLocalScrollProperties();
-      mLastVelocity = gesture.velocity;
-      mPanning = false;
-      self.SetProperty( mPropertyPanning, false );
-
-      if( mScrollMainInternalPrePositionConstraint )
+      if ( mPanning )
+      {
+        DALI_LOG_SCROLL_STATE("[0x%X] Pan %s", this, ( ( gesture.state == Gesture::Finished ) ? "Finished" : "Cancelled" ) );
+
+        UpdateLocalScrollProperties();
+        mLastVelocity = gesture.velocity;
+        mPanning = false;
+        self.SetProperty( mPropertyPanning, false );
+
+        if( mScrollMainInternalPrePositionConstraint )
+        {
+          self.RemoveConstraint(mScrollMainInternalPrePositionConstraint);
+        }
+
+        if( mOvershootIndicator )
+        {
+          mOvershootIndicator->ClearOvershoot();
+        }
+      }
+      else
       {
-        self.RemoveConstraint(mScrollMainInternalPrePositionConstraint);
+        // If we do not think we are panning, then we should not do anything here
+        return;
       }
       break;
     }
@@ -2525,6 +2632,7 @@ void ScrollView::OnGestureEx(Gesture::State state)
     Vector3 currentScrollPosition = GetCurrentScrollPosition();
     Self().SetProperty(mPropertyScrolling, true);
     mScrolling = true;
+    DALI_LOG_SCROLL_STATE("[0x%X] mScrollStartedSignalV2 2 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
     mScrollStartedSignalV2.Emit( currentScrollPosition );
   }
   else if( (state == Gesture::Finished) ||
@@ -2539,6 +2647,10 @@ void ScrollView::OnGestureEx(Gesture::State state)
     {
       FinishTransform();
     }
+    else
+    {
+      DALI_LOG_SCROLL_STATE("[0x%X] mGestureStackDepth[%d]", this, mGestureStackDepth);
+    }
   }
 }
 
@@ -2562,6 +2674,15 @@ void ScrollView::FinishTransform()
     SetScrollUpdateNotification(false);
     mScrolling = false;
     Self().SetProperty(mPropertyScrolling, false);
+
+    if( fabs(mScrollPrePosition.x - mScrollTargetPosition.x) > Math::MACHINE_EPSILON_10 )
+    {
+      SnapInternalXTo(mScrollTargetPosition.x);
+    }
+    if( fabs(mScrollPrePosition.y - mScrollTargetPosition.y) > Math::MACHINE_EPSILON_10 )
+    {
+      SnapInternalYTo(mScrollTargetPosition.y);
+    }
     Vector3 currentScrollPosition = GetCurrentScrollPosition();
     DALI_LOG_SCROLL_STATE("[0x%X] mScrollCompletedSignalV2 6 [%.2f, %.2f]", this, currentScrollPosition.x, currentScrollPosition.y);
     mScrollCompletedSignalV2.Emit( currentScrollPosition );