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/dali.h>
25 #include <dali-toolkit/public-api/controls/control-impl.h>
26 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-view.h>
27 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-layout.h>
28 #include <dali-toolkit/public-api/controls/scrollable/scroll-connector.h>
29 #include <dali-toolkit/internal/controls/scrollable/scrollable-impl.h>
30 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
43 typedef IntrusivePtr<ItemView> ItemViewPtr;
46 * ItemView is a scrollable layout container.
47 * Multiple ItemLayouts may be provided, to determine the logical position of each item a layout.
48 * Actor-ID pairs are provided from a shared ItemFactory, to display the currently visible items.
50 class ItemView : public Scrollable
55 * Create a new ItemView.
56 * @param[in] factory The factory which provides ItemView with items.
57 * @return A public handle to the newly allocated ItemView.
59 static Dali::Toolkit::ItemView New(ItemFactory& factory);
62 * @copydoc Toolkit::ItemView::GetScrollConnector
64 Dali::Toolkit::ScrollConnector GetScrollConnector() const;
67 * @copydoc Toolkit::ItemView::GetLayoutCount
69 unsigned int GetLayoutCount() const;
72 * @copydoc Toolkit::ItemView::AddLayout
74 void AddLayout(ItemLayout& layout);
77 * @copydoc Toolkit::ItemView::RemoveLayout
79 void RemoveLayout(unsigned int layoutIndex);
82 * @copydoc Toolkit::ItemView::GetLayout
84 ItemLayoutPtr GetLayout(unsigned int layoutIndex) const;
87 * @copydoc Toolkit::ItemView::GetActiveLayout
89 ItemLayoutPtr GetActiveLayout() const;
92 * @copydoc Toolkit::ItemView::GetCurrentLayoutPosition
94 float GetCurrentLayoutPosition(unsigned int itemId) const;
97 * @copydoc Toolkit::ItemView::ActivateLayout
99 void ActivateLayout(unsigned int layoutIndex, const Vector3& targetSize, float durationSeconds);
102 * @copydoc Toolkit::ItemView::SetDefaultAlphaFunction
104 void SetDefaultAlphaFunction(AlphaFunction func);
107 * @copydoc Toolkit::ItemView::GetDefaultAlphaFunction
109 AlphaFunction GetDefaultAlphaFunction() const;
112 * @copydoc Toolkit::ItemView::DeactivateCurrentLayout
114 void DeactivateCurrentLayout();
117 * @copydoc Toolkit::ItemView::SetMinimumSwipeSpeed
119 void SetMinimumSwipeSpeed(float speed);
122 * @copydoc Toolkit::ItemView::GetMinimumSwipeSpeed
124 float GetMinimumSwipeSpeed() const;
127 * @copydoc Toolkit::ItemView::SetMinimumSwipeDistance
129 void SetMinimumSwipeDistance(float distance);
132 * @copydoc Toolkit::ItemView::GetMinimumSwipeDistance
134 float GetMinimumSwipeDistance() const;
137 * @copydoc Toolkit::ItemView::SetMouseWheelScrollDistanceStep
139 void SetMouseWheelScrollDistanceStep(float step);
142 * @copydoc Toolkit::ItemView::GetMouseWheelScrollDistanceStep
144 float GetMouseWheelScrollDistanceStep() const;
147 * @copydoc Toolkit::ItemView::SetAnchoring
149 void SetAnchoring(bool enabled);
152 * @copydoc Toolkit::ItemView::GetAnchoring
154 bool GetAnchoring() const;
157 * @copydoc Toolkit::ItemView::SetAnchoringDuration
159 void SetAnchoringDuration(float durationSeconds);
162 * @copydoc Toolkit::ItemView::GetAnchoringDuration
164 float GetAnchoringDuration() const;
167 * @copydoc Toolkit::ItemView::ScrollToItem
169 void ScrollToItem(unsigned int itemId, float durationSeconds);
172 * @copydoc Toolkit::ItemView::SetRefreshInterval
174 void SetRefreshInterval(float intervalLayoutPositions);
177 * @copydoc Toolkit::ItemView::GetRefreshInterval
179 float GetRefreshInterval() const;
182 * @copydoc Toolkit::ItemView::GetItem
184 Actor GetItem(unsigned int itemId) const;
187 * @copydoc Toolkit::ItemView::GetItemId
189 unsigned int GetItemId(Actor actor) const;
192 * @copydoc Toolkit::ItemView::InsertItem
194 void InsertItem(Item newItem, float durationSeconds);
197 * @copydoc Toolkit::ItemView::InsertItem
199 void InsertItems(const ItemContainer& newItems, float durationSeconds);
202 * @copydoc Toolkit::ItemView::RemoveItem
204 void RemoveItem(ItemId itemId, float durationSeconds);
207 * @copydoc Toolkit::ItemView::InsertItem
209 void RemoveItems(const ItemIdContainer& itemIds, float durationSeconds);
212 * @copydoc Toolkit::ItemView::InsertItem
214 void ReplaceItem(Item replacementItem, float durationSeconds);
217 * @copydoc Toolkit::ItemView::InsertItem
219 void ReplaceItems(const ItemContainer& replacementItems, float durationSeconds);
222 * @copydoc Toolkit::Scrollable::GetDomainSize
224 Vector3 GetDomainSize() const;
227 * @copydoc Toolkit::Scrollable::GetCurrentScrollPosition
229 Vector3 GetCurrentScrollPosition() const;
232 * @copydoc Toolkit::Scrollable::AddOverlay()
234 void AddOverlay(Actor actor);
237 * @copydoc Toolkit::Scrollable::RemoveOverlay()
239 void RemoveOverlay(Actor actor);
242 * @copydoc Toolkit::Scrollable::ScrollTo(const Vector3& position, float duration)
244 void ScrollTo(const Vector3& position, float duration);
247 * @copydoc Toolkit::Internal::Scrollable::SetOvershootEffectColor
249 void SetOvershootEffectColor( const Vector4& color );
252 * @brief Set whether to enable automatic refresh or not. When refresh is disabled,
253 * ItemView will not automatically refresh the cache in the given interval when the
254 * layout position is changed. This is useful in some cases, for example, automatic
255 * refresh is not needed during fast scrolling, otherwise it will cache unneeded
256 * items when the layout position changes quickly.
258 * @param[in] enabled True to enable automatic refresh or false to disable it.
260 void SetRefreshEnabled(bool enabled);
263 * @brief Helper to perform the refresh.
265 * @param[in] currentLayoutPosition The current layout position.
266 * @param[in] cacheExtra Whether to cache extra items during refresh.
268 void DoRefresh(float currentLayoutPosition, bool cacheExtra);
271 * @copydoc Toolkit::ItemView::SetItemsParentOrigin
273 void SetItemsParentOrigin( const Vector3& parentOrigin );
276 * @copydoc Toolkit::ItemView::GetItemsParentOrigin
278 Vector3 GetItemsParentOrigin() const;
281 * @copydoc Toolkit::ItemView::SetItemsAnchorPoint
283 void SetItemsAnchorPoint( const Vector3& anchorPoint );
286 * @copydoc Toolkit::ItemView::GetItemsAnchorPoint
288 Vector3 GetItemsAnchorPoint() const;
291 * @copydoc Toolkit::ItemView::GetItemsRange
293 void GetItemsRange(ItemRange& range);
298 * Remove an Actor if found in the ItemPool.
299 * @param[in] itemId The item to remove.
300 * @return True if an actor was removed.
302 bool RemoveActor( unsigned int itemId );
305 * Remove any Actors outside a given range.
306 * @param[in] @param[in] range The range of required items.
308 void RemoveActorsOutsideRange( ItemRange range );
311 * Add a range of Actors, if they are not already in the ItemPool.
312 * @param[in] range The range of Item IDs to associate with the new actors.
313 * @param[in] durationSeconds The time taken to fully constrain the newly added actor.
315 void AddActorsWithinRange( ItemRange range, float durationSeconds );
318 * Add a new Actor, if not already in the ItemPool.
319 * @param[in] item The ID for the new item.
320 * @param[in] durationSeconds The time taken to fully constrain the new actor.
322 void AddNewActor( ItemId item, float durationSeconds );
325 * Apply the constraints etc. that are required for ItemView children.
326 * @param[in] item The item to setup.
327 * @param[in] durationSeconds The time taken to fully constrain the actor.
329 void SetupActor( Item item, float durationSeconds );
332 * Remove the Actor from the ItemPool and notify the ItemFactory the actor has been released by ItemView.
333 * @param[in] item The ID for the item to be released.
334 * @param[in] actor The actor to be removed from ItemView.
336 void ReleaseActor( ItemId item, Actor actor );
338 private: // From CustomActorImpl
341 * From CustomActorImpl; called after a child has been added to the owning actor.
342 * @param[in] child The child which has been added.
344 virtual void OnChildAdd(Actor& child);
347 * From CustomActorImpl; called after a touch-signal is received by the owning actor.
348 * @param[in] event The touch event.
349 * @return True if the event should be consumed.
351 virtual bool OnTouchEvent(const TouchEvent& event);
354 * From CustomActorImpl; called after a mouse-wheel-event is received by the owning actor.
355 * @param[in] event The mouse wheel event.
356 * @return True if the event should be consumed.
358 virtual bool OnMouseWheelEvent(const MouseWheelEvent& event);
360 private: // From Control
363 * @copydoc Toolkit::Control::OnInitialize()
365 virtual void OnInitialize();
368 * @copydoc Toolkit::Control::OnAccessibilityPan()
370 virtual bool OnAccessibilityPan(PanGesture gesture);
373 * @copydoc Toolkit::Control::GetNextKeyboardFocusableActor()
375 virtual Actor GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled);
378 * @copydoc Toolkit::Control::OnKeyboardFocusChangeCommitted()
380 virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor);
385 * Construct a new ItemView.
386 * @param[in] factory The factory which provides ItemView with items.
388 ItemView(ItemFactory& factory);
391 * A reference counted object may only be deleted by calling Unreference()
398 ItemView(const ItemView&);
401 ItemView& operator=(const ItemView& rhs);
404 * Helper to re-apply all the constraints after items have been inserted, removed etc.
405 * @param[in] durationSeconds The time taken to fully constrain the actors.
407 void ReapplyAllConstraints( float durationSeconds );
410 * Helper to remove items outside a given range.
411 * @param[in] range The range of required items.
413 void RemoveItems(ItemRange range);
416 * Helper to add a range of items, if not already in the ItemPool.
417 * @param[in] layout The layout used to position the new items.
418 * @param[in] layoutSize The current size of the layout.
419 * @param[in] range The range of required items.
421 void AddItems(ItemLayout& layout, const Vector3& layoutSize, ItemRange range);
424 * Helper to find the range of items to populate with.
425 * @param[in] layout The current layout.
426 * @param[in] range The range of items.
427 * @param[in] reserveExtra True if reserve items should be included.
429 ItemRange GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra);
434 * Helper to handle pressed (Down) events.
435 * @param[in] x The X coordinate of the touch event.
436 * @param[in] y The Y coordinate of the touch event.
437 * @param[in] timeMs The time-stamp of the touch event.
439 void OnPressed(float x, float y, unsigned long timeMs);
442 * Helper to clamp the first-item position when dragging/swiping.
443 * @param[in] targetPosition The target position of the drag etc.
444 * @param[in] targetSize The target ItemView & layout size.
445 * @param[in] layout The current layout.
446 * @return The clamped first-item position.
448 float ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout);
451 * Called upon pan gesture event.
453 * @param[in] gesture The gesture event.
455 void OnPan(PanGesture pan);
458 * Helper to handle anchoring animations.
459 * @return The animation, or an uninitialized handle if not necessary.
461 Animation DoAnchoring();
464 * Callback from scroll animations
465 * @param[in] animation The scroll-animation which has finished.
467 void OnScrollFinished(Animation& animation);
470 * Callback from layout activation scroll animations
471 * @param[in] animation The scroll-animation which has finished.
473 void OnLayoutActivationScrollFinished(Animation& animation);
476 * Called by animation system when overshoot has finished animating to maximum (either -1.0f or 1.0f)
478 * @param[in] animation the animation that has finished
480 void OnOvershootOnFinished(Animation& animation);
483 * This is called after a timeout when no new mouse wheel event is received for a certain period of time.
484 * @return will return false; one-shot timer.
486 bool OnMouseWheelEventFinished();
489 * Stops and removes animation if exists.
490 * @param[in,out] animation The animation handle to be removed.
492 void RemoveAnimation(Animation& animation);
495 * @copydoc Toolkit::Internal::Scrollable::SetOvershootEnabled
497 virtual void SetOvershootEnabled( bool enable );
500 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
501 * @return The scroll overshoot.
503 float CalculateScrollOvershoot();
506 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
508 * @param[in] overshootAmount amount to animate to
509 * @param[in] animateBack whether to animate back to zero immediately after
510 * @return The scroll overshoot.
512 void AnimateScrollOvershoot(float overshootAmount, bool animateBack = false);
515 * Gets the scroll position in pixels according to the logical layout position.
516 * @param[in] layoutSize The current size of the layout.
518 float GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const;
521 * Calculates the minimum and maximum positions for each axis to scroll to.
522 * @param[in] layoutSize The current size of the layout.
524 void CalculateDomainSize(const Vector3& layoutSize);
527 * Calculates whether we want to allow current item view to scroll.
528 * @param[in] layoutSize The current size of the layout.
529 * @return true if itemview is scrollable
531 bool IsLayoutScrollable(const Vector3& layoutSize);
534 * Callback when the current layout position of ItemView changes in both positive
535 * and negative directions by the specified amount. Refresh the ItemView to create
536 * newly visible items.
537 * @param[in] source the property notification that triggered this callback
539 void OnRefreshNotification(PropertyNotification& source);
542 * This is called when scroll position has been changed by ScrollConnector::SetScrollPosition.
543 * @param[in] position The new scroll position
545 void OnScrollPositionChanged( float position );
549 ItemFactory& mItemFactory;
551 typedef std::map<unsigned int, Actor> ItemPool;
552 typedef ItemPool::iterator ItemPoolIter;
553 typedef ItemPool::const_iterator ConstItemPoolIter;
557 ItemLayoutContainer mLayouts;
558 ItemLayout* mActiveLayout;
559 Vector3 mActiveLayoutTargetSize;
561 AlphaFunction mDefaultAlphaFunction;
563 Animation mResizeAnimation;
564 Animation mScrollAnimation;
565 Animation mScrollOvershootAnimation;
566 bool mAnimatingOvershootOn; ///< whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
567 bool mAnimateOvershootOff; ///< whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
569 bool mAnchoringEnabled;
570 float mAnchoringDuration;
572 float mRefreshIntervalLayoutPositions; ///< Refresh item view when the layout position changes by this interval in both positive and negative directions.
573 PropertyNotification mRefreshNotification; // stores the property notification used for item view refresh
574 bool mRefreshOrderHint; ///< True if scrolling towards the last item
578 float mMinimumSwipeSpeed;
579 float mMinimumSwipeDistance;
580 float mMouseWheelScrollDistanceStep; ///< The step of scroll distance in actor coordinates for each mouse wheel event received.
582 float mScrollDistance;
584 Vector2 mTotalPanDisplacement;
586 float mScrollOvershoot;
589 Timer mMouseWheelEventFinishedTimer; ///< The timer to determine whether there is no mouse wheel event received for a certain period of time.
591 Dali::Gesture::State mGestureState;
593 Actor mOvershootOverlay; ///< The overlay actor for overshoot effect
595 Dali::Toolkit::ScrollConnector mScrollConnector; ///< Connects ItemView with scrollable components e.g. scroll bars
596 Constrainable mScrollPositionObject; ///< From mScrollConnector
600 Property::Index mPropertyPosition; ///< The physical position of the first item within the layout
601 Property::Index mPropertyMinimumLayoutPosition; ///< The minimum valid layout position in the layout.
602 Property::Index mPropertyScrollSpeed; ///< The current scroll speed of item view
604 bool mRefreshEnabled; ///< Whether to refresh the cache automatically
606 Vector3 mItemsParentOrigin;
607 Vector3 mItemsAnchorPoint;
610 } // namespace Internal
612 // Helpers for public-api forwarding methods
614 inline Toolkit::Internal::ItemView& GetImpl(Toolkit::ItemView& itemView)
616 DALI_ASSERT_ALWAYS(itemView);
618 Dali::RefObject& handle = itemView.GetImplementation();
620 return static_cast<Toolkit::Internal::ItemView&>(handle);
623 inline const Toolkit::Internal::ItemView& GetImpl(const Toolkit::ItemView& itemView)
625 DALI_ASSERT_ALWAYS(itemView);
627 const Dali::RefObject& handle = itemView.GetImplementation();
629 return static_cast<const Toolkit::Internal::ItemView&>(handle);
632 } // namespace Toolkit
636 #endif // __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__