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_TYPE_REGISTRATION_END()
267 bool FindById( const ItemContainer& items, ItemId id )
269 for( ConstItemIter iter = items.begin(); items.end() != iter; ++iter )
271 if( iter->first == id )
280 } // unnamed namespace
282 Dali::Toolkit::ItemView ItemView::New(ItemFactory& factory)
284 // Create the implementation
285 ItemViewPtr itemView(new ItemView(factory));
287 // Pass ownership to CustomActor via derived handle
288 Dali::Toolkit::ItemView handle(*itemView);
290 // Second-phase init of the implementation
291 // This can only be done after the CustomActor connection has been made...
292 itemView->Initialize();
297 ItemView::ItemView(ItemFactory& factory)
298 : Scrollable( ControlBehaviour( DISABLE_SIZE_NEGOTIATION | REQUIRES_WHEEL_EVENTS | REQUIRES_KEYBOARD_NAVIGATION_SUPPORT ) ),
299 mItemFactory(factory),
300 mItemsParentOrigin(ParentOrigin::CENTER),
301 mItemsAnchorPoint(AnchorPoint::CENTER),
302 mTotalPanDisplacement(Vector2::ZERO),
304 mAnchoringDuration(DEFAULT_ANCHORING_DURATION),
305 mRefreshIntervalLayoutPositions(0.0f),
306 mMinimumSwipeSpeed(DEFAULT_MINIMUM_SWIPE_SPEED),
307 mMinimumSwipeDistance(DEFAULT_MINIMUM_SWIPE_DISTANCE),
308 mWheelScrollDistanceStep(0.0f),
309 mScrollDistance(0.0f),
311 mScrollOvershoot(0.0f),
312 mGestureState(Gesture::Clear),
313 mAnimatingOvershootOn(false),
314 mAnimateOvershootOff(false),
315 mAnchoringEnabled(false),
316 mRefreshOrderHint(true/*Refresh item 0 first*/),
319 mRefreshEnabled(true),
324 void ItemView::OnInitialize()
328 Vector2 stageSize = Stage::GetCurrent().GetSize();
329 mWheelScrollDistanceStep = stageSize.y * DEFAULT_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION;
331 self.TouchSignal().Connect( this, &ItemView::OnTouch );
332 EnableGestureDetection(Gesture::Type(Gesture::Pan));
334 mWheelEventFinishedTimer = Timer::New( WHEEL_EVENT_FINISHED_TIME_OUT );
335 mWheelEventFinishedTimer.TickSignal().Connect( this, &ItemView::OnWheelEventFinished );
337 SetRefreshInterval(DEFAULT_REFRESH_INTERVAL_LAYOUT_POSITIONS);
340 ItemView::~ItemView()
344 unsigned int ItemView::GetLayoutCount() const
346 return mLayouts.size();
349 void ItemView::AddLayout(ItemLayout& layout)
351 mLayouts.push_back(ItemLayoutPtr(&layout));
354 void ItemView::RemoveLayout(unsigned int layoutIndex)
356 DALI_ASSERT_ALWAYS(layoutIndex < mLayouts.size());
358 if (mActiveLayout == mLayouts[layoutIndex].Get())
360 mActiveLayout = NULL;
363 mLayouts.erase(mLayouts.begin() + layoutIndex);
366 ItemLayoutPtr ItemView::GetLayout(unsigned int layoutIndex) const
368 return mLayouts[layoutIndex];
371 ItemLayoutPtr ItemView::GetActiveLayout() const
373 return ItemLayoutPtr(mActiveLayout);
376 float ItemView::GetCurrentLayoutPosition(unsigned int itemId) const
378 return Self().GetProperty<float>( Toolkit::ItemView::Property::LAYOUT_POSITION ) + static_cast<float>( itemId );
381 void ItemView::ActivateLayout(unsigned int layoutIndex, const Vector3& targetSize, float durationSeconds)
383 DALI_ASSERT_ALWAYS(layoutIndex < mLayouts.size());
385 mRefreshEnabled = false;
389 // The ItemView size should match the active layout size
390 self.SetSize(targetSize);
391 mActiveLayoutTargetSize = targetSize;
393 // Switch to the new layout
394 mActiveLayout = mLayouts[layoutIndex].Get();
396 // Move the items to the new layout positions...
398 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
400 unsigned int itemId = iter->first;
401 Actor actor = iter->second;
403 // Remove constraints from previous layout
404 actor.RemoveConstraints();
407 mActiveLayout->GetItemSize( itemId, targetSize, size );
408 actor.SetSize( size.GetVectorXY() );
410 mActiveLayout->ApplyConstraints(actor, itemId, targetSize, Self() );
413 // Refresh the new layout
414 ItemRange range = GetItemRange(*mActiveLayout, targetSize, GetCurrentLayoutPosition(0), false/* don't reserve extra*/);
415 AddActorsWithinRange( range, targetSize );
417 // Scroll to an appropriate layout position
419 bool scrollAnimationNeeded(false);
420 float firstItemScrollPosition(0.0f);
422 float current = GetCurrentLayoutPosition(0);
423 float minimum = ClampFirstItemPosition(current, targetSize, *mActiveLayout);
425 if (current < minimum)
427 scrollAnimationNeeded = true;
428 firstItemScrollPosition = minimum;
430 else if (mAnchoringEnabled)
432 scrollAnimationNeeded = true;
433 firstItemScrollPosition = mActiveLayout->GetClosestAnchorPosition(current);
436 if (scrollAnimationNeeded)
438 RemoveAnimation(mScrollAnimation);
439 mScrollAnimation = Animation::New(durationSeconds);
440 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, AlphaFunction::EASE_OUT );
441 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnLayoutActivationScrollFinished);
442 mScrollAnimation.Play();
446 // Emit the layout activated signal
447 mLayoutActivatedSignal.Emit();
450 AnimateScrollOvershoot(0.0f);
451 mScrollOvershoot = 0.0f;
453 Radian scrollDirection(mActiveLayout->GetScrollDirection());
454 self.SetProperty(Toolkit::ItemView::Property::SCROLL_DIRECTION, Vector2(sinf(scrollDirection), cosf(scrollDirection)));
455 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_ORIENTATION, static_cast<int>(mActiveLayout->GetOrientation()));
456 self.SetProperty(Toolkit::ItemView::Property::SCROLL_SPEED, mScrollSpeed);
458 CalculateDomainSize(targetSize);
461 void ItemView::DeactivateCurrentLayout()
465 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
467 Actor actor = iter->second;
468 actor.RemoveConstraints();
471 mActiveLayout = NULL;
475 void ItemView::OnRefreshNotification(PropertyNotification& source)
477 // Cancel scroll animation to prevent any fighting of setting the scroll position property by scroll bar during fast scroll.
478 if(!mRefreshEnabled && mScrollAnimation)
480 RemoveAnimation(mScrollAnimation);
483 // Only cache extra items when it is not a fast scroll
484 DoRefresh(GetCurrentLayoutPosition(0), mRefreshEnabled || mScrollAnimation);
487 void ItemView::Refresh()
489 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter )
491 ReleaseActor( iter->first, iter->second );
495 DoRefresh(GetCurrentLayoutPosition(0), true);
498 void ItemView::DoRefresh(float currentLayoutPosition, bool cacheExtra)
502 ItemRange range = GetItemRange(*mActiveLayout, mActiveLayoutTargetSize, currentLayoutPosition, cacheExtra/*reserve extra*/);
503 RemoveActorsOutsideRange( range );
504 AddActorsWithinRange( range, Self().GetCurrentSize() );
506 mScrollUpdatedSignal.Emit( Vector2(0.0f, currentLayoutPosition) );
510 void ItemView::SetMinimumSwipeSpeed(float speed)
512 mMinimumSwipeSpeed = speed;
515 float ItemView::GetMinimumSwipeSpeed() const
517 return mMinimumSwipeSpeed;
520 void ItemView::SetMinimumSwipeDistance(float distance)
522 mMinimumSwipeDistance = distance;
525 float ItemView::GetMinimumSwipeDistance() const
527 return mMinimumSwipeDistance;
530 void ItemView::SetWheelScrollDistanceStep(float step)
532 mWheelScrollDistanceStep = step;
535 float ItemView::GetWheelScrollDistanceStep() const
537 return mWheelScrollDistanceStep;
540 void ItemView::SetAnchoring(bool enabled)
542 mAnchoringEnabled = enabled;
545 bool ItemView::GetAnchoring() const
547 return mAnchoringEnabled;
550 void ItemView::SetAnchoringDuration(float durationSeconds)
552 mAnchoringDuration = durationSeconds;
555 float ItemView::GetAnchoringDuration() const
557 return mAnchoringDuration;
560 void ItemView::SetRefreshInterval(float intervalLayoutPositions)
562 if( !Equals(mRefreshIntervalLayoutPositions, intervalLayoutPositions) )
564 mRefreshIntervalLayoutPositions = intervalLayoutPositions;
567 if(mRefreshNotification)
569 self.RemovePropertyNotification(mRefreshNotification);
571 mRefreshNotification = self.AddPropertyNotification( Toolkit::ItemView::Property::LAYOUT_POSITION, StepCondition(mRefreshIntervalLayoutPositions, 0.0f) );
572 mRefreshNotification.NotifySignal().Connect( this, &ItemView::OnRefreshNotification );
576 float ItemView::GetRefreshInterval() const
578 return mRefreshIntervalLayoutPositions;
581 void ItemView::SetRefreshEnabled(bool enabled)
583 mRefreshEnabled = enabled;
586 Actor ItemView::GetItem(unsigned int itemId) const
590 ConstItemPoolIter iter = mItemPool.find( itemId );
591 if( iter != mItemPool.end() )
593 actor = iter->second;
599 unsigned int ItemView::GetItemId( Actor actor ) const
601 unsigned int itemId( 0 );
603 for ( ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter )
605 if( iter->second == actor )
607 itemId = iter->first;
615 void ItemView::InsertItem( Item newItem, float durationSeconds )
618 Vector3 layoutSize = Self().GetCurrentSize();
620 Actor displacedActor;
621 ItemPoolIter afterDisplacedIter = mItemPool.end();
623 ItemPoolIter foundIter = mItemPool.find( newItem.first );
624 if( mItemPool.end() != foundIter )
626 SetupActor( newItem, layoutSize );
627 Self().Add( newItem.second );
629 displacedActor = foundIter->second;
630 foundIter->second = newItem.second;
632 afterDisplacedIter = ++foundIter;
636 // Inserting before the existing item range?
637 ItemPoolIter iter = mItemPool.begin();
638 if( iter != mItemPool.end() &&
639 iter->first > newItem.first )
641 displacedActor = iter->second;
642 mItemPool.erase( iter++ ); // iter is still valid after the erase
644 afterDisplacedIter = iter;
650 // Move the existing actors to make room
651 for( ItemPoolIter iter = afterDisplacedIter; mItemPool.end() != iter; ++iter )
653 Actor temp = iter->second;
654 iter->second = displacedActor;
655 displacedActor = temp;
657 iter->second.RemoveConstraints();
658 mActiveLayout->ApplyConstraints( iter->second, iter->first, layoutSize, Self() );
662 ItemPool::reverse_iterator lastIter = mItemPool.rbegin();
663 if ( lastIter != mItemPool.rend() )
665 ItemId lastId = lastIter->first;
666 Item lastItem( lastId + 1, displacedActor );
667 mItemPool.insert( lastItem );
669 lastItem.second.RemoveConstraints();
670 mActiveLayout->ApplyConstraints( lastItem.second, lastItem.first, layoutSize, Self() );
674 CalculateDomainSize( layoutSize );
676 mAddingItems = false;
679 void ItemView::InsertItems( const ItemContainer& newItems, float durationSeconds )
682 Vector3 layoutSize = Self().GetCurrentSize();
684 // Insert from lowest id to highest
685 std::set<Item> sortedItems;
686 for( ConstItemIter iter = newItems.begin(); newItems.end() != iter; ++iter )
688 sortedItems.insert( *iter );
691 for( std::set<Item>::iterator iter = sortedItems.begin(); sortedItems.end() != iter; ++iter )
693 Self().Add( iter->second );
695 ItemPoolIter foundIter = mItemPool.find( iter->first );
696 if( mItemPool.end() != foundIter )
698 Actor moveMe = foundIter->second;
699 foundIter->second = iter->second;
701 // Move the existing actors to make room
702 for( ItemPoolIter iter = ++foundIter; mItemPool.end() != iter; ++iter )
704 Actor temp = iter->second;
705 iter->second = moveMe;
710 ItemId lastId = mItemPool.rbegin()->first;
711 Item lastItem( lastId + 1, moveMe );
712 mItemPool.insert( lastItem );
716 mItemPool.insert( *iter );
720 // Relayout everything
721 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
724 if( FindById( newItems, iter->first ) )
726 SetupActor( *iter, layoutSize );
730 iter->second.RemoveConstraints();
731 mActiveLayout->ApplyConstraints( iter->second, iter->first, layoutSize, Self() );
735 CalculateDomainSize( layoutSize );
737 mAddingItems = false;
740 void ItemView::RemoveItem( unsigned int itemId, float durationSeconds )
742 bool actorsReordered = RemoveActor( itemId );
743 if( actorsReordered )
745 ReapplyAllConstraints();
751 void ItemView::RemoveItems( const ItemIdContainer& itemIds, float durationSeconds )
753 bool actorsReordered( false );
755 // Remove from highest id to lowest
756 set<ItemId> sortedItems;
757 for( ConstItemIdIter iter = itemIds.begin(); itemIds.end() != iter; ++iter )
759 sortedItems.insert( *iter );
762 for( set<ItemId>::reverse_iterator iter = sortedItems.rbegin(); sortedItems.rend() != iter; ++iter )
764 if( RemoveActor( *iter ) )
766 actorsReordered = true;
770 if( actorsReordered )
772 ReapplyAllConstraints();
778 bool ItemView::RemoveActor(unsigned int itemId)
780 bool reordered( false );
782 ItemPoolIter removeIter = mItemPool.find( itemId );
783 if( removeIter != mItemPool.end() )
785 ReleaseActor(itemId, removeIter->second);
789 // Removing before the existing item range?
790 ItemPoolIter iter = mItemPool.begin();
791 if( iter != mItemPool.end() &&
792 iter->first > itemId )
794 // In order to decrement the first visible item ID
795 mItemPool.insert( Item(iter->first - 1, Actor()) );
797 removeIter = mItemPool.begin();
801 if( removeIter != mItemPool.end() )
805 // Adjust the remaining item IDs, for example if item 2 is removed:
806 // Initial actors: After insert:
807 // ID 1 - ActorA ID 1 - ActorA
808 // ID 2 - ActorB ID 2 - ActorC (previously ID 3)
809 // ID 3 - ActorC ID 3 - ActorB (previously ID 4)
811 for (ItemPoolIter iter = removeIter; iter != mItemPool.end(); ++iter)
813 if( iter->first < mItemPool.rbegin()->first )
815 iter->second = mItemPool[ iter->first + 1 ];
819 mItemPool.erase( iter );
828 void ItemView::ReplaceItem( Item replacementItem, float durationSeconds )
831 Vector3 layoutSize = Self().GetCurrentSize();
833 SetupActor( replacementItem, layoutSize );
834 Self().Add( replacementItem.second );
836 const ItemPoolIter iter = mItemPool.find( replacementItem.first );
837 if( mItemPool.end() != iter )
839 ReleaseActor(iter->first, iter->second);
840 iter->second = replacementItem.second;
844 mItemPool.insert( replacementItem );
847 CalculateDomainSize( layoutSize );
849 mAddingItems = false;
852 void ItemView::ReplaceItems( const ItemContainer& replacementItems, float durationSeconds )
854 for( ConstItemIter iter = replacementItems.begin(); replacementItems.end() != iter; ++iter )
856 ReplaceItem( *iter, durationSeconds );
860 void ItemView::RemoveActorsOutsideRange( ItemRange range )
862 // Remove unwanted actors from the ItemView & ItemPool
863 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); )
865 unsigned int current = iter->first;
867 if( ! range.Within( current ) )
869 ReleaseActor(iter->first, iter->second);
871 mItemPool.erase( iter++ ); // erase invalidates the return value of post-increment; iter remains valid
880 void ItemView::AddActorsWithinRange( ItemRange range, const Vector3& layoutSize )
882 range.end = std::min(mItemFactory.GetNumberOfItems(), range.end);
884 // The order of addition depends on the scroll direction.
885 if (mRefreshOrderHint)
887 for (unsigned int itemId = range.begin; itemId < range.end; ++itemId)
889 AddNewActor( itemId, layoutSize );
894 for (unsigned int itemId = range.end; itemId > range.begin; --itemId)
896 AddNewActor( itemId-1, layoutSize );
900 // Total number of items may change dynamically.
901 // Always recalculate the domain size to reflect that.
902 CalculateDomainSize(Self().GetCurrentSize());
905 void ItemView::AddNewActor( unsigned int itemId, const Vector3& layoutSize )
909 if( mItemPool.end() == mItemPool.find( itemId ) )
911 Actor actor = mItemFactory.NewItem( itemId );
915 Item newItem( itemId, actor );
917 mItemPool.insert( newItem );
919 SetupActor( newItem, layoutSize );
924 mAddingItems = false;
927 void ItemView::SetupActor( Item item, const Vector3& layoutSize )
929 item.second.SetParentOrigin( mItemsParentOrigin );
930 item.second.SetAnchorPoint( mItemsAnchorPoint );
935 mActiveLayout->GetItemSize( item.first, mActiveLayoutTargetSize, size );
936 item.second.SetSize( size.GetVectorXY() );
938 mActiveLayout->ApplyConstraints( item.second, item.first, layoutSize, Self() );
942 void ItemView::ReleaseActor( ItemId item, Actor actor )
944 Self().Remove( actor );
945 mItemFactory.ItemReleased(item, actor);
948 ItemRange ItemView::GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra)
950 unsigned int itemCount = mItemFactory.GetNumberOfItems();
952 ItemRange available(0u, itemCount);
954 ItemRange range = layout.GetItemsWithinArea( layoutPosition, layoutSize );
958 // Add the reserve items for scrolling
959 unsigned int extra = layout.GetReserveItemCount(layoutSize);
960 range.begin = (range.begin >= extra) ? (range.begin - extra) : 0u;
964 return range.Intersection(available);
967 void ItemView::OnChildAdd(Actor& child)
971 // We don't want to do this downcast check for any item added by ItemView itself.
972 Dali::Toolkit::ScrollBar scrollBar = Dali::Toolkit::ScrollBar::DownCast(child);
975 scrollBar.SetScrollPropertySource(Self(),
976 Toolkit::ItemView::Property::LAYOUT_POSITION,
977 Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_Y,
978 Toolkit::Scrollable::Property::SCROLL_POSITION_MAX_Y,
979 Toolkit::ItemView::Property::SCROLL_CONTENT_SIZE);
984 bool ItemView::OnWheelEvent(const WheelEvent& event)
986 // Respond the wheel event to scroll
990 const Vector3 layoutSize = Self().GetCurrentSize();
991 float layoutPositionDelta = GetCurrentLayoutPosition(0) - (event.z * mWheelScrollDistanceStep * mActiveLayout->GetScrollSpeedFactor());
992 float firstItemScrollPosition = ClampFirstItemPosition(layoutPositionDelta, layoutSize, *mActiveLayout);
994 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
996 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
997 mRefreshEnabled = true;
1000 if (mWheelEventFinishedTimer.IsRunning())
1002 mWheelEventFinishedTimer.Stop();
1005 mWheelEventFinishedTimer.Start();
1010 bool ItemView::OnWheelEventFinished()
1014 RemoveAnimation(mScrollAnimation);
1016 // No more wheel events coming. Do the anchoring if enabled.
1017 mScrollAnimation = DoAnchoring();
1018 if (mScrollAnimation)
1020 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1021 mScrollAnimation.Play();
1025 mScrollOvershoot = 0.0f;
1026 AnimateScrollOvershoot(0.0f);
1028 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1035 void ItemView::ReapplyAllConstraints()
1037 Vector3 layoutSize = Self().GetCurrentSize();
1039 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1041 unsigned int id = iter->first;
1042 Actor actor = iter->second;
1044 actor.RemoveConstraints();
1045 mActiveLayout->ApplyConstraints(actor, id, layoutSize, Self());
1049 void ItemView::OnItemsRemoved()
1051 CalculateDomainSize(Self().GetCurrentSize());
1053 // Adjust scroll-position after an item is removed
1056 float firstItemScrollPosition = ClampFirstItemPosition(GetCurrentLayoutPosition(0), Self().GetCurrentSize(), *mActiveLayout);
1057 Self().SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1061 float ItemView::ClampFirstItemPosition( float targetPosition, const Vector3& targetSize, ItemLayout& layout, bool updateOvershoot )
1063 Actor self = Self();
1064 float minLayoutPosition = layout.GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), targetSize);
1065 float clamppedPosition = std::min(0.0f, std::max(minLayoutPosition, targetPosition));
1066 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1068 if( updateOvershoot )
1070 mScrollOvershoot = targetPosition - clamppedPosition;
1073 return clamppedPosition;
1076 bool ItemView::OnTouch( Actor actor, const TouchData& touch )
1078 // Ignore events with multiple-touch points
1079 if (touch.GetPointCount() != 1)
1084 if ( touch.GetState( 0 ) == PointState::DOWN )
1086 // Cancel ongoing scrolling etc.
1087 mGestureState = Gesture::Clear;
1089 mScrollDistance = 0.0f;
1090 mScrollSpeed = 0.0f;
1091 Self().SetProperty(Toolkit::ItemView::Property::SCROLL_SPEED, mScrollSpeed);
1093 mScrollOvershoot = 0.0f;
1094 AnimateScrollOvershoot(0.0f);
1096 if(mScrollAnimation)
1098 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1101 RemoveAnimation(mScrollAnimation);
1104 return true; // consume since we're potentially scrolling
1107 void ItemView::OnPan( const PanGesture& gesture )
1109 Actor self = Self();
1110 const Vector3 layoutSize = Self().GetCurrentSize();
1112 RemoveAnimation(mScrollAnimation);
1114 // Short-circuit if there is no active layout
1117 mGestureState = Gesture::Clear;
1121 mGestureState = gesture.state;
1123 switch (mGestureState)
1125 case Gesture::Finished:
1128 if (fabsf(mScrollDistance) > mMinimumSwipeDistance &&
1129 mScrollSpeed > mMinimumSwipeSpeed)
1131 float direction = (mScrollDistance < 0.0f) ? -1.0f : 1.0f;
1133 mRefreshOrderHint = true;
1135 float currentLayoutPosition = GetCurrentLayoutPosition(0);
1136 float firstItemScrollPosition = ClampFirstItemPosition(currentLayoutPosition + mScrollSpeed * direction,
1140 if (mAnchoringEnabled)
1142 firstItemScrollPosition = mActiveLayout->GetClosestAnchorPosition(firstItemScrollPosition);
1145 RemoveAnimation(mScrollAnimation);
1147 float flickAnimationDuration = Clamp( mActiveLayout->GetItemFlickAnimationDuration() * std::max(1.0f, fabsf(firstItemScrollPosition - GetCurrentLayoutPosition(0)))
1148 , DEFAULT_MINIMUM_SWIPE_DURATION, DEFAULT_MAXIMUM_SWIPE_DURATION);
1150 mScrollAnimation = Animation::New(flickAnimationDuration);
1151 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION ), firstItemScrollPosition, AlphaFunction::EASE_OUT );
1152 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::SCROLL_SPEED), 0.0f, AlphaFunction::EASE_OUT );
1155 // Check whether it has already scrolled to the end
1156 if(fabs(currentLayoutPosition - firstItemScrollPosition) > Math::MACHINE_EPSILON_0)
1158 AnimateScrollOvershoot(0.0f);
1162 // Anchoring may be triggered when there was no swipe
1163 if (!mScrollAnimation)
1165 mScrollAnimation = DoAnchoring();
1168 // Reset the overshoot if no scroll animation.
1169 if (!mScrollAnimation)
1171 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1173 AnimateScrollOvershoot(0.0f, false);
1178 case Gesture::Started: // Fall through
1180 mTotalPanDisplacement = Vector2::ZERO;
1181 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1182 mRefreshEnabled = true;
1185 case Gesture::Continuing:
1187 mScrollDistance = CalculateScrollDistance(gesture.displacement, *mActiveLayout);
1188 mScrollSpeed = Clamp((gesture.GetSpeed() * gesture.GetSpeed() * mActiveLayout->GetFlickSpeedFactor() * MILLISECONDS_PER_SECONDS), 0.0f, mActiveLayout->GetMaximumSwipeSpeed());
1190 // Refresh order depends on the direction of the scroll; negative is towards the last item.
1191 mRefreshOrderHint = mScrollDistance < 0.0f;
1193 float layoutPositionDelta = GetCurrentLayoutPosition(0) + (mScrollDistance * mActiveLayout->GetScrollSpeedFactor());
1195 float firstItemScrollPosition = ClampFirstItemPosition(layoutPositionDelta, layoutSize, *mActiveLayout);
1197 float currentOvershoot = self.GetProperty<float>(Toolkit::ItemView::Property::OVERSHOOT);
1199 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1201 if( ( firstItemScrollPosition >= 0.0f &&
1202 currentOvershoot < 1.0f ) ||
1203 ( firstItemScrollPosition <= mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), layoutSize) &&
1204 currentOvershoot > -1.0f ) )
1206 mTotalPanDisplacement += gesture.displacement;
1209 mScrollOvershoot = CalculateScrollOvershoot();
1211 // If the view is moved in a direction against the overshoot indicator, then the indicator should be animated off.
1212 // First make sure we are not in an animation, otherwise a previously started
1213 // off-animation will be overwritten as the user continues scrolling.
1216 // Check if the movement is against the current overshoot amount (if we are currently displaying the indicator).
1217 if( ( ( mScrollOvershoot > Math::MACHINE_EPSILON_0 ) && ( mScrollDistance < -Math::MACHINE_EPSILON_0 ) ) ||
1218 ( ( mScrollOvershoot < Math::MACHINE_EPSILON_0 ) && ( mScrollDistance > Math::MACHINE_EPSILON_0 ) ) )
1220 // The user has moved against the indicator direction.
1221 // First, we reset the total displacement. This means the overshoot amount will become zero the next frame,
1222 // and if the user starts dragging in the overshoot direction again, the indicator will appear once more.
1223 mTotalPanDisplacement = Vector2::ZERO;
1224 // Animate the overshoot indicator off.
1225 AnimateScrollOvershoot( 0.0f, false );
1229 // Only set the property directly if we are not animating the overshoot away,
1230 // as otherwise this will overwrite the animation generated value.
1231 self.SetProperty( Toolkit::ItemView::Property::OVERSHOOT, mScrollOvershoot );
1237 case Gesture::Cancelled:
1239 mScrollAnimation = DoAnchoring();
1247 if (mScrollAnimation)
1249 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1250 mScrollAnimation.Play();
1254 bool ItemView::OnAccessibilityPan(PanGesture gesture)
1260 Actor ItemView::GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
1262 Actor nextFocusActor;
1266 if(!actor || actor == this->Self())
1268 nextFocusActor = GetItem(nextItemID);
1270 else if(actor && actor.GetParent() == this->Self())
1272 int itemID = GetItemId(actor);
1273 nextItemID = mActiveLayout->GetNextFocusItemID(itemID, mItemFactory.GetNumberOfItems(), direction, loopEnabled);
1274 nextFocusActor = GetItem(nextItemID);
1275 if(nextFocusActor == actor)
1277 // need to pass NULL actor back to focus manager
1278 nextFocusActor.Reset();
1279 return nextFocusActor;
1282 float layoutPosition = mActiveLayout->GetClosestAnchorPosition( GetCurrentLayoutPosition(0) );
1283 Vector3 layoutSize = Self().GetCurrentSize();
1286 // likely the current item is not buffered, so not in our item pool, probably best to get first viewable item
1287 ItemRange viewableItems = mActiveLayout->GetItemsWithinArea(layoutPosition, layoutSize);
1288 nextItemID = viewableItems.begin;
1289 nextFocusActor = GetItem(nextItemID);
1292 return nextFocusActor;
1295 void ItemView::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
1297 // only in this function if our chosen focus actor was actually used
1298 if(commitedFocusableActor)
1300 int nextItemID = GetItemId(commitedFocusableActor);
1301 float layoutPosition = GetCurrentLayoutPosition(0);
1302 Vector3 layoutSize = Self().GetCurrentSize();
1304 float scrollTo = mActiveLayout->GetClosestOnScreenLayoutPosition(nextItemID, layoutPosition, layoutSize);
1305 ScrollTo(Vector2(0.0f, scrollTo), DEFAULT_KEYBOARD_FOCUS_SCROLL_DURATION);
1309 Animation ItemView::DoAnchoring()
1311 Animation anchoringAnimation;
1312 Actor self = Self();
1314 if (mActiveLayout && mAnchoringEnabled)
1316 float anchorPosition = mActiveLayout->GetClosestAnchorPosition( GetCurrentLayoutPosition(0) );
1318 anchoringAnimation = Animation::New(mAnchoringDuration);
1319 anchoringAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), anchorPosition, AlphaFunction::EASE_OUT );
1320 anchoringAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::SCROLL_SPEED), 0.0f, AlphaFunction::EASE_OUT );
1323 AnimateScrollOvershoot(0.0f);
1327 return anchoringAnimation;
1330 void ItemView::OnScrollFinished(Animation& source)
1332 Actor self = Self();
1334 RemoveAnimation(mScrollAnimation); // mScrollAnimation is used to query whether we're scrolling
1336 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1338 if(mIsFlicking && fabsf(mScrollOvershoot) > Math::MACHINE_EPSILON_1)
1340 AnimateScrollOvershoot( mScrollOvershoot > 0.0f ? 1.0f : -1.0f, true);
1344 // Reset the overshoot
1345 AnimateScrollOvershoot( 0.0f );
1347 mIsFlicking = false;
1349 mScrollOvershoot = 0.0f;
1352 void ItemView::OnLayoutActivationScrollFinished(Animation& source)
1354 RemoveAnimation(mScrollAnimation);
1355 mRefreshEnabled = true;
1356 DoRefresh(GetCurrentLayoutPosition(0), true);
1358 // Emit the layout activated signal
1359 mLayoutActivatedSignal.Emit();
1362 void ItemView::OnOvershootOnFinished(Animation& animation)
1364 mAnimatingOvershootOn = false;
1365 mScrollOvershootAnimation.FinishedSignal().Disconnect(this, &ItemView::OnOvershootOnFinished);
1366 RemoveAnimation(mScrollOvershootAnimation);
1367 if(mAnimateOvershootOff)
1369 AnimateScrollOvershoot(0.0f);
1371 mInAnimation = false;
1374 void ItemView::ScrollToItem(unsigned int itemId, float durationSeconds)
1376 Actor self = Self();
1377 const Vector3 layoutSize = Self().GetCurrentSize();
1378 float firstItemScrollPosition = ClampFirstItemPosition(mActiveLayout->GetItemScrollToPosition(itemId), layoutSize, *mActiveLayout);
1380 if(durationSeconds > 0.0f)
1382 RemoveAnimation(mScrollAnimation);
1383 mScrollAnimation = Animation::New(durationSeconds);
1384 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, mScrollToAlphaFunction );
1385 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1386 mScrollAnimation.Play();
1390 self.SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1391 AnimateScrollOvershoot(0.0f);
1394 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1395 mRefreshEnabled = true;
1398 void ItemView::RemoveAnimation(Animation& animation)
1402 // Cease animating, and reset handle.
1408 void ItemView::CalculateDomainSize(const Vector3& layoutSize)
1410 Actor self = Self();
1412 Vector3 firstItemPosition(Vector3::ZERO);
1413 Vector3 lastItemPosition(Vector3::ZERO);
1417 firstItemPosition = mActiveLayout->GetItemPosition( 0,0,layoutSize );
1419 float minLayoutPosition = mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), layoutSize);
1420 lastItemPosition = mActiveLayout->GetItemPosition( fabs(minLayoutPosition),fabs(minLayoutPosition),layoutSize );
1424 if(IsHorizontal(mActiveLayout->GetOrientation()))
1426 domainSize = fabs(firstItemPosition.x - lastItemPosition.x);
1430 domainSize = fabs(firstItemPosition.y - lastItemPosition.y);
1433 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MIN, Vector2::ZERO);
1434 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1436 self.SetProperty(Toolkit::ItemView::Property::SCROLL_CONTENT_SIZE, domainSize);
1438 bool isLayoutScrollable = IsLayoutScrollable(layoutSize);
1439 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL, isLayoutScrollable);
1440 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL, false);
1444 bool ItemView::IsLayoutScrollable(const Vector3& layoutSize)
1446 Actor self = Self();
1448 float currentLayoutPosition = ClampFirstItemPosition( GetCurrentLayoutPosition(0), layoutSize, *mActiveLayout, false );
1449 float forwardClampedPosition = ClampFirstItemPosition( currentLayoutPosition + 1.0, layoutSize, *mActiveLayout, false );
1450 float backwardClampedPosition = ClampFirstItemPosition( currentLayoutPosition - 1.0, layoutSize, *mActiveLayout, false );
1452 return (fabs(forwardClampedPosition - backwardClampedPosition) > Math::MACHINE_EPSILON_0);
1455 float ItemView::GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const
1457 Vector3 firstItemPosition( mActiveLayout->GetItemPosition(0, layoutPosition, layoutSize ) );
1458 return IsHorizontal(mActiveLayout->GetOrientation()) ? firstItemPosition.x: firstItemPosition.y;
1461 Vector2 ItemView::GetCurrentScrollPosition() const
1463 return Vector2(0.0f, GetScrollPosition(GetCurrentLayoutPosition(0), Self().GetCurrentSize()));
1466 void ItemView::AddOverlay(Actor actor)
1468 actor.SetDrawMode( DrawMode::OVERLAY_2D );
1472 void ItemView::RemoveOverlay(Actor actor)
1474 Self().Remove(actor);
1477 void ItemView::ScrollTo(const Vector2& position, float duration)
1479 Actor self = Self();
1480 const Vector3 layoutSize = Self().GetCurrentSize();
1482 float firstItemScrollPosition = ClampFirstItemPosition(position.y, layoutSize, *mActiveLayout);
1486 RemoveAnimation(mScrollAnimation);
1487 mScrollAnimation = Animation::New(duration);
1488 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, mScrollToAlphaFunction );
1489 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1490 mScrollAnimation.Play();
1494 self.SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1495 AnimateScrollOvershoot(0.0f);
1498 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1499 mRefreshEnabled = true;
1502 void ItemView::SetOvershootEffectColor( const Vector4& color )
1504 mOvershootEffectColor = color;
1505 if( mOvershootOverlay )
1507 mOvershootOverlay.SetColor( color );
1511 void ItemView::EnableScrollOvershoot( bool enable )
1513 Actor self = Self();
1516 if( !mOvershootOverlay )
1518 Property::Index effectOvershootPropertyIndex = Property::INVALID_INDEX;
1519 mOvershootOverlay = CreateBouncingEffectActor( effectOvershootPropertyIndex );
1520 mOvershootOverlay.SetColor(mOvershootEffectColor);
1521 mOvershootOverlay.SetParentOrigin(ParentOrigin::TOP_LEFT);
1522 mOvershootOverlay.SetAnchorPoint(AnchorPoint::TOP_LEFT);
1523 mOvershootOverlay.SetDrawMode( DrawMode::OVERLAY_2D );
1524 self.Add(mOvershootOverlay);
1526 Constraint constraint = Constraint::New<Vector3>( mOvershootOverlay, Actor::Property::SIZE, OvershootOverlaySizeConstraint(mOvershootSize.height) );
1527 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1528 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1529 constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
1532 mOvershootOverlay.SetSize(mOvershootSize.width, mOvershootSize.height);
1534 constraint = Constraint::New<Quaternion>( mOvershootOverlay, Actor::Property::ORIENTATION, OvershootOverlayRotationConstraint );
1535 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1536 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1537 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1540 constraint = Constraint::New<Vector3>( mOvershootOverlay, Actor::Property::POSITION, OvershootOverlayPositionConstraint );
1541 constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
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<bool>( mOvershootOverlay, Actor::Property::VISIBLE, OvershootOverlayVisibilityConstraint );
1548 constraint.AddSource( ParentSource( Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL ) );
1551 constraint = Constraint::New<float>( mOvershootOverlay, effectOvershootPropertyIndex, EqualToConstraint() );
1552 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1558 if( mOvershootOverlay )
1560 self.Remove(mOvershootOverlay);
1561 mOvershootOverlay.Reset();
1566 float ItemView::CalculateScrollOvershoot()
1568 float overshoot = 0.0f;
1572 // The overshoot must be calculated from the accumulated pan gesture displacement
1573 // since the pan gesture starts.
1574 Actor self = Self();
1575 float scrollDistance = CalculateScrollDistance(mTotalPanDisplacement, *mActiveLayout) * mActiveLayout->GetScrollSpeedFactor();
1576 float positionDelta = GetCurrentLayoutPosition(0) + scrollDistance;
1577 float minLayoutPosition = mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), Self().GetCurrentSize());
1578 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1579 float clamppedPosition = std::min(0.0f, std::max(minLayoutPosition, positionDelta));
1580 overshoot = positionDelta - clamppedPosition;
1583 return overshoot > 0.0f ? std::min(overshoot, 1.0f) : std::max(overshoot, -1.0f);
1586 void ItemView::AnimateScrollOvershoot(float overshootAmount, bool animateBack)
1588 bool animatingOn = fabsf(overshootAmount) > Math::MACHINE_EPSILON_1;
1590 // make sure we animate back if needed
1591 mAnimateOvershootOff = animateBack || (!animatingOn && mAnimatingOvershootOn);
1593 if( mAnimatingOvershootOn )
1595 // animating on, do not allow animate off
1599 Actor self = Self();
1601 if(mOvershootAnimationSpeed > Math::MACHINE_EPSILON_0)
1603 float currentOvershoot = self.GetProperty<float>(Toolkit::ItemView::Property::OVERSHOOT);
1604 float duration = 0.0f;
1606 if (mOvershootOverlay)
1608 duration = mOvershootOverlay.GetCurrentSize().height * (animatingOn ? (1.0f - fabsf(currentOvershoot)) : fabsf(currentOvershoot)) / mOvershootAnimationSpeed;
1611 // Mark the animation as in progress to prevent manual property sets overwriting it.
1612 mInAnimation = true;
1613 mAnimatingOvershootOn = animatingOn;
1614 RemoveAnimation(mScrollOvershootAnimation);
1615 mScrollOvershootAnimation = Animation::New(duration);
1616 mScrollOvershootAnimation.FinishedSignal().Connect(this, &ItemView::OnOvershootOnFinished);
1617 mScrollOvershootAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::OVERSHOOT), overshootAmount, TimePeriod(0.0f, duration) );
1618 mScrollOvershootAnimation.Play();
1622 self.SetProperty( Toolkit::ItemView::Property::OVERSHOOT, overshootAmount );
1626 void ItemView::SetItemsParentOrigin( const Vector3& parentOrigin )
1628 if( parentOrigin != mItemsParentOrigin )
1630 mItemsParentOrigin = parentOrigin;
1631 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1633 iter->second.SetParentOrigin(parentOrigin);
1638 Vector3 ItemView::GetItemsParentOrigin() const
1640 return mItemsParentOrigin;
1643 void ItemView::SetItemsAnchorPoint( const Vector3& anchorPoint )
1645 if( anchorPoint != mItemsAnchorPoint )
1647 mItemsAnchorPoint = anchorPoint;
1648 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1650 iter->second.SetAnchorPoint(anchorPoint);
1655 Vector3 ItemView::GetItemsAnchorPoint() const
1657 return mItemsAnchorPoint;
1660 void ItemView::GetItemsRange(ItemRange& range)
1662 if( !mItemPool.empty() )
1664 range.begin = mItemPool.begin()->first;
1665 range.end = mItemPool.rbegin()->first + 1;
1674 bool ItemView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1676 Dali::BaseHandle handle( object );
1678 bool connected( true );
1679 Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( handle );
1681 if( 0 == strcmp( signalName.c_str(), LAYOUT_ACTIVATED_SIGNAL ) )
1683 itemView.LayoutActivatedSignal().Connect( tracker, functor );
1687 // signalName does not match any signal
1694 void ItemView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
1696 Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( Dali::BaseHandle( object ) );
1700 ItemView& itemViewImpl( GetImpl( itemView ) );
1703 case Toolkit::ItemView::Property::MINIMUM_SWIPE_SPEED:
1705 itemViewImpl.SetMinimumSwipeSpeed( value.Get<float>() );
1708 case Toolkit::ItemView::Property::MINIMUM_SWIPE_DISTANCE:
1710 itemViewImpl.SetMinimumSwipeDistance( value.Get<float>() );
1713 case Toolkit::ItemView::Property::WHEEL_SCROLL_DISTANCE_STEP:
1715 itemViewImpl.SetWheelScrollDistanceStep( value.Get<float>() );
1718 case Toolkit::ItemView::Property::SNAP_TO_ITEM_ENABLED:
1720 itemViewImpl.SetAnchoring( value.Get<bool>() );
1723 case Toolkit::ItemView::Property::REFRESH_INTERVAL:
1725 itemViewImpl.SetRefreshInterval( value.Get<float>() );
1732 Property::Value ItemView::GetProperty( BaseObject* object, Property::Index index )
1734 Property::Value value;
1736 Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( Dali::BaseHandle( object ) );
1740 ItemView& itemViewImpl( GetImpl( itemView ) );
1743 case Toolkit::ItemView::Property::MINIMUM_SWIPE_SPEED:
1745 value = itemViewImpl.GetMinimumSwipeSpeed();
1748 case Toolkit::ItemView::Property::MINIMUM_SWIPE_DISTANCE:
1750 value = itemViewImpl.GetMinimumSwipeDistance();
1753 case Toolkit::ItemView::Property::WHEEL_SCROLL_DISTANCE_STEP:
1755 value = itemViewImpl.GetWheelScrollDistanceStep();
1758 case Toolkit::ItemView::Property::SNAP_TO_ITEM_ENABLED:
1760 value = itemViewImpl.GetAnchoring();
1763 case Toolkit::ItemView::Property::REFRESH_INTERVAL:
1765 value = itemViewImpl.GetRefreshInterval();
1774 bool ItemView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
1776 Dali::BaseHandle handle( object );
1778 Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( handle );
1780 DALI_ASSERT_ALWAYS( itemView );
1782 if( 0 == strcmp( actionName.c_str(), ACTION_STOP_SCROLLING ) )
1784 GetImpl( itemView ).DoStopScrolling();
1790 void ItemView::DoStopScrolling()
1792 if( mScrollAnimation )
1794 mScrollAnimation.Stop();
1795 mScrollAnimation.Reset();
1799 } // namespace Internal
1801 } // namespace Toolkit