1 #ifndef __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__
2 #define __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/public-api/adaptor-framework/timer.h>
23 #include <dali/public-api/animation/animation.h>
24 #include <dali/public-api/object/property-notification.h>
25 #include <dali/devel-api/common/map-wrapper.h>
26 #include <dali/public-api/object/property-map.h>
27 #include <dali/public-api/object/property-array.h>
30 #include <dali-toolkit/public-api/controls/control-impl.h>
31 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-view.h>
32 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-layout.h>
33 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
34 #include <dali-toolkit/internal/controls/scrollable/scrollable-impl.h>
35 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
48 typedef IntrusivePtr<ItemView> ItemViewPtr;
51 * ItemView is a scrollable layout container.
52 * Multiple ItemLayouts may be provided, to determine the logical position of each item a layout.
53 * Actor-ID pairs are provided from a shared ItemFactory, to display the currently visible items.
55 class ItemView : public Scrollable
60 typedef Toolkit::ItemView::LayoutActivatedSignalType LayoutActivatedSignalType;
65 * Create a new ItemView.
66 * @param[in] factory The factory which provides ItemView with items.
67 * @return A public handle to the newly allocated ItemView.
69 static Dali::Toolkit::ItemView New(ItemFactory& factory);
72 * @copydoc Toolkit::ItemView::GetLayoutCount
74 unsigned int GetLayoutCount() const;
77 * @copydoc Toolkit::ItemView::AddLayout
79 void AddLayout(ItemLayout& layout);
82 * @copydoc Toolkit::ItemView::RemoveLayout
84 void RemoveLayout(unsigned int layoutIndex);
87 * @copydoc Toolkit::ItemView::GetLayout
89 ItemLayoutPtr GetLayout(unsigned int layoutIndex) const;
92 * @copydoc Toolkit::ItemView::GetActiveLayout
94 ItemLayoutPtr GetActiveLayout() const;
97 * @copydoc Toolkit::ItemView::GetCurrentLayoutPosition
99 float GetCurrentLayoutPosition(unsigned int itemId) const;
102 * @copydoc Toolkit::ItemView::ActivateLayout
104 void ActivateLayout(unsigned int layoutIndex, const Vector3& targetSize, float durationSeconds);
107 * @copydoc Toolkit::ItemView::DeactivateCurrentLayout
109 void DeactivateCurrentLayout();
112 * @copydoc Toolkit::ItemView::SetMinimumSwipeSpeed
114 void SetMinimumSwipeSpeed(float speed);
117 * @copydoc Toolkit::ItemView::GetMinimumSwipeSpeed
119 float GetMinimumSwipeSpeed() const;
122 * @copydoc Toolkit::ItemView::SetMinimumSwipeDistance
124 void SetMinimumSwipeDistance(float distance);
127 * @copydoc Toolkit::ItemView::GetMinimumSwipeDistance
129 float GetMinimumSwipeDistance() const;
132 * @copydoc Toolkit::ItemView::SetWheelScrollDistanceStep
134 void SetWheelScrollDistanceStep(float step);
137 * @copydoc Toolkit::ItemView::GetWheelScrollDistanceStep
139 float GetWheelScrollDistanceStep() const;
142 * @copydoc Toolkit::ItemView::SetAnchoring
144 void SetAnchoring(bool enabled);
147 * @copydoc Toolkit::ItemView::GetAnchoring
149 bool GetAnchoring() const;
152 * @copydoc Toolkit::ItemView::SetAnchoringDuration
154 void SetAnchoringDuration(float durationSeconds);
157 * @copydoc Toolkit::ItemView::GetAnchoringDuration
159 float GetAnchoringDuration() const;
162 * @copydoc Toolkit::ItemView::ScrollToItem
164 void ScrollToItem(unsigned int itemId, float durationSeconds);
167 * @copydoc Toolkit::ItemView::SetRefreshInterval
169 void SetRefreshInterval(float intervalLayoutPositions);
172 * @copydoc Toolkit::ItemView::GetRefreshInterval
174 float GetRefreshInterval() const;
177 * @copydoc Toolkit::ItemView::Refresh
182 * @copydoc Toolkit::ItemView::GetItem
184 Actor GetItem(unsigned int itemId) const;
187 * @copydoc Toolkit::ItemView::GetItemId
189 unsigned int GetItemId(Actor actor) const;
192 * @copydoc Toolkit::ItemView::InsertItem
194 void InsertItem(Item newItem, float durationSeconds);
197 * @copydoc Toolkit::ItemView::InsertItem
199 void InsertItems(const ItemContainer& newItems, float durationSeconds);
202 * @copydoc Toolkit::ItemView::RemoveItem
204 void RemoveItem(ItemId itemId, float durationSeconds);
207 * @copydoc Toolkit::ItemView::InsertItem
209 void RemoveItems(const ItemIdContainer& itemIds, float durationSeconds);
212 * @copydoc Toolkit::ItemView::InsertItem
214 void ReplaceItem(Item replacementItem, float durationSeconds);
217 * @copydoc Toolkit::ItemView::InsertItem
219 void ReplaceItems(const ItemContainer& replacementItems, float durationSeconds);
222 * @copydoc Toolkit::Scrollable::GetCurrentScrollPosition
224 Vector2 GetCurrentScrollPosition() const;
227 * @copydoc Toolkit::Scrollable::AddOverlay()
229 void AddOverlay(Actor actor);
232 * @copydoc Toolkit::Scrollable::RemoveOverlay()
234 void RemoveOverlay(Actor actor);
237 * @copydoc Toolkit::Scrollable::ScrollTo(const Vector2& position, float duration)
239 void ScrollTo(const Vector2& position, float duration);
242 * @copydoc Toolkit::Internal::Scrollable::SetOvershootSize
244 void SetOvershootSize( const Vector2& size );
247 * @copydoc Toolkit::Internal::Scrollable::SetOvershootEffectColor
249 void SetOvershootEffectColor( const Vector4& color );
252 * @brief Set whether to enable automatic refresh or not. When refresh is disabled,
253 * ItemView will not automatically refresh the cache in the given interval when the
254 * layout position is changed. This is useful in some cases, for example, automatic
255 * refresh is not needed during fast scrolling, otherwise it will cache unneeded
256 * items when the layout position changes quickly.
258 * @param[in] enabled True to enable automatic refresh or false to disable it.
260 void SetRefreshEnabled(bool enabled);
263 * @brief Helper to perform the refresh.
265 * @param[in] currentLayoutPosition The current layout position.
266 * @param[in] cacheExtra Whether to cache extra items during refresh.
268 void DoRefresh(float currentLayoutPosition, bool cacheExtra);
271 * @copydoc Toolkit::ItemView::SetItemsParentOrigin
273 void SetItemsParentOrigin( const Vector3& parentOrigin );
276 * @copydoc Toolkit::ItemView::GetItemsParentOrigin
278 Vector3 GetItemsParentOrigin() const;
281 * @copydoc Toolkit::ItemView::SetItemsAnchorPoint
283 void SetItemsAnchorPoint( const Vector3& anchorPoint );
286 * @copydoc Toolkit::ItemView::GetItemsAnchorPoint
288 Vector3 GetItemsAnchorPoint() const;
291 * @copydoc Toolkit::ItemView::GetItemsRange
293 void GetItemsRange(ItemRange& range);
296 * @copydoc Toolkit::ItemView::LayoutActivatedSignal()
298 LayoutActivatedSignalType& LayoutActivatedSignal()
300 return mLayoutActivatedSignal;
304 * Connects a callback function with the object's signals.
305 * @param[in] object The object providing the signal.
306 * @param[in] tracker Used to disconnect the signal.
307 * @param[in] signalName The signal to connect to.
308 * @param[in] functor A newly allocated FunctorDelegate.
309 * @return True if the signal was connected.
310 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
312 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
317 * Called when a property of an object of this type is set.
318 * @param[in] object The object whose property is set.
319 * @param[in] index The property index.
320 * @param[in] value The new property value.
322 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
325 * Called to retrieve a property of an object of this type.
326 * @param[in] object The object whose property is to be retrieved.
327 * @param[in] index The property index.
328 * @return The current value of the property.
330 static Property::Value GetProperty( BaseObject* object, Property::Index index );
333 * Performs actions as requested using the action name.
334 * @param[in] object The object on which to perform the action.
335 * @param[in] actionName The action to perform.
336 * @param[in] attributes The attributes with which to perfrom this action.
337 * @return true if action has been accepted by this control
339 static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes );
342 * Helper for DoAction( ACTION_STOP_SCROLLING ).
344 void DoStopScrolling();
347 * Helper for DoAction( ACTION_ENABLE/DISABLE_REFRESH_NOTIFICATIONS ).
348 * @param[in] enabled Whether to disable refresh notifications or not.
350 void SetRefreshNotificationEnabled( bool enabled );
355 * Get all the layouts used in the ItemView.
356 * @return The layout array
358 Property::Array GetLayoutArray();
361 * Set all the layouts. that will be used in the ItemView.
362 * @param[in] layouts The layouts used in the itemView.
364 void SetLayoutArray( const Property::Array& layouts );
367 * Remove an Actor if found in the ItemPool.
368 * @param[in] itemId The item to remove.
369 * @return True if the remaining actors were reordered.
371 bool RemoveActor( unsigned int itemId );
374 * Remove any Actors outside a given range.
375 * @param[in] @param[in] range The range of required items.
377 void RemoveActorsOutsideRange( ItemRange range );
380 * Add a range of Actors, if they are not already in the ItemPool.
381 * @param[in] range The range of Item IDs to associate with the new actors.
382 * @param[in] layoutSize The layout-size.
384 void AddActorsWithinRange( ItemRange range, const Vector3& layoutSize );
387 * Add a new Actor, if not already in the ItemPool.
388 * @param[in] item The ID for the new item.
389 * @param[in] layoutSize The layout-size.
391 void AddNewActor( ItemId item, const Vector3& layoutSize );
394 * Apply the constraints etc. that are required for ItemView children.
395 * @param[in] item The item to setup.
396 * @param[in] layoutSize The layout-size.
398 void SetupActor( Item item, const Vector3& layoutSize );
401 * Remove the Actor from the ItemPool and notify the ItemFactory the actor has been released by ItemView.
402 * @param[in] item The ID for the item to be released.
403 * @param[in] actor The actor to be removed from ItemView.
405 void ReleaseActor( ItemId item, Actor actor );
407 private: // From CustomActorImpl
410 * From CustomActorImpl; called after a child has been added to the owning actor.
411 * @param[in] child The child which has been added.
413 virtual void OnChildAdd(Actor& child);
416 * From CustomActorImpl; called after a wheel-event is received by the owning actor.
417 * @param[in] event The wheel event.
418 * @return True if the event should be consumed.
420 virtual bool OnWheelEvent(const WheelEvent& event);
422 private: // From Control
425 * @copydoc Toolkit::Control::OnInitialize()
427 virtual void OnInitialize();
430 * @copydoc Toolkit::Control::OnAccessibilityPan()
432 virtual bool OnAccessibilityPan(PanGesture gesture);
435 * @copydoc Toolkit::Control::GetNextKeyboardFocusableActor()
437 virtual Actor GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled);
440 * @copydoc Toolkit::Control::OnKeyboardFocusChangeCommitted()
442 virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor);
447 * Construct a new ItemView.
448 * @param[in] factory The factory which provides ItemView with items.
450 ItemView(ItemFactory& factory);
453 * A reference counted object may only be deleted by calling Unreference()
460 ItemView(const ItemView&);
463 ItemView& operator=(const ItemView& rhs);
466 * Helper to re-apply all the constraints after items have been inserted, removed etc.
467 * @param[in] durationSeconds The time taken to fully constrain the actors.
469 void ReapplyAllConstraints();
472 * Helper to relayout after item(s) are removed.
474 void OnItemsRemoved();
477 * Helper to remove items outside a given range.
478 * @param[in] range The range of required items.
480 void RemoveItems(ItemRange range);
483 * Helper to add a range of items, if not already in the ItemPool.
484 * @param[in] layout The layout used to position the new items.
485 * @param[in] layoutSize The current size of the layout.
486 * @param[in] range The range of required items.
488 void AddItems(ItemLayout& layout, const Vector3& layoutSize, ItemRange range);
491 * Helper to find the range of items to populate with.
492 * @param[in] layout The current layout.
493 * @param[in] range The range of items.
494 * @param[in] reserveExtra True if reserve items should be included.
496 ItemRange GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra);
501 * Helper to clamp the first-item position when dragging/swiping.
502 * @param[in] targetPosition The target position of the drag etc.
503 * @param[in] targetSize The target ItemView & layout size.
504 * @param[in] layout The current layout.
505 * @param[in] updateOvershoot False stops the current overshoot value from being clamped also.
506 * @return The clamped first-item position.
508 float ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout, bool updateOvershoot = true);
511 * Called after a touch-signal is received by the owning actor.
512 * @param[in] actor The touched actor.
513 * @param[in] touch The touch information.
514 * @return True if the event should be consumed.
516 bool OnTouch( Actor actor, const TouchData& touch );
519 * Called upon pan gesture event.
521 * @param[in] gesture The gesture event.
523 void OnPan( const PanGesture& pan );
526 * Helper to handle anchoring animations.
527 * @return The animation, or an uninitialized handle if not necessary.
529 Animation DoAnchoring();
532 * Callback from scroll animations
533 * @param[in] animation The scroll-animation which has finished.
535 void OnScrollFinished(Animation& animation);
538 * Callback from layout activation scroll animations
539 * @param[in] animation The scroll-animation which has finished.
541 void OnLayoutActivationScrollFinished(Animation& animation);
544 * Called by animation system when overshoot has finished animating to maximum (either -1.0f or 1.0f)
546 * @param[in] animation the animation that has finished
548 void OnOvershootOnFinished(Animation& animation);
551 * This is called after a timeout when no new wheel event is received for a certain period of time.
552 * @return will return false; one-shot timer.
554 bool OnWheelEventFinished();
557 * Stops and removes animation if exists.
558 * @param[in,out] animation The animation handle to be removed.
560 void RemoveAnimation(Animation& animation);
563 * @copydoc Toolkit::Internal::Scrollable::EnableScrollOvershoot
565 virtual void EnableScrollOvershoot( bool enable );
568 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
569 * @return The scroll overshoot.
571 float CalculateScrollOvershoot();
574 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
576 * @param[in] overshootAmount amount to animate to
577 * @param[in] animateBack whether to animate back to zero immediately after
578 * @return The scroll overshoot.
580 void AnimateScrollOvershoot(float overshootAmount, bool animateBack = false);
583 * Gets the scroll position in pixels according to the logical layout position.
584 * @param[in] layoutSize The current size of the layout.
586 float GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const;
589 * Calculates the minimum and maximum positions for each axis to scroll to.
590 * @param[in] layoutSize The current size of the layout.
592 void CalculateDomainSize(const Vector3& layoutSize);
595 * Calculates whether we want to allow current item view to scroll.
596 * @param[in] layoutSize The current size of the layout.
597 * @return true if itemview is scrollable
599 bool IsLayoutScrollable(const Vector3& layoutSize);
602 * Callback when the current layout position of ItemView changes in both positive
603 * and negative directions by the specified amount. Refresh the ItemView to create
604 * newly visible items.
605 * @param[in] source the property notification that triggered this callback
607 void OnRefreshNotification(PropertyNotification& source);
611 typedef std::map<unsigned int, Actor> ItemPool;
612 typedef ItemPool::iterator ItemPoolIter;
613 typedef ItemPool::const_iterator ConstItemPoolIter;
615 Property::Array mlayoutArray;
618 ItemFactory& mItemFactory;
619 std::vector< ItemLayoutPtr > mLayouts; ///< Container of Dali::Toolkit::ItemLayout objects
620 Actor mOvershootOverlay; ///< The overlay actor for overshoot effect
621 Animation mResizeAnimation;
622 Animation mScrollAnimation;
623 Animation mScrollOvershootAnimation;
624 Timer mWheelEventFinishedTimer; ///< The timer to determine whether there is no wheel event received for a certain period of time.
625 PropertyNotification mRefreshNotification; ///< Stores the property notification used for item view refresh
626 LayoutActivatedSignalType mLayoutActivatedSignal;
627 Vector3 mActiveLayoutTargetSize;
628 Vector3 mItemsParentOrigin;
629 Vector3 mItemsAnchorPoint;
630 Vector2 mTotalPanDisplacement;
631 ItemLayout* mActiveLayout;
633 float mAnchoringDuration;
634 float mRefreshIntervalLayoutPositions; ///< Refresh item view when the layout position changes by this interval in both positive and negative directions.
635 float mMinimumSwipeSpeed;
636 float mMinimumSwipeDistance;
637 float mWheelScrollDistanceStep; ///< The step of scroll distance in actor coordinates for each wheel event received.
638 float mScrollDistance;
640 float mScrollOvershoot;
642 Dali::Gesture::State mGestureState : 3;
643 bool mAnimatingOvershootOn : 1; ///< Whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
644 bool mAnimateOvershootOff : 1; ///< Whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
645 bool mAnchoringEnabled : 1;
646 bool mRefreshOrderHint : 1; ///< True if scrolling towards the last item
647 bool mIsFlicking : 1;
648 bool mAddingItems : 1;
649 bool mRefreshEnabled : 1; ///< Whether to refresh the cache automatically
650 bool mRefreshNotificationEnabled : 1; ///< Whether to disable refresh notifications or not.
651 bool mInAnimation : 1; ///< Keeps track of whether an animation is controlling the overshoot property.
654 } // namespace Internal
656 // Helpers for public-api forwarding methods
658 inline Toolkit::Internal::ItemView& GetImpl(Toolkit::ItemView& itemView)
660 DALI_ASSERT_ALWAYS(itemView);
662 Dali::RefObject& handle = itemView.GetImplementation();
664 return static_cast<Toolkit::Internal::ItemView&>(handle);
667 inline const Toolkit::Internal::ItemView& GetImpl(const Toolkit::ItemView& itemView)
669 DALI_ASSERT_ALWAYS(itemView);
671 const Dali::RefObject& handle = itemView.GetImplementation();
673 return static_cast<const Toolkit::Internal::ItemView&>(handle);
676 } // namespace Toolkit
680 #endif // __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__