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/internal/controls/scrollable/scrollable-impl.h>
28 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
29 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-overshoot-indicator-impl.h>
42 typedef IntrusivePtr<ItemView> ItemViewPtr;
45 * ItemPool is container of Actor/ID pairs stored by ItemView
51 typedef std::pair<unsigned int, Actor> IDKeyPair;
52 typedef std::map<unsigned int, Actor> IDKeyContainer;
53 typedef IDKeyContainer::iterator IDKeyIter;
54 typedef IDKeyContainer::const_iterator IDKeyConstIter;
56 typedef std::pair<Actor, unsigned int> ActorKeyPair;
57 typedef std::map<Actor, unsigned int> ActorKeyContainer;
58 typedef ActorKeyContainer::iterator ActorKeyIter;
59 typedef ActorKeyContainer::const_iterator ActorKeyConstIter;
61 public: // Construction & Destruction
65 * @param[in] itemView A reference to the ItemView class that owns this.
67 ItemPool(ItemView& itemView)
75 * Returns a const reference to the IDKeyContainer whose key is based on the Item ID.
76 * @return Const reference to IDKeyContainer.
78 const IDKeyContainer& GetIDKeyContainer() const
80 return mIdKeyContainer;
84 * Returns a const reference to the ActorKeyContainer whose key is based on the Actor.
85 * @return Const reference to IDKeyContainer.
87 const ActorKeyContainer& GetActorKeyContainer() const
89 return mActorKeyContainer;
93 * Add a range of items to the ItemPool, if they are not already in the ItemPool.
94 * @param[in] scrollingTowardsLast Should be true if scrolling towards the last item in the range.
95 * @param[in] range The range of Item IDs to associate with the new actors.
96 * @param[in] targetSize The current size of the layout.
98 void AddItems(bool scrollingTowardsLast, ItemRange range, const Vector3& targetSize);
101 * Remove items from the ItemPool that are outside the given range.
102 * @param[in] range The range of required items.
104 void RemoveItems(ItemRange range);
107 * Remove an item from the ItemPool.
108 * @param[in] itemId The item to remove.
109 * @return Whether the item is found and removed or not.
111 bool RemoveItem(unsigned int itemId);
116 * Adds an item, if not already in the ItemPool.
117 * @param[in] itemId The item to add.
118 * @param[in] targetSize The target size of the layout.
120 void AddItem(unsigned int itemId, const Vector3& targetSize);
124 IDKeyContainer mIdKeyContainer;
125 ActorKeyContainer mActorKeyContainer;
130 * ItemView is a scrollable layout container.
131 * Multiple ItemLayouts may be provided, to determine the logical position of each item a layout.
132 * Actor-ID pairs are provided from a shared ItemFactory, to display the currently visible items.
134 class ItemView : public Scrollable
139 * Create a new ItemView.
140 * @param[in] factory The factory which provides ItemView with items.
141 * @return A public handle to the newly allocated ItemView.
143 static Dali::Toolkit::ItemView New(ItemFactory& factory);
146 * @copydoc Toolkit::ItemView::GetLayoutCount
148 unsigned int GetLayoutCount() const;
151 * @copydoc Toolkit::ItemView::AddLayout
153 void AddLayout(ItemLayout& layout);
156 * @copydoc Toolkit::ItemView::RemoveLayout
158 void RemoveLayout(unsigned int layoutIndex);
161 * @copydoc Toolkit::ItemView::GetLayout
163 ItemLayoutPtr GetLayout(unsigned int layoutIndex) const;
166 * @copydoc Toolkit::ItemView::GetActiveLayout
168 ItemLayoutPtr GetActiveLayout() const;
171 * @copydoc Toolkit::ItemView::GetCurrentLayoutPosition
173 float GetCurrentLayoutPosition(unsigned int itemId) const;
176 * @copydoc Toolkit::ItemView::ActivateLayout
178 void ActivateLayout(unsigned int layoutIndex, const Vector3& targetSize, float durationSeconds);
181 * @copydoc Toolkit::ItemView::DeactivateCurrentLayout
183 void DeactivateCurrentLayout();
186 * @copydoc Toolkit::ItemView::SetMinimumSwipeSpeed
188 void SetMinimumSwipeSpeed(float speed);
191 * @copydoc Toolkit::ItemView::GetMinimumSwipeSpeed
193 float GetMinimumSwipeSpeed() const;
196 * @copydoc Toolkit::ItemView::SetMinimumSwipeDistance
198 void SetMinimumSwipeDistance(float distance);
201 * @copydoc Toolkit::ItemView::GetMinimumSwipeDistance
203 float GetMinimumSwipeDistance() const;
206 * @copydoc Toolkit::ItemView::SetMouseWheelScrollDistanceStep
208 void SetMouseWheelScrollDistanceStep(float step);
211 * @copydoc Toolkit::ItemView::GetMouseWheelScrollDistanceStep
213 float GetMouseWheelScrollDistanceStep() const;
216 * @copydoc Toolkit::ItemView::SetAnchoring
218 void SetAnchoring(bool enabled);
221 * @copydoc Toolkit::ItemView::GetAnchoring
223 bool GetAnchoring() const;
226 * @copydoc Toolkit::ItemView::SetAnchoringDuration
228 void SetAnchoringDuration(float durationSeconds);
231 * @copydoc Toolkit::ItemView::GetAnchoringDuration
233 float GetAnchoringDuration() const;
236 * @copydoc Toolkit::ItemView::ScrollToItem
238 void ScrollToItem(unsigned int itemId, float durationSeconds);
241 * @copydoc Toolkit::ItemView::SetRefreshInterval
243 void SetRefreshInterval(unsigned int intervalMilliseconds);
246 * @copydoc Toolkit::ItemView::GetRefreshInterval
248 unsigned int GetRefreshInterval() const;
251 * @copydoc Toolkit::ItemView::GetItem
253 Actor GetItem(unsigned int itemId) const;
256 * @copydoc Toolkit::ItemView::GetItemId
258 unsigned int GetItemId(Actor actor) const;
261 * @copydoc Toolkit::ItemView::RemoveItem
263 void RemoveItem(unsigned int itemId, float durationSeconds);
266 * @copydoc Toolkit::Scrollable::GetDomainSize
268 Vector3 GetDomainSize() const;
271 * @copydoc Toolkit::Scrollable::GetCurrentScrollPosition
273 Vector3 GetCurrentScrollPosition() const;
276 * @copydoc Toolkit::Scrollable::AddOverlay()
278 void AddOverlay(Actor actor);
281 * @copydoc Toolkit::Scrollable::RemoveOverlay()
283 void RemoveOverlay(Actor actor);
286 * @copydoc Toolkit::Scrollable::ScrollTo(const Vector3& position, float duration)
288 void ScrollTo(const Vector3& position, float duration);
290 public: // Used By ItemPool
293 * Called by the ItemPool when it wishes to create an actor from the ItemFactory with the
295 * @param[in] itemId The Item ID of the actor to create.
297 Actor CreateActor(unsigned int itemId);
300 * Called by the ItemPool when an actor is added to the ItemPool.
301 * @param[in] actor The actor that has been added.
302 * @param[in] itemId The associated Item ID.
303 * @param[in] targetSize The target size that was specified upon addition.
305 void ActorAddedToItemPool(Actor actor, unsigned int itemId, const Vector3& targetSize);
308 * Called by the ItemPool when an actor with the specified itemId is about to be removed from the
310 * @param[in] actor The actor about to be removed.
311 * @param[in] itemId The associated Item ID.
313 void ActorRemovedFromItemPool(Actor actor, unsigned int itemId);
315 private: // From CustomActorImpl
318 * From CustomActorImpl; called after a touch-signal is received by the owning actor.
319 * @param[in] event The touch event.
320 * @return True if the event should be consumed.
322 virtual bool OnTouchEvent(const TouchEvent& event);
325 * From CustomActorImpl; called after a mouse-wheel-event is received by the owning actor.
326 * @param[in] event The mouse wheel event.
327 * @return True if the event should be consumed.
329 virtual bool OnMouseWheelEvent(const MouseWheelEvent& event);
331 private: // From ControlImpl
334 * @copydoc Toolkit::Control::OnInitialize()
336 virtual void OnInitialize();
339 * @copydoc Toolkit::Control::OnAccessibilityPan()
341 virtual bool OnAccessibilityPan(PanGesture gesture);
344 * @copydoc Toolkit::Control::GetNextKeyboardFocusableActor()
346 virtual Actor GetNextKeyboardFocusableActor(Actor actor, Control::KeyboardFocusNavigationDirection direction, bool loopEnabled);
349 * @copydoc Toolkit::Control::OnKeyboardFocusChangeCommitted()
351 virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor);
356 * Construct a new ItemView.
357 * @param[in] factory The factory which provides ItemView with items.
359 ItemView(ItemFactory& factory);
362 * A reference counted object may only be deleted by calling Unreference()
369 ItemView(const ItemView&);
372 ItemView& operator=(const ItemView& rhs);
375 * Helper to apply constraints to an actor.
376 * @param[in] actor The actor to constrain.
377 * @param[in] layout The active layout.
378 * @param[in] itemId The ID of the item represented by the actor.
379 * @param[in] durationSeconds The time taken to fully constrain the actors.
381 void ApplyConstraints(Actor& actor, ItemLayout& layout, unsigned int itemId, float durationSeconds);
384 * Helper to remove items outside a given range.
385 * @param[in] range The range of required items.
387 void RemoveItems(ItemRange range);
390 * Helper to add a range of items, if not already in the ItemPool.
391 * @param[in] layout The layout used to position the new items.
392 * @param[in] layoutSize The current size of the layout.
393 * @param[in] range The range of required items.
395 void AddItems(ItemLayout& layout, const Vector3& layoutSize, ItemRange range);
398 * Helper to find the range of items to populate with.
399 * @param[in] layout The current layout.
400 * @param[in] range The range of items.
401 * @param[in] reserveExtra True if reserve items should be included.
403 ItemRange GetItemRange(ItemLayout& layout, const Vector3& layoutSize, bool reserveExtra);
408 * Helper to handle pressed (Down) events.
409 * @param[in] x The X coordinate of the touch event.
410 * @param[in] y The Y coordinate of the touch event.
411 * @param[in] timeMs The time-stamp of the touch event.
413 void OnPressed(float x, float y, unsigned long timeMs);
416 * Helper to clamp the first-item position when dragging/swiping.
417 * @param[in] targetPosition The target position of the drag etc.
418 * @param[in] targetSize The target ItemView & layout size.
419 * @param[in] layout The current layout.
420 * @return The clamped first-item position.
422 float ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout);
425 * Called upon pan gesture event.
427 * @param[in] gesture The gesture event.
429 void OnPan(PanGesture pan);
432 * Helper to handle anchoring animations.
433 * @return The animation, or an uninitialized handle if not necessary.
435 Animation DoAnchoring();
438 * Callback from scroll animations
439 * @param[in] animation The scroll-animation which has finished.
441 void OnScrollFinished(Animation& animation);
444 * Called by animation system when overshoot has finished animating to maximum (either -1.0f or 1.0f)
446 * @param[in] animation the animation that has finished
448 void OnOvershootOnFinished(Animation& animation);
451 * Helper to start the refresh timer.
453 void StartRefreshTimer();
456 * Helper to cancel the refresh timer.
458 void CancelRefreshTimer();
461 * Refresh the ItemView; this is called after a timeout when scrolling.
462 * During a refresh, ItemFactory::NewItem() will be called to create newly visible items.
463 * @return True if the refresh timer should be kept running.
465 bool OnRefreshTick();
468 * This is called after a timeout when no new mouse wheel event is received for a certain period of time.
469 * @return will return false; one-shot timer.
471 bool OnMouseWheelEventFinished();
474 * Stops and removes animation if exists.
475 * @param[in,out] animation The animation handle to be removed.
477 void RemoveAnimation(Animation& animation);
480 * Helper to apply constraints to the overshoot overlay actor.
482 void ApplyOvershootOverlayConstraints();
485 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
486 * @return The scroll overshoot.
488 float CalculateScrollOvershoot();
491 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
493 * @param[in] overshootAmount amount to animate to
494 * @param[in] animateBack whether to animate back to zero immediately after
495 * @return The scroll overshoot.
497 void AnimateScrollOvershoot(float overshootAmount, bool animateBack = false);
500 * Gets the scroll position in pixels according to the logical layout position.
501 * @param[in] layoutSize The current size of the layout.
503 float GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const;
506 * Calculates the minimum and maximum positions for each axis to scroll to.
507 * @param[in] layoutSize The current size of the layout.
509 void CalculateDomainSize(const Vector3& layoutSize);
513 * Calculates whether we want to allow current item view to scroll.
514 * @param[in] layoutSize The current size of the layout.
515 * @return true if itemview is scrollable
517 bool IsLayoutScrollable(const Vector3& layoutSize);
521 ItemFactory& mItemFactory;
525 ItemLayoutContainer mLayouts;
526 ItemLayout* mActiveLayout;
528 Animation mResizeAnimation;
529 Animation mScrollAnimation;
530 Animation mScrollSpeedAnimation;
531 Animation mScrollOvershootAnimation;
532 bool mAnimatingOvershootOn; ///< whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
533 bool mAnimateOvershootOff; ///< whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
535 bool mAnchoringEnabled;
536 float mAnchoringDuration;
539 int mRefreshIntervalMilliseconds;
540 bool mRefreshOrderHint; ///< True if scrolling towards the last item
544 float mMinimumSwipeSpeed;
545 float mMinimumSwipeDistance;
546 float mMouseWheelScrollDistanceStep; ///< The step of scroll distance in actor coordinates for each mouse wheel event received.
548 float mScrollDistance;
550 Vector2 mTotalPanDisplacement;
552 float mScrollOvershoot;
555 Timer mMouseWheelEventFinishedTimer; ///< The timer to determine whether there is no mouse wheel event received for a certain period of time.
557 Dali::Gesture::State mGestureState;
559 ImageActor mOvershootOverlay; ///< The overlay actor for overshoot effect
560 OvershootRippleEffect mOvershootEffect; ///< The vertex/fragment shader used to display the overshoot ripple effect
562 Property::Index mPropertyLayoutPosition; ///< The logical position of the first item within the layout
563 Property::Index mPropertyPosition; ///< The physical position of the first item within the layout
564 Property::Index mPropertyMinimumLayoutPosition; ///< The minimum valid layout position in the layout.
565 Property::Index mPropertyScrollSpeed; ///< The current scroll speed of item view
566 Property::Index mPropertyOvershoot; ///< The scroll overshoot (difference of the layout position before and after clamping)
569 } // namespace Internal
571 // Helpers for public-api forwarding methods
573 inline Toolkit::Internal::ItemView& GetImpl(Toolkit::ItemView& itemView)
575 DALI_ASSERT_ALWAYS(itemView);
577 Dali::RefObject& handle = itemView.GetImplementation();
579 return static_cast<Toolkit::Internal::ItemView&>(handle);
582 inline const Toolkit::Internal::ItemView& GetImpl(const Toolkit::ItemView& itemView)
584 DALI_ASSERT_ALWAYS(itemView);
586 const Dali::RefObject& handle = itemView.GetImplementation();
588 return static_cast<const Toolkit::Internal::ItemView&>(handle);
591 } // namespace Toolkit
595 #endif // __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__