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
47 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ItemView, Toolkit::Scrollable, NULL)
49 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "layout-position", FLOAT, LAYOUT_POSITION)
50 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "scroll-speed", FLOAT, SCROLL_SPEED)
51 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "overshoot", FLOAT, OVERSHOOT)
52 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "scroll-direction", VECTOR2, SCROLL_DIRECTION)
53 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "layout-orientation", INTEGER, LAYOUT_ORIENTATION)
54 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "scroll-content-size", FLOAT, SCROLL_CONTENT_SIZE)
56 DALI_SIGNAL_REGISTRATION( Toolkit, ItemView, "layout-activated", LAYOUT_ACTIVATED_SIGNAL )
58 DALI_TYPE_REGISTRATION_END()
60 const float DEFAULT_MINIMUM_SWIPE_SPEED = 1.0f;
61 const float DEFAULT_MINIMUM_SWIPE_DISTANCE = 3.0f;
62 const float DEFAULT_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION = 0.1f;
64 const float DEFAULT_MINIMUM_SWIPE_DURATION = 0.45f;
65 const float DEFAULT_MAXIMUM_SWIPE_DURATION = 2.6f;
67 const float DEFAULT_REFRESH_INTERVAL_LAYOUT_POSITIONS = 20.0f; // 1 updates per 20 items
68 const int WHEEL_EVENT_FINISHED_TIME_OUT = 500; // 0.5 second
70 const float DEFAULT_ANCHORING_DURATION = 1.0f; // 1 second
72 const float MILLISECONDS_PER_SECONDS = 1000.0f;
74 const float OVERSHOOT_BOUNCE_ACTOR_RESIZE_THRESHOLD = 180.0f;
75 const Vector4 OVERSHOOT_OVERLAY_NINE_PATCH_BORDER(0.0f, 0.0f, 1.0f, 12.0f);
76 const float DEFAULT_KEYBOARD_FOCUS_SCROLL_DURATION = 0.2f;
79 * Local helper to convert pan distance (in actor coordinates) to the layout-specific scrolling direction
81 float CalculateScrollDistance(Vector2 panDistance, Toolkit::ItemLayout& layout)
83 Radian scrollDirection(layout.GetScrollDirection());
85 float cosTheta = cosf(scrollDirection);
86 float sinTheta = sinf(scrollDirection);
88 return panDistance.x * sinTheta + panDistance.y * cosTheta;
91 // Overshoot overlay constraints
92 struct OvershootOverlaySizeConstraint
94 OvershootOverlaySizeConstraint( float height )
95 : mOvershootHeight( height )
99 void operator()( Vector3& current, const PropertyInputContainer& inputs )
101 const Vector2& parentScrollDirection = inputs[0]->GetVector2();
102 const Toolkit::ControlOrientation::Type& layoutOrientation = static_cast<Toolkit::ControlOrientation::Type>(inputs[1]->GetInteger());
103 const Vector3& parentSize = inputs[2]->GetVector3();
105 if(Toolkit::IsVertical(layoutOrientation))
107 current.width = fabsf(parentScrollDirection.y) > Math::MACHINE_EPSILON_1 ? parentSize.x : parentSize.y;
111 current.width = fabsf(parentScrollDirection.x) > Math::MACHINE_EPSILON_1 ? parentSize.y : parentSize.x;
114 current.height = ( current.width > OVERSHOOT_BOUNCE_ACTOR_RESIZE_THRESHOLD ) ? mOvershootHeight : mOvershootHeight*0.5f;
117 float mOvershootHeight;
120 void OvershootOverlayRotationConstraint( Quaternion& current, const PropertyInputContainer& inputs )
122 const Vector2& parentScrollDirection = inputs[0]->GetVector2();
123 const Toolkit::ControlOrientation::Type& layoutOrientation = static_cast<Toolkit::ControlOrientation::Type>(inputs[1]->GetInteger());
124 const float parentOvershoot = inputs[2]->GetFloat();
126 float multiplier = 0;
127 if(Toolkit::IsVertical(layoutOrientation))
129 if(fabsf(parentScrollDirection.y) <= Math::MACHINE_EPSILON_1)
131 if( (layoutOrientation == Toolkit::ControlOrientation::Up && parentOvershoot < Math::MACHINE_EPSILON_0)
132 || (layoutOrientation == Toolkit::ControlOrientation::Down && parentOvershoot > Math::MACHINE_EPSILON_0) )
141 else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.y > Math::MACHINE_EPSILON_0)
142 || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.y < Math::MACHINE_EPSILON_0) )
153 if(fabsf(parentScrollDirection.x) <= Math::MACHINE_EPSILON_1)
155 if( (layoutOrientation == Toolkit::ControlOrientation::Left && parentOvershoot > Math::MACHINE_EPSILON_0)
156 ||(layoutOrientation == Toolkit::ControlOrientation::Right && parentOvershoot < Math::MACHINE_EPSILON_0) )
165 else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.x > Math::MACHINE_EPSILON_0)
166 || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.x < Math::MACHINE_EPSILON_0) )
176 current = Quaternion( Radian( multiplier * Math::PI ), Vector3::ZAXIS );
179 void OvershootOverlayPositionConstraint( Vector3& current, const PropertyInputContainer& inputs )
181 const Vector3& parentSize = inputs[0]->GetVector3();
182 const Vector2& parentScrollDirection = inputs[1]->GetVector2();
183 const Toolkit::ControlOrientation::Type& layoutOrientation = static_cast<Toolkit::ControlOrientation::Type>(inputs[2]->GetInteger());
184 const float parentOvershoot = inputs[3]->GetFloat();
186 Vector3 relativeOffset;
188 if(Toolkit::IsVertical(layoutOrientation))
190 if(fabsf(parentScrollDirection.y) <= Math::MACHINE_EPSILON_1)
192 if( (layoutOrientation == Toolkit::ControlOrientation::Up && parentOvershoot < Math::MACHINE_EPSILON_0)
193 || (layoutOrientation == Toolkit::ControlOrientation::Down && parentOvershoot > Math::MACHINE_EPSILON_0) )
195 relativeOffset = Vector3(1.0f, 0.0f, 0.0f);
199 relativeOffset =Vector3(0.0f, 1.0f, 0.0f);
202 else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.y > Math::MACHINE_EPSILON_0)
203 || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.y < Math::MACHINE_EPSILON_0) )
205 relativeOffset = Vector3(0.0f, 0.0f, 0.0f);
209 relativeOffset = Vector3(1.0f, 1.0f, 0.0f);
214 if(fabsf(parentScrollDirection.x) <= Math::MACHINE_EPSILON_1)
216 if( (layoutOrientation == Toolkit::ControlOrientation::Left && parentOvershoot < Math::MACHINE_EPSILON_0)
217 || (layoutOrientation == Toolkit::ControlOrientation::Right && parentOvershoot > Math::MACHINE_EPSILON_0) )
219 relativeOffset = Vector3(0.0f, 0.0f, 0.0f);
223 relativeOffset = Vector3(1.0f, 1.0f, 0.0f);
226 else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.x > Math::MACHINE_EPSILON_0)
227 || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.x < Math::MACHINE_EPSILON_0) )
229 relativeOffset = Vector3(0.0f, 1.0f, 0.0f);
233 relativeOffset = Vector3(1.0f, 0.0f, 0.0f);
237 current = relativeOffset * parentSize;
240 void OvershootOverlayVisibilityConstraint( bool& current, const PropertyInputContainer& inputs )
242 current = inputs[0]->GetBoolean();
245 } // unnamed namespace
256 namespace // unnamed namespace
259 bool FindById( const ItemContainer& items, ItemId id )
261 for( ConstItemIter iter = items.begin(); items.end() != iter; ++iter )
263 if( iter->first == id )
272 } // unnamed namespace
274 Dali::Toolkit::ItemView ItemView::New(ItemFactory& factory)
276 // Create the implementation
277 ItemViewPtr itemView(new ItemView(factory));
279 // Pass ownership to CustomActor via derived handle
280 Dali::Toolkit::ItemView handle(*itemView);
282 // Second-phase init of the implementation
283 // This can only be done after the CustomActor connection has been made...
284 itemView->Initialize();
289 ItemView::ItemView(ItemFactory& factory)
290 : Scrollable( ControlBehaviour( DISABLE_SIZE_NEGOTIATION | REQUIRES_WHEEL_EVENTS | REQUIRES_KEYBOARD_NAVIGATION_SUPPORT ) ),
291 mItemFactory(factory),
292 mItemsParentOrigin(ParentOrigin::CENTER),
293 mItemsAnchorPoint(AnchorPoint::CENTER),
294 mTotalPanDisplacement(Vector2::ZERO),
296 mAnchoringDuration(DEFAULT_ANCHORING_DURATION),
297 mRefreshIntervalLayoutPositions(0.0f),
298 mMinimumSwipeSpeed(DEFAULT_MINIMUM_SWIPE_SPEED),
299 mMinimumSwipeDistance(DEFAULT_MINIMUM_SWIPE_DISTANCE),
300 mWheelScrollDistanceStep(0.0f),
301 mScrollDistance(0.0f),
303 mScrollOvershoot(0.0f),
304 mGestureState(Gesture::Clear),
305 mAnimatingOvershootOn(false),
306 mAnimateOvershootOff(false),
307 mAnchoringEnabled(false),
308 mRefreshOrderHint(true/*Refresh item 0 first*/),
311 mRefreshEnabled(true),
316 void ItemView::OnInitialize()
320 Vector2 stageSize = Stage::GetCurrent().GetSize();
321 mWheelScrollDistanceStep = stageSize.y * DEFAULT_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION;
323 EnableGestureDetection(Gesture::Type(Gesture::Pan));
325 mWheelEventFinishedTimer = Timer::New( WHEEL_EVENT_FINISHED_TIME_OUT );
326 mWheelEventFinishedTimer.TickSignal().Connect( this, &ItemView::OnWheelEventFinished );
328 SetRefreshInterval(DEFAULT_REFRESH_INTERVAL_LAYOUT_POSITIONS);
331 ItemView::~ItemView()
335 unsigned int ItemView::GetLayoutCount() const
337 return mLayouts.size();
340 void ItemView::AddLayout(ItemLayout& layout)
342 mLayouts.push_back(ItemLayoutPtr(&layout));
345 void ItemView::RemoveLayout(unsigned int layoutIndex)
347 DALI_ASSERT_ALWAYS(layoutIndex < mLayouts.size());
349 if (mActiveLayout == mLayouts[layoutIndex].Get())
351 mActiveLayout = NULL;
354 mLayouts.erase(mLayouts.begin() + layoutIndex);
357 ItemLayoutPtr ItemView::GetLayout(unsigned int layoutIndex) const
359 return mLayouts[layoutIndex];
362 ItemLayoutPtr ItemView::GetActiveLayout() const
364 return ItemLayoutPtr(mActiveLayout);
367 float ItemView::GetCurrentLayoutPosition(unsigned int itemId) const
369 return Self().GetProperty<float>( Toolkit::ItemView::Property::LAYOUT_POSITION ) + static_cast<float>( itemId );
372 void ItemView::ActivateLayout(unsigned int layoutIndex, const Vector3& targetSize, float durationSeconds)
374 DALI_ASSERT_ALWAYS(layoutIndex < mLayouts.size());
376 mRefreshEnabled = false;
380 // The ItemView size should match the active layout size
381 self.SetSize(targetSize);
382 mActiveLayoutTargetSize = targetSize;
384 // Switch to the new layout
385 mActiveLayout = mLayouts[layoutIndex].Get();
387 // Move the items to the new layout positions...
389 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
391 unsigned int itemId = iter->first;
392 Actor actor = iter->second;
394 // Remove constraints from previous layout
395 actor.RemoveConstraints();
398 mActiveLayout->GetItemSize( itemId, targetSize, size );
399 actor.SetSize( size.GetVectorXY() );
401 mActiveLayout->ApplyConstraints(actor, itemId, targetSize, Self() );
404 // Refresh the new layout
405 ItemRange range = GetItemRange(*mActiveLayout, targetSize, GetCurrentLayoutPosition(0), false/* don't reserve extra*/);
406 AddActorsWithinRange( range, targetSize );
408 // Scroll to an appropriate layout position
410 bool scrollAnimationNeeded(false);
411 float firstItemScrollPosition(0.0f);
413 float current = GetCurrentLayoutPosition(0);
414 float minimum = ClampFirstItemPosition(current, targetSize, *mActiveLayout);
416 if (current < minimum)
418 scrollAnimationNeeded = true;
419 firstItemScrollPosition = minimum;
421 else if (mAnchoringEnabled)
423 scrollAnimationNeeded = true;
424 firstItemScrollPosition = mActiveLayout->GetClosestAnchorPosition(current);
427 if (scrollAnimationNeeded)
429 RemoveAnimation(mScrollAnimation);
430 mScrollAnimation = Animation::New(durationSeconds);
431 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, AlphaFunction::EASE_OUT );
432 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnLayoutActivationScrollFinished);
433 mScrollAnimation.Play();
437 // Emit the layout activated signal
438 mLayoutActivatedSignal.Emit();
441 AnimateScrollOvershoot(0.0f);
442 mScrollOvershoot = 0.0f;
444 Radian scrollDirection(mActiveLayout->GetScrollDirection());
445 self.SetProperty(Toolkit::ItemView::Property::SCROLL_DIRECTION, Vector2(sinf(scrollDirection), cosf(scrollDirection)));
446 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_ORIENTATION, static_cast<int>(mActiveLayout->GetOrientation()));
447 self.SetProperty(Toolkit::ItemView::Property::SCROLL_SPEED, mScrollSpeed);
449 CalculateDomainSize(targetSize);
452 void ItemView::DeactivateCurrentLayout()
456 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
458 Actor actor = iter->second;
459 actor.RemoveConstraints();
462 mActiveLayout = NULL;
466 void ItemView::OnRefreshNotification(PropertyNotification& source)
468 // Cancel scroll animation to prevent any fighting of setting the scroll position property by scroll bar during fast scroll.
469 if(!mRefreshEnabled && mScrollAnimation)
471 RemoveAnimation(mScrollAnimation);
474 // Only cache extra items when it is not a fast scroll
475 DoRefresh(GetCurrentLayoutPosition(0), mRefreshEnabled || mScrollAnimation);
478 void ItemView::Refresh()
480 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter )
482 ReleaseActor( iter->first, iter->second );
486 DoRefresh(GetCurrentLayoutPosition(0), true);
489 void ItemView::DoRefresh(float currentLayoutPosition, bool cacheExtra)
493 ItemRange range = GetItemRange(*mActiveLayout, mActiveLayoutTargetSize, currentLayoutPosition, cacheExtra/*reserve extra*/);
494 RemoveActorsOutsideRange( range );
495 AddActorsWithinRange( range, Self().GetCurrentSize() );
497 mScrollUpdatedSignal.Emit( Vector2(0.0f, currentLayoutPosition) );
501 void ItemView::SetMinimumSwipeSpeed(float speed)
503 mMinimumSwipeSpeed = speed;
506 float ItemView::GetMinimumSwipeSpeed() const
508 return mMinimumSwipeSpeed;
511 void ItemView::SetMinimumSwipeDistance(float distance)
513 mMinimumSwipeDistance = distance;
516 float ItemView::GetMinimumSwipeDistance() const
518 return mMinimumSwipeDistance;
521 void ItemView::SetWheelScrollDistanceStep(float step)
523 mWheelScrollDistanceStep = step;
526 float ItemView::GetWheelScrollDistanceStep() const
528 return mWheelScrollDistanceStep;
531 void ItemView::SetAnchoring(bool enabled)
533 mAnchoringEnabled = enabled;
536 bool ItemView::GetAnchoring() const
538 return mAnchoringEnabled;
541 void ItemView::SetAnchoringDuration(float durationSeconds)
543 mAnchoringDuration = durationSeconds;
546 float ItemView::GetAnchoringDuration() const
548 return mAnchoringDuration;
551 void ItemView::SetRefreshInterval(float intervalLayoutPositions)
553 if( !Equals(mRefreshIntervalLayoutPositions, intervalLayoutPositions) )
555 mRefreshIntervalLayoutPositions = intervalLayoutPositions;
558 if(mRefreshNotification)
560 self.RemovePropertyNotification(mRefreshNotification);
562 mRefreshNotification = self.AddPropertyNotification( Toolkit::ItemView::Property::LAYOUT_POSITION, StepCondition(mRefreshIntervalLayoutPositions, 0.0f) );
563 mRefreshNotification.NotifySignal().Connect( this, &ItemView::OnRefreshNotification );
567 float ItemView::GetRefreshInterval() const
569 return mRefreshIntervalLayoutPositions;
572 void ItemView::SetRefreshEnabled(bool enabled)
574 mRefreshEnabled = enabled;
577 Actor ItemView::GetItem(unsigned int itemId) const
581 ConstItemPoolIter iter = mItemPool.find( itemId );
582 if( iter != mItemPool.end() )
584 actor = iter->second;
590 unsigned int ItemView::GetItemId( Actor actor ) const
592 unsigned int itemId( 0 );
594 for ( ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter )
596 if( iter->second == actor )
598 itemId = iter->first;
606 void ItemView::InsertItem( Item newItem, float durationSeconds )
609 Vector3 layoutSize = Self().GetCurrentSize();
611 Actor displacedActor;
612 ItemPoolIter afterDisplacedIter = mItemPool.end();
614 ItemPoolIter foundIter = mItemPool.find( newItem.first );
615 if( mItemPool.end() != foundIter )
617 SetupActor( newItem, layoutSize );
618 Self().Add( newItem.second );
620 displacedActor = foundIter->second;
621 foundIter->second = newItem.second;
623 afterDisplacedIter = ++foundIter;
627 // Inserting before the existing item range?
628 ItemPoolIter iter = mItemPool.begin();
629 if( iter != mItemPool.end() &&
630 iter->first > newItem.first )
632 displacedActor = iter->second;
633 mItemPool.erase( iter++ ); // iter is still valid after the erase
635 afterDisplacedIter = iter;
641 // Move the existing actors to make room
642 for( ItemPoolIter iter = afterDisplacedIter; mItemPool.end() != iter; ++iter )
644 Actor temp = iter->second;
645 iter->second = displacedActor;
646 displacedActor = temp;
648 iter->second.RemoveConstraints();
649 mActiveLayout->ApplyConstraints( iter->second, iter->first, layoutSize, Self() );
653 ItemPool::reverse_iterator lastIter = mItemPool.rbegin();
654 if ( lastIter != mItemPool.rend() )
656 ItemId lastId = lastIter->first;
657 Item lastItem( lastId + 1, displacedActor );
658 mItemPool.insert( lastItem );
660 lastItem.second.RemoveConstraints();
661 mActiveLayout->ApplyConstraints( lastItem.second, lastItem.first, layoutSize, Self() );
665 CalculateDomainSize( layoutSize );
667 mAddingItems = false;
670 void ItemView::InsertItems( const ItemContainer& newItems, float durationSeconds )
673 Vector3 layoutSize = Self().GetCurrentSize();
675 // Insert from lowest id to highest
676 std::set<Item> sortedItems;
677 for( ConstItemIter iter = newItems.begin(); newItems.end() != iter; ++iter )
679 sortedItems.insert( *iter );
682 for( std::set<Item>::iterator iter = sortedItems.begin(); sortedItems.end() != iter; ++iter )
684 Self().Add( iter->second );
686 ItemPoolIter foundIter = mItemPool.find( iter->first );
687 if( mItemPool.end() != foundIter )
689 Actor moveMe = foundIter->second;
690 foundIter->second = iter->second;
692 // Move the existing actors to make room
693 for( ItemPoolIter iter = ++foundIter; mItemPool.end() != iter; ++iter )
695 Actor temp = iter->second;
696 iter->second = moveMe;
701 ItemId lastId = mItemPool.rbegin()->first;
702 Item lastItem( lastId + 1, moveMe );
703 mItemPool.insert( lastItem );
707 mItemPool.insert( *iter );
711 // Relayout everything
712 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
715 if( FindById( newItems, iter->first ) )
717 SetupActor( *iter, layoutSize );
721 iter->second.RemoveConstraints();
722 mActiveLayout->ApplyConstraints( iter->second, iter->first, layoutSize, Self() );
726 CalculateDomainSize( layoutSize );
728 mAddingItems = false;
731 void ItemView::RemoveItem( unsigned int itemId, float durationSeconds )
733 bool actorsReordered = RemoveActor( itemId );
734 if( actorsReordered )
736 ReapplyAllConstraints();
742 void ItemView::RemoveItems( const ItemIdContainer& itemIds, float durationSeconds )
744 bool actorsReordered( false );
746 // Remove from highest id to lowest
747 set<ItemId> sortedItems;
748 for( ConstItemIdIter iter = itemIds.begin(); itemIds.end() != iter; ++iter )
750 sortedItems.insert( *iter );
753 for( set<ItemId>::reverse_iterator iter = sortedItems.rbegin(); sortedItems.rend() != iter; ++iter )
755 if( RemoveActor( *iter ) )
757 actorsReordered = true;
761 if( actorsReordered )
763 ReapplyAllConstraints();
769 bool ItemView::RemoveActor(unsigned int itemId)
771 bool reordered( false );
773 ItemPoolIter removeIter = mItemPool.find( itemId );
774 if( removeIter != mItemPool.end() )
776 ReleaseActor(itemId, removeIter->second);
780 // Removing before the existing item range?
781 ItemPoolIter iter = mItemPool.begin();
782 if( iter != mItemPool.end() &&
783 iter->first > itemId )
785 // In order to decrement the first visible item ID
786 mItemPool.insert( Item(iter->first - 1, Actor()) );
788 removeIter = mItemPool.begin();
792 if( removeIter != mItemPool.end() )
796 // Adjust the remaining item IDs, for example if item 2 is removed:
797 // Initial actors: After insert:
798 // ID 1 - ActorA ID 1 - ActorA
799 // ID 2 - ActorB ID 2 - ActorC (previously ID 3)
800 // ID 3 - ActorC ID 3 - ActorB (previously ID 4)
802 for (ItemPoolIter iter = removeIter; iter != mItemPool.end(); ++iter)
804 if( iter->first < mItemPool.rbegin()->first )
806 iter->second = mItemPool[ iter->first + 1 ];
810 mItemPool.erase( iter );
819 void ItemView::ReplaceItem( Item replacementItem, float durationSeconds )
822 Vector3 layoutSize = Self().GetCurrentSize();
824 SetupActor( replacementItem, layoutSize );
825 Self().Add( replacementItem.second );
827 const ItemPoolIter iter = mItemPool.find( replacementItem.first );
828 if( mItemPool.end() != iter )
830 ReleaseActor(iter->first, iter->second);
831 iter->second = replacementItem.second;
835 mItemPool.insert( replacementItem );
838 CalculateDomainSize( layoutSize );
840 mAddingItems = false;
843 void ItemView::ReplaceItems( const ItemContainer& replacementItems, float durationSeconds )
845 for( ConstItemIter iter = replacementItems.begin(); replacementItems.end() != iter; ++iter )
847 ReplaceItem( *iter, durationSeconds );
851 void ItemView::RemoveActorsOutsideRange( ItemRange range )
853 // Remove unwanted actors from the ItemView & ItemPool
854 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); )
856 unsigned int current = iter->first;
858 if( ! range.Within( current ) )
860 ReleaseActor(iter->first, iter->second);
862 mItemPool.erase( iter++ ); // erase invalidates the return value of post-increment; iter remains valid
871 void ItemView::AddActorsWithinRange( ItemRange range, const Vector3& layoutSize )
873 range.end = std::min(mItemFactory.GetNumberOfItems(), range.end);
875 // The order of addition depends on the scroll direction.
876 if (mRefreshOrderHint)
878 for (unsigned int itemId = range.begin; itemId < range.end; ++itemId)
880 AddNewActor( itemId, layoutSize );
885 for (unsigned int itemId = range.end; itemId > range.begin; --itemId)
887 AddNewActor( itemId-1, layoutSize );
891 // Total number of items may change dynamically.
892 // Always recalculate the domain size to reflect that.
893 CalculateDomainSize(Self().GetCurrentSize());
896 void ItemView::AddNewActor( unsigned int itemId, const Vector3& layoutSize )
900 if( mItemPool.end() == mItemPool.find( itemId ) )
902 Actor actor = mItemFactory.NewItem( itemId );
906 Item newItem( itemId, actor );
908 mItemPool.insert( newItem );
910 SetupActor( newItem, layoutSize );
915 mAddingItems = false;
918 void ItemView::SetupActor( Item item, const Vector3& layoutSize )
920 item.second.SetParentOrigin( mItemsParentOrigin );
921 item.second.SetAnchorPoint( mItemsAnchorPoint );
926 mActiveLayout->GetItemSize( item.first, mActiveLayoutTargetSize, size );
927 item.second.SetSize( size.GetVectorXY() );
929 mActiveLayout->ApplyConstraints( item.second, item.first, layoutSize, Self() );
933 void ItemView::ReleaseActor( ItemId item, Actor actor )
935 Self().Remove( actor );
936 mItemFactory.ItemReleased(item, actor);
939 ItemRange ItemView::GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra)
941 unsigned int itemCount = mItemFactory.GetNumberOfItems();
943 ItemRange available(0u, itemCount);
945 ItemRange range = layout.GetItemsWithinArea( layoutPosition, layoutSize );
949 // Add the reserve items for scrolling
950 unsigned int extra = layout.GetReserveItemCount(layoutSize);
951 range.begin = (range.begin >= extra) ? (range.begin - extra) : 0u;
955 return range.Intersection(available);
958 void ItemView::OnChildAdd(Actor& child)
962 // We don't want to do this downcast check for any item added by ItemView itself.
963 Dali::Toolkit::ScrollBar scrollBar = Dali::Toolkit::ScrollBar::DownCast(child);
966 scrollBar.SetScrollPropertySource(Self(),
967 Toolkit::ItemView::Property::LAYOUT_POSITION,
968 Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_Y,
969 Toolkit::Scrollable::Property::SCROLL_POSITION_MAX_Y,
970 Toolkit::ItemView::Property::SCROLL_CONTENT_SIZE);
975 bool ItemView::OnTouchEvent(const TouchEvent& event)
977 // Ignore events with multiple-touch points
978 if (event.GetPointCount() != 1)
983 if (event.GetPoint(0).state == TouchPoint::Down)
985 // Cancel ongoing scrolling etc.
986 mGestureState = Gesture::Clear;
988 mScrollDistance = 0.0f;
990 Self().SetProperty(Toolkit::ItemView::Property::SCROLL_SPEED, mScrollSpeed);
992 mScrollOvershoot = 0.0f;
993 AnimateScrollOvershoot(0.0f);
997 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1000 RemoveAnimation(mScrollAnimation);
1003 return true; // consume since we're potentially scrolling
1006 bool ItemView::OnWheelEvent(const WheelEvent& event)
1008 // Respond the wheel event to scroll
1011 Actor self = Self();
1012 const Vector3 layoutSize = Self().GetCurrentSize();
1013 float layoutPositionDelta = GetCurrentLayoutPosition(0) - (event.z * mWheelScrollDistanceStep * mActiveLayout->GetScrollSpeedFactor());
1014 float firstItemScrollPosition = ClampFirstItemPosition(layoutPositionDelta, layoutSize, *mActiveLayout);
1016 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1018 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1019 mRefreshEnabled = true;
1022 if (mWheelEventFinishedTimer.IsRunning())
1024 mWheelEventFinishedTimer.Stop();
1027 mWheelEventFinishedTimer.Start();
1032 bool ItemView::OnWheelEventFinished()
1036 RemoveAnimation(mScrollAnimation);
1038 // No more wheel events coming. Do the anchoring if enabled.
1039 mScrollAnimation = DoAnchoring();
1040 if (mScrollAnimation)
1042 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1043 mScrollAnimation.Play();
1047 mScrollOvershoot = 0.0f;
1048 AnimateScrollOvershoot(0.0f);
1050 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1057 void ItemView::ReapplyAllConstraints()
1059 Vector3 layoutSize = Self().GetCurrentSize();
1061 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1063 unsigned int id = iter->first;
1064 Actor actor = iter->second;
1066 actor.RemoveConstraints();
1067 mActiveLayout->ApplyConstraints(actor, id, layoutSize, Self());
1071 void ItemView::OnItemsRemoved()
1073 CalculateDomainSize(Self().GetCurrentSize());
1075 // Adjust scroll-position after an item is removed
1078 float firstItemScrollPosition = ClampFirstItemPosition(GetCurrentLayoutPosition(0), Self().GetCurrentSize(), *mActiveLayout);
1079 Self().SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1083 float ItemView::ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout)
1085 Actor self = Self();
1086 float minLayoutPosition = layout.GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), targetSize);
1087 float clamppedPosition = std::min(0.0f, std::max(minLayoutPosition, targetPosition));
1088 mScrollOvershoot = targetPosition - clamppedPosition;
1089 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1091 return clamppedPosition;
1094 void ItemView::OnPan( const PanGesture& gesture )
1096 Actor self = Self();
1097 const Vector3 layoutSize = Self().GetCurrentSize();
1099 RemoveAnimation(mScrollAnimation);
1101 // Short-circuit if there is no active layout
1104 mGestureState = Gesture::Clear;
1108 mGestureState = gesture.state;
1110 switch (mGestureState)
1112 case Gesture::Finished:
1115 if (fabsf(mScrollDistance) > mMinimumSwipeDistance &&
1116 mScrollSpeed > mMinimumSwipeSpeed)
1118 float direction = (mScrollDistance < 0.0f) ? -1.0f : 1.0f;
1120 mRefreshOrderHint = true;
1122 float currentLayoutPosition = GetCurrentLayoutPosition(0);
1123 float firstItemScrollPosition = ClampFirstItemPosition(currentLayoutPosition + mScrollSpeed * direction,
1127 if (mAnchoringEnabled)
1129 firstItemScrollPosition = mActiveLayout->GetClosestAnchorPosition(firstItemScrollPosition);
1132 RemoveAnimation(mScrollAnimation);
1134 float flickAnimationDuration = Clamp( mActiveLayout->GetItemFlickAnimationDuration() * std::max(1.0f, fabsf(firstItemScrollPosition - GetCurrentLayoutPosition(0)))
1135 , DEFAULT_MINIMUM_SWIPE_DURATION, DEFAULT_MAXIMUM_SWIPE_DURATION);
1137 mScrollAnimation = Animation::New(flickAnimationDuration);
1138 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION ), firstItemScrollPosition, AlphaFunction::EASE_OUT );
1139 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::SCROLL_SPEED), 0.0f, AlphaFunction::EASE_OUT );
1142 // Check whether it has already scrolled to the end
1143 if(fabs(currentLayoutPosition - firstItemScrollPosition) > Math::MACHINE_EPSILON_0)
1145 AnimateScrollOvershoot(0.0f);
1149 // Anchoring may be triggered when there was no swipe
1150 if (!mScrollAnimation)
1152 mScrollAnimation = DoAnchoring();
1155 // Reset the overshoot if no scroll animation.
1156 if (!mScrollAnimation)
1158 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1160 AnimateScrollOvershoot(0.0f, false);
1165 case Gesture::Started: // Fall through
1167 mTotalPanDisplacement = Vector2::ZERO;
1168 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1169 mRefreshEnabled = true;
1172 case Gesture::Continuing:
1174 mScrollDistance = CalculateScrollDistance(gesture.displacement, *mActiveLayout);
1175 mScrollSpeed = Clamp((gesture.GetSpeed() * gesture.GetSpeed() * mActiveLayout->GetFlickSpeedFactor() * MILLISECONDS_PER_SECONDS), 0.0f, mActiveLayout->GetMaximumSwipeSpeed());
1177 // Refresh order depends on the direction of the scroll; negative is towards the last item.
1178 mRefreshOrderHint = mScrollDistance < 0.0f;
1180 float layoutPositionDelta = GetCurrentLayoutPosition(0) + (mScrollDistance * mActiveLayout->GetScrollSpeedFactor());
1182 float firstItemScrollPosition = ClampFirstItemPosition(layoutPositionDelta, layoutSize, *mActiveLayout);
1184 float currentOvershoot = self.GetProperty<float>(Toolkit::ItemView::Property::OVERSHOOT);
1186 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1188 if( ( firstItemScrollPosition >= 0.0f &&
1189 currentOvershoot < 1.0f ) ||
1190 ( firstItemScrollPosition <= mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), layoutSize) &&
1191 currentOvershoot > -1.0f ) )
1193 mTotalPanDisplacement += gesture.displacement;
1196 mScrollOvershoot = CalculateScrollOvershoot();
1198 // If the view is moved in a direction against the overshoot indicator, then the indicator should be animated off.
1199 // First make sure we are not in an animation, otherwise a previously started
1200 // off-animation will be overwritten as the user continues scrolling.
1203 // Check if the movement is against the current overshoot amount (if we are currently displaying the indicator).
1204 if( ( ( mScrollOvershoot > Math::MACHINE_EPSILON_0 ) && ( mScrollDistance < -Math::MACHINE_EPSILON_0 ) ) ||
1205 ( ( mScrollOvershoot < Math::MACHINE_EPSILON_0 ) && ( mScrollDistance > Math::MACHINE_EPSILON_0 ) ) )
1207 // The user has moved against the indicator direction.
1208 // First, we reset the total displacement. This means the overshoot amount will become zero the next frame,
1209 // and if the user starts dragging in the overshoot direction again, the indicator will appear once more.
1210 mTotalPanDisplacement = Vector2::ZERO;
1211 // Animate the overshoot indicator off.
1212 AnimateScrollOvershoot( 0.0f, false );
1216 // Only set the property directly if we are not animating the overshoot away,
1217 // as otherwise this will overwrite the animation generated value.
1218 self.SetProperty( Toolkit::ItemView::Property::OVERSHOOT, mScrollOvershoot );
1224 case Gesture::Cancelled:
1226 mScrollAnimation = DoAnchoring();
1234 if (mScrollAnimation)
1236 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1237 mScrollAnimation.Play();
1241 bool ItemView::OnAccessibilityPan(PanGesture gesture)
1247 Actor ItemView::GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
1249 Actor nextFocusActor;
1253 if(!actor || actor == this->Self())
1255 nextFocusActor = GetItem(nextItemID);
1257 else if(actor && actor.GetParent() == this->Self())
1259 int itemID = GetItemId(actor);
1260 nextItemID = mActiveLayout->GetNextFocusItemID(itemID, mItemFactory.GetNumberOfItems(), direction, loopEnabled);
1261 nextFocusActor = GetItem(nextItemID);
1262 if(nextFocusActor == actor)
1264 // need to pass NULL actor back to focus manager
1265 nextFocusActor.Reset();
1266 return nextFocusActor;
1269 float layoutPosition = mActiveLayout->GetClosestAnchorPosition( GetCurrentLayoutPosition(0) );
1270 Vector3 layoutSize = Self().GetCurrentSize();
1273 // likely the current item is not buffered, so not in our item pool, probably best to get first viewable item
1274 ItemRange viewableItems = mActiveLayout->GetItemsWithinArea(layoutPosition, layoutSize);
1275 nextItemID = viewableItems.begin;
1276 nextFocusActor = GetItem(nextItemID);
1279 return nextFocusActor;
1282 void ItemView::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
1284 // only in this function if our chosen focus actor was actually used
1285 if(commitedFocusableActor)
1287 int nextItemID = GetItemId(commitedFocusableActor);
1288 float layoutPosition = GetCurrentLayoutPosition(0);
1289 Vector3 layoutSize = Self().GetCurrentSize();
1291 float scrollTo = mActiveLayout->GetClosestOnScreenLayoutPosition(nextItemID, layoutPosition, layoutSize);
1292 ScrollTo(Vector2(0.0f, scrollTo), DEFAULT_KEYBOARD_FOCUS_SCROLL_DURATION);
1296 Animation ItemView::DoAnchoring()
1298 Animation anchoringAnimation;
1299 Actor self = Self();
1301 if (mActiveLayout && mAnchoringEnabled)
1303 float anchorPosition = mActiveLayout->GetClosestAnchorPosition( GetCurrentLayoutPosition(0) );
1305 anchoringAnimation = Animation::New(mAnchoringDuration);
1306 anchoringAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), anchorPosition, AlphaFunction::EASE_OUT );
1307 anchoringAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::SCROLL_SPEED), 0.0f, AlphaFunction::EASE_OUT );
1310 AnimateScrollOvershoot(0.0f);
1314 return anchoringAnimation;
1317 void ItemView::OnScrollFinished(Animation& source)
1319 Actor self = Self();
1321 RemoveAnimation(mScrollAnimation); // mScrollAnimation is used to query whether we're scrolling
1323 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1325 if(mIsFlicking && fabsf(mScrollOvershoot) > Math::MACHINE_EPSILON_1)
1327 AnimateScrollOvershoot( mScrollOvershoot > 0.0f ? 1.0f : -1.0f, true);
1331 // Reset the overshoot
1332 AnimateScrollOvershoot( 0.0f );
1334 mIsFlicking = false;
1336 mScrollOvershoot = 0.0f;
1339 void ItemView::OnLayoutActivationScrollFinished(Animation& source)
1341 RemoveAnimation(mScrollAnimation);
1342 mRefreshEnabled = true;
1343 DoRefresh(GetCurrentLayoutPosition(0), true);
1345 // Emit the layout activated signal
1346 mLayoutActivatedSignal.Emit();
1349 void ItemView::OnOvershootOnFinished(Animation& animation)
1351 mAnimatingOvershootOn = false;
1352 mScrollOvershootAnimation.FinishedSignal().Disconnect(this, &ItemView::OnOvershootOnFinished);
1353 RemoveAnimation(mScrollOvershootAnimation);
1354 if(mAnimateOvershootOff)
1356 AnimateScrollOvershoot(0.0f);
1358 mInAnimation = false;
1361 void ItemView::ScrollToItem(unsigned int itemId, float durationSeconds)
1363 Actor self = Self();
1364 const Vector3 layoutSize = Self().GetCurrentSize();
1365 float firstItemScrollPosition = ClampFirstItemPosition(mActiveLayout->GetItemScrollToPosition(itemId), layoutSize, *mActiveLayout);
1367 if(durationSeconds > 0.0f)
1369 RemoveAnimation(mScrollAnimation);
1370 mScrollAnimation = Animation::New(durationSeconds);
1371 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, AlphaFunction::EASE_OUT );
1372 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1373 mScrollAnimation.Play();
1377 self.SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1378 AnimateScrollOvershoot(0.0f);
1381 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1382 mRefreshEnabled = true;
1385 void ItemView::RemoveAnimation(Animation& animation)
1389 // Cease animating, and reset handle.
1395 void ItemView::CalculateDomainSize(const Vector3& layoutSize)
1397 Actor self = Self();
1399 Vector3 firstItemPosition(Vector3::ZERO);
1400 Vector3 lastItemPosition(Vector3::ZERO);
1404 firstItemPosition = mActiveLayout->GetItemPosition( 0,0,layoutSize );
1406 float minLayoutPosition = mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), layoutSize);
1407 lastItemPosition = mActiveLayout->GetItemPosition( fabs(minLayoutPosition),fabs(minLayoutPosition),layoutSize );
1411 if(IsHorizontal(mActiveLayout->GetOrientation()))
1413 domainSize = fabs(firstItemPosition.x - lastItemPosition.x);
1417 domainSize = fabs(firstItemPosition.y - lastItemPosition.y);
1420 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MIN, Vector2::ZERO);
1421 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1423 self.SetProperty(Toolkit::ItemView::Property::SCROLL_CONTENT_SIZE, domainSize);
1425 bool isLayoutScrollable = IsLayoutScrollable(layoutSize);
1426 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL, isLayoutScrollable);
1427 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL, false);
1431 Vector2 ItemView::GetDomainSize() const
1433 Actor self = Self();
1435 float minScrollPosition = self.GetProperty<float>(Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_Y);
1436 float maxScrollPosition = self.GetProperty<float>(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX_Y);
1438 return Vector2(0.0f, fabs(GetScrollPosition(minScrollPosition, self.GetCurrentSize()) - GetScrollPosition(-maxScrollPosition, self.GetCurrentSize())));
1441 bool ItemView::IsLayoutScrollable(const Vector3& layoutSize)
1443 Actor self = Self();
1445 float currentLayoutPosition = ClampFirstItemPosition( GetCurrentLayoutPosition(0), layoutSize, *mActiveLayout );
1446 float forwardClampedPosition = ClampFirstItemPosition(currentLayoutPosition + 1.0, layoutSize, *mActiveLayout);
1447 float backwardClampedPosition = ClampFirstItemPosition(currentLayoutPosition - 1.0, layoutSize, *mActiveLayout);
1449 return (fabs(forwardClampedPosition - backwardClampedPosition) > Math::MACHINE_EPSILON_0);
1452 float ItemView::GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const
1454 Vector3 firstItemPosition( mActiveLayout->GetItemPosition(0, layoutPosition, layoutSize ) );
1455 return IsHorizontal(mActiveLayout->GetOrientation()) ? firstItemPosition.x: firstItemPosition.y;
1458 Vector2 ItemView::GetCurrentScrollPosition() const
1460 return Vector2(0.0f, GetScrollPosition(GetCurrentLayoutPosition(0), Self().GetCurrentSize()));
1463 void ItemView::AddOverlay(Actor actor)
1465 actor.SetDrawMode( DrawMode::OVERLAY_2D );
1469 void ItemView::RemoveOverlay(Actor actor)
1471 Self().Remove(actor);
1474 void ItemView::ScrollTo(const Vector2& position, float duration)
1476 Actor self = Self();
1477 const Vector3 layoutSize = Self().GetCurrentSize();
1479 float firstItemScrollPosition = ClampFirstItemPosition(position.y, layoutSize, *mActiveLayout);
1483 RemoveAnimation(mScrollAnimation);
1484 mScrollAnimation = Animation::New(duration);
1485 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, AlphaFunction::EASE_OUT );
1486 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1487 mScrollAnimation.Play();
1491 self.SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1492 AnimateScrollOvershoot(0.0f);
1495 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1496 mRefreshEnabled = true;
1499 void ItemView::SetOvershootEffectColor( const Vector4& color )
1501 mOvershootEffectColor = color;
1502 if( mOvershootOverlay )
1504 mOvershootOverlay.SetColor( color );
1508 void ItemView::EnableScrollOvershoot( bool enable )
1510 Actor self = Self();
1513 Property::Index effectOvershootPropertyIndex = Property::INVALID_INDEX;
1514 mOvershootOverlay = CreateBouncingEffectActor( effectOvershootPropertyIndex );
1515 mOvershootOverlay.SetColor(mOvershootEffectColor);
1516 mOvershootOverlay.SetParentOrigin(ParentOrigin::TOP_LEFT);
1517 mOvershootOverlay.SetAnchorPoint(AnchorPoint::TOP_LEFT);
1518 mOvershootOverlay.SetDrawMode( DrawMode::OVERLAY_2D );
1519 self.Add(mOvershootOverlay);
1521 Constraint constraint = Constraint::New<Vector3>( mOvershootOverlay, Actor::Property::SIZE, OvershootOverlaySizeConstraint(mOvershootSize.height) );
1522 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1523 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1524 constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
1527 mOvershootOverlay.SetSize(mOvershootSize.width, mOvershootSize.height);
1529 constraint = Constraint::New<Quaternion>( mOvershootOverlay, Actor::Property::ORIENTATION, OvershootOverlayRotationConstraint );
1530 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1531 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1532 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1535 constraint = Constraint::New<Vector3>( mOvershootOverlay, Actor::Property::POSITION, OvershootOverlayPositionConstraint );
1536 constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
1537 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1538 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1539 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1542 constraint = Constraint::New<bool>( mOvershootOverlay, Actor::Property::VISIBLE, OvershootOverlayVisibilityConstraint );
1543 constraint.AddSource( ParentSource( Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL ) );
1546 constraint = Constraint::New<float>( mOvershootOverlay, effectOvershootPropertyIndex, EqualToConstraint() );
1547 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1552 if( mOvershootOverlay )
1554 self.Remove(mOvershootOverlay);
1555 mOvershootOverlay.Reset();
1560 float ItemView::CalculateScrollOvershoot()
1562 float overshoot = 0.0f;
1566 // The overshoot must be calculated from the accumulated pan gesture displacement
1567 // since the pan gesture starts.
1568 Actor self = Self();
1569 float scrollDistance = CalculateScrollDistance(mTotalPanDisplacement, *mActiveLayout) * mActiveLayout->GetScrollSpeedFactor();
1570 float positionDelta = GetCurrentLayoutPosition(0) + scrollDistance;
1571 float minLayoutPosition = mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), Self().GetCurrentSize());
1572 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1573 float clamppedPosition = std::min(0.0f, std::max(minLayoutPosition, positionDelta));
1574 overshoot = positionDelta - clamppedPosition;
1577 return overshoot > 0.0f ? std::min(overshoot, 1.0f) : std::max(overshoot, -1.0f);
1580 void ItemView::AnimateScrollOvershoot(float overshootAmount, bool animateBack)
1582 bool animatingOn = fabsf(overshootAmount) > Math::MACHINE_EPSILON_1;
1584 // make sure we animate back if needed
1585 mAnimateOvershootOff = animateBack || (!animatingOn && mAnimatingOvershootOn);
1587 if( mAnimatingOvershootOn )
1589 // animating on, do not allow animate off
1593 Actor self = Self();
1595 if(mOvershootAnimationSpeed > Math::MACHINE_EPSILON_0)
1597 float currentOvershoot = self.GetProperty<float>(Toolkit::ItemView::Property::OVERSHOOT);
1598 float duration = 0.0f;
1600 if (mOvershootOverlay)
1602 duration = mOvershootOverlay.GetCurrentSize().height * (animatingOn ? (1.0f - fabsf(currentOvershoot)) : fabsf(currentOvershoot)) / mOvershootAnimationSpeed;
1605 // Mark the animation as in progress to prevent manual property sets overwriting it.
1606 mInAnimation = true;
1607 mAnimatingOvershootOn = animatingOn;
1608 RemoveAnimation(mScrollOvershootAnimation);
1609 mScrollOvershootAnimation = Animation::New(duration);
1610 mScrollOvershootAnimation.FinishedSignal().Connect(this, &ItemView::OnOvershootOnFinished);
1611 mScrollOvershootAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::OVERSHOOT), overshootAmount, TimePeriod(0.0f, duration) );
1612 mScrollOvershootAnimation.Play();
1616 self.SetProperty( Toolkit::ItemView::Property::OVERSHOOT, overshootAmount );
1620 void ItemView::SetItemsParentOrigin( const Vector3& parentOrigin )
1622 if( parentOrigin != mItemsParentOrigin )
1624 mItemsParentOrigin = parentOrigin;
1625 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1627 iter->second.SetParentOrigin(parentOrigin);
1632 Vector3 ItemView::GetItemsParentOrigin() const
1634 return mItemsParentOrigin;
1637 void ItemView::SetItemsAnchorPoint( const Vector3& anchorPoint )
1639 if( anchorPoint != mItemsAnchorPoint )
1641 mItemsAnchorPoint = anchorPoint;
1642 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1644 iter->second.SetAnchorPoint(anchorPoint);
1649 Vector3 ItemView::GetItemsAnchorPoint() const
1651 return mItemsAnchorPoint;
1654 void ItemView::GetItemsRange(ItemRange& range)
1656 if( !mItemPool.empty() )
1658 range.begin = mItemPool.begin()->first;
1659 range.end = mItemPool.rbegin()->first + 1;
1668 bool ItemView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1670 Dali::BaseHandle handle( object );
1672 bool connected( true );
1673 Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( handle );
1675 if( 0 == strcmp( signalName.c_str(), LAYOUT_ACTIVATED_SIGNAL ) )
1677 itemView.LayoutActivatedSignal().Connect( tracker, functor );
1681 // signalName does not match any signal
1688 } // namespace Internal
1690 } // namespace Toolkit