Change overshoot to have constant speed as a property
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / controls / scrollable / scroll-view / scroll-view-impl.cpp
index 81c8893..e2df7a5 100644 (file)
 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-view-impl.h>
 
 // EXTERNAL INCLUDES
+#include <dali/public-api/animation/constraints.h>
+#include <dali/public-api/common/stage.h>
 #include <dali/public-api/events/mouse-wheel-event.h>
+#include <dali/public-api/events/touch-event.h>
+#include <dali/public-api/object/type-registry.h>
 #include <dali/integration-api/debug.h>
 
 // INTERNAL INCLUDES
@@ -57,8 +61,6 @@ const float FLICK_ORTHO_ANGLE_RANGE = 75.0f;
 const unsigned int MAXIMUM_NUMBER_OF_VALUES = 5;                                          ///< Number of values to use for weighted pan calculation.
 const Vector2 DEFAULT_MOUSE_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION = Vector2(0.17f, 0.1f); ///< The step of horizontal scroll distance in the proportion of stage size for each mouse wheel event received.
 const unsigned long MINIMUM_TIME_BETWEEN_DOWN_AND_UP_FOR_RESET( 150u );
-const float DEFAULT_OVERSHOOT_ANIMATION_DURATION = 0.35f;  // time in seconds
-const Vector2 OVERSCROLL_CLAMP(1.0f, 1.0f);                // maximum overscroll allowed in pixels when overshoot indicator is being used
 const float TOUCH_DOWN_TIMER_INTERVAL = 100.0f;
 const float DEFAULT_SCROLL_UPDATE_DISTANCE( 30.0f );                               ///< Default distance to travel in pixels for scroll update signal
 
@@ -72,27 +74,6 @@ const Vector2 ANGLE_OUTER_CUBE_SWING(Math::PI * 0.5f, Math::PI * 0.5f);  ///< ou
 
 // Helpers ////////////////////////////////////////////////////////////////////////////////////////
 
-// TODO: GetAngle for Vector2 can be moved.
-// GetAngle for Vector3 needs to be measured against a normal/plane.
-
-/**
- * @param[in] vector The 3D vector to be measured
- * @return angle in radians from 0 to 2PI
- */
-float GetAngle(const Vector3& vector)
-{
-  return atan2(vector.y, vector.x) + Math::PI;
-}
-
-/**
- * @param[in] vector The 2D vector to be measured
- * @return angle in radians from 0 to 2PI
- */
-float GetAngle(const Vector2& vector)
-{
-  return atan2(vector.y, vector.x) + Math::PI;
-}
-
 /**
  * Find the vector (distance) from (a) to (b)
  * in domain (start) to (end)
@@ -276,13 +257,15 @@ ScrollView::LockAxis GetLockAxis(const Vector2& panDelta, ScrollView::LockAxis c
  */
 struct InternalPrePositionConstraint
 {
-  InternalPrePositionConstraint(const Vector2& initialPanMask,
+  InternalPrePositionConstraint(const Vector2& initialPanPosition,
+                                const Vector2& initialPanMask,
                                 bool axisAutoLock,
                                 float axisAutoLockGradient,
                                 ScrollView::LockAxis initialLockAxis,
                                 const Vector2& maxOvershoot,
                                 const RulerDomain& domainX, const RulerDomain& domainY)
-  : mInitialPanMask(initialPanMask),
+  : mLocalStart(initialPanPosition),
+    mInitialPanMask(initialPanMask),
     mDomainMin( -domainX.min, -domainY.min ),
     mDomainMax( -domainX.max, -domainY.max ),
     mMaxOvershoot(maxOvershoot),
@@ -297,7 +280,6 @@ struct InternalPrePositionConstraint
 
   Vector3 operator()(const Vector3&    current,
                      const PropertyInput& gesturePositionProperty,
-                     const PropertyInput& gestureDisplacementProperty,
                      const PropertyInput& sizeProperty)
   {
     Vector3 scrollPostPosition = current;
@@ -305,7 +287,6 @@ struct InternalPrePositionConstraint
 
     if(!mWasPanning)
     {
-      mLocalStart = gesturePositionProperty.GetVector2() - gestureDisplacementProperty.GetVector2();
       mPrePosition = current;
       mCurrentPanMask = mInitialPanMask;
       mWasPanning = true;
@@ -487,26 +468,6 @@ struct OvershootYConstraint
 };
 
 /**
- * When panning, this constraint updates the X property, otherwise
- * it has no effect on the X property.
- */
-float InternalXConstraint(const float&    current,
-                          const PropertyInput& scrollPosition)
-{
-  return scrollPosition.GetVector3().x;
-}
-
-/**
- * When panning, this constraint updates the Y property, otherwise
- * it has no effect on the Y property.
- */
-float InternalYConstraint(const float&    current,
-                          const PropertyInput& scrollPosition)
-{
-  return scrollPosition.GetVector3().y;
-}
-
-/**
  * Internal Position-Delta Property Constraint.
  *
  * Generates position-delta property based on scroll-position + scroll-offset properties.
@@ -994,7 +955,7 @@ void ScrollView::UpdatePropertyDomain(const Vector3& size)
     domainChanged = true;
     min.y = 0.0f;
     max.y = 0.0f;
-    canScrollHorizontal = false;
+    canScrollVertical = false;
   }
 
   // avoid setting properties if possible, otherwise this will cause an entire update as well as triggering constraints using each property we update
@@ -1113,16 +1074,6 @@ void ScrollView::SetWrapMode(bool enable)
   Self().SetProperty(mPropertyWrap, enable);
 }
 
-int ScrollView::GetRefreshInterval() const
-{
-  return mScrollUpdateDistance;
-}
-
-void ScrollView::SetRefreshInterval(int milliseconds)
-{
-  mScrollUpdateDistance = milliseconds;
-}
-
 int ScrollView::GetScrollUpdateDistance() const
 {
   return mScrollUpdateDistance;
@@ -1259,10 +1210,10 @@ Vector3 ScrollView::GetDomainSize() const
 void ScrollView::TransformTo(const Vector3& position,
                              DirectionBias horizontalBias, DirectionBias verticalBias)
 {
-  TransformTo(position, mSnapDuration, horizontalBias, verticalBias);
+  TransformTo(position, mSnapDuration, mSnapAlphaFunction, horizontalBias, verticalBias);
 }
 
-void ScrollView::TransformTo(const Vector3& position, float duration,
+void ScrollView::TransformTo(const Vector3& position, float duration, AlphaFunction alpha,
                              DirectionBias horizontalBias, DirectionBias verticalBias)
 {
   Actor self( Self() );
@@ -1305,7 +1256,7 @@ void ScrollView::TransformTo(const Vector3& position, float duration,
   mScrollStartedSignalV2.Emit( currentScrollPosition );
   bool animating = AnimateTo(-position,
                              Vector3::ONE * duration,
-                             mSnapAlphaFunction,
+                             alpha,
                              true,
                              horizontalBias,
                              verticalBias,
@@ -1341,13 +1292,22 @@ void ScrollView::ScrollTo(const Vector3& position, float duration)
   ScrollTo(position, duration, DirectionBiasNone, DirectionBiasNone);
 }
 
+void ScrollView::ScrollTo(const Vector3& position, float duration, AlphaFunction alpha)
+{
+  ScrollTo(position, duration, alpha, DirectionBiasNone, DirectionBiasNone);
+}
+
 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));
+  ScrollTo(position, duration, mSnapAlphaFunction, horizontalBias, verticalBias);
+}
 
-  TransformTo(position, duration, horizontalBias, verticalBias);
+void ScrollView::ScrollTo(const Vector3& position, float duration, AlphaFunction alpha,
+                DirectionBias horizontalBias, DirectionBias verticalBias)
+{
+  DALI_LOG_SCROLL_STATE("[0x%X] position[%.2f, %.2f] duration[%.2f], bias[%d, %d]", this, position.x, position.y, duration, int(horizontalBias), int(verticalBias));
+  TransformTo(position, duration, alpha, horizontalBias, verticalBias);
 }
 
 void ScrollView::ScrollTo(unsigned int page)
@@ -1814,7 +1774,6 @@ void ScrollView::SetOvershootEnabled(bool enabled)
   }
   if( enabled )
   {
-    mMaxOvershoot = OVERSCROLL_CLAMP;
     mOvershootIndicator->AttachToScrollable(*this);
   }
   else
@@ -2013,21 +1972,7 @@ void ScrollView::OnChildRemove(Actor& child)
 void ScrollView::OnPropertySet( Property::Index index, Property::Value propertyValue )
 {
   Actor self = Self();
-  if( index == mPropertyX )
-  {
-    self.GetProperty(mPropertyPrePosition).Get(mScrollPrePosition);
-    propertyValue.Get(mScrollPrePosition.x);
-    DALI_LOG_SCROLL_STATE("[0x%X] Setting mPropertyPrePosition To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y );
-    self.SetProperty(mPropertyPrePosition, mScrollPrePosition);
-  }
-  else if( index == mPropertyY )
-  {
-    self.GetProperty(mPropertyPrePosition).Get(mScrollPrePosition);
-    propertyValue.Get(mScrollPrePosition.y);
-    DALI_LOG_SCROLL_STATE("[0x%X] Setting mPropertyPrePosition To[%.2f, %.2f]", this, mScrollPrePosition.x, mScrollPrePosition.y );
-    self.SetProperty(mPropertyPrePosition, mScrollPrePosition);
-  }
-  else if( index == mPropertyPrePosition )
+  if( index == mPropertyPrePosition )
   {
     DALI_LOG_SCROLL_STATE("[0x%X]: mPropertyPrePosition[%.2f, %.2f]", this, propertyValue.Get<Vector3>().x, propertyValue.Get<Vector3>().y);
     propertyValue.Get(mScrollPrePosition);
@@ -2487,7 +2432,7 @@ void ScrollView::GestureContinuing(const Vector2& panDelta)
 // TODO: Upgrade to use a more powerful gesture detector (one that supports multiple touches on pan - so works as pan and flick gesture)
 // BUG: Gesture::Finished doesn't always return velocity on release (due to
 // timeDelta between last two events being 0 sometimes, or posiiton being the same)
-void ScrollView::OnPan(PanGesture gesture)
+void ScrollView::OnPan( const PanGesture& gesture )
 {
   // Guard against destruction during signal emission
   // Note that Emit() methods are called indirectly e.g. from within ScrollView::OnGestureEx()
@@ -2508,6 +2453,7 @@ void ScrollView::OnPan(PanGesture gesture)
     case Gesture::Started:
     {
       DALI_LOG_SCROLL_STATE("[0x%X] Pan Started", this);
+      mPanStartPosition = gesture.position - gesture.displacement;
       UpdateLocalScrollProperties();
       GestureStarted();
       mPanning = true;
@@ -2741,8 +2687,6 @@ void ScrollView::UpdateMainInternalConstraint()
     self.RemoveConstraint(mScrollMainInternalDeltaConstraint);
     self.RemoveConstraint(mScrollMainInternalFinalConstraint);
     self.RemoveConstraint(mScrollMainInternalRelativeConstraint);
-    self.RemoveConstraint(mScrollMainInternalXConstraint);
-    self.RemoveConstraint(mScrollMainInternalYConstraint);
   }
   if( mScrollMainInternalPrePositionConstraint )
   {
@@ -2769,9 +2713,8 @@ void ScrollView::UpdateMainInternalConstraint()
   {
     constraint = Constraint::New<Vector3>( mPropertyPrePosition,
                                                       Source( detector, PanGestureDetector::LOCAL_POSITION ),
-                                                      Source( detector, PanGestureDetector::LOCAL_DISPLACEMENT ),
                                                       Source( self, Actor::SIZE ),
-                                                      InternalPrePositionConstraint( initialPanMask, mAxisAutoLock, mAxisAutoLockGradient, mLockAxis, mMaxOvershoot, mRulerX->GetDomain(), mRulerY->GetDomain() ) );
+                                                      InternalPrePositionConstraint( mPanStartPosition, initialPanMask, mAxisAutoLock, mAxisAutoLockGradient, mLockAxis, mMaxOvershoot, mRulerX->GetDomain(), mRulerY->GetDomain() ) );
     mScrollMainInternalPrePositionConstraint = self.ApplyConstraint( constraint );
   }
 
@@ -2807,16 +2750,6 @@ void ScrollView::UpdateMainInternalConstraint()
                                          InternalRelativePositionConstraint );
   mScrollMainInternalRelativeConstraint = self.ApplyConstraint( constraint );
 
-  constraint = Constraint::New<float>( mPropertyX,
-                                         LocalSource( mPropertyPrePosition ),
-                                         InternalXConstraint );
-  mScrollMainInternalXConstraint = self.ApplyConstraint( constraint );
-
-  constraint = Constraint::New<float>( mPropertyY,
-                                         LocalSource( mPropertyPrePosition ),
-                                         InternalYConstraint );
-  mScrollMainInternalYConstraint = self.ApplyConstraint( constraint );
-
   // When panning we want to make sure overshoot values are affected by pre position and post position
   SetOvershootConstraintsEnabled(!mWrapMode);
 }
@@ -2863,27 +2796,15 @@ void ScrollView::SetInternalConstraints()
   // User definable constraints to apply to all child actors //////////////////
   Actor self = Self();
 
-  // LocalSource - The Actors to be moved.
-  // self - The ScrollView
-
-  // Apply some default constraints to ScrollView.
+  // Apply some default constraints to ScrollView & its bound actors
   // Movement + Wrap function
-  // TODO: Look into removing some of these constraints
 
   Constraint constraint;
 
-  // MoveScaledActor (scrolling/zooming)
+  // MoveActor (scrolling)
   constraint = Constraint::New<Vector3>( Actor::POSITION,
                                          Source( self, mPropertyPosition ),
-                                         Source( self, mPropertyScale ),
-                                         MoveScaledActorConstraint );
-  constraint.SetRemoveAction(Constraint::Discard);
-  ApplyConstraintToBoundActors(constraint);
-
-  // ScaleActor (scrolling/zooming)
-  constraint = Constraint::New<Vector3>( Actor::SCALE,
-                                         Source( self, mPropertyScale ),
-                                         ScaleActorConstraint );
+                                         MoveActorConstraint );
   constraint.SetRemoveAction(Constraint::Discard);
   ApplyConstraintToBoundActors(constraint);