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 Flora License, Version 1.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://floralicense.org/license/
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.
21 #include <dali/dali.h>
24 #include <dali-toolkit/public-api/controls/control-impl.h>
25 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-view.h>
26 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-layout.h>
27 #include <dali-toolkit/public-api/controls/scrollable/scroll-connector.h>
28 #include <dali-toolkit/internal/controls/scrollable/scrollable-impl.h>
29 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
30 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-overshoot-indicator-impl.h>
43 typedef IntrusivePtr<ItemView> ItemViewPtr;
46 * ItemPool is container of Actor/ID pairs stored by ItemView
52 typedef std::pair<unsigned int, Actor> IDKeyPair;
53 typedef std::map<unsigned int, Actor> IDKeyContainer;
54 typedef IDKeyContainer::iterator IDKeyIter;
55 typedef IDKeyContainer::const_iterator IDKeyConstIter;
57 typedef std::pair<Actor, unsigned int> ActorKeyPair;
58 typedef std::map<Actor, unsigned int> ActorKeyContainer;
59 typedef ActorKeyContainer::iterator ActorKeyIter;
60 typedef ActorKeyContainer::const_iterator ActorKeyConstIter;
62 public: // Construction & Destruction
66 * @param[in] itemView A reference to the ItemView class that owns this.
68 ItemPool(ItemView& itemView)
76 * Returns a const reference to the IDKeyContainer whose key is based on the Item ID.
77 * @return Const reference to IDKeyContainer.
79 const IDKeyContainer& GetIDKeyContainer() const
81 return mIdKeyContainer;
85 * Returns a const reference to the ActorKeyContainer whose key is based on the Actor.
86 * @return Const reference to IDKeyContainer.
88 const ActorKeyContainer& GetActorKeyContainer() const
90 return mActorKeyContainer;
94 * Add a range of items to the ItemPool, if they are not already in the ItemPool.
95 * @param[in] scrollingTowardsLast Should be true if scrolling towards the last item in the range.
96 * @param[in] range The range of Item IDs to associate with the new actors.
97 * @param[in] targetSize The current size of the layout.
99 void AddItems(bool scrollingTowardsLast, ItemRange range, const Vector3& targetSize);
102 * Remove items from the ItemPool that are outside the given range.
103 * @param[in] range The range of required items.
105 void RemoveItems(ItemRange range);
108 * Remove an item from the ItemPool.
109 * @param[in] itemId The item to remove.
110 * @return Whether the item is found and removed or not.
112 bool RemoveItem(unsigned int itemId);
117 * Adds an item, if not already in the ItemPool.
118 * @param[in] itemId The item to add.
119 * @param[in] targetSize The target size of the layout.
121 void AddItem(unsigned int itemId, const Vector3& targetSize);
125 IDKeyContainer mIdKeyContainer;
126 ActorKeyContainer mActorKeyContainer;
131 * ItemView is a scrollable layout container.
132 * Multiple ItemLayouts may be provided, to determine the logical position of each item a layout.
133 * Actor-ID pairs are provided from a shared ItemFactory, to display the currently visible items.
135 class ItemView : public Scrollable
140 * Create a new ItemView.
141 * @param[in] factory The factory which provides ItemView with items.
142 * @return A public handle to the newly allocated ItemView.
144 static Dali::Toolkit::ItemView New(ItemFactory& factory);
147 * @copydoc Toolkit::ItemView::GetScrollConnector
149 Dali::Toolkit::ScrollConnector GetScrollConnector() const;
152 * @copydoc Toolkit::ItemView::GetLayoutCount
154 unsigned int GetLayoutCount() const;
157 * @copydoc Toolkit::ItemView::AddLayout
159 void AddLayout(ItemLayout& layout);
162 * @copydoc Toolkit::ItemView::RemoveLayout
164 void RemoveLayout(unsigned int layoutIndex);
167 * @copydoc Toolkit::ItemView::GetLayout
169 ItemLayoutPtr GetLayout(unsigned int layoutIndex) const;
172 * @copydoc Toolkit::ItemView::GetActiveLayout
174 ItemLayoutPtr GetActiveLayout() const;
177 * @copydoc Toolkit::ItemView::GetCurrentLayoutPosition
179 float GetCurrentLayoutPosition(unsigned int itemId) const;
182 * @copydoc Toolkit::ItemView::ActivateLayout
184 void ActivateLayout(unsigned int layoutIndex, const Vector3& targetSize, float durationSeconds);
187 * @copydoc Toolkit::ItemView::DeactivateCurrentLayout
189 void DeactivateCurrentLayout();
192 * @copydoc Toolkit::ItemView::SetMinimumSwipeSpeed
194 void SetMinimumSwipeSpeed(float speed);
197 * @copydoc Toolkit::ItemView::GetMinimumSwipeSpeed
199 float GetMinimumSwipeSpeed() const;
202 * @copydoc Toolkit::ItemView::SetMinimumSwipeDistance
204 void SetMinimumSwipeDistance(float distance);
207 * @copydoc Toolkit::ItemView::GetMinimumSwipeDistance
209 float GetMinimumSwipeDistance() const;
212 * @copydoc Toolkit::ItemView::SetMouseWheelScrollDistanceStep
214 void SetMouseWheelScrollDistanceStep(float step);
217 * @copydoc Toolkit::ItemView::GetMouseWheelScrollDistanceStep
219 float GetMouseWheelScrollDistanceStep() const;
222 * @copydoc Toolkit::ItemView::SetAnchoring
224 void SetAnchoring(bool enabled);
227 * @copydoc Toolkit::ItemView::GetAnchoring
229 bool GetAnchoring() const;
232 * @copydoc Toolkit::ItemView::SetAnchoringDuration
234 void SetAnchoringDuration(float durationSeconds);
237 * @copydoc Toolkit::ItemView::GetAnchoringDuration
239 float GetAnchoringDuration() const;
242 * @copydoc Toolkit::ItemView::ScrollToItem
244 void ScrollToItem(unsigned int itemId, float durationSeconds);
247 * @copydoc Toolkit::ItemView::SetRefreshInterval
249 void SetRefreshInterval(unsigned int intervalMilliseconds);
252 * @copydoc Toolkit::ItemView::GetRefreshInterval
254 unsigned int GetRefreshInterval() const;
257 * @copydoc Toolkit::ItemView::GetItem
259 Actor GetItem(unsigned int itemId) const;
262 * @copydoc Toolkit::ItemView::GetItemId
264 unsigned int GetItemId(Actor actor) const;
267 * @copydoc Toolkit::ItemView::RemoveItem
269 void RemoveItem(unsigned int itemId, float durationSeconds);
272 * @copydoc Toolkit::Scrollable::GetDomainSize
274 Vector3 GetDomainSize() const;
277 * @copydoc Toolkit::Scrollable::GetCurrentScrollPosition
279 Vector3 GetCurrentScrollPosition() const;
282 * @copydoc Toolkit::Scrollable::AddOverlay()
284 void AddOverlay(Actor actor);
287 * @copydoc Toolkit::Scrollable::RemoveOverlay()
289 void RemoveOverlay(Actor actor);
292 * @copydoc Toolkit::Scrollable::ScrollTo(const Vector3& position, float duration)
294 void ScrollTo(const Vector3& position, float duration);
296 public: // Used By ItemPool
299 * Called by the ItemPool when it wishes to create an actor from the ItemFactory with the
301 * @param[in] itemId The Item ID of the actor to create.
303 Actor CreateActor(unsigned int itemId);
306 * Called by the ItemPool when an actor is added to the ItemPool.
307 * @param[in] actor The actor that has been added.
308 * @param[in] itemId The associated Item ID.
309 * @param[in] targetSize The target size that was specified upon addition.
311 void ActorAddedToItemPool(Actor actor, unsigned int itemId, const Vector3& targetSize);
314 * Called by the ItemPool when an actor with the specified itemId is about to be removed from the
316 * @param[in] actor The actor about to be removed.
317 * @param[in] itemId The associated Item ID.
319 void ActorRemovedFromItemPool(Actor actor, unsigned int itemId);
321 private: // From CustomActorImpl
324 * From CustomActorImpl; called after a touch-signal is received by the owning actor.
325 * @param[in] event The touch event.
326 * @return True if the event should be consumed.
328 virtual bool OnTouchEvent(const TouchEvent& event);
331 * From CustomActorImpl; called after a mouse-wheel-event is received by the owning actor.
332 * @param[in] event The mouse wheel event.
333 * @return True if the event should be consumed.
335 virtual bool OnMouseWheelEvent(const MouseWheelEvent& event);
337 private: // From ControlImpl
340 * @copydoc Toolkit::Control::OnInitialize()
342 virtual void OnInitialize();
345 * @copydoc Toolkit::Control::OnAccessibilityPan()
347 virtual bool OnAccessibilityPan(PanGesture gesture);
350 * @copydoc Toolkit::Control::GetNextKeyboardFocusableActor()
352 virtual Actor GetNextKeyboardFocusableActor(Actor actor, Control::KeyboardFocusNavigationDirection direction, bool loopEnabled);
355 * @copydoc Toolkit::Control::OnKeyboardFocusChangeCommitted()
357 virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor);
362 * Construct a new ItemView.
363 * @param[in] factory The factory which provides ItemView with items.
365 ItemView(ItemFactory& factory);
368 * A reference counted object may only be deleted by calling Unreference()
375 ItemView(const ItemView&);
378 ItemView& operator=(const ItemView& rhs);
381 * Helper to apply constraints to an actor.
382 * @param[in] actor The actor to constrain.
383 * @param[in] layout The active layout.
384 * @param[in] itemId The ID of the item represented by the actor.
385 * @param[in] durationSeconds The time taken to fully constrain the actors.
387 void ApplyConstraints(Actor& actor, ItemLayout& layout, unsigned int itemId, float durationSeconds);
390 * Helper to remove items outside a given range.
391 * @param[in] range The range of required items.
393 void RemoveItems(ItemRange range);
396 * Helper to add a range of items, if not already in the ItemPool.
397 * @param[in] layout The layout used to position the new items.
398 * @param[in] layoutSize The current size of the layout.
399 * @param[in] range The range of required items.
401 void AddItems(ItemLayout& layout, const Vector3& layoutSize, ItemRange range);
404 * Helper to find the range of items to populate with.
405 * @param[in] layout The current layout.
406 * @param[in] range The range of items.
407 * @param[in] reserveExtra True if reserve items should be included.
409 ItemRange GetItemRange(ItemLayout& layout, const Vector3& layoutSize, bool reserveExtra);
414 * Helper to handle pressed (Down) events.
415 * @param[in] x The X coordinate of the touch event.
416 * @param[in] y The Y coordinate of the touch event.
417 * @param[in] timeMs The time-stamp of the touch event.
419 void OnPressed(float x, float y, unsigned long timeMs);
422 * Helper to clamp the first-item position when dragging/swiping.
423 * @param[in] targetPosition The target position of the drag etc.
424 * @param[in] targetSize The target ItemView & layout size.
425 * @param[in] layout The current layout.
426 * @return The clamped first-item position.
428 float ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout);
431 * Called upon pan gesture event.
433 * @param[in] gesture The gesture event.
435 void OnPan(PanGesture pan);
438 * Helper to handle anchoring animations.
439 * @return The animation, or an uninitialized handle if not necessary.
441 Animation DoAnchoring();
444 * Callback from scroll animations
445 * @param[in] animation The scroll-animation which has finished.
447 void OnScrollFinished(Animation& animation);
450 * Called by animation system when overshoot has finished animating to maximum (either -1.0f or 1.0f)
452 * @param[in] animation the animation that has finished
454 void OnOvershootOnFinished(Animation& animation);
457 * Helper to start the refresh timer.
459 void StartRefreshTimer();
462 * Helper to cancel the refresh timer.
464 void CancelRefreshTimer();
467 * Refresh the ItemView; this is called after a timeout when scrolling.
468 * During a refresh, ItemFactory::NewItem() will be called to create newly visible items.
469 * @return True if the refresh timer should be kept running.
471 bool OnRefreshTick();
474 * This is called after a timeout when no new mouse wheel event is received for a certain period of time.
475 * @return will return false; one-shot timer.
477 bool OnMouseWheelEventFinished();
480 * Stops and removes animation if exists.
481 * @param[in,out] animation The animation handle to be removed.
483 void RemoveAnimation(Animation& animation);
486 * Helper to apply constraints to the overshoot overlay actor.
488 void ApplyOvershootOverlayConstraints();
491 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
492 * @return The scroll overshoot.
494 float CalculateScrollOvershoot();
497 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
499 * @param[in] overshootAmount amount to animate to
500 * @param[in] animateBack whether to animate back to zero immediately after
501 * @return The scroll overshoot.
503 void AnimateScrollOvershoot(float overshootAmount, bool animateBack = false);
506 * Gets the scroll position in pixels according to the logical layout position.
507 * @param[in] layoutSize The current size of the layout.
509 float GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const;
512 * Calculates the minimum and maximum positions for each axis to scroll to.
513 * @param[in] layoutSize The current size of the layout.
515 void CalculateDomainSize(const Vector3& layoutSize);
519 * Calculates whether we want to allow current item view to scroll.
520 * @param[in] layoutSize The current size of the layout.
521 * @return true if itemview is scrollable
523 bool IsLayoutScrollable(const Vector3& layoutSize);
527 ItemFactory& mItemFactory;
531 ItemLayoutContainer mLayouts;
532 ItemLayout* mActiveLayout;
534 Animation mResizeAnimation;
535 Animation mScrollAnimation;
536 Animation mScrollSpeedAnimation;
537 Animation mScrollOvershootAnimation;
538 bool mAnimatingOvershootOn; ///< whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
539 bool mAnimateOvershootOff; ///< whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
541 bool mAnchoringEnabled;
542 float mAnchoringDuration;
545 int mRefreshIntervalMilliseconds;
546 bool mRefreshOrderHint; ///< True if scrolling towards the last item
550 float mMinimumSwipeSpeed;
551 float mMinimumSwipeDistance;
552 float mMouseWheelScrollDistanceStep; ///< The step of scroll distance in actor coordinates for each mouse wheel event received.
554 float mScrollDistance;
556 Vector2 mTotalPanDisplacement;
558 float mScrollOvershoot;
561 Timer mMouseWheelEventFinishedTimer; ///< The timer to determine whether there is no mouse wheel event received for a certain period of time.
563 Dali::Gesture::State mGestureState;
565 ImageActor mOvershootOverlay; ///< The overlay actor for overshoot effect
566 OvershootRippleEffect mOvershootEffect; ///< The vertex/fragment shader used to display the overshoot ripple effect
568 Dali::Toolkit::ScrollConnector mScrollConnector; ///< Connects ItemView with scrollable components e.g. scroll bars
569 Constrainable mScrollPositionObject; ///< From mScrollConnector
571 Property::Index mPropertyPosition; ///< The physical position of the first item within the layout
572 Property::Index mPropertyMinimumLayoutPosition; ///< The minimum valid layout position in the layout.
573 Property::Index mPropertyScrollSpeed; ///< The current scroll speed of item view
574 Property::Index mPropertyOvershoot; ///< The scroll overshoot (difference of the layout position before and after clamping)
577 } // namespace Internal
579 // Helpers for public-api forwarding methods
581 inline Toolkit::Internal::ItemView& GetImpl(Toolkit::ItemView& itemView)
583 DALI_ASSERT_ALWAYS(itemView);
585 Dali::RefObject& handle = itemView.GetImplementation();
587 return static_cast<Toolkit::Internal::ItemView&>(handle);
590 inline const Toolkit::Internal::ItemView& GetImpl(const Toolkit::ItemView& itemView)
592 DALI_ASSERT_ALWAYS(itemView);
594 const Dali::RefObject& handle = itemView.GetImplementation();
596 return static_cast<const Toolkit::Internal::ItemView&>(handle);
599 } // namespace Toolkit
603 #endif // __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__