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;
63 const unsigned int OVERSHOOT_SIZE_CONSTRAINT_TAG(42);
66 * Local helper to convert pan distance (in actor coordinates) to the layout-specific scrolling direction
68 float CalculateScrollDistance(Vector2 panDistance, Toolkit::ItemLayout& layout)
70 Radian scrollDirection(layout.GetScrollDirection());
72 float cosTheta = cosf(scrollDirection);
73 float sinTheta = sinf(scrollDirection);
75 return panDistance.x * sinTheta + panDistance.y * cosTheta;
78 // Overshoot overlay constraints
79 struct OvershootOverlaySizeConstraint
81 OvershootOverlaySizeConstraint( float height )
82 : mOvershootHeight( height )
86 void operator()( Vector3& current, const PropertyInputContainer& inputs )
88 const Vector2& parentScrollDirection = inputs[0]->GetVector2();
89 const Toolkit::ControlOrientation::Type& layoutOrientation = static_cast<Toolkit::ControlOrientation::Type>(inputs[1]->GetInteger());
90 const Vector3& parentSize = inputs[2]->GetVector3();
92 if(Toolkit::IsVertical(layoutOrientation))
94 current.width = fabsf(parentScrollDirection.y) > Math::MACHINE_EPSILON_1 ? parentSize.x : parentSize.y;
98 current.width = fabsf(parentScrollDirection.x) > Math::MACHINE_EPSILON_1 ? parentSize.y : parentSize.x;
101 current.height = ( current.width > OVERSHOOT_BOUNCE_ACTOR_RESIZE_THRESHOLD ) ? mOvershootHeight : mOvershootHeight*0.5f;
104 float mOvershootHeight;
107 void OvershootOverlayRotationConstraint( Quaternion& current, const PropertyInputContainer& inputs )
109 const Vector2& parentScrollDirection = inputs[0]->GetVector2();
110 const Toolkit::ControlOrientation::Type& layoutOrientation = static_cast<Toolkit::ControlOrientation::Type>(inputs[1]->GetInteger());
111 const float parentOvershoot = inputs[2]->GetFloat();
113 float multiplier = 0;
114 if(Toolkit::IsVertical(layoutOrientation))
116 if(fabsf(parentScrollDirection.y) <= Math::MACHINE_EPSILON_1)
118 if( (layoutOrientation == Toolkit::ControlOrientation::Up && parentOvershoot < Math::MACHINE_EPSILON_0)
119 || (layoutOrientation == Toolkit::ControlOrientation::Down && parentOvershoot > Math::MACHINE_EPSILON_0) )
128 else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.y > Math::MACHINE_EPSILON_0)
129 || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.y < Math::MACHINE_EPSILON_0) )
140 if(fabsf(parentScrollDirection.x) <= Math::MACHINE_EPSILON_1)
142 if( (layoutOrientation == Toolkit::ControlOrientation::Left && parentOvershoot > Math::MACHINE_EPSILON_0)
143 ||(layoutOrientation == Toolkit::ControlOrientation::Right && parentOvershoot < Math::MACHINE_EPSILON_0) )
152 else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.x > Math::MACHINE_EPSILON_0)
153 || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.x < Math::MACHINE_EPSILON_0) )
163 current = Quaternion( Radian( multiplier * Math::PI ), Vector3::ZAXIS );
166 void OvershootOverlayPositionConstraint( Vector3& current, const PropertyInputContainer& inputs )
168 const Vector3& parentSize = inputs[0]->GetVector3();
169 const Vector2& parentScrollDirection = inputs[1]->GetVector2();
170 const Toolkit::ControlOrientation::Type& layoutOrientation = static_cast<Toolkit::ControlOrientation::Type>(inputs[2]->GetInteger());
171 const float parentOvershoot = inputs[3]->GetFloat();
173 Vector3 relativeOffset;
175 if(Toolkit::IsVertical(layoutOrientation))
177 if(fabsf(parentScrollDirection.y) <= Math::MACHINE_EPSILON_1)
179 if( (layoutOrientation == Toolkit::ControlOrientation::Up && parentOvershoot < Math::MACHINE_EPSILON_0)
180 || (layoutOrientation == Toolkit::ControlOrientation::Down && parentOvershoot > Math::MACHINE_EPSILON_0) )
182 relativeOffset = Vector3(1.0f, 0.0f, 0.0f);
186 relativeOffset =Vector3(0.0f, 1.0f, 0.0f);
189 else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.y > Math::MACHINE_EPSILON_0)
190 || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.y < Math::MACHINE_EPSILON_0) )
192 relativeOffset = Vector3(0.0f, 0.0f, 0.0f);
196 relativeOffset = Vector3(1.0f, 1.0f, 0.0f);
201 if(fabsf(parentScrollDirection.x) <= Math::MACHINE_EPSILON_1)
203 if( (layoutOrientation == Toolkit::ControlOrientation::Left && parentOvershoot < Math::MACHINE_EPSILON_0)
204 || (layoutOrientation == Toolkit::ControlOrientation::Right && parentOvershoot > Math::MACHINE_EPSILON_0) )
206 relativeOffset = Vector3(0.0f, 0.0f, 0.0f);
210 relativeOffset = Vector3(1.0f, 1.0f, 0.0f);
213 else if( (parentOvershoot > Math::MACHINE_EPSILON_0 && parentScrollDirection.x > Math::MACHINE_EPSILON_0)
214 || (parentOvershoot < Math::MACHINE_EPSILON_0 && parentScrollDirection.x < Math::MACHINE_EPSILON_0) )
216 relativeOffset = Vector3(0.0f, 1.0f, 0.0f);
220 relativeOffset = Vector3(1.0f, 0.0f, 0.0f);
224 current = relativeOffset * parentSize;
227 void OvershootOverlayVisibilityConstraint( bool& current, const PropertyInputContainer& inputs )
229 current = inputs[0]->GetBoolean();
232 } // unnamed namespace
243 namespace // unnamed namespace
248 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ItemView, Toolkit::Scrollable, NULL)
250 DALI_PROPERTY_REGISTRATION( Toolkit, ItemView, "minimumSwipeSpeed", FLOAT, MINIMUM_SWIPE_SPEED )
251 DALI_PROPERTY_REGISTRATION( Toolkit, ItemView, "minimumSwipeDistance", FLOAT, MINIMUM_SWIPE_DISTANCE )
252 DALI_PROPERTY_REGISTRATION( Toolkit, ItemView, "wheelScrollDistanceStep", FLOAT, WHEEL_SCROLL_DISTANCE_STEP )
253 DALI_PROPERTY_REGISTRATION( Toolkit, ItemView, "snapToItemEnabled", BOOLEAN, SNAP_TO_ITEM_ENABLED )
254 DALI_PROPERTY_REGISTRATION( Toolkit, ItemView, "refreshInterval", FLOAT, REFRESH_INTERVAL )
256 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "layoutPosition", FLOAT, LAYOUT_POSITION)
257 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "scrollSpeed", FLOAT, SCROLL_SPEED)
258 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "overshoot", FLOAT, OVERSHOOT)
259 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "scrollDirection", VECTOR2, SCROLL_DIRECTION)
260 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "layoutOrientation", INTEGER, LAYOUT_ORIENTATION)
261 DALI_ANIMATABLE_PROPERTY_REGISTRATION( Toolkit, ItemView, "scrollContentSize", FLOAT, SCROLL_CONTENT_SIZE)
263 DALI_SIGNAL_REGISTRATION( Toolkit, ItemView, "layoutActivated", LAYOUT_ACTIVATED_SIGNAL )
265 DALI_ACTION_REGISTRATION( Toolkit, ItemView, "stopScrolling", ACTION_STOP_SCROLLING )
267 DALI_ACTION_REGISTRATION( Toolkit, ItemView, "enableRefresh", ACTION_ENABLE_REFRESH )
268 DALI_ACTION_REGISTRATION( Toolkit, ItemView, "disableRefresh", ACTION_DISABLE_REFRESH )
270 DALI_TYPE_REGISTRATION_END()
272 bool FindById( const ItemContainer& items, ItemId id )
274 for( ConstItemIter iter = items.begin(); items.end() != iter; ++iter )
276 if( iter->first == id )
286 * Helper to apply size constraint to mOvershootOverlay
287 * @param[in] overshootOverlay The overshootOverlay actor
288 * @param[in] The required height
290 void ApplyOvershootSizeConstraint( Actor overshootOverlay, float height )
292 Constraint constraint = Constraint::New<Vector3>( overshootOverlay, Actor::Property::SIZE, OvershootOverlaySizeConstraint( height ) );
293 constraint.AddSource( ParentSource( Dali::Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
294 constraint.AddSource( ParentSource( Dali::Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
295 constraint.AddSource( ParentSource( Dali::Actor::Property::SIZE ) );
296 constraint.SetTag( OVERSHOOT_SIZE_CONSTRAINT_TAG );
300 } // unnamed namespace
302 Dali::Toolkit::ItemView ItemView::New(ItemFactory& factory)
304 // Create the implementation
305 ItemViewPtr itemView(new ItemView(factory));
307 // Pass ownership to CustomActor via derived handle
308 Dali::Toolkit::ItemView handle(*itemView);
310 // Second-phase init of the implementation
311 // This can only be done after the CustomActor connection has been made...
312 itemView->Initialize();
317 ItemView::ItemView(ItemFactory& factory)
318 : Scrollable( ControlBehaviour( DISABLE_SIZE_NEGOTIATION | DISABLE_STYLE_CHANGE_SIGNALS | REQUIRES_WHEEL_EVENTS | REQUIRES_KEYBOARD_NAVIGATION_SUPPORT ) ),
319 mItemFactory(factory),
320 mItemsParentOrigin(ParentOrigin::CENTER),
321 mItemsAnchorPoint(AnchorPoint::CENTER),
322 mTotalPanDisplacement(Vector2::ZERO),
324 mAnchoringDuration(DEFAULT_ANCHORING_DURATION),
325 mRefreshIntervalLayoutPositions(0.0f),
326 mMinimumSwipeSpeed(DEFAULT_MINIMUM_SWIPE_SPEED),
327 mMinimumSwipeDistance(DEFAULT_MINIMUM_SWIPE_DISTANCE),
328 mWheelScrollDistanceStep(0.0f),
329 mScrollDistance(0.0f),
331 mScrollOvershoot(0.0f),
332 mGestureState(Gesture::Clear),
333 mAnimatingOvershootOn(false),
334 mAnimateOvershootOff(false),
335 mAnchoringEnabled(false),
336 mRefreshOrderHint(true/*Refresh item 0 first*/),
339 mRefreshEnabled(true),
340 mRefreshNotificationEnabled(true),
345 void ItemView::OnInitialize()
349 Vector2 stageSize = Stage::GetCurrent().GetSize();
350 mWheelScrollDistanceStep = stageSize.y * DEFAULT_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION;
352 self.TouchSignal().Connect( this, &ItemView::OnTouch );
353 EnableGestureDetection(Gesture::Type(Gesture::Pan));
355 mWheelEventFinishedTimer = Timer::New( WHEEL_EVENT_FINISHED_TIME_OUT );
356 mWheelEventFinishedTimer.TickSignal().Connect( this, &ItemView::OnWheelEventFinished );
358 SetRefreshInterval(DEFAULT_REFRESH_INTERVAL_LAYOUT_POSITIONS);
361 ItemView::~ItemView()
365 unsigned int ItemView::GetLayoutCount() const
367 return mLayouts.size();
370 void ItemView::AddLayout(ItemLayout& layout)
372 mLayouts.push_back(ItemLayoutPtr(&layout));
375 void ItemView::RemoveLayout(unsigned int layoutIndex)
377 DALI_ASSERT_ALWAYS(layoutIndex < mLayouts.size());
379 if (mActiveLayout == mLayouts[layoutIndex].Get())
381 mActiveLayout = NULL;
384 mLayouts.erase(mLayouts.begin() + layoutIndex);
387 ItemLayoutPtr ItemView::GetLayout(unsigned int layoutIndex) const
389 return mLayouts[layoutIndex];
392 ItemLayoutPtr ItemView::GetActiveLayout() const
394 return ItemLayoutPtr(mActiveLayout);
397 float ItemView::GetCurrentLayoutPosition(unsigned int itemId) const
399 return Self().GetProperty<float>( Toolkit::ItemView::Property::LAYOUT_POSITION ) + static_cast<float>( itemId );
402 void ItemView::ActivateLayout(unsigned int layoutIndex, const Vector3& targetSize, float durationSeconds)
404 DALI_ASSERT_ALWAYS(layoutIndex < mLayouts.size());
406 mRefreshEnabled = false;
410 // The ItemView size should match the active layout size
411 self.SetSize(targetSize);
412 mActiveLayoutTargetSize = targetSize;
414 // Switch to the new layout
415 mActiveLayout = mLayouts[layoutIndex].Get();
417 // Move the items to the new layout positions...
419 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
421 unsigned int itemId = iter->first;
422 Actor actor = iter->second;
424 // Remove constraints from previous layout
425 actor.RemoveConstraints();
428 mActiveLayout->GetItemSize( itemId, targetSize, size );
429 actor.SetSize( size.GetVectorXY() );
431 mActiveLayout->ApplyConstraints(actor, itemId, targetSize, Self() );
434 // Refresh the new layout
435 ItemRange range = GetItemRange(*mActiveLayout, targetSize, GetCurrentLayoutPosition(0), false/* don't reserve extra*/);
436 AddActorsWithinRange( range, targetSize );
438 // Scroll to an appropriate layout position
440 bool scrollAnimationNeeded(false);
441 float firstItemScrollPosition(0.0f);
443 float current = GetCurrentLayoutPosition(0);
444 float minimum = ClampFirstItemPosition(current, targetSize, *mActiveLayout);
446 if (current < minimum)
448 scrollAnimationNeeded = true;
449 firstItemScrollPosition = minimum;
451 else if (mAnchoringEnabled)
453 scrollAnimationNeeded = true;
454 firstItemScrollPosition = mActiveLayout->GetClosestAnchorPosition(current);
457 if (scrollAnimationNeeded)
459 RemoveAnimation(mScrollAnimation);
460 mScrollAnimation = Animation::New(durationSeconds);
461 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, AlphaFunction::EASE_OUT );
462 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnLayoutActivationScrollFinished);
463 mScrollAnimation.Play();
467 // Emit the layout activated signal
468 mLayoutActivatedSignal.Emit();
471 AnimateScrollOvershoot(0.0f);
472 mScrollOvershoot = 0.0f;
474 Radian scrollDirection(mActiveLayout->GetScrollDirection());
475 self.SetProperty(Toolkit::ItemView::Property::SCROLL_DIRECTION, Vector2(sinf(scrollDirection), cosf(scrollDirection)));
476 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_ORIENTATION, static_cast<int>(mActiveLayout->GetOrientation()));
477 self.SetProperty(Toolkit::ItemView::Property::SCROLL_SPEED, mScrollSpeed);
479 CalculateDomainSize(targetSize);
482 void ItemView::DeactivateCurrentLayout()
486 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
488 Actor actor = iter->second;
489 actor.RemoveConstraints();
492 mActiveLayout = NULL;
496 void ItemView::OnRefreshNotification(PropertyNotification& source)
498 if( mRefreshNotificationEnabled )
500 // Cancel scroll animation to prevent any fighting of setting the scroll position property by scroll bar during fast scroll.
501 if(!mRefreshEnabled && mScrollAnimation)
503 RemoveAnimation(mScrollAnimation);
506 // Only cache extra items when it is not a fast scroll
507 DoRefresh(GetCurrentLayoutPosition(0), mRefreshEnabled || mScrollAnimation);
511 void ItemView::Refresh()
513 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter )
515 ReleaseActor( iter->first, iter->second );
519 DoRefresh(GetCurrentLayoutPosition(0), true);
522 void ItemView::DoRefresh(float currentLayoutPosition, bool cacheExtra)
526 ItemRange range = GetItemRange(*mActiveLayout, mActiveLayoutTargetSize, currentLayoutPosition, cacheExtra/*reserve extra*/);
527 RemoveActorsOutsideRange( range );
528 AddActorsWithinRange( range, Self().GetCurrentSize() );
530 mScrollUpdatedSignal.Emit( Vector2(0.0f, currentLayoutPosition) );
534 void ItemView::SetMinimumSwipeSpeed(float speed)
536 mMinimumSwipeSpeed = speed;
539 float ItemView::GetMinimumSwipeSpeed() const
541 return mMinimumSwipeSpeed;
544 void ItemView::SetMinimumSwipeDistance(float distance)
546 mMinimumSwipeDistance = distance;
549 float ItemView::GetMinimumSwipeDistance() const
551 return mMinimumSwipeDistance;
554 void ItemView::SetWheelScrollDistanceStep(float step)
556 mWheelScrollDistanceStep = step;
559 float ItemView::GetWheelScrollDistanceStep() const
561 return mWheelScrollDistanceStep;
564 void ItemView::SetAnchoring(bool enabled)
566 mAnchoringEnabled = enabled;
569 bool ItemView::GetAnchoring() const
571 return mAnchoringEnabled;
574 void ItemView::SetAnchoringDuration(float durationSeconds)
576 mAnchoringDuration = durationSeconds;
579 float ItemView::GetAnchoringDuration() const
581 return mAnchoringDuration;
584 void ItemView::SetRefreshInterval(float intervalLayoutPositions)
586 if( !Equals(mRefreshIntervalLayoutPositions, intervalLayoutPositions) )
588 mRefreshIntervalLayoutPositions = intervalLayoutPositions;
591 if(mRefreshNotification)
593 self.RemovePropertyNotification(mRefreshNotification);
595 mRefreshNotification = self.AddPropertyNotification( Toolkit::ItemView::Property::LAYOUT_POSITION, StepCondition(mRefreshIntervalLayoutPositions, 0.0f) );
596 mRefreshNotification.NotifySignal().Connect( this, &ItemView::OnRefreshNotification );
600 float ItemView::GetRefreshInterval() const
602 return mRefreshIntervalLayoutPositions;
605 void ItemView::SetRefreshEnabled(bool enabled)
607 mRefreshEnabled = enabled;
610 Actor ItemView::GetItem(unsigned int itemId) const
614 ConstItemPoolIter iter = mItemPool.find( itemId );
615 if( iter != mItemPool.end() )
617 actor = iter->second;
623 unsigned int ItemView::GetItemId( Actor actor ) const
625 unsigned int itemId( 0 );
627 for ( ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter )
629 if( iter->second == actor )
631 itemId = iter->first;
639 void ItemView::InsertItem( Item newItem, float durationSeconds )
642 Vector3 layoutSize = Self().GetCurrentSize();
644 Actor displacedActor;
645 ItemPoolIter afterDisplacedIter = mItemPool.end();
647 ItemPoolIter foundIter = mItemPool.find( newItem.first );
648 if( mItemPool.end() != foundIter )
650 SetupActor( newItem, layoutSize );
651 Self().Add( newItem.second );
653 displacedActor = foundIter->second;
654 foundIter->second = newItem.second;
656 afterDisplacedIter = ++foundIter;
660 // Inserting before the existing item range?
661 ItemPoolIter iter = mItemPool.begin();
662 if( iter != mItemPool.end() &&
663 iter->first > newItem.first )
665 displacedActor = iter->second;
666 mItemPool.erase( iter++ ); // iter is still valid after the erase
668 afterDisplacedIter = iter;
674 // Move the existing actors to make room
675 for( ItemPoolIter iter = afterDisplacedIter; mItemPool.end() != iter; ++iter )
677 Actor temp = iter->second;
678 iter->second = displacedActor;
679 displacedActor = temp;
681 iter->second.RemoveConstraints();
682 mActiveLayout->ApplyConstraints( iter->second, iter->first, layoutSize, Self() );
686 ItemPool::reverse_iterator lastIter = mItemPool.rbegin();
687 if ( lastIter != mItemPool.rend() )
689 ItemId lastId = lastIter->first;
690 Item lastItem( lastId + 1, displacedActor );
691 mItemPool.insert( lastItem );
693 lastItem.second.RemoveConstraints();
694 mActiveLayout->ApplyConstraints( lastItem.second, lastItem.first, layoutSize, Self() );
698 CalculateDomainSize( layoutSize );
700 mAddingItems = false;
703 void ItemView::InsertItems( const ItemContainer& newItems, float durationSeconds )
706 Vector3 layoutSize = Self().GetCurrentSize();
708 // Insert from lowest id to highest
709 std::set<Item> sortedItems;
710 for( ConstItemIter iter = newItems.begin(); newItems.end() != iter; ++iter )
712 sortedItems.insert( *iter );
715 for( std::set<Item>::iterator iter = sortedItems.begin(); sortedItems.end() != iter; ++iter )
717 Self().Add( iter->second );
719 ItemPoolIter foundIter = mItemPool.find( iter->first );
720 if( mItemPool.end() != foundIter )
722 Actor moveMe = foundIter->second;
723 foundIter->second = iter->second;
725 // Move the existing actors to make room
726 for( ItemPoolIter iter = ++foundIter; mItemPool.end() != iter; ++iter )
728 Actor temp = iter->second;
729 iter->second = moveMe;
734 ItemId lastId = mItemPool.rbegin()->first;
735 Item lastItem( lastId + 1, moveMe );
736 mItemPool.insert( lastItem );
740 mItemPool.insert( *iter );
744 // Relayout everything
745 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
748 if( FindById( newItems, iter->first ) )
750 SetupActor( *iter, layoutSize );
754 iter->second.RemoveConstraints();
755 mActiveLayout->ApplyConstraints( iter->second, iter->first, layoutSize, Self() );
759 CalculateDomainSize( layoutSize );
761 mAddingItems = false;
764 void ItemView::RemoveItem( unsigned int itemId, float durationSeconds )
766 bool actorsReordered = RemoveActor( itemId );
767 if( actorsReordered )
769 ReapplyAllConstraints();
775 void ItemView::RemoveItems( const ItemIdContainer& itemIds, float durationSeconds )
777 bool actorsReordered( false );
779 // Remove from highest id to lowest
780 set<ItemId> sortedItems;
781 for( ConstItemIdIter iter = itemIds.begin(); itemIds.end() != iter; ++iter )
783 sortedItems.insert( *iter );
786 for( set<ItemId>::reverse_iterator iter = sortedItems.rbegin(); sortedItems.rend() != iter; ++iter )
788 if( RemoveActor( *iter ) )
790 actorsReordered = true;
794 if( actorsReordered )
796 ReapplyAllConstraints();
802 bool ItemView::RemoveActor(unsigned int itemId)
804 bool reordered( false );
806 ItemPoolIter removeIter = mItemPool.find( itemId );
807 if( removeIter != mItemPool.end() )
809 ReleaseActor(itemId, removeIter->second);
813 // Removing before the existing item range?
814 ItemPoolIter iter = mItemPool.begin();
815 if( iter != mItemPool.end() &&
816 iter->first > itemId )
818 // In order to decrement the first visible item ID
819 mItemPool.insert( Item(iter->first - 1, Actor()) );
821 removeIter = mItemPool.begin();
825 if( removeIter != mItemPool.end() )
829 // Adjust the remaining item IDs, for example if item 2 is removed:
830 // Initial actors: After insert:
831 // ID 1 - ActorA ID 1 - ActorA
832 // ID 2 - ActorB ID 2 - ActorC (previously ID 3)
833 // ID 3 - ActorC ID 3 - ActorB (previously ID 4)
835 for (ItemPoolIter iter = removeIter; iter != mItemPool.end(); ++iter)
837 if( iter->first < mItemPool.rbegin()->first )
839 iter->second = mItemPool[ iter->first + 1 ];
843 mItemPool.erase( iter );
852 void ItemView::ReplaceItem( Item replacementItem, float durationSeconds )
855 Vector3 layoutSize = Self().GetCurrentSize();
857 SetupActor( replacementItem, layoutSize );
858 Self().Add( replacementItem.second );
860 const ItemPoolIter iter = mItemPool.find( replacementItem.first );
861 if( mItemPool.end() != iter )
863 ReleaseActor(iter->first, iter->second);
864 iter->second = replacementItem.second;
868 mItemPool.insert( replacementItem );
871 CalculateDomainSize( layoutSize );
873 mAddingItems = false;
876 void ItemView::ReplaceItems( const ItemContainer& replacementItems, float durationSeconds )
878 for( ConstItemIter iter = replacementItems.begin(); replacementItems.end() != iter; ++iter )
880 ReplaceItem( *iter, durationSeconds );
884 void ItemView::RemoveActorsOutsideRange( ItemRange range )
886 // Remove unwanted actors from the ItemView & ItemPool
887 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); )
889 unsigned int current = iter->first;
891 if( ! range.Within( current ) )
893 ReleaseActor(iter->first, iter->second);
895 mItemPool.erase( iter++ ); // erase invalidates the return value of post-increment; iter remains valid
904 void ItemView::AddActorsWithinRange( ItemRange range, const Vector3& layoutSize )
906 range.end = std::min(mItemFactory.GetNumberOfItems(), range.end);
908 // The order of addition depends on the scroll direction.
909 if (mRefreshOrderHint)
911 for (unsigned int itemId = range.begin; itemId < range.end; ++itemId)
913 AddNewActor( itemId, layoutSize );
918 for (unsigned int itemId = range.end; itemId > range.begin; --itemId)
920 AddNewActor( itemId-1, layoutSize );
924 // Total number of items may change dynamically.
925 // Always recalculate the domain size to reflect that.
926 CalculateDomainSize(Self().GetCurrentSize());
929 void ItemView::AddNewActor( unsigned int itemId, const Vector3& layoutSize )
933 if( mItemPool.end() == mItemPool.find( itemId ) )
935 Actor actor = mItemFactory.NewItem( itemId );
939 Item newItem( itemId, actor );
941 mItemPool.insert( newItem );
943 SetupActor( newItem, layoutSize );
948 mAddingItems = false;
951 void ItemView::SetupActor( Item item, const Vector3& layoutSize )
953 item.second.SetParentOrigin( mItemsParentOrigin );
954 item.second.SetAnchorPoint( mItemsAnchorPoint );
959 mActiveLayout->GetItemSize( item.first, mActiveLayoutTargetSize, size );
960 item.second.SetSize( size.GetVectorXY() );
962 mActiveLayout->ApplyConstraints( item.second, item.first, layoutSize, Self() );
966 void ItemView::ReleaseActor( ItemId item, Actor actor )
968 Self().Remove( actor );
969 mItemFactory.ItemReleased(item, actor);
972 ItemRange ItemView::GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra)
974 unsigned int itemCount = mItemFactory.GetNumberOfItems();
976 ItemRange available(0u, itemCount);
978 ItemRange range = layout.GetItemsWithinArea( layoutPosition, layoutSize );
982 // Add the reserve items for scrolling
983 unsigned int extra = layout.GetReserveItemCount(layoutSize);
984 range.begin = (range.begin >= extra) ? (range.begin - extra) : 0u;
988 return range.Intersection(available);
991 void ItemView::OnChildAdd(Actor& child)
995 // We don't want to do this downcast check for any item added by ItemView itself.
996 Dali::Toolkit::ScrollBar scrollBar = Dali::Toolkit::ScrollBar::DownCast(child);
999 scrollBar.SetScrollPropertySource(Self(),
1000 Toolkit::ItemView::Property::LAYOUT_POSITION,
1001 Toolkit::Scrollable::Property::SCROLL_POSITION_MIN_Y,
1002 Toolkit::Scrollable::Property::SCROLL_POSITION_MAX_Y,
1003 Toolkit::ItemView::Property::SCROLL_CONTENT_SIZE);
1008 bool ItemView::OnWheelEvent(const WheelEvent& event)
1010 // Respond the wheel event to scroll
1013 Actor self = Self();
1014 const Vector3 layoutSize = Self().GetCurrentSize();
1015 float layoutPositionDelta = GetCurrentLayoutPosition(0) - (event.z * mWheelScrollDistanceStep * mActiveLayout->GetScrollSpeedFactor());
1016 float firstItemScrollPosition = ClampFirstItemPosition(layoutPositionDelta, layoutSize, *mActiveLayout);
1018 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1020 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1021 mRefreshEnabled = true;
1024 if (mWheelEventFinishedTimer.IsRunning())
1026 mWheelEventFinishedTimer.Stop();
1029 mWheelEventFinishedTimer.Start();
1034 bool ItemView::OnWheelEventFinished()
1038 RemoveAnimation(mScrollAnimation);
1040 // No more wheel events coming. Do the anchoring if enabled.
1041 mScrollAnimation = DoAnchoring();
1042 if (mScrollAnimation)
1044 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1045 mScrollAnimation.Play();
1049 mScrollOvershoot = 0.0f;
1050 AnimateScrollOvershoot(0.0f);
1052 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1059 void ItemView::ReapplyAllConstraints()
1061 Vector3 layoutSize = Self().GetCurrentSize();
1063 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1065 unsigned int id = iter->first;
1066 Actor actor = iter->second;
1068 actor.RemoveConstraints();
1069 mActiveLayout->ApplyConstraints(actor, id, layoutSize, Self());
1073 void ItemView::OnItemsRemoved()
1075 CalculateDomainSize(Self().GetCurrentSize());
1077 // Adjust scroll-position after an item is removed
1080 float firstItemScrollPosition = ClampFirstItemPosition(GetCurrentLayoutPosition(0), Self().GetCurrentSize(), *mActiveLayout);
1081 Self().SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1085 float ItemView::ClampFirstItemPosition( float targetPosition, const Vector3& targetSize, ItemLayout& layout, bool updateOvershoot )
1087 Actor self = Self();
1088 float minLayoutPosition = layout.GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), targetSize);
1089 float clamppedPosition = std::min(0.0f, std::max(minLayoutPosition, targetPosition));
1090 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1092 if( updateOvershoot )
1094 mScrollOvershoot = targetPosition - clamppedPosition;
1097 return clamppedPosition;
1100 bool ItemView::OnTouch( Actor actor, const TouchData& touch )
1102 // Ignore events with multiple-touch points
1103 if (touch.GetPointCount() != 1)
1108 if ( touch.GetState( 0 ) == PointState::DOWN )
1110 // Cancel ongoing scrolling etc.
1111 mGestureState = Gesture::Clear;
1113 mScrollDistance = 0.0f;
1114 mScrollSpeed = 0.0f;
1115 Self().SetProperty(Toolkit::ItemView::Property::SCROLL_SPEED, mScrollSpeed);
1117 mScrollOvershoot = 0.0f;
1118 AnimateScrollOvershoot(0.0f);
1120 if(mScrollAnimation)
1122 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1125 RemoveAnimation(mScrollAnimation);
1128 return true; // consume since we're potentially scrolling
1131 void ItemView::OnPan( const PanGesture& gesture )
1133 Actor self = Self();
1134 const Vector3 layoutSize = Self().GetCurrentSize();
1136 RemoveAnimation(mScrollAnimation);
1138 // Short-circuit if there is no active layout
1141 mGestureState = Gesture::Clear;
1145 mGestureState = gesture.state;
1147 switch (mGestureState)
1149 case Gesture::Finished:
1152 if (fabsf(mScrollDistance) > mMinimumSwipeDistance &&
1153 mScrollSpeed > mMinimumSwipeSpeed)
1155 float direction = (mScrollDistance < 0.0f) ? -1.0f : 1.0f;
1157 mRefreshOrderHint = true;
1159 float currentLayoutPosition = GetCurrentLayoutPosition(0);
1160 float firstItemScrollPosition = ClampFirstItemPosition(currentLayoutPosition + mScrollSpeed * direction,
1164 if (mAnchoringEnabled)
1166 firstItemScrollPosition = mActiveLayout->GetClosestAnchorPosition(firstItemScrollPosition);
1169 RemoveAnimation(mScrollAnimation);
1171 float flickAnimationDuration = Clamp( mActiveLayout->GetItemFlickAnimationDuration() * std::max(1.0f, fabsf(firstItemScrollPosition - GetCurrentLayoutPosition(0)))
1172 , DEFAULT_MINIMUM_SWIPE_DURATION, DEFAULT_MAXIMUM_SWIPE_DURATION);
1174 mScrollAnimation = Animation::New(flickAnimationDuration);
1175 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION ), firstItemScrollPosition, AlphaFunction::EASE_OUT );
1176 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::SCROLL_SPEED), 0.0f, AlphaFunction::EASE_OUT );
1179 // Check whether it has already scrolled to the end
1180 if(fabs(currentLayoutPosition - firstItemScrollPosition) > Math::MACHINE_EPSILON_0)
1182 AnimateScrollOvershoot(0.0f);
1186 // Anchoring may be triggered when there was no swipe
1187 if (!mScrollAnimation)
1189 mScrollAnimation = DoAnchoring();
1192 // Reset the overshoot if no scroll animation.
1193 if (!mScrollAnimation)
1195 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1197 AnimateScrollOvershoot(0.0f, false);
1202 case Gesture::Started: // Fall through
1204 mTotalPanDisplacement = Vector2::ZERO;
1205 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1206 mRefreshEnabled = true;
1209 case Gesture::Continuing:
1211 mScrollDistance = CalculateScrollDistance(gesture.displacement, *mActiveLayout);
1212 mScrollSpeed = Clamp((gesture.GetSpeed() * gesture.GetSpeed() * mActiveLayout->GetFlickSpeedFactor() * MILLISECONDS_PER_SECONDS), 0.0f, mActiveLayout->GetMaximumSwipeSpeed());
1214 // Refresh order depends on the direction of the scroll; negative is towards the last item.
1215 mRefreshOrderHint = mScrollDistance < 0.0f;
1217 float layoutPositionDelta = GetCurrentLayoutPosition(0) + (mScrollDistance * mActiveLayout->GetScrollSpeedFactor());
1219 float firstItemScrollPosition = ClampFirstItemPosition(layoutPositionDelta, layoutSize, *mActiveLayout);
1221 float currentOvershoot = self.GetProperty<float>(Toolkit::ItemView::Property::OVERSHOOT);
1223 self.SetProperty(Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1225 if( ( firstItemScrollPosition >= 0.0f &&
1226 currentOvershoot < 1.0f ) ||
1227 ( firstItemScrollPosition <= mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), layoutSize) &&
1228 currentOvershoot > -1.0f ) )
1230 mTotalPanDisplacement += gesture.displacement;
1233 mScrollOvershoot = CalculateScrollOvershoot();
1235 // If the view is moved in a direction against the overshoot indicator, then the indicator should be animated off.
1236 // First make sure we are not in an animation, otherwise a previously started
1237 // off-animation will be overwritten as the user continues scrolling.
1240 // Check if the movement is against the current overshoot amount (if we are currently displaying the indicator).
1241 if( ( ( mScrollOvershoot > Math::MACHINE_EPSILON_0 ) && ( mScrollDistance < -Math::MACHINE_EPSILON_0 ) ) ||
1242 ( ( mScrollOvershoot < Math::MACHINE_EPSILON_0 ) && ( mScrollDistance > Math::MACHINE_EPSILON_0 ) ) )
1244 // The user has moved against the indicator direction.
1245 // First, we reset the total displacement. This means the overshoot amount will become zero the next frame,
1246 // and if the user starts dragging in the overshoot direction again, the indicator will appear once more.
1247 mTotalPanDisplacement = Vector2::ZERO;
1248 // Animate the overshoot indicator off.
1249 AnimateScrollOvershoot( 0.0f, false );
1253 // Only set the property directly if we are not animating the overshoot away,
1254 // as otherwise this will overwrite the animation generated value.
1255 self.SetProperty( Toolkit::ItemView::Property::OVERSHOOT, mScrollOvershoot );
1261 case Gesture::Cancelled:
1263 mScrollAnimation = DoAnchoring();
1271 if (mScrollAnimation)
1273 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1274 mScrollAnimation.Play();
1278 bool ItemView::OnAccessibilityPan(PanGesture gesture)
1284 Actor ItemView::GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled)
1286 Actor nextFocusActor;
1290 if(!actor || actor == this->Self())
1292 nextFocusActor = GetItem(nextItemID);
1294 else if(actor && actor.GetParent() == this->Self())
1296 int itemID = GetItemId(actor);
1297 nextItemID = mActiveLayout->GetNextFocusItemID(itemID, mItemFactory.GetNumberOfItems(), direction, loopEnabled);
1298 nextFocusActor = GetItem(nextItemID);
1299 if(nextFocusActor == actor)
1301 // need to pass NULL actor back to focus manager
1302 nextFocusActor.Reset();
1303 return nextFocusActor;
1306 float layoutPosition = mActiveLayout->GetClosestAnchorPosition( GetCurrentLayoutPosition(0) );
1307 Vector3 layoutSize = Self().GetCurrentSize();
1310 // likely the current item is not buffered, so not in our item pool, probably best to get first viewable item
1311 ItemRange viewableItems = mActiveLayout->GetItemsWithinArea(layoutPosition, layoutSize);
1312 nextItemID = viewableItems.begin;
1313 nextFocusActor = GetItem(nextItemID);
1316 return nextFocusActor;
1319 void ItemView::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
1321 // only in this function if our chosen focus actor was actually used
1322 if(commitedFocusableActor)
1324 int nextItemID = GetItemId(commitedFocusableActor);
1325 float layoutPosition = GetCurrentLayoutPosition(0);
1326 Vector3 layoutSize = Self().GetCurrentSize();
1328 float scrollTo = mActiveLayout->GetClosestOnScreenLayoutPosition(nextItemID, layoutPosition, layoutSize);
1329 ScrollTo(Vector2(0.0f, scrollTo), DEFAULT_KEYBOARD_FOCUS_SCROLL_DURATION);
1333 Animation ItemView::DoAnchoring()
1335 Animation anchoringAnimation;
1336 Actor self = Self();
1338 if (mActiveLayout && mAnchoringEnabled)
1340 float anchorPosition = mActiveLayout->GetClosestAnchorPosition( GetCurrentLayoutPosition(0) );
1342 anchoringAnimation = Animation::New(mAnchoringDuration);
1343 anchoringAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), anchorPosition, AlphaFunction::EASE_OUT );
1344 anchoringAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::SCROLL_SPEED), 0.0f, AlphaFunction::EASE_OUT );
1347 AnimateScrollOvershoot(0.0f);
1351 return anchoringAnimation;
1354 void ItemView::OnScrollFinished(Animation& source)
1356 Actor self = Self();
1358 RemoveAnimation(mScrollAnimation); // mScrollAnimation is used to query whether we're scrolling
1360 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1362 if(mIsFlicking && fabsf(mScrollOvershoot) > Math::MACHINE_EPSILON_1)
1364 AnimateScrollOvershoot( mScrollOvershoot > 0.0f ? 1.0f : -1.0f, true);
1368 // Reset the overshoot
1369 AnimateScrollOvershoot( 0.0f );
1371 mIsFlicking = false;
1373 mScrollOvershoot = 0.0f;
1376 void ItemView::OnLayoutActivationScrollFinished(Animation& source)
1378 RemoveAnimation(mScrollAnimation);
1379 mRefreshEnabled = true;
1380 DoRefresh(GetCurrentLayoutPosition(0), true);
1382 // Emit the layout activated signal
1383 mLayoutActivatedSignal.Emit();
1386 void ItemView::OnOvershootOnFinished(Animation& animation)
1388 mAnimatingOvershootOn = false;
1389 mScrollOvershootAnimation.FinishedSignal().Disconnect(this, &ItemView::OnOvershootOnFinished);
1390 RemoveAnimation(mScrollOvershootAnimation);
1391 if(mAnimateOvershootOff)
1393 AnimateScrollOvershoot(0.0f);
1395 mInAnimation = false;
1398 void ItemView::ScrollToItem(unsigned int itemId, float durationSeconds)
1400 Actor self = Self();
1401 const Vector3 layoutSize = Self().GetCurrentSize();
1402 float firstItemScrollPosition = ClampFirstItemPosition(mActiveLayout->GetItemScrollToPosition(itemId), layoutSize, *mActiveLayout);
1404 if(durationSeconds > 0.0f)
1406 RemoveAnimation(mScrollAnimation);
1407 mScrollAnimation = Animation::New(durationSeconds);
1408 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, mScrollToAlphaFunction );
1409 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1410 mScrollAnimation.Play();
1414 self.SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1415 AnimateScrollOvershoot(0.0f);
1418 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1419 mRefreshEnabled = true;
1422 void ItemView::RemoveAnimation(Animation& animation)
1426 // Cease animating, and reset handle.
1432 void ItemView::CalculateDomainSize(const Vector3& layoutSize)
1434 Actor self = Self();
1436 Vector3 firstItemPosition(Vector3::ZERO);
1437 Vector3 lastItemPosition(Vector3::ZERO);
1441 firstItemPosition = mActiveLayout->GetItemPosition( 0,0,layoutSize );
1443 float minLayoutPosition = mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), layoutSize);
1444 lastItemPosition = mActiveLayout->GetItemPosition( fabs(minLayoutPosition),fabs(minLayoutPosition),layoutSize );
1448 if(IsHorizontal(mActiveLayout->GetOrientation()))
1450 domainSize = fabs(firstItemPosition.x - lastItemPosition.x);
1454 domainSize = fabs(firstItemPosition.y - lastItemPosition.y);
1457 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MIN, Vector2::ZERO);
1458 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1460 self.SetProperty(Toolkit::ItemView::Property::SCROLL_CONTENT_SIZE, domainSize);
1462 bool isLayoutScrollable = IsLayoutScrollable(layoutSize);
1463 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL, isLayoutScrollable);
1464 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL, false);
1468 bool ItemView::IsLayoutScrollable(const Vector3& layoutSize)
1470 Actor self = Self();
1472 float currentLayoutPosition = ClampFirstItemPosition( GetCurrentLayoutPosition(0), layoutSize, *mActiveLayout, false );
1473 float forwardClampedPosition = ClampFirstItemPosition( currentLayoutPosition + 1.0, layoutSize, *mActiveLayout, false );
1474 float backwardClampedPosition = ClampFirstItemPosition( currentLayoutPosition - 1.0, layoutSize, *mActiveLayout, false );
1476 return (fabs(forwardClampedPosition - backwardClampedPosition) > Math::MACHINE_EPSILON_0);
1479 float ItemView::GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const
1481 Vector3 firstItemPosition( mActiveLayout->GetItemPosition(0, layoutPosition, layoutSize ) );
1482 return IsHorizontal(mActiveLayout->GetOrientation()) ? firstItemPosition.x: firstItemPosition.y;
1485 Vector2 ItemView::GetCurrentScrollPosition() const
1487 return Vector2(0.0f, GetScrollPosition(GetCurrentLayoutPosition(0), Self().GetCurrentSize()));
1490 void ItemView::AddOverlay(Actor actor)
1492 actor.SetDrawMode( DrawMode::OVERLAY_2D );
1496 void ItemView::RemoveOverlay(Actor actor)
1498 Self().Remove(actor);
1501 void ItemView::ScrollTo(const Vector2& position, float duration)
1503 Actor self = Self();
1504 const Vector3 layoutSize = Self().GetCurrentSize();
1506 float firstItemScrollPosition = ClampFirstItemPosition(position.y, layoutSize, *mActiveLayout);
1510 RemoveAnimation(mScrollAnimation);
1511 mScrollAnimation = Animation::New(duration);
1512 mScrollAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::LAYOUT_POSITION), firstItemScrollPosition, mScrollToAlphaFunction );
1513 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1514 mScrollAnimation.Play();
1518 self.SetProperty( Toolkit::ItemView::Property::LAYOUT_POSITION, firstItemScrollPosition );
1519 AnimateScrollOvershoot(0.0f);
1522 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1523 mRefreshEnabled = true;
1526 void ItemView::SetOvershootSize( const Vector2& size )
1528 mOvershootSize = size;
1530 if( mOvershootOverlay )
1532 // Remove old & add new size constraint
1533 mOvershootOverlay.RemoveConstraints( OVERSHOOT_SIZE_CONSTRAINT_TAG );
1534 ApplyOvershootSizeConstraint( mOvershootOverlay, mOvershootSize.height );
1538 void ItemView::SetOvershootEffectColor( const Vector4& color )
1540 mOvershootEffectColor = color;
1541 if( mOvershootOverlay )
1543 mOvershootOverlay.SetColor( color );
1547 void ItemView::EnableScrollOvershoot( bool enable )
1549 Actor self = Self();
1552 if( !mOvershootOverlay )
1554 Property::Index effectOvershootPropertyIndex = Property::INVALID_INDEX;
1555 mOvershootOverlay = CreateBouncingEffectActor( effectOvershootPropertyIndex );
1556 mOvershootOverlay.SetColor(mOvershootEffectColor);
1557 mOvershootOverlay.SetParentOrigin(ParentOrigin::TOP_LEFT);
1558 mOvershootOverlay.SetAnchorPoint(AnchorPoint::TOP_LEFT);
1559 mOvershootOverlay.SetDrawMode( DrawMode::OVERLAY_2D );
1560 self.Add(mOvershootOverlay);
1562 ApplyOvershootSizeConstraint( mOvershootOverlay, mOvershootSize.height );
1564 Constraint constraint = Constraint::New<Quaternion>( mOvershootOverlay, Actor::Property::ORIENTATION, OvershootOverlayRotationConstraint );
1565 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1566 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1567 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1570 constraint = Constraint::New<Vector3>( mOvershootOverlay, Actor::Property::POSITION, OvershootOverlayPositionConstraint );
1571 constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
1572 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::SCROLL_DIRECTION ) );
1573 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::LAYOUT_ORIENTATION ) );
1574 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1577 constraint = Constraint::New<bool>( mOvershootOverlay, Actor::Property::VISIBLE, OvershootOverlayVisibilityConstraint );
1578 constraint.AddSource( ParentSource( Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL ) );
1581 constraint = Constraint::New<float>( mOvershootOverlay, effectOvershootPropertyIndex, EqualToConstraint() );
1582 constraint.AddSource( ParentSource( Toolkit::ItemView::Property::OVERSHOOT ) );
1588 if( mOvershootOverlay )
1590 self.Remove(mOvershootOverlay);
1591 mOvershootOverlay.Reset();
1596 float ItemView::CalculateScrollOvershoot()
1598 float overshoot = 0.0f;
1602 // The overshoot must be calculated from the accumulated pan gesture displacement
1603 // since the pan gesture starts.
1604 Actor self = Self();
1605 float scrollDistance = CalculateScrollDistance(mTotalPanDisplacement, *mActiveLayout) * mActiveLayout->GetScrollSpeedFactor();
1606 float positionDelta = GetCurrentLayoutPosition(0) + scrollDistance;
1607 float minLayoutPosition = mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), Self().GetCurrentSize());
1608 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector2(0.0f, -minLayoutPosition));
1609 float clamppedPosition = std::min(0.0f, std::max(minLayoutPosition, positionDelta));
1610 overshoot = positionDelta - clamppedPosition;
1613 return overshoot > 0.0f ? std::min(overshoot, 1.0f) : std::max(overshoot, -1.0f);
1616 void ItemView::AnimateScrollOvershoot(float overshootAmount, bool animateBack)
1618 bool animatingOn = fabsf(overshootAmount) > Math::MACHINE_EPSILON_1;
1620 // make sure we animate back if needed
1621 mAnimateOvershootOff = animateBack || (!animatingOn && mAnimatingOvershootOn);
1623 if( mAnimatingOvershootOn )
1625 // animating on, do not allow animate off
1629 Actor self = Self();
1631 if(mOvershootAnimationSpeed > Math::MACHINE_EPSILON_0)
1633 float currentOvershoot = self.GetProperty<float>(Toolkit::ItemView::Property::OVERSHOOT);
1634 float duration = 0.0f;
1636 if (mOvershootOverlay)
1638 duration = mOvershootOverlay.GetCurrentSize().height * (animatingOn ? (1.0f - fabsf(currentOvershoot)) : fabsf(currentOvershoot)) / mOvershootAnimationSpeed;
1641 // Mark the animation as in progress to prevent manual property sets overwriting it.
1642 mInAnimation = true;
1643 mAnimatingOvershootOn = animatingOn;
1644 RemoveAnimation(mScrollOvershootAnimation);
1645 mScrollOvershootAnimation = Animation::New(duration);
1646 mScrollOvershootAnimation.FinishedSignal().Connect(this, &ItemView::OnOvershootOnFinished);
1647 mScrollOvershootAnimation.AnimateTo( Property(self, Toolkit::ItemView::Property::OVERSHOOT), overshootAmount, TimePeriod(0.0f, duration) );
1648 mScrollOvershootAnimation.Play();
1652 self.SetProperty( Toolkit::ItemView::Property::OVERSHOOT, overshootAmount );
1656 void ItemView::SetItemsParentOrigin( const Vector3& parentOrigin )
1658 if( parentOrigin != mItemsParentOrigin )
1660 mItemsParentOrigin = parentOrigin;
1661 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1663 iter->second.SetParentOrigin(parentOrigin);
1668 Vector3 ItemView::GetItemsParentOrigin() const
1670 return mItemsParentOrigin;
1673 void ItemView::SetItemsAnchorPoint( const Vector3& anchorPoint )
1675 if( anchorPoint != mItemsAnchorPoint )
1677 mItemsAnchorPoint = anchorPoint;
1678 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1680 iter->second.SetAnchorPoint(anchorPoint);
1685 Vector3 ItemView::GetItemsAnchorPoint() const
1687 return mItemsAnchorPoint;
1690 void ItemView::GetItemsRange(ItemRange& range)
1692 if( !mItemPool.empty() )
1694 range.begin = mItemPool.begin()->first;
1695 range.end = mItemPool.rbegin()->first + 1;
1704 bool ItemView::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1706 Dali::BaseHandle handle( object );
1708 bool connected( true );
1709 Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( handle );
1711 if( 0 == strcmp( signalName.c_str(), LAYOUT_ACTIVATED_SIGNAL ) )
1713 itemView.LayoutActivatedSignal().Connect( tracker, functor );
1717 // signalName does not match any signal
1724 void ItemView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
1726 Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( Dali::BaseHandle( object ) );
1730 ItemView& itemViewImpl( GetImpl( itemView ) );
1733 case Toolkit::ItemView::Property::MINIMUM_SWIPE_SPEED:
1735 itemViewImpl.SetMinimumSwipeSpeed( value.Get<float>() );
1738 case Toolkit::ItemView::Property::MINIMUM_SWIPE_DISTANCE:
1740 itemViewImpl.SetMinimumSwipeDistance( value.Get<float>() );
1743 case Toolkit::ItemView::Property::WHEEL_SCROLL_DISTANCE_STEP:
1745 itemViewImpl.SetWheelScrollDistanceStep( value.Get<float>() );
1748 case Toolkit::ItemView::Property::SNAP_TO_ITEM_ENABLED:
1750 itemViewImpl.SetAnchoring( value.Get<bool>() );
1753 case Toolkit::ItemView::Property::REFRESH_INTERVAL:
1755 itemViewImpl.SetRefreshInterval( value.Get<float>() );
1762 Property::Value ItemView::GetProperty( BaseObject* object, Property::Index index )
1764 Property::Value value;
1766 Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( Dali::BaseHandle( object ) );
1770 ItemView& itemViewImpl( GetImpl( itemView ) );
1773 case Toolkit::ItemView::Property::MINIMUM_SWIPE_SPEED:
1775 value = itemViewImpl.GetMinimumSwipeSpeed();
1778 case Toolkit::ItemView::Property::MINIMUM_SWIPE_DISTANCE:
1780 value = itemViewImpl.GetMinimumSwipeDistance();
1783 case Toolkit::ItemView::Property::WHEEL_SCROLL_DISTANCE_STEP:
1785 value = itemViewImpl.GetWheelScrollDistanceStep();
1788 case Toolkit::ItemView::Property::SNAP_TO_ITEM_ENABLED:
1790 value = itemViewImpl.GetAnchoring();
1793 case Toolkit::ItemView::Property::REFRESH_INTERVAL:
1795 value = itemViewImpl.GetRefreshInterval();
1804 bool ItemView::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
1806 Dali::BaseHandle handle( object );
1808 Toolkit::ItemView itemView = Toolkit::ItemView::DownCast( handle );
1810 DALI_ASSERT_ALWAYS( itemView );
1812 if( 0 == strcmp( actionName.c_str(), ACTION_STOP_SCROLLING ) )
1814 GetImpl( itemView ).DoStopScrolling();
1816 else if ( 0 == strcmp( actionName.c_str(), ACTION_ENABLE_REFRESH ) )
1818 GetImpl( itemView ).SetRefreshNotificationEnabled( true );
1820 else if ( 0 == strcmp( actionName.c_str(), ACTION_DISABLE_REFRESH ) )
1822 GetImpl( itemView ).SetRefreshNotificationEnabled( false );
1828 void ItemView::DoStopScrolling()
1830 if( mScrollAnimation )
1832 mScrollAnimation.Stop();
1833 mScrollAnimation.Reset();
1837 void ItemView::SetRefreshNotificationEnabled( bool enabled )
1839 mRefreshNotificationEnabled = enabled;
1842 } // namespace Internal
1844 } // namespace Toolkit