Allow overshoot indicators to be implemented outside of ItemView
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / scrollable / item-view / item-view-impl.cpp
index dad90ef..5f1f7b8 100644 (file)
@@ -19,7 +19,6 @@
 
 // EXTERNAL INCLUDES
 #include <algorithm>
-#include <set>
 
 // INTERNAL INCLUDES
 #include <dali/public-api/events/mouse-wheel-event.h>
@@ -396,6 +395,7 @@ ItemView::ItemView(ItemFactory& factory)
 : Scrollable(),
   mItemFactory(factory),
   mActiveLayout(NULL),
+  mDefaultAlphaFunction(Dali::Constraint::DEFAULT_ALPHA_FUNCTION),
   mAnimatingOvershootOn(false),
   mAnimateOvershootOff(false),
   mAnchoringEnabled(true),
@@ -439,7 +439,6 @@ void ItemView::OnInitialize()
   mPropertyMinimumLayoutPosition = self.RegisterProperty(MINIMUM_LAYOUT_POSITION_PROPERTY_NAME, 0.0f);
   mPropertyPosition = self.RegisterProperty(POSITION_PROPERTY_NAME, 0.0f);
   mPropertyScrollSpeed = self.RegisterProperty(SCROLL_SPEED_PROPERTY_NAME, 0.0f);
-  mPropertyOvershoot = self.RegisterProperty(OVERSHOOT_PROPERTY_NAME, 0.0f);
 
   ApplyOvershootOverlayConstraints();
 
@@ -517,13 +516,8 @@ void ItemView::ActivateLayout(unsigned int layoutIndex, const Vector3& targetSiz
   mActiveLayoutTargetSize = targetSize;
 
   // Switch to the new layout
-  ItemLayout* previousLayout = mActiveLayout;
   mActiveLayout = mLayouts[layoutIndex].Get();
 
-  // Calculate which items are within either layout
-  ItemRange oldRange = previousLayout ? GetItemRange(*previousLayout, targetSize, false/*don't reserve extra*/) : ItemRange(0u, 0u);
-  ItemRange newRange = GetItemRange(*mActiveLayout, targetSize, false/*don't reserve extra*/);
-
   // Move the items to the new layout positions...
 
   bool resizeAnimationNeeded(false);
@@ -533,18 +527,13 @@ void ItemView::ActivateLayout(unsigned int layoutIndex, const Vector3& targetSiz
     unsigned int itemId = iter->first;
     Actor actor = iter->second;
 
-    // Immediately relayout items that aren't within either layout
-    bool immediate = !oldRange.Within(itemId) &&
-                     !newRange.Within(itemId);
-
     // Remove constraints from previous layout
     actor.RemoveConstraints();
 
     Vector3 size;
     if(mActiveLayout->GetItemSize(itemId, targetSize, size))
     {
-      if (!immediate &&
-          durationSeconds > 0.0f)
+      if( durationSeconds > 0.0f )
       {
         // Use a size animation
         if (!resizeAnimationNeeded)
@@ -564,7 +553,7 @@ void ItemView::ActivateLayout(unsigned int layoutIndex, const Vector3& targetSiz
       }
     }
 
-    ApplyConstraints(actor, *mActiveLayout, itemId, immediate ? 0.0f : durationSeconds);
+    ApplyConstraints(actor, *mActiveLayout, itemId, durationSeconds);
   }
 
   if (resizeAnimationNeeded)
@@ -574,7 +563,7 @@ void ItemView::ActivateLayout(unsigned int layoutIndex, const Vector3& targetSiz
 
   // Refresh the new layout
   ItemRange range = GetItemRange(*mActiveLayout, targetSize, true/*reserve extra*/);
-  AddActorsWithinRange( range );
+  AddActorsWithinRange( range, durationSeconds );
 
   // Scroll to an appropriate layout position
 
@@ -634,6 +623,16 @@ void ItemView::DeactivateCurrentLayout()
   CancelRefreshTimer();
 }
 
+void ItemView::SetDefaultAlphaFunction(AlphaFunction func)
+{
+  mDefaultAlphaFunction = func;
+}
+
+AlphaFunction ItemView::GetDefaultAlphaFunction() const
+{
+  return mDefaultAlphaFunction;
+}
+
 bool ItemView::OnRefreshTick()
 {
   // Short-circuit if there is no active layout
@@ -646,7 +645,7 @@ bool ItemView::OnRefreshTick()
 
   RemoveActorsOutsideRange( range );
 
-  AddActorsWithinRange( range );
+  AddActorsWithinRange( range, 0.0f/*immediate*/ );
 
   // Keep refreshing whilst the layout is moving
   return mScrollAnimation || (mGestureState == Gesture::Started || mGestureState == Gesture::Continuing);
@@ -946,7 +945,7 @@ void ItemView::RemoveActorsOutsideRange( ItemRange range )
   }
 }
 
-void ItemView::AddActorsWithinRange( ItemRange range )
+void ItemView::AddActorsWithinRange( ItemRange range, float durationSeconds )
 {
   range.end = min(mItemFactory.GetNumberOfItems(), range.end);
 
@@ -955,19 +954,19 @@ void ItemView::AddActorsWithinRange( ItemRange range )
   {
     for (unsigned int itemId = range.begin; itemId < range.end; ++itemId)
     {
-      AddNewActor( itemId );
+      AddNewActor( itemId, durationSeconds );
     }
   }
   else
   {
     for (unsigned int itemId = range.end; itemId > range.begin; --itemId)
     {
-      AddNewActor( itemId-1 );
+      AddNewActor( itemId-1, durationSeconds );
     }
   }
 }
 
-void ItemView::AddNewActor( unsigned int itemId )
+void ItemView::AddNewActor( unsigned int itemId, float durationSeconds )
 {
   if( mItemPool.end() == mItemPool.find( itemId ) )
   {
@@ -979,7 +978,7 @@ void ItemView::AddNewActor( unsigned int itemId )
 
       mItemPool.insert( newItem );
 
-      SetupActor( newItem, 0.0f/*immediate*/ );
+      SetupActor( newItem, durationSeconds );
       Self().Add( actor );
     }
   }
@@ -1115,6 +1114,7 @@ void ItemView::ApplyConstraints(Actor& actor, ItemLayout& layout, unsigned int i
                                                       ParentSource( Actor::SIZE ),
                                                       wrapped );
     constraint.SetApplyTime(duration);
+    constraint.SetAlphaFunction(mDefaultAlphaFunction);
 
     actor.ApplyConstraint(constraint);
   }
@@ -1130,6 +1130,7 @@ void ItemView::ApplyConstraints(Actor& actor, ItemLayout& layout, unsigned int i
                                                          ParentSource( Actor::SIZE ),
                                                          wrapped );
     constraint.SetApplyTime(duration);
+    constraint.SetAlphaFunction(mDefaultAlphaFunction);
 
     actor.ApplyConstraint(constraint);
   }
@@ -1145,6 +1146,7 @@ void ItemView::ApplyConstraints(Actor& actor, ItemLayout& layout, unsigned int i
                                                       ParentSource( Actor::SIZE ),
                                                       wrapped );
     constraint.SetApplyTime(duration);
+    constraint.SetAlphaFunction(mDefaultAlphaFunction);
 
     actor.ApplyConstraint(constraint);
   }
@@ -1160,6 +1162,7 @@ void ItemView::ApplyConstraints(Actor& actor, ItemLayout& layout, unsigned int i
                                                       ParentSource( Actor::SIZE ),
                                                       wrapped );
     constraint.SetApplyTime(duration);
+    constraint.SetAlphaFunction(mDefaultAlphaFunction);
 
     // Release color constraints slowly; this allows ItemView to co-exist with ImageActor fade-in
     constraint.SetRemoveTime(DEFAULT_COLOR_VISIBILITY_REMOVE_TIME);
@@ -1179,6 +1182,7 @@ void ItemView::ApplyConstraints(Actor& actor, ItemLayout& layout, unsigned int i
                                                    ParentSource( Actor::SIZE ),
                                                    wrapped );
     constraint.SetApplyTime(duration);
+    constraint.SetAlphaFunction(mDefaultAlphaFunction);
 
     // Release visibility constraints the same time as the color constraint
     constraint.SetRemoveTime(DEFAULT_COLOR_VISIBILITY_REMOVE_TIME);
@@ -1640,7 +1644,7 @@ void ItemView::ApplyOvershootOverlayConstraints()
 {
   Constraint constraint = Constraint::New<float>( Actor::SIZE_WIDTH,
                                                     ParentSource( mPropertyScrollDirection ),
-                                                    ParentSource( mPropertyOvershoot ),
+                                                    Source( mScrollPositionObject, ScrollConnector::OVERSHOOT ),
                                                     ParentSource( Actor::SIZE ),
                                                     OvershootOverlaySizeConstraint() );
   mOvershootOverlay.ApplyConstraint(constraint);
@@ -1648,14 +1652,14 @@ void ItemView::ApplyOvershootOverlayConstraints()
 
   constraint = Constraint::New<Quaternion>( Actor::ROTATION,
                                             ParentSource( mPropertyScrollDirection ),
-                                            ParentSource( mPropertyOvershoot ),
+                                            Source( mScrollPositionObject, ScrollConnector::OVERSHOOT ),
                                             OvershootOverlayRotationConstraint() );
   mOvershootOverlay.ApplyConstraint(constraint);
 
   constraint = Constraint::New<Vector3>( Actor::POSITION,
                                          ParentSource( Actor::SIZE ),
                                          ParentSource( mPropertyScrollDirection ),
-                                         ParentSource( mPropertyOvershoot ),
+                                         Source( mScrollPositionObject, ScrollConnector::OVERSHOOT ),
                                          OvershootOverlayPositionConstraint() );
   mOvershootOverlay.ApplyConstraint(constraint);
 
@@ -1667,7 +1671,7 @@ void ItemView::ApplyOvershootOverlayConstraints()
   int effectOvershootPropertyIndex = mOvershootEffect.GetPropertyIndex(mOvershootEffect.GetOvershootPropertyName());
   Actor self = Self();
   constraint = Constraint::New<float>( effectOvershootPropertyIndex,
-                                       Source( self, mPropertyOvershoot ),
+                                       Source( mScrollPositionObject, ScrollConnector::OVERSHOOT ),
                                        EqualToConstraint() );
   mOvershootEffect.ApplyConstraint(constraint);
 }
@@ -1706,13 +1710,13 @@ void ItemView::AnimateScrollOvershoot(float overshootAmount, bool animateBack)
   }
 
   Actor self = Self();
-  float currentOvershoot = self.GetProperty<float>(mPropertyOvershoot);
+  float currentOvershoot = mScrollPositionObject.GetProperty<float>(ScrollConnector::OVERSHOOT);
   float duration = DEFAULT_OVERSHOOT_ANIMATION_DURATION * (animatingOn ? (1.0f - fabsf(currentOvershoot)) : fabsf(currentOvershoot));
 
   RemoveAnimation(mScrollOvershootAnimation);
   mScrollOvershootAnimation = Animation::New(duration);
   mScrollOvershootAnimation.FinishedSignal().Connect(this, &ItemView::OnOvershootOnFinished);
-  mScrollOvershootAnimation.AnimateTo( Property(self, mPropertyOvershoot), overshootAmount, TimePeriod(0.0f, duration) );
+  mScrollOvershootAnimation.AnimateTo( Property(mScrollPositionObject, ScrollConnector::OVERSHOOT), overshootAmount, TimePeriod(0.0f, duration) );
   mScrollOvershootAnimation.Play();
 
   mAnimatingOvershootOn = animatingOn;