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>
27 #include <dali-toolkit/public-api/controls/control-impl.h>
28 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-view.h>
29 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-layout.h>
30 #include <dali-toolkit/public-api/controls/scrollable/scroll-connector.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 * Create a new ItemView.
58 * @param[in] factory The factory which provides ItemView with items.
59 * @return A public handle to the newly allocated ItemView.
61 static Dali::Toolkit::ItemView New(ItemFactory& factory);
64 * @copydoc Toolkit::ItemView::GetScrollConnector
66 Dali::Toolkit::ScrollConnector GetScrollConnector() const;
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::SetMouseWheelScrollDistanceStep
131 void SetMouseWheelScrollDistanceStep(float step);
134 * @copydoc Toolkit::ItemView::GetMouseWheelScrollDistanceStep
136 float GetMouseWheelScrollDistanceStep() 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::GetItem
176 Actor GetItem(unsigned int itemId) const;
179 * @copydoc Toolkit::ItemView::GetItemId
181 unsigned int GetItemId(Actor actor) const;
184 * @copydoc Toolkit::ItemView::InsertItem
186 void InsertItem(Item newItem, float durationSeconds);
189 * @copydoc Toolkit::ItemView::InsertItem
191 void InsertItems(const ItemContainer& newItems, float durationSeconds);
194 * @copydoc Toolkit::ItemView::RemoveItem
196 void RemoveItem(ItemId itemId, float durationSeconds);
199 * @copydoc Toolkit::ItemView::InsertItem
201 void RemoveItems(const ItemIdContainer& itemIds, float durationSeconds);
204 * @copydoc Toolkit::ItemView::InsertItem
206 void ReplaceItem(Item replacementItem, float durationSeconds);
209 * @copydoc Toolkit::ItemView::InsertItem
211 void ReplaceItems(const ItemContainer& replacementItems, float durationSeconds);
214 * @copydoc Toolkit::Scrollable::GetDomainSize
216 Vector3 GetDomainSize() const;
219 * @copydoc Toolkit::Scrollable::GetCurrentScrollPosition
221 Vector3 GetCurrentScrollPosition() const;
224 * @copydoc Toolkit::Scrollable::AddOverlay()
226 void AddOverlay(Actor actor);
229 * @copydoc Toolkit::Scrollable::RemoveOverlay()
231 void RemoveOverlay(Actor actor);
234 * @copydoc Toolkit::Scrollable::ScrollTo(const Vector3& position, float duration)
236 void ScrollTo(const Vector3& position, float duration);
239 * @copydoc Toolkit::Internal::Scrollable::SetOvershootEffectColor
241 void SetOvershootEffectColor( const Vector4& color );
244 * @brief Set whether to enable automatic refresh or not. When refresh is disabled,
245 * ItemView will not automatically refresh the cache in the given interval when the
246 * layout position is changed. This is useful in some cases, for example, automatic
247 * refresh is not needed during fast scrolling, otherwise it will cache unneeded
248 * items when the layout position changes quickly.
250 * @param[in] enabled True to enable automatic refresh or false to disable it.
252 void SetRefreshEnabled(bool enabled);
255 * @brief Helper to perform the refresh.
257 * @param[in] currentLayoutPosition The current layout position.
258 * @param[in] cacheExtra Whether to cache extra items during refresh.
260 void DoRefresh(float currentLayoutPosition, bool cacheExtra);
263 * @copydoc Toolkit::ItemView::SetItemsParentOrigin
265 void SetItemsParentOrigin( const Vector3& parentOrigin );
268 * @copydoc Toolkit::ItemView::GetItemsParentOrigin
270 Vector3 GetItemsParentOrigin() const;
273 * @copydoc Toolkit::ItemView::SetItemsAnchorPoint
275 void SetItemsAnchorPoint( const Vector3& anchorPoint );
278 * @copydoc Toolkit::ItemView::GetItemsAnchorPoint
280 Vector3 GetItemsAnchorPoint() const;
283 * @copydoc Toolkit::ItemView::GetItemsRange
285 void GetItemsRange(ItemRange& range);
290 * Remove an Actor if found in the ItemPool.
291 * @param[in] itemId The item to remove.
292 * @return True if the remaining actors were reordered.
294 bool RemoveActor( unsigned int itemId );
297 * Remove any Actors outside a given range.
298 * @param[in] @param[in] range The range of required items.
300 void RemoveActorsOutsideRange( ItemRange range );
303 * Add a range of Actors, if they are not already in the ItemPool.
304 * @param[in] range The range of Item IDs to associate with the new actors.
305 * @param[in] durationSeconds The time taken to fully constrain the newly added actor.
307 void AddActorsWithinRange( ItemRange range, float durationSeconds );
310 * Add a new Actor, if not already in the ItemPool.
311 * @param[in] item The ID for the new item.
312 * @param[in] durationSeconds The time taken to fully constrain the new actor.
314 void AddNewActor( ItemId item, float durationSeconds );
317 * Apply the constraints etc. that are required for ItemView children.
318 * @param[in] item The item to setup.
319 * @param[in] durationSeconds The time taken to fully constrain the actor.
321 void SetupActor( Item item, float durationSeconds );
324 * Remove the Actor from the ItemPool and notify the ItemFactory the actor has been released by ItemView.
325 * @param[in] item The ID for the item to be released.
326 * @param[in] actor The actor to be removed from ItemView.
328 void ReleaseActor( ItemId item, Actor actor );
330 private: // From CustomActorImpl
333 * From CustomActorImpl; called after a child has been added to the owning actor.
334 * @param[in] child The child which has been added.
336 virtual void OnChildAdd(Actor& child);
339 * From CustomActorImpl; called after a touch-signal is received by the owning actor.
340 * @param[in] event The touch event.
341 * @return True if the event should be consumed.
343 virtual bool OnTouchEvent(const TouchEvent& event);
346 * From CustomActorImpl; called after a mouse-wheel-event is received by the owning actor.
347 * @param[in] event The mouse wheel event.
348 * @return True if the event should be consumed.
350 virtual bool OnMouseWheelEvent(const MouseWheelEvent& event);
352 private: // From Control
355 * @copydoc Toolkit::Control::OnInitialize()
357 virtual void OnInitialize();
360 * @copydoc Toolkit::Control::OnAccessibilityPan()
362 virtual bool OnAccessibilityPan(PanGesture gesture);
365 * @copydoc Toolkit::Control::GetNextKeyboardFocusableActor()
367 virtual Actor GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled);
370 * @copydoc Toolkit::Control::OnKeyboardFocusChangeCommitted()
372 virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor);
377 * Construct a new ItemView.
378 * @param[in] factory The factory which provides ItemView with items.
380 ItemView(ItemFactory& factory);
383 * A reference counted object may only be deleted by calling Unreference()
390 ItemView(const ItemView&);
393 ItemView& operator=(const ItemView& rhs);
396 * Helper to re-apply all the constraints after items have been inserted, removed etc.
397 * @param[in] durationSeconds The time taken to fully constrain the actors.
399 void ReapplyAllConstraints( float durationSeconds );
402 * Helper to relayout after item(s) are removed.
404 void OnItemsRemoved();
407 * Helper to remove items outside a given range.
408 * @param[in] range The range of required items.
410 void RemoveItems(ItemRange range);
413 * Helper to add a range of items, if not already in the ItemPool.
414 * @param[in] layout The layout used to position the new items.
415 * @param[in] layoutSize The current size of the layout.
416 * @param[in] range The range of required items.
418 void AddItems(ItemLayout& layout, const Vector3& layoutSize, ItemRange range);
421 * Helper to find the range of items to populate with.
422 * @param[in] layout The current layout.
423 * @param[in] range The range of items.
424 * @param[in] reserveExtra True if reserve items should be included.
426 ItemRange GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra);
431 * Helper to handle pressed (Down) events.
432 * @param[in] x The X coordinate of the touch event.
433 * @param[in] y The Y coordinate of the touch event.
434 * @param[in] timeMs The time-stamp of the touch event.
436 void OnPressed(float x, float y, unsigned long timeMs);
439 * Helper to clamp the first-item position when dragging/swiping.
440 * @param[in] targetPosition The target position of the drag etc.
441 * @param[in] targetSize The target ItemView & layout size.
442 * @param[in] layout The current layout.
443 * @return The clamped first-item position.
445 float ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout);
448 * Called upon pan gesture event.
450 * @param[in] gesture The gesture event.
452 void OnPan( const PanGesture& pan );
455 * Helper to handle anchoring animations.
456 * @return The animation, or an uninitialized handle if not necessary.
458 Animation DoAnchoring();
461 * Callback from scroll animations
462 * @param[in] animation The scroll-animation which has finished.
464 void OnScrollFinished(Animation& animation);
467 * Callback from layout activation scroll animations
468 * @param[in] animation The scroll-animation which has finished.
470 void OnLayoutActivationScrollFinished(Animation& animation);
473 * Called by animation system when overshoot has finished animating to maximum (either -1.0f or 1.0f)
475 * @param[in] animation the animation that has finished
477 void OnOvershootOnFinished(Animation& animation);
480 * This is called after a timeout when no new mouse wheel event is received for a certain period of time.
481 * @return will return false; one-shot timer.
483 bool OnMouseWheelEventFinished();
486 * Stops and removes animation if exists.
487 * @param[in,out] animation The animation handle to be removed.
489 void RemoveAnimation(Animation& animation);
492 * @copydoc Toolkit::Internal::Scrollable::SetOvershootEnabled
494 virtual void SetOvershootEnabled( bool enable );
497 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
498 * @return The scroll overshoot.
500 float CalculateScrollOvershoot();
503 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
505 * @param[in] overshootAmount amount to animate to
506 * @param[in] animateBack whether to animate back to zero immediately after
507 * @return The scroll overshoot.
509 void AnimateScrollOvershoot(float overshootAmount, bool animateBack = false);
512 * Gets the scroll position in pixels according to the logical layout position.
513 * @param[in] layoutSize The current size of the layout.
515 float GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const;
518 * Calculates the minimum and maximum positions for each axis to scroll to.
519 * @param[in] layoutSize The current size of the layout.
521 void CalculateDomainSize(const Vector3& layoutSize);
524 * Calculates whether we want to allow current item view to scroll.
525 * @param[in] layoutSize The current size of the layout.
526 * @return true if itemview is scrollable
528 bool IsLayoutScrollable(const Vector3& layoutSize);
531 * Callback when the current layout position of ItemView changes in both positive
532 * and negative directions by the specified amount. Refresh the ItemView to create
533 * newly visible items.
534 * @param[in] source the property notification that triggered this callback
536 void OnRefreshNotification(PropertyNotification& source);
539 * This is called when scroll position has been changed by ScrollConnector::SetScrollPosition.
540 * @param[in] position The new scroll position
542 void OnScrollPositionChanged( float position );
546 ItemFactory& mItemFactory;
548 typedef std::map<unsigned int, Actor> ItemPool;
549 typedef ItemPool::iterator ItemPoolIter;
550 typedef ItemPool::const_iterator ConstItemPoolIter;
554 ItemLayoutContainer mLayouts;
555 ItemLayout* mActiveLayout;
556 Vector3 mActiveLayoutTargetSize;
558 Animation mResizeAnimation;
559 Animation mScrollAnimation;
560 Animation mScrollOvershootAnimation;
561 bool mAnimatingOvershootOn; ///< whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
562 bool mAnimateOvershootOff; ///< whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
564 bool mAnchoringEnabled;
565 float mAnchoringDuration;
567 float mRefreshIntervalLayoutPositions; ///< Refresh item view when the layout position changes by this interval in both positive and negative directions.
568 PropertyNotification mRefreshNotification; // stores the property notification used for item view refresh
569 bool mRefreshOrderHint; ///< True if scrolling towards the last item
573 float mMinimumSwipeSpeed;
574 float mMinimumSwipeDistance;
575 float mMouseWheelScrollDistanceStep; ///< The step of scroll distance in actor coordinates for each mouse wheel event received.
577 float mScrollDistance;
579 Vector2 mTotalPanDisplacement;
581 float mScrollOvershoot;
584 Timer mMouseWheelEventFinishedTimer; ///< The timer to determine whether there is no mouse wheel event received for a certain period of time.
586 Dali::Gesture::State mGestureState;
588 Actor mOvershootOverlay; ///< The overlay actor for overshoot effect
590 Dali::Toolkit::ScrollConnector mScrollConnector; ///< Connects ItemView with scrollable components e.g. scroll bars
591 Constrainable mScrollPositionObject; ///< From mScrollConnector
595 Property::Index mPropertyPosition; ///< The physical position of the first item within the layout
596 Property::Index mPropertyMinimumLayoutPosition; ///< The minimum valid layout position in the layout.
597 Property::Index mPropertyScrollSpeed; ///< The current scroll speed of item view
599 bool mRefreshEnabled; ///< Whether to refresh the cache automatically
601 Vector3 mItemsParentOrigin;
602 Vector3 mItemsAnchorPoint;
605 } // namespace Internal
607 // Helpers for public-api forwarding methods
609 inline Toolkit::Internal::ItemView& GetImpl(Toolkit::ItemView& itemView)
611 DALI_ASSERT_ALWAYS(itemView);
613 Dali::RefObject& handle = itemView.GetImplementation();
615 return static_cast<Toolkit::Internal::ItemView&>(handle);
618 inline const Toolkit::Internal::ItemView& GetImpl(const Toolkit::ItemView& itemView)
620 DALI_ASSERT_ALWAYS(itemView);
622 const Dali::RefObject& handle = itemView.GetImplementation();
624 return static_cast<const Toolkit::Internal::ItemView&>(handle);
627 } // namespace Toolkit
631 #endif // __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__