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),
293 mAnimatingOvershootOn(false),
294 mAnimateOvershootOff(false),
295 mAnchoringEnabled(false),
296 mAnchoringDuration(DEFAULT_ANCHORING_DURATION),
297 mRefreshIntervalLayoutPositions(0.0f),
298 mRefreshOrderHint(true/*Refresh item 0 first*/),
299 mMinimumSwipeSpeed(DEFAULT_MINIMUM_SWIPE_SPEED),
300 mMinimumSwipeDistance(DEFAULT_MINIMUM_SWIPE_DISTANCE),
301 mWheelScrollDistanceStep(0.0f),
302 mScrollDistance(0.0f),
304 mTotalPanDisplacement(Vector2::ZERO),
305 mScrollOvershoot(0.0f),
307 mGestureState(Gesture::Clear),
309 mRefreshEnabled(true),
310 mItemsParentOrigin( ParentOrigin::CENTER),
311 mItemsAnchorPoint( AnchorPoint::CENTER)
315 void ItemView::OnInitialize()
319 Vector2 stageSize = Stage::GetCurrent().GetSize();
320 mWheelScrollDistanceStep = stageSize.y * DEFAULT_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION;
322 EnableGestureDetection(Gesture::Type(Gesture::Pan));
324 mWheelEventFinishedTimer = Timer::New( WHEEL_EVENT_FINISHED_TIME_OUT );
325 mWheelEventFinishedTimer.TickSignal().Connect( this, &ItemView::OnWheelEventFinished );
327 SetRefreshInterval(DEFAULT_REFRESH_INTERVAL_LAYOUT_POSITIONS);
330 ItemView::~ItemView()
334 unsigned int ItemView::GetLayoutCount() const
336 return mLayouts.size();
339 void ItemView::AddLayout(ItemLayout& layout)
341 mLayouts.push_back(ItemLayoutPtr(&layout));
344 void ItemView::RemoveLayout(unsigned int layoutIndex)
346 DALI_ASSERT_ALWAYS(layoutIndex < mLayouts.size());
348 if (mActiveLayout == mLayouts[layoutIndex].Get())
350 mActiveLayout = NULL;
353 mLayouts.erase(mLayouts.begin() + layoutIndex);
356 ItemLayoutPtr ItemView::GetLayout(unsigned int layoutIndex) const
358 return mLayouts[layoutIndex];
361 ItemLayoutPtr ItemView::GetActiveLayout() const
363 return ItemLayoutPtr(mActiveLayout);
366 float ItemView::GetCurrentLayoutPosition(unsigned int itemId) const
368 return Self().GetProperty<float>( Toolkit::ItemView::Property::LAYOUT_POSITION ) + static_cast<float>( itemId );
371 void ItemView::ActivateLayout(unsigned int layoutIndex, const Vector3& targetSize, float durationSeconds)
373 DALI_ASSERT_ALWAYS(layoutIndex < mLayouts.size());
375 mRefreshEnabled = false;
379 // The ItemView size should match the active layout size
380 self.SetSize(targetSize);
381 mActiveLayoutTargetSize = targetSize;
383 // Switch to the new layout
384 mActiveLayout = mLayouts[layoutIndex].Get();
386 // Move the items to the new layout positions...
388 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
390 unsigned int itemId = iter->first;
391 Actor actor = iter->second;
393 // Remove constraints from previous layout
394 actor.RemoveConstraints();
397 mActiveLayout->GetItemSize( itemId, targetSize, size );
398 actor.SetSize( size.GetVectorXY() );
400 mActiveLayout->ApplyConstraints(actor, itemId, targetSize, Self() );
403 // Refresh the new layout
404 ItemRange range = GetItemRange(*mActiveLayout, targetSize, GetCurrentLayoutPosition(0), false/* don't reserve extra*/);
405 AddActorsWithinRange( range, targetSize );
407 // Scroll to an appropriate layout position
409 bool scrollAnimationNeeded(false);
410 float firstItemScrollPosition(0.0f);
412 float current = GetCurrentLayoutPosition(0);
413 float minimum = ClampFirstItemPosition(current, targetSize, *mActiveLayout);
415 if (current < minimum)
417 scrollAnimationNeeded = true;
418 firstItemScrollPosition = minimum;
420 else if (mAnchoringEnabled)
422 scrollAnimationNeeded = true;
423 firstItemScrollPosition = mActiveLayout->GetClosestAnchorPosition(current);
426 if (scrollAnimationNeeded)
428 RemoveAnimation(mScrollAnimation);
429 mScrollAnimation = Animation::New(durationSeconds);
430 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, AlphaFunction::EASE_OUT );
431 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnLayoutActivationScrollFinished);
432 mScrollAnimation.Play();
436 // Emit the layout activated signal
437 mLayoutActivatedSignal.Emit();
440 AnimateScrollOvershoot(0.0f);
441 mScrollOvershoot = 0.0f;
443 Radian scrollDirection(mActiveLayout->GetScrollDirection());
444 self.SetProperty(Toolkit::ItemView::Property::SCROLL_DIRECTION, Vector2(sinf(scrollDirection), cosf(scrollDirection)));
445 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_ORIENTATION, static_cast<int>(mActiveLayout->GetOrientation()));
446 self.SetProperty(Toolkit::ItemView::Property::SCROLL_SPEED, mScrollSpeed);
448 CalculateDomainSize(targetSize);
451 void ItemView::DeactivateCurrentLayout()
455 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
457 Actor actor = iter->second;
458 actor.RemoveConstraints();
461 mActiveLayout = NULL;
465 void ItemView::OnRefreshNotification(PropertyNotification& source)
467 // Cancel scroll animation to prevent any fighting of setting the scroll position property by scroll bar during fast scroll.
468 if(!mRefreshEnabled && mScrollAnimation)
470 RemoveAnimation(mScrollAnimation);
473 // Only cache extra items when it is not a fast scroll
474 DoRefresh(GetCurrentLayoutPosition(0), mRefreshEnabled || mScrollAnimation);
477 void ItemView::Refresh()
479 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter )
481 ReleaseActor( iter->first, iter->second );
485 DoRefresh(GetCurrentLayoutPosition(0), true);
488 void ItemView::DoRefresh(float currentLayoutPosition, bool cacheExtra)
492 ItemRange range = GetItemRange(*mActiveLayout, mActiveLayoutTargetSize, currentLayoutPosition, cacheExtra/*reserve extra*/);
493 RemoveActorsOutsideRange( range );
494 AddActorsWithinRange( range, Self().GetCurrentSize() );
496 mScrollUpdatedSignal.Emit( Vector2(0.0f, currentLayoutPosition) );
500 void ItemView::SetMinimumSwipeSpeed(float speed)
502 mMinimumSwipeSpeed = speed;
505 float ItemView::GetMinimumSwipeSpeed() const
507 return mMinimumSwipeSpeed;
510 void ItemView::SetMinimumSwipeDistance(float distance)
512 mMinimumSwipeDistance = distance;
515 float ItemView::GetMinimumSwipeDistance() const
517 return mMinimumSwipeDistance;
520 void ItemView::SetWheelScrollDistanceStep(float step)
522 mWheelScrollDistanceStep = step;
525 float ItemView::GetWheelScrollDistanceStep() const
527 return mWheelScrollDistanceStep;
530 void ItemView::SetAnchoring(bool enabled)
532 mAnchoringEnabled = enabled;
535 bool ItemView::GetAnchoring() const
537 return mAnchoringEnabled;
540 void ItemView::SetAnchoringDuration(float durationSeconds)
542 mAnchoringDuration = durationSeconds;
545 float ItemView::GetAnchoringDuration() const
547 return mAnchoringDuration;
550 void ItemView::SetRefreshInterval(float intervalLayoutPositions)
552 if( !Equals(mRefreshIntervalLayoutPositions, intervalLayoutPositions) )
554 mRefreshIntervalLayoutPositions = intervalLayoutPositions;
557 if(mRefreshNotification)
559 self.RemovePropertyNotification(mRefreshNotification);
561 mRefreshNotification = self.AddPropertyNotification( Toolkit::ItemView::Property::LAYOUT_POSITION, StepCondition(mRefreshIntervalLayoutPositions, 0.0f) );
562 mRefreshNotification.NotifySignal().Connect( this, &ItemView::OnRefreshNotification );
566 float ItemView::GetRefreshInterval() const
568 return mRefreshIntervalLayoutPositions;
571 void ItemView::SetRefreshEnabled(bool enabled)
573 mRefreshEnabled = enabled;
576 Actor ItemView::GetItem(unsigned int itemId) const
580 ConstItemPoolIter iter = mItemPool.find( itemId );
581 if( iter != mItemPool.end() )
583 actor = iter->second;
589 unsigned int ItemView::GetItemId( Actor actor ) const
591 unsigned int itemId( 0 );
593 for ( ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter )
595 if( iter->second == actor )
597 itemId = iter->first;
605 void ItemView::InsertItem( Item newItem, float durationSeconds )
608 Vector3 layoutSize = Self().GetCurrentSize();
610 Actor displacedActor;
611 ItemPoolIter afterDisplacedIter = mItemPool.end();
613 ItemPoolIter foundIter = mItemPool.find( newItem.first );
614 if( mItemPool.end() != foundIter )
616 SetupActor( newItem, layoutSize );
617 Self().Add( newItem.second );
619 displacedActor = foundIter->second;
620 foundIter->second = newItem.second;
622 afterDisplacedIter = ++foundIter;
626 // Inserting before the existing item range?
627 ItemPoolIter iter = mItemPool.begin();
628 if( iter != mItemPool.end() &&
629 iter->first > newItem.first )
631 displacedActor = iter->second;
632 mItemPool.erase( iter++ ); // iter is still valid after the erase
634 afterDisplacedIter = iter;
640 // Move the existing actors to make room
641 for( ItemPoolIter iter = afterDisplacedIter; mItemPool.end() != iter; ++iter )
643 Actor temp = iter->second;
644 iter->second = displacedActor;
645 displacedActor = temp;
647 iter->second.RemoveConstraints();
648 mActiveLayout->ApplyConstraints( iter->second, iter->first, layoutSize, Self() );
652 ItemPool::reverse_iterator lastIter = mItemPool.rbegin();
653 if ( lastIter != mItemPool.rend() )
655 ItemId lastId = lastIter->first;
656 Item lastItem( lastId + 1, displacedActor );
657 mItemPool.insert( lastItem );
659 lastItem.second.RemoveConstraints();
660 mActiveLayout->ApplyConstraints( lastItem.second, lastItem.first, layoutSize, Self() );
664 CalculateDomainSize( layoutSize );
666 mAddingItems = false;
669 void ItemView::InsertItems( const ItemContainer& newItems, float durationSeconds )
672 Vector3 layoutSize = Self().GetCurrentSize();
674 // Insert from lowest id to highest
675 std::set<Item> sortedItems;
676 for( ConstItemIter iter = newItems.begin(); newItems.end() != iter; ++iter )
678 sortedItems.insert( *iter );
681 for( std::set<Item>::iterator iter = sortedItems.begin(); sortedItems.end() != iter; ++iter )
683 Self().Add( iter->second );
685 ItemPoolIter foundIter = mItemPool.find( iter->first );
686 if( mItemPool.end() != foundIter )
688 Actor moveMe = foundIter->second;
689 foundIter->second = iter->second;
691 // Move the existing actors to make room
692 for( ItemPoolIter iter = ++foundIter; mItemPool.end() != iter; ++iter )
694 Actor temp = iter->second;
695 iter->second = moveMe;
700 ItemId lastId = mItemPool.rbegin()->first;
701 Item lastItem( lastId + 1, moveMe );
702 mItemPool.insert( lastItem );
706 mItemPool.insert( *iter );
710 // Relayout everything
711 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
714 if( FindById( newItems, iter->first ) )
716 SetupActor( *iter, layoutSize );
720 iter->second.RemoveConstraints();
721 mActiveLayout->ApplyConstraints( iter->second, iter->first, layoutSize, Self() );
725 CalculateDomainSize( layoutSize );
727 mAddingItems = false;
730 void ItemView::RemoveItem( unsigned int itemId, float durationSeconds )
732 bool actorsReordered = RemoveActor( itemId );
733 if( actorsReordered )
735 ReapplyAllConstraints();
741 void ItemView::RemoveItems( const ItemIdContainer& itemIds, float durationSeconds )
743 bool actorsReordered( false );
745 // Remove from highest id to lowest
746 set<ItemId> sortedItems;
747 for( ConstItemIdIter iter = itemIds.begin(); itemIds.end() != iter; ++iter )
749 sortedItems.insert( *iter );
752 for( set<ItemId>::reverse_iterator iter = sortedItems.rbegin(); sortedItems.rend() != iter; ++iter )
754 if( RemoveActor( *iter ) )
756 actorsReordered = true;
760 if( actorsReordered )
762 ReapplyAllConstraints();
768 bool ItemView::RemoveActor(unsigned int itemId)
770 bool reordered( false );
772 ItemPoolIter removeIter = mItemPool.find( itemId );
773 if( removeIter != mItemPool.end() )
775 ReleaseActor(itemId, removeIter->second);
779 // Removing before the existing item range?
780 ItemPoolIter iter = mItemPool.begin();
781 if( iter != mItemPool.end() &&
782 iter->first > itemId )
784 // In order to decrement the first visible item ID
785 mItemPool.insert( Item(iter->first - 1, Actor()) );
787 removeIter = mItemPool.begin();
791 if( removeIter != mItemPool.end() )
795 // Adjust the remaining item IDs, for example if item 2 is removed:
796 // Initial actors: After insert:
797 // ID 1 - ActorA ID 1 - ActorA
798 // ID 2 - ActorB ID 2 - ActorC (previously ID 3)
799 // ID 3 - ActorC ID 3 - ActorB (previously ID 4)
801 for (ItemPoolIter iter = removeIter; iter != mItemPool.end(); ++iter)
803 if( iter->first < mItemPool.rbegin()->first )
805 iter->second = mItemPool[ iter->first + 1 ];
809 mItemPool.erase( iter );
818 void ItemView::ReplaceItem( Item replacementItem, float durationSeconds )
821 Vector3 layoutSize = Self().GetCurrentSize();
823 SetupActor( replacementItem, layoutSize );
824 Self().Add( replacementItem.second );
826 const ItemPoolIter iter = mItemPool.find( replacementItem.first );
827 if( mItemPool.end() != iter )
829 ReleaseActor(iter->first, iter->second);
830 iter->second = replacementItem.second;
834 mItemPool.insert( replacementItem );
837 CalculateDomainSize( layoutSize );
839 mAddingItems = false;
842 void ItemView::ReplaceItems( const ItemContainer& replacementItems, float durationSeconds )
844 for( ConstItemIter iter = replacementItems.begin(); replacementItems.end() != iter; ++iter )
846 ReplaceItem( *iter, durationSeconds );
850 void ItemView::RemoveActorsOutsideRange( ItemRange range )
852 // Remove unwanted actors from the ItemView & ItemPool
853 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); )
855 unsigned int current = iter->first;
857 if( ! range.Within( current ) )
859 ReleaseActor(iter->first, iter->second);
861 mItemPool.erase( iter++ ); // erase invalidates the return value of post-increment; iter remains valid
870 void ItemView::AddActorsWithinRange( ItemRange range, const Vector3& layoutSize )
872 range.end = std::min(mItemFactory.GetNumberOfItems(), range.end);
874 // The order of addition depends on the scroll direction.
875 if (mRefreshOrderHint)
877 for (unsigned int itemId = range.begin; itemId < range.end; ++itemId)
879 AddNewActor( itemId, layoutSize );
884 for (unsigned int itemId = range.end; itemId > range.begin; --itemId)
886 AddNewActor( itemId-1, layoutSize );
890 // Total number of items may change dynamically.
891 // Always recalculate the domain size to reflect that.
892 CalculateDomainSize(Self().GetCurrentSize());
895 void ItemView::AddNewActor( unsigned int itemId, const Vector3& layoutSize )
899 if( mItemPool.end() == mItemPool.find( itemId ) )
901 Actor actor = mItemFactory.NewItem( itemId );
905 Item newItem( itemId, actor );
907 mItemPool.insert( newItem );
909 SetupActor( newItem, layoutSize );
914 mAddingItems = false;
917 void ItemView::SetupActor( Item item, const Vector3& layoutSize )
919 item.second.SetParentOrigin( mItemsParentOrigin );
920 item.second.SetAnchorPoint( mItemsAnchorPoint );
925 mActiveLayout->GetItemSize( item.first, mActiveLayoutTargetSize, size );
926 item.second.SetSize( size.GetVectorXY() );
928 mActiveLayout->ApplyConstraints( item.second, item.first, layoutSize, Self() );
932 void ItemView::ReleaseActor( ItemId item, Actor actor )
934 Self().Remove( actor );
935 mItemFactory.ItemReleased(item, actor);
938 ItemRange ItemView::GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra)
940 unsigned int itemCount = mItemFactory.GetNumberOfItems();
942 ItemRange available(0u, itemCount);
944 ItemRange range = layout.GetItemsWithinArea( layoutPosition, layoutSize );
948 // Add the reserve items for scrolling
949 unsigned int extra = layout.GetReserveItemCount(layoutSize);
950 range.begin = (range.begin >= extra) ? (range.begin - extra) : 0u;
954 return range.Intersection(available);
957 void ItemView::OnChildAdd(Actor& child)
961 // We don't want to do this downcast check for any item added by ItemView itself.
962 Dali::Toolkit::ScrollBar scrollBar = Dali::Toolkit::ScrollBar::DownCast(child);
965 scrollBar.SetScrollPropertySource(Self(),
966 Toolkit::ItemView::Property::LAYOUT_POSITION,
967 Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_Y,
968 Toolkit::Scrollable::Property::SCROLL_POSITION_MAX_Y,
969 Toolkit::ItemView::Property::SCROLL_CONTENT_SIZE);
974 bool ItemView::OnTouchEvent(const TouchEvent& event)
976 // Ignore events with multiple-touch points
977 if (event.GetPointCount() != 1)
982 if (event.GetPoint(0).state == TouchPoint::Down)
984 // Cancel ongoing scrolling etc.
985 mGestureState = Gesture::Clear;
987 mScrollDistance = 0.0f;
989 Self().SetProperty(Toolkit::ItemView::Property::SCROLL_SPEED, mScrollSpeed);
991 mScrollOvershoot = 0.0f;
992 AnimateScrollOvershoot(0.0f);
996 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
999 RemoveAnimation(mScrollAnimation);
1002 return true; // consume since we're potentially scrolling
1005 bool ItemView::OnWheelEvent(const WheelEvent& event)
1007 // Respond the wheel event to scroll
1010 Actor self = Self();
1011 const Vector3 layoutSize = Self().GetCurrentSize();
1012 float layoutPositionDelta = GetCurrentLayoutPosition(0) - (event.z * mWheelScrollDistanceStep * mActiveLayout->GetScrollSpeedFactor());
1013 float firstItemScrollPosition = ClampFirstItemPosition(layoutPositionDelta, layoutSize, *mActiveLayout);
1015 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1017 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1018 mRefreshEnabled = true;
1021 if (mWheelEventFinishedTimer.IsRunning())
1023 mWheelEventFinishedTimer.Stop();
1026 mWheelEventFinishedTimer.Start();
1031 bool ItemView::OnWheelEventFinished()
1035 RemoveAnimation(mScrollAnimation);
1037 // No more wheel events coming. Do the anchoring if enabled.
1038 mScrollAnimation = DoAnchoring();
1039 if (mScrollAnimation)
1041 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1042 mScrollAnimation.Play();
1046 mScrollOvershoot = 0.0f;
1047 AnimateScrollOvershoot(0.0f);
1049 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1056 void ItemView::ReapplyAllConstraints()
1058 Vector3 layoutSize = Self().GetCurrentSize();
1060 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1062 unsigned int id = iter->first;
1063 Actor actor = iter->second;
1065 actor.RemoveConstraints();
1066 mActiveLayout->ApplyConstraints(actor, id, layoutSize, Self());
1070 void ItemView::OnItemsRemoved()
1072 CalculateDomainSize(Self().GetCurrentSize());
1074 // Adjust scroll-position after an item is removed
1077 float firstItemScrollPosition = ClampFirstItemPosition(GetCurrentLayoutPosition(0), Self().GetCurrentSize(), *mActiveLayout);
1078 Self().SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1082 float ItemView::ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout)
1084 Actor self = Self();
1085 float minLayoutPosition = layout.GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), targetSize);
1086 float clamppedPosition = std::min(0.0f, std::max(minLayoutPosition, targetPosition));
1087 mScrollOvershoot = targetPosition - clamppedPosition;
1088 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1090 return clamppedPosition;
1093 void ItemView::OnPan( const PanGesture& gesture )
1095 Actor self = Self();
1096 const Vector3 layoutSize = Self().GetCurrentSize();
1098 RemoveAnimation(mScrollAnimation);
1100 // Short-circuit if there is no active layout
1103 mGestureState = Gesture::Clear;
1107 mGestureState = gesture.state;
1109 switch (mGestureState)
1111 case Gesture::Finished:
1114 if (fabsf(mScrollDistance) > mMinimumSwipeDistance &&
1115 mScrollSpeed > mMinimumSwipeSpeed)
1117 float direction = (mScrollDistance < 0.0f) ? -1.0f : 1.0f;
1119 mRefreshOrderHint = true;
1121 float currentLayoutPosition = GetCurrentLayoutPosition(0);
1122 float firstItemScrollPosition = ClampFirstItemPosition(currentLayoutPosition + mScrollSpeed * direction,
1126 if (mAnchoringEnabled)
1128 firstItemScrollPosition = mActiveLayout->GetClosestAnchorPosition(firstItemScrollPosition);
1131 RemoveAnimation(mScrollAnimation);
1133 float flickAnimationDuration = Clamp( mActiveLayout->GetItemFlickAnimationDuration() * std::max(1.0f, fabsf(firstItemScrollPosition - GetCurrentLayoutPosition(0)))
1134 , DEFAULT_MINIMUM_SWIPE_DURATION, DEFAULT_MAXIMUM_SWIPE_DURATION);
1136 mScrollAnimation = Animation::New(flickAnimationDuration);
1137 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION ), firstItemScrollPosition, AlphaFunction::EASE_OUT );
1138 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::SCROLL_SPEED), 0.0f, AlphaFunction::EASE_OUT );
1141 // Check whether it has already scrolled to the end
1142 if(fabs(currentLayoutPosition - firstItemScrollPosition) > Math::MACHINE_EPSILON_0)
1144 AnimateScrollOvershoot(0.0f);
1148 // Anchoring may be triggered when there was no swipe
1149 if (!mScrollAnimation)
1151 mScrollAnimation = DoAnchoring();
1154 // Reset the overshoot if no scroll animation.
1155 if (!mScrollAnimation)
1157 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1159 AnimateScrollOvershoot(0.0f, false);
1164 case Gesture::Started: // Fall through
1166 mTotalPanDisplacement = Vector2::ZERO;
1167 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1168 mRefreshEnabled = true;
1171 case Gesture::Continuing:
1173 mScrollDistance = CalculateScrollDistance(gesture.displacement, *mActiveLayout);
1174 mScrollSpeed = Clamp((gesture.GetSpeed() * gesture.GetSpeed() * mActiveLayout->GetFlickSpeedFactor() * MILLISECONDS_PER_SECONDS), 0.0f, mActiveLayout->GetMaximumSwipeSpeed());
1176 // Refresh order depends on the direction of the scroll; negative is towards the last item.
1177 mRefreshOrderHint = mScrollDistance < 0.0f;
1179 float layoutPositionDelta = GetCurrentLayoutPosition(0) + (mScrollDistance * mActiveLayout->GetScrollSpeedFactor());
1181 float firstItemScrollPosition = ClampFirstItemPosition(layoutPositionDelta, layoutSize, *mActiveLayout);
1183 float currentOvershoot = self.GetProperty<float>(Toolkit::ItemView::Property::OVERSHOOT);
1185 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1187 if( ( firstItemScrollPosition >= 0.0f &&
1188 currentOvershoot < 1.0f ) ||
1189 ( firstItemScrollPosition <= mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), layoutSize) &&
1190 currentOvershoot > -1.0f ) )
1192 mTotalPanDisplacement += gesture.displacement;
1195 mScrollOvershoot = CalculateScrollOvershoot();
1196 self.SetProperty( Toolkit::ItemView::Property::OVERSHOOT, mScrollOvershoot );
1200 case Gesture::Cancelled:
1202 mScrollAnimation = DoAnchoring();
1210 if (mScrollAnimation)
1212 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1213 mScrollAnimation.Play();
1217 bool ItemView::OnAccessibilityPan(PanGesture gesture)
1223 Actor ItemView::GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
1225 Actor nextFocusActor;
1229 if(!actor || actor == this->Self())
1231 nextFocusActor = GetItem(nextItemID);
1233 else if(actor && actor.GetParent() == this->Self())
1235 int itemID = GetItemId(actor);
1236 nextItemID = mActiveLayout->GetNextFocusItemID(itemID, mItemFactory.GetNumberOfItems(), direction, loopEnabled);
1237 nextFocusActor = GetItem(nextItemID);
1238 if(nextFocusActor == actor)
1240 // need to pass NULL actor back to focus manager
1241 nextFocusActor.Reset();
1242 return nextFocusActor;
1245 float layoutPosition = mActiveLayout->GetClosestAnchorPosition( GetCurrentLayoutPosition(0) );
1246 Vector3 layoutSize = Self().GetCurrentSize();
1249 // likely the current item is not buffered, so not in our item pool, probably best to get first viewable item
1250 ItemRange viewableItems = mActiveLayout->GetItemsWithinArea(layoutPosition, layoutSize);
1251 nextItemID = viewableItems.begin;
1252 nextFocusActor = GetItem(nextItemID);
1255 return nextFocusActor;
1258 void ItemView::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
1260 // only in this function if our chosen focus actor was actually used
1261 if(commitedFocusableActor)
1263 int nextItemID = GetItemId(commitedFocusableActor);
1264 float layoutPosition = GetCurrentLayoutPosition(0);
1265 Vector3 layoutSize = Self().GetCurrentSize();
1267 float scrollTo = mActiveLayout->GetClosestOnScreenLayoutPosition(nextItemID, layoutPosition, layoutSize);
1268 ScrollTo(Vector2(0.0f, scrollTo), DEFAULT_KEYBOARD_FOCUS_SCROLL_DURATION);
1272 Animation ItemView::DoAnchoring()
1274 Animation anchoringAnimation;
1275 Actor self = Self();
1277 if (mActiveLayout && mAnchoringEnabled)
1279 float anchorPosition = mActiveLayout->GetClosestAnchorPosition( GetCurrentLayoutPosition(0) );
1281 anchoringAnimation = Animation::New(mAnchoringDuration);
1282 anchoringAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), anchorPosition, AlphaFunction::EASE_OUT );
1283 anchoringAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::SCROLL_SPEED), 0.0f, AlphaFunction::EASE_OUT );
1286 AnimateScrollOvershoot(0.0f);
1290 return anchoringAnimation;
1293 void ItemView::OnScrollFinished(Animation& source)
1295 Actor self = Self();
1297 RemoveAnimation(mScrollAnimation); // mScrollAnimation is used to query whether we're scrolling
1299 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1301 if(mIsFlicking && fabsf(mScrollOvershoot) > Math::MACHINE_EPSILON_1)
1303 AnimateScrollOvershoot( mScrollOvershoot > 0.0f ? 1.0f : -1.0f, true);
1307 // Reset the overshoot
1308 AnimateScrollOvershoot( 0.0f );
1310 mIsFlicking = false;
1312 mScrollOvershoot = 0.0f;
1315 void ItemView::OnLayoutActivationScrollFinished(Animation& source)
1317 RemoveAnimation(mScrollAnimation);
1318 mRefreshEnabled = true;
1319 DoRefresh(GetCurrentLayoutPosition(0), true);
1321 // Emit the layout activated signal
1322 mLayoutActivatedSignal.Emit();
1325 void ItemView::OnOvershootOnFinished(Animation& animation)
1327 mAnimatingOvershootOn = false;
1328 mScrollOvershootAnimation.FinishedSignal().Disconnect(this, &ItemView::OnOvershootOnFinished);
1329 RemoveAnimation(mScrollOvershootAnimation);
1330 if(mAnimateOvershootOff)
1332 AnimateScrollOvershoot(0.0f);
1336 void ItemView::ScrollToItem(unsigned int itemId, float durationSeconds)
1338 Actor self = Self();
1339 const Vector3 layoutSize = Self().GetCurrentSize();
1340 float firstItemScrollPosition = ClampFirstItemPosition(mActiveLayout->GetItemScrollToPosition(itemId), layoutSize, *mActiveLayout);
1342 if(durationSeconds > 0.0f)
1344 RemoveAnimation(mScrollAnimation);
1345 mScrollAnimation = Animation::New(durationSeconds);
1346 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, AlphaFunction::EASE_OUT );
1347 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1348 mScrollAnimation.Play();
1352 self.SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1353 AnimateScrollOvershoot(0.0f);
1356 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1357 mRefreshEnabled = true;
1360 void ItemView::RemoveAnimation(Animation& animation)
1364 // Cease animating, and reset handle.
1370 void ItemView::CalculateDomainSize(const Vector3& layoutSize)
1372 Actor self = Self();
1374 Vector3 firstItemPosition(Vector3::ZERO);
1375 Vector3 lastItemPosition(Vector3::ZERO);
1379 firstItemPosition = mActiveLayout->GetItemPosition( 0,0,layoutSize );
1381 float minLayoutPosition = mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), layoutSize);
1382 lastItemPosition = mActiveLayout->GetItemPosition( fabs(minLayoutPosition),fabs(minLayoutPosition),layoutSize );
1386 if(IsHorizontal(mActiveLayout->GetOrientation()))
1388 domainSize = fabs(firstItemPosition.x - lastItemPosition.x);
1392 domainSize = fabs(firstItemPosition.y - lastItemPosition.y);
1395 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MIN, Vector2::ZERO);
1396 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1398 self.SetProperty(Toolkit::ItemView::Property::SCROLL_CONTENT_SIZE, domainSize);
1400 bool isLayoutScrollable = IsLayoutScrollable(layoutSize);
1401 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL, isLayoutScrollable);
1402 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL, false);
1406 Vector2 ItemView::GetDomainSize() const
1408 Actor self = Self();
1410 float minScrollPosition = self.GetProperty<float>(Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_Y);
1411 float maxScrollPosition = self.GetProperty<float>(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX_Y);
1413 return Vector2(0.0f, fabs(GetScrollPosition(minScrollPosition, self.GetCurrentSize()) - GetScrollPosition(-maxScrollPosition, self.GetCurrentSize())));
1416 bool ItemView::IsLayoutScrollable(const Vector3& layoutSize)
1418 Actor self = Self();
1420 float currentLayoutPosition = ClampFirstItemPosition( GetCurrentLayoutPosition(0), layoutSize, *mActiveLayout );
1421 float forwardClampedPosition = ClampFirstItemPosition(currentLayoutPosition + 1.0, layoutSize, *mActiveLayout);
1422 float backwardClampedPosition = ClampFirstItemPosition(currentLayoutPosition - 1.0, layoutSize, *mActiveLayout);
1424 return (fabs(forwardClampedPosition - backwardClampedPosition) > Math::MACHINE_EPSILON_0);
1427 float ItemView::GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const
1429 Vector3 firstItemPosition( mActiveLayout->GetItemPosition(0, layoutPosition, layoutSize ) );
1430 return IsHorizontal(mActiveLayout->GetOrientation()) ? firstItemPosition.x: firstItemPosition.y;
1433 Vector2 ItemView::GetCurrentScrollPosition() const
1435 return Vector2(0.0f, GetScrollPosition(GetCurrentLayoutPosition(0), Self().GetCurrentSize()));
1438 void ItemView::AddOverlay(Actor actor)
1440 actor.SetDrawMode( DrawMode::OVERLAY_2D );
1444 void ItemView::RemoveOverlay(Actor actor)
1446 Self().Remove(actor);
1449 void ItemView::ScrollTo(const Vector2& position, float duration)
1451 Actor self = Self();
1452 const Vector3 layoutSize = Self().GetCurrentSize();
1454 float firstItemScrollPosition = ClampFirstItemPosition(position.y, layoutSize, *mActiveLayout);
1458 RemoveAnimation(mScrollAnimation);
1459 mScrollAnimation = Animation::New(duration);
1460 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, AlphaFunction::EASE_OUT );
1461 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1462 mScrollAnimation.Play();
1466 self.SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1467 AnimateScrollOvershoot(0.0f);
1470 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1471 mRefreshEnabled = true;
1474 void ItemView::SetOvershootEffectColor( const Vector4& color )
1476 mOvershootEffectColor = color;
1477 if( mOvershootOverlay )
1479 mOvershootOverlay.SetColor( color );
1483 void ItemView::EnableScrollOvershoot( bool enable )
1485 Actor self = Self();
1488 Property::Index effectOvershootPropertyIndex = Property::INVALID_INDEX;
1489 mOvershootOverlay = CreateBouncingEffectActor( effectOvershootPropertyIndex );
1490 mOvershootOverlay.SetColor(mOvershootEffectColor);
1491 mOvershootOverlay.SetParentOrigin(ParentOrigin::TOP_LEFT);
1492 mOvershootOverlay.SetAnchorPoint(AnchorPoint::TOP_LEFT);
1493 mOvershootOverlay.SetDrawMode( DrawMode::OVERLAY_2D );
1494 self.Add(mOvershootOverlay);
1496 Constraint constraint = Constraint::New<Vector3>( mOvershootOverlay, Actor::Property::SIZE, OvershootOverlaySizeConstraint(mOvershootSize.height) );
1497 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1498 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1499 constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
1502 mOvershootOverlay.SetSize(mOvershootSize.width, mOvershootSize.height);
1504 constraint = Constraint::New<Quaternion>( mOvershootOverlay, Actor::Property::ORIENTATION, OvershootOverlayRotationConstraint );
1505 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1506 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1507 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1510 constraint = Constraint::New<Vector3>( mOvershootOverlay, Actor::Property::POSITION, OvershootOverlayPositionConstraint );
1511 constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
1512 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1513 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1514 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1517 constraint = Constraint::New<bool>( mOvershootOverlay, Actor::Property::VISIBLE, OvershootOverlayVisibilityConstraint );
1518 constraint.AddSource( ParentSource( Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL ) );
1521 constraint = Constraint::New<float>( mOvershootOverlay, effectOvershootPropertyIndex, EqualToConstraint() );
1522 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1527 if( mOvershootOverlay )
1529 self.Remove(mOvershootOverlay);
1530 mOvershootOverlay.Reset();
1535 float ItemView::CalculateScrollOvershoot()
1537 float overshoot = 0.0f;
1541 // The overshoot must be calculated from the accumulated pan gesture displacement
1542 // since the pan gesture starts.
1543 Actor self = Self();
1544 float scrollDistance = CalculateScrollDistance(mTotalPanDisplacement, *mActiveLayout) * mActiveLayout->GetScrollSpeedFactor();
1545 float positionDelta = GetCurrentLayoutPosition(0) + scrollDistance;
1546 float minLayoutPosition = mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), Self().GetCurrentSize());
1547 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1548 float clamppedPosition = std::min(0.0f, std::max(minLayoutPosition, positionDelta));
1549 overshoot = positionDelta - clamppedPosition;
1552 return overshoot > 0.0f ? std::min(overshoot, 1.0f) : std::max(overshoot, -1.0f);
1555 void ItemView::AnimateScrollOvershoot(float overshootAmount, bool animateBack)
1557 bool animatingOn = fabsf(overshootAmount) > Math::MACHINE_EPSILON_1;
1559 // make sure we animate back if needed
1560 mAnimateOvershootOff = animateBack || (!animatingOn && mAnimatingOvershootOn);
1562 if( mAnimatingOvershootOn )
1564 // animating on, do not allow animate off
1568 Actor self = Self();
1570 if(mOvershootAnimationSpeed > Math::MACHINE_EPSILON_0)
1572 float currentOvershoot = self.GetProperty<float>(Toolkit::ItemView::Property::OVERSHOOT);
1573 float duration = 0.0f;
1575 if (mOvershootOverlay)
1577 duration = mOvershootOverlay.GetCurrentSize().height * (animatingOn ? (1.0f - fabsf(currentOvershoot)) : fabsf(currentOvershoot)) / mOvershootAnimationSpeed;
1580 RemoveAnimation(mScrollOvershootAnimation);
1581 mScrollOvershootAnimation = Animation::New(duration);
1582 mScrollOvershootAnimation.FinishedSignal().Connect(this, &ItemView::OnOvershootOnFinished);
1583 mScrollOvershootAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::OVERSHOOT), overshootAmount, TimePeriod(0.0f, duration) );
1584 mScrollOvershootAnimation.Play();
1586 mAnimatingOvershootOn = animatingOn;
1590 self.SetProperty( Toolkit::ItemView::Property::OVERSHOOT, overshootAmount );
1594 void ItemView::SetItemsParentOrigin( const Vector3& parentOrigin )
1596 if( parentOrigin != mItemsParentOrigin )
1598 mItemsParentOrigin = parentOrigin;
1599 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1601 iter->second.SetParentOrigin(parentOrigin);
1606 Vector3 ItemView::GetItemsParentOrigin() const
1608 return mItemsParentOrigin;
1611 void ItemView::SetItemsAnchorPoint( const Vector3& anchorPoint )
1613 if( anchorPoint != mItemsAnchorPoint )
1615 mItemsAnchorPoint = anchorPoint;
1616 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1618 iter->second.SetAnchorPoint(anchorPoint);
1623 Vector3 ItemView::GetItemsAnchorPoint() const
1625 return mItemsAnchorPoint;
1628 void ItemView::GetItemsRange(ItemRange& range)
1630 if( !mItemPool.empty() )
1632 range.begin = mItemPool.begin()->first;
1633 range.end = mItemPool.rbegin()->first + 1;
1642 bool ItemView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1644 Dali::BaseHandle handle( object );
1646 bool connected( true );
1647 Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( handle );
1649 if( 0 == strcmp( signalName.c_str(), LAYOUT_ACTIVATED_SIGNAL ) )
1651 itemView.LayoutActivatedSignal().Connect( tracker, functor );
1655 // signalName does not match any signal
1662 } // namespace Internal
1664 } // namespace Toolkit