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_TYPE_REGISTRATION_END()
58 const float DEFAULT_MINIMUM_SWIPE_SPEED = 1.0f;
59 const float DEFAULT_MINIMUM_SWIPE_DISTANCE = 3.0f;
60 const float DEFAULT_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION = 0.1f;
62 const float DEFAULT_MINIMUM_SWIPE_DURATION = 0.45f;
63 const float DEFAULT_MAXIMUM_SWIPE_DURATION = 2.6f;
65 const float DEFAULT_REFRESH_INTERVAL_LAYOUT_POSITIONS = 20.0f; // 1 updates per 20 items
66 const int WHEEL_EVENT_FINISHED_TIME_OUT = 500; // 0.5 second
68 const float DEFAULT_ANCHORING_DURATION = 1.0f; // 1 second
70 const float MILLISECONDS_PER_SECONDS = 1000.0f;
72 const Vector2 OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE( 720.0f, 42.0f );
73 const float OVERSHOOT_BOUNCE_ACTOR_RESIZE_THRESHOLD = 180.0f;
74 const Vector4 OVERSHOOT_OVERLAY_NINE_PATCH_BORDER(0.0f, 0.0f, 1.0f, 12.0f);
75 const float DEFAULT_KEYBOARD_FOCUS_SCROLL_DURATION = 0.2f;
78 * Local helper to convert pan distance (in actor coordinates) to the layout-specific scrolling direction
80 float CalculateScrollDistance(Vector2 panDistance, Toolkit::ItemLayout& layout)
82 Radian scrollDirection(layout.GetScrollDirection());
84 float cosTheta = cosf(scrollDirection);
85 float sinTheta = sinf(scrollDirection);
87 return panDistance.x * sinTheta + panDistance.y * cosTheta;
90 // Overshoot overlay constraints
91 void OvershootOverlaySizeConstraint( Vector3& current, const PropertyInputContainer& inputs )
93 const Vector2& parentScrollDirection = inputs[0]->GetVector2();
94 const Toolkit::ControlOrientation::Type& layoutOrientation = static_cast<Toolkit::ControlOrientation::Type>(inputs[1]->GetInteger());
95 const Vector3& parentSize = inputs[2]->GetVector3();
97 if(Toolkit::IsVertical(layoutOrientation))
99 current.width = fabsf(parentScrollDirection.y) > Math::MACHINE_EPSILON_1 ? parentSize.x : parentSize.y;
103 current.width = fabsf(parentScrollDirection.x) > Math::MACHINE_EPSILON_1 ? parentSize.y : parentSize.x;
106 current.height = ( current.width > OVERSHOOT_BOUNCE_ACTOR_RESIZE_THRESHOLD ) ? OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE.height : OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE.height*0.5f;
109 void OvershootOverlayRotationConstraint( Quaternion& current, const PropertyInputContainer& inputs )
111 const Vector2& parentScrollDirection = inputs[0]->GetVector2();
112 const Toolkit::ControlOrientation::Type& layoutOrientation = static_cast<Toolkit::ControlOrientation::Type>(inputs[1]->GetInteger());
113 const float parentOvershoot = inputs[2]->GetFloat();
115 float multiplier = 0;
116 if(Toolkit::IsVertical(layoutOrientation))
118 if(fabsf(parentScrollDirection.y) <= Math::MACHINE_EPSILON_1)
120 if( (layoutOrientation == Toolkit::ControlOrientation::Up && parentOvershoot < Math::MACHINE_EPSILON_0)
121 || (layoutOrientation == Toolkit::ControlOrientation::Down && parentOvershoot > Math::MACHINE_EPSILON_0) )
130 else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.y > Math::MACHINE_EPSILON_0)
131 || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.y < Math::MACHINE_EPSILON_0) )
142 if(fabsf(parentScrollDirection.x) <= Math::MACHINE_EPSILON_1)
144 if( (layoutOrientation == Toolkit::ControlOrientation::Left && parentOvershoot > Math::MACHINE_EPSILON_0)
145 ||(layoutOrientation == Toolkit::ControlOrientation::Right && parentOvershoot < Math::MACHINE_EPSILON_0) )
154 else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.x > Math::MACHINE_EPSILON_0)
155 || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.x < Math::MACHINE_EPSILON_0) )
165 current = Quaternion( Radian( multiplier * Math::PI ), Vector3::ZAXIS );
168 void OvershootOverlayPositionConstraint( Vector3& current, const PropertyInputContainer& inputs )
170 const Vector3& parentSize = inputs[0]->GetVector3();
171 const Vector2& parentScrollDirection = inputs[1]->GetVector2();
172 const Toolkit::ControlOrientation::Type& layoutOrientation = static_cast<Toolkit::ControlOrientation::Type>(inputs[2]->GetInteger());
173 const float parentOvershoot = inputs[3]->GetFloat();
175 Vector3 relativeOffset;
177 if(Toolkit::IsVertical(layoutOrientation))
179 if(fabsf(parentScrollDirection.y) <= Math::MACHINE_EPSILON_1)
181 if( (layoutOrientation == Toolkit::ControlOrientation::Up && parentOvershoot < Math::MACHINE_EPSILON_0)
182 || (layoutOrientation == Toolkit::ControlOrientation::Down && parentOvershoot > Math::MACHINE_EPSILON_0) )
184 relativeOffset = Vector3(1.0f, 0.0f, 0.0f);
188 relativeOffset =Vector3(0.0f, 1.0f, 0.0f);
191 else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.y > Math::MACHINE_EPSILON_0)
192 || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.y < Math::MACHINE_EPSILON_0) )
194 relativeOffset = Vector3(0.0f, 0.0f, 0.0f);
198 relativeOffset = Vector3(1.0f, 1.0f, 0.0f);
203 if(fabsf(parentScrollDirection.x) <= Math::MACHINE_EPSILON_1)
205 if( (layoutOrientation == Toolkit::ControlOrientation::Left && parentOvershoot < Math::MACHINE_EPSILON_0)
206 || (layoutOrientation == Toolkit::ControlOrientation::Right && parentOvershoot > Math::MACHINE_EPSILON_0) )
208 relativeOffset = Vector3(0.0f, 0.0f, 0.0f);
212 relativeOffset = Vector3(1.0f, 1.0f, 0.0f);
215 else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.x > Math::MACHINE_EPSILON_0)
216 || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.x < Math::MACHINE_EPSILON_0) )
218 relativeOffset = Vector3(0.0f, 1.0f, 0.0f);
222 relativeOffset = Vector3(1.0f, 0.0f, 0.0f);
226 current = relativeOffset * parentSize;
229 void OvershootOverlayVisibilityConstraint( bool& current, const PropertyInputContainer& inputs )
231 current = inputs[0]->GetBoolean();
234 } // unnamed namespace
245 namespace // unnamed namespace
248 bool FindById( const ItemContainer& items, ItemId id )
250 for( ConstItemIter iter = items.begin(); items.end() != iter; ++iter )
252 if( iter->first == id )
261 } // unnamed namespace
263 Dali::Toolkit::ItemView ItemView::New(ItemFactory& factory)
265 // Create the implementation
266 ItemViewPtr itemView(new ItemView(factory));
268 // Pass ownership to CustomActor via derived handle
269 Dali::Toolkit::ItemView handle(*itemView);
271 // Second-phase init of the implementation
272 // This can only be done after the CustomActor connection has been made...
273 itemView->Initialize();
278 ItemView::ItemView(ItemFactory& factory)
279 : Scrollable( ControlBehaviour( DISABLE_SIZE_NEGOTIATION | REQUIRES_WHEEL_EVENTS | REQUIRES_KEYBOARD_NAVIGATION_SUPPORT ) ),
280 mItemFactory(factory),
282 mAnimatingOvershootOn(false),
283 mAnimateOvershootOff(false),
284 mAnchoringEnabled(false),
285 mAnchoringDuration(DEFAULT_ANCHORING_DURATION),
286 mRefreshIntervalLayoutPositions(0.0f),
287 mRefreshOrderHint(true/*Refresh item 0 first*/),
288 mMinimumSwipeSpeed(DEFAULT_MINIMUM_SWIPE_SPEED),
289 mMinimumSwipeDistance(DEFAULT_MINIMUM_SWIPE_DISTANCE),
290 mWheelScrollDistanceStep(0.0f),
291 mScrollDistance(0.0f),
293 mTotalPanDisplacement(Vector2::ZERO),
294 mScrollOvershoot(0.0f),
296 mGestureState(Gesture::Clear),
298 mRefreshEnabled(true),
299 mItemsParentOrigin( ParentOrigin::CENTER),
300 mItemsAnchorPoint( AnchorPoint::CENTER)
304 void ItemView::OnInitialize()
308 SetOvershootEnabled(true);
310 Vector2 stageSize = Stage::GetCurrent().GetSize();
311 mWheelScrollDistanceStep = stageSize.y * DEFAULT_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION;
313 EnableGestureDetection(Gesture::Type(Gesture::Pan));
315 mWheelEventFinishedTimer = Timer::New( WHEEL_EVENT_FINISHED_TIME_OUT );
316 mWheelEventFinishedTimer.TickSignal().Connect( this, &ItemView::OnWheelEventFinished );
318 SetRefreshInterval(DEFAULT_REFRESH_INTERVAL_LAYOUT_POSITIONS);
321 ItemView::~ItemView()
325 unsigned int ItemView::GetLayoutCount() const
327 return mLayouts.size();
330 void ItemView::AddLayout(ItemLayout& layout)
332 mLayouts.push_back(ItemLayoutPtr(&layout));
335 void ItemView::RemoveLayout(unsigned int layoutIndex)
337 DALI_ASSERT_ALWAYS(layoutIndex < mLayouts.size());
339 if (mActiveLayout == mLayouts[layoutIndex].Get())
341 mActiveLayout = NULL;
344 mLayouts.erase(mLayouts.begin() + layoutIndex);
347 ItemLayoutPtr ItemView::GetLayout(unsigned int layoutIndex) const
349 return mLayouts[layoutIndex];
352 ItemLayoutPtr ItemView::GetActiveLayout() const
354 return ItemLayoutPtr(mActiveLayout);
357 float ItemView::GetCurrentLayoutPosition(unsigned int itemId) const
359 return Self().GetProperty<float>( Toolkit::ItemView::Property::LAYOUT_POSITION ) + static_cast<float>( itemId );
362 void ItemView::ActivateLayout(unsigned int layoutIndex, const Vector3& targetSize, float durationSeconds)
364 DALI_ASSERT_ALWAYS(layoutIndex < mLayouts.size());
366 mRefreshEnabled = false;
370 // The ItemView size should match the active layout size
371 self.SetSize(targetSize);
372 mActiveLayoutTargetSize = targetSize;
374 // Switch to the new layout
375 mActiveLayout = mLayouts[layoutIndex].Get();
377 // Move the items to the new layout positions...
379 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
381 unsigned int itemId = iter->first;
382 Actor actor = iter->second;
384 // Remove constraints from previous layout
385 actor.RemoveConstraints();
388 mActiveLayout->GetItemSize( itemId, targetSize, size );
389 actor.SetSize( size.GetVectorXY() );
391 mActiveLayout->ApplyConstraints(actor, itemId, targetSize, Self() );
394 // Refresh the new layout
395 ItemRange range = GetItemRange(*mActiveLayout, targetSize, GetCurrentLayoutPosition(0), false/* don't reserve extra*/);
396 AddActorsWithinRange( range, targetSize );
398 // Scroll to an appropriate layout position
400 bool scrollAnimationNeeded(false);
401 float firstItemScrollPosition(0.0f);
403 float current = GetCurrentLayoutPosition(0);
404 float minimum = ClampFirstItemPosition(current, targetSize, *mActiveLayout);
406 if (current < minimum)
408 scrollAnimationNeeded = true;
409 firstItemScrollPosition = minimum;
411 else if (mAnchoringEnabled)
413 scrollAnimationNeeded = true;
414 firstItemScrollPosition = mActiveLayout->GetClosestAnchorPosition(current);
417 if (scrollAnimationNeeded)
419 RemoveAnimation(mScrollAnimation);
420 mScrollAnimation = Animation::New(durationSeconds);
421 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, AlphaFunction::EASE_OUT );
422 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnLayoutActivationScrollFinished);
423 mScrollAnimation.Play();
426 AnimateScrollOvershoot(0.0f);
427 mScrollOvershoot = 0.0f;
429 Radian scrollDirection(mActiveLayout->GetScrollDirection());
430 self.SetProperty(Toolkit::ItemView::Property::SCROLL_DIRECTION, Vector2(sinf(scrollDirection), cosf(scrollDirection)));
431 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_ORIENTATION, static_cast<int>(mActiveLayout->GetOrientation()));
432 self.SetProperty(Toolkit::ItemView::Property::SCROLL_SPEED, mScrollSpeed);
434 CalculateDomainSize(targetSize);
437 void ItemView::DeactivateCurrentLayout()
441 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
443 Actor actor = iter->second;
444 actor.RemoveConstraints();
447 mActiveLayout = NULL;
451 void ItemView::OnRefreshNotification(PropertyNotification& source)
453 if(mRefreshEnabled || mScrollAnimation)
455 // Only refresh the cache during normal scrolling
456 DoRefresh(GetCurrentLayoutPosition(0), true);
460 void ItemView::Refresh()
462 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter )
464 ReleaseActor( iter->first, iter->second );
468 DoRefresh(GetCurrentLayoutPosition(0), true);
471 void ItemView::DoRefresh(float currentLayoutPosition, bool cacheExtra)
475 ItemRange range = GetItemRange(*mActiveLayout, mActiveLayoutTargetSize, currentLayoutPosition, cacheExtra/*reserve extra*/);
476 RemoveActorsOutsideRange( range );
477 AddActorsWithinRange( range, Self().GetCurrentSize() );
479 mScrollUpdatedSignal.Emit( Vector2(0.0f, currentLayoutPosition) );
483 void ItemView::SetMinimumSwipeSpeed(float speed)
485 mMinimumSwipeSpeed = speed;
488 float ItemView::GetMinimumSwipeSpeed() const
490 return mMinimumSwipeSpeed;
493 void ItemView::SetMinimumSwipeDistance(float distance)
495 mMinimumSwipeDistance = distance;
498 float ItemView::GetMinimumSwipeDistance() const
500 return mMinimumSwipeDistance;
503 void ItemView::SetWheelScrollDistanceStep(float step)
505 mWheelScrollDistanceStep = step;
508 float ItemView::GetWheelScrollDistanceStep() const
510 return mWheelScrollDistanceStep;
513 void ItemView::SetAnchoring(bool enabled)
515 mAnchoringEnabled = enabled;
518 bool ItemView::GetAnchoring() const
520 return mAnchoringEnabled;
523 void ItemView::SetAnchoringDuration(float durationSeconds)
525 mAnchoringDuration = durationSeconds;
528 float ItemView::GetAnchoringDuration() const
530 return mAnchoringDuration;
533 void ItemView::SetRefreshInterval(float intervalLayoutPositions)
535 if( !Equals(mRefreshIntervalLayoutPositions, intervalLayoutPositions) )
537 mRefreshIntervalLayoutPositions = intervalLayoutPositions;
540 if(mRefreshNotification)
542 self.RemovePropertyNotification(mRefreshNotification);
544 mRefreshNotification = self.AddPropertyNotification( Toolkit::ItemView::Property::LAYOUT_POSITION, StepCondition(mRefreshIntervalLayoutPositions, 0.0f) );
545 mRefreshNotification.NotifySignal().Connect( this, &ItemView::OnRefreshNotification );
549 float ItemView::GetRefreshInterval() const
551 return mRefreshIntervalLayoutPositions;
554 void ItemView::SetRefreshEnabled(bool enabled)
556 mRefreshEnabled = enabled;
559 Actor ItemView::GetItem(unsigned int itemId) const
563 ConstItemPoolIter iter = mItemPool.find( itemId );
564 if( iter != mItemPool.end() )
566 actor = iter->second;
572 unsigned int ItemView::GetItemId( Actor actor ) const
574 unsigned int itemId( 0 );
576 for ( ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter )
578 if( iter->second == actor )
580 itemId = iter->first;
588 void ItemView::InsertItem( Item newItem, float durationSeconds )
591 Vector3 layoutSize = Self().GetCurrentSize();
593 Actor displacedActor;
594 ItemPoolIter afterDisplacedIter = mItemPool.end();
596 ItemPoolIter foundIter = mItemPool.find( newItem.first );
597 if( mItemPool.end() != foundIter )
599 SetupActor( newItem, layoutSize );
600 Self().Add( newItem.second );
602 displacedActor = foundIter->second;
603 foundIter->second = newItem.second;
605 afterDisplacedIter = ++foundIter;
609 // Inserting before the existing item range?
610 ItemPoolIter iter = mItemPool.begin();
611 if( iter != mItemPool.end() &&
612 iter->first > newItem.first )
614 displacedActor = iter->second;
615 mItemPool.erase( iter++ ); // iter is still valid after the erase
617 afterDisplacedIter = iter;
623 // Move the existing actors to make room
624 for( ItemPoolIter iter = afterDisplacedIter; mItemPool.end() != iter; ++iter )
626 Actor temp = iter->second;
627 iter->second = displacedActor;
628 displacedActor = temp;
630 iter->second.RemoveConstraints();
631 mActiveLayout->ApplyConstraints( iter->second, iter->first, layoutSize, Self() );
635 ItemPool::reverse_iterator lastIter = mItemPool.rbegin();
636 if ( lastIter != mItemPool.rend() )
638 ItemId lastId = lastIter->first;
639 Item lastItem( lastId + 1, displacedActor );
640 mItemPool.insert( lastItem );
642 lastItem.second.RemoveConstraints();
643 mActiveLayout->ApplyConstraints( lastItem.second, lastItem.first, layoutSize, Self() );
647 CalculateDomainSize( layoutSize );
649 mAddingItems = false;
652 void ItemView::InsertItems( const ItemContainer& newItems, float durationSeconds )
655 Vector3 layoutSize = Self().GetCurrentSize();
657 // Insert from lowest id to highest
658 std::set<Item> sortedItems;
659 for( ConstItemIter iter = newItems.begin(); newItems.end() != iter; ++iter )
661 sortedItems.insert( *iter );
664 for( std::set<Item>::iterator iter = sortedItems.begin(); sortedItems.end() != iter; ++iter )
666 Self().Add( iter->second );
668 ItemPoolIter foundIter = mItemPool.find( iter->first );
669 if( mItemPool.end() != foundIter )
671 Actor moveMe = foundIter->second;
672 foundIter->second = iter->second;
674 // Move the existing actors to make room
675 for( ItemPoolIter iter = ++foundIter; mItemPool.end() != iter; ++iter )
677 Actor temp = iter->second;
678 iter->second = moveMe;
683 ItemId lastId = mItemPool.rbegin()->first;
684 Item lastItem( lastId + 1, moveMe );
685 mItemPool.insert( lastItem );
689 mItemPool.insert( *iter );
693 // Relayout everything
694 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
697 if( FindById( newItems, iter->first ) )
699 SetupActor( *iter, layoutSize );
703 iter->second.RemoveConstraints();
704 mActiveLayout->ApplyConstraints( iter->second, iter->first, layoutSize, Self() );
708 CalculateDomainSize( layoutSize );
710 mAddingItems = false;
713 void ItemView::RemoveItem( unsigned int itemId, float durationSeconds )
715 bool actorsReordered = RemoveActor( itemId );
716 if( actorsReordered )
718 ReapplyAllConstraints();
724 void ItemView::RemoveItems( const ItemIdContainer& itemIds, float durationSeconds )
726 bool actorsReordered( false );
728 // Remove from highest id to lowest
729 set<ItemId> sortedItems;
730 for( ConstItemIdIter iter = itemIds.begin(); itemIds.end() != iter; ++iter )
732 sortedItems.insert( *iter );
735 for( set<ItemId>::reverse_iterator iter = sortedItems.rbegin(); sortedItems.rend() != iter; ++iter )
737 if( RemoveActor( *iter ) )
739 actorsReordered = true;
743 if( actorsReordered )
745 ReapplyAllConstraints();
751 bool ItemView::RemoveActor(unsigned int itemId)
753 bool reordered( false );
755 ItemPoolIter removeIter = mItemPool.find( itemId );
756 if( removeIter != mItemPool.end() )
758 ReleaseActor(itemId, removeIter->second);
762 // Removing before the existing item range?
763 ItemPoolIter iter = mItemPool.begin();
764 if( iter != mItemPool.end() &&
765 iter->first > itemId )
767 // In order to decrement the first visible item ID
768 mItemPool.insert( Item(iter->first - 1, Actor()) );
770 removeIter = mItemPool.begin();
774 if( removeIter != mItemPool.end() )
778 // Adjust the remaining item IDs, for example if item 2 is removed:
779 // Initial actors: After insert:
780 // ID 1 - ActorA ID 1 - ActorA
781 // ID 2 - ActorB ID 2 - ActorC (previously ID 3)
782 // ID 3 - ActorC ID 3 - ActorB (previously ID 4)
784 for (ItemPoolIter iter = removeIter; iter != mItemPool.end(); ++iter)
786 if( iter->first < mItemPool.rbegin()->first )
788 iter->second = mItemPool[ iter->first + 1 ];
792 mItemPool.erase( iter );
801 void ItemView::ReplaceItem( Item replacementItem, float durationSeconds )
804 Vector3 layoutSize = Self().GetCurrentSize();
806 SetupActor( replacementItem, layoutSize );
807 Self().Add( replacementItem.second );
809 const ItemPoolIter iter = mItemPool.find( replacementItem.first );
810 if( mItemPool.end() != iter )
812 ReleaseActor(iter->first, iter->second);
813 iter->second = replacementItem.second;
817 mItemPool.insert( replacementItem );
820 CalculateDomainSize( layoutSize );
822 mAddingItems = false;
825 void ItemView::ReplaceItems( const ItemContainer& replacementItems, float durationSeconds )
827 for( ConstItemIter iter = replacementItems.begin(); replacementItems.end() != iter; ++iter )
829 ReplaceItem( *iter, durationSeconds );
833 void ItemView::RemoveActorsOutsideRange( ItemRange range )
835 // Remove unwanted actors from the ItemView & ItemPool
836 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); )
838 unsigned int current = iter->first;
840 if( ! range.Within( current ) )
842 ReleaseActor(iter->first, iter->second);
844 mItemPool.erase( iter++ ); // erase invalidates the return value of post-increment; iter remains valid
853 void ItemView::AddActorsWithinRange( ItemRange range, const Vector3& layoutSize )
855 range.end = std::min(mItemFactory.GetNumberOfItems(), range.end);
857 // The order of addition depends on the scroll direction.
858 if (mRefreshOrderHint)
860 for (unsigned int itemId = range.begin; itemId < range.end; ++itemId)
862 AddNewActor( itemId, layoutSize );
867 for (unsigned int itemId = range.end; itemId > range.begin; --itemId)
869 AddNewActor( itemId-1, layoutSize );
873 // Total number of items may change dynamically.
874 // Always recalculate the domain size to reflect that.
875 CalculateDomainSize(Self().GetCurrentSize());
878 void ItemView::AddNewActor( unsigned int itemId, const Vector3& layoutSize )
882 if( mItemPool.end() == mItemPool.find( itemId ) )
884 Actor actor = mItemFactory.NewItem( itemId );
888 Item newItem( itemId, actor );
890 mItemPool.insert( newItem );
892 SetupActor( newItem, layoutSize );
897 mAddingItems = false;
900 void ItemView::SetupActor( Item item, const Vector3& layoutSize )
902 item.second.SetParentOrigin( mItemsParentOrigin );
903 item.second.SetAnchorPoint( mItemsAnchorPoint );
908 mActiveLayout->GetItemSize( item.first, mActiveLayoutTargetSize, size );
909 item.second.SetSize( size.GetVectorXY() );
911 mActiveLayout->ApplyConstraints( item.second, item.first, layoutSize, Self() );
915 void ItemView::ReleaseActor( ItemId item, Actor actor )
917 Self().Remove( actor );
918 mItemFactory.ItemReleased(item, actor);
921 ItemRange ItemView::GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra)
923 unsigned int itemCount = mItemFactory.GetNumberOfItems();
925 ItemRange available(0u, itemCount);
927 ItemRange range = layout.GetItemsWithinArea( layoutPosition, layoutSize );
931 // Add the reserve items for scrolling
932 unsigned int extra = layout.GetReserveItemCount(layoutSize);
933 range.begin = (range.begin >= extra) ? (range.begin - extra) : 0u;
937 return range.Intersection(available);
940 void ItemView::OnChildAdd(Actor& child)
944 // We don't want to do this downcast check for any item added by ItemView itself.
945 Dali::Toolkit::ScrollBar scrollBar = Dali::Toolkit::ScrollBar::DownCast(child);
948 scrollBar.SetScrollPropertySource(Self(),
949 Toolkit::ItemView::Property::LAYOUT_POSITION,
950 Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_Y,
951 Toolkit::Scrollable::Property::SCROLL_POSITION_MAX_Y,
952 Toolkit::ItemView::Property::SCROLL_CONTENT_SIZE);
953 scrollBar.ScrollPositionIntervalReachedSignal().Connect( this, &ItemView::OnScrollPositionChanged );
958 bool ItemView::OnTouchEvent(const TouchEvent& event)
960 // Ignore events with multiple-touch points
961 if (event.GetPointCount() != 1)
966 if (event.GetPoint(0).state == TouchPoint::Down)
968 // Cancel ongoing scrolling etc.
969 mGestureState = Gesture::Clear;
971 mScrollDistance = 0.0f;
973 Self().SetProperty(Toolkit::ItemView::Property::SCROLL_SPEED, mScrollSpeed);
975 mScrollOvershoot = 0.0f;
976 AnimateScrollOvershoot(0.0f);
980 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
983 RemoveAnimation(mScrollAnimation);
986 return true; // consume since we're potentially scrolling
989 bool ItemView::OnWheelEvent(const WheelEvent& event)
991 // Respond the wheel event to scroll
995 const Vector3 layoutSize = Self().GetCurrentSize();
996 float layoutPositionDelta = GetCurrentLayoutPosition(0) - (event.z * mWheelScrollDistanceStep * mActiveLayout->GetScrollSpeedFactor());
997 float firstItemScrollPosition = ClampFirstItemPosition(layoutPositionDelta, layoutSize, *mActiveLayout);
999 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1001 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1002 mRefreshEnabled = true;
1005 if (mWheelEventFinishedTimer.IsRunning())
1007 mWheelEventFinishedTimer.Stop();
1010 mWheelEventFinishedTimer.Start();
1015 bool ItemView::OnWheelEventFinished()
1019 RemoveAnimation(mScrollAnimation);
1021 // No more wheel events coming. Do the anchoring if enabled.
1022 mScrollAnimation = DoAnchoring();
1023 if (mScrollAnimation)
1025 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1026 mScrollAnimation.Play();
1030 mScrollOvershoot = 0.0f;
1031 AnimateScrollOvershoot(0.0f);
1033 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1040 void ItemView::ReapplyAllConstraints()
1042 Vector3 layoutSize = Self().GetCurrentSize();
1044 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1046 unsigned int id = iter->first;
1047 Actor actor = iter->second;
1049 actor.RemoveConstraints();
1050 mActiveLayout->ApplyConstraints(actor, id, layoutSize, Self());
1054 void ItemView::OnItemsRemoved()
1056 CalculateDomainSize(Self().GetCurrentSize());
1058 // Adjust scroll-position after an item is removed
1061 float firstItemScrollPosition = ClampFirstItemPosition(GetCurrentLayoutPosition(0), Self().GetCurrentSize(), *mActiveLayout);
1062 Self().SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1066 float ItemView::ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout)
1068 Actor self = Self();
1069 float minLayoutPosition = layout.GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), targetSize);
1070 float clamppedPosition = std::min(0.0f, std::max(minLayoutPosition, targetPosition));
1071 mScrollOvershoot = targetPosition - clamppedPosition;
1072 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1074 return clamppedPosition;
1077 void ItemView::OnPan( const PanGesture& gesture )
1079 Actor self = Self();
1080 const Vector3 layoutSize = Self().GetCurrentSize();
1082 RemoveAnimation(mScrollAnimation);
1084 // Short-circuit if there is no active layout
1087 mGestureState = Gesture::Clear;
1091 mGestureState = gesture.state;
1093 switch (mGestureState)
1095 case Gesture::Finished:
1098 if (fabsf(mScrollDistance) > mMinimumSwipeDistance &&
1099 mScrollSpeed > mMinimumSwipeSpeed)
1101 float direction = (mScrollDistance < 0.0f) ? -1.0f : 1.0f;
1103 mRefreshOrderHint = true;
1105 float currentLayoutPosition = GetCurrentLayoutPosition(0);
1106 float firstItemScrollPosition = ClampFirstItemPosition(currentLayoutPosition + mScrollSpeed * direction,
1110 if (mAnchoringEnabled)
1112 firstItemScrollPosition = mActiveLayout->GetClosestAnchorPosition(firstItemScrollPosition);
1115 RemoveAnimation(mScrollAnimation);
1117 float flickAnimationDuration = Clamp( mActiveLayout->GetItemFlickAnimationDuration() * std::max(1.0f, fabsf(firstItemScrollPosition - GetCurrentLayoutPosition(0)))
1118 , DEFAULT_MINIMUM_SWIPE_DURATION, DEFAULT_MAXIMUM_SWIPE_DURATION);
1120 mScrollAnimation = Animation::New(flickAnimationDuration);
1121 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION ), firstItemScrollPosition, AlphaFunction::EASE_OUT );
1122 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::SCROLL_SPEED), 0.0f, AlphaFunction::EASE_OUT );
1125 // Check whether it has already scrolled to the end
1126 if(fabs(currentLayoutPosition - firstItemScrollPosition) > Math::MACHINE_EPSILON_0)
1128 AnimateScrollOvershoot(0.0f);
1132 // Anchoring may be triggered when there was no swipe
1133 if (!mScrollAnimation)
1135 mScrollAnimation = DoAnchoring();
1138 // Reset the overshoot if no scroll animation.
1139 if (!mScrollAnimation)
1141 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1143 AnimateScrollOvershoot(0.0f, false);
1148 case Gesture::Started: // Fall through
1150 mTotalPanDisplacement = Vector2::ZERO;
1151 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1152 mRefreshEnabled = true;
1155 case Gesture::Continuing:
1157 mScrollDistance = CalculateScrollDistance(gesture.displacement, *mActiveLayout);
1158 mScrollSpeed = Clamp((gesture.GetSpeed() * gesture.GetSpeed() * mActiveLayout->GetFlickSpeedFactor() * MILLISECONDS_PER_SECONDS), 0.0f, mActiveLayout->GetMaximumSwipeSpeed());
1160 // Refresh order depends on the direction of the scroll; negative is towards the last item.
1161 mRefreshOrderHint = mScrollDistance < 0.0f;
1163 float layoutPositionDelta = GetCurrentLayoutPosition(0) + (mScrollDistance * mActiveLayout->GetScrollSpeedFactor());
1165 float firstItemScrollPosition = ClampFirstItemPosition(layoutPositionDelta, layoutSize, *mActiveLayout);
1167 float currentOvershoot = self.GetProperty<float>(Toolkit::ItemView::Property::OVERSHOOT);
1169 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1171 if( (firstItemScrollPosition >= 0.0f && currentOvershoot < 1.0f) || (firstItemScrollPosition >= mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), layoutSize) && currentOvershoot > -1.0f) )
1173 mTotalPanDisplacement += gesture.displacement;
1176 mScrollOvershoot = CalculateScrollOvershoot();
1177 self.SetProperty( Toolkit::ItemView::Property::OVERSHOOT, mScrollOvershoot );
1181 case Gesture::Cancelled:
1183 mScrollAnimation = DoAnchoring();
1191 if (mScrollAnimation)
1193 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1194 mScrollAnimation.Play();
1198 bool ItemView::OnAccessibilityPan(PanGesture gesture)
1204 Actor ItemView::GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled)
1206 Actor nextFocusActor;
1210 if(!actor || actor == this->Self())
1212 nextFocusActor = GetItem(nextItemID);
1214 else if(actor && actor.GetParent() == this->Self())
1216 int itemID = GetItemId(actor);
1217 nextItemID = mActiveLayout->GetNextFocusItemID(itemID, mItemFactory.GetNumberOfItems(), direction, loopEnabled);
1218 nextFocusActor = GetItem(nextItemID);
1219 if(nextFocusActor == actor)
1221 // need to pass NULL actor back to focus manager
1222 nextFocusActor.Reset();
1223 return nextFocusActor;
1226 float layoutPosition = mActiveLayout->GetClosestAnchorPosition( GetCurrentLayoutPosition(0) );
1227 Vector3 layoutSize = Self().GetCurrentSize();
1230 // likely the current item is not buffered, so not in our item pool, probably best to get first viewable item
1231 ItemRange viewableItems = mActiveLayout->GetItemsWithinArea(layoutPosition, layoutSize);
1232 nextItemID = viewableItems.begin;
1233 nextFocusActor = GetItem(nextItemID);
1236 return nextFocusActor;
1239 void ItemView::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
1241 // only in this function if our chosen focus actor was actually used
1242 if(commitedFocusableActor)
1244 int nextItemID = GetItemId(commitedFocusableActor);
1245 float layoutPosition = GetCurrentLayoutPosition(0);
1246 Vector3 layoutSize = Self().GetCurrentSize();
1248 float scrollTo = mActiveLayout->GetClosestOnScreenLayoutPosition(nextItemID, layoutPosition, layoutSize);
1249 ScrollTo(Vector2(0.0f, scrollTo), DEFAULT_KEYBOARD_FOCUS_SCROLL_DURATION);
1253 Animation ItemView::DoAnchoring()
1255 Animation anchoringAnimation;
1256 Actor self = Self();
1258 if (mActiveLayout && mAnchoringEnabled)
1260 float anchorPosition = mActiveLayout->GetClosestAnchorPosition( GetCurrentLayoutPosition(0) );
1262 anchoringAnimation = Animation::New(mAnchoringDuration);
1263 anchoringAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), anchorPosition, AlphaFunction::EASE_OUT );
1264 anchoringAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::SCROLL_SPEED), 0.0f, AlphaFunction::EASE_OUT );
1267 AnimateScrollOvershoot(0.0f);
1271 return anchoringAnimation;
1274 void ItemView::OnScrollFinished(Animation& source)
1276 Actor self = Self();
1278 RemoveAnimation(mScrollAnimation); // mScrollAnimation is used to query whether we're scrolling
1280 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1282 if(mIsFlicking && fabsf(mScrollOvershoot) > Math::MACHINE_EPSILON_1)
1284 AnimateScrollOvershoot( mScrollOvershoot > 0.0f ? 1.0f : -1.0f, true);
1288 // Reset the overshoot
1289 AnimateScrollOvershoot( 0.0f );
1291 mIsFlicking = false;
1293 mScrollOvershoot = 0.0f;
1296 void ItemView::OnLayoutActivationScrollFinished(Animation& source)
1298 RemoveAnimation(mScrollAnimation);
1299 mRefreshEnabled = true;
1300 DoRefresh(GetCurrentLayoutPosition(0), true);
1303 void ItemView::OnOvershootOnFinished(Animation& animation)
1305 mAnimatingOvershootOn = false;
1306 mScrollOvershootAnimation.FinishedSignal().Disconnect(this, &ItemView::OnOvershootOnFinished);
1307 RemoveAnimation(mScrollOvershootAnimation);
1308 if(mAnimateOvershootOff)
1310 AnimateScrollOvershoot(0.0f);
1314 void ItemView::ScrollToItem(unsigned int itemId, float durationSeconds)
1316 Actor self = Self();
1317 const Vector3 layoutSize = Self().GetCurrentSize();
1318 float firstItemScrollPosition = ClampFirstItemPosition(mActiveLayout->GetItemScrollToPosition(itemId), layoutSize, *mActiveLayout);
1320 if(durationSeconds > 0.0f)
1322 RemoveAnimation(mScrollAnimation);
1323 mScrollAnimation = Animation::New(durationSeconds);
1324 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, AlphaFunction::EASE_OUT );
1325 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1326 mScrollAnimation.Play();
1330 self.SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1331 AnimateScrollOvershoot(0.0f);
1334 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1335 mRefreshEnabled = true;
1338 void ItemView::RemoveAnimation(Animation& animation)
1342 // Cease animating, and reset handle.
1348 void ItemView::CalculateDomainSize(const Vector3& layoutSize)
1350 Actor self = Self();
1352 Vector3 firstItemPosition(Vector3::ZERO);
1353 Vector3 lastItemPosition(Vector3::ZERO);
1357 firstItemPosition = mActiveLayout->GetItemPosition( 0,0,layoutSize );
1359 float minLayoutPosition = mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), layoutSize);
1360 lastItemPosition = mActiveLayout->GetItemPosition( fabs(minLayoutPosition),fabs(minLayoutPosition),layoutSize );
1364 if(IsHorizontal(mActiveLayout->GetOrientation()))
1366 domainSize = fabs(firstItemPosition.x - lastItemPosition.x);
1370 domainSize = fabs(firstItemPosition.y - lastItemPosition.y);
1373 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MIN, Vector2::ZERO);
1374 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1376 self.SetProperty(Toolkit::ItemView::Property::SCROLL_CONTENT_SIZE, domainSize);
1378 bool isLayoutScrollable = IsLayoutScrollable(layoutSize);
1379 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL, isLayoutScrollable);
1380 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL, false);
1384 Vector2 ItemView::GetDomainSize() const
1386 Actor self = Self();
1388 float minScrollPosition = self.GetProperty<float>(Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_Y);
1389 float maxScrollPosition = self.GetProperty<float>(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX_Y);
1391 return Vector2(0.0f, fabs(GetScrollPosition(minScrollPosition, self.GetCurrentSize()) - GetScrollPosition(-maxScrollPosition, self.GetCurrentSize())));
1394 bool ItemView::IsLayoutScrollable(const Vector3& layoutSize)
1396 Actor self = Self();
1398 float currentLayoutPosition = ClampFirstItemPosition( GetCurrentLayoutPosition(0), layoutSize, *mActiveLayout );
1399 float forwardClampedPosition = ClampFirstItemPosition(currentLayoutPosition + 1.0, layoutSize, *mActiveLayout);
1400 float backwardClampedPosition = ClampFirstItemPosition(currentLayoutPosition - 1.0, layoutSize, *mActiveLayout);
1402 return (fabs(forwardClampedPosition - backwardClampedPosition) > Math::MACHINE_EPSILON_0);
1405 float ItemView::GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const
1407 Vector3 firstItemPosition( mActiveLayout->GetItemPosition(0, layoutPosition, layoutSize ) );
1408 return IsHorizontal(mActiveLayout->GetOrientation()) ? firstItemPosition.x: firstItemPosition.y;
1411 Vector2 ItemView::GetCurrentScrollPosition() const
1413 return Vector2(0.0f, GetScrollPosition(GetCurrentLayoutPosition(0), Self().GetCurrentSize()));
1416 void ItemView::AddOverlay(Actor actor)
1421 void ItemView::RemoveOverlay(Actor actor)
1423 Self().Remove(actor);
1426 void ItemView::ScrollTo(const Vector2& position, float duration)
1428 Actor self = Self();
1429 const Vector3 layoutSize = Self().GetCurrentSize();
1431 float firstItemScrollPosition = ClampFirstItemPosition(position.y, layoutSize, *mActiveLayout);
1435 RemoveAnimation(mScrollAnimation);
1436 mScrollAnimation = Animation::New(duration);
1437 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, AlphaFunction::EASE_OUT );
1438 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1439 mScrollAnimation.Play();
1443 self.SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1444 AnimateScrollOvershoot(0.0f);
1447 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1448 mRefreshEnabled = true;
1451 void ItemView::SetOvershootEffectColor( const Vector4& color )
1453 mOvershootEffectColor = color;
1454 if( mOvershootOverlay )
1456 mOvershootOverlay.SetColor( color );
1460 void ItemView::EnableScrollOvershoot( bool enable )
1462 Actor self = Self();
1465 Property::Index effectOvershootPropertyIndex = Property::INVALID_INDEX;
1466 mOvershootOverlay = CreateBouncingEffectActor( effectOvershootPropertyIndex );
1467 mOvershootOverlay.SetColor(mOvershootEffectColor);
1468 mOvershootOverlay.SetParentOrigin(ParentOrigin::TOP_LEFT);
1469 mOvershootOverlay.SetAnchorPoint(AnchorPoint::TOP_LEFT);
1470 mOvershootOverlay.SetDrawMode(DrawMode::OVERLAY);
1471 self.Add(mOvershootOverlay);
1473 Constraint constraint = Constraint::New<Vector3>( mOvershootOverlay, Actor::Property::SIZE, OvershootOverlaySizeConstraint );
1474 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1475 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1476 constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
1479 mOvershootOverlay.SetSize(OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE.width, OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE.height);
1481 constraint = Constraint::New<Quaternion>( mOvershootOverlay, Actor::Property::ORIENTATION, OvershootOverlayRotationConstraint );
1482 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1483 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1484 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1487 constraint = Constraint::New<Vector3>( mOvershootOverlay, Actor::Property::POSITION, OvershootOverlayPositionConstraint );
1488 constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
1489 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1490 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1491 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1494 constraint = Constraint::New<bool>( mOvershootOverlay, Actor::Property::VISIBLE, OvershootOverlayVisibilityConstraint );
1495 constraint.AddSource( ParentSource( Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL ) );
1498 constraint = Constraint::New<float>( mOvershootOverlay, effectOvershootPropertyIndex, EqualToConstraint() );
1499 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1504 if( mOvershootOverlay )
1506 self.Remove(mOvershootOverlay);
1507 mOvershootOverlay.Reset();
1512 float ItemView::CalculateScrollOvershoot()
1514 float overshoot = 0.0f;
1518 // The overshoot must be calculated from the accumulated pan gesture displacement
1519 // since the pan gesture starts.
1520 Actor self = Self();
1521 float scrollDistance = CalculateScrollDistance(mTotalPanDisplacement, *mActiveLayout) * mActiveLayout->GetScrollSpeedFactor();
1522 float positionDelta = GetCurrentLayoutPosition(0) + scrollDistance;
1523 float minLayoutPosition = mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), Self().GetCurrentSize());
1524 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1525 float clamppedPosition = std::min(0.0f, std::max(minLayoutPosition, positionDelta));
1526 overshoot = positionDelta - clamppedPosition;
1529 return overshoot > 0.0f ? std::min(overshoot, 1.0f) : std::max(overshoot, -1.0f);
1532 void ItemView::AnimateScrollOvershoot(float overshootAmount, bool animateBack)
1534 bool animatingOn = fabsf(overshootAmount) > Math::MACHINE_EPSILON_1;
1536 // make sure we animate back if needed
1537 mAnimateOvershootOff = animateBack || (!animatingOn && mAnimatingOvershootOn);
1539 if( mAnimatingOvershootOn )
1541 // animating on, do not allow animate off
1545 Actor self = Self();
1547 if(mOvershootAnimationSpeed > Math::MACHINE_EPSILON_0)
1549 float currentOvershoot = self.GetProperty<float>(Toolkit::ItemView::Property::OVERSHOOT);
1550 float duration = 0.0f;
1552 if (mOvershootOverlay)
1554 duration = mOvershootOverlay.GetCurrentSize().height * (animatingOn ? (1.0f - fabsf(currentOvershoot)) : fabsf(currentOvershoot)) / mOvershootAnimationSpeed;
1557 RemoveAnimation(mScrollOvershootAnimation);
1558 mScrollOvershootAnimation = Animation::New(duration);
1559 mScrollOvershootAnimation.FinishedSignal().Connect(this, &ItemView::OnOvershootOnFinished);
1560 mScrollOvershootAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::OVERSHOOT), overshootAmount, TimePeriod(0.0f, duration) );
1561 mScrollOvershootAnimation.Play();
1563 mAnimatingOvershootOn = animatingOn;
1567 self.SetProperty( Toolkit::ItemView::Property::OVERSHOOT, overshootAmount );
1571 void ItemView::SetItemsParentOrigin( const Vector3& parentOrigin )
1573 if( parentOrigin != mItemsParentOrigin )
1575 mItemsParentOrigin = parentOrigin;
1576 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1578 iter->second.SetParentOrigin(parentOrigin);
1583 Vector3 ItemView::GetItemsParentOrigin() const
1585 return mItemsParentOrigin;
1588 void ItemView::SetItemsAnchorPoint( const Vector3& anchorPoint )
1590 if( anchorPoint != mItemsAnchorPoint )
1592 mItemsAnchorPoint = anchorPoint;
1593 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1595 iter->second.SetAnchorPoint(anchorPoint);
1600 Vector3 ItemView::GetItemsAnchorPoint() const
1602 return mItemsAnchorPoint;
1605 void ItemView::GetItemsRange(ItemRange& range)
1607 if( !mItemPool.empty() )
1609 range.begin = mItemPool.begin()->first;
1610 range.end = mItemPool.rbegin()->first + 1;
1619 void ItemView::OnScrollPositionChanged( float position )
1621 // Cancel scroll animation to prevent any fighting of setting the scroll position property.
1622 RemoveAnimation(mScrollAnimation);
1624 // Refresh the cache immediately when the scroll position is changed.
1625 DoRefresh(position, false); // No need to cache extra items.
1628 } // namespace Internal
1630 } // namespace Toolkit