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-data.h>
30 #include <dali/public-api/object/type-registry.h>
31 #include <dali/public-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
45 const float DEFAULT_MINIMUM_SWIPE_SPEED = 1.0f;
46 const float DEFAULT_MINIMUM_SWIPE_DISTANCE = 3.0f;
47 const float DEFAULT_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION = 0.1f;
49 const float DEFAULT_MINIMUM_SWIPE_DURATION = 0.45f;
50 const float DEFAULT_MAXIMUM_SWIPE_DURATION = 2.6f;
52 const float DEFAULT_REFRESH_INTERVAL_LAYOUT_POSITIONS = 20.0f; // 1 updates per 20 items
53 const int WHEEL_EVENT_FINISHED_TIME_OUT = 500; // 0.5 second
55 const float DEFAULT_ANCHORING_DURATION = 1.0f; // 1 second
57 const float MILLISECONDS_PER_SECONDS = 1000.0f;
59 const float OVERSHOOT_BOUNCE_ACTOR_RESIZE_THRESHOLD = 180.0f;
60 const Vector4 OVERSHOOT_OVERLAY_NINE_PATCH_BORDER(0.0f, 0.0f, 1.0f, 12.0f);
61 const float DEFAULT_KEYBOARD_FOCUS_SCROLL_DURATION = 0.2f;
64 * Local helper to convert pan distance (in actor coordinates) to the layout-specific scrolling direction
66 float CalculateScrollDistance(Vector2 panDistance, Toolkit::ItemLayout& layout)
68 Radian scrollDirection(layout.GetScrollDirection());
70 float cosTheta = cosf(scrollDirection);
71 float sinTheta = sinf(scrollDirection);
73 return panDistance.x * sinTheta + panDistance.y * cosTheta;
76 // Overshoot overlay constraints
77 struct OvershootOverlaySizeConstraint
79 OvershootOverlaySizeConstraint( float height )
80 : mOvershootHeight( height )
84 void operator()( Vector3& current, const PropertyInputContainer& inputs )
86 const Vector2& parentScrollDirection = inputs[0]->GetVector2();
87 const Toolkit::ControlOrientation::Type& layoutOrientation = static_cast<Toolkit::ControlOrientation::Type>(inputs[1]->GetInteger());
88 const Vector3& parentSize = inputs[2]->GetVector3();
90 if(Toolkit::IsVertical(layoutOrientation))
92 current.width = fabsf(parentScrollDirection.y) > Math::MACHINE_EPSILON_1 ? parentSize.x : parentSize.y;
96 current.width = fabsf(parentScrollDirection.x) > Math::MACHINE_EPSILON_1 ? parentSize.y : parentSize.x;
99 current.height = ( current.width > OVERSHOOT_BOUNCE_ACTOR_RESIZE_THRESHOLD ) ? mOvershootHeight : mOvershootHeight*0.5f;
102 float mOvershootHeight;
105 void OvershootOverlayRotationConstraint( Quaternion& current, const PropertyInputContainer& inputs )
107 const Vector2& parentScrollDirection = inputs[0]->GetVector2();
108 const Toolkit::ControlOrientation::Type& layoutOrientation = static_cast<Toolkit::ControlOrientation::Type>(inputs[1]->GetInteger());
109 const float parentOvershoot = inputs[2]->GetFloat();
111 float multiplier = 0;
112 if(Toolkit::IsVertical(layoutOrientation))
114 if(fabsf(parentScrollDirection.y) <= Math::MACHINE_EPSILON_1)
116 if( (layoutOrientation == Toolkit::ControlOrientation::Up && parentOvershoot < Math::MACHINE_EPSILON_0)
117 || (layoutOrientation == Toolkit::ControlOrientation::Down && parentOvershoot > Math::MACHINE_EPSILON_0) )
126 else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.y > Math::MACHINE_EPSILON_0)
127 || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.y < Math::MACHINE_EPSILON_0) )
138 if(fabsf(parentScrollDirection.x) <= Math::MACHINE_EPSILON_1)
140 if( (layoutOrientation == Toolkit::ControlOrientation::Left && parentOvershoot > Math::MACHINE_EPSILON_0)
141 ||(layoutOrientation == Toolkit::ControlOrientation::Right && parentOvershoot < Math::MACHINE_EPSILON_0) )
150 else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.x > Math::MACHINE_EPSILON_0)
151 || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.x < Math::MACHINE_EPSILON_0) )
161 current = Quaternion( Radian( multiplier * Math::PI ), Vector3::ZAXIS );
164 void OvershootOverlayPositionConstraint( Vector3& current, const PropertyInputContainer& inputs )
166 const Vector3& parentSize = inputs[0]->GetVector3();
167 const Vector2& parentScrollDirection = inputs[1]->GetVector2();
168 const Toolkit::ControlOrientation::Type& layoutOrientation = static_cast<Toolkit::ControlOrientation::Type>(inputs[2]->GetInteger());
169 const float parentOvershoot = inputs[3]->GetFloat();
171 Vector3 relativeOffset;
173 if(Toolkit::IsVertical(layoutOrientation))
175 if(fabsf(parentScrollDirection.y) <= Math::MACHINE_EPSILON_1)
177 if( (layoutOrientation == Toolkit::ControlOrientation::Up && parentOvershoot < Math::MACHINE_EPSILON_0)
178 || (layoutOrientation == Toolkit::ControlOrientation::Down && parentOvershoot > Math::MACHINE_EPSILON_0) )
180 relativeOffset = Vector3(1.0f, 0.0f, 0.0f);
184 relativeOffset =Vector3(0.0f, 1.0f, 0.0f);
187 else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.y > Math::MACHINE_EPSILON_0)
188 || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.y < Math::MACHINE_EPSILON_0) )
190 relativeOffset = Vector3(0.0f, 0.0f, 0.0f);
194 relativeOffset = Vector3(1.0f, 1.0f, 0.0f);
199 if(fabsf(parentScrollDirection.x) <= Math::MACHINE_EPSILON_1)
201 if( (layoutOrientation == Toolkit::ControlOrientation::Left && parentOvershoot < Math::MACHINE_EPSILON_0)
202 || (layoutOrientation == Toolkit::ControlOrientation::Right && parentOvershoot > Math::MACHINE_EPSILON_0) )
204 relativeOffset = Vector3(0.0f, 0.0f, 0.0f);
208 relativeOffset = Vector3(1.0f, 1.0f, 0.0f);
211 else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.x > Math::MACHINE_EPSILON_0)
212 || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.x < Math::MACHINE_EPSILON_0) )
214 relativeOffset = Vector3(0.0f, 1.0f, 0.0f);
218 relativeOffset = Vector3(1.0f, 0.0f, 0.0f);
222 current = relativeOffset * parentSize;
225 void OvershootOverlayVisibilityConstraint( bool& current, const PropertyInputContainer& inputs )
227 current = inputs[0]->GetBoolean();
230 } // unnamed namespace
241 namespace // unnamed namespace
246 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ItemView, Toolkit::Scrollable, NULL)
248 DALI_PROPERTY_REGISTRATION( Toolkit, ItemView, "minimumSwipeSpeed", FLOAT, MINIMUM_SWIPE_SPEED )
249 DALI_PROPERTY_REGISTRATION( Toolkit, ItemView, "minimumSwipeDistance", FLOAT, MINIMUM_SWIPE_DISTANCE )
250 DALI_PROPERTY_REGISTRATION( Toolkit, ItemView, "wheelScrollDistanceStep", FLOAT, WHEEL_SCROLL_DISTANCE_STEP )
251 DALI_PROPERTY_REGISTRATION( Toolkit, ItemView, "snapToItemEnabled", BOOLEAN, SNAP_TO_ITEM_ENABLED )
252 DALI_PROPERTY_REGISTRATION( Toolkit, ItemView, "refreshInterval", FLOAT, REFRESH_INTERVAL )
254 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "layoutPosition", FLOAT, LAYOUT_POSITION)
255 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "scrollSpeed", FLOAT, SCROLL_SPEED)
256 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "overshoot", FLOAT, OVERSHOOT)
257 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "scrollDirection", VECTOR2, SCROLL_DIRECTION)
258 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "layoutOrientation", INTEGER, LAYOUT_ORIENTATION)
259 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "scrollContentSize", FLOAT, SCROLL_CONTENT_SIZE)
261 DALI_SIGNAL_REGISTRATION( Toolkit, ItemView, "layoutActivated", LAYOUT_ACTIVATED_SIGNAL )
263 DALI_ACTION_REGISTRATION( Toolkit, ItemView, "stopScrolling", ACTION_STOP_SCROLLING )
265 DALI_ACTION_REGISTRATION( Toolkit, ItemView, "enableRefresh", ACTION_ENABLE_REFRESH )
266 DALI_ACTION_REGISTRATION( Toolkit, ItemView, "disableRefresh", ACTION_DISABLE_REFRESH )
268 DALI_TYPE_REGISTRATION_END()
270 bool FindById( const ItemContainer& items, ItemId id )
272 for( ConstItemIter iter = items.begin(); items.end() != iter; ++iter )
274 if( iter->first == id )
283 } // unnamed namespace
285 Dali::Toolkit::ItemView ItemView::New(ItemFactory& factory)
287 // Create the implementation
288 ItemViewPtr itemView(new ItemView(factory));
290 // Pass ownership to CustomActor via derived handle
291 Dali::Toolkit::ItemView handle(*itemView);
293 // Second-phase init of the implementation
294 // This can only be done after the CustomActor connection has been made...
295 itemView->Initialize();
300 ItemView::ItemView(ItemFactory& factory)
301 : Scrollable( ControlBehaviour( DISABLE_SIZE_NEGOTIATION | REQUIRES_WHEEL_EVENTS | REQUIRES_KEYBOARD_NAVIGATION_SUPPORT ) ),
302 mItemFactory(factory),
303 mItemsParentOrigin(ParentOrigin::CENTER),
304 mItemsAnchorPoint(AnchorPoint::CENTER),
305 mTotalPanDisplacement(Vector2::ZERO),
307 mAnchoringDuration(DEFAULT_ANCHORING_DURATION),
308 mRefreshIntervalLayoutPositions(0.0f),
309 mMinimumSwipeSpeed(DEFAULT_MINIMUM_SWIPE_SPEED),
310 mMinimumSwipeDistance(DEFAULT_MINIMUM_SWIPE_DISTANCE),
311 mWheelScrollDistanceStep(0.0f),
312 mScrollDistance(0.0f),
314 mScrollOvershoot(0.0f),
315 mGestureState(Gesture::Clear),
316 mAnimatingOvershootOn(false),
317 mAnimateOvershootOff(false),
318 mAnchoringEnabled(false),
319 mRefreshOrderHint(true/*Refresh item 0 first*/),
322 mRefreshEnabled(true),
323 mRefreshNotificationEnabled(true),
328 void ItemView::OnInitialize()
332 Vector2 stageSize = Stage::GetCurrent().GetSize();
333 mWheelScrollDistanceStep = stageSize.y * DEFAULT_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION;
335 self.TouchSignal().Connect( this, &ItemView::OnTouch );
336 EnableGestureDetection(Gesture::Type(Gesture::Pan));
338 mWheelEventFinishedTimer = Timer::New( WHEEL_EVENT_FINISHED_TIME_OUT );
339 mWheelEventFinishedTimer.TickSignal().Connect( this, &ItemView::OnWheelEventFinished );
341 SetRefreshInterval(DEFAULT_REFRESH_INTERVAL_LAYOUT_POSITIONS);
344 ItemView::~ItemView()
348 unsigned int ItemView::GetLayoutCount() const
350 return mLayouts.size();
353 void ItemView::AddLayout(ItemLayout& layout)
355 mLayouts.push_back(ItemLayoutPtr(&layout));
358 void ItemView::RemoveLayout(unsigned int layoutIndex)
360 DALI_ASSERT_ALWAYS(layoutIndex < mLayouts.size());
362 if (mActiveLayout == mLayouts[layoutIndex].Get())
364 mActiveLayout = NULL;
367 mLayouts.erase(mLayouts.begin() + layoutIndex);
370 ItemLayoutPtr ItemView::GetLayout(unsigned int layoutIndex) const
372 return mLayouts[layoutIndex];
375 ItemLayoutPtr ItemView::GetActiveLayout() const
377 return ItemLayoutPtr(mActiveLayout);
380 float ItemView::GetCurrentLayoutPosition(unsigned int itemId) const
382 return Self().GetProperty<float>( Toolkit::ItemView::Property::LAYOUT_POSITION ) + static_cast<float>( itemId );
385 void ItemView::ActivateLayout(unsigned int layoutIndex, const Vector3& targetSize, float durationSeconds)
387 DALI_ASSERT_ALWAYS(layoutIndex < mLayouts.size());
389 mRefreshEnabled = false;
393 // The ItemView size should match the active layout size
394 self.SetSize(targetSize);
395 mActiveLayoutTargetSize = targetSize;
397 // Switch to the new layout
398 mActiveLayout = mLayouts[layoutIndex].Get();
400 // Move the items to the new layout positions...
402 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
404 unsigned int itemId = iter->first;
405 Actor actor = iter->second;
407 // Remove constraints from previous layout
408 actor.RemoveConstraints();
411 mActiveLayout->GetItemSize( itemId, targetSize, size );
412 actor.SetSize( size.GetVectorXY() );
414 mActiveLayout->ApplyConstraints(actor, itemId, targetSize, Self() );
417 // Refresh the new layout
418 ItemRange range = GetItemRange(*mActiveLayout, targetSize, GetCurrentLayoutPosition(0), false/* don't reserve extra*/);
419 AddActorsWithinRange( range, targetSize );
421 // Scroll to an appropriate layout position
423 bool scrollAnimationNeeded(false);
424 float firstItemScrollPosition(0.0f);
426 float current = GetCurrentLayoutPosition(0);
427 float minimum = ClampFirstItemPosition(current, targetSize, *mActiveLayout);
429 if (current < minimum)
431 scrollAnimationNeeded = true;
432 firstItemScrollPosition = minimum;
434 else if (mAnchoringEnabled)
436 scrollAnimationNeeded = true;
437 firstItemScrollPosition = mActiveLayout->GetClosestAnchorPosition(current);
440 if (scrollAnimationNeeded)
442 RemoveAnimation(mScrollAnimation);
443 mScrollAnimation = Animation::New(durationSeconds);
444 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, AlphaFunction::EASE_OUT );
445 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnLayoutActivationScrollFinished);
446 mScrollAnimation.Play();
450 // Emit the layout activated signal
451 mLayoutActivatedSignal.Emit();
454 AnimateScrollOvershoot(0.0f);
455 mScrollOvershoot = 0.0f;
457 Radian scrollDirection(mActiveLayout->GetScrollDirection());
458 self.SetProperty(Toolkit::ItemView::Property::SCROLL_DIRECTION, Vector2(sinf(scrollDirection), cosf(scrollDirection)));
459 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_ORIENTATION, static_cast<int>(mActiveLayout->GetOrientation()));
460 self.SetProperty(Toolkit::ItemView::Property::SCROLL_SPEED, mScrollSpeed);
462 CalculateDomainSize(targetSize);
465 void ItemView::DeactivateCurrentLayout()
469 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
471 Actor actor = iter->second;
472 actor.RemoveConstraints();
475 mActiveLayout = NULL;
479 void ItemView::OnRefreshNotification(PropertyNotification& source)
481 if( mRefreshNotificationEnabled )
483 // Cancel scroll animation to prevent any fighting of setting the scroll position property by scroll bar during fast scroll.
484 if(!mRefreshEnabled && mScrollAnimation)
486 RemoveAnimation(mScrollAnimation);
489 // Only cache extra items when it is not a fast scroll
490 DoRefresh(GetCurrentLayoutPosition(0), mRefreshEnabled || mScrollAnimation);
494 void ItemView::Refresh()
496 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter )
498 ReleaseActor( iter->first, iter->second );
502 DoRefresh(GetCurrentLayoutPosition(0), true);
505 void ItemView::DoRefresh(float currentLayoutPosition, bool cacheExtra)
509 ItemRange range = GetItemRange(*mActiveLayout, mActiveLayoutTargetSize, currentLayoutPosition, cacheExtra/*reserve extra*/);
510 RemoveActorsOutsideRange( range );
511 AddActorsWithinRange( range, Self().GetCurrentSize() );
513 mScrollUpdatedSignal.Emit( Vector2(0.0f, currentLayoutPosition) );
517 void ItemView::SetMinimumSwipeSpeed(float speed)
519 mMinimumSwipeSpeed = speed;
522 float ItemView::GetMinimumSwipeSpeed() const
524 return mMinimumSwipeSpeed;
527 void ItemView::SetMinimumSwipeDistance(float distance)
529 mMinimumSwipeDistance = distance;
532 float ItemView::GetMinimumSwipeDistance() const
534 return mMinimumSwipeDistance;
537 void ItemView::SetWheelScrollDistanceStep(float step)
539 mWheelScrollDistanceStep = step;
542 float ItemView::GetWheelScrollDistanceStep() const
544 return mWheelScrollDistanceStep;
547 void ItemView::SetAnchoring(bool enabled)
549 mAnchoringEnabled = enabled;
552 bool ItemView::GetAnchoring() const
554 return mAnchoringEnabled;
557 void ItemView::SetAnchoringDuration(float durationSeconds)
559 mAnchoringDuration = durationSeconds;
562 float ItemView::GetAnchoringDuration() const
564 return mAnchoringDuration;
567 void ItemView::SetRefreshInterval(float intervalLayoutPositions)
569 if( !Equals(mRefreshIntervalLayoutPositions, intervalLayoutPositions) )
571 mRefreshIntervalLayoutPositions = intervalLayoutPositions;
574 if(mRefreshNotification)
576 self.RemovePropertyNotification(mRefreshNotification);
578 mRefreshNotification = self.AddPropertyNotification( Toolkit::ItemView::Property::LAYOUT_POSITION, StepCondition(mRefreshIntervalLayoutPositions, 0.0f) );
579 mRefreshNotification.NotifySignal().Connect( this, &ItemView::OnRefreshNotification );
583 float ItemView::GetRefreshInterval() const
585 return mRefreshIntervalLayoutPositions;
588 void ItemView::SetRefreshEnabled(bool enabled)
590 mRefreshEnabled = enabled;
593 Actor ItemView::GetItem(unsigned int itemId) const
597 ConstItemPoolIter iter = mItemPool.find( itemId );
598 if( iter != mItemPool.end() )
600 actor = iter->second;
606 unsigned int ItemView::GetItemId( Actor actor ) const
608 unsigned int itemId( 0 );
610 for ( ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter )
612 if( iter->second == actor )
614 itemId = iter->first;
622 void ItemView::InsertItem( Item newItem, float durationSeconds )
625 Vector3 layoutSize = Self().GetCurrentSize();
627 Actor displacedActor;
628 ItemPoolIter afterDisplacedIter = mItemPool.end();
630 ItemPoolIter foundIter = mItemPool.find( newItem.first );
631 if( mItemPool.end() != foundIter )
633 SetupActor( newItem, layoutSize );
634 Self().Add( newItem.second );
636 displacedActor = foundIter->second;
637 foundIter->second = newItem.second;
639 afterDisplacedIter = ++foundIter;
643 // Inserting before the existing item range?
644 ItemPoolIter iter = mItemPool.begin();
645 if( iter != mItemPool.end() &&
646 iter->first > newItem.first )
648 displacedActor = iter->second;
649 mItemPool.erase( iter++ ); // iter is still valid after the erase
651 afterDisplacedIter = iter;
657 // Move the existing actors to make room
658 for( ItemPoolIter iter = afterDisplacedIter; mItemPool.end() != iter; ++iter )
660 Actor temp = iter->second;
661 iter->second = displacedActor;
662 displacedActor = temp;
664 iter->second.RemoveConstraints();
665 mActiveLayout->ApplyConstraints( iter->second, iter->first, layoutSize, Self() );
669 ItemPool::reverse_iterator lastIter = mItemPool.rbegin();
670 if ( lastIter != mItemPool.rend() )
672 ItemId lastId = lastIter->first;
673 Item lastItem( lastId + 1, displacedActor );
674 mItemPool.insert( lastItem );
676 lastItem.second.RemoveConstraints();
677 mActiveLayout->ApplyConstraints( lastItem.second, lastItem.first, layoutSize, Self() );
681 CalculateDomainSize( layoutSize );
683 mAddingItems = false;
686 void ItemView::InsertItems( const ItemContainer& newItems, float durationSeconds )
689 Vector3 layoutSize = Self().GetCurrentSize();
691 // Insert from lowest id to highest
692 std::set<Item> sortedItems;
693 for( ConstItemIter iter = newItems.begin(); newItems.end() != iter; ++iter )
695 sortedItems.insert( *iter );
698 for( std::set<Item>::iterator iter = sortedItems.begin(); sortedItems.end() != iter; ++iter )
700 Self().Add( iter->second );
702 ItemPoolIter foundIter = mItemPool.find( iter->first );
703 if( mItemPool.end() != foundIter )
705 Actor moveMe = foundIter->second;
706 foundIter->second = iter->second;
708 // Move the existing actors to make room
709 for( ItemPoolIter iter = ++foundIter; mItemPool.end() != iter; ++iter )
711 Actor temp = iter->second;
712 iter->second = moveMe;
717 ItemId lastId = mItemPool.rbegin()->first;
718 Item lastItem( lastId + 1, moveMe );
719 mItemPool.insert( lastItem );
723 mItemPool.insert( *iter );
727 // Relayout everything
728 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
731 if( FindById( newItems, iter->first ) )
733 SetupActor( *iter, layoutSize );
737 iter->second.RemoveConstraints();
738 mActiveLayout->ApplyConstraints( iter->second, iter->first, layoutSize, Self() );
742 CalculateDomainSize( layoutSize );
744 mAddingItems = false;
747 void ItemView::RemoveItem( unsigned int itemId, float durationSeconds )
749 bool actorsReordered = RemoveActor( itemId );
750 if( actorsReordered )
752 ReapplyAllConstraints();
758 void ItemView::RemoveItems( const ItemIdContainer& itemIds, float durationSeconds )
760 bool actorsReordered( false );
762 // Remove from highest id to lowest
763 set<ItemId> sortedItems;
764 for( ConstItemIdIter iter = itemIds.begin(); itemIds.end() != iter; ++iter )
766 sortedItems.insert( *iter );
769 for( set<ItemId>::reverse_iterator iter = sortedItems.rbegin(); sortedItems.rend() != iter; ++iter )
771 if( RemoveActor( *iter ) )
773 actorsReordered = true;
777 if( actorsReordered )
779 ReapplyAllConstraints();
785 bool ItemView::RemoveActor(unsigned int itemId)
787 bool reordered( false );
789 ItemPoolIter removeIter = mItemPool.find( itemId );
790 if( removeIter != mItemPool.end() )
792 ReleaseActor(itemId, removeIter->second);
796 // Removing before the existing item range?
797 ItemPoolIter iter = mItemPool.begin();
798 if( iter != mItemPool.end() &&
799 iter->first > itemId )
801 // In order to decrement the first visible item ID
802 mItemPool.insert( Item(iter->first - 1, Actor()) );
804 removeIter = mItemPool.begin();
808 if( removeIter != mItemPool.end() )
812 // Adjust the remaining item IDs, for example if item 2 is removed:
813 // Initial actors: After insert:
814 // ID 1 - ActorA ID 1 - ActorA
815 // ID 2 - ActorB ID 2 - ActorC (previously ID 3)
816 // ID 3 - ActorC ID 3 - ActorB (previously ID 4)
818 for (ItemPoolIter iter = removeIter; iter != mItemPool.end(); ++iter)
820 if( iter->first < mItemPool.rbegin()->first )
822 iter->second = mItemPool[ iter->first + 1 ];
826 mItemPool.erase( iter );
835 void ItemView::ReplaceItem( Item replacementItem, float durationSeconds )
838 Vector3 layoutSize = Self().GetCurrentSize();
840 SetupActor( replacementItem, layoutSize );
841 Self().Add( replacementItem.second );
843 const ItemPoolIter iter = mItemPool.find( replacementItem.first );
844 if( mItemPool.end() != iter )
846 ReleaseActor(iter->first, iter->second);
847 iter->second = replacementItem.second;
851 mItemPool.insert( replacementItem );
854 CalculateDomainSize( layoutSize );
856 mAddingItems = false;
859 void ItemView::ReplaceItems( const ItemContainer& replacementItems, float durationSeconds )
861 for( ConstItemIter iter = replacementItems.begin(); replacementItems.end() != iter; ++iter )
863 ReplaceItem( *iter, durationSeconds );
867 void ItemView::RemoveActorsOutsideRange( ItemRange range )
869 // Remove unwanted actors from the ItemView & ItemPool
870 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); )
872 unsigned int current = iter->first;
874 if( ! range.Within( current ) )
876 ReleaseActor(iter->first, iter->second);
878 mItemPool.erase( iter++ ); // erase invalidates the return value of post-increment; iter remains valid
887 void ItemView::AddActorsWithinRange( ItemRange range, const Vector3& layoutSize )
889 range.end = std::min(mItemFactory.GetNumberOfItems(), range.end);
891 // The order of addition depends on the scroll direction.
892 if (mRefreshOrderHint)
894 for (unsigned int itemId = range.begin; itemId < range.end; ++itemId)
896 AddNewActor( itemId, layoutSize );
901 for (unsigned int itemId = range.end; itemId > range.begin; --itemId)
903 AddNewActor( itemId-1, layoutSize );
907 // Total number of items may change dynamically.
908 // Always recalculate the domain size to reflect that.
909 CalculateDomainSize(Self().GetCurrentSize());
912 void ItemView::AddNewActor( unsigned int itemId, const Vector3& layoutSize )
916 if( mItemPool.end() == mItemPool.find( itemId ) )
918 Actor actor = mItemFactory.NewItem( itemId );
922 Item newItem( itemId, actor );
924 mItemPool.insert( newItem );
926 SetupActor( newItem, layoutSize );
931 mAddingItems = false;
934 void ItemView::SetupActor( Item item, const Vector3& layoutSize )
936 item.second.SetParentOrigin( mItemsParentOrigin );
937 item.second.SetAnchorPoint( mItemsAnchorPoint );
942 mActiveLayout->GetItemSize( item.first, mActiveLayoutTargetSize, size );
943 item.second.SetSize( size.GetVectorXY() );
945 mActiveLayout->ApplyConstraints( item.second, item.first, layoutSize, Self() );
949 void ItemView::ReleaseActor( ItemId item, Actor actor )
951 Self().Remove( actor );
952 mItemFactory.ItemReleased(item, actor);
955 ItemRange ItemView::GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra)
957 unsigned int itemCount = mItemFactory.GetNumberOfItems();
959 ItemRange available(0u, itemCount);
961 ItemRange range = layout.GetItemsWithinArea( layoutPosition, layoutSize );
965 // Add the reserve items for scrolling
966 unsigned int extra = layout.GetReserveItemCount(layoutSize);
967 range.begin = (range.begin >= extra) ? (range.begin - extra) : 0u;
971 return range.Intersection(available);
974 void ItemView::OnChildAdd(Actor& child)
978 // We don't want to do this downcast check for any item added by ItemView itself.
979 Dali::Toolkit::ScrollBar scrollBar = Dali::Toolkit::ScrollBar::DownCast(child);
982 scrollBar.SetScrollPropertySource(Self(),
983 Toolkit::ItemView::Property::LAYOUT_POSITION,
984 Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_Y,
985 Toolkit::Scrollable::Property::SCROLL_POSITION_MAX_Y,
986 Toolkit::ItemView::Property::SCROLL_CONTENT_SIZE);
991 bool ItemView::OnWheelEvent(const WheelEvent& event)
993 // Respond the wheel event to scroll
997 const Vector3 layoutSize = Self().GetCurrentSize();
998 float layoutPositionDelta = GetCurrentLayoutPosition(0) - (event.z * mWheelScrollDistanceStep * mActiveLayout->GetScrollSpeedFactor());
999 float firstItemScrollPosition = ClampFirstItemPosition(layoutPositionDelta, layoutSize, *mActiveLayout);
1001 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1003 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1004 mRefreshEnabled = true;
1007 if (mWheelEventFinishedTimer.IsRunning())
1009 mWheelEventFinishedTimer.Stop();
1012 mWheelEventFinishedTimer.Start();
1017 bool ItemView::OnWheelEventFinished()
1021 RemoveAnimation(mScrollAnimation);
1023 // No more wheel events coming. Do the anchoring if enabled.
1024 mScrollAnimation = DoAnchoring();
1025 if (mScrollAnimation)
1027 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1028 mScrollAnimation.Play();
1032 mScrollOvershoot = 0.0f;
1033 AnimateScrollOvershoot(0.0f);
1035 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1042 void ItemView::ReapplyAllConstraints()
1044 Vector3 layoutSize = Self().GetCurrentSize();
1046 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1048 unsigned int id = iter->first;
1049 Actor actor = iter->second;
1051 actor.RemoveConstraints();
1052 mActiveLayout->ApplyConstraints(actor, id, layoutSize, Self());
1056 void ItemView::OnItemsRemoved()
1058 CalculateDomainSize(Self().GetCurrentSize());
1060 // Adjust scroll-position after an item is removed
1063 float firstItemScrollPosition = ClampFirstItemPosition(GetCurrentLayoutPosition(0), Self().GetCurrentSize(), *mActiveLayout);
1064 Self().SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1068 float ItemView::ClampFirstItemPosition( float targetPosition, const Vector3& targetSize, ItemLayout& layout, bool updateOvershoot )
1070 Actor self = Self();
1071 float minLayoutPosition = layout.GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), targetSize);
1072 float clamppedPosition = std::min(0.0f, std::max(minLayoutPosition, targetPosition));
1073 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1075 if( updateOvershoot )
1077 mScrollOvershoot = targetPosition - clamppedPosition;
1080 return clamppedPosition;
1083 bool ItemView::OnTouch( Actor actor, const TouchData& touch )
1085 // Ignore events with multiple-touch points
1086 if (touch.GetPointCount() != 1)
1091 if ( touch.GetState( 0 ) == PointState::DOWN )
1093 // Cancel ongoing scrolling etc.
1094 mGestureState = Gesture::Clear;
1096 mScrollDistance = 0.0f;
1097 mScrollSpeed = 0.0f;
1098 Self().SetProperty(Toolkit::ItemView::Property::SCROLL_SPEED, mScrollSpeed);
1100 mScrollOvershoot = 0.0f;
1101 AnimateScrollOvershoot(0.0f);
1103 if(mScrollAnimation)
1105 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1108 RemoveAnimation(mScrollAnimation);
1111 return true; // consume since we're potentially scrolling
1114 void ItemView::OnPan( const PanGesture& gesture )
1116 Actor self = Self();
1117 const Vector3 layoutSize = Self().GetCurrentSize();
1119 RemoveAnimation(mScrollAnimation);
1121 // Short-circuit if there is no active layout
1124 mGestureState = Gesture::Clear;
1128 mGestureState = gesture.state;
1130 switch (mGestureState)
1132 case Gesture::Finished:
1135 if (fabsf(mScrollDistance) > mMinimumSwipeDistance &&
1136 mScrollSpeed > mMinimumSwipeSpeed)
1138 float direction = (mScrollDistance < 0.0f) ? -1.0f : 1.0f;
1140 mRefreshOrderHint = true;
1142 float currentLayoutPosition = GetCurrentLayoutPosition(0);
1143 float firstItemScrollPosition = ClampFirstItemPosition(currentLayoutPosition + mScrollSpeed * direction,
1147 if (mAnchoringEnabled)
1149 firstItemScrollPosition = mActiveLayout->GetClosestAnchorPosition(firstItemScrollPosition);
1152 RemoveAnimation(mScrollAnimation);
1154 float flickAnimationDuration = Clamp( mActiveLayout->GetItemFlickAnimationDuration() * std::max(1.0f, fabsf(firstItemScrollPosition - GetCurrentLayoutPosition(0)))
1155 , DEFAULT_MINIMUM_SWIPE_DURATION, DEFAULT_MAXIMUM_SWIPE_DURATION);
1157 mScrollAnimation = Animation::New(flickAnimationDuration);
1158 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION ), firstItemScrollPosition, AlphaFunction::EASE_OUT );
1159 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::SCROLL_SPEED), 0.0f, AlphaFunction::EASE_OUT );
1162 // Check whether it has already scrolled to the end
1163 if(fabs(currentLayoutPosition - firstItemScrollPosition) > Math::MACHINE_EPSILON_0)
1165 AnimateScrollOvershoot(0.0f);
1169 // Anchoring may be triggered when there was no swipe
1170 if (!mScrollAnimation)
1172 mScrollAnimation = DoAnchoring();
1175 // Reset the overshoot if no scroll animation.
1176 if (!mScrollAnimation)
1178 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1180 AnimateScrollOvershoot(0.0f, false);
1185 case Gesture::Started: // Fall through
1187 mTotalPanDisplacement = Vector2::ZERO;
1188 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1189 mRefreshEnabled = true;
1192 case Gesture::Continuing:
1194 mScrollDistance = CalculateScrollDistance(gesture.displacement, *mActiveLayout);
1195 mScrollSpeed = Clamp((gesture.GetSpeed() * gesture.GetSpeed() * mActiveLayout->GetFlickSpeedFactor() * MILLISECONDS_PER_SECONDS), 0.0f, mActiveLayout->GetMaximumSwipeSpeed());
1197 // Refresh order depends on the direction of the scroll; negative is towards the last item.
1198 mRefreshOrderHint = mScrollDistance < 0.0f;
1200 float layoutPositionDelta = GetCurrentLayoutPosition(0) + (mScrollDistance * mActiveLayout->GetScrollSpeedFactor());
1202 float firstItemScrollPosition = ClampFirstItemPosition(layoutPositionDelta, layoutSize, *mActiveLayout);
1204 float currentOvershoot = self.GetProperty<float>(Toolkit::ItemView::Property::OVERSHOOT);
1206 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1208 if( ( firstItemScrollPosition >= 0.0f &&
1209 currentOvershoot < 1.0f ) ||
1210 ( firstItemScrollPosition <= mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), layoutSize) &&
1211 currentOvershoot > -1.0f ) )
1213 mTotalPanDisplacement += gesture.displacement;
1216 mScrollOvershoot = CalculateScrollOvershoot();
1218 // If the view is moved in a direction against the overshoot indicator, then the indicator should be animated off.
1219 // First make sure we are not in an animation, otherwise a previously started
1220 // off-animation will be overwritten as the user continues scrolling.
1223 // Check if the movement is against the current overshoot amount (if we are currently displaying the indicator).
1224 if( ( ( mScrollOvershoot > Math::MACHINE_EPSILON_0 ) && ( mScrollDistance < -Math::MACHINE_EPSILON_0 ) ) ||
1225 ( ( mScrollOvershoot < Math::MACHINE_EPSILON_0 ) && ( mScrollDistance > Math::MACHINE_EPSILON_0 ) ) )
1227 // The user has moved against the indicator direction.
1228 // First, we reset the total displacement. This means the overshoot amount will become zero the next frame,
1229 // and if the user starts dragging in the overshoot direction again, the indicator will appear once more.
1230 mTotalPanDisplacement = Vector2::ZERO;
1231 // Animate the overshoot indicator off.
1232 AnimateScrollOvershoot( 0.0f, false );
1236 // Only set the property directly if we are not animating the overshoot away,
1237 // as otherwise this will overwrite the animation generated value.
1238 self.SetProperty( Toolkit::ItemView::Property::OVERSHOOT, mScrollOvershoot );
1244 case Gesture::Cancelled:
1246 mScrollAnimation = DoAnchoring();
1254 if (mScrollAnimation)
1256 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1257 mScrollAnimation.Play();
1261 bool ItemView::OnAccessibilityPan(PanGesture gesture)
1267 Actor ItemView::GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
1269 Actor nextFocusActor;
1273 if(!actor || actor == this->Self())
1275 nextFocusActor = GetItem(nextItemID);
1277 else if(actor && actor.GetParent() == this->Self())
1279 int itemID = GetItemId(actor);
1280 nextItemID = mActiveLayout->GetNextFocusItemID(itemID, mItemFactory.GetNumberOfItems(), direction, loopEnabled);
1281 nextFocusActor = GetItem(nextItemID);
1282 if(nextFocusActor == actor)
1284 // need to pass NULL actor back to focus manager
1285 nextFocusActor.Reset();
1286 return nextFocusActor;
1289 float layoutPosition = mActiveLayout->GetClosestAnchorPosition( GetCurrentLayoutPosition(0) );
1290 Vector3 layoutSize = Self().GetCurrentSize();
1293 // likely the current item is not buffered, so not in our item pool, probably best to get first viewable item
1294 ItemRange viewableItems = mActiveLayout->GetItemsWithinArea(layoutPosition, layoutSize);
1295 nextItemID = viewableItems.begin;
1296 nextFocusActor = GetItem(nextItemID);
1299 return nextFocusActor;
1302 void ItemView::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
1304 // only in this function if our chosen focus actor was actually used
1305 if(commitedFocusableActor)
1307 int nextItemID = GetItemId(commitedFocusableActor);
1308 float layoutPosition = GetCurrentLayoutPosition(0);
1309 Vector3 layoutSize = Self().GetCurrentSize();
1311 float scrollTo = mActiveLayout->GetClosestOnScreenLayoutPosition(nextItemID, layoutPosition, layoutSize);
1312 ScrollTo(Vector2(0.0f, scrollTo), DEFAULT_KEYBOARD_FOCUS_SCROLL_DURATION);
1316 Animation ItemView::DoAnchoring()
1318 Animation anchoringAnimation;
1319 Actor self = Self();
1321 if (mActiveLayout && mAnchoringEnabled)
1323 float anchorPosition = mActiveLayout->GetClosestAnchorPosition( GetCurrentLayoutPosition(0) );
1325 anchoringAnimation = Animation::New(mAnchoringDuration);
1326 anchoringAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), anchorPosition, AlphaFunction::EASE_OUT );
1327 anchoringAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::SCROLL_SPEED), 0.0f, AlphaFunction::EASE_OUT );
1330 AnimateScrollOvershoot(0.0f);
1334 return anchoringAnimation;
1337 void ItemView::OnScrollFinished(Animation& source)
1339 Actor self = Self();
1341 RemoveAnimation(mScrollAnimation); // mScrollAnimation is used to query whether we're scrolling
1343 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1345 if(mIsFlicking && fabsf(mScrollOvershoot) > Math::MACHINE_EPSILON_1)
1347 AnimateScrollOvershoot( mScrollOvershoot > 0.0f ? 1.0f : -1.0f, true);
1351 // Reset the overshoot
1352 AnimateScrollOvershoot( 0.0f );
1354 mIsFlicking = false;
1356 mScrollOvershoot = 0.0f;
1359 void ItemView::OnLayoutActivationScrollFinished(Animation& source)
1361 RemoveAnimation(mScrollAnimation);
1362 mRefreshEnabled = true;
1363 DoRefresh(GetCurrentLayoutPosition(0), true);
1365 // Emit the layout activated signal
1366 mLayoutActivatedSignal.Emit();
1369 void ItemView::OnOvershootOnFinished(Animation& animation)
1371 mAnimatingOvershootOn = false;
1372 mScrollOvershootAnimation.FinishedSignal().Disconnect(this, &ItemView::OnOvershootOnFinished);
1373 RemoveAnimation(mScrollOvershootAnimation);
1374 if(mAnimateOvershootOff)
1376 AnimateScrollOvershoot(0.0f);
1378 mInAnimation = false;
1381 void ItemView::ScrollToItem(unsigned int itemId, float durationSeconds)
1383 Actor self = Self();
1384 const Vector3 layoutSize = Self().GetCurrentSize();
1385 float firstItemScrollPosition = ClampFirstItemPosition(mActiveLayout->GetItemScrollToPosition(itemId), layoutSize, *mActiveLayout);
1387 if(durationSeconds > 0.0f)
1389 RemoveAnimation(mScrollAnimation);
1390 mScrollAnimation = Animation::New(durationSeconds);
1391 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, mScrollToAlphaFunction );
1392 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1393 mScrollAnimation.Play();
1397 self.SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1398 AnimateScrollOvershoot(0.0f);
1401 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1402 mRefreshEnabled = true;
1405 void ItemView::RemoveAnimation(Animation& animation)
1409 // Cease animating, and reset handle.
1415 void ItemView::CalculateDomainSize(const Vector3& layoutSize)
1417 Actor self = Self();
1419 Vector3 firstItemPosition(Vector3::ZERO);
1420 Vector3 lastItemPosition(Vector3::ZERO);
1424 firstItemPosition = mActiveLayout->GetItemPosition( 0,0,layoutSize );
1426 float minLayoutPosition = mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), layoutSize);
1427 lastItemPosition = mActiveLayout->GetItemPosition( fabs(minLayoutPosition),fabs(minLayoutPosition),layoutSize );
1431 if(IsHorizontal(mActiveLayout->GetOrientation()))
1433 domainSize = fabs(firstItemPosition.x - lastItemPosition.x);
1437 domainSize = fabs(firstItemPosition.y - lastItemPosition.y);
1440 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MIN, Vector2::ZERO);
1441 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1443 self.SetProperty(Toolkit::ItemView::Property::SCROLL_CONTENT_SIZE, domainSize);
1445 bool isLayoutScrollable = IsLayoutScrollable(layoutSize);
1446 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL, isLayoutScrollable);
1447 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL, false);
1451 bool ItemView::IsLayoutScrollable(const Vector3& layoutSize)
1453 Actor self = Self();
1455 float currentLayoutPosition = ClampFirstItemPosition( GetCurrentLayoutPosition(0), layoutSize, *mActiveLayout, false );
1456 float forwardClampedPosition = ClampFirstItemPosition( currentLayoutPosition + 1.0, layoutSize, *mActiveLayout, false );
1457 float backwardClampedPosition = ClampFirstItemPosition( currentLayoutPosition - 1.0, layoutSize, *mActiveLayout, false );
1459 return (fabs(forwardClampedPosition - backwardClampedPosition) > Math::MACHINE_EPSILON_0);
1462 float ItemView::GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const
1464 Vector3 firstItemPosition( mActiveLayout->GetItemPosition(0, layoutPosition, layoutSize ) );
1465 return IsHorizontal(mActiveLayout->GetOrientation()) ? firstItemPosition.x: firstItemPosition.y;
1468 Vector2 ItemView::GetCurrentScrollPosition() const
1470 return Vector2(0.0f, GetScrollPosition(GetCurrentLayoutPosition(0), Self().GetCurrentSize()));
1473 void ItemView::AddOverlay(Actor actor)
1475 actor.SetDrawMode( DrawMode::OVERLAY_2D );
1479 void ItemView::RemoveOverlay(Actor actor)
1481 Self().Remove(actor);
1484 void ItemView::ScrollTo(const Vector2& position, float duration)
1486 Actor self = Self();
1487 const Vector3 layoutSize = Self().GetCurrentSize();
1489 float firstItemScrollPosition = ClampFirstItemPosition(position.y, layoutSize, *mActiveLayout);
1493 RemoveAnimation(mScrollAnimation);
1494 mScrollAnimation = Animation::New(duration);
1495 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, mScrollToAlphaFunction );
1496 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1497 mScrollAnimation.Play();
1501 self.SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1502 AnimateScrollOvershoot(0.0f);
1505 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1506 mRefreshEnabled = true;
1509 void ItemView::SetOvershootEffectColor( const Vector4& color )
1511 mOvershootEffectColor = color;
1512 if( mOvershootOverlay )
1514 mOvershootOverlay.SetColor( color );
1518 void ItemView::EnableScrollOvershoot( bool enable )
1520 Actor self = Self();
1523 if( !mOvershootOverlay )
1525 Property::Index effectOvershootPropertyIndex = Property::INVALID_INDEX;
1526 mOvershootOverlay = CreateBouncingEffectActor( effectOvershootPropertyIndex );
1527 mOvershootOverlay.SetColor(mOvershootEffectColor);
1528 mOvershootOverlay.SetParentOrigin(ParentOrigin::TOP_LEFT);
1529 mOvershootOverlay.SetAnchorPoint(AnchorPoint::TOP_LEFT);
1530 mOvershootOverlay.SetDrawMode( DrawMode::OVERLAY_2D );
1531 self.Add(mOvershootOverlay);
1533 Constraint constraint = Constraint::New<Vector3>( mOvershootOverlay, Actor::Property::SIZE, OvershootOverlaySizeConstraint(mOvershootSize.height) );
1534 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1535 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1536 constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
1539 mOvershootOverlay.SetSize(mOvershootSize.width, mOvershootSize.height);
1541 constraint = Constraint::New<Quaternion>( mOvershootOverlay, Actor::Property::ORIENTATION, OvershootOverlayRotationConstraint );
1542 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1543 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1544 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1547 constraint = Constraint::New<Vector3>( mOvershootOverlay, Actor::Property::POSITION, OvershootOverlayPositionConstraint );
1548 constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
1549 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1550 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1551 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1554 constraint = Constraint::New<bool>( mOvershootOverlay, Actor::Property::VISIBLE, OvershootOverlayVisibilityConstraint );
1555 constraint.AddSource( ParentSource( Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL ) );
1558 constraint = Constraint::New<float>( mOvershootOverlay, effectOvershootPropertyIndex, EqualToConstraint() );
1559 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1565 if( mOvershootOverlay )
1567 self.Remove(mOvershootOverlay);
1568 mOvershootOverlay.Reset();
1573 float ItemView::CalculateScrollOvershoot()
1575 float overshoot = 0.0f;
1579 // The overshoot must be calculated from the accumulated pan gesture displacement
1580 // since the pan gesture starts.
1581 Actor self = Self();
1582 float scrollDistance = CalculateScrollDistance(mTotalPanDisplacement, *mActiveLayout) * mActiveLayout->GetScrollSpeedFactor();
1583 float positionDelta = GetCurrentLayoutPosition(0) + scrollDistance;
1584 float minLayoutPosition = mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), Self().GetCurrentSize());
1585 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1586 float clamppedPosition = std::min(0.0f, std::max(minLayoutPosition, positionDelta));
1587 overshoot = positionDelta - clamppedPosition;
1590 return overshoot > 0.0f ? std::min(overshoot, 1.0f) : std::max(overshoot, -1.0f);
1593 void ItemView::AnimateScrollOvershoot(float overshootAmount, bool animateBack)
1595 bool animatingOn = fabsf(overshootAmount) > Math::MACHINE_EPSILON_1;
1597 // make sure we animate back if needed
1598 mAnimateOvershootOff = animateBack || (!animatingOn && mAnimatingOvershootOn);
1600 if( mAnimatingOvershootOn )
1602 // animating on, do not allow animate off
1606 Actor self = Self();
1608 if(mOvershootAnimationSpeed > Math::MACHINE_EPSILON_0)
1610 float currentOvershoot = self.GetProperty<float>(Toolkit::ItemView::Property::OVERSHOOT);
1611 float duration = 0.0f;
1613 if (mOvershootOverlay)
1615 duration = mOvershootOverlay.GetCurrentSize().height * (animatingOn ? (1.0f - fabsf(currentOvershoot)) : fabsf(currentOvershoot)) / mOvershootAnimationSpeed;
1618 // Mark the animation as in progress to prevent manual property sets overwriting it.
1619 mInAnimation = true;
1620 mAnimatingOvershootOn = animatingOn;
1621 RemoveAnimation(mScrollOvershootAnimation);
1622 mScrollOvershootAnimation = Animation::New(duration);
1623 mScrollOvershootAnimation.FinishedSignal().Connect(this, &ItemView::OnOvershootOnFinished);
1624 mScrollOvershootAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::OVERSHOOT), overshootAmount, TimePeriod(0.0f, duration) );
1625 mScrollOvershootAnimation.Play();
1629 self.SetProperty( Toolkit::ItemView::Property::OVERSHOOT, overshootAmount );
1633 void ItemView::SetItemsParentOrigin( const Vector3& parentOrigin )
1635 if( parentOrigin != mItemsParentOrigin )
1637 mItemsParentOrigin = parentOrigin;
1638 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1640 iter->second.SetParentOrigin(parentOrigin);
1645 Vector3 ItemView::GetItemsParentOrigin() const
1647 return mItemsParentOrigin;
1650 void ItemView::SetItemsAnchorPoint( const Vector3& anchorPoint )
1652 if( anchorPoint != mItemsAnchorPoint )
1654 mItemsAnchorPoint = anchorPoint;
1655 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1657 iter->second.SetAnchorPoint(anchorPoint);
1662 Vector3 ItemView::GetItemsAnchorPoint() const
1664 return mItemsAnchorPoint;
1667 void ItemView::GetItemsRange(ItemRange& range)
1669 if( !mItemPool.empty() )
1671 range.begin = mItemPool.begin()->first;
1672 range.end = mItemPool.rbegin()->first + 1;
1681 bool ItemView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1683 Dali::BaseHandle handle( object );
1685 bool connected( true );
1686 Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( handle );
1688 if( 0 == strcmp( signalName.c_str(), LAYOUT_ACTIVATED_SIGNAL ) )
1690 itemView.LayoutActivatedSignal().Connect( tracker, functor );
1694 // signalName does not match any signal
1701 void ItemView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
1703 Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( Dali::BaseHandle( object ) );
1707 ItemView& itemViewImpl( GetImpl( itemView ) );
1710 case Toolkit::ItemView::Property::MINIMUM_SWIPE_SPEED:
1712 itemViewImpl.SetMinimumSwipeSpeed( value.Get<float>() );
1715 case Toolkit::ItemView::Property::MINIMUM_SWIPE_DISTANCE:
1717 itemViewImpl.SetMinimumSwipeDistance( value.Get<float>() );
1720 case Toolkit::ItemView::Property::WHEEL_SCROLL_DISTANCE_STEP:
1722 itemViewImpl.SetWheelScrollDistanceStep( value.Get<float>() );
1725 case Toolkit::ItemView::Property::SNAP_TO_ITEM_ENABLED:
1727 itemViewImpl.SetAnchoring( value.Get<bool>() );
1730 case Toolkit::ItemView::Property::REFRESH_INTERVAL:
1732 itemViewImpl.SetRefreshInterval( value.Get<float>() );
1739 Property::Value ItemView::GetProperty( BaseObject* object, Property::Index index )
1741 Property::Value value;
1743 Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( Dali::BaseHandle( object ) );
1747 ItemView& itemViewImpl( GetImpl( itemView ) );
1750 case Toolkit::ItemView::Property::MINIMUM_SWIPE_SPEED:
1752 value = itemViewImpl.GetMinimumSwipeSpeed();
1755 case Toolkit::ItemView::Property::MINIMUM_SWIPE_DISTANCE:
1757 value = itemViewImpl.GetMinimumSwipeDistance();
1760 case Toolkit::ItemView::Property::WHEEL_SCROLL_DISTANCE_STEP:
1762 value = itemViewImpl.GetWheelScrollDistanceStep();
1765 case Toolkit::ItemView::Property::SNAP_TO_ITEM_ENABLED:
1767 value = itemViewImpl.GetAnchoring();
1770 case Toolkit::ItemView::Property::REFRESH_INTERVAL:
1772 value = itemViewImpl.GetRefreshInterval();
1781 bool ItemView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
1783 Dali::BaseHandle handle( object );
1785 Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( handle );
1787 DALI_ASSERT_ALWAYS( itemView );
1789 if( 0 == strcmp( actionName.c_str(), ACTION_STOP_SCROLLING ) )
1791 GetImpl( itemView ).DoStopScrolling();
1793 else if ( 0 == strcmp( actionName.c_str(), ACTION_ENABLE_REFRESH ) )
1795 GetImpl( itemView ).SetRefreshNotificationEnabled( true );
1797 else if ( 0 == strcmp( actionName.c_str(), ACTION_DISABLE_REFRESH ) )
1799 GetImpl( itemView ).SetRefreshNotificationEnabled( false );
1805 void ItemView::DoStopScrolling()
1807 if( mScrollAnimation )
1809 mScrollAnimation.Stop();
1810 mScrollAnimation.Reset();
1814 void ItemView::SetRefreshNotificationEnabled( bool enabled )
1816 mRefreshNotificationEnabled = enabled;
1819 } // namespace Internal
1821 } // namespace Toolkit