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 Vector2 OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE( 720.0f, 42.0f );
75 const float OVERSHOOT_BOUNCE_ACTOR_RESIZE_THRESHOLD = 180.0f;
76 const Vector4 OVERSHOOT_OVERLAY_NINE_PATCH_BORDER(0.0f, 0.0f, 1.0f, 12.0f);
77 const float DEFAULT_KEYBOARD_FOCUS_SCROLL_DURATION = 0.2f;
80 * Local helper to convert pan distance (in actor coordinates) to the layout-specific scrolling direction
82 float CalculateScrollDistance(Vector2 panDistance, Toolkit::ItemLayout& layout)
84 Radian scrollDirection(layout.GetScrollDirection());
86 float cosTheta = cosf(scrollDirection);
87 float sinTheta = sinf(scrollDirection);
89 return panDistance.x * sinTheta + panDistance.y * cosTheta;
92 // Overshoot overlay constraints
93 void OvershootOverlaySizeConstraint( Vector3& current, const PropertyInputContainer& inputs )
95 const Vector2& parentScrollDirection = inputs[0]->GetVector2();
96 const Toolkit::ControlOrientation::Type& layoutOrientation = static_cast<Toolkit::ControlOrientation::Type>(inputs[1]->GetInteger());
97 const Vector3& parentSize = inputs[2]->GetVector3();
99 if(Toolkit::IsVertical(layoutOrientation))
101 current.width = fabsf(parentScrollDirection.y) > Math::MACHINE_EPSILON_1 ? parentSize.x : parentSize.y;
105 current.width = fabsf(parentScrollDirection.x) > Math::MACHINE_EPSILON_1 ? parentSize.y : parentSize.x;
108 current.height = ( current.width > OVERSHOOT_BOUNCE_ACTOR_RESIZE_THRESHOLD ) ? OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE.height : OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE.height*0.5f;
111 void OvershootOverlayRotationConstraint( Quaternion& current, const PropertyInputContainer& inputs )
113 const Vector2& parentScrollDirection = inputs[0]->GetVector2();
114 const Toolkit::ControlOrientation::Type& layoutOrientation = static_cast<Toolkit::ControlOrientation::Type>(inputs[1]->GetInteger());
115 const float parentOvershoot = inputs[2]->GetFloat();
117 float multiplier = 0;
118 if(Toolkit::IsVertical(layoutOrientation))
120 if(fabsf(parentScrollDirection.y) <= Math::MACHINE_EPSILON_1)
122 if( (layoutOrientation == Toolkit::ControlOrientation::Up && parentOvershoot < Math::MACHINE_EPSILON_0)
123 || (layoutOrientation == Toolkit::ControlOrientation::Down && parentOvershoot > Math::MACHINE_EPSILON_0) )
132 else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.y > Math::MACHINE_EPSILON_0)
133 || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.y < Math::MACHINE_EPSILON_0) )
144 if(fabsf(parentScrollDirection.x) <= Math::MACHINE_EPSILON_1)
146 if( (layoutOrientation == Toolkit::ControlOrientation::Left && parentOvershoot > Math::MACHINE_EPSILON_0)
147 ||(layoutOrientation == Toolkit::ControlOrientation::Right && parentOvershoot < Math::MACHINE_EPSILON_0) )
156 else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.x > Math::MACHINE_EPSILON_0)
157 || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.x < Math::MACHINE_EPSILON_0) )
167 current = Quaternion( Radian( multiplier * Math::PI ), Vector3::ZAXIS );
170 void OvershootOverlayPositionConstraint( Vector3& current, const PropertyInputContainer& inputs )
172 const Vector3& parentSize = inputs[0]->GetVector3();
173 const Vector2& parentScrollDirection = inputs[1]->GetVector2();
174 const Toolkit::ControlOrientation::Type& layoutOrientation = static_cast<Toolkit::ControlOrientation::Type>(inputs[2]->GetInteger());
175 const float parentOvershoot = inputs[3]->GetFloat();
177 Vector3 relativeOffset;
179 if(Toolkit::IsVertical(layoutOrientation))
181 if(fabsf(parentScrollDirection.y) <= Math::MACHINE_EPSILON_1)
183 if( (layoutOrientation == Toolkit::ControlOrientation::Up && parentOvershoot < Math::MACHINE_EPSILON_0)
184 || (layoutOrientation == Toolkit::ControlOrientation::Down && parentOvershoot > Math::MACHINE_EPSILON_0) )
186 relativeOffset = Vector3(1.0f, 0.0f, 0.0f);
190 relativeOffset =Vector3(0.0f, 1.0f, 0.0f);
193 else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.y > Math::MACHINE_EPSILON_0)
194 || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.y < Math::MACHINE_EPSILON_0) )
196 relativeOffset = Vector3(0.0f, 0.0f, 0.0f);
200 relativeOffset = Vector3(1.0f, 1.0f, 0.0f);
205 if(fabsf(parentScrollDirection.x) <= Math::MACHINE_EPSILON_1)
207 if( (layoutOrientation == Toolkit::ControlOrientation::Left && parentOvershoot < Math::MACHINE_EPSILON_0)
208 || (layoutOrientation == Toolkit::ControlOrientation::Right && parentOvershoot > Math::MACHINE_EPSILON_0) )
210 relativeOffset = Vector3(0.0f, 0.0f, 0.0f);
214 relativeOffset = Vector3(1.0f, 1.0f, 0.0f);
217 else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.x > Math::MACHINE_EPSILON_0)
218 || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.x < Math::MACHINE_EPSILON_0) )
220 relativeOffset = Vector3(0.0f, 1.0f, 0.0f);
224 relativeOffset = Vector3(1.0f, 0.0f, 0.0f);
228 current = relativeOffset * parentSize;
231 void OvershootOverlayVisibilityConstraint( bool& current, const PropertyInputContainer& inputs )
233 current = inputs[0]->GetBoolean();
236 } // unnamed namespace
247 namespace // unnamed namespace
250 bool FindById( const ItemContainer& items, ItemId id )
252 for( ConstItemIter iter = items.begin(); items.end() != iter; ++iter )
254 if( iter->first == id )
263 } // unnamed namespace
265 Dali::Toolkit::ItemView ItemView::New(ItemFactory& factory)
267 // Create the implementation
268 ItemViewPtr itemView(new ItemView(factory));
270 // Pass ownership to CustomActor via derived handle
271 Dali::Toolkit::ItemView handle(*itemView);
273 // Second-phase init of the implementation
274 // This can only be done after the CustomActor connection has been made...
275 itemView->Initialize();
280 ItemView::ItemView(ItemFactory& factory)
281 : Scrollable( ControlBehaviour( DISABLE_SIZE_NEGOTIATION | REQUIRES_WHEEL_EVENTS | REQUIRES_KEYBOARD_NAVIGATION_SUPPORT ) ),
282 mItemFactory(factory),
284 mAnimatingOvershootOn(false),
285 mAnimateOvershootOff(false),
286 mAnchoringEnabled(false),
287 mAnchoringDuration(DEFAULT_ANCHORING_DURATION),
288 mRefreshIntervalLayoutPositions(0.0f),
289 mRefreshOrderHint(true/*Refresh item 0 first*/),
290 mMinimumSwipeSpeed(DEFAULT_MINIMUM_SWIPE_SPEED),
291 mMinimumSwipeDistance(DEFAULT_MINIMUM_SWIPE_DISTANCE),
292 mWheelScrollDistanceStep(0.0f),
293 mScrollDistance(0.0f),
295 mTotalPanDisplacement(Vector2::ZERO),
296 mScrollOvershoot(0.0f),
298 mGestureState(Gesture::Clear),
300 mRefreshEnabled(true),
301 mItemsParentOrigin( ParentOrigin::CENTER),
302 mItemsAnchorPoint( AnchorPoint::CENTER)
306 void ItemView::OnInitialize()
310 SetOvershootEnabled(true);
312 Vector2 stageSize = Stage::GetCurrent().GetSize();
313 mWheelScrollDistanceStep = stageSize.y * DEFAULT_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION;
315 EnableGestureDetection(Gesture::Type(Gesture::Pan));
317 mWheelEventFinishedTimer = Timer::New( WHEEL_EVENT_FINISHED_TIME_OUT );
318 mWheelEventFinishedTimer.TickSignal().Connect( this, &ItemView::OnWheelEventFinished );
320 SetRefreshInterval(DEFAULT_REFRESH_INTERVAL_LAYOUT_POSITIONS);
323 ItemView::~ItemView()
327 unsigned int ItemView::GetLayoutCount() const
329 return mLayouts.size();
332 void ItemView::AddLayout(ItemLayout& layout)
334 mLayouts.push_back(ItemLayoutPtr(&layout));
337 void ItemView::RemoveLayout(unsigned int layoutIndex)
339 DALI_ASSERT_ALWAYS(layoutIndex < mLayouts.size());
341 if (mActiveLayout == mLayouts[layoutIndex].Get())
343 mActiveLayout = NULL;
346 mLayouts.erase(mLayouts.begin() + layoutIndex);
349 ItemLayoutPtr ItemView::GetLayout(unsigned int layoutIndex) const
351 return mLayouts[layoutIndex];
354 ItemLayoutPtr ItemView::GetActiveLayout() const
356 return ItemLayoutPtr(mActiveLayout);
359 float ItemView::GetCurrentLayoutPosition(unsigned int itemId) const
361 return Self().GetProperty<float>( Toolkit::ItemView::Property::LAYOUT_POSITION ) + static_cast<float>( itemId );
364 void ItemView::ActivateLayout(unsigned int layoutIndex, const Vector3& targetSize, float durationSeconds)
366 DALI_ASSERT_ALWAYS(layoutIndex < mLayouts.size());
368 mRefreshEnabled = false;
372 // The ItemView size should match the active layout size
373 self.SetSize(targetSize);
374 mActiveLayoutTargetSize = targetSize;
376 // Switch to the new layout
377 mActiveLayout = mLayouts[layoutIndex].Get();
379 // Move the items to the new layout positions...
381 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
383 unsigned int itemId = iter->first;
384 Actor actor = iter->second;
386 // Remove constraints from previous layout
387 actor.RemoveConstraints();
390 mActiveLayout->GetItemSize( itemId, targetSize, size );
391 actor.SetSize( size.GetVectorXY() );
393 mActiveLayout->ApplyConstraints(actor, itemId, targetSize, Self() );
396 // Refresh the new layout
397 ItemRange range = GetItemRange(*mActiveLayout, targetSize, GetCurrentLayoutPosition(0), false/* don't reserve extra*/);
398 AddActorsWithinRange( range, targetSize );
400 // Scroll to an appropriate layout position
402 bool scrollAnimationNeeded(false);
403 float firstItemScrollPosition(0.0f);
405 float current = GetCurrentLayoutPosition(0);
406 float minimum = ClampFirstItemPosition(current, targetSize, *mActiveLayout);
408 if (current < minimum)
410 scrollAnimationNeeded = true;
411 firstItemScrollPosition = minimum;
413 else if (mAnchoringEnabled)
415 scrollAnimationNeeded = true;
416 firstItemScrollPosition = mActiveLayout->GetClosestAnchorPosition(current);
419 if (scrollAnimationNeeded)
421 RemoveAnimation(mScrollAnimation);
422 mScrollAnimation = Animation::New(durationSeconds);
423 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, AlphaFunction::EASE_OUT );
424 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnLayoutActivationScrollFinished);
425 mScrollAnimation.Play();
429 // Emit the layout activated signal
430 mLayoutActivatedSignal.Emit();
433 AnimateScrollOvershoot(0.0f);
434 mScrollOvershoot = 0.0f;
436 Radian scrollDirection(mActiveLayout->GetScrollDirection());
437 self.SetProperty(Toolkit::ItemView::Property::SCROLL_DIRECTION, Vector2(sinf(scrollDirection), cosf(scrollDirection)));
438 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_ORIENTATION, static_cast<int>(mActiveLayout->GetOrientation()));
439 self.SetProperty(Toolkit::ItemView::Property::SCROLL_SPEED, mScrollSpeed);
441 CalculateDomainSize(targetSize);
444 void ItemView::DeactivateCurrentLayout()
448 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
450 Actor actor = iter->second;
451 actor.RemoveConstraints();
454 mActiveLayout = NULL;
458 void ItemView::OnRefreshNotification(PropertyNotification& source)
460 if(mRefreshEnabled || mScrollAnimation)
462 // Only refresh the cache during normal scrolling
463 DoRefresh(GetCurrentLayoutPosition(0), true);
467 void ItemView::Refresh()
469 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter )
471 ReleaseActor( iter->first, iter->second );
475 DoRefresh(GetCurrentLayoutPosition(0), true);
478 void ItemView::DoRefresh(float currentLayoutPosition, bool cacheExtra)
482 ItemRange range = GetItemRange(*mActiveLayout, mActiveLayoutTargetSize, currentLayoutPosition, cacheExtra/*reserve extra*/);
483 RemoveActorsOutsideRange( range );
484 AddActorsWithinRange( range, Self().GetCurrentSize() );
486 mScrollUpdatedSignal.Emit( Vector2(0.0f, currentLayoutPosition) );
490 void ItemView::SetMinimumSwipeSpeed(float speed)
492 mMinimumSwipeSpeed = speed;
495 float ItemView::GetMinimumSwipeSpeed() const
497 return mMinimumSwipeSpeed;
500 void ItemView::SetMinimumSwipeDistance(float distance)
502 mMinimumSwipeDistance = distance;
505 float ItemView::GetMinimumSwipeDistance() const
507 return mMinimumSwipeDistance;
510 void ItemView::SetWheelScrollDistanceStep(float step)
512 mWheelScrollDistanceStep = step;
515 float ItemView::GetWheelScrollDistanceStep() const
517 return mWheelScrollDistanceStep;
520 void ItemView::SetAnchoring(bool enabled)
522 mAnchoringEnabled = enabled;
525 bool ItemView::GetAnchoring() const
527 return mAnchoringEnabled;
530 void ItemView::SetAnchoringDuration(float durationSeconds)
532 mAnchoringDuration = durationSeconds;
535 float ItemView::GetAnchoringDuration() const
537 return mAnchoringDuration;
540 void ItemView::SetRefreshInterval(float intervalLayoutPositions)
542 if( !Equals(mRefreshIntervalLayoutPositions, intervalLayoutPositions) )
544 mRefreshIntervalLayoutPositions = intervalLayoutPositions;
547 if(mRefreshNotification)
549 self.RemovePropertyNotification(mRefreshNotification);
551 mRefreshNotification = self.AddPropertyNotification( Toolkit::ItemView::Property::LAYOUT_POSITION, StepCondition(mRefreshIntervalLayoutPositions, 0.0f) );
552 mRefreshNotification.NotifySignal().Connect( this, &ItemView::OnRefreshNotification );
556 float ItemView::GetRefreshInterval() const
558 return mRefreshIntervalLayoutPositions;
561 void ItemView::SetRefreshEnabled(bool enabled)
563 mRefreshEnabled = enabled;
566 Actor ItemView::GetItem(unsigned int itemId) const
570 ConstItemPoolIter iter = mItemPool.find( itemId );
571 if( iter != mItemPool.end() )
573 actor = iter->second;
579 unsigned int ItemView::GetItemId( Actor actor ) const
581 unsigned int itemId( 0 );
583 for ( ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter )
585 if( iter->second == actor )
587 itemId = iter->first;
595 void ItemView::InsertItem( Item newItem, float durationSeconds )
598 Vector3 layoutSize = Self().GetCurrentSize();
600 Actor displacedActor;
601 ItemPoolIter afterDisplacedIter = mItemPool.end();
603 ItemPoolIter foundIter = mItemPool.find( newItem.first );
604 if( mItemPool.end() != foundIter )
606 SetupActor( newItem, layoutSize );
607 Self().Add( newItem.second );
609 displacedActor = foundIter->second;
610 foundIter->second = newItem.second;
612 afterDisplacedIter = ++foundIter;
616 // Inserting before the existing item range?
617 ItemPoolIter iter = mItemPool.begin();
618 if( iter != mItemPool.end() &&
619 iter->first > newItem.first )
621 displacedActor = iter->second;
622 mItemPool.erase( iter++ ); // iter is still valid after the erase
624 afterDisplacedIter = iter;
630 // Move the existing actors to make room
631 for( ItemPoolIter iter = afterDisplacedIter; mItemPool.end() != iter; ++iter )
633 Actor temp = iter->second;
634 iter->second = displacedActor;
635 displacedActor = temp;
637 iter->second.RemoveConstraints();
638 mActiveLayout->ApplyConstraints( iter->second, iter->first, layoutSize, Self() );
642 ItemPool::reverse_iterator lastIter = mItemPool.rbegin();
643 if ( lastIter != mItemPool.rend() )
645 ItemId lastId = lastIter->first;
646 Item lastItem( lastId + 1, displacedActor );
647 mItemPool.insert( lastItem );
649 lastItem.second.RemoveConstraints();
650 mActiveLayout->ApplyConstraints( lastItem.second, lastItem.first, layoutSize, Self() );
654 CalculateDomainSize( layoutSize );
656 mAddingItems = false;
659 void ItemView::InsertItems( const ItemContainer& newItems, float durationSeconds )
662 Vector3 layoutSize = Self().GetCurrentSize();
664 // Insert from lowest id to highest
665 std::set<Item> sortedItems;
666 for( ConstItemIter iter = newItems.begin(); newItems.end() != iter; ++iter )
668 sortedItems.insert( *iter );
671 for( std::set<Item>::iterator iter = sortedItems.begin(); sortedItems.end() != iter; ++iter )
673 Self().Add( iter->second );
675 ItemPoolIter foundIter = mItemPool.find( iter->first );
676 if( mItemPool.end() != foundIter )
678 Actor moveMe = foundIter->second;
679 foundIter->second = iter->second;
681 // Move the existing actors to make room
682 for( ItemPoolIter iter = ++foundIter; mItemPool.end() != iter; ++iter )
684 Actor temp = iter->second;
685 iter->second = moveMe;
690 ItemId lastId = mItemPool.rbegin()->first;
691 Item lastItem( lastId + 1, moveMe );
692 mItemPool.insert( lastItem );
696 mItemPool.insert( *iter );
700 // Relayout everything
701 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
704 if( FindById( newItems, iter->first ) )
706 SetupActor( *iter, layoutSize );
710 iter->second.RemoveConstraints();
711 mActiveLayout->ApplyConstraints( iter->second, iter->first, layoutSize, Self() );
715 CalculateDomainSize( layoutSize );
717 mAddingItems = false;
720 void ItemView::RemoveItem( unsigned int itemId, float durationSeconds )
722 bool actorsReordered = RemoveActor( itemId );
723 if( actorsReordered )
725 ReapplyAllConstraints();
731 void ItemView::RemoveItems( const ItemIdContainer& itemIds, float durationSeconds )
733 bool actorsReordered( false );
735 // Remove from highest id to lowest
736 set<ItemId> sortedItems;
737 for( ConstItemIdIter iter = itemIds.begin(); itemIds.end() != iter; ++iter )
739 sortedItems.insert( *iter );
742 for( set<ItemId>::reverse_iterator iter = sortedItems.rbegin(); sortedItems.rend() != iter; ++iter )
744 if( RemoveActor( *iter ) )
746 actorsReordered = true;
750 if( actorsReordered )
752 ReapplyAllConstraints();
758 bool ItemView::RemoveActor(unsigned int itemId)
760 bool reordered( false );
762 ItemPoolIter removeIter = mItemPool.find( itemId );
763 if( removeIter != mItemPool.end() )
765 ReleaseActor(itemId, removeIter->second);
769 // Removing before the existing item range?
770 ItemPoolIter iter = mItemPool.begin();
771 if( iter != mItemPool.end() &&
772 iter->first > itemId )
774 // In order to decrement the first visible item ID
775 mItemPool.insert( Item(iter->first - 1, Actor()) );
777 removeIter = mItemPool.begin();
781 if( removeIter != mItemPool.end() )
785 // Adjust the remaining item IDs, for example if item 2 is removed:
786 // Initial actors: After insert:
787 // ID 1 - ActorA ID 1 - ActorA
788 // ID 2 - ActorB ID 2 - ActorC (previously ID 3)
789 // ID 3 - ActorC ID 3 - ActorB (previously ID 4)
791 for (ItemPoolIter iter = removeIter; iter != mItemPool.end(); ++iter)
793 if( iter->first < mItemPool.rbegin()->first )
795 iter->second = mItemPool[ iter->first + 1 ];
799 mItemPool.erase( iter );
808 void ItemView::ReplaceItem( Item replacementItem, float durationSeconds )
811 Vector3 layoutSize = Self().GetCurrentSize();
813 SetupActor( replacementItem, layoutSize );
814 Self().Add( replacementItem.second );
816 const ItemPoolIter iter = mItemPool.find( replacementItem.first );
817 if( mItemPool.end() != iter )
819 ReleaseActor(iter->first, iter->second);
820 iter->second = replacementItem.second;
824 mItemPool.insert( replacementItem );
827 CalculateDomainSize( layoutSize );
829 mAddingItems = false;
832 void ItemView::ReplaceItems( const ItemContainer& replacementItems, float durationSeconds )
834 for( ConstItemIter iter = replacementItems.begin(); replacementItems.end() != iter; ++iter )
836 ReplaceItem( *iter, durationSeconds );
840 void ItemView::RemoveActorsOutsideRange( ItemRange range )
842 // Remove unwanted actors from the ItemView & ItemPool
843 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); )
845 unsigned int current = iter->first;
847 if( ! range.Within( current ) )
849 ReleaseActor(iter->first, iter->second);
851 mItemPool.erase( iter++ ); // erase invalidates the return value of post-increment; iter remains valid
860 void ItemView::AddActorsWithinRange( ItemRange range, const Vector3& layoutSize )
862 range.end = std::min(mItemFactory.GetNumberOfItems(), range.end);
864 // The order of addition depends on the scroll direction.
865 if (mRefreshOrderHint)
867 for (unsigned int itemId = range.begin; itemId < range.end; ++itemId)
869 AddNewActor( itemId, layoutSize );
874 for (unsigned int itemId = range.end; itemId > range.begin; --itemId)
876 AddNewActor( itemId-1, layoutSize );
880 // Total number of items may change dynamically.
881 // Always recalculate the domain size to reflect that.
882 CalculateDomainSize(Self().GetCurrentSize());
885 void ItemView::AddNewActor( unsigned int itemId, const Vector3& layoutSize )
889 if( mItemPool.end() == mItemPool.find( itemId ) )
891 Actor actor = mItemFactory.NewItem( itemId );
895 Item newItem( itemId, actor );
897 mItemPool.insert( newItem );
899 SetupActor( newItem, layoutSize );
904 mAddingItems = false;
907 void ItemView::SetupActor( Item item, const Vector3& layoutSize )
909 item.second.SetParentOrigin( mItemsParentOrigin );
910 item.second.SetAnchorPoint( mItemsAnchorPoint );
915 mActiveLayout->GetItemSize( item.first, mActiveLayoutTargetSize, size );
916 item.second.SetSize( size.GetVectorXY() );
918 mActiveLayout->ApplyConstraints( item.second, item.first, layoutSize, Self() );
922 void ItemView::ReleaseActor( ItemId item, Actor actor )
924 Self().Remove( actor );
925 mItemFactory.ItemReleased(item, actor);
928 ItemRange ItemView::GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra)
930 unsigned int itemCount = mItemFactory.GetNumberOfItems();
932 ItemRange available(0u, itemCount);
934 ItemRange range = layout.GetItemsWithinArea( layoutPosition, layoutSize );
938 // Add the reserve items for scrolling
939 unsigned int extra = layout.GetReserveItemCount(layoutSize);
940 range.begin = (range.begin >= extra) ? (range.begin - extra) : 0u;
944 return range.Intersection(available);
947 void ItemView::OnChildAdd(Actor& child)
951 // We don't want to do this downcast check for any item added by ItemView itself.
952 Dali::Toolkit::ScrollBar scrollBar = Dali::Toolkit::ScrollBar::DownCast(child);
955 scrollBar.SetScrollPropertySource(Self(),
956 Toolkit::ItemView::Property::LAYOUT_POSITION,
957 Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_Y,
958 Toolkit::Scrollable::Property::SCROLL_POSITION_MAX_Y,
959 Toolkit::ItemView::Property::SCROLL_CONTENT_SIZE);
960 scrollBar.ScrollPositionIntervalReachedSignal().Connect( this, &ItemView::OnScrollPositionChanged );
965 bool ItemView::OnTouchEvent(const TouchEvent& event)
967 // Ignore events with multiple-touch points
968 if (event.GetPointCount() != 1)
973 if (event.GetPoint(0).state == TouchPoint::Down)
975 // Cancel ongoing scrolling etc.
976 mGestureState = Gesture::Clear;
978 mScrollDistance = 0.0f;
980 Self().SetProperty(Toolkit::ItemView::Property::SCROLL_SPEED, mScrollSpeed);
982 mScrollOvershoot = 0.0f;
983 AnimateScrollOvershoot(0.0f);
987 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
990 RemoveAnimation(mScrollAnimation);
993 return true; // consume since we're potentially scrolling
996 bool ItemView::OnWheelEvent(const WheelEvent& event)
998 // Respond the wheel event to scroll
1001 Actor self = Self();
1002 const Vector3 layoutSize = Self().GetCurrentSize();
1003 float layoutPositionDelta = GetCurrentLayoutPosition(0) - (event.z * mWheelScrollDistanceStep * mActiveLayout->GetScrollSpeedFactor());
1004 float firstItemScrollPosition = ClampFirstItemPosition(layoutPositionDelta, layoutSize, *mActiveLayout);
1006 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1008 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1009 mRefreshEnabled = true;
1012 if (mWheelEventFinishedTimer.IsRunning())
1014 mWheelEventFinishedTimer.Stop();
1017 mWheelEventFinishedTimer.Start();
1022 bool ItemView::OnWheelEventFinished()
1026 RemoveAnimation(mScrollAnimation);
1028 // No more wheel events coming. Do the anchoring if enabled.
1029 mScrollAnimation = DoAnchoring();
1030 if (mScrollAnimation)
1032 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1033 mScrollAnimation.Play();
1037 mScrollOvershoot = 0.0f;
1038 AnimateScrollOvershoot(0.0f);
1040 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1047 void ItemView::ReapplyAllConstraints()
1049 Vector3 layoutSize = Self().GetCurrentSize();
1051 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1053 unsigned int id = iter->first;
1054 Actor actor = iter->second;
1056 actor.RemoveConstraints();
1057 mActiveLayout->ApplyConstraints(actor, id, layoutSize, Self());
1061 void ItemView::OnItemsRemoved()
1063 CalculateDomainSize(Self().GetCurrentSize());
1065 // Adjust scroll-position after an item is removed
1068 float firstItemScrollPosition = ClampFirstItemPosition(GetCurrentLayoutPosition(0), Self().GetCurrentSize(), *mActiveLayout);
1069 Self().SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1073 float ItemView::ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout)
1075 Actor self = Self();
1076 float minLayoutPosition = layout.GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), targetSize);
1077 float clamppedPosition = std::min(0.0f, std::max(minLayoutPosition, targetPosition));
1078 mScrollOvershoot = targetPosition - clamppedPosition;
1079 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1081 return clamppedPosition;
1084 void ItemView::OnPan( const PanGesture& gesture )
1086 Actor self = Self();
1087 const Vector3 layoutSize = Self().GetCurrentSize();
1089 RemoveAnimation(mScrollAnimation);
1091 // Short-circuit if there is no active layout
1094 mGestureState = Gesture::Clear;
1098 mGestureState = gesture.state;
1100 switch (mGestureState)
1102 case Gesture::Finished:
1105 if (fabsf(mScrollDistance) > mMinimumSwipeDistance &&
1106 mScrollSpeed > mMinimumSwipeSpeed)
1108 float direction = (mScrollDistance < 0.0f) ? -1.0f : 1.0f;
1110 mRefreshOrderHint = true;
1112 float currentLayoutPosition = GetCurrentLayoutPosition(0);
1113 float firstItemScrollPosition = ClampFirstItemPosition(currentLayoutPosition + mScrollSpeed * direction,
1117 if (mAnchoringEnabled)
1119 firstItemScrollPosition = mActiveLayout->GetClosestAnchorPosition(firstItemScrollPosition);
1122 RemoveAnimation(mScrollAnimation);
1124 float flickAnimationDuration = Clamp( mActiveLayout->GetItemFlickAnimationDuration() * std::max(1.0f, fabsf(firstItemScrollPosition - GetCurrentLayoutPosition(0)))
1125 , DEFAULT_MINIMUM_SWIPE_DURATION, DEFAULT_MAXIMUM_SWIPE_DURATION);
1127 mScrollAnimation = Animation::New(flickAnimationDuration);
1128 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION ), firstItemScrollPosition, AlphaFunction::EASE_OUT );
1129 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::SCROLL_SPEED), 0.0f, AlphaFunction::EASE_OUT );
1132 // Check whether it has already scrolled to the end
1133 if(fabs(currentLayoutPosition - firstItemScrollPosition) > Math::MACHINE_EPSILON_0)
1135 AnimateScrollOvershoot(0.0f);
1139 // Anchoring may be triggered when there was no swipe
1140 if (!mScrollAnimation)
1142 mScrollAnimation = DoAnchoring();
1145 // Reset the overshoot if no scroll animation.
1146 if (!mScrollAnimation)
1148 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1150 AnimateScrollOvershoot(0.0f, false);
1155 case Gesture::Started: // Fall through
1157 mTotalPanDisplacement = Vector2::ZERO;
1158 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1159 mRefreshEnabled = true;
1162 case Gesture::Continuing:
1164 mScrollDistance = CalculateScrollDistance(gesture.displacement, *mActiveLayout);
1165 mScrollSpeed = Clamp((gesture.GetSpeed() * gesture.GetSpeed() * mActiveLayout->GetFlickSpeedFactor() * MILLISECONDS_PER_SECONDS), 0.0f, mActiveLayout->GetMaximumSwipeSpeed());
1167 // Refresh order depends on the direction of the scroll; negative is towards the last item.
1168 mRefreshOrderHint = mScrollDistance < 0.0f;
1170 float layoutPositionDelta = GetCurrentLayoutPosition(0) + (mScrollDistance * mActiveLayout->GetScrollSpeedFactor());
1172 float firstItemScrollPosition = ClampFirstItemPosition(layoutPositionDelta, layoutSize, *mActiveLayout);
1174 float currentOvershoot = self.GetProperty<float>(Toolkit::ItemView::Property::OVERSHOOT);
1176 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1178 if( (firstItemScrollPosition >= 0.0f && currentOvershoot < 1.0f) || (firstItemScrollPosition >= mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), layoutSize) && currentOvershoot > -1.0f) )
1180 mTotalPanDisplacement += gesture.displacement;
1183 mScrollOvershoot = CalculateScrollOvershoot();
1184 self.SetProperty( Toolkit::ItemView::Property::OVERSHOOT, mScrollOvershoot );
1188 case Gesture::Cancelled:
1190 mScrollAnimation = DoAnchoring();
1198 if (mScrollAnimation)
1200 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1201 mScrollAnimation.Play();
1205 bool ItemView::OnAccessibilityPan(PanGesture gesture)
1211 Actor ItemView::GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
1213 Actor nextFocusActor;
1217 if(!actor || actor == this->Self())
1219 nextFocusActor = GetItem(nextItemID);
1221 else if(actor && actor.GetParent() == this->Self())
1223 int itemID = GetItemId(actor);
1224 nextItemID = mActiveLayout->GetNextFocusItemID(itemID, mItemFactory.GetNumberOfItems(), direction, loopEnabled);
1225 nextFocusActor = GetItem(nextItemID);
1226 if(nextFocusActor == actor)
1228 // need to pass NULL actor back to focus manager
1229 nextFocusActor.Reset();
1230 return nextFocusActor;
1233 float layoutPosition = mActiveLayout->GetClosestAnchorPosition( GetCurrentLayoutPosition(0) );
1234 Vector3 layoutSize = Self().GetCurrentSize();
1237 // likely the current item is not buffered, so not in our item pool, probably best to get first viewable item
1238 ItemRange viewableItems = mActiveLayout->GetItemsWithinArea(layoutPosition, layoutSize);
1239 nextItemID = viewableItems.begin;
1240 nextFocusActor = GetItem(nextItemID);
1243 return nextFocusActor;
1246 void ItemView::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
1248 // only in this function if our chosen focus actor was actually used
1249 if(commitedFocusableActor)
1251 int nextItemID = GetItemId(commitedFocusableActor);
1252 float layoutPosition = GetCurrentLayoutPosition(0);
1253 Vector3 layoutSize = Self().GetCurrentSize();
1255 float scrollTo = mActiveLayout->GetClosestOnScreenLayoutPosition(nextItemID, layoutPosition, layoutSize);
1256 ScrollTo(Vector2(0.0f, scrollTo), DEFAULT_KEYBOARD_FOCUS_SCROLL_DURATION);
1260 Animation ItemView::DoAnchoring()
1262 Animation anchoringAnimation;
1263 Actor self = Self();
1265 if (mActiveLayout && mAnchoringEnabled)
1267 float anchorPosition = mActiveLayout->GetClosestAnchorPosition( GetCurrentLayoutPosition(0) );
1269 anchoringAnimation = Animation::New(mAnchoringDuration);
1270 anchoringAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), anchorPosition, AlphaFunction::EASE_OUT );
1271 anchoringAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::SCROLL_SPEED), 0.0f, AlphaFunction::EASE_OUT );
1274 AnimateScrollOvershoot(0.0f);
1278 return anchoringAnimation;
1281 void ItemView::OnScrollFinished(Animation& source)
1283 Actor self = Self();
1285 RemoveAnimation(mScrollAnimation); // mScrollAnimation is used to query whether we're scrolling
1287 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1289 if(mIsFlicking && fabsf(mScrollOvershoot) > Math::MACHINE_EPSILON_1)
1291 AnimateScrollOvershoot( mScrollOvershoot > 0.0f ? 1.0f : -1.0f, true);
1295 // Reset the overshoot
1296 AnimateScrollOvershoot( 0.0f );
1298 mIsFlicking = false;
1300 mScrollOvershoot = 0.0f;
1303 void ItemView::OnLayoutActivationScrollFinished(Animation& source)
1305 RemoveAnimation(mScrollAnimation);
1306 mRefreshEnabled = true;
1307 DoRefresh(GetCurrentLayoutPosition(0), true);
1309 // Emit the layout activated signal
1310 mLayoutActivatedSignal.Emit();
1313 void ItemView::OnOvershootOnFinished(Animation& animation)
1315 mAnimatingOvershootOn = false;
1316 mScrollOvershootAnimation.FinishedSignal().Disconnect(this, &ItemView::OnOvershootOnFinished);
1317 RemoveAnimation(mScrollOvershootAnimation);
1318 if(mAnimateOvershootOff)
1320 AnimateScrollOvershoot(0.0f);
1324 void ItemView::ScrollToItem(unsigned int itemId, float durationSeconds)
1326 Actor self = Self();
1327 const Vector3 layoutSize = Self().GetCurrentSize();
1328 float firstItemScrollPosition = ClampFirstItemPosition(mActiveLayout->GetItemScrollToPosition(itemId), layoutSize, *mActiveLayout);
1330 if(durationSeconds > 0.0f)
1332 RemoveAnimation(mScrollAnimation);
1333 mScrollAnimation = Animation::New(durationSeconds);
1334 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, AlphaFunction::EASE_OUT );
1335 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1336 mScrollAnimation.Play();
1340 self.SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1341 AnimateScrollOvershoot(0.0f);
1344 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1345 mRefreshEnabled = true;
1348 void ItemView::RemoveAnimation(Animation& animation)
1352 // Cease animating, and reset handle.
1358 void ItemView::CalculateDomainSize(const Vector3& layoutSize)
1360 Actor self = Self();
1362 Vector3 firstItemPosition(Vector3::ZERO);
1363 Vector3 lastItemPosition(Vector3::ZERO);
1367 firstItemPosition = mActiveLayout->GetItemPosition( 0,0,layoutSize );
1369 float minLayoutPosition = mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), layoutSize);
1370 lastItemPosition = mActiveLayout->GetItemPosition( fabs(minLayoutPosition),fabs(minLayoutPosition),layoutSize );
1374 if(IsHorizontal(mActiveLayout->GetOrientation()))
1376 domainSize = fabs(firstItemPosition.x - lastItemPosition.x);
1380 domainSize = fabs(firstItemPosition.y - lastItemPosition.y);
1383 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MIN, Vector2::ZERO);
1384 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1386 self.SetProperty(Toolkit::ItemView::Property::SCROLL_CONTENT_SIZE, domainSize);
1388 bool isLayoutScrollable = IsLayoutScrollable(layoutSize);
1389 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL, isLayoutScrollable);
1390 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL, false);
1394 Vector2 ItemView::GetDomainSize() const
1396 Actor self = Self();
1398 float minScrollPosition = self.GetProperty<float>(Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_Y);
1399 float maxScrollPosition = self.GetProperty<float>(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX_Y);
1401 return Vector2(0.0f, fabs(GetScrollPosition(minScrollPosition, self.GetCurrentSize()) - GetScrollPosition(-maxScrollPosition, self.GetCurrentSize())));
1404 bool ItemView::IsLayoutScrollable(const Vector3& layoutSize)
1406 Actor self = Self();
1408 float currentLayoutPosition = ClampFirstItemPosition( GetCurrentLayoutPosition(0), layoutSize, *mActiveLayout );
1409 float forwardClampedPosition = ClampFirstItemPosition(currentLayoutPosition + 1.0, layoutSize, *mActiveLayout);
1410 float backwardClampedPosition = ClampFirstItemPosition(currentLayoutPosition - 1.0, layoutSize, *mActiveLayout);
1412 return (fabs(forwardClampedPosition - backwardClampedPosition) > Math::MACHINE_EPSILON_0);
1415 float ItemView::GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const
1417 Vector3 firstItemPosition( mActiveLayout->GetItemPosition(0, layoutPosition, layoutSize ) );
1418 return IsHorizontal(mActiveLayout->GetOrientation()) ? firstItemPosition.x: firstItemPosition.y;
1421 Vector2 ItemView::GetCurrentScrollPosition() const
1423 return Vector2(0.0f, GetScrollPosition(GetCurrentLayoutPosition(0), Self().GetCurrentSize()));
1426 void ItemView::AddOverlay(Actor actor)
1431 void ItemView::RemoveOverlay(Actor actor)
1433 Self().Remove(actor);
1436 void ItemView::ScrollTo(const Vector2& position, float duration)
1438 Actor self = Self();
1439 const Vector3 layoutSize = Self().GetCurrentSize();
1441 float firstItemScrollPosition = ClampFirstItemPosition(position.y, layoutSize, *mActiveLayout);
1445 RemoveAnimation(mScrollAnimation);
1446 mScrollAnimation = Animation::New(duration);
1447 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, AlphaFunction::EASE_OUT );
1448 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1449 mScrollAnimation.Play();
1453 self.SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1454 AnimateScrollOvershoot(0.0f);
1457 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1458 mRefreshEnabled = true;
1461 void ItemView::SetOvershootEffectColor( const Vector4& color )
1463 mOvershootEffectColor = color;
1464 if( mOvershootOverlay )
1466 mOvershootOverlay.SetColor( color );
1470 void ItemView::EnableScrollOvershoot( bool enable )
1472 Actor self = Self();
1475 Property::Index effectOvershootPropertyIndex = Property::INVALID_INDEX;
1476 mOvershootOverlay = CreateBouncingEffectActor( effectOvershootPropertyIndex );
1477 mOvershootOverlay.SetColor(mOvershootEffectColor);
1478 mOvershootOverlay.SetParentOrigin(ParentOrigin::TOP_LEFT);
1479 mOvershootOverlay.SetAnchorPoint(AnchorPoint::TOP_LEFT);
1480 mOvershootOverlay.SetDrawMode(DrawMode::OVERLAY);
1481 self.Add(mOvershootOverlay);
1483 Constraint constraint = Constraint::New<Vector3>( mOvershootOverlay, Actor::Property::SIZE, OvershootOverlaySizeConstraint );
1484 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1485 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1486 constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
1489 mOvershootOverlay.SetSize(OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE.width, OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE.height);
1491 constraint = Constraint::New<Quaternion>( mOvershootOverlay, Actor::Property::ORIENTATION, OvershootOverlayRotationConstraint );
1492 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1493 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1494 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1497 constraint = Constraint::New<Vector3>( mOvershootOverlay, Actor::Property::POSITION, OvershootOverlayPositionConstraint );
1498 constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
1499 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1500 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1501 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1504 constraint = Constraint::New<bool>( mOvershootOverlay, Actor::Property::VISIBLE, OvershootOverlayVisibilityConstraint );
1505 constraint.AddSource( ParentSource( Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL ) );
1508 constraint = Constraint::New<float>( mOvershootOverlay, effectOvershootPropertyIndex, EqualToConstraint() );
1509 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1514 if( mOvershootOverlay )
1516 self.Remove(mOvershootOverlay);
1517 mOvershootOverlay.Reset();
1522 float ItemView::CalculateScrollOvershoot()
1524 float overshoot = 0.0f;
1528 // The overshoot must be calculated from the accumulated pan gesture displacement
1529 // since the pan gesture starts.
1530 Actor self = Self();
1531 float scrollDistance = CalculateScrollDistance(mTotalPanDisplacement, *mActiveLayout) * mActiveLayout->GetScrollSpeedFactor();
1532 float positionDelta = GetCurrentLayoutPosition(0) + scrollDistance;
1533 float minLayoutPosition = mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), Self().GetCurrentSize());
1534 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1535 float clamppedPosition = std::min(0.0f, std::max(minLayoutPosition, positionDelta));
1536 overshoot = positionDelta - clamppedPosition;
1539 return overshoot > 0.0f ? std::min(overshoot, 1.0f) : std::max(overshoot, -1.0f);
1542 void ItemView::AnimateScrollOvershoot(float overshootAmount, bool animateBack)
1544 bool animatingOn = fabsf(overshootAmount) > Math::MACHINE_EPSILON_1;
1546 // make sure we animate back if needed
1547 mAnimateOvershootOff = animateBack || (!animatingOn && mAnimatingOvershootOn);
1549 if( mAnimatingOvershootOn )
1551 // animating on, do not allow animate off
1555 Actor self = Self();
1557 if(mOvershootAnimationSpeed > Math::MACHINE_EPSILON_0)
1559 float currentOvershoot = self.GetProperty<float>(Toolkit::ItemView::Property::OVERSHOOT);
1560 float duration = 0.0f;
1562 if (mOvershootOverlay)
1564 duration = mOvershootOverlay.GetCurrentSize().height * (animatingOn ? (1.0f - fabsf(currentOvershoot)) : fabsf(currentOvershoot)) / mOvershootAnimationSpeed;
1567 RemoveAnimation(mScrollOvershootAnimation);
1568 mScrollOvershootAnimation = Animation::New(duration);
1569 mScrollOvershootAnimation.FinishedSignal().Connect(this, &ItemView::OnOvershootOnFinished);
1570 mScrollOvershootAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::OVERSHOOT), overshootAmount, TimePeriod(0.0f, duration) );
1571 mScrollOvershootAnimation.Play();
1573 mAnimatingOvershootOn = animatingOn;
1577 self.SetProperty( Toolkit::ItemView::Property::OVERSHOOT, overshootAmount );
1581 void ItemView::SetItemsParentOrigin( const Vector3& parentOrigin )
1583 if( parentOrigin != mItemsParentOrigin )
1585 mItemsParentOrigin = parentOrigin;
1586 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1588 iter->second.SetParentOrigin(parentOrigin);
1593 Vector3 ItemView::GetItemsParentOrigin() const
1595 return mItemsParentOrigin;
1598 void ItemView::SetItemsAnchorPoint( const Vector3& anchorPoint )
1600 if( anchorPoint != mItemsAnchorPoint )
1602 mItemsAnchorPoint = anchorPoint;
1603 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1605 iter->second.SetAnchorPoint(anchorPoint);
1610 Vector3 ItemView::GetItemsAnchorPoint() const
1612 return mItemsAnchorPoint;
1615 void ItemView::GetItemsRange(ItemRange& range)
1617 if( !mItemPool.empty() )
1619 range.begin = mItemPool.begin()->first;
1620 range.end = mItemPool.rbegin()->first + 1;
1629 void ItemView::OnScrollPositionChanged( float position )
1631 // Cancel scroll animation to prevent any fighting of setting the scroll position property.
1632 RemoveAnimation(mScrollAnimation);
1634 // Refresh the cache immediately when the scroll position is changed.
1635 DoRefresh(position, false); // No need to cache extra items.
1638 bool ItemView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1640 Dali::BaseHandle handle( object );
1642 bool connected( true );
1643 Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( handle );
1645 if( 0 == strcmp( signalName.c_str(), LAYOUT_ACTIVATED_SIGNAL ) )
1647 itemView.LayoutActivatedSignal().Connect( tracker, functor );
1651 // signalName does not match any signal
1658 } // namespace Internal
1660 } // namespace Toolkit