2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/controls/scrollable/item-view/item-view-impl.h>
22 #include <cstring> // for strcmp
24 #include <dali/public-api/animation/constraint.h>
25 #include <dali/public-api/animation/constraints.h>
26 #include <dali/devel-api/common/set-wrapper.h>
27 #include <dali/public-api/common/stage.h>
28 #include <dali/public-api/events/wheel-event.h>
29 #include <dali/public-api/events/touch-event.h>
30 #include <dali/public-api/object/type-registry.h>
31 #include <dali/devel-api/object/type-registry-helper.h>
34 #include <dali-toolkit/public-api/controls/scroll-bar/scroll-bar.h>
35 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-factory.h>
36 #include <dali-toolkit/internal/controls/scrollable/bouncing-effect-actor.h>
42 namespace // Unnamed namespace
45 const float DEFAULT_MINIMUM_SWIPE_SPEED = 1.0f;
46 const float DEFAULT_MINIMUM_SWIPE_DISTANCE = 3.0f;
47 const float DEFAULT_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION = 0.1f;
49 const float DEFAULT_MINIMUM_SWIPE_DURATION = 0.45f;
50 const float DEFAULT_MAXIMUM_SWIPE_DURATION = 2.6f;
52 const float DEFAULT_REFRESH_INTERVAL_LAYOUT_POSITIONS = 20.0f; // 1 updates per 20 items
53 const int WHEEL_EVENT_FINISHED_TIME_OUT = 500; // 0.5 second
55 const float DEFAULT_ANCHORING_DURATION = 1.0f; // 1 second
57 const float MILLISECONDS_PER_SECONDS = 1000.0f;
59 const float OVERSHOOT_BOUNCE_ACTOR_RESIZE_THRESHOLD = 180.0f;
60 const Vector4 OVERSHOOT_OVERLAY_NINE_PATCH_BORDER(0.0f, 0.0f, 1.0f, 12.0f);
61 const float DEFAULT_KEYBOARD_FOCUS_SCROLL_DURATION = 0.2f;
64 * Local helper to convert pan distance (in actor coordinates) to the layout-specific scrolling direction
66 float CalculateScrollDistance(Vector2 panDistance, Toolkit::ItemLayout& layout)
68 Radian scrollDirection(layout.GetScrollDirection());
70 float cosTheta = cosf(scrollDirection);
71 float sinTheta = sinf(scrollDirection);
73 return panDistance.x * sinTheta + panDistance.y * cosTheta;
76 // Overshoot overlay constraints
77 struct OvershootOverlaySizeConstraint
79 OvershootOverlaySizeConstraint( float height )
80 : mOvershootHeight( height )
84 void operator()( Vector3& current, const PropertyInputContainer& inputs )
86 const Vector2& parentScrollDirection = inputs[0]->GetVector2();
87 const Toolkit::ControlOrientation::Type& layoutOrientation = static_cast<Toolkit::ControlOrientation::Type>(inputs[1]->GetInteger());
88 const Vector3& parentSize = inputs[2]->GetVector3();
90 if(Toolkit::IsVertical(layoutOrientation))
92 current.width = fabsf(parentScrollDirection.y) > Math::MACHINE_EPSILON_1 ? parentSize.x : parentSize.y;
96 current.width = fabsf(parentScrollDirection.x) > Math::MACHINE_EPSILON_1 ? parentSize.y : parentSize.x;
99 current.height = ( current.width > OVERSHOOT_BOUNCE_ACTOR_RESIZE_THRESHOLD ) ? mOvershootHeight : mOvershootHeight*0.5f;
102 float mOvershootHeight;
105 void OvershootOverlayRotationConstraint( Quaternion& current, const PropertyInputContainer& inputs )
107 const Vector2& parentScrollDirection = inputs[0]->GetVector2();
108 const Toolkit::ControlOrientation::Type& layoutOrientation = static_cast<Toolkit::ControlOrientation::Type>(inputs[1]->GetInteger());
109 const float parentOvershoot = inputs[2]->GetFloat();
111 float multiplier = 0;
112 if(Toolkit::IsVertical(layoutOrientation))
114 if(fabsf(parentScrollDirection.y) <= Math::MACHINE_EPSILON_1)
116 if( (layoutOrientation == Toolkit::ControlOrientation::Up && parentOvershoot < Math::MACHINE_EPSILON_0)
117 || (layoutOrientation == Toolkit::ControlOrientation::Down && parentOvershoot > Math::MACHINE_EPSILON_0) )
126 else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.y > Math::MACHINE_EPSILON_0)
127 || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.y < Math::MACHINE_EPSILON_0) )
138 if(fabsf(parentScrollDirection.x) <= Math::MACHINE_EPSILON_1)
140 if( (layoutOrientation == Toolkit::ControlOrientation::Left && parentOvershoot > Math::MACHINE_EPSILON_0)
141 ||(layoutOrientation == Toolkit::ControlOrientation::Right && parentOvershoot < Math::MACHINE_EPSILON_0) )
150 else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.x > Math::MACHINE_EPSILON_0)
151 || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.x < Math::MACHINE_EPSILON_0) )
161 current = Quaternion( Radian( multiplier * Math::PI ), Vector3::ZAXIS );
164 void OvershootOverlayPositionConstraint( Vector3& current, const PropertyInputContainer& inputs )
166 const Vector3& parentSize = inputs[0]->GetVector3();
167 const Vector2& parentScrollDirection = inputs[1]->GetVector2();
168 const Toolkit::ControlOrientation::Type& layoutOrientation = static_cast<Toolkit::ControlOrientation::Type>(inputs[2]->GetInteger());
169 const float parentOvershoot = inputs[3]->GetFloat();
171 Vector3 relativeOffset;
173 if(Toolkit::IsVertical(layoutOrientation))
175 if(fabsf(parentScrollDirection.y) <= Math::MACHINE_EPSILON_1)
177 if( (layoutOrientation == Toolkit::ControlOrientation::Up && parentOvershoot < Math::MACHINE_EPSILON_0)
178 || (layoutOrientation == Toolkit::ControlOrientation::Down && parentOvershoot > Math::MACHINE_EPSILON_0) )
180 relativeOffset = Vector3(1.0f, 0.0f, 0.0f);
184 relativeOffset =Vector3(0.0f, 1.0f, 0.0f);
187 else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.y > Math::MACHINE_EPSILON_0)
188 || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.y < Math::MACHINE_EPSILON_0) )
190 relativeOffset = Vector3(0.0f, 0.0f, 0.0f);
194 relativeOffset = Vector3(1.0f, 1.0f, 0.0f);
199 if(fabsf(parentScrollDirection.x) <= Math::MACHINE_EPSILON_1)
201 if( (layoutOrientation == Toolkit::ControlOrientation::Left && parentOvershoot < Math::MACHINE_EPSILON_0)
202 || (layoutOrientation == Toolkit::ControlOrientation::Right && parentOvershoot > Math::MACHINE_EPSILON_0) )
204 relativeOffset = Vector3(0.0f, 0.0f, 0.0f);
208 relativeOffset = Vector3(1.0f, 1.0f, 0.0f);
211 else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.x > Math::MACHINE_EPSILON_0)
212 || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.x < Math::MACHINE_EPSILON_0) )
214 relativeOffset = Vector3(0.0f, 1.0f, 0.0f);
218 relativeOffset = Vector3(1.0f, 0.0f, 0.0f);
222 current = relativeOffset * parentSize;
225 void OvershootOverlayVisibilityConstraint( bool& current, const PropertyInputContainer& inputs )
227 current = inputs[0]->GetBoolean();
230 } // unnamed namespace
241 namespace // unnamed namespace
246 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ItemView, Toolkit::Scrollable, NULL)
248 DALI_PROPERTY_REGISTRATION( Toolkit, ItemView, "minimumSwipeSpeed", FLOAT, MINIMUM_SWIPE_SPEED )
249 DALI_PROPERTY_REGISTRATION( Toolkit, ItemView, "minimumSwipeDistance", FLOAT, MINIMUM_SWIPE_DISTANCE )
250 DALI_PROPERTY_REGISTRATION( Toolkit, ItemView, "wheelScrollDistanceStep", FLOAT, WHEEL_SCROLL_DISTANCE_STEP )
251 DALI_PROPERTY_REGISTRATION( Toolkit, ItemView, "snapToItemEnabled", BOOLEAN, SNAP_TO_ITEM_ENABLED )
252 DALI_PROPERTY_REGISTRATION( Toolkit, ItemView, "refreshInterval", FLOAT, REFRESH_INTERVAL )
254 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "layoutPosition", FLOAT, LAYOUT_POSITION)
255 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "scrollSpeed", FLOAT, SCROLL_SPEED)
256 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "overshoot", FLOAT, OVERSHOOT)
257 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "scrollDirection", VECTOR2, SCROLL_DIRECTION)
258 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "layoutOrientation", INTEGER, LAYOUT_ORIENTATION)
259 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "scrollContentSize", FLOAT, SCROLL_CONTENT_SIZE)
261 DALI_SIGNAL_REGISTRATION( Toolkit, ItemView, "layoutActivated", LAYOUT_ACTIVATED_SIGNAL )
263 DALI_TYPE_REGISTRATION_END()
265 bool FindById( const ItemContainer& items, ItemId id )
267 for( ConstItemIter iter = items.begin(); items.end() != iter; ++iter )
269 if( iter->first == id )
278 } // unnamed namespace
280 Dali::Toolkit::ItemView ItemView::New(ItemFactory& factory)
282 // Create the implementation
283 ItemViewPtr itemView(new ItemView(factory));
285 // Pass ownership to CustomActor via derived handle
286 Dali::Toolkit::ItemView handle(*itemView);
288 // Second-phase init of the implementation
289 // This can only be done after the CustomActor connection has been made...
290 itemView->Initialize();
295 ItemView::ItemView(ItemFactory& factory)
296 : Scrollable( ControlBehaviour( DISABLE_SIZE_NEGOTIATION | REQUIRES_WHEEL_EVENTS | REQUIRES_KEYBOARD_NAVIGATION_SUPPORT ) ),
297 mItemFactory(factory),
298 mItemsParentOrigin(ParentOrigin::CENTER),
299 mItemsAnchorPoint(AnchorPoint::CENTER),
300 mTotalPanDisplacement(Vector2::ZERO),
302 mAnchoringDuration(DEFAULT_ANCHORING_DURATION),
303 mRefreshIntervalLayoutPositions(0.0f),
304 mMinimumSwipeSpeed(DEFAULT_MINIMUM_SWIPE_SPEED),
305 mMinimumSwipeDistance(DEFAULT_MINIMUM_SWIPE_DISTANCE),
306 mWheelScrollDistanceStep(0.0f),
307 mScrollDistance(0.0f),
309 mScrollOvershoot(0.0f),
310 mGestureState(Gesture::Clear),
311 mAnimatingOvershootOn(false),
312 mAnimateOvershootOff(false),
313 mAnchoringEnabled(false),
314 mRefreshOrderHint(true/*Refresh item 0 first*/),
317 mRefreshEnabled(true),
322 void ItemView::OnInitialize()
326 Vector2 stageSize = Stage::GetCurrent().GetSize();
327 mWheelScrollDistanceStep = stageSize.y * DEFAULT_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION;
329 EnableGestureDetection(Gesture::Type(Gesture::Pan));
331 mWheelEventFinishedTimer = Timer::New( WHEEL_EVENT_FINISHED_TIME_OUT );
332 mWheelEventFinishedTimer.TickSignal().Connect( this, &ItemView::OnWheelEventFinished );
334 SetRefreshInterval(DEFAULT_REFRESH_INTERVAL_LAYOUT_POSITIONS);
337 ItemView::~ItemView()
341 unsigned int ItemView::GetLayoutCount() const
343 return mLayouts.size();
346 void ItemView::AddLayout(ItemLayout& layout)
348 mLayouts.push_back(ItemLayoutPtr(&layout));
351 void ItemView::RemoveLayout(unsigned int layoutIndex)
353 DALI_ASSERT_ALWAYS(layoutIndex < mLayouts.size());
355 if (mActiveLayout == mLayouts[layoutIndex].Get())
357 mActiveLayout = NULL;
360 mLayouts.erase(mLayouts.begin() + layoutIndex);
363 ItemLayoutPtr ItemView::GetLayout(unsigned int layoutIndex) const
365 return mLayouts[layoutIndex];
368 ItemLayoutPtr ItemView::GetActiveLayout() const
370 return ItemLayoutPtr(mActiveLayout);
373 float ItemView::GetCurrentLayoutPosition(unsigned int itemId) const
375 return Self().GetProperty<float>( Toolkit::ItemView::Property::LAYOUT_POSITION ) + static_cast<float>( itemId );
378 void ItemView::ActivateLayout(unsigned int layoutIndex, const Vector3& targetSize, float durationSeconds)
380 DALI_ASSERT_ALWAYS(layoutIndex < mLayouts.size());
382 mRefreshEnabled = false;
386 // The ItemView size should match the active layout size
387 self.SetSize(targetSize);
388 mActiveLayoutTargetSize = targetSize;
390 // Switch to the new layout
391 mActiveLayout = mLayouts[layoutIndex].Get();
393 // Move the items to the new layout positions...
395 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
397 unsigned int itemId = iter->first;
398 Actor actor = iter->second;
400 // Remove constraints from previous layout
401 actor.RemoveConstraints();
404 mActiveLayout->GetItemSize( itemId, targetSize, size );
405 actor.SetSize( size.GetVectorXY() );
407 mActiveLayout->ApplyConstraints(actor, itemId, targetSize, Self() );
410 // Refresh the new layout
411 ItemRange range = GetItemRange(*mActiveLayout, targetSize, GetCurrentLayoutPosition(0), false/* don't reserve extra*/);
412 AddActorsWithinRange( range, targetSize );
414 // Scroll to an appropriate layout position
416 bool scrollAnimationNeeded(false);
417 float firstItemScrollPosition(0.0f);
419 float current = GetCurrentLayoutPosition(0);
420 float minimum = ClampFirstItemPosition(current, targetSize, *mActiveLayout);
422 if (current < minimum)
424 scrollAnimationNeeded = true;
425 firstItemScrollPosition = minimum;
427 else if (mAnchoringEnabled)
429 scrollAnimationNeeded = true;
430 firstItemScrollPosition = mActiveLayout->GetClosestAnchorPosition(current);
433 if (scrollAnimationNeeded)
435 RemoveAnimation(mScrollAnimation);
436 mScrollAnimation = Animation::New(durationSeconds);
437 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, AlphaFunction::EASE_OUT );
438 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnLayoutActivationScrollFinished);
439 mScrollAnimation.Play();
443 // Emit the layout activated signal
444 mLayoutActivatedSignal.Emit();
447 AnimateScrollOvershoot(0.0f);
448 mScrollOvershoot = 0.0f;
450 Radian scrollDirection(mActiveLayout->GetScrollDirection());
451 self.SetProperty(Toolkit::ItemView::Property::SCROLL_DIRECTION, Vector2(sinf(scrollDirection), cosf(scrollDirection)));
452 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_ORIENTATION, static_cast<int>(mActiveLayout->GetOrientation()));
453 self.SetProperty(Toolkit::ItemView::Property::SCROLL_SPEED, mScrollSpeed);
455 CalculateDomainSize(targetSize);
458 void ItemView::DeactivateCurrentLayout()
462 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
464 Actor actor = iter->second;
465 actor.RemoveConstraints();
468 mActiveLayout = NULL;
472 void ItemView::OnRefreshNotification(PropertyNotification& source)
474 // Cancel scroll animation to prevent any fighting of setting the scroll position property by scroll bar during fast scroll.
475 if(!mRefreshEnabled && mScrollAnimation)
477 RemoveAnimation(mScrollAnimation);
480 // Only cache extra items when it is not a fast scroll
481 DoRefresh(GetCurrentLayoutPosition(0), mRefreshEnabled || mScrollAnimation);
484 void ItemView::Refresh()
486 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter )
488 ReleaseActor( iter->first, iter->second );
492 DoRefresh(GetCurrentLayoutPosition(0), true);
495 void ItemView::DoRefresh(float currentLayoutPosition, bool cacheExtra)
499 ItemRange range = GetItemRange(*mActiveLayout, mActiveLayoutTargetSize, currentLayoutPosition, cacheExtra/*reserve extra*/);
500 RemoveActorsOutsideRange( range );
501 AddActorsWithinRange( range, Self().GetCurrentSize() );
503 mScrollUpdatedSignal.Emit( Vector2(0.0f, currentLayoutPosition) );
507 void ItemView::SetMinimumSwipeSpeed(float speed)
509 mMinimumSwipeSpeed = speed;
512 float ItemView::GetMinimumSwipeSpeed() const
514 return mMinimumSwipeSpeed;
517 void ItemView::SetMinimumSwipeDistance(float distance)
519 mMinimumSwipeDistance = distance;
522 float ItemView::GetMinimumSwipeDistance() const
524 return mMinimumSwipeDistance;
527 void ItemView::SetWheelScrollDistanceStep(float step)
529 mWheelScrollDistanceStep = step;
532 float ItemView::GetWheelScrollDistanceStep() const
534 return mWheelScrollDistanceStep;
537 void ItemView::SetAnchoring(bool enabled)
539 mAnchoringEnabled = enabled;
542 bool ItemView::GetAnchoring() const
544 return mAnchoringEnabled;
547 void ItemView::SetAnchoringDuration(float durationSeconds)
549 mAnchoringDuration = durationSeconds;
552 float ItemView::GetAnchoringDuration() const
554 return mAnchoringDuration;
557 void ItemView::SetRefreshInterval(float intervalLayoutPositions)
559 if( !Equals(mRefreshIntervalLayoutPositions, intervalLayoutPositions) )
561 mRefreshIntervalLayoutPositions = intervalLayoutPositions;
564 if(mRefreshNotification)
566 self.RemovePropertyNotification(mRefreshNotification);
568 mRefreshNotification = self.AddPropertyNotification( Toolkit::ItemView::Property::LAYOUT_POSITION, StepCondition(mRefreshIntervalLayoutPositions, 0.0f) );
569 mRefreshNotification.NotifySignal().Connect( this, &ItemView::OnRefreshNotification );
573 float ItemView::GetRefreshInterval() const
575 return mRefreshIntervalLayoutPositions;
578 void ItemView::SetRefreshEnabled(bool enabled)
580 mRefreshEnabled = enabled;
583 Actor ItemView::GetItem(unsigned int itemId) const
587 ConstItemPoolIter iter = mItemPool.find( itemId );
588 if( iter != mItemPool.end() )
590 actor = iter->second;
596 unsigned int ItemView::GetItemId( Actor actor ) const
598 unsigned int itemId( 0 );
600 for ( ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter )
602 if( iter->second == actor )
604 itemId = iter->first;
612 void ItemView::InsertItem( Item newItem, float durationSeconds )
615 Vector3 layoutSize = Self().GetCurrentSize();
617 Actor displacedActor;
618 ItemPoolIter afterDisplacedIter = mItemPool.end();
620 ItemPoolIter foundIter = mItemPool.find( newItem.first );
621 if( mItemPool.end() != foundIter )
623 SetupActor( newItem, layoutSize );
624 Self().Add( newItem.second );
626 displacedActor = foundIter->second;
627 foundIter->second = newItem.second;
629 afterDisplacedIter = ++foundIter;
633 // Inserting before the existing item range?
634 ItemPoolIter iter = mItemPool.begin();
635 if( iter != mItemPool.end() &&
636 iter->first > newItem.first )
638 displacedActor = iter->second;
639 mItemPool.erase( iter++ ); // iter is still valid after the erase
641 afterDisplacedIter = iter;
647 // Move the existing actors to make room
648 for( ItemPoolIter iter = afterDisplacedIter; mItemPool.end() != iter; ++iter )
650 Actor temp = iter->second;
651 iter->second = displacedActor;
652 displacedActor = temp;
654 iter->second.RemoveConstraints();
655 mActiveLayout->ApplyConstraints( iter->second, iter->first, layoutSize, Self() );
659 ItemPool::reverse_iterator lastIter = mItemPool.rbegin();
660 if ( lastIter != mItemPool.rend() )
662 ItemId lastId = lastIter->first;
663 Item lastItem( lastId + 1, displacedActor );
664 mItemPool.insert( lastItem );
666 lastItem.second.RemoveConstraints();
667 mActiveLayout->ApplyConstraints( lastItem.second, lastItem.first, layoutSize, Self() );
671 CalculateDomainSize( layoutSize );
673 mAddingItems = false;
676 void ItemView::InsertItems( const ItemContainer& newItems, float durationSeconds )
679 Vector3 layoutSize = Self().GetCurrentSize();
681 // Insert from lowest id to highest
682 std::set<Item> sortedItems;
683 for( ConstItemIter iter = newItems.begin(); newItems.end() != iter; ++iter )
685 sortedItems.insert( *iter );
688 for( std::set<Item>::iterator iter = sortedItems.begin(); sortedItems.end() != iter; ++iter )
690 Self().Add( iter->second );
692 ItemPoolIter foundIter = mItemPool.find( iter->first );
693 if( mItemPool.end() != foundIter )
695 Actor moveMe = foundIter->second;
696 foundIter->second = iter->second;
698 // Move the existing actors to make room
699 for( ItemPoolIter iter = ++foundIter; mItemPool.end() != iter; ++iter )
701 Actor temp = iter->second;
702 iter->second = moveMe;
707 ItemId lastId = mItemPool.rbegin()->first;
708 Item lastItem( lastId + 1, moveMe );
709 mItemPool.insert( lastItem );
713 mItemPool.insert( *iter );
717 // Relayout everything
718 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
721 if( FindById( newItems, iter->first ) )
723 SetupActor( *iter, layoutSize );
727 iter->second.RemoveConstraints();
728 mActiveLayout->ApplyConstraints( iter->second, iter->first, layoutSize, Self() );
732 CalculateDomainSize( layoutSize );
734 mAddingItems = false;
737 void ItemView::RemoveItem( unsigned int itemId, float durationSeconds )
739 bool actorsReordered = RemoveActor( itemId );
740 if( actorsReordered )
742 ReapplyAllConstraints();
748 void ItemView::RemoveItems( const ItemIdContainer& itemIds, float durationSeconds )
750 bool actorsReordered( false );
752 // Remove from highest id to lowest
753 set<ItemId> sortedItems;
754 for( ConstItemIdIter iter = itemIds.begin(); itemIds.end() != iter; ++iter )
756 sortedItems.insert( *iter );
759 for( set<ItemId>::reverse_iterator iter = sortedItems.rbegin(); sortedItems.rend() != iter; ++iter )
761 if( RemoveActor( *iter ) )
763 actorsReordered = true;
767 if( actorsReordered )
769 ReapplyAllConstraints();
775 bool ItemView::RemoveActor(unsigned int itemId)
777 bool reordered( false );
779 ItemPoolIter removeIter = mItemPool.find( itemId );
780 if( removeIter != mItemPool.end() )
782 ReleaseActor(itemId, removeIter->second);
786 // Removing before the existing item range?
787 ItemPoolIter iter = mItemPool.begin();
788 if( iter != mItemPool.end() &&
789 iter->first > itemId )
791 // In order to decrement the first visible item ID
792 mItemPool.insert( Item(iter->first - 1, Actor()) );
794 removeIter = mItemPool.begin();
798 if( removeIter != mItemPool.end() )
802 // Adjust the remaining item IDs, for example if item 2 is removed:
803 // Initial actors: After insert:
804 // ID 1 - ActorA ID 1 - ActorA
805 // ID 2 - ActorB ID 2 - ActorC (previously ID 3)
806 // ID 3 - ActorC ID 3 - ActorB (previously ID 4)
808 for (ItemPoolIter iter = removeIter; iter != mItemPool.end(); ++iter)
810 if( iter->first < mItemPool.rbegin()->first )
812 iter->second = mItemPool[ iter->first + 1 ];
816 mItemPool.erase( iter );
825 void ItemView::ReplaceItem( Item replacementItem, float durationSeconds )
828 Vector3 layoutSize = Self().GetCurrentSize();
830 SetupActor( replacementItem, layoutSize );
831 Self().Add( replacementItem.second );
833 const ItemPoolIter iter = mItemPool.find( replacementItem.first );
834 if( mItemPool.end() != iter )
836 ReleaseActor(iter->first, iter->second);
837 iter->second = replacementItem.second;
841 mItemPool.insert( replacementItem );
844 CalculateDomainSize( layoutSize );
846 mAddingItems = false;
849 void ItemView::ReplaceItems( const ItemContainer& replacementItems, float durationSeconds )
851 for( ConstItemIter iter = replacementItems.begin(); replacementItems.end() != iter; ++iter )
853 ReplaceItem( *iter, durationSeconds );
857 void ItemView::RemoveActorsOutsideRange( ItemRange range )
859 // Remove unwanted actors from the ItemView & ItemPool
860 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); )
862 unsigned int current = iter->first;
864 if( ! range.Within( current ) )
866 ReleaseActor(iter->first, iter->second);
868 mItemPool.erase( iter++ ); // erase invalidates the return value of post-increment; iter remains valid
877 void ItemView::AddActorsWithinRange( ItemRange range, const Vector3& layoutSize )
879 range.end = std::min(mItemFactory.GetNumberOfItems(), range.end);
881 // The order of addition depends on the scroll direction.
882 if (mRefreshOrderHint)
884 for (unsigned int itemId = range.begin; itemId < range.end; ++itemId)
886 AddNewActor( itemId, layoutSize );
891 for (unsigned int itemId = range.end; itemId > range.begin; --itemId)
893 AddNewActor( itemId-1, layoutSize );
897 // Total number of items may change dynamically.
898 // Always recalculate the domain size to reflect that.
899 CalculateDomainSize(Self().GetCurrentSize());
902 void ItemView::AddNewActor( unsigned int itemId, const Vector3& layoutSize )
906 if( mItemPool.end() == mItemPool.find( itemId ) )
908 Actor actor = mItemFactory.NewItem( itemId );
912 Item newItem( itemId, actor );
914 mItemPool.insert( newItem );
916 SetupActor( newItem, layoutSize );
921 mAddingItems = false;
924 void ItemView::SetupActor( Item item, const Vector3& layoutSize )
926 item.second.SetParentOrigin( mItemsParentOrigin );
927 item.second.SetAnchorPoint( mItemsAnchorPoint );
932 mActiveLayout->GetItemSize( item.first, mActiveLayoutTargetSize, size );
933 item.second.SetSize( size.GetVectorXY() );
935 mActiveLayout->ApplyConstraints( item.second, item.first, layoutSize, Self() );
939 void ItemView::ReleaseActor( ItemId item, Actor actor )
941 Self().Remove( actor );
942 mItemFactory.ItemReleased(item, actor);
945 ItemRange ItemView::GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra)
947 unsigned int itemCount = mItemFactory.GetNumberOfItems();
949 ItemRange available(0u, itemCount);
951 ItemRange range = layout.GetItemsWithinArea( layoutPosition, layoutSize );
955 // Add the reserve items for scrolling
956 unsigned int extra = layout.GetReserveItemCount(layoutSize);
957 range.begin = (range.begin >= extra) ? (range.begin - extra) : 0u;
961 return range.Intersection(available);
964 void ItemView::OnChildAdd(Actor& child)
968 // We don't want to do this downcast check for any item added by ItemView itself.
969 Dali::Toolkit::ScrollBar scrollBar = Dali::Toolkit::ScrollBar::DownCast(child);
972 scrollBar.SetScrollPropertySource(Self(),
973 Toolkit::ItemView::Property::LAYOUT_POSITION,
974 Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_Y,
975 Toolkit::Scrollable::Property::SCROLL_POSITION_MAX_Y,
976 Toolkit::ItemView::Property::SCROLL_CONTENT_SIZE);
981 bool ItemView::OnTouchEvent(const TouchEvent& event)
983 // Ignore events with multiple-touch points
984 if (event.GetPointCount() != 1)
989 if (event.GetPoint(0).state == TouchPoint::Down)
991 // Cancel ongoing scrolling etc.
992 mGestureState = Gesture::Clear;
994 mScrollDistance = 0.0f;
996 Self().SetProperty(Toolkit::ItemView::Property::SCROLL_SPEED, mScrollSpeed);
998 mScrollOvershoot = 0.0f;
999 AnimateScrollOvershoot(0.0f);
1001 if(mScrollAnimation)
1003 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1006 RemoveAnimation(mScrollAnimation);
1009 return true; // consume since we're potentially scrolling
1012 bool ItemView::OnWheelEvent(const WheelEvent& event)
1014 // Respond the wheel event to scroll
1017 Actor self = Self();
1018 const Vector3 layoutSize = Self().GetCurrentSize();
1019 float layoutPositionDelta = GetCurrentLayoutPosition(0) - (event.z * mWheelScrollDistanceStep * mActiveLayout->GetScrollSpeedFactor());
1020 float firstItemScrollPosition = ClampFirstItemPosition(layoutPositionDelta, layoutSize, *mActiveLayout);
1022 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1024 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1025 mRefreshEnabled = true;
1028 if (mWheelEventFinishedTimer.IsRunning())
1030 mWheelEventFinishedTimer.Stop();
1033 mWheelEventFinishedTimer.Start();
1038 bool ItemView::OnWheelEventFinished()
1042 RemoveAnimation(mScrollAnimation);
1044 // No more wheel events coming. Do the anchoring if enabled.
1045 mScrollAnimation = DoAnchoring();
1046 if (mScrollAnimation)
1048 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1049 mScrollAnimation.Play();
1053 mScrollOvershoot = 0.0f;
1054 AnimateScrollOvershoot(0.0f);
1056 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1063 void ItemView::ReapplyAllConstraints()
1065 Vector3 layoutSize = Self().GetCurrentSize();
1067 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1069 unsigned int id = iter->first;
1070 Actor actor = iter->second;
1072 actor.RemoveConstraints();
1073 mActiveLayout->ApplyConstraints(actor, id, layoutSize, Self());
1077 void ItemView::OnItemsRemoved()
1079 CalculateDomainSize(Self().GetCurrentSize());
1081 // Adjust scroll-position after an item is removed
1084 float firstItemScrollPosition = ClampFirstItemPosition(GetCurrentLayoutPosition(0), Self().GetCurrentSize(), *mActiveLayout);
1085 Self().SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1089 float ItemView::ClampFirstItemPosition( float targetPosition, const Vector3& targetSize, ItemLayout& layout, bool updateOvershoot )
1091 Actor self = Self();
1092 float minLayoutPosition = layout.GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), targetSize);
1093 float clamppedPosition = std::min(0.0f, std::max(minLayoutPosition, targetPosition));
1094 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1096 if( updateOvershoot )
1098 mScrollOvershoot = targetPosition - clamppedPosition;
1101 return clamppedPosition;
1104 void ItemView::OnPan( const PanGesture& gesture )
1106 Actor self = Self();
1107 const Vector3 layoutSize = Self().GetCurrentSize();
1109 RemoveAnimation(mScrollAnimation);
1111 // Short-circuit if there is no active layout
1114 mGestureState = Gesture::Clear;
1118 mGestureState = gesture.state;
1120 switch (mGestureState)
1122 case Gesture::Finished:
1125 if (fabsf(mScrollDistance) > mMinimumSwipeDistance &&
1126 mScrollSpeed > mMinimumSwipeSpeed)
1128 float direction = (mScrollDistance < 0.0f) ? -1.0f : 1.0f;
1130 mRefreshOrderHint = true;
1132 float currentLayoutPosition = GetCurrentLayoutPosition(0);
1133 float firstItemScrollPosition = ClampFirstItemPosition(currentLayoutPosition + mScrollSpeed * direction,
1137 if (mAnchoringEnabled)
1139 firstItemScrollPosition = mActiveLayout->GetClosestAnchorPosition(firstItemScrollPosition);
1142 RemoveAnimation(mScrollAnimation);
1144 float flickAnimationDuration = Clamp( mActiveLayout->GetItemFlickAnimationDuration() * std::max(1.0f, fabsf(firstItemScrollPosition - GetCurrentLayoutPosition(0)))
1145 , DEFAULT_MINIMUM_SWIPE_DURATION, DEFAULT_MAXIMUM_SWIPE_DURATION);
1147 mScrollAnimation = Animation::New(flickAnimationDuration);
1148 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION ), firstItemScrollPosition, AlphaFunction::EASE_OUT );
1149 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::SCROLL_SPEED), 0.0f, AlphaFunction::EASE_OUT );
1152 // Check whether it has already scrolled to the end
1153 if(fabs(currentLayoutPosition - firstItemScrollPosition) > Math::MACHINE_EPSILON_0)
1155 AnimateScrollOvershoot(0.0f);
1159 // Anchoring may be triggered when there was no swipe
1160 if (!mScrollAnimation)
1162 mScrollAnimation = DoAnchoring();
1165 // Reset the overshoot if no scroll animation.
1166 if (!mScrollAnimation)
1168 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1170 AnimateScrollOvershoot(0.0f, false);
1175 case Gesture::Started: // Fall through
1177 mTotalPanDisplacement = Vector2::ZERO;
1178 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1179 mRefreshEnabled = true;
1182 case Gesture::Continuing:
1184 mScrollDistance = CalculateScrollDistance(gesture.displacement, *mActiveLayout);
1185 mScrollSpeed = Clamp((gesture.GetSpeed() * gesture.GetSpeed() * mActiveLayout->GetFlickSpeedFactor() * MILLISECONDS_PER_SECONDS), 0.0f, mActiveLayout->GetMaximumSwipeSpeed());
1187 // Refresh order depends on the direction of the scroll; negative is towards the last item.
1188 mRefreshOrderHint = mScrollDistance < 0.0f;
1190 float layoutPositionDelta = GetCurrentLayoutPosition(0) + (mScrollDistance * mActiveLayout->GetScrollSpeedFactor());
1192 float firstItemScrollPosition = ClampFirstItemPosition(layoutPositionDelta, layoutSize, *mActiveLayout);
1194 float currentOvershoot = self.GetProperty<float>(Toolkit::ItemView::Property::OVERSHOOT);
1196 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1198 if( ( firstItemScrollPosition >= 0.0f &&
1199 currentOvershoot < 1.0f ) ||
1200 ( firstItemScrollPosition <= mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), layoutSize) &&
1201 currentOvershoot > -1.0f ) )
1203 mTotalPanDisplacement += gesture.displacement;
1206 mScrollOvershoot = CalculateScrollOvershoot();
1208 // If the view is moved in a direction against the overshoot indicator, then the indicator should be animated off.
1209 // First make sure we are not in an animation, otherwise a previously started
1210 // off-animation will be overwritten as the user continues scrolling.
1213 // Check if the movement is against the current overshoot amount (if we are currently displaying the indicator).
1214 if( ( ( mScrollOvershoot > Math::MACHINE_EPSILON_0 ) && ( mScrollDistance < -Math::MACHINE_EPSILON_0 ) ) ||
1215 ( ( mScrollOvershoot < Math::MACHINE_EPSILON_0 ) && ( mScrollDistance > Math::MACHINE_EPSILON_0 ) ) )
1217 // The user has moved against the indicator direction.
1218 // First, we reset the total displacement. This means the overshoot amount will become zero the next frame,
1219 // and if the user starts dragging in the overshoot direction again, the indicator will appear once more.
1220 mTotalPanDisplacement = Vector2::ZERO;
1221 // Animate the overshoot indicator off.
1222 AnimateScrollOvershoot( 0.0f, false );
1226 // Only set the property directly if we are not animating the overshoot away,
1227 // as otherwise this will overwrite the animation generated value.
1228 self.SetProperty( Toolkit::ItemView::Property::OVERSHOOT, mScrollOvershoot );
1234 case Gesture::Cancelled:
1236 mScrollAnimation = DoAnchoring();
1244 if (mScrollAnimation)
1246 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1247 mScrollAnimation.Play();
1251 bool ItemView::OnAccessibilityPan(PanGesture gesture)
1257 Actor ItemView::GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
1259 Actor nextFocusActor;
1263 if(!actor || actor == this->Self())
1265 nextFocusActor = GetItem(nextItemID);
1267 else if(actor && actor.GetParent() == this->Self())
1269 int itemID = GetItemId(actor);
1270 nextItemID = mActiveLayout->GetNextFocusItemID(itemID, mItemFactory.GetNumberOfItems(), direction, loopEnabled);
1271 nextFocusActor = GetItem(nextItemID);
1272 if(nextFocusActor == actor)
1274 // need to pass NULL actor back to focus manager
1275 nextFocusActor.Reset();
1276 return nextFocusActor;
1279 float layoutPosition = mActiveLayout->GetClosestAnchorPosition( GetCurrentLayoutPosition(0) );
1280 Vector3 layoutSize = Self().GetCurrentSize();
1283 // likely the current item is not buffered, so not in our item pool, probably best to get first viewable item
1284 ItemRange viewableItems = mActiveLayout->GetItemsWithinArea(layoutPosition, layoutSize);
1285 nextItemID = viewableItems.begin;
1286 nextFocusActor = GetItem(nextItemID);
1289 return nextFocusActor;
1292 void ItemView::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
1294 // only in this function if our chosen focus actor was actually used
1295 if(commitedFocusableActor)
1297 int nextItemID = GetItemId(commitedFocusableActor);
1298 float layoutPosition = GetCurrentLayoutPosition(0);
1299 Vector3 layoutSize = Self().GetCurrentSize();
1301 float scrollTo = mActiveLayout->GetClosestOnScreenLayoutPosition(nextItemID, layoutPosition, layoutSize);
1302 ScrollTo(Vector2(0.0f, scrollTo), DEFAULT_KEYBOARD_FOCUS_SCROLL_DURATION);
1306 Animation ItemView::DoAnchoring()
1308 Animation anchoringAnimation;
1309 Actor self = Self();
1311 if (mActiveLayout && mAnchoringEnabled)
1313 float anchorPosition = mActiveLayout->GetClosestAnchorPosition( GetCurrentLayoutPosition(0) );
1315 anchoringAnimation = Animation::New(mAnchoringDuration);
1316 anchoringAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), anchorPosition, AlphaFunction::EASE_OUT );
1317 anchoringAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::SCROLL_SPEED), 0.0f, AlphaFunction::EASE_OUT );
1320 AnimateScrollOvershoot(0.0f);
1324 return anchoringAnimation;
1327 void ItemView::OnScrollFinished(Animation& source)
1329 Actor self = Self();
1331 RemoveAnimation(mScrollAnimation); // mScrollAnimation is used to query whether we're scrolling
1333 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1335 if(mIsFlicking && fabsf(mScrollOvershoot) > Math::MACHINE_EPSILON_1)
1337 AnimateScrollOvershoot( mScrollOvershoot > 0.0f ? 1.0f : -1.0f, true);
1341 // Reset the overshoot
1342 AnimateScrollOvershoot( 0.0f );
1344 mIsFlicking = false;
1346 mScrollOvershoot = 0.0f;
1349 void ItemView::OnLayoutActivationScrollFinished(Animation& source)
1351 RemoveAnimation(mScrollAnimation);
1352 mRefreshEnabled = true;
1353 DoRefresh(GetCurrentLayoutPosition(0), true);
1355 // Emit the layout activated signal
1356 mLayoutActivatedSignal.Emit();
1359 void ItemView::OnOvershootOnFinished(Animation& animation)
1361 mAnimatingOvershootOn = false;
1362 mScrollOvershootAnimation.FinishedSignal().Disconnect(this, &ItemView::OnOvershootOnFinished);
1363 RemoveAnimation(mScrollOvershootAnimation);
1364 if(mAnimateOvershootOff)
1366 AnimateScrollOvershoot(0.0f);
1368 mInAnimation = false;
1371 void ItemView::ScrollToItem(unsigned int itemId, float durationSeconds)
1373 Actor self = Self();
1374 const Vector3 layoutSize = Self().GetCurrentSize();
1375 float firstItemScrollPosition = ClampFirstItemPosition(mActiveLayout->GetItemScrollToPosition(itemId), layoutSize, *mActiveLayout);
1377 if(durationSeconds > 0.0f)
1379 RemoveAnimation(mScrollAnimation);
1380 mScrollAnimation = Animation::New(durationSeconds);
1381 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, AlphaFunction::EASE_OUT );
1382 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1383 mScrollAnimation.Play();
1387 self.SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1388 AnimateScrollOvershoot(0.0f);
1391 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1392 mRefreshEnabled = true;
1395 void ItemView::RemoveAnimation(Animation& animation)
1399 // Cease animating, and reset handle.
1405 void ItemView::CalculateDomainSize(const Vector3& layoutSize)
1407 Actor self = Self();
1409 Vector3 firstItemPosition(Vector3::ZERO);
1410 Vector3 lastItemPosition(Vector3::ZERO);
1414 firstItemPosition = mActiveLayout->GetItemPosition( 0,0,layoutSize );
1416 float minLayoutPosition = mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), layoutSize);
1417 lastItemPosition = mActiveLayout->GetItemPosition( fabs(minLayoutPosition),fabs(minLayoutPosition),layoutSize );
1421 if(IsHorizontal(mActiveLayout->GetOrientation()))
1423 domainSize = fabs(firstItemPosition.x - lastItemPosition.x);
1427 domainSize = fabs(firstItemPosition.y - lastItemPosition.y);
1430 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MIN, Vector2::ZERO);
1431 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1433 self.SetProperty(Toolkit::ItemView::Property::SCROLL_CONTENT_SIZE, domainSize);
1435 bool isLayoutScrollable = IsLayoutScrollable(layoutSize);
1436 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL, isLayoutScrollable);
1437 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL, false);
1441 Vector2 ItemView::GetDomainSize() const
1443 Actor self = Self();
1445 float minScrollPosition = self.GetProperty<float>(Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_Y);
1446 float maxScrollPosition = self.GetProperty<float>(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX_Y);
1448 return Vector2(0.0f, fabs(GetScrollPosition(minScrollPosition, self.GetCurrentSize()) - GetScrollPosition(-maxScrollPosition, self.GetCurrentSize())));
1451 bool ItemView::IsLayoutScrollable(const Vector3& layoutSize)
1453 Actor self = Self();
1455 float currentLayoutPosition = ClampFirstItemPosition( GetCurrentLayoutPosition(0), layoutSize, *mActiveLayout, false );
1456 float forwardClampedPosition = ClampFirstItemPosition( currentLayoutPosition + 1.0, layoutSize, *mActiveLayout, false );
1457 float backwardClampedPosition = ClampFirstItemPosition( currentLayoutPosition - 1.0, layoutSize, *mActiveLayout, false );
1459 return (fabs(forwardClampedPosition - backwardClampedPosition) > Math::MACHINE_EPSILON_0);
1462 float ItemView::GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const
1464 Vector3 firstItemPosition( mActiveLayout->GetItemPosition(0, layoutPosition, layoutSize ) );
1465 return IsHorizontal(mActiveLayout->GetOrientation()) ? firstItemPosition.x: firstItemPosition.y;
1468 Vector2 ItemView::GetCurrentScrollPosition() const
1470 return Vector2(0.0f, GetScrollPosition(GetCurrentLayoutPosition(0), Self().GetCurrentSize()));
1473 void ItemView::AddOverlay(Actor actor)
1475 actor.SetDrawMode( DrawMode::OVERLAY_2D );
1479 void ItemView::RemoveOverlay(Actor actor)
1481 Self().Remove(actor);
1484 void ItemView::ScrollTo(const Vector2& position, float duration)
1486 Actor self = Self();
1487 const Vector3 layoutSize = Self().GetCurrentSize();
1489 float firstItemScrollPosition = ClampFirstItemPosition(position.y, layoutSize, *mActiveLayout);
1493 RemoveAnimation(mScrollAnimation);
1494 mScrollAnimation = Animation::New(duration);
1495 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, AlphaFunction::EASE_OUT );
1496 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1497 mScrollAnimation.Play();
1501 self.SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1502 AnimateScrollOvershoot(0.0f);
1505 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1506 mRefreshEnabled = true;
1509 void ItemView::SetOvershootEffectColor( const Vector4& color )
1511 mOvershootEffectColor = color;
1512 if( mOvershootOverlay )
1514 mOvershootOverlay.SetColor( color );
1518 void ItemView::EnableScrollOvershoot( bool enable )
1520 Actor self = Self();
1523 if( !mOvershootOverlay )
1525 Property::Index effectOvershootPropertyIndex = Property::INVALID_INDEX;
1526 mOvershootOverlay = CreateBouncingEffectActor( effectOvershootPropertyIndex );
1527 mOvershootOverlay.SetColor(mOvershootEffectColor);
1528 mOvershootOverlay.SetParentOrigin(ParentOrigin::TOP_LEFT);
1529 mOvershootOverlay.SetAnchorPoint(AnchorPoint::TOP_LEFT);
1530 mOvershootOverlay.SetDrawMode( DrawMode::OVERLAY_2D );
1531 self.Add(mOvershootOverlay);
1533 Constraint constraint = Constraint::New<Vector3>( mOvershootOverlay, Actor::Property::SIZE, OvershootOverlaySizeConstraint(mOvershootSize.height) );
1534 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1535 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1536 constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
1539 mOvershootOverlay.SetSize(mOvershootSize.width, mOvershootSize.height);
1541 constraint = Constraint::New<Quaternion>( mOvershootOverlay, Actor::Property::ORIENTATION, OvershootOverlayRotationConstraint );
1542 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1543 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1544 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1547 constraint = Constraint::New<Vector3>( mOvershootOverlay, Actor::Property::POSITION, OvershootOverlayPositionConstraint );
1548 constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
1549 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1550 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1551 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1554 constraint = Constraint::New<bool>( mOvershootOverlay, Actor::Property::VISIBLE, OvershootOverlayVisibilityConstraint );
1555 constraint.AddSource( ParentSource( Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL ) );
1558 constraint = Constraint::New<float>( mOvershootOverlay, effectOvershootPropertyIndex, EqualToConstraint() );
1559 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1565 if( mOvershootOverlay )
1567 self.Remove(mOvershootOverlay);
1568 mOvershootOverlay.Reset();
1573 float ItemView::CalculateScrollOvershoot()
1575 float overshoot = 0.0f;
1579 // The overshoot must be calculated from the accumulated pan gesture displacement
1580 // since the pan gesture starts.
1581 Actor self = Self();
1582 float scrollDistance = CalculateScrollDistance(mTotalPanDisplacement, *mActiveLayout) * mActiveLayout->GetScrollSpeedFactor();
1583 float positionDelta = GetCurrentLayoutPosition(0) + scrollDistance;
1584 float minLayoutPosition = mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), Self().GetCurrentSize());
1585 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1586 float clamppedPosition = std::min(0.0f, std::max(minLayoutPosition, positionDelta));
1587 overshoot = positionDelta - clamppedPosition;
1590 return overshoot > 0.0f ? std::min(overshoot, 1.0f) : std::max(overshoot, -1.0f);
1593 void ItemView::AnimateScrollOvershoot(float overshootAmount, bool animateBack)
1595 bool animatingOn = fabsf(overshootAmount) > Math::MACHINE_EPSILON_1;
1597 // make sure we animate back if needed
1598 mAnimateOvershootOff = animateBack || (!animatingOn && mAnimatingOvershootOn);
1600 if( mAnimatingOvershootOn )
1602 // animating on, do not allow animate off
1606 Actor self = Self();
1608 if(mOvershootAnimationSpeed > Math::MACHINE_EPSILON_0)
1610 float currentOvershoot = self.GetProperty<float>(Toolkit::ItemView::Property::OVERSHOOT);
1611 float duration = 0.0f;
1613 if (mOvershootOverlay)
1615 duration = mOvershootOverlay.GetCurrentSize().height * (animatingOn ? (1.0f - fabsf(currentOvershoot)) : fabsf(currentOvershoot)) / mOvershootAnimationSpeed;
1618 // Mark the animation as in progress to prevent manual property sets overwriting it.
1619 mInAnimation = true;
1620 mAnimatingOvershootOn = animatingOn;
1621 RemoveAnimation(mScrollOvershootAnimation);
1622 mScrollOvershootAnimation = Animation::New(duration);
1623 mScrollOvershootAnimation.FinishedSignal().Connect(this, &ItemView::OnOvershootOnFinished);
1624 mScrollOvershootAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::OVERSHOOT), overshootAmount, TimePeriod(0.0f, duration) );
1625 mScrollOvershootAnimation.Play();
1629 self.SetProperty( Toolkit::ItemView::Property::OVERSHOOT, overshootAmount );
1633 void ItemView::SetItemsParentOrigin( const Vector3& parentOrigin )
1635 if( parentOrigin != mItemsParentOrigin )
1637 mItemsParentOrigin = parentOrigin;
1638 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1640 iter->second.SetParentOrigin(parentOrigin);
1645 Vector3 ItemView::GetItemsParentOrigin() const
1647 return mItemsParentOrigin;
1650 void ItemView::SetItemsAnchorPoint( const Vector3& anchorPoint )
1652 if( anchorPoint != mItemsAnchorPoint )
1654 mItemsAnchorPoint = anchorPoint;
1655 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1657 iter->second.SetAnchorPoint(anchorPoint);
1662 Vector3 ItemView::GetItemsAnchorPoint() const
1664 return mItemsAnchorPoint;
1667 void ItemView::GetItemsRange(ItemRange& range)
1669 if( !mItemPool.empty() )
1671 range.begin = mItemPool.begin()->first;
1672 range.end = mItemPool.rbegin()->first + 1;
1681 bool ItemView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1683 Dali::BaseHandle handle( object );
1685 bool connected( true );
1686 Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( handle );
1688 if( 0 == strcmp( signalName.c_str(), LAYOUT_ACTIVATED_SIGNAL ) )
1690 itemView.LayoutActivatedSignal().Connect( tracker, functor );
1694 // signalName does not match any signal
1701 void ItemView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
1703 Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( Dali::BaseHandle( object ) );
1707 ItemView& itemViewImpl( GetImpl( itemView ) );
1710 case Toolkit::ItemView::Property::MINIMUM_SWIPE_SPEED:
1712 itemViewImpl.SetMinimumSwipeSpeed( value.Get<float>() );
1715 case Toolkit::ItemView::Property::MINIMUM_SWIPE_DISTANCE:
1717 itemViewImpl.SetMinimumSwipeDistance( value.Get<float>() );
1720 case Toolkit::ItemView::Property::WHEEL_SCROLL_DISTANCE_STEP:
1722 itemViewImpl.SetWheelScrollDistanceStep( value.Get<float>() );
1725 case Toolkit::ItemView::Property::SNAP_TO_ITEM_ENABLED:
1727 itemViewImpl.SetAnchoring( value.Get<bool>() );
1730 case Toolkit::ItemView::Property::REFRESH_INTERVAL:
1732 itemViewImpl.SetRefreshInterval( value.Get<float>() );
1739 Property::Value ItemView::GetProperty( BaseObject* object, Property::Index index )
1741 Property::Value value;
1743 Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( Dali::BaseHandle( object ) );
1747 ItemView& itemViewImpl( GetImpl( itemView ) );
1750 case Toolkit::ItemView::Property::MINIMUM_SWIPE_SPEED:
1752 value = itemViewImpl.GetMinimumSwipeSpeed();
1755 case Toolkit::ItemView::Property::MINIMUM_SWIPE_DISTANCE:
1757 value = itemViewImpl.GetMinimumSwipeDistance();
1760 case Toolkit::ItemView::Property::WHEEL_SCROLL_DISTANCE_STEP:
1762 value = itemViewImpl.GetWheelScrollDistanceStep();
1765 case Toolkit::ItemView::Property::SNAP_TO_ITEM_ENABLED:
1767 value = itemViewImpl.GetAnchoring();
1770 case Toolkit::ItemView::Property::REFRESH_INTERVAL:
1772 value = itemViewImpl.GetRefreshInterval();
1781 } // namespace Internal
1783 } // namespace Toolkit