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/public-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_ACTION_REGISTRATION( Toolkit, ItemView, "stopScrolling", ACTION_STOP_SCROLLING )
265 DALI_TYPE_REGISTRATION_END()
267 bool FindById( const ItemContainer& items, ItemId id )
269 for( ConstItemIter iter = items.begin(); items.end() != iter; ++iter )
271 if( iter->first == id )
280 } // unnamed namespace
282 Dali::Toolkit::ItemView ItemView::New(ItemFactory& factory)
284 // Create the implementation
285 ItemViewPtr itemView(new ItemView(factory));
287 // Pass ownership to CustomActor via derived handle
288 Dali::Toolkit::ItemView handle(*itemView);
290 // Second-phase init of the implementation
291 // This can only be done after the CustomActor connection has been made...
292 itemView->Initialize();
297 ItemView::ItemView(ItemFactory& factory)
298 : Scrollable( ControlBehaviour( DISABLE_SIZE_NEGOTIATION | REQUIRES_WHEEL_EVENTS | REQUIRES_KEYBOARD_NAVIGATION_SUPPORT ) ),
299 mItemFactory(factory),
300 mItemsParentOrigin(ParentOrigin::CENTER),
301 mItemsAnchorPoint(AnchorPoint::CENTER),
302 mTotalPanDisplacement(Vector2::ZERO),
304 mAnchoringDuration(DEFAULT_ANCHORING_DURATION),
305 mRefreshIntervalLayoutPositions(0.0f),
306 mMinimumSwipeSpeed(DEFAULT_MINIMUM_SWIPE_SPEED),
307 mMinimumSwipeDistance(DEFAULT_MINIMUM_SWIPE_DISTANCE),
308 mWheelScrollDistanceStep(0.0f),
309 mScrollDistance(0.0f),
311 mScrollOvershoot(0.0f),
312 mGestureState(Gesture::Clear),
313 mAnimatingOvershootOn(false),
314 mAnimateOvershootOff(false),
315 mAnchoringEnabled(false),
316 mRefreshOrderHint(true/*Refresh item 0 first*/),
319 mRefreshEnabled(true),
324 void ItemView::OnInitialize()
328 Vector2 stageSize = Stage::GetCurrent().GetSize();
329 mWheelScrollDistanceStep = stageSize.y * DEFAULT_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION;
331 EnableGestureDetection(Gesture::Type(Gesture::Pan));
333 mWheelEventFinishedTimer = Timer::New( WHEEL_EVENT_FINISHED_TIME_OUT );
334 mWheelEventFinishedTimer.TickSignal().Connect( this, &ItemView::OnWheelEventFinished );
336 SetRefreshInterval(DEFAULT_REFRESH_INTERVAL_LAYOUT_POSITIONS);
339 ItemView::~ItemView()
343 unsigned int ItemView::GetLayoutCount() const
345 return mLayouts.size();
348 void ItemView::AddLayout(ItemLayout& layout)
350 mLayouts.push_back(ItemLayoutPtr(&layout));
353 void ItemView::RemoveLayout(unsigned int layoutIndex)
355 DALI_ASSERT_ALWAYS(layoutIndex < mLayouts.size());
357 if (mActiveLayout == mLayouts[layoutIndex].Get())
359 mActiveLayout = NULL;
362 mLayouts.erase(mLayouts.begin() + layoutIndex);
365 ItemLayoutPtr ItemView::GetLayout(unsigned int layoutIndex) const
367 return mLayouts[layoutIndex];
370 ItemLayoutPtr ItemView::GetActiveLayout() const
372 return ItemLayoutPtr(mActiveLayout);
375 float ItemView::GetCurrentLayoutPosition(unsigned int itemId) const
377 return Self().GetProperty<float>( Toolkit::ItemView::Property::LAYOUT_POSITION ) + static_cast<float>( itemId );
380 void ItemView::ActivateLayout(unsigned int layoutIndex, const Vector3& targetSize, float durationSeconds)
382 DALI_ASSERT_ALWAYS(layoutIndex < mLayouts.size());
384 mRefreshEnabled = false;
388 // The ItemView size should match the active layout size
389 self.SetSize(targetSize);
390 mActiveLayoutTargetSize = targetSize;
392 // Switch to the new layout
393 mActiveLayout = mLayouts[layoutIndex].Get();
395 // Move the items to the new layout positions...
397 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
399 unsigned int itemId = iter->first;
400 Actor actor = iter->second;
402 // Remove constraints from previous layout
403 actor.RemoveConstraints();
406 mActiveLayout->GetItemSize( itemId, targetSize, size );
407 actor.SetSize( size.GetVectorXY() );
409 mActiveLayout->ApplyConstraints(actor, itemId, targetSize, Self() );
412 // Refresh the new layout
413 ItemRange range = GetItemRange(*mActiveLayout, targetSize, GetCurrentLayoutPosition(0), false/* don't reserve extra*/);
414 AddActorsWithinRange( range, targetSize );
416 // Scroll to an appropriate layout position
418 bool scrollAnimationNeeded(false);
419 float firstItemScrollPosition(0.0f);
421 float current = GetCurrentLayoutPosition(0);
422 float minimum = ClampFirstItemPosition(current, targetSize, *mActiveLayout);
424 if (current < minimum)
426 scrollAnimationNeeded = true;
427 firstItemScrollPosition = minimum;
429 else if (mAnchoringEnabled)
431 scrollAnimationNeeded = true;
432 firstItemScrollPosition = mActiveLayout->GetClosestAnchorPosition(current);
435 if (scrollAnimationNeeded)
437 RemoveAnimation(mScrollAnimation);
438 mScrollAnimation = Animation::New(durationSeconds);
439 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, AlphaFunction::EASE_OUT );
440 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnLayoutActivationScrollFinished);
441 mScrollAnimation.Play();
445 // Emit the layout activated signal
446 mLayoutActivatedSignal.Emit();
449 AnimateScrollOvershoot(0.0f);
450 mScrollOvershoot = 0.0f;
452 Radian scrollDirection(mActiveLayout->GetScrollDirection());
453 self.SetProperty(Toolkit::ItemView::Property::SCROLL_DIRECTION, Vector2(sinf(scrollDirection), cosf(scrollDirection)));
454 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_ORIENTATION, static_cast<int>(mActiveLayout->GetOrientation()));
455 self.SetProperty(Toolkit::ItemView::Property::SCROLL_SPEED, mScrollSpeed);
457 CalculateDomainSize(targetSize);
460 void ItemView::DeactivateCurrentLayout()
464 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
466 Actor actor = iter->second;
467 actor.RemoveConstraints();
470 mActiveLayout = NULL;
474 void ItemView::OnRefreshNotification(PropertyNotification& source)
476 // Cancel scroll animation to prevent any fighting of setting the scroll position property by scroll bar during fast scroll.
477 if(!mRefreshEnabled && mScrollAnimation)
479 RemoveAnimation(mScrollAnimation);
482 // Only cache extra items when it is not a fast scroll
483 DoRefresh(GetCurrentLayoutPosition(0), mRefreshEnabled || mScrollAnimation);
486 void ItemView::Refresh()
488 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter )
490 ReleaseActor( iter->first, iter->second );
494 DoRefresh(GetCurrentLayoutPosition(0), true);
497 void ItemView::DoRefresh(float currentLayoutPosition, bool cacheExtra)
501 ItemRange range = GetItemRange(*mActiveLayout, mActiveLayoutTargetSize, currentLayoutPosition, cacheExtra/*reserve extra*/);
502 RemoveActorsOutsideRange( range );
503 AddActorsWithinRange( range, Self().GetCurrentSize() );
505 mScrollUpdatedSignal.Emit( Vector2(0.0f, currentLayoutPosition) );
509 void ItemView::SetMinimumSwipeSpeed(float speed)
511 mMinimumSwipeSpeed = speed;
514 float ItemView::GetMinimumSwipeSpeed() const
516 return mMinimumSwipeSpeed;
519 void ItemView::SetMinimumSwipeDistance(float distance)
521 mMinimumSwipeDistance = distance;
524 float ItemView::GetMinimumSwipeDistance() const
526 return mMinimumSwipeDistance;
529 void ItemView::SetWheelScrollDistanceStep(float step)
531 mWheelScrollDistanceStep = step;
534 float ItemView::GetWheelScrollDistanceStep() const
536 return mWheelScrollDistanceStep;
539 void ItemView::SetAnchoring(bool enabled)
541 mAnchoringEnabled = enabled;
544 bool ItemView::GetAnchoring() const
546 return mAnchoringEnabled;
549 void ItemView::SetAnchoringDuration(float durationSeconds)
551 mAnchoringDuration = durationSeconds;
554 float ItemView::GetAnchoringDuration() const
556 return mAnchoringDuration;
559 void ItemView::SetRefreshInterval(float intervalLayoutPositions)
561 if( !Equals(mRefreshIntervalLayoutPositions, intervalLayoutPositions) )
563 mRefreshIntervalLayoutPositions = intervalLayoutPositions;
566 if(mRefreshNotification)
568 self.RemovePropertyNotification(mRefreshNotification);
570 mRefreshNotification = self.AddPropertyNotification( Toolkit::ItemView::Property::LAYOUT_POSITION, StepCondition(mRefreshIntervalLayoutPositions, 0.0f) );
571 mRefreshNotification.NotifySignal().Connect( this, &ItemView::OnRefreshNotification );
575 float ItemView::GetRefreshInterval() const
577 return mRefreshIntervalLayoutPositions;
580 void ItemView::SetRefreshEnabled(bool enabled)
582 mRefreshEnabled = enabled;
585 Actor ItemView::GetItem(unsigned int itemId) const
589 ConstItemPoolIter iter = mItemPool.find( itemId );
590 if( iter != mItemPool.end() )
592 actor = iter->second;
598 unsigned int ItemView::GetItemId( Actor actor ) const
600 unsigned int itemId( 0 );
602 for ( ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter )
604 if( iter->second == actor )
606 itemId = iter->first;
614 void ItemView::InsertItem( Item newItem, float durationSeconds )
617 Vector3 layoutSize = Self().GetCurrentSize();
619 Actor displacedActor;
620 ItemPoolIter afterDisplacedIter = mItemPool.end();
622 ItemPoolIter foundIter = mItemPool.find( newItem.first );
623 if( mItemPool.end() != foundIter )
625 SetupActor( newItem, layoutSize );
626 Self().Add( newItem.second );
628 displacedActor = foundIter->second;
629 foundIter->second = newItem.second;
631 afterDisplacedIter = ++foundIter;
635 // Inserting before the existing item range?
636 ItemPoolIter iter = mItemPool.begin();
637 if( iter != mItemPool.end() &&
638 iter->first > newItem.first )
640 displacedActor = iter->second;
641 mItemPool.erase( iter++ ); // iter is still valid after the erase
643 afterDisplacedIter = iter;
649 // Move the existing actors to make room
650 for( ItemPoolIter iter = afterDisplacedIter; mItemPool.end() != iter; ++iter )
652 Actor temp = iter->second;
653 iter->second = displacedActor;
654 displacedActor = temp;
656 iter->second.RemoveConstraints();
657 mActiveLayout->ApplyConstraints( iter->second, iter->first, layoutSize, Self() );
661 ItemPool::reverse_iterator lastIter = mItemPool.rbegin();
662 if ( lastIter != mItemPool.rend() )
664 ItemId lastId = lastIter->first;
665 Item lastItem( lastId + 1, displacedActor );
666 mItemPool.insert( lastItem );
668 lastItem.second.RemoveConstraints();
669 mActiveLayout->ApplyConstraints( lastItem.second, lastItem.first, layoutSize, Self() );
673 CalculateDomainSize( layoutSize );
675 mAddingItems = false;
678 void ItemView::InsertItems( const ItemContainer& newItems, float durationSeconds )
681 Vector3 layoutSize = Self().GetCurrentSize();
683 // Insert from lowest id to highest
684 std::set<Item> sortedItems;
685 for( ConstItemIter iter = newItems.begin(); newItems.end() != iter; ++iter )
687 sortedItems.insert( *iter );
690 for( std::set<Item>::iterator iter = sortedItems.begin(); sortedItems.end() != iter; ++iter )
692 Self().Add( iter->second );
694 ItemPoolIter foundIter = mItemPool.find( iter->first );
695 if( mItemPool.end() != foundIter )
697 Actor moveMe = foundIter->second;
698 foundIter->second = iter->second;
700 // Move the existing actors to make room
701 for( ItemPoolIter iter = ++foundIter; mItemPool.end() != iter; ++iter )
703 Actor temp = iter->second;
704 iter->second = moveMe;
709 ItemId lastId = mItemPool.rbegin()->first;
710 Item lastItem( lastId + 1, moveMe );
711 mItemPool.insert( lastItem );
715 mItemPool.insert( *iter );
719 // Relayout everything
720 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
723 if( FindById( newItems, iter->first ) )
725 SetupActor( *iter, layoutSize );
729 iter->second.RemoveConstraints();
730 mActiveLayout->ApplyConstraints( iter->second, iter->first, layoutSize, Self() );
734 CalculateDomainSize( layoutSize );
736 mAddingItems = false;
739 void ItemView::RemoveItem( unsigned int itemId, float durationSeconds )
741 bool actorsReordered = RemoveActor( itemId );
742 if( actorsReordered )
744 ReapplyAllConstraints();
750 void ItemView::RemoveItems( const ItemIdContainer& itemIds, float durationSeconds )
752 bool actorsReordered( false );
754 // Remove from highest id to lowest
755 set<ItemId> sortedItems;
756 for( ConstItemIdIter iter = itemIds.begin(); itemIds.end() != iter; ++iter )
758 sortedItems.insert( *iter );
761 for( set<ItemId>::reverse_iterator iter = sortedItems.rbegin(); sortedItems.rend() != iter; ++iter )
763 if( RemoveActor( *iter ) )
765 actorsReordered = true;
769 if( actorsReordered )
771 ReapplyAllConstraints();
777 bool ItemView::RemoveActor(unsigned int itemId)
779 bool reordered( false );
781 ItemPoolIter removeIter = mItemPool.find( itemId );
782 if( removeIter != mItemPool.end() )
784 ReleaseActor(itemId, removeIter->second);
788 // Removing before the existing item range?
789 ItemPoolIter iter = mItemPool.begin();
790 if( iter != mItemPool.end() &&
791 iter->first > itemId )
793 // In order to decrement the first visible item ID
794 mItemPool.insert( Item(iter->first - 1, Actor()) );
796 removeIter = mItemPool.begin();
800 if( removeIter != mItemPool.end() )
804 // Adjust the remaining item IDs, for example if item 2 is removed:
805 // Initial actors: After insert:
806 // ID 1 - ActorA ID 1 - ActorA
807 // ID 2 - ActorB ID 2 - ActorC (previously ID 3)
808 // ID 3 - ActorC ID 3 - ActorB (previously ID 4)
810 for (ItemPoolIter iter = removeIter; iter != mItemPool.end(); ++iter)
812 if( iter->first < mItemPool.rbegin()->first )
814 iter->second = mItemPool[ iter->first + 1 ];
818 mItemPool.erase( iter );
827 void ItemView::ReplaceItem( Item replacementItem, float durationSeconds )
830 Vector3 layoutSize = Self().GetCurrentSize();
832 SetupActor( replacementItem, layoutSize );
833 Self().Add( replacementItem.second );
835 const ItemPoolIter iter = mItemPool.find( replacementItem.first );
836 if( mItemPool.end() != iter )
838 ReleaseActor(iter->first, iter->second);
839 iter->second = replacementItem.second;
843 mItemPool.insert( replacementItem );
846 CalculateDomainSize( layoutSize );
848 mAddingItems = false;
851 void ItemView::ReplaceItems( const ItemContainer& replacementItems, float durationSeconds )
853 for( ConstItemIter iter = replacementItems.begin(); replacementItems.end() != iter; ++iter )
855 ReplaceItem( *iter, durationSeconds );
859 void ItemView::RemoveActorsOutsideRange( ItemRange range )
861 // Remove unwanted actors from the ItemView & ItemPool
862 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); )
864 unsigned int current = iter->first;
866 if( ! range.Within( current ) )
868 ReleaseActor(iter->first, iter->second);
870 mItemPool.erase( iter++ ); // erase invalidates the return value of post-increment; iter remains valid
879 void ItemView::AddActorsWithinRange( ItemRange range, const Vector3& layoutSize )
881 range.end = std::min(mItemFactory.GetNumberOfItems(), range.end);
883 // The order of addition depends on the scroll direction.
884 if (mRefreshOrderHint)
886 for (unsigned int itemId = range.begin; itemId < range.end; ++itemId)
888 AddNewActor( itemId, layoutSize );
893 for (unsigned int itemId = range.end; itemId > range.begin; --itemId)
895 AddNewActor( itemId-1, layoutSize );
899 // Total number of items may change dynamically.
900 // Always recalculate the domain size to reflect that.
901 CalculateDomainSize(Self().GetCurrentSize());
904 void ItemView::AddNewActor( unsigned int itemId, const Vector3& layoutSize )
908 if( mItemPool.end() == mItemPool.find( itemId ) )
910 Actor actor = mItemFactory.NewItem( itemId );
914 Item newItem( itemId, actor );
916 mItemPool.insert( newItem );
918 SetupActor( newItem, layoutSize );
923 mAddingItems = false;
926 void ItemView::SetupActor( Item item, const Vector3& layoutSize )
928 item.second.SetParentOrigin( mItemsParentOrigin );
929 item.second.SetAnchorPoint( mItemsAnchorPoint );
934 mActiveLayout->GetItemSize( item.first, mActiveLayoutTargetSize, size );
935 item.second.SetSize( size.GetVectorXY() );
937 mActiveLayout->ApplyConstraints( item.second, item.first, layoutSize, Self() );
941 void ItemView::ReleaseActor( ItemId item, Actor actor )
943 Self().Remove( actor );
944 mItemFactory.ItemReleased(item, actor);
947 ItemRange ItemView::GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra)
949 unsigned int itemCount = mItemFactory.GetNumberOfItems();
951 ItemRange available(0u, itemCount);
953 ItemRange range = layout.GetItemsWithinArea( layoutPosition, layoutSize );
957 // Add the reserve items for scrolling
958 unsigned int extra = layout.GetReserveItemCount(layoutSize);
959 range.begin = (range.begin >= extra) ? (range.begin - extra) : 0u;
963 return range.Intersection(available);
966 void ItemView::OnChildAdd(Actor& child)
970 // We don't want to do this downcast check for any item added by ItemView itself.
971 Dali::Toolkit::ScrollBar scrollBar = Dali::Toolkit::ScrollBar::DownCast(child);
974 scrollBar.SetScrollPropertySource(Self(),
975 Toolkit::ItemView::Property::LAYOUT_POSITION,
976 Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_Y,
977 Toolkit::Scrollable::Property::SCROLL_POSITION_MAX_Y,
978 Toolkit::ItemView::Property::SCROLL_CONTENT_SIZE);
983 bool ItemView::OnTouchEvent(const TouchEvent& event)
985 // Ignore events with multiple-touch points
986 if (event.GetPointCount() != 1)
991 if (event.GetPoint(0).state == TouchPoint::Down)
993 // Cancel ongoing scrolling etc.
994 mGestureState = Gesture::Clear;
996 mScrollDistance = 0.0f;
998 Self().SetProperty(Toolkit::ItemView::Property::SCROLL_SPEED, mScrollSpeed);
1000 mScrollOvershoot = 0.0f;
1001 AnimateScrollOvershoot(0.0f);
1003 if(mScrollAnimation)
1005 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1008 RemoveAnimation(mScrollAnimation);
1011 return true; // consume since we're potentially scrolling
1014 bool ItemView::OnWheelEvent(const WheelEvent& event)
1016 // Respond the wheel event to scroll
1019 Actor self = Self();
1020 const Vector3 layoutSize = Self().GetCurrentSize();
1021 float layoutPositionDelta = GetCurrentLayoutPosition(0) - (event.z * mWheelScrollDistanceStep * mActiveLayout->GetScrollSpeedFactor());
1022 float firstItemScrollPosition = ClampFirstItemPosition(layoutPositionDelta, layoutSize, *mActiveLayout);
1024 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1026 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1027 mRefreshEnabled = true;
1030 if (mWheelEventFinishedTimer.IsRunning())
1032 mWheelEventFinishedTimer.Stop();
1035 mWheelEventFinishedTimer.Start();
1040 bool ItemView::OnWheelEventFinished()
1044 RemoveAnimation(mScrollAnimation);
1046 // No more wheel events coming. Do the anchoring if enabled.
1047 mScrollAnimation = DoAnchoring();
1048 if (mScrollAnimation)
1050 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1051 mScrollAnimation.Play();
1055 mScrollOvershoot = 0.0f;
1056 AnimateScrollOvershoot(0.0f);
1058 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1065 void ItemView::ReapplyAllConstraints()
1067 Vector3 layoutSize = Self().GetCurrentSize();
1069 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1071 unsigned int id = iter->first;
1072 Actor actor = iter->second;
1074 actor.RemoveConstraints();
1075 mActiveLayout->ApplyConstraints(actor, id, layoutSize, Self());
1079 void ItemView::OnItemsRemoved()
1081 CalculateDomainSize(Self().GetCurrentSize());
1083 // Adjust scroll-position after an item is removed
1086 float firstItemScrollPosition = ClampFirstItemPosition(GetCurrentLayoutPosition(0), Self().GetCurrentSize(), *mActiveLayout);
1087 Self().SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1091 float ItemView::ClampFirstItemPosition( float targetPosition, const Vector3& targetSize, ItemLayout& layout, bool updateOvershoot )
1093 Actor self = Self();
1094 float minLayoutPosition = layout.GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), targetSize);
1095 float clamppedPosition = std::min(0.0f, std::max(minLayoutPosition, targetPosition));
1096 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1098 if( updateOvershoot )
1100 mScrollOvershoot = targetPosition - clamppedPosition;
1103 return clamppedPosition;
1106 void ItemView::OnPan( const PanGesture& gesture )
1108 Actor self = Self();
1109 const Vector3 layoutSize = Self().GetCurrentSize();
1111 RemoveAnimation(mScrollAnimation);
1113 // Short-circuit if there is no active layout
1116 mGestureState = Gesture::Clear;
1120 mGestureState = gesture.state;
1122 switch (mGestureState)
1124 case Gesture::Finished:
1127 if (fabsf(mScrollDistance) > mMinimumSwipeDistance &&
1128 mScrollSpeed > mMinimumSwipeSpeed)
1130 float direction = (mScrollDistance < 0.0f) ? -1.0f : 1.0f;
1132 mRefreshOrderHint = true;
1134 float currentLayoutPosition = GetCurrentLayoutPosition(0);
1135 float firstItemScrollPosition = ClampFirstItemPosition(currentLayoutPosition + mScrollSpeed * direction,
1139 if (mAnchoringEnabled)
1141 firstItemScrollPosition = mActiveLayout->GetClosestAnchorPosition(firstItemScrollPosition);
1144 RemoveAnimation(mScrollAnimation);
1146 float flickAnimationDuration = Clamp( mActiveLayout->GetItemFlickAnimationDuration() * std::max(1.0f, fabsf(firstItemScrollPosition - GetCurrentLayoutPosition(0)))
1147 , DEFAULT_MINIMUM_SWIPE_DURATION, DEFAULT_MAXIMUM_SWIPE_DURATION);
1149 mScrollAnimation = Animation::New(flickAnimationDuration);
1150 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION ), firstItemScrollPosition, AlphaFunction::EASE_OUT );
1151 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::SCROLL_SPEED), 0.0f, AlphaFunction::EASE_OUT );
1154 // Check whether it has already scrolled to the end
1155 if(fabs(currentLayoutPosition - firstItemScrollPosition) > Math::MACHINE_EPSILON_0)
1157 AnimateScrollOvershoot(0.0f);
1161 // Anchoring may be triggered when there was no swipe
1162 if (!mScrollAnimation)
1164 mScrollAnimation = DoAnchoring();
1167 // Reset the overshoot if no scroll animation.
1168 if (!mScrollAnimation)
1170 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1172 AnimateScrollOvershoot(0.0f, false);
1177 case Gesture::Started: // Fall through
1179 mTotalPanDisplacement = Vector2::ZERO;
1180 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1181 mRefreshEnabled = true;
1184 case Gesture::Continuing:
1186 mScrollDistance = CalculateScrollDistance(gesture.displacement, *mActiveLayout);
1187 mScrollSpeed = Clamp((gesture.GetSpeed() * gesture.GetSpeed() * mActiveLayout->GetFlickSpeedFactor() * MILLISECONDS_PER_SECONDS), 0.0f, mActiveLayout->GetMaximumSwipeSpeed());
1189 // Refresh order depends on the direction of the scroll; negative is towards the last item.
1190 mRefreshOrderHint = mScrollDistance < 0.0f;
1192 float layoutPositionDelta = GetCurrentLayoutPosition(0) + (mScrollDistance * mActiveLayout->GetScrollSpeedFactor());
1194 float firstItemScrollPosition = ClampFirstItemPosition(layoutPositionDelta, layoutSize, *mActiveLayout);
1196 float currentOvershoot = self.GetProperty<float>(Toolkit::ItemView::Property::OVERSHOOT);
1198 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1200 if( ( firstItemScrollPosition >= 0.0f &&
1201 currentOvershoot < 1.0f ) ||
1202 ( firstItemScrollPosition <= mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), layoutSize) &&
1203 currentOvershoot > -1.0f ) )
1205 mTotalPanDisplacement += gesture.displacement;
1208 mScrollOvershoot = CalculateScrollOvershoot();
1210 // If the view is moved in a direction against the overshoot indicator, then the indicator should be animated off.
1211 // First make sure we are not in an animation, otherwise a previously started
1212 // off-animation will be overwritten as the user continues scrolling.
1215 // Check if the movement is against the current overshoot amount (if we are currently displaying the indicator).
1216 if( ( ( mScrollOvershoot > Math::MACHINE_EPSILON_0 ) && ( mScrollDistance < -Math::MACHINE_EPSILON_0 ) ) ||
1217 ( ( mScrollOvershoot < Math::MACHINE_EPSILON_0 ) && ( mScrollDistance > Math::MACHINE_EPSILON_0 ) ) )
1219 // The user has moved against the indicator direction.
1220 // First, we reset the total displacement. This means the overshoot amount will become zero the next frame,
1221 // and if the user starts dragging in the overshoot direction again, the indicator will appear once more.
1222 mTotalPanDisplacement = Vector2::ZERO;
1223 // Animate the overshoot indicator off.
1224 AnimateScrollOvershoot( 0.0f, false );
1228 // Only set the property directly if we are not animating the overshoot away,
1229 // as otherwise this will overwrite the animation generated value.
1230 self.SetProperty( Toolkit::ItemView::Property::OVERSHOOT, mScrollOvershoot );
1236 case Gesture::Cancelled:
1238 mScrollAnimation = DoAnchoring();
1246 if (mScrollAnimation)
1248 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1249 mScrollAnimation.Play();
1253 bool ItemView::OnAccessibilityPan(PanGesture gesture)
1259 Actor ItemView::GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
1261 Actor nextFocusActor;
1265 if(!actor || actor == this->Self())
1267 nextFocusActor = GetItem(nextItemID);
1269 else if(actor && actor.GetParent() == this->Self())
1271 int itemID = GetItemId(actor);
1272 nextItemID = mActiveLayout->GetNextFocusItemID(itemID, mItemFactory.GetNumberOfItems(), direction, loopEnabled);
1273 nextFocusActor = GetItem(nextItemID);
1274 if(nextFocusActor == actor)
1276 // need to pass NULL actor back to focus manager
1277 nextFocusActor.Reset();
1278 return nextFocusActor;
1281 float layoutPosition = mActiveLayout->GetClosestAnchorPosition( GetCurrentLayoutPosition(0) );
1282 Vector3 layoutSize = Self().GetCurrentSize();
1285 // likely the current item is not buffered, so not in our item pool, probably best to get first viewable item
1286 ItemRange viewableItems = mActiveLayout->GetItemsWithinArea(layoutPosition, layoutSize);
1287 nextItemID = viewableItems.begin;
1288 nextFocusActor = GetItem(nextItemID);
1291 return nextFocusActor;
1294 void ItemView::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
1296 // only in this function if our chosen focus actor was actually used
1297 if(commitedFocusableActor)
1299 int nextItemID = GetItemId(commitedFocusableActor);
1300 float layoutPosition = GetCurrentLayoutPosition(0);
1301 Vector3 layoutSize = Self().GetCurrentSize();
1303 float scrollTo = mActiveLayout->GetClosestOnScreenLayoutPosition(nextItemID, layoutPosition, layoutSize);
1304 ScrollTo(Vector2(0.0f, scrollTo), DEFAULT_KEYBOARD_FOCUS_SCROLL_DURATION);
1308 Animation ItemView::DoAnchoring()
1310 Animation anchoringAnimation;
1311 Actor self = Self();
1313 if (mActiveLayout && mAnchoringEnabled)
1315 float anchorPosition = mActiveLayout->GetClosestAnchorPosition( GetCurrentLayoutPosition(0) );
1317 anchoringAnimation = Animation::New(mAnchoringDuration);
1318 anchoringAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), anchorPosition, AlphaFunction::EASE_OUT );
1319 anchoringAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::SCROLL_SPEED), 0.0f, AlphaFunction::EASE_OUT );
1322 AnimateScrollOvershoot(0.0f);
1326 return anchoringAnimation;
1329 void ItemView::OnScrollFinished(Animation& source)
1331 Actor self = Self();
1333 RemoveAnimation(mScrollAnimation); // mScrollAnimation is used to query whether we're scrolling
1335 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1337 if(mIsFlicking && fabsf(mScrollOvershoot) > Math::MACHINE_EPSILON_1)
1339 AnimateScrollOvershoot( mScrollOvershoot > 0.0f ? 1.0f : -1.0f, true);
1343 // Reset the overshoot
1344 AnimateScrollOvershoot( 0.0f );
1346 mIsFlicking = false;
1348 mScrollOvershoot = 0.0f;
1351 void ItemView::OnLayoutActivationScrollFinished(Animation& source)
1353 RemoveAnimation(mScrollAnimation);
1354 mRefreshEnabled = true;
1355 DoRefresh(GetCurrentLayoutPosition(0), true);
1357 // Emit the layout activated signal
1358 mLayoutActivatedSignal.Emit();
1361 void ItemView::OnOvershootOnFinished(Animation& animation)
1363 mAnimatingOvershootOn = false;
1364 mScrollOvershootAnimation.FinishedSignal().Disconnect(this, &ItemView::OnOvershootOnFinished);
1365 RemoveAnimation(mScrollOvershootAnimation);
1366 if(mAnimateOvershootOff)
1368 AnimateScrollOvershoot(0.0f);
1370 mInAnimation = false;
1373 void ItemView::ScrollToItem(unsigned int itemId, float durationSeconds)
1375 Actor self = Self();
1376 const Vector3 layoutSize = Self().GetCurrentSize();
1377 float firstItemScrollPosition = ClampFirstItemPosition(mActiveLayout->GetItemScrollToPosition(itemId), layoutSize, *mActiveLayout);
1379 if(durationSeconds > 0.0f)
1381 RemoveAnimation(mScrollAnimation);
1382 mScrollAnimation = Animation::New(durationSeconds);
1383 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, mScrollToAlphaFunction );
1384 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1385 mScrollAnimation.Play();
1389 self.SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1390 AnimateScrollOvershoot(0.0f);
1393 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1394 mRefreshEnabled = true;
1397 void ItemView::RemoveAnimation(Animation& animation)
1401 // Cease animating, and reset handle.
1407 void ItemView::CalculateDomainSize(const Vector3& layoutSize)
1409 Actor self = Self();
1411 Vector3 firstItemPosition(Vector3::ZERO);
1412 Vector3 lastItemPosition(Vector3::ZERO);
1416 firstItemPosition = mActiveLayout->GetItemPosition( 0,0,layoutSize );
1418 float minLayoutPosition = mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), layoutSize);
1419 lastItemPosition = mActiveLayout->GetItemPosition( fabs(minLayoutPosition),fabs(minLayoutPosition),layoutSize );
1423 if(IsHorizontal(mActiveLayout->GetOrientation()))
1425 domainSize = fabs(firstItemPosition.x - lastItemPosition.x);
1429 domainSize = fabs(firstItemPosition.y - lastItemPosition.y);
1432 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MIN, Vector2::ZERO);
1433 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1435 self.SetProperty(Toolkit::ItemView::Property::SCROLL_CONTENT_SIZE, domainSize);
1437 bool isLayoutScrollable = IsLayoutScrollable(layoutSize);
1438 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL, isLayoutScrollable);
1439 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL, false);
1443 bool ItemView::IsLayoutScrollable(const Vector3& layoutSize)
1445 Actor self = Self();
1447 float currentLayoutPosition = ClampFirstItemPosition( GetCurrentLayoutPosition(0), layoutSize, *mActiveLayout, false );
1448 float forwardClampedPosition = ClampFirstItemPosition( currentLayoutPosition + 1.0, layoutSize, *mActiveLayout, false );
1449 float backwardClampedPosition = ClampFirstItemPosition( currentLayoutPosition - 1.0, layoutSize, *mActiveLayout, false );
1451 return (fabs(forwardClampedPosition - backwardClampedPosition) > Math::MACHINE_EPSILON_0);
1454 float ItemView::GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const
1456 Vector3 firstItemPosition( mActiveLayout->GetItemPosition(0, layoutPosition, layoutSize ) );
1457 return IsHorizontal(mActiveLayout->GetOrientation()) ? firstItemPosition.x: firstItemPosition.y;
1460 Vector2 ItemView::GetCurrentScrollPosition() const
1462 return Vector2(0.0f, GetScrollPosition(GetCurrentLayoutPosition(0), Self().GetCurrentSize()));
1465 void ItemView::AddOverlay(Actor actor)
1467 actor.SetDrawMode( DrawMode::OVERLAY_2D );
1471 void ItemView::RemoveOverlay(Actor actor)
1473 Self().Remove(actor);
1476 void ItemView::ScrollTo(const Vector2& position, float duration)
1478 Actor self = Self();
1479 const Vector3 layoutSize = Self().GetCurrentSize();
1481 float firstItemScrollPosition = ClampFirstItemPosition(position.y, layoutSize, *mActiveLayout);
1485 RemoveAnimation(mScrollAnimation);
1486 mScrollAnimation = Animation::New(duration);
1487 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, mScrollToAlphaFunction );
1488 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1489 mScrollAnimation.Play();
1493 self.SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1494 AnimateScrollOvershoot(0.0f);
1497 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1498 mRefreshEnabled = true;
1501 void ItemView::SetOvershootEffectColor( const Vector4& color )
1503 mOvershootEffectColor = color;
1504 if( mOvershootOverlay )
1506 mOvershootOverlay.SetColor( color );
1510 void ItemView::EnableScrollOvershoot( bool enable )
1512 Actor self = Self();
1515 if( !mOvershootOverlay )
1517 Property::Index effectOvershootPropertyIndex = Property::INVALID_INDEX;
1518 mOvershootOverlay = CreateBouncingEffectActor( effectOvershootPropertyIndex );
1519 mOvershootOverlay.SetColor(mOvershootEffectColor);
1520 mOvershootOverlay.SetParentOrigin(ParentOrigin::TOP_LEFT);
1521 mOvershootOverlay.SetAnchorPoint(AnchorPoint::TOP_LEFT);
1522 mOvershootOverlay.SetDrawMode( DrawMode::OVERLAY_2D );
1523 self.Add(mOvershootOverlay);
1525 Constraint constraint = Constraint::New<Vector3>( mOvershootOverlay, Actor::Property::SIZE, OvershootOverlaySizeConstraint(mOvershootSize.height) );
1526 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1527 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1528 constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
1531 mOvershootOverlay.SetSize(mOvershootSize.width, mOvershootSize.height);
1533 constraint = Constraint::New<Quaternion>( mOvershootOverlay, Actor::Property::ORIENTATION, OvershootOverlayRotationConstraint );
1534 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1535 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1536 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1539 constraint = Constraint::New<Vector3>( mOvershootOverlay, Actor::Property::POSITION, OvershootOverlayPositionConstraint );
1540 constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
1541 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1542 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1543 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1546 constraint = Constraint::New<bool>( mOvershootOverlay, Actor::Property::VISIBLE, OvershootOverlayVisibilityConstraint );
1547 constraint.AddSource( ParentSource( Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL ) );
1550 constraint = Constraint::New<float>( mOvershootOverlay, effectOvershootPropertyIndex, EqualToConstraint() );
1551 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1557 if( mOvershootOverlay )
1559 self.Remove(mOvershootOverlay);
1560 mOvershootOverlay.Reset();
1565 float ItemView::CalculateScrollOvershoot()
1567 float overshoot = 0.0f;
1571 // The overshoot must be calculated from the accumulated pan gesture displacement
1572 // since the pan gesture starts.
1573 Actor self = Self();
1574 float scrollDistance = CalculateScrollDistance(mTotalPanDisplacement, *mActiveLayout) * mActiveLayout->GetScrollSpeedFactor();
1575 float positionDelta = GetCurrentLayoutPosition(0) + scrollDistance;
1576 float minLayoutPosition = mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), Self().GetCurrentSize());
1577 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1578 float clamppedPosition = std::min(0.0f, std::max(minLayoutPosition, positionDelta));
1579 overshoot = positionDelta - clamppedPosition;
1582 return overshoot > 0.0f ? std::min(overshoot, 1.0f) : std::max(overshoot, -1.0f);
1585 void ItemView::AnimateScrollOvershoot(float overshootAmount, bool animateBack)
1587 bool animatingOn = fabsf(overshootAmount) > Math::MACHINE_EPSILON_1;
1589 // make sure we animate back if needed
1590 mAnimateOvershootOff = animateBack || (!animatingOn && mAnimatingOvershootOn);
1592 if( mAnimatingOvershootOn )
1594 // animating on, do not allow animate off
1598 Actor self = Self();
1600 if(mOvershootAnimationSpeed > Math::MACHINE_EPSILON_0)
1602 float currentOvershoot = self.GetProperty<float>(Toolkit::ItemView::Property::OVERSHOOT);
1603 float duration = 0.0f;
1605 if (mOvershootOverlay)
1607 duration = mOvershootOverlay.GetCurrentSize().height * (animatingOn ? (1.0f - fabsf(currentOvershoot)) : fabsf(currentOvershoot)) / mOvershootAnimationSpeed;
1610 // Mark the animation as in progress to prevent manual property sets overwriting it.
1611 mInAnimation = true;
1612 mAnimatingOvershootOn = animatingOn;
1613 RemoveAnimation(mScrollOvershootAnimation);
1614 mScrollOvershootAnimation = Animation::New(duration);
1615 mScrollOvershootAnimation.FinishedSignal().Connect(this, &ItemView::OnOvershootOnFinished);
1616 mScrollOvershootAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::OVERSHOOT), overshootAmount, TimePeriod(0.0f, duration) );
1617 mScrollOvershootAnimation.Play();
1621 self.SetProperty( Toolkit::ItemView::Property::OVERSHOOT, overshootAmount );
1625 void ItemView::SetItemsParentOrigin( const Vector3& parentOrigin )
1627 if( parentOrigin != mItemsParentOrigin )
1629 mItemsParentOrigin = parentOrigin;
1630 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1632 iter->second.SetParentOrigin(parentOrigin);
1637 Vector3 ItemView::GetItemsParentOrigin() const
1639 return mItemsParentOrigin;
1642 void ItemView::SetItemsAnchorPoint( const Vector3& anchorPoint )
1644 if( anchorPoint != mItemsAnchorPoint )
1646 mItemsAnchorPoint = anchorPoint;
1647 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1649 iter->second.SetAnchorPoint(anchorPoint);
1654 Vector3 ItemView::GetItemsAnchorPoint() const
1656 return mItemsAnchorPoint;
1659 void ItemView::GetItemsRange(ItemRange& range)
1661 if( !mItemPool.empty() )
1663 range.begin = mItemPool.begin()->first;
1664 range.end = mItemPool.rbegin()->first + 1;
1673 bool ItemView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1675 Dali::BaseHandle handle( object );
1677 bool connected( true );
1678 Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( handle );
1680 if( 0 == strcmp( signalName.c_str(), LAYOUT_ACTIVATED_SIGNAL ) )
1682 itemView.LayoutActivatedSignal().Connect( tracker, functor );
1686 // signalName does not match any signal
1693 void ItemView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
1695 Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( Dali::BaseHandle( object ) );
1699 ItemView& itemViewImpl( GetImpl( itemView ) );
1702 case Toolkit::ItemView::Property::MINIMUM_SWIPE_SPEED:
1704 itemViewImpl.SetMinimumSwipeSpeed( value.Get<float>() );
1707 case Toolkit::ItemView::Property::MINIMUM_SWIPE_DISTANCE:
1709 itemViewImpl.SetMinimumSwipeDistance( value.Get<float>() );
1712 case Toolkit::ItemView::Property::WHEEL_SCROLL_DISTANCE_STEP:
1714 itemViewImpl.SetWheelScrollDistanceStep( value.Get<float>() );
1717 case Toolkit::ItemView::Property::SNAP_TO_ITEM_ENABLED:
1719 itemViewImpl.SetAnchoring( value.Get<bool>() );
1722 case Toolkit::ItemView::Property::REFRESH_INTERVAL:
1724 itemViewImpl.SetRefreshInterval( value.Get<float>() );
1731 Property::Value ItemView::GetProperty( BaseObject* object, Property::Index index )
1733 Property::Value value;
1735 Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( Dali::BaseHandle( object ) );
1739 ItemView& itemViewImpl( GetImpl( itemView ) );
1742 case Toolkit::ItemView::Property::MINIMUM_SWIPE_SPEED:
1744 value = itemViewImpl.GetMinimumSwipeSpeed();
1747 case Toolkit::ItemView::Property::MINIMUM_SWIPE_DISTANCE:
1749 value = itemViewImpl.GetMinimumSwipeDistance();
1752 case Toolkit::ItemView::Property::WHEEL_SCROLL_DISTANCE_STEP:
1754 value = itemViewImpl.GetWheelScrollDistanceStep();
1757 case Toolkit::ItemView::Property::SNAP_TO_ITEM_ENABLED:
1759 value = itemViewImpl.GetAnchoring();
1762 case Toolkit::ItemView::Property::REFRESH_INTERVAL:
1764 value = itemViewImpl.GetRefreshInterval();
1773 bool ItemView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
1775 Dali::BaseHandle handle( object );
1777 Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( handle );
1779 DALI_ASSERT_ALWAYS( itemView );
1781 if( 0 == strcmp( actionName.c_str(), ACTION_STOP_SCROLLING ) )
1783 GetImpl( itemView ).DoStopScrolling();
1789 void ItemView::DoStopScrolling()
1791 if( mScrollAnimation )
1793 mScrollAnimation.Stop();
1794 mScrollAnimation.Reset();
1798 } // namespace Internal
1800 } // namespace Toolkit