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