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::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 Vector3 GetDomainSize() const;
224 * @copydoc Toolkit::Scrollable::GetCurrentScrollPosition
226 Vector3 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 Vector3& position, float duration)
241 void ScrollTo(const Vector3& 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);
295 * Remove an Actor if found in the ItemPool.
296 * @param[in] itemId The item to remove.
297 * @return True if the remaining actors were reordered.
299 bool RemoveActor( unsigned int itemId );
302 * Remove any Actors outside a given range.
303 * @param[in] @param[in] range The range of required items.
305 void RemoveActorsOutsideRange( ItemRange range );
308 * Add a range of Actors, if they are not already in the ItemPool.
309 * @param[in] range The range of Item IDs to associate with the new actors.
310 * @param[in] durationSeconds The time taken to fully constrain the newly added actor.
312 void AddActorsWithinRange( ItemRange range, float durationSeconds );
315 * Add a new Actor, if not already in the ItemPool.
316 * @param[in] item The ID for the new item.
317 * @param[in] durationSeconds The time taken to fully constrain the new actor.
319 void AddNewActor( ItemId item, float durationSeconds );
322 * Apply the constraints etc. that are required for ItemView children.
323 * @param[in] item The item to setup.
324 * @param[in] durationSeconds The time taken to fully constrain the actor.
326 void SetupActor( Item item, float durationSeconds );
329 * Remove the Actor from the ItemPool and notify the ItemFactory the actor has been released by ItemView.
330 * @param[in] item The ID for the item to be released.
331 * @param[in] actor The actor to be removed from ItemView.
333 void ReleaseActor( ItemId item, Actor actor );
335 private: // From CustomActorImpl
338 * From CustomActorImpl; called after a child has been added to the owning actor.
339 * @param[in] child The child which has been added.
341 virtual void OnChildAdd(Actor& child);
344 * From CustomActorImpl; called after a touch-signal is received by the owning actor.
345 * @param[in] event The touch event.
346 * @return True if the event should be consumed.
348 virtual bool OnTouchEvent(const TouchEvent& event);
351 * From CustomActorImpl; called after a mouse-wheel-event is received by the owning actor.
352 * @param[in] event The mouse wheel event.
353 * @return True if the event should be consumed.
355 virtual bool OnMouseWheelEvent(const MouseWheelEvent& event);
357 private: // From Control
360 * @copydoc Toolkit::Control::OnInitialize()
362 virtual void OnInitialize();
365 * @copydoc Toolkit::Control::OnAccessibilityPan()
367 virtual bool OnAccessibilityPan(PanGesture gesture);
370 * @copydoc Toolkit::Control::GetNextKeyboardFocusableActor()
372 virtual Actor GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled);
375 * @copydoc Toolkit::Control::OnKeyboardFocusChangeCommitted()
377 virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor);
382 * Construct a new ItemView.
383 * @param[in] factory The factory which provides ItemView with items.
385 ItemView(ItemFactory& factory);
388 * A reference counted object may only be deleted by calling Unreference()
395 ItemView(const ItemView&);
398 ItemView& operator=(const ItemView& rhs);
401 * Helper to re-apply all the constraints after items have been inserted, removed etc.
402 * @param[in] durationSeconds The time taken to fully constrain the actors.
404 void ReapplyAllConstraints( float durationSeconds );
407 * Helper to relayout after item(s) are removed.
409 void OnItemsRemoved();
412 * Helper to remove items outside a given range.
413 * @param[in] range The range of required items.
415 void RemoveItems(ItemRange range);
418 * Helper to add a range of items, if not already in the ItemPool.
419 * @param[in] layout The layout used to position the new items.
420 * @param[in] layoutSize The current size of the layout.
421 * @param[in] range The range of required items.
423 void AddItems(ItemLayout& layout, const Vector3& layoutSize, ItemRange range);
426 * Helper to find the range of items to populate with.
427 * @param[in] layout The current layout.
428 * @param[in] range The range of items.
429 * @param[in] reserveExtra True if reserve items should be included.
431 ItemRange GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra);
436 * Helper to handle pressed (Down) events.
437 * @param[in] x The X coordinate of the touch event.
438 * @param[in] y The Y coordinate of the touch event.
439 * @param[in] timeMs The time-stamp of the touch event.
441 void OnPressed(float x, float y, unsigned long timeMs);
444 * Helper to clamp the first-item position when dragging/swiping.
445 * @param[in] targetPosition The target position of the drag etc.
446 * @param[in] targetSize The target ItemView & layout size.
447 * @param[in] layout The current layout.
448 * @return The clamped first-item position.
450 float ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout);
453 * Called upon pan gesture event.
455 * @param[in] gesture The gesture event.
457 void OnPan( const PanGesture& pan );
460 * Helper to handle anchoring animations.
461 * @return The animation, or an uninitialized handle if not necessary.
463 Animation DoAnchoring();
466 * Callback from scroll animations
467 * @param[in] animation The scroll-animation which has finished.
469 void OnScrollFinished(Animation& animation);
472 * Callback from layout activation scroll animations
473 * @param[in] animation The scroll-animation which has finished.
475 void OnLayoutActivationScrollFinished(Animation& animation);
478 * Called by animation system when overshoot has finished animating to maximum (either -1.0f or 1.0f)
480 * @param[in] animation the animation that has finished
482 void OnOvershootOnFinished(Animation& animation);
485 * This is called after a timeout when no new mouse wheel event is received for a certain period of time.
486 * @return will return false; one-shot timer.
488 bool OnMouseWheelEventFinished();
491 * Stops and removes animation if exists.
492 * @param[in,out] animation The animation handle to be removed.
494 void RemoveAnimation(Animation& animation);
497 * @copydoc Toolkit::Internal::Scrollable::SetOvershootEnabled
499 virtual void SetOvershootEnabled( bool enable );
502 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
503 * @return The scroll overshoot.
505 float CalculateScrollOvershoot();
508 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
510 * @param[in] overshootAmount amount to animate to
511 * @param[in] animateBack whether to animate back to zero immediately after
512 * @return The scroll overshoot.
514 void AnimateScrollOvershoot(float overshootAmount, bool animateBack = false);
517 * Gets the scroll position in pixels according to the logical layout position.
518 * @param[in] layoutSize The current size of the layout.
520 float GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const;
523 * Calculates the minimum and maximum positions for each axis to scroll to.
524 * @param[in] layoutSize The current size of the layout.
526 void CalculateDomainSize(const Vector3& layoutSize);
529 * Calculates whether we want to allow current item view to scroll.
530 * @param[in] layoutSize The current size of the layout.
531 * @return true if itemview is scrollable
533 bool IsLayoutScrollable(const Vector3& layoutSize);
536 * Callback when the current layout position of ItemView changes in both positive
537 * and negative directions by the specified amount. Refresh the ItemView to create
538 * newly visible items.
539 * @param[in] source the property notification that triggered this callback
541 void OnRefreshNotification(PropertyNotification& source);
544 * This is called when scroll position has been changed by ScrollConnector::SetScrollPosition.
545 * @param[in] position The new scroll position
547 void OnScrollPositionChanged( float position );
551 ItemFactory& mItemFactory;
553 typedef std::map<unsigned int, Actor> ItemPool;
554 typedef ItemPool::iterator ItemPoolIter;
555 typedef ItemPool::const_iterator ConstItemPoolIter;
559 ItemLayoutContainer mLayouts;
560 ItemLayout* mActiveLayout;
561 Vector3 mActiveLayoutTargetSize;
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 Handle 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__