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/public-api/object/property-map.h>
26 #include <dali/public-api/object/property-array.h>
29 #include <dali-toolkit/public-api/controls/control-impl.h>
30 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-view.h>
31 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-layout.h>
32 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
33 #include <dali-toolkit/internal/controls/scrollable/scrollable-impl.h>
34 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
47 typedef IntrusivePtr<ItemView> ItemViewPtr;
50 * ItemView is a scrollable layout container.
51 * Multiple ItemLayouts may be provided, to determine the logical position of each item a layout.
52 * Actor-ID pairs are provided from a shared ItemFactory, to display the currently visible items.
54 class ItemView : public Scrollable
59 typedef Toolkit::ItemView::LayoutActivatedSignalType LayoutActivatedSignalType;
64 * Create a new ItemView.
65 * @param[in] factory The factory which provides ItemView with items.
66 * @return A public handle to the newly allocated ItemView.
68 static Dali::Toolkit::ItemView New(ItemFactory& factory);
71 * @copydoc Toolkit::ItemView::GetLayoutCount
73 unsigned int GetLayoutCount() const;
76 * @copydoc Toolkit::ItemView::AddLayout
78 void AddLayout(ItemLayout& layout);
81 * @copydoc Toolkit::ItemView::RemoveLayout
83 void RemoveLayout(unsigned int layoutIndex);
86 * @copydoc Toolkit::ItemView::GetLayout
88 ItemLayoutPtr GetLayout(unsigned int layoutIndex) const;
91 * @copydoc Toolkit::ItemView::GetActiveLayout
93 ItemLayoutPtr GetActiveLayout() const;
96 * @copydoc Toolkit::ItemView::GetCurrentLayoutPosition
98 float GetCurrentLayoutPosition(unsigned int itemId) const;
101 * @copydoc Toolkit::ItemView::ActivateLayout
103 void ActivateLayout(unsigned int layoutIndex, const Vector3& targetSize, float durationSeconds);
106 * @copydoc Toolkit::ItemView::DeactivateCurrentLayout
108 void DeactivateCurrentLayout();
111 * @copydoc Toolkit::ItemView::SetMinimumSwipeSpeed
113 void SetMinimumSwipeSpeed(float speed);
116 * @copydoc Toolkit::ItemView::GetMinimumSwipeSpeed
118 float GetMinimumSwipeSpeed() const;
121 * @copydoc Toolkit::ItemView::SetMinimumSwipeDistance
123 void SetMinimumSwipeDistance(float distance);
126 * @copydoc Toolkit::ItemView::GetMinimumSwipeDistance
128 float GetMinimumSwipeDistance() const;
131 * @copydoc Toolkit::ItemView::SetWheelScrollDistanceStep
133 void SetWheelScrollDistanceStep(float step);
136 * @copydoc Toolkit::ItemView::GetWheelScrollDistanceStep
138 float GetWheelScrollDistanceStep() const;
141 * @copydoc Toolkit::ItemView::SetAnchoring
143 void SetAnchoring(bool enabled);
146 * @copydoc Toolkit::ItemView::GetAnchoring
148 bool GetAnchoring() const;
151 * @copydoc Toolkit::ItemView::SetAnchoringDuration
153 void SetAnchoringDuration(float durationSeconds);
156 * @copydoc Toolkit::ItemView::GetAnchoringDuration
158 float GetAnchoringDuration() const;
161 * @copydoc Toolkit::ItemView::ScrollToItem
163 void ScrollToItem(unsigned int itemId, float durationSeconds);
166 * @copydoc Toolkit::ItemView::SetRefreshInterval
168 void SetRefreshInterval(float intervalLayoutPositions);
171 * @copydoc Toolkit::ItemView::GetRefreshInterval
173 float GetRefreshInterval() const;
176 * @copydoc Toolkit::ItemView::Refresh
181 * @copydoc Toolkit::ItemView::GetItem
183 Actor GetItem(unsigned int itemId) const;
186 * @copydoc Toolkit::ItemView::GetItemId
188 unsigned int GetItemId(Actor actor) const;
191 * @copydoc Toolkit::ItemView::InsertItem
193 void InsertItem(Item newItem, float durationSeconds);
196 * @copydoc Toolkit::ItemView::InsertItem
198 void InsertItems(const ItemContainer& newItems, float durationSeconds);
201 * @copydoc Toolkit::ItemView::RemoveItem
203 void RemoveItem(ItemId itemId, float durationSeconds);
206 * @copydoc Toolkit::ItemView::InsertItem
208 void RemoveItems(const ItemIdContainer& itemIds, float durationSeconds);
211 * @copydoc Toolkit::ItemView::InsertItem
213 void ReplaceItem(Item replacementItem, float durationSeconds);
216 * @copydoc Toolkit::ItemView::InsertItem
218 void ReplaceItems(const ItemContainer& replacementItems, float durationSeconds);
221 * @copydoc Toolkit::Scrollable::GetCurrentScrollPosition
223 Vector2 GetCurrentScrollPosition() const;
226 * @copydoc Toolkit::Scrollable::AddOverlay()
228 void AddOverlay(Actor actor);
231 * @copydoc Toolkit::Scrollable::RemoveOverlay()
233 void RemoveOverlay(Actor actor);
236 * @copydoc Toolkit::Scrollable::ScrollTo(const Vector2& position, float duration)
238 void ScrollTo(const Vector2& position, float duration);
241 * @copydoc Toolkit::Internal::Scrollable::SetOvershootSize
243 void SetOvershootSize( const Vector2& size );
246 * @copydoc Toolkit::Internal::Scrollable::SetOvershootEffectColor
248 void SetOvershootEffectColor( const Vector4& color );
251 * @brief Set whether to enable automatic refresh or not. When refresh is disabled,
252 * ItemView will not automatically refresh the cache in the given interval when the
253 * layout position is changed. This is useful in some cases, for example, automatic
254 * refresh is not needed during fast scrolling, otherwise it will cache unneeded
255 * items when the layout position changes quickly.
257 * @param[in] enabled True to enable automatic refresh or false to disable it.
259 void SetRefreshEnabled(bool enabled);
262 * @brief Helper to perform the refresh.
264 * @param[in] currentLayoutPosition The current layout position.
265 * @param[in] cacheExtra Whether to cache extra items during refresh.
267 void DoRefresh(float currentLayoutPosition, bool cacheExtra);
270 * @copydoc Toolkit::ItemView::SetItemsParentOrigin
272 void SetItemsParentOrigin( const Vector3& parentOrigin );
275 * @copydoc Toolkit::ItemView::GetItemsParentOrigin
277 Vector3 GetItemsParentOrigin() const;
280 * @copydoc Toolkit::ItemView::SetItemsAnchorPoint
282 void SetItemsAnchorPoint( const Vector3& anchorPoint );
285 * @copydoc Toolkit::ItemView::GetItemsAnchorPoint
287 Vector3 GetItemsAnchorPoint() const;
290 * @copydoc Toolkit::ItemView::GetItemsRange
292 void GetItemsRange(ItemRange& range);
295 * @copydoc Toolkit::ItemView::LayoutActivatedSignal()
297 LayoutActivatedSignalType& LayoutActivatedSignal()
299 return mLayoutActivatedSignal;
303 * Connects a callback function with the object's signals.
304 * @param[in] object The object providing the signal.
305 * @param[in] tracker Used to disconnect the signal.
306 * @param[in] signalName The signal to connect to.
307 * @param[in] functor A newly allocated FunctorDelegate.
308 * @return True if the signal was connected.
309 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
311 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
316 * Called when a property of an object of this type is set.
317 * @param[in] object The object whose property is set.
318 * @param[in] index The property index.
319 * @param[in] value The new property value.
321 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
324 * Called to retrieve a property of an object of this type.
325 * @param[in] object The object whose property is to be retrieved.
326 * @param[in] index The property index.
327 * @return The current value of the property.
329 static Property::Value GetProperty( BaseObject* object, Property::Index index );
332 * Performs actions as requested using the action name.
333 * @param[in] object The object on which to perform the action.
334 * @param[in] actionName The action to perform.
335 * @param[in] attributes The attributes with which to perfrom this action.
336 * @return true if action has been accepted by this control
338 static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes );
341 * Helper for DoAction( ACTION_STOP_SCROLLING ).
343 void DoStopScrolling();
346 * Helper for DoAction( ACTION_ENABLE/DISABLE_REFRESH_NOTIFICATIONS ).
347 * @param[in] enabled Whether to disable refresh notifications or not.
349 void SetRefreshNotificationEnabled( bool enabled );
354 * Get all the layouts used in the ItemView.
355 * @return The layout array
357 Property::Array GetLayoutArray();
360 * Set all the layouts. that will be used in the ItemView.
361 * @param[in] layouts The layouts used in the itemView.
363 void SetLayoutArray( const Property::Array& layouts );
366 * Remove an Actor if found in the ItemPool.
367 * @param[in] itemId The item to remove.
368 * @return True if the remaining actors were reordered.
370 bool RemoveActor( unsigned int itemId );
373 * Remove any Actors outside a given range.
374 * @param[in] @param[in] range The range of required items.
376 void RemoveActorsOutsideRange( ItemRange range );
379 * Add a range of Actors, if they are not already in the ItemPool.
380 * @param[in] range The range of Item IDs to associate with the new actors.
381 * @param[in] layoutSize The layout-size.
383 void AddActorsWithinRange( ItemRange range, const Vector3& layoutSize );
386 * Add a new Actor, if not already in the ItemPool.
387 * @param[in] item The ID for the new item.
388 * @param[in] layoutSize The layout-size.
390 void AddNewActor( ItemId item, const Vector3& layoutSize );
393 * Apply the constraints etc. that are required for ItemView children.
394 * @param[in] item The item to setup.
395 * @param[in] layoutSize The layout-size.
397 void SetupActor( Item item, const Vector3& layoutSize );
400 * Remove the Actor from the ItemPool and notify the ItemFactory the actor has been released by ItemView.
401 * @param[in] item The ID for the item to be released.
402 * @param[in] actor The actor to be removed from ItemView.
404 void ReleaseActor( ItemId item, Actor actor );
406 private: // From CustomActorImpl
409 * From CustomActorImpl; called after a child has been added to the owning actor.
410 * @param[in] child The child which has been added.
412 virtual void OnChildAdd(Actor& child);
415 * From CustomActorImpl; called after a wheel-event is received by the owning actor.
416 * @param[in] event The wheel event.
417 * @return True if the event should be consumed.
419 virtual bool OnWheelEvent(const WheelEvent& event);
421 private: // From Control
424 * @copydoc Toolkit::Control::OnInitialize()
426 virtual void OnInitialize();
429 * @copydoc Toolkit::Control::OnAccessibilityPan()
431 virtual bool OnAccessibilityPan(PanGesture gesture);
434 * @copydoc Toolkit::Control::GetNextKeyboardFocusableActor()
436 virtual Actor GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled);
439 * @copydoc Toolkit::Control::OnKeyboardFocusChangeCommitted()
441 virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor);
446 * Construct a new ItemView.
447 * @param[in] factory The factory which provides ItemView with items.
449 ItemView(ItemFactory& factory);
452 * A reference counted object may only be deleted by calling Unreference()
459 ItemView(const ItemView&);
462 ItemView& operator=(const ItemView& rhs);
465 * Helper to re-apply all the constraints after items have been inserted, removed etc.
466 * @param[in] durationSeconds The time taken to fully constrain the actors.
468 void ReapplyAllConstraints();
471 * Helper to relayout after item(s) are removed.
473 void OnItemsRemoved();
476 * Helper to remove items outside a given range.
477 * @param[in] range The range of required items.
479 void RemoveItems(ItemRange range);
482 * Helper to add a range of items, if not already in the ItemPool.
483 * @param[in] layout The layout used to position the new items.
484 * @param[in] layoutSize The current size of the layout.
485 * @param[in] range The range of required items.
487 void AddItems(ItemLayout& layout, const Vector3& layoutSize, ItemRange range);
490 * Helper to find the range of items to populate with.
491 * @param[in] layout The current layout.
492 * @param[in] range The range of items.
493 * @param[in] reserveExtra True if reserve items should be included.
495 ItemRange GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra);
500 * Helper to clamp the first-item position when dragging/swiping.
501 * @param[in] targetPosition The target position of the drag etc.
502 * @param[in] targetSize The target ItemView & layout size.
503 * @param[in] layout The current layout.
504 * @param[in] updateOvershoot False stops the current overshoot value from being clamped also.
505 * @return The clamped first-item position.
507 float ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout, bool updateOvershoot = true);
510 * Called after a touch-signal is received by the owning actor.
511 * @param[in] actor The touched actor.
512 * @param[in] touch The touch information.
513 * @return True if the event should be consumed.
515 bool OnTouch( Actor actor, const TouchData& touch );
518 * Called upon pan gesture event.
520 * @param[in] gesture The gesture event.
522 void OnPan( const PanGesture& pan );
525 * Helper to handle anchoring animations.
526 * @return The animation, or an uninitialized handle if not necessary.
528 Animation DoAnchoring();
531 * Callback from scroll animations
532 * @param[in] animation The scroll-animation which has finished.
534 void OnScrollFinished(Animation& animation);
537 * Callback from layout activation scroll animations
538 * @param[in] animation The scroll-animation which has finished.
540 void OnLayoutActivationScrollFinished(Animation& animation);
543 * Called by animation system when overshoot has finished animating to maximum (either -1.0f or 1.0f)
545 * @param[in] animation the animation that has finished
547 void OnOvershootOnFinished(Animation& animation);
550 * This is called after a timeout when no new wheel event is received for a certain period of time.
551 * @return will return false; one-shot timer.
553 bool OnWheelEventFinished();
556 * Stops and removes animation if exists.
557 * @param[in,out] animation The animation handle to be removed.
559 void RemoveAnimation(Animation& animation);
562 * @copydoc Toolkit::Internal::Scrollable::EnableScrollOvershoot
564 virtual void EnableScrollOvershoot( bool enable );
567 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
568 * @return The scroll overshoot.
570 float CalculateScrollOvershoot();
573 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
575 * @param[in] overshootAmount amount to animate to
576 * @param[in] animateBack whether to animate back to zero immediately after
577 * @return The scroll overshoot.
579 void AnimateScrollOvershoot(float overshootAmount, bool animateBack = false);
582 * Gets the scroll position in pixels according to the logical layout position.
583 * @param[in] layoutSize The current size of the layout.
585 float GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const;
588 * Calculates the minimum and maximum positions for each axis to scroll to.
589 * @param[in] layoutSize The current size of the layout.
591 void CalculateDomainSize(const Vector3& layoutSize);
594 * Calculates whether we want to allow current item view to scroll.
595 * @param[in] layoutSize The current size of the layout.
596 * @return true if itemview is scrollable
598 bool IsLayoutScrollable(const Vector3& layoutSize);
601 * Callback when the current layout position of ItemView changes in both positive
602 * and negative directions by the specified amount. Refresh the ItemView to create
603 * newly visible items.
604 * @param[in] source the property notification that triggered this callback
606 void OnRefreshNotification(PropertyNotification& source);
610 Property::Array mlayoutArray;
612 ItemContainer mItemPool;
613 ItemFactory& mItemFactory;
614 std::vector< ItemLayoutPtr > mLayouts; ///< Container of Dali::Toolkit::ItemLayout objects
615 Actor mOvershootOverlay; ///< The overlay actor for overshoot effect
616 Animation mResizeAnimation;
617 Animation mScrollAnimation;
618 Animation mScrollOvershootAnimation;
619 Timer mWheelEventFinishedTimer; ///< The timer to determine whether there is no wheel event received for a certain period of time.
620 PropertyNotification mRefreshNotification; ///< Stores the property notification used for item view refresh
621 LayoutActivatedSignalType mLayoutActivatedSignal;
622 Vector3 mActiveLayoutTargetSize;
623 Vector3 mItemsParentOrigin;
624 Vector3 mItemsAnchorPoint;
625 Vector2 mTotalPanDisplacement;
626 ItemLayout* mActiveLayout;
628 float mAnchoringDuration;
629 float mRefreshIntervalLayoutPositions; ///< Refresh item view when the layout position changes by this interval in both positive and negative directions.
630 float mMinimumSwipeSpeed;
631 float mMinimumSwipeDistance;
632 float mWheelScrollDistanceStep; ///< The step of scroll distance in actor coordinates for each wheel event received.
633 float mScrollDistance;
635 float mScrollOvershoot;
637 Dali::Gesture::State mGestureState : 4;
638 bool mAnimatingOvershootOn : 1; ///< Whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
639 bool mAnimateOvershootOff : 1; ///< Whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
640 bool mAnchoringEnabled : 1;
641 bool mRefreshOrderHint : 1; ///< True if scrolling towards the last item
642 bool mIsFlicking : 1;
643 bool mAddingItems : 1;
644 bool mRefreshEnabled : 1; ///< Whether to refresh the cache automatically
645 bool mRefreshNotificationEnabled : 1; ///< Whether to disable refresh notifications or not.
646 bool mInAnimation : 1; ///< Keeps track of whether an animation is controlling the overshoot property.
649 } // namespace Internal
651 // Helpers for public-api forwarding methods
653 inline Toolkit::Internal::ItemView& GetImpl(Toolkit::ItemView& itemView)
655 DALI_ASSERT_ALWAYS(itemView);
657 Dali::RefObject& handle = itemView.GetImplementation();
659 return static_cast<Toolkit::Internal::ItemView&>(handle);
662 inline const Toolkit::Internal::ItemView& GetImpl(const Toolkit::ItemView& itemView)
664 DALI_ASSERT_ALWAYS(itemView);
666 const Dali::RefObject& handle = itemView.GetImplementation();
668 return static_cast<const Toolkit::Internal::ItemView&>(handle);
671 } // namespace Toolkit
675 #endif // __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__