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>
23 #include <dali/public-api/animation/constraint.h>
24 #include <dali/public-api/animation/constraints.h>
25 #include <dali/public-api/common/set-wrapper.h>
26 #include <dali/public-api/common/stage.h>
27 #include <dali/public-api/events/mouse-wheel-event.h>
28 #include <dali/public-api/events/touch-event.h>
29 #include <dali/public-api/object/type-registry.h>
30 #include <dali/public-api/object/type-registry-helper.h>
33 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-factory.h>
34 #include <dali-toolkit/internal/controls/scrollable/bouncing-effect-actor.h>
35 #include <dali-toolkit/internal/controls/scrollable/scroll-connector-impl.h>
41 namespace // Unnamed namespace
46 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ItemView, Toolkit::Scrollable, NULL )
47 DALI_TYPE_REGISTRATION_END()
49 const float DEFAULT_MINIMUM_SWIPE_SPEED = 1.0f;
50 const float DEFAULT_MINIMUM_SWIPE_DISTANCE = 3.0f;
51 const float DEFAULT_MOUSE_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION = 0.1f;
53 const float DEFAULT_MINIMUM_SWIPE_DURATION = 0.45f;
54 const float DEFAULT_MAXIMUM_SWIPE_DURATION = 2.6f;
56 const float DEFAULT_REFRESH_INTERVAL_LAYOUT_POSITIONS = 20.0f; // 1 updates per 20 items
57 const int MOUSE_WHEEL_EVENT_FINISHED_TIME_OUT = 500; // 0.5 second
59 const float DEFAULT_ANCHORING_DURATION = 1.0f; // 1 second
61 const float MILLISECONDS_PER_SECONDS = 1000.0f;
63 const Vector2 OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE( 720.0f, 42.0f );
64 const float OVERSHOOT_BOUNCE_ACTOR_RESIZE_THRESHOLD = 180.0f;
65 const Vector4 OVERSHOOT_OVERLAY_NINE_PATCH_BORDER(0.0f, 0.0f, 1.0f, 12.0f);
66 const float DEFAULT_KEYBOARD_FOCUS_SCROLL_DURATION = 0.2f;
68 const string LAYOUT_POSITION_PROPERTY_NAME( "item-view-layout-position" );
69 const string POSITION_PROPERTY_NAME( "item-view-position" );
70 const string MINIMUM_LAYOUT_POSITION_PROPERTY_NAME( "item-view-minimum-layout-position" );
71 const string SCROLL_SPEED_PROPERTY_NAME( "item-view-scroll-speed" );
72 const string SCROLL_DIRECTION_PROPERTY_NAME( "item-view-scroll-direction" );
73 const string OVERSHOOT_PROPERTY_NAME( "item-view-overshoot" );
76 * Local helper to convert pan distance (in actor coordinates) to the layout-specific scrolling direction
78 float CalculateScrollDistance(Vector2 panDistance, Toolkit::ItemLayout& layout)
80 Radian scrollDirection(layout.GetScrollDirection());
82 float cosTheta = cosf(scrollDirection);
83 float sinTheta = sinf(scrollDirection);
85 return panDistance.x * sinTheta + panDistance.y * cosTheta;
88 // Overshoot overlay constraints
89 void OvershootOverlaySizeConstraint( Vector3& current, const PropertyInputContainer& inputs )
91 const Vector3& parentScrollDirection = inputs[0]->GetVector3();
92 const Vector3& parentSize = inputs[1]->GetVector3();
93 const Toolkit::ControlOrientation::Type& parentOrientation = static_cast<Toolkit::ControlOrientation::Type>(parentScrollDirection.z);
95 if(Toolkit::IsVertical(parentOrientation))
97 current.width = fabsf(parentScrollDirection.y) > Math::MACHINE_EPSILON_1 ? parentSize.x : parentSize.y;
101 current.width = fabsf(parentScrollDirection.x) > Math::MACHINE_EPSILON_1 ? parentSize.y : parentSize.x;
104 current.height = ( current.width > OVERSHOOT_BOUNCE_ACTOR_RESIZE_THRESHOLD ) ? OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE.height : OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE.height*0.5f;
107 void OvershootOverlayRotationConstraint( Quaternion& current, const PropertyInputContainer& inputs )
109 const Vector3& parentScrollDirection = inputs[0]->GetVector3();
110 const float parentOvershoot = inputs[1]->GetFloat();
111 const Toolkit::ControlOrientation::Type& parentOrientation = static_cast<Toolkit::ControlOrientation::Type>(parentScrollDirection.z);
113 float multiplier = 0;
114 if(Toolkit::IsVertical(parentOrientation))
116 if(fabsf(parentScrollDirection.y) <= Math::MACHINE_EPSILON_1)
118 if( (parentOrientation == Toolkit::ControlOrientation::Up && parentOvershoot < Math::MACHINE_EPSILON_0)
119 || (parentOrientation == 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( (parentOrientation == Toolkit::ControlOrientation::Left && parentOvershoot > Math::MACHINE_EPSILON_0)
143 ||(parentOrientation == 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 Vector3& parentScrollDirection = inputs[1]->GetVector3();
170 const float parentOvershoot = inputs[2]->GetFloat();
171 const Toolkit::ControlOrientation::Type& parentOrientation = static_cast<Toolkit::ControlOrientation::Type>(parentScrollDirection.z);
173 Vector3 relativeOffset;
175 if(Toolkit::IsVertical(parentOrientation))
177 if(fabsf(parentScrollDirection.y) <= Math::MACHINE_EPSILON_1)
179 if( (parentOrientation == Toolkit::ControlOrientation::Up && parentOvershoot < Math::MACHINE_EPSILON_0)
180 || (parentOrientation == 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( (parentOrientation == Toolkit::ControlOrientation::Left && parentOvershoot < Math::MACHINE_EPSILON_0)
204 || (parentOrientation == 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();
233 * Relative position Constraint
234 * Generates the relative position value of the item view based on the layout position,
235 * and it's relation to the layout domain. This is a value from 0.0f to 1.0f in each axis.
237 void RelativePositionConstraint( Vector3& current, const PropertyInputContainer& inputs )
239 const Vector3& position = Vector3(0.0f, inputs[0]->GetFloat(), 0.0f);
240 const Vector3& min = inputs[1]->GetVector3();
241 const Vector3& max = inputs[2]->GetVector3();
243 Vector3 domainSize = max - min;
245 current.x = fabsf(domainSize.x) > Math::MACHINE_EPSILON_1 ? ((min.x - position.x) / fabsf(domainSize.x)) : 0.0f;
246 current.y = fabsf(domainSize.y) > Math::MACHINE_EPSILON_1 ? ((min.y - position.y) / fabsf(domainSize.y)) : 0.0f;
250 } // unnamed namespace
261 namespace // unnamed namespace
264 bool FindById( const ItemContainer& items, ItemId id )
266 for( ConstItemIter iter = items.begin(); items.end() != iter; ++iter )
268 if( iter->first == id )
277 } // unnamed namespace
279 Dali::Toolkit::ItemView ItemView::New(ItemFactory& factory)
281 // Create the implementation
282 ItemViewPtr itemView(new ItemView(factory));
284 // Pass ownership to CustomActor via derived handle
285 Dali::Toolkit::ItemView handle(*itemView);
287 // Second-phase init of the implementation
288 // This can only be done after the CustomActor connection has been made...
289 itemView->Initialize();
294 ItemView::ItemView(ItemFactory& factory)
296 mItemFactory(factory),
298 mAnimatingOvershootOn(false),
299 mAnimateOvershootOff(false),
300 mAnchoringEnabled(true),
301 mAnchoringDuration(DEFAULT_ANCHORING_DURATION),
302 mRefreshIntervalLayoutPositions(0.0f),
303 mRefreshOrderHint(true/*Refresh item 0 first*/),
304 mMinimumSwipeSpeed(DEFAULT_MINIMUM_SWIPE_SPEED),
305 mMinimumSwipeDistance(DEFAULT_MINIMUM_SWIPE_DISTANCE),
306 mMouseWheelScrollDistanceStep(0.0f),
307 mScrollDistance(0.0f),
309 mTotalPanDisplacement(Vector2::ZERO),
310 mScrollOvershoot(0.0f),
312 mGestureState(Gesture::Clear),
314 mPropertyPosition(Property::INVALID_INDEX),
315 mPropertyMinimumLayoutPosition(Property::INVALID_INDEX),
316 mPropertyScrollSpeed(Property::INVALID_INDEX),
317 mRefreshEnabled(true),
318 mItemsParentOrigin( ParentOrigin::CENTER),
319 mItemsAnchorPoint( AnchorPoint::CENTER)
321 SetRequiresMouseWheelEvents(true);
322 SetKeyboardNavigationSupport(true);
325 void ItemView::OnInitialize()
329 // Disable size negotiation for item views
330 self.SetRelayoutEnabled( false );
332 mScrollConnector = Dali::Toolkit::ScrollConnector::New();
333 mScrollPositionObject = mScrollConnector.GetScrollPositionObject();
334 mScrollConnector.ScrollPositionChangedSignal().Connect( this, &ItemView::OnScrollPositionChanged );
336 mPropertyMinimumLayoutPosition = self.RegisterProperty(MINIMUM_LAYOUT_POSITION_PROPERTY_NAME, 0.0f);
337 mPropertyPosition = self.RegisterProperty(POSITION_PROPERTY_NAME, 0.0f);
338 mPropertyScrollSpeed = self.RegisterProperty(SCROLL_SPEED_PROPERTY_NAME, 0.0f);
340 EnableScrollComponent(Toolkit::Scrollable::OvershootIndicator);
342 Constraint constraint = Constraint::New<Vector3>( self, Toolkit::Scrollable::Property::SCROLL_RELATIVE_POSITION, RelativePositionConstraint );
343 constraint.AddSource( LocalSource( mPropertyPosition ) );
344 constraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MIN ) );
345 constraint.AddSource( LocalSource( Toolkit::Scrollable::Property::SCROLL_POSITION_MAX ) );
348 Vector2 stageSize = Stage::GetCurrent().GetSize();
349 mMouseWheelScrollDistanceStep = stageSize.y * DEFAULT_MOUSE_WHEEL_SCROLL_DISTANCE_STEP_PROPORTION;
351 EnableGestureDetection(Gesture::Type(Gesture::Pan));
353 mMouseWheelEventFinishedTimer = Timer::New( MOUSE_WHEEL_EVENT_FINISHED_TIME_OUT );
354 mMouseWheelEventFinishedTimer.TickSignal().Connect( this, &ItemView::OnMouseWheelEventFinished );
356 SetRefreshInterval(DEFAULT_REFRESH_INTERVAL_LAYOUT_POSITIONS);
359 ItemView::~ItemView()
363 Dali::Toolkit::ScrollConnector ItemView::GetScrollConnector() const
365 return mScrollConnector;
368 unsigned int ItemView::GetLayoutCount() const
370 return mLayouts.size();
373 void ItemView::AddLayout(ItemLayout& layout)
375 mLayouts.push_back(ItemLayoutPtr(&layout));
378 void ItemView::RemoveLayout(unsigned int layoutIndex)
380 DALI_ASSERT_ALWAYS(layoutIndex < mLayouts.size());
382 if (mActiveLayout == mLayouts[layoutIndex].Get())
384 mActiveLayout = NULL;
387 mLayouts.erase(mLayouts.begin() + layoutIndex);
390 ItemLayoutPtr ItemView::GetLayout(unsigned int layoutIndex) const
392 return mLayouts[layoutIndex];
395 ItemLayoutPtr ItemView::GetActiveLayout() const
397 return ItemLayoutPtr(mActiveLayout);
400 float ItemView::GetCurrentLayoutPosition(unsigned int itemId) const
402 return mScrollConnector.GetScrollPosition() + static_cast<float>( itemId );
405 void ItemView::ActivateLayout(unsigned int layoutIndex, const Vector3& targetSize, float durationSeconds)
407 DALI_ASSERT_ALWAYS(layoutIndex < mLayouts.size());
409 mRefreshEnabled = false;
413 // The ItemView size should match the active layout size
414 self.SetSize(targetSize);
415 mActiveLayoutTargetSize = targetSize;
417 // Switch to the new layout
418 mActiveLayout = mLayouts[layoutIndex].Get();
420 // Move the items to the new layout positions...
422 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
424 unsigned int itemId = iter->first;
425 Actor actor = iter->second;
427 // Remove constraints from previous layout
428 actor.RemoveConstraints();
431 if(mActiveLayout->GetItemSize(itemId, targetSize, size))
433 // resize immediately
434 actor.SetSize( size.GetVectorXY() );
437 mActiveLayout->ApplyConstraints(actor, itemId, durationSeconds, mScrollPositionObject, Self() );
440 // Refresh the new layout
441 ItemRange range = GetItemRange(*mActiveLayout, targetSize, GetCurrentLayoutPosition(0), false/* don't reserve extra*/);
442 AddActorsWithinRange( range, durationSeconds );
444 // Scroll to an appropriate layout position
446 bool scrollAnimationNeeded(false);
447 float firstItemScrollPosition(0.0f);
449 float current = GetCurrentLayoutPosition(0);
450 float minimum = ClampFirstItemPosition(current, targetSize, *mActiveLayout);
451 self.SetProperty(mPropertyPosition, GetScrollPosition(current, targetSize));
453 if (current < minimum)
455 scrollAnimationNeeded = true;
456 firstItemScrollPosition = minimum;
458 else if (mAnchoringEnabled)
460 scrollAnimationNeeded = true;
461 firstItemScrollPosition = mActiveLayout->GetClosestAnchorPosition(current);
464 if (scrollAnimationNeeded)
466 RemoveAnimation(mScrollAnimation);
467 mScrollAnimation = Animation::New(durationSeconds);
468 mScrollAnimation.AnimateTo( Property( mScrollPositionObject, ScrollConnector::SCROLL_POSITION ), firstItemScrollPosition, AlphaFunctions::EaseOut );
469 mScrollAnimation.AnimateTo( Property(self, mPropertyPosition), GetScrollPosition(firstItemScrollPosition, targetSize), AlphaFunctions::EaseOut );
470 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnLayoutActivationScrollFinished);
471 mScrollAnimation.Play();
474 self.SetProperty(mPropertyMinimumLayoutPosition, mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), targetSize));
475 AnimateScrollOvershoot(0.0f);
476 mScrollOvershoot = 0.0f;
478 Radian scrollDirection(mActiveLayout->GetScrollDirection());
479 float orientation = static_cast<float>(mActiveLayout->GetOrientation());
480 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_DIRECTION, Vector3(sinf(scrollDirection), cosf(scrollDirection), orientation));
482 self.SetProperty(mPropertyScrollSpeed, mScrollSpeed);
484 CalculateDomainSize(targetSize);
487 void ItemView::DeactivateCurrentLayout()
491 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
493 Actor actor = iter->second;
494 actor.RemoveConstraints();
497 mActiveLayout = NULL;
501 void ItemView::OnRefreshNotification(PropertyNotification& source)
503 if(mRefreshEnabled || mScrollAnimation)
505 // Only refresh the cache during normal scrolling
506 DoRefresh(GetCurrentLayoutPosition(0), true);
510 void ItemView::Refresh()
512 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter )
514 ReleaseActor( iter->first, iter->second );
518 DoRefresh(GetCurrentLayoutPosition(0), true);
521 void ItemView::DoRefresh(float currentLayoutPosition, bool cacheExtra)
525 ItemRange range = GetItemRange(*mActiveLayout, mActiveLayoutTargetSize, currentLayoutPosition, cacheExtra/*reserve extra*/);
526 RemoveActorsOutsideRange( range );
527 AddActorsWithinRange( range, 0.0f/*immediate*/ );
529 mScrollUpdatedSignal.Emit( Vector3(0.0f, currentLayoutPosition, 0.0f) );
533 void ItemView::SetMinimumSwipeSpeed(float speed)
535 mMinimumSwipeSpeed = speed;
538 float ItemView::GetMinimumSwipeSpeed() const
540 return mMinimumSwipeSpeed;
543 void ItemView::SetMinimumSwipeDistance(float distance)
545 mMinimumSwipeDistance = distance;
548 float ItemView::GetMinimumSwipeDistance() const
550 return mMinimumSwipeDistance;
553 void ItemView::SetMouseWheelScrollDistanceStep(float step)
555 mMouseWheelScrollDistanceStep = step;
558 float ItemView::GetMouseWheelScrollDistanceStep() const
560 return mMouseWheelScrollDistanceStep;
563 void ItemView::SetAnchoring(bool enabled)
565 mAnchoringEnabled = enabled;
568 bool ItemView::GetAnchoring() const
570 return mAnchoringEnabled;
573 void ItemView::SetAnchoringDuration(float durationSeconds)
575 mAnchoringDuration = durationSeconds;
578 float ItemView::GetAnchoringDuration() const
580 return mAnchoringDuration;
583 void ItemView::SetRefreshInterval(float intervalLayoutPositions)
585 if( !Equals(mRefreshIntervalLayoutPositions, intervalLayoutPositions) )
587 mRefreshIntervalLayoutPositions = intervalLayoutPositions;
589 if(mRefreshNotification)
591 mScrollPositionObject.RemovePropertyNotification(mRefreshNotification);
593 mRefreshNotification = mScrollPositionObject.AddPropertyNotification( ScrollConnector::SCROLL_POSITION, StepCondition(mRefreshIntervalLayoutPositions, 0.0f) );
594 mRefreshNotification.NotifySignal().Connect( this, &ItemView::OnRefreshNotification );
598 float ItemView::GetRefreshInterval() const
600 return mRefreshIntervalLayoutPositions;
603 void ItemView::SetRefreshEnabled(bool enabled)
605 mRefreshEnabled = enabled;
608 Actor ItemView::GetItem(unsigned int itemId) const
612 ConstItemPoolIter iter = mItemPool.find( itemId );
613 if( iter != mItemPool.end() )
615 actor = iter->second;
621 unsigned int ItemView::GetItemId( Actor actor ) const
623 unsigned int itemId( 0 );
625 for ( ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter )
627 if( iter->second == actor )
629 itemId = iter->first;
637 void ItemView::InsertItem( Item newItem, float durationSeconds )
641 Actor displacedActor;
642 ItemPoolIter afterDisplacedIter = mItemPool.end();
644 ItemPoolIter foundIter = mItemPool.find( newItem.first );
645 if( mItemPool.end() != foundIter )
647 SetupActor( newItem, durationSeconds );
648 Self().Add( newItem.second );
650 displacedActor = foundIter->second;
651 foundIter->second = newItem.second;
653 afterDisplacedIter = ++foundIter;
657 // Inserting before the existing item range?
658 ItemPoolIter iter = mItemPool.begin();
659 if( iter != mItemPool.end() &&
660 iter->first > newItem.first )
662 displacedActor = iter->second;
663 mItemPool.erase( iter++ ); // iter is still valid after the erase
665 afterDisplacedIter = iter;
671 // Move the existing actors to make room
672 for( ItemPoolIter iter = afterDisplacedIter; mItemPool.end() != iter; ++iter )
674 Actor temp = iter->second;
675 iter->second = displacedActor;
676 displacedActor = temp;
678 iter->second.RemoveConstraints();
679 mActiveLayout->ApplyConstraints( iter->second, iter->first, durationSeconds, mScrollPositionObject, Self() );
683 ItemPool::reverse_iterator lastIter = mItemPool.rbegin();
684 if ( lastIter != mItemPool.rend() )
686 ItemId lastId = lastIter->first;
687 Item lastItem( lastId + 1, displacedActor );
688 mItemPool.insert( lastItem );
690 lastItem.second.RemoveConstraints();
691 mActiveLayout->ApplyConstraints( lastItem.second, lastItem.first, durationSeconds, mScrollPositionObject, Self() );
695 CalculateDomainSize(Self().GetCurrentSize());
697 mAddingItems = false;
700 void ItemView::InsertItems( const ItemContainer& newItems, float durationSeconds )
704 // Insert from lowest id to highest
705 std::set<Item> sortedItems;
706 for( ConstItemIter iter = newItems.begin(); newItems.end() != iter; ++iter )
708 sortedItems.insert( *iter );
711 for( std::set<Item>::iterator iter = sortedItems.begin(); sortedItems.end() != iter; ++iter )
713 Self().Add( iter->second );
715 ItemPoolIter foundIter = mItemPool.find( iter->first );
716 if( mItemPool.end() != foundIter )
718 Actor moveMe = foundIter->second;
719 foundIter->second = iter->second;
721 // Move the existing actors to make room
722 for( ItemPoolIter iter = ++foundIter; mItemPool.end() != iter; ++iter )
724 Actor temp = iter->second;
725 iter->second = moveMe;
730 ItemId lastId = mItemPool.rbegin()->first;
731 Item lastItem( lastId + 1, moveMe );
732 mItemPool.insert( lastItem );
736 mItemPool.insert( *iter );
740 // Relayout everything
741 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
744 if( FindById( newItems, iter->first ) )
746 SetupActor( *iter, durationSeconds );
750 iter->second.RemoveConstraints();
751 mActiveLayout->ApplyConstraints( iter->second, iter->first, durationSeconds, mScrollPositionObject, Self() );
755 CalculateDomainSize(Self().GetCurrentSize());
757 mAddingItems = false;
760 void ItemView::RemoveItem( unsigned int itemId, float durationSeconds )
762 bool actorsReordered = RemoveActor( itemId );
763 if( actorsReordered )
765 ReapplyAllConstraints( durationSeconds );
771 void ItemView::RemoveItems( const ItemIdContainer& itemIds, float durationSeconds )
773 bool actorsReordered( false );
775 // Remove from highest id to lowest
776 set<ItemId> sortedItems;
777 for( ConstItemIdIter iter = itemIds.begin(); itemIds.end() != iter; ++iter )
779 sortedItems.insert( *iter );
782 for( set<ItemId>::reverse_iterator iter = sortedItems.rbegin(); sortedItems.rend() != iter; ++iter )
784 if( RemoveActor( *iter ) )
786 actorsReordered = true;
790 if( actorsReordered )
792 ReapplyAllConstraints( durationSeconds );
798 bool ItemView::RemoveActor(unsigned int itemId)
800 bool reordered( false );
802 ItemPoolIter removeIter = mItemPool.find( itemId );
803 if( removeIter != mItemPool.end() )
805 ReleaseActor(itemId, removeIter->second);
809 // Removing before the existing item range?
810 ItemPoolIter iter = mItemPool.begin();
811 if( iter != mItemPool.end() &&
812 iter->first > itemId )
814 // In order to decrement the first visible item ID
815 mItemPool.insert( Item(iter->first - 1, Actor()) );
817 removeIter = mItemPool.begin();
821 if( removeIter != mItemPool.end() )
825 // Adjust the remaining item IDs, for example if item 2 is removed:
826 // Initial actors: After insert:
827 // ID 1 - ActorA ID 1 - ActorA
828 // ID 2 - ActorB ID 2 - ActorC (previously ID 3)
829 // ID 3 - ActorC ID 3 - ActorB (previously ID 4)
831 for (ItemPoolIter iter = removeIter; iter != mItemPool.end(); ++iter)
833 if( iter->first < mItemPool.rbegin()->first )
835 iter->second = mItemPool[ iter->first + 1 ];
839 mItemPool.erase( iter );
848 void ItemView::ReplaceItem( Item replacementItem, float durationSeconds )
852 SetupActor( replacementItem, durationSeconds );
853 Self().Add( replacementItem.second );
855 const ItemPoolIter iter = mItemPool.find( replacementItem.first );
856 if( mItemPool.end() != iter )
858 ReleaseActor(iter->first, iter->second);
859 iter->second = replacementItem.second;
863 mItemPool.insert( replacementItem );
866 CalculateDomainSize(Self().GetCurrentSize());
868 mAddingItems = false;
871 void ItemView::ReplaceItems( const ItemContainer& replacementItems, float durationSeconds )
873 for( ConstItemIter iter = replacementItems.begin(); replacementItems.end() != iter; ++iter )
875 ReplaceItem( *iter, durationSeconds );
879 void ItemView::RemoveActorsOutsideRange( ItemRange range )
881 // Remove unwanted actors from the ItemView & ItemPool
882 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); )
884 unsigned int current = iter->first;
886 if( ! range.Within( current ) )
888 ReleaseActor(iter->first, iter->second);
890 mItemPool.erase( iter++ ); // erase invalidates the return value of post-increment; iter remains valid
899 void ItemView::AddActorsWithinRange( ItemRange range, float durationSeconds )
901 range.end = std::min(mItemFactory.GetNumberOfItems(), range.end);
903 // The order of addition depends on the scroll direction.
904 if (mRefreshOrderHint)
906 for (unsigned int itemId = range.begin; itemId < range.end; ++itemId)
908 AddNewActor( itemId, durationSeconds );
913 for (unsigned int itemId = range.end; itemId > range.begin; --itemId)
915 AddNewActor( itemId-1, durationSeconds );
919 // Total number of items may change dynamically.
920 // Always recalculate the domain size to reflect that.
921 CalculateDomainSize(Self().GetCurrentSize());
924 void ItemView::AddNewActor( unsigned int itemId, float durationSeconds )
928 if( mItemPool.end() == mItemPool.find( itemId ) )
930 Actor actor = mItemFactory.NewItem( itemId );
934 Item newItem( itemId, actor );
936 mItemPool.insert( newItem );
938 SetupActor( newItem, durationSeconds );
943 mAddingItems = false;
946 void ItemView::SetupActor( Item item, float durationSeconds )
948 item.second.SetParentOrigin( mItemsParentOrigin );
949 item.second.SetAnchorPoint( mItemsAnchorPoint );
954 if( mActiveLayout->GetItemSize( item.first, mActiveLayoutTargetSize, size ) )
956 item.second.SetSize( size.GetVectorXY() );
959 mActiveLayout->ApplyConstraints( item.second, item.first, durationSeconds, mScrollPositionObject, Self() );
963 void ItemView::ReleaseActor( ItemId item, Actor actor )
965 Self().Remove( actor );
966 mItemFactory.ItemReleased(item, actor);
969 ItemRange ItemView::GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra)
971 unsigned int itemCount = mItemFactory.GetNumberOfItems();
973 ItemRange available(0u, itemCount);
975 ItemRange range = layout.GetItemsWithinArea( layoutPosition, layoutSize );
979 // Add the reserve items for scrolling
980 unsigned int extra = layout.GetReserveItemCount(layoutSize);
981 range.begin = (range.begin >= extra) ? (range.begin - extra) : 0u;
985 return range.Intersection(available);
988 void ItemView::OnChildAdd(Actor& child)
992 // We don't want to do this downcast check for any item added by ItemView itself.
993 Dali::Toolkit::ScrollComponent scrollComponent = Dali::Toolkit::ScrollComponent::DownCast(child);
996 // Set the scroll connector when scroll bar is being added
997 scrollComponent.SetScrollConnector(mScrollConnector);
1002 bool ItemView::OnTouchEvent(const TouchEvent& event)
1004 // Ignore events with multiple-touch points
1005 if (event.GetPointCount() != 1)
1010 if (event.GetPoint(0).state == TouchPoint::Down)
1012 // Cancel ongoing scrolling etc.
1013 mGestureState = Gesture::Clear;
1015 mScrollDistance = 0.0f;
1016 mScrollSpeed = 0.0f;
1017 Self().SetProperty(mPropertyScrollSpeed, mScrollSpeed);
1019 mScrollOvershoot = 0.0f;
1020 AnimateScrollOvershoot(0.0f);
1022 if(mScrollAnimation)
1024 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1027 RemoveAnimation(mScrollAnimation);
1030 return true; // consume since we're potentially scrolling
1033 bool ItemView::OnMouseWheelEvent(const MouseWheelEvent& event)
1035 // Respond the mouse wheel event to scroll
1038 Actor self = Self();
1039 const Vector3 layoutSize = Self().GetCurrentSize();
1040 float layoutPositionDelta = GetCurrentLayoutPosition(0) - (event.z * mMouseWheelScrollDistanceStep * mActiveLayout->GetScrollSpeedFactor());
1041 float firstItemScrollPosition = ClampFirstItemPosition(layoutPositionDelta, layoutSize, *mActiveLayout);
1043 mScrollPositionObject.SetProperty( ScrollConnector::SCROLL_POSITION, firstItemScrollPosition );
1044 self.SetProperty(mPropertyPosition, GetScrollPosition(firstItemScrollPosition, layoutSize));
1045 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1046 mRefreshEnabled = true;
1049 if (mMouseWheelEventFinishedTimer.IsRunning())
1051 mMouseWheelEventFinishedTimer.Stop();
1054 mMouseWheelEventFinishedTimer.Start();
1059 bool ItemView::OnMouseWheelEventFinished()
1063 RemoveAnimation(mScrollAnimation);
1065 // No more mouse wheel events coming. Do the anchoring if enabled.
1066 mScrollAnimation = DoAnchoring();
1067 if (mScrollAnimation)
1069 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1070 mScrollAnimation.Play();
1074 mScrollOvershoot = 0.0f;
1075 AnimateScrollOvershoot(0.0f);
1077 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1084 void ItemView::ReapplyAllConstraints( float durationSeconds )
1086 for (ConstItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1088 unsigned int id = iter->first;
1089 Actor actor = iter->second;
1091 actor.RemoveConstraints();
1092 mActiveLayout->ApplyConstraints(actor, id, durationSeconds, mScrollPositionObject, Self());
1096 void ItemView::OnItemsRemoved()
1098 CalculateDomainSize(Self().GetCurrentSize());
1100 // Adjust scroll-position after an item is removed
1103 float firstItemScrollPosition = ClampFirstItemPosition(GetCurrentLayoutPosition(0), Self().GetCurrentSize(), *mActiveLayout);
1105 mScrollPositionObject.SetProperty( ScrollConnector::SCROLL_POSITION, firstItemScrollPosition );
1109 float ItemView::ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout)
1111 Actor self = Self();
1112 float minLayoutPosition = layout.GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), targetSize);
1113 float clamppedPosition = std::min(0.0f, std::max(minLayoutPosition, targetPosition));
1114 mScrollOvershoot = targetPosition - clamppedPosition;
1115 self.SetProperty(mPropertyMinimumLayoutPosition, minLayoutPosition);
1117 return clamppedPosition;
1120 void ItemView::OnPan( const PanGesture& gesture )
1122 Actor self = Self();
1123 const Vector3 layoutSize = Self().GetCurrentSize();
1125 RemoveAnimation(mScrollAnimation);
1127 // Short-circuit if there is no active layout
1130 mGestureState = Gesture::Clear;
1134 mGestureState = gesture.state;
1136 switch (mGestureState)
1138 case Gesture::Finished:
1141 if (fabsf(mScrollDistance) > mMinimumSwipeDistance &&
1142 mScrollSpeed > mMinimumSwipeSpeed)
1144 float direction = (mScrollDistance < 0.0f) ? -1.0f : 1.0f;
1146 mRefreshOrderHint = true;
1148 float currentLayoutPosition = GetCurrentLayoutPosition(0);
1149 float firstItemScrollPosition = ClampFirstItemPosition(currentLayoutPosition + mScrollSpeed * direction,
1153 if (mAnchoringEnabled)
1155 firstItemScrollPosition = mActiveLayout->GetClosestAnchorPosition(firstItemScrollPosition);
1158 RemoveAnimation(mScrollAnimation);
1160 float flickAnimationDuration = Clamp( mActiveLayout->GetItemFlickAnimationDuration() * std::max(1.0f, fabsf(firstItemScrollPosition - GetCurrentLayoutPosition(0)))
1161 , DEFAULT_MINIMUM_SWIPE_DURATION, DEFAULT_MAXIMUM_SWIPE_DURATION);
1163 mScrollAnimation = Animation::New(flickAnimationDuration);
1164 mScrollAnimation.AnimateTo( Property( mScrollPositionObject, ScrollConnector::SCROLL_POSITION ), firstItemScrollPosition, AlphaFunctions::EaseOut );
1165 mScrollAnimation.AnimateTo( Property(self, mPropertyPosition), GetScrollPosition(firstItemScrollPosition, layoutSize), AlphaFunctions::EaseOut );
1166 mScrollAnimation.AnimateTo( Property(self, mPropertyScrollSpeed), 0.0f, AlphaFunctions::EaseOut );
1169 // Check whether it has already scrolled to the end
1170 if(fabs(currentLayoutPosition - firstItemScrollPosition) > Math::MACHINE_EPSILON_0)
1172 AnimateScrollOvershoot(0.0f);
1176 // Anchoring may be triggered when there was no swipe
1177 if (!mScrollAnimation)
1179 mScrollAnimation = DoAnchoring();
1182 // Reset the overshoot if no scroll animation.
1183 if (!mScrollAnimation)
1185 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1187 AnimateScrollOvershoot(0.0f, false);
1192 case Gesture::Started: // Fall through
1194 mTotalPanDisplacement = Vector2::ZERO;
1195 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1196 mRefreshEnabled = true;
1199 case Gesture::Continuing:
1201 mScrollDistance = CalculateScrollDistance(gesture.displacement, *mActiveLayout);
1202 mScrollSpeed = Clamp((gesture.GetSpeed() * gesture.GetSpeed() * mActiveLayout->GetFlickSpeedFactor() * MILLISECONDS_PER_SECONDS), 0.0f, mActiveLayout->GetMaximumSwipeSpeed());
1204 // Refresh order depends on the direction of the scroll; negative is towards the last item.
1205 mRefreshOrderHint = mScrollDistance < 0.0f;
1207 float layoutPositionDelta = GetCurrentLayoutPosition(0) + (mScrollDistance * mActiveLayout->GetScrollSpeedFactor());
1209 float firstItemScrollPosition = ClampFirstItemPosition(layoutPositionDelta, layoutSize, *mActiveLayout);
1211 float currentOvershoot = mScrollPositionObject.GetProperty<float>(ScrollConnector::OVERSHOOT);
1213 mScrollPositionObject.SetProperty( ScrollConnector::SCROLL_POSITION, firstItemScrollPosition );
1214 self.SetProperty(mPropertyPosition, GetScrollPosition(firstItemScrollPosition, layoutSize));
1216 if( (firstItemScrollPosition >= 0.0f && currentOvershoot < 1.0f) || (firstItemScrollPosition <= mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), layoutSize) && currentOvershoot > -1.0f) )
1218 mTotalPanDisplacement += gesture.displacement;
1221 mScrollOvershoot = CalculateScrollOvershoot();
1222 mScrollPositionObject.SetProperty( ScrollConnector::OVERSHOOT, mScrollOvershoot );
1226 case Gesture::Cancelled:
1228 mScrollAnimation = DoAnchoring();
1236 if (mScrollAnimation)
1238 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1239 mScrollAnimation.Play();
1243 bool ItemView::OnAccessibilityPan(PanGesture gesture)
1249 Actor ItemView::GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled)
1251 Actor nextFocusActor;
1255 if(!actor || actor == this->Self())
1257 nextFocusActor = GetItem(nextItemID);
1259 else if(actor && actor.GetParent() == this->Self())
1261 int itemID = GetItemId(actor);
1262 nextItemID = mActiveLayout->GetNextFocusItemID(itemID, mItemFactory.GetNumberOfItems(), direction, loopEnabled);
1263 nextFocusActor = GetItem(nextItemID);
1264 if(nextFocusActor == actor)
1266 // need to pass NULL actor back to focus manager
1267 nextFocusActor.Reset();
1268 return nextFocusActor;
1271 float layoutPosition = mActiveLayout->GetClosestAnchorPosition( GetCurrentLayoutPosition(0) );
1272 Vector3 layoutSize = Self().GetCurrentSize();
1275 // likely the current item is not buffered, so not in our item pool, probably best to get first viewable item
1276 ItemRange viewableItems = mActiveLayout->GetItemsWithinArea(layoutPosition, layoutSize);
1277 nextItemID = viewableItems.begin;
1278 nextFocusActor = GetItem(nextItemID);
1281 return nextFocusActor;
1284 void ItemView::OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor)
1286 // only in this function if our chosen focus actor was actually used
1287 if(commitedFocusableActor)
1289 int nextItemID = GetItemId(commitedFocusableActor);
1290 float layoutPosition = GetCurrentLayoutPosition(0);
1291 Vector3 layoutSize = Self().GetCurrentSize();
1293 float scrollTo = mActiveLayout->GetClosestOnScreenLayoutPosition(nextItemID, layoutPosition, layoutSize);
1294 ScrollTo(Vector3(0.0f, scrollTo, 0.0f), DEFAULT_KEYBOARD_FOCUS_SCROLL_DURATION);
1298 Animation ItemView::DoAnchoring()
1300 Animation anchoringAnimation;
1301 Actor self = Self();
1303 if (mActiveLayout && mAnchoringEnabled)
1305 float anchorPosition = mActiveLayout->GetClosestAnchorPosition( GetCurrentLayoutPosition(0) );
1307 anchoringAnimation = Animation::New(mAnchoringDuration);
1308 anchoringAnimation.AnimateTo( Property( mScrollPositionObject, ScrollConnector::SCROLL_POSITION ), anchorPosition, AlphaFunctions::EaseOut );
1309 anchoringAnimation.AnimateTo( Property(self, mPropertyPosition), GetScrollPosition(anchorPosition, self.GetCurrentSize()), AlphaFunctions::EaseOut );
1310 anchoringAnimation.AnimateTo( Property(self, mPropertyScrollSpeed), 0.0f, AlphaFunctions::EaseOut );
1313 AnimateScrollOvershoot(0.0f);
1317 return anchoringAnimation;
1320 void ItemView::OnScrollFinished(Animation& source)
1322 Actor self = Self();
1324 RemoveAnimation(mScrollAnimation); // mScrollAnimation is used to query whether we're scrolling
1326 mScrollCompletedSignal.Emit(GetCurrentScrollPosition());
1328 if(mIsFlicking && fabsf(mScrollOvershoot) > Math::MACHINE_EPSILON_1)
1330 AnimateScrollOvershoot( mScrollOvershoot > 0.0f ? 1.0f : -1.0f, true);
1334 // Reset the overshoot
1335 AnimateScrollOvershoot( 0.0f );
1337 mIsFlicking = false;
1339 mScrollOvershoot = 0.0f;
1342 void ItemView::OnLayoutActivationScrollFinished(Animation& source)
1344 RemoveAnimation(mScrollAnimation);
1345 mRefreshEnabled = true;
1346 DoRefresh(GetCurrentLayoutPosition(0), true);
1349 void ItemView::OnOvershootOnFinished(Animation& animation)
1351 mAnimatingOvershootOn = false;
1352 mScrollOvershootAnimation.FinishedSignal().Disconnect(this, &ItemView::OnOvershootOnFinished);
1353 RemoveAnimation(mScrollOvershootAnimation);
1354 if(mAnimateOvershootOff)
1356 AnimateScrollOvershoot(0.0f);
1360 void ItemView::ScrollToItem(unsigned int itemId, float durationSeconds)
1362 Actor self = Self();
1363 const Vector3 layoutSize = Self().GetCurrentSize();
1364 float firstItemScrollPosition = ClampFirstItemPosition(mActiveLayout->GetItemScrollToPosition(itemId), layoutSize, *mActiveLayout);
1366 if(durationSeconds > 0.0f)
1368 RemoveAnimation(mScrollAnimation);
1369 mScrollAnimation = Animation::New(durationSeconds);
1370 mScrollAnimation.AnimateTo( Property( mScrollPositionObject, ScrollConnector::SCROLL_POSITION ), firstItemScrollPosition, AlphaFunctions::EaseOut );
1371 mScrollAnimation.AnimateTo( Property(self, mPropertyPosition), GetScrollPosition(firstItemScrollPosition, layoutSize), AlphaFunctions::EaseOut );
1372 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1373 mScrollAnimation.Play();
1377 mScrollPositionObject.SetProperty( ScrollConnector::SCROLL_POSITION, firstItemScrollPosition );
1378 AnimateScrollOvershoot(0.0f);
1381 mScrollStartedSignal.Emit(GetCurrentScrollPosition());
1382 mRefreshEnabled = true;
1385 void ItemView::RemoveAnimation(Animation& animation)
1389 // Cease animating, and reset handle.
1395 void ItemView::CalculateDomainSize(const Vector3& layoutSize)
1397 Actor self = Self();
1399 Vector3 firstItemPosition(Vector3::ZERO);
1400 Vector3 lastItemPosition(Vector3::ZERO);
1404 firstItemPosition = mActiveLayout->GetItemPosition( 0,0,layoutSize );
1406 float minLayoutPosition = mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), layoutSize);
1407 self.SetProperty(mPropertyMinimumLayoutPosition, minLayoutPosition);
1408 lastItemPosition = mActiveLayout->GetItemPosition( fabs(minLayoutPosition),fabs(minLayoutPosition),layoutSize );
1412 if(IsHorizontal(mActiveLayout->GetOrientation()))
1414 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MIN, Vector3(0.0f, firstItemPosition.x, 0.0f));
1415 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector3(0.0f, lastItemPosition.x, 0.0f));
1416 domainSize = fabs(firstItemPosition.x - lastItemPosition.x);
1420 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MIN, Vector3(0.0f, firstItemPosition.y, 0.0f));
1421 self.SetProperty(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX, Vector3(0.0f, lastItemPosition.y, 0.0f));
1422 domainSize = fabs(firstItemPosition.y - lastItemPosition.y);
1425 mScrollConnector.SetScrollDomain(minLayoutPosition, 0.0f, domainSize);
1427 bool isLayoutScrollable = IsLayoutScrollable(layoutSize);
1428 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL, isLayoutScrollable);
1429 self.SetProperty(Toolkit::Scrollable::Property::CAN_SCROLL_HORIZONTAL, false);
1433 Vector3 ItemView::GetDomainSize() const
1435 Actor self = Self();
1437 float minScrollPosition = self.GetProperty<float>(Toolkit::Scrollable::Property::SCROLL_POSITION_MIN);
1438 float maxScrollPosition = self.GetProperty<float>(Toolkit::Scrollable::Property::SCROLL_POSITION_MAX);
1440 return Vector3(0.0f, fabs(maxScrollPosition - minScrollPosition), 0.0f);
1443 bool ItemView::IsLayoutScrollable(const Vector3& layoutSize)
1445 Actor self = Self();
1447 float currentLayoutPosition = ClampFirstItemPosition( GetCurrentLayoutPosition(0), layoutSize, *mActiveLayout );
1448 float forwardClampedPosition = ClampFirstItemPosition(currentLayoutPosition + 1.0, layoutSize, *mActiveLayout);
1449 float backwardClampedPosition = ClampFirstItemPosition(currentLayoutPosition - 1.0, layoutSize, *mActiveLayout);
1451 return (fabs(forwardClampedPosition - backwardClampedPosition) > Math::MACHINE_EPSILON_0);
1454 float ItemView::GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const
1456 Vector3 firstItemPosition( mActiveLayout->GetItemPosition(0, layoutPosition, layoutSize ) );
1457 return IsHorizontal(mActiveLayout->GetOrientation()) ? firstItemPosition.x: firstItemPosition.y;
1460 Vector3 ItemView::GetCurrentScrollPosition() const
1462 float currentLayoutPosition = GetCurrentLayoutPosition(0);
1463 return Vector3(0.0f, GetScrollPosition(currentLayoutPosition, Self().GetCurrentSize()), 0.0f);
1466 void ItemView::AddOverlay(Actor actor)
1471 void ItemView::RemoveOverlay(Actor actor)
1473 Self().Remove(actor);
1476 void ItemView::ScrollTo(const Vector3& position, float duration)
1478 Actor self = Self();
1479 const Vector3 layoutSize = Self().GetCurrentSize();
1481 float firstItemScrollPosition = ClampFirstItemPosition(position.y, layoutSize, *mActiveLayout);
1485 RemoveAnimation(mScrollAnimation);
1486 mScrollAnimation = Animation::New(duration);
1487 mScrollAnimation.AnimateTo( Property( mScrollPositionObject, ScrollConnector::SCROLL_POSITION ), firstItemScrollPosition, AlphaFunctions::EaseOut );
1488 mScrollAnimation.AnimateTo( Property(self, mPropertyPosition), GetScrollPosition(firstItemScrollPosition, layoutSize), AlphaFunctions::EaseOut );
1489 mScrollAnimation.FinishedSignal().Connect(this, &ItemView::OnScrollFinished);
1490 mScrollAnimation.Play();
1494 mScrollPositionObject.SetProperty( ScrollConnector::SCROLL_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::SetOvershootEnabled( bool enable )
1513 Actor self = Self();
1516 Property::Index effectOvershootPropertyIndex = Property::INVALID_INDEX;
1517 mOvershootOverlay = CreateBouncingEffectActor( effectOvershootPropertyIndex );
1518 mOvershootOverlay.SetColor(mOvershootEffectColor);
1519 mOvershootOverlay.SetParentOrigin(ParentOrigin::TOP_LEFT);
1520 mOvershootOverlay.SetAnchorPoint(AnchorPoint::TOP_LEFT);
1521 mOvershootOverlay.SetDrawMode(DrawMode::OVERLAY);
1522 self.Add(mOvershootOverlay);
1524 Constraint constraint = Constraint::New<Vector3>( mOvershootOverlay, Actor::Property::SIZE, OvershootOverlaySizeConstraint );
1525 constraint.AddSource( ParentSource( Toolkit::Scrollable::Property::SCROLL_DIRECTION ) );
1526 constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
1529 mOvershootOverlay.SetSize(OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE.width, OVERSHOOT_BOUNCE_ACTOR_DEFAULT_SIZE.height);
1531 constraint = Constraint::New<Quaternion>( mOvershootOverlay, Actor::Property::ORIENTATION, OvershootOverlayRotationConstraint );
1532 constraint.AddSource( ParentSource( Toolkit::Scrollable::Property::SCROLL_DIRECTION ) );
1533 constraint.AddSource( Source( mScrollPositionObject, ScrollConnector::OVERSHOOT ) );
1536 constraint = Constraint::New<Vector3>( mOvershootOverlay, Actor::Property::POSITION, OvershootOverlayPositionConstraint );
1537 constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
1538 constraint.AddSource( ParentSource( Toolkit::Scrollable::Property::SCROLL_DIRECTION ) );
1539 constraint.AddSource( Source( mScrollPositionObject, ScrollConnector::OVERSHOOT ) );
1542 constraint = Constraint::New<bool>( mOvershootOverlay, Actor::Property::VISIBLE, OvershootOverlayVisibilityConstraint );
1543 constraint.AddSource( ParentSource( Toolkit::Scrollable::Property::CAN_SCROLL_VERTICAL ) );
1546 constraint = Constraint::New<float>( mOvershootOverlay, effectOvershootPropertyIndex, EqualToConstraint() );
1547 constraint.AddSource( Source( mScrollPositionObject, ScrollConnector::OVERSHOOT ) );
1552 if( mOvershootOverlay )
1554 self.Remove(mOvershootOverlay);
1555 mOvershootOverlay.Reset();
1560 float ItemView::CalculateScrollOvershoot()
1562 float overshoot = 0.0f;
1566 // The overshoot must be calculated from the accumulated pan gesture displacement
1567 // since the pan gesture starts.
1568 Actor self = Self();
1569 float scrollDistance = CalculateScrollDistance(mTotalPanDisplacement, *mActiveLayout) * mActiveLayout->GetScrollSpeedFactor();
1570 float positionDelta = GetCurrentLayoutPosition(0) + scrollDistance;
1571 float minLayoutPosition = mActiveLayout->GetMinimumLayoutPosition(mItemFactory.GetNumberOfItems(), Self().GetCurrentSize());
1572 self.SetProperty(mPropertyMinimumLayoutPosition, minLayoutPosition);
1573 float clamppedPosition = std::min(0.0f, std::max(minLayoutPosition, positionDelta));
1574 overshoot = positionDelta - clamppedPosition;
1577 return overshoot > 0.0f ? std::min(overshoot, 1.0f) : std::max(overshoot, -1.0f);
1580 void ItemView::AnimateScrollOvershoot(float overshootAmount, bool animateBack)
1582 bool animatingOn = fabsf(overshootAmount) > Math::MACHINE_EPSILON_1;
1584 // make sure we animate back if needed
1585 mAnimateOvershootOff = animateBack || (!animatingOn && mAnimatingOvershootOn);
1587 if( mAnimatingOvershootOn )
1589 // animating on, do not allow animate off
1593 if(mOvershootAnimationSpeed > Math::MACHINE_EPSILON_0)
1595 float currentOvershoot = mScrollPositionObject.GetProperty<float>(ScrollConnector::OVERSHOOT);
1596 float duration = 0.0f;
1598 if (mOvershootOverlay)
1600 duration = mOvershootOverlay.GetCurrentSize().height * (animatingOn ? (1.0f - fabsf(currentOvershoot)) : fabsf(currentOvershoot)) / mOvershootAnimationSpeed;
1603 RemoveAnimation(mScrollOvershootAnimation);
1604 mScrollOvershootAnimation = Animation::New(duration);
1605 mScrollOvershootAnimation.FinishedSignal().Connect(this, &ItemView::OnOvershootOnFinished);
1606 mScrollOvershootAnimation.AnimateTo( Property(mScrollPositionObject, ScrollConnector::OVERSHOOT), overshootAmount, TimePeriod(0.0f, duration) );
1607 mScrollOvershootAnimation.Play();
1609 mAnimatingOvershootOn = animatingOn;
1613 mScrollPositionObject.SetProperty( ScrollConnector::OVERSHOOT, overshootAmount );
1617 void ItemView::SetItemsParentOrigin( const Vector3& parentOrigin )
1619 if( parentOrigin != mItemsParentOrigin )
1621 mItemsParentOrigin = parentOrigin;
1622 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1624 iter->second.SetParentOrigin(parentOrigin);
1629 Vector3 ItemView::GetItemsParentOrigin() const
1631 return mItemsParentOrigin;
1634 void ItemView::SetItemsAnchorPoint( const Vector3& anchorPoint )
1636 if( anchorPoint != mItemsAnchorPoint )
1638 mItemsAnchorPoint = anchorPoint;
1639 for (ItemPoolIter iter = mItemPool.begin(); iter != mItemPool.end(); ++iter)
1641 iter->second.SetAnchorPoint(anchorPoint);
1646 Vector3 ItemView::GetItemsAnchorPoint() const
1648 return mItemsAnchorPoint;
1651 void ItemView::GetItemsRange(ItemRange& range)
1653 if( !mItemPool.empty() )
1655 range.begin = mItemPool.begin()->first;
1656 range.end = mItemPool.rbegin()->first + 1;
1665 void ItemView::OnScrollPositionChanged( float position )
1667 // Cancel scroll animation to prevent any fighting of setting the scroll position property.
1668 RemoveAnimation(mScrollAnimation);
1670 // Refresh the cache immediately when the scroll position is changed.
1671 DoRefresh(position, false); // No need to cache extra items.
1674 } // namespace Internal
1676 } // namespace Toolkit