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>
28 #include <dali-toolkit/public-api/controls/control-impl.h>
29 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-view.h>
30 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-layout.h>
31 #include <dali-toolkit/internal/controls/scrollable/scrollable-impl.h>
32 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
45 typedef IntrusivePtr<ItemView> ItemViewPtr;
48 * ItemView is a scrollable layout container.
49 * Multiple ItemLayouts may be provided, to determine the logical position of each item a layout.
50 * Actor-ID pairs are provided from a shared ItemFactory, to display the currently visible items.
52 class ItemView : public Scrollable
57 typedef Toolkit::ItemView::LayoutActivatedSignalType LayoutActivatedSignalType;
62 * Create a new ItemView.
63 * @param[in] factory The factory which provides ItemView with items.
64 * @return A public handle to the newly allocated ItemView.
66 static Dali::Toolkit::ItemView New(ItemFactory& factory);
69 * @copydoc Toolkit::ItemView::GetLayoutCount
71 unsigned int GetLayoutCount() const;
74 * @copydoc Toolkit::ItemView::AddLayout
76 void AddLayout(ItemLayout& layout);
79 * @copydoc Toolkit::ItemView::RemoveLayout
81 void RemoveLayout(unsigned int layoutIndex);
84 * @copydoc Toolkit::ItemView::GetLayout
86 ItemLayoutPtr GetLayout(unsigned int layoutIndex) const;
89 * @copydoc Toolkit::ItemView::GetActiveLayout
91 ItemLayoutPtr GetActiveLayout() const;
94 * @copydoc Toolkit::ItemView::GetCurrentLayoutPosition
96 float GetCurrentLayoutPosition(unsigned int itemId) const;
99 * @copydoc Toolkit::ItemView::ActivateLayout
101 void ActivateLayout(unsigned int layoutIndex, const Vector3& targetSize, float durationSeconds);
104 * @copydoc Toolkit::ItemView::DeactivateCurrentLayout
106 void DeactivateCurrentLayout();
109 * @copydoc Toolkit::ItemView::SetMinimumSwipeSpeed
111 void SetMinimumSwipeSpeed(float speed);
114 * @copydoc Toolkit::ItemView::GetMinimumSwipeSpeed
116 float GetMinimumSwipeSpeed() const;
119 * @copydoc Toolkit::ItemView::SetMinimumSwipeDistance
121 void SetMinimumSwipeDistance(float distance);
124 * @copydoc Toolkit::ItemView::GetMinimumSwipeDistance
126 float GetMinimumSwipeDistance() const;
129 * @copydoc Toolkit::ItemView::SetWheelScrollDistanceStep
131 void SetWheelScrollDistanceStep(float step);
134 * @copydoc Toolkit::ItemView::GetWheelScrollDistanceStep
136 float GetWheelScrollDistanceStep() const;
139 * @copydoc Toolkit::ItemView::SetAnchoring
141 void SetAnchoring(bool enabled);
144 * @copydoc Toolkit::ItemView::GetAnchoring
146 bool GetAnchoring() const;
149 * @copydoc Toolkit::ItemView::SetAnchoringDuration
151 void SetAnchoringDuration(float durationSeconds);
154 * @copydoc Toolkit::ItemView::GetAnchoringDuration
156 float GetAnchoringDuration() const;
159 * @copydoc Toolkit::ItemView::ScrollToItem
161 void ScrollToItem(unsigned int itemId, float durationSeconds);
164 * @copydoc Toolkit::ItemView::SetRefreshInterval
166 void SetRefreshInterval(float intervalLayoutPositions);
169 * @copydoc Toolkit::ItemView::GetRefreshInterval
171 float GetRefreshInterval() const;
174 * @copydoc Toolkit::ItemView::Refresh
179 * @copydoc Toolkit::ItemView::GetItem
181 Actor GetItem(unsigned int itemId) const;
184 * @copydoc Toolkit::ItemView::GetItemId
186 unsigned int GetItemId(Actor actor) const;
189 * @copydoc Toolkit::ItemView::InsertItem
191 void InsertItem(Item newItem, float durationSeconds);
194 * @copydoc Toolkit::ItemView::InsertItem
196 void InsertItems(const ItemContainer& newItems, float durationSeconds);
199 * @copydoc Toolkit::ItemView::RemoveItem
201 void RemoveItem(ItemId itemId, float durationSeconds);
204 * @copydoc Toolkit::ItemView::InsertItem
206 void RemoveItems(const ItemIdContainer& itemIds, float durationSeconds);
209 * @copydoc Toolkit::ItemView::InsertItem
211 void ReplaceItem(Item replacementItem, float durationSeconds);
214 * @copydoc Toolkit::ItemView::InsertItem
216 void ReplaceItems(const ItemContainer& replacementItems, float durationSeconds);
219 * @copydoc Toolkit::Scrollable::GetDomainSize
221 Vector2 GetDomainSize() const;
224 * @copydoc Toolkit::Scrollable::GetCurrentScrollPosition
226 Vector2 GetCurrentScrollPosition() const;
229 * @copydoc Toolkit::Scrollable::AddOverlay()
231 void AddOverlay(Actor actor);
234 * @copydoc Toolkit::Scrollable::RemoveOverlay()
236 void RemoveOverlay(Actor actor);
239 * @copydoc Toolkit::Scrollable::ScrollTo(const Vector2& position, float duration)
241 void ScrollTo(const Vector2& position, float duration);
244 * @copydoc Toolkit::Internal::Scrollable::SetOvershootEffectColor
246 void SetOvershootEffectColor( const Vector4& color );
249 * @brief Set whether to enable automatic refresh or not. When refresh is disabled,
250 * ItemView will not automatically refresh the cache in the given interval when the
251 * layout position is changed. This is useful in some cases, for example, automatic
252 * refresh is not needed during fast scrolling, otherwise it will cache unneeded
253 * items when the layout position changes quickly.
255 * @param[in] enabled True to enable automatic refresh or false to disable it.
257 void SetRefreshEnabled(bool enabled);
260 * @brief Helper to perform the refresh.
262 * @param[in] currentLayoutPosition The current layout position.
263 * @param[in] cacheExtra Whether to cache extra items during refresh.
265 void DoRefresh(float currentLayoutPosition, bool cacheExtra);
268 * @copydoc Toolkit::ItemView::SetItemsParentOrigin
270 void SetItemsParentOrigin( const Vector3& parentOrigin );
273 * @copydoc Toolkit::ItemView::GetItemsParentOrigin
275 Vector3 GetItemsParentOrigin() const;
278 * @copydoc Toolkit::ItemView::SetItemsAnchorPoint
280 void SetItemsAnchorPoint( const Vector3& anchorPoint );
283 * @copydoc Toolkit::ItemView::GetItemsAnchorPoint
285 Vector3 GetItemsAnchorPoint() const;
288 * @copydoc Toolkit::ItemView::GetItemsRange
290 void GetItemsRange(ItemRange& range);
293 * @copydoc Toolkit::ItemView::LayoutActivatedSignal()
295 LayoutActivatedSignalType& LayoutActivatedSignal()
297 return mLayoutActivatedSignal;
301 * Connects a callback function with the object's signals.
302 * @param[in] object The object providing the signal.
303 * @param[in] tracker Used to disconnect the signal.
304 * @param[in] signalName The signal to connect to.
305 * @param[in] functor A newly allocated FunctorDelegate.
306 * @return True if the signal was connected.
307 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
309 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
314 * Called when a property of an object of this type is set.
315 * @param[in] object The object whose property is set.
316 * @param[in] index The property index.
317 * @param[in] value The new property value.
319 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
322 * Called to retrieve a property of an object of this type.
323 * @param[in] object The object whose property is to be retrieved.
324 * @param[in] index The property index.
325 * @return The current value of the property.
327 static Property::Value GetProperty( BaseObject* object, Property::Index index );
330 * Performs actions as requested using the action name.
331 * @param[in] object The object on which to perform the action.
332 * @param[in] actionName The action to perform.
333 * @param[in] attributes The attributes with which to perfrom this action.
334 * @return true if action has been accepted by this control
336 static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes );
339 * Helper for DoAction( ACTION_STOP_SCROLLING ).
341 void DoStopScrolling();
346 * Remove an Actor if found in the ItemPool.
347 * @param[in] itemId The item to remove.
348 * @return True if the remaining actors were reordered.
350 bool RemoveActor( unsigned int itemId );
353 * Remove any Actors outside a given range.
354 * @param[in] @param[in] range The range of required items.
356 void RemoveActorsOutsideRange( ItemRange range );
359 * Add a range of Actors, if they are not already in the ItemPool.
360 * @param[in] range The range of Item IDs to associate with the new actors.
361 * @param[in] layoutSize The layout-size.
363 void AddActorsWithinRange( ItemRange range, const Vector3& layoutSize );
366 * Add a new Actor, if not already in the ItemPool.
367 * @param[in] item The ID for the new item.
368 * @param[in] layoutSize The layout-size.
370 void AddNewActor( ItemId item, const Vector3& layoutSize );
373 * Apply the constraints etc. that are required for ItemView children.
374 * @param[in] item The item to setup.
375 * @param[in] layoutSize The layout-size.
377 void SetupActor( Item item, const Vector3& layoutSize );
380 * Remove the Actor from the ItemPool and notify the ItemFactory the actor has been released by ItemView.
381 * @param[in] item The ID for the item to be released.
382 * @param[in] actor The actor to be removed from ItemView.
384 void ReleaseActor( ItemId item, Actor actor );
386 private: // From CustomActorImpl
389 * From CustomActorImpl; called after a child has been added to the owning actor.
390 * @param[in] child The child which has been added.
392 virtual void OnChildAdd(Actor& child);
395 * From CustomActorImpl; called after a touch-signal is received by the owning actor.
396 * @param[in] event The touch event.
397 * @return True if the event should be consumed.
399 virtual bool OnTouchEvent(const TouchEvent& event);
402 * From CustomActorImpl; called after a wheel-event is received by the owning actor.
403 * @param[in] event The wheel event.
404 * @return True if the event should be consumed.
406 virtual bool OnWheelEvent(const WheelEvent& event);
408 private: // From Control
411 * @copydoc Toolkit::Control::OnInitialize()
413 virtual void OnInitialize();
416 * @copydoc Toolkit::Control::OnAccessibilityPan()
418 virtual bool OnAccessibilityPan(PanGesture gesture);
421 * @copydoc Toolkit::Control::GetNextKeyboardFocusableActor()
423 virtual Actor GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled);
426 * @copydoc Toolkit::Control::OnKeyboardFocusChangeCommitted()
428 virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor);
433 * Construct a new ItemView.
434 * @param[in] factory The factory which provides ItemView with items.
436 ItemView(ItemFactory& factory);
439 * A reference counted object may only be deleted by calling Unreference()
446 ItemView(const ItemView&);
449 ItemView& operator=(const ItemView& rhs);
452 * Helper to re-apply all the constraints after items have been inserted, removed etc.
453 * @param[in] durationSeconds The time taken to fully constrain the actors.
455 void ReapplyAllConstraints();
458 * Helper to relayout after item(s) are removed.
460 void OnItemsRemoved();
463 * Helper to remove items outside a given range.
464 * @param[in] range The range of required items.
466 void RemoveItems(ItemRange range);
469 * Helper to add a range of items, if not already in the ItemPool.
470 * @param[in] layout The layout used to position the new items.
471 * @param[in] layoutSize The current size of the layout.
472 * @param[in] range The range of required items.
474 void AddItems(ItemLayout& layout, const Vector3& layoutSize, ItemRange range);
477 * Helper to find the range of items to populate with.
478 * @param[in] layout The current layout.
479 * @param[in] range The range of items.
480 * @param[in] reserveExtra True if reserve items should be included.
482 ItemRange GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra);
487 * Helper to handle pressed (Down) events.
488 * @param[in] x The X coordinate of the touch event.
489 * @param[in] y The Y coordinate of the touch event.
490 * @param[in] timeMs The time-stamp of the touch event.
492 void OnPressed(float x, float y, unsigned long timeMs);
495 * Helper to clamp the first-item position when dragging/swiping.
496 * @param[in] targetPosition The target position of the drag etc.
497 * @param[in] targetSize The target ItemView & layout size.
498 * @param[in] layout The current layout.
499 * @param[in] updateOvershoot False stops the current overshoot value from being clamped also.
500 * @return The clamped first-item position.
502 float ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout, bool updateOvershoot = true);
505 * Called upon pan gesture event.
507 * @param[in] gesture The gesture event.
509 void OnPan( const PanGesture& pan );
512 * Helper to handle anchoring animations.
513 * @return The animation, or an uninitialized handle if not necessary.
515 Animation DoAnchoring();
518 * Callback from scroll animations
519 * @param[in] animation The scroll-animation which has finished.
521 void OnScrollFinished(Animation& animation);
524 * Callback from layout activation scroll animations
525 * @param[in] animation The scroll-animation which has finished.
527 void OnLayoutActivationScrollFinished(Animation& animation);
530 * Called by animation system when overshoot has finished animating to maximum (either -1.0f or 1.0f)
532 * @param[in] animation the animation that has finished
534 void OnOvershootOnFinished(Animation& animation);
537 * This is called after a timeout when no new wheel event is received for a certain period of time.
538 * @return will return false; one-shot timer.
540 bool OnWheelEventFinished();
543 * Stops and removes animation if exists.
544 * @param[in,out] animation The animation handle to be removed.
546 void RemoveAnimation(Animation& animation);
549 * @copydoc Toolkit::Internal::Scrollable::EnableScrollOvershoot
551 virtual void EnableScrollOvershoot( bool enable );
554 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
555 * @return The scroll overshoot.
557 float CalculateScrollOvershoot();
560 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
562 * @param[in] overshootAmount amount to animate to
563 * @param[in] animateBack whether to animate back to zero immediately after
564 * @return The scroll overshoot.
566 void AnimateScrollOvershoot(float overshootAmount, bool animateBack = false);
569 * Gets the scroll position in pixels according to the logical layout position.
570 * @param[in] layoutSize The current size of the layout.
572 float GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const;
575 * Calculates the minimum and maximum positions for each axis to scroll to.
576 * @param[in] layoutSize The current size of the layout.
578 void CalculateDomainSize(const Vector3& layoutSize);
581 * Calculates whether we want to allow current item view to scroll.
582 * @param[in] layoutSize The current size of the layout.
583 * @return true if itemview is scrollable
585 bool IsLayoutScrollable(const Vector3& layoutSize);
588 * Callback when the current layout position of ItemView changes in both positive
589 * and negative directions by the specified amount. Refresh the ItemView to create
590 * newly visible items.
591 * @param[in] source the property notification that triggered this callback
593 void OnRefreshNotification(PropertyNotification& source);
597 typedef std::map<unsigned int, Actor> ItemPool;
598 typedef ItemPool::iterator ItemPoolIter;
599 typedef ItemPool::const_iterator ConstItemPoolIter;
602 ItemFactory& mItemFactory;
603 std::vector< ItemLayoutPtr > mLayouts; ///< Container of Dali::Toolkit::ItemLayout objects
604 Actor mOvershootOverlay; ///< The overlay actor for overshoot effect
605 Animation mResizeAnimation;
606 Animation mScrollAnimation;
607 Animation mScrollOvershootAnimation;
608 Timer mWheelEventFinishedTimer; ///< The timer to determine whether there is no wheel event received for a certain period of time.
609 PropertyNotification mRefreshNotification; ///< Stores the property notification used for item view refresh
610 LayoutActivatedSignalType mLayoutActivatedSignal;
611 Vector3 mActiveLayoutTargetSize;
612 Vector3 mItemsParentOrigin;
613 Vector3 mItemsAnchorPoint;
614 Vector2 mTotalPanDisplacement;
615 ItemLayout* mActiveLayout;
617 float mAnchoringDuration;
618 float mRefreshIntervalLayoutPositions; ///< Refresh item view when the layout position changes by this interval in both positive and negative directions.
619 float mMinimumSwipeSpeed;
620 float mMinimumSwipeDistance;
621 float mWheelScrollDistanceStep; ///< The step of scroll distance in actor coordinates for each wheel event received.
622 float mScrollDistance;
624 float mScrollOvershoot;
626 Dali::Gesture::State mGestureState : 3;
627 bool mAnimatingOvershootOn : 1; ///< Whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
628 bool mAnimateOvershootOff : 1; ///< Whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
629 bool mAnchoringEnabled : 1;
630 bool mRefreshOrderHint : 1; ///< True if scrolling towards the last item
631 bool mIsFlicking : 1;
632 bool mAddingItems : 1;
633 bool mRefreshEnabled : 1; ///< Whether to refresh the cache automatically
634 bool mInAnimation : 1; ///< Keeps track of whether an animation is controlling the overshoot property.
637 } // namespace Internal
639 // Helpers for public-api forwarding methods
641 inline Toolkit::Internal::ItemView& GetImpl(Toolkit::ItemView& itemView)
643 DALI_ASSERT_ALWAYS(itemView);
645 Dali::RefObject& handle = itemView.GetImplementation();
647 return static_cast<Toolkit::Internal::ItemView&>(handle);
650 inline const Toolkit::Internal::ItemView& GetImpl(const Toolkit::ItemView& itemView)
652 DALI_ASSERT_ALWAYS(itemView);
654 const Dali::RefObject& handle = itemView.GetImplementation();
656 return static_cast<const Toolkit::Internal::ItemView&>(handle);
659 } // namespace Toolkit
663 #endif // __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__