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>
31 #include <dali-toolkit/public-api/shader-effects/bouncing-effect.h>
44 typedef IntrusivePtr<ItemView> ItemViewPtr;
47 * ItemView is a scrollable layout container.
48 * Multiple ItemLayouts may be provided, to determine the logical position of each item a layout.
49 * Actor-ID pairs are provided from a shared ItemFactory, to display the currently visible items.
51 class ItemView : public Scrollable
56 * Create a new ItemView.
57 * @param[in] factory The factory which provides ItemView with items.
58 * @return A public handle to the newly allocated ItemView.
60 static Dali::Toolkit::ItemView New(ItemFactory& factory);
63 * @copydoc Toolkit::ItemView::GetScrollConnector
65 Dali::Toolkit::ScrollConnector GetScrollConnector() const;
68 * @copydoc Toolkit::ItemView::GetLayoutCount
70 unsigned int GetLayoutCount() const;
73 * @copydoc Toolkit::ItemView::AddLayout
75 void AddLayout(ItemLayout& layout);
78 * @copydoc Toolkit::ItemView::RemoveLayout
80 void RemoveLayout(unsigned int layoutIndex);
83 * @copydoc Toolkit::ItemView::GetLayout
85 ItemLayoutPtr GetLayout(unsigned int layoutIndex) const;
88 * @copydoc Toolkit::ItemView::GetActiveLayout
90 ItemLayoutPtr GetActiveLayout() const;
93 * @copydoc Toolkit::ItemView::GetCurrentLayoutPosition
95 float GetCurrentLayoutPosition(unsigned int itemId) const;
98 * @copydoc Toolkit::ItemView::ActivateLayout
100 void ActivateLayout(unsigned int layoutIndex, const Vector3& targetSize, float durationSeconds);
103 * @copydoc Toolkit::ItemView::SetDefaultAlphaFunction
105 void SetDefaultAlphaFunction(AlphaFunction func);
108 * @copydoc Toolkit::ItemView::GetDefaultAlphaFunction
110 AlphaFunction GetDefaultAlphaFunction() const;
113 * @copydoc Toolkit::ItemView::DeactivateCurrentLayout
115 void DeactivateCurrentLayout();
118 * @copydoc Toolkit::ItemView::SetMinimumSwipeSpeed
120 void SetMinimumSwipeSpeed(float speed);
123 * @copydoc Toolkit::ItemView::GetMinimumSwipeSpeed
125 float GetMinimumSwipeSpeed() const;
128 * @copydoc Toolkit::ItemView::SetMinimumSwipeDistance
130 void SetMinimumSwipeDistance(float distance);
133 * @copydoc Toolkit::ItemView::GetMinimumSwipeDistance
135 float GetMinimumSwipeDistance() const;
138 * @copydoc Toolkit::ItemView::SetMouseWheelScrollDistanceStep
140 void SetMouseWheelScrollDistanceStep(float step);
143 * @copydoc Toolkit::ItemView::GetMouseWheelScrollDistanceStep
145 float GetMouseWheelScrollDistanceStep() const;
148 * @copydoc Toolkit::ItemView::SetAnchoring
150 void SetAnchoring(bool enabled);
153 * @copydoc Toolkit::ItemView::GetAnchoring
155 bool GetAnchoring() const;
158 * @copydoc Toolkit::ItemView::SetAnchoringDuration
160 void SetAnchoringDuration(float durationSeconds);
163 * @copydoc Toolkit::ItemView::GetAnchoringDuration
165 float GetAnchoringDuration() const;
168 * @copydoc Toolkit::ItemView::ScrollToItem
170 void ScrollToItem(unsigned int itemId, float durationSeconds);
173 * @copydoc Toolkit::ItemView::SetRefreshInterval
175 void SetRefreshInterval(float intervalLayoutPositions);
178 * @copydoc Toolkit::ItemView::GetRefreshInterval
180 float GetRefreshInterval() const;
183 * @copydoc Toolkit::ItemView::GetItem
185 Actor GetItem(unsigned int itemId) const;
188 * @copydoc Toolkit::ItemView::GetItemId
190 unsigned int GetItemId(Actor actor) const;
193 * @copydoc Toolkit::ItemView::InsertItem
195 void InsertItem(Item newItem, float durationSeconds);
198 * @copydoc Toolkit::ItemView::InsertItem
200 void InsertItems(const ItemContainer& newItems, float durationSeconds);
203 * @copydoc Toolkit::ItemView::RemoveItem
205 void RemoveItem(ItemId itemId, float durationSeconds);
208 * @copydoc Toolkit::ItemView::InsertItem
210 void RemoveItems(const ItemIdContainer& itemIds, float durationSeconds);
213 * @copydoc Toolkit::ItemView::InsertItem
215 void ReplaceItem(Item replacementItem, float durationSeconds);
218 * @copydoc Toolkit::ItemView::InsertItem
220 void ReplaceItems(const ItemContainer& replacementItems, float durationSeconds);
223 * @copydoc Toolkit::Scrollable::GetDomainSize
225 Vector3 GetDomainSize() const;
228 * @copydoc Toolkit::Scrollable::GetCurrentScrollPosition
230 Vector3 GetCurrentScrollPosition() const;
233 * @copydoc Toolkit::Scrollable::AddOverlay()
235 void AddOverlay(Actor actor);
238 * @copydoc Toolkit::Scrollable::RemoveOverlay()
240 void RemoveOverlay(Actor actor);
243 * @copydoc Toolkit::Scrollable::ScrollTo(const Vector3& position, float duration)
245 void ScrollTo(const Vector3& position, float duration);
248 * @brief Set whether to enable automatic refresh or not. When refresh is disabled,
249 * ItemView will not automatically refresh the cache in the given interval when the
250 * layout position is changed. This is useful in some cases, for example, automatic
251 * refresh is not needed during fast scrolling, otherwise it will cache unneeded
252 * items when the layout position changes quickly.
254 * @param[in] enabled True to enable automatic refresh or false to disable it.
256 void SetRefreshEnabled(bool enabled);
259 * @brief Helper to perform the refresh.
261 * @param[in] currentLayoutPosition The current layout position.
262 * @param[in] cacheExtra Whether to cache extra items during refresh.
264 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 an actor was removed.
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 );
328 private: // From CustomActorImpl
331 * From CustomActorImpl; called after a child has been added to the owning actor.
332 * @param[in] child The child which has been added.
334 virtual void OnChildAdd(Actor& child);
337 * From CustomActorImpl; called after a touch-signal is received by the owning actor.
338 * @param[in] event The touch event.
339 * @return True if the event should be consumed.
341 virtual bool OnTouchEvent(const TouchEvent& event);
344 * From CustomActorImpl; called after a mouse-wheel-event is received by the owning actor.
345 * @param[in] event The mouse wheel event.
346 * @return True if the event should be consumed.
348 virtual bool OnMouseWheelEvent(const MouseWheelEvent& event);
350 private: // From Control
353 * @copydoc Toolkit::Control::OnInitialize()
355 virtual void OnInitialize();
358 * @copydoc Toolkit::Control::OnAccessibilityPan()
360 virtual bool OnAccessibilityPan(PanGesture gesture);
363 * @copydoc Toolkit::Control::GetNextKeyboardFocusableActor()
365 virtual Actor GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled);
368 * @copydoc Toolkit::Control::OnKeyboardFocusChangeCommitted()
370 virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor);
375 * Construct a new ItemView.
376 * @param[in] factory The factory which provides ItemView with items.
378 ItemView(ItemFactory& factory);
381 * A reference counted object may only be deleted by calling Unreference()
388 ItemView(const ItemView&);
391 ItemView& operator=(const ItemView& rhs);
394 * Helper to apply constraints to an actor.
395 * @param[in] actor The actor to constrain.
396 * @param[in] layout The active layout.
397 * @param[in] itemId The ID of the item represented by the actor.
398 * @param[in] durationSeconds The time taken to fully constrain the actors.
400 void ApplyConstraints(Actor& actor, ItemLayout& layout, unsigned int itemId, float durationSeconds);
403 * Helper to re-apply all the constraints after items have been inserted, removed etc.
404 * @param[in] durationSeconds The time taken to fully constrain the actors.
406 void ReapplyAllConstraints( float durationSeconds );
409 * Helper to remove items outside a given range.
410 * @param[in] range The range of required items.
412 void RemoveItems(ItemRange range);
415 * Helper to add a range of items, if not already in the ItemPool.
416 * @param[in] layout The layout used to position the new items.
417 * @param[in] layoutSize The current size of the layout.
418 * @param[in] range The range of required items.
420 void AddItems(ItemLayout& layout, const Vector3& layoutSize, ItemRange range);
423 * Helper to find the range of items to populate with.
424 * @param[in] layout The current layout.
425 * @param[in] range The range of items.
426 * @param[in] reserveExtra True if reserve items should be included.
428 ItemRange GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra);
433 * Helper to handle pressed (Down) events.
434 * @param[in] x The X coordinate of the touch event.
435 * @param[in] y The Y coordinate of the touch event.
436 * @param[in] timeMs The time-stamp of the touch event.
438 void OnPressed(float x, float y, unsigned long timeMs);
441 * Helper to clamp the first-item position when dragging/swiping.
442 * @param[in] targetPosition The target position of the drag etc.
443 * @param[in] targetSize The target ItemView & layout size.
444 * @param[in] layout The current layout.
445 * @return The clamped first-item position.
447 float ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout);
450 * Called upon pan gesture event.
452 * @param[in] gesture The gesture event.
454 void OnPan(PanGesture pan);
457 * Helper to handle anchoring animations.
458 * @return The animation, or an uninitialized handle if not necessary.
460 Animation DoAnchoring();
463 * Callback from scroll animations
464 * @param[in] animation The scroll-animation which has finished.
466 void OnScrollFinished(Animation& animation);
469 * Called by animation system when overshoot has finished animating to maximum (either -1.0f or 1.0f)
471 * @param[in] animation the animation that has finished
473 void OnOvershootOnFinished(Animation& animation);
476 * This is called after a timeout when no new mouse wheel event is received for a certain period of time.
477 * @return will return false; one-shot timer.
479 bool OnMouseWheelEventFinished();
482 * Stops and removes animation if exists.
483 * @param[in,out] animation The animation handle to be removed.
485 void RemoveAnimation(Animation& animation);
488 * @copydoc Toolkit::Internal::Scrollable::SetOvershootEnabled
490 virtual void SetOvershootEnabled( bool enable );
493 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
494 * @return The scroll overshoot.
496 float CalculateScrollOvershoot();
499 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
501 * @param[in] overshootAmount amount to animate to
502 * @param[in] animateBack whether to animate back to zero immediately after
503 * @return The scroll overshoot.
505 void AnimateScrollOvershoot(float overshootAmount, bool animateBack = false);
508 * Gets the scroll position in pixels according to the logical layout position.
509 * @param[in] layoutSize The current size of the layout.
511 float GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const;
514 * Calculates the minimum and maximum positions for each axis to scroll to.
515 * @param[in] layoutSize The current size of the layout.
517 void CalculateDomainSize(const Vector3& layoutSize);
520 * Calculates whether we want to allow current item view to scroll.
521 * @param[in] layoutSize The current size of the layout.
522 * @return true if itemview is scrollable
524 bool IsLayoutScrollable(const Vector3& layoutSize);
527 * Callback when the current layout position of ItemView changes in both positive
528 * and negative directions by the specified amount. Refresh the ItemView to create
529 * newly visible items.
530 * @param[in] source the property notification that triggered this callback
532 void OnRefreshNotification(PropertyNotification& source);
536 ItemFactory& mItemFactory;
538 typedef std::map<unsigned int, Actor> ItemPool;
539 typedef ItemPool::iterator ItemPoolIter;
540 typedef ItemPool::const_iterator ConstItemPoolIter;
544 ItemLayoutContainer mLayouts;
545 ItemLayout* mActiveLayout;
546 Vector3 mActiveLayoutTargetSize;
548 AlphaFunction mDefaultAlphaFunction;
550 Animation mResizeAnimation;
551 Animation mScrollAnimation;
552 Animation mScrollOvershootAnimation;
553 bool mAnimatingOvershootOn; ///< whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
554 bool mAnimateOvershootOff; ///< whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
556 bool mAnchoringEnabled;
557 float mAnchoringDuration;
559 float mRefreshIntervalLayoutPositions; ///< Refresh item view when the layout position changes by this interval in both positive and negative directions.
560 PropertyNotification mRefreshNotification; // stores the property notification used for item view refresh
561 bool mRefreshOrderHint; ///< True if scrolling towards the last item
565 float mMinimumSwipeSpeed;
566 float mMinimumSwipeDistance;
567 float mMouseWheelScrollDistanceStep; ///< The step of scroll distance in actor coordinates for each mouse wheel event received.
569 float mScrollDistance;
571 Vector2 mTotalPanDisplacement;
573 float mScrollOvershoot;
576 Timer mMouseWheelEventFinishedTimer; ///< The timer to determine whether there is no mouse wheel event received for a certain period of time.
578 Dali::Gesture::State mGestureState;
580 ImageActor mOvershootOverlay; ///< The overlay actor for overshoot effect
581 BouncingEffect mOvershootEffect; ///< The vertex/fragment shader used to display the overshoot ripple effect
583 Dali::Toolkit::ScrollConnector mScrollConnector; ///< Connects ItemView with scrollable components e.g. scroll bars
584 Constrainable mScrollPositionObject; ///< From mScrollConnector
588 Property::Index mPropertyPosition; ///< The physical position of the first item within the layout
589 Property::Index mPropertyMinimumLayoutPosition; ///< The minimum valid layout position in the layout.
590 Property::Index mPropertyScrollSpeed; ///< The current scroll speed of item view
592 bool mRefreshEnabled; ///< Whether to refresh the cache automatically
594 Vector3 mItemsParentOrigin;
595 Vector3 mItemsAnchorPoint;
598 } // namespace Internal
600 // Helpers for public-api forwarding methods
602 inline Toolkit::Internal::ItemView& GetImpl(Toolkit::ItemView& itemView)
604 DALI_ASSERT_ALWAYS(itemView);
606 Dali::RefObject& handle = itemView.GetImplementation();
608 return static_cast<Toolkit::Internal::ItemView&>(handle);
611 inline const Toolkit::Internal::ItemView& GetImpl(const Toolkit::ItemView& itemView)
613 DALI_ASSERT_ALWAYS(itemView);
615 const Dali::RefObject& handle = itemView.GetImplementation();
617 return static_cast<const Toolkit::Internal::ItemView&>(handle);
620 } // namespace Toolkit
624 #endif // __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__