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>
25 #include <dali/devel-api/common/map-wrapper.h>
28 #include <dali-toolkit/public-api/controls/control-impl.h>
29 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-view.h>
30 #include <dali-toolkit/public-api/controls/scrollable/item-view/item-layout.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 typedef Toolkit::ItemView::LayoutActivatedSignalType LayoutActivatedSignalType;
62 * Create a new ItemView.
63 * @param[in] factory The factory which provides ItemView with items.
64 * @return A public handle to the newly allocated ItemView.
66 static Dali::Toolkit::ItemView New(ItemFactory& factory);
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::SetWheelScrollDistanceStep
131 void SetWheelScrollDistanceStep(float step);
134 * @copydoc Toolkit::ItemView::GetWheelScrollDistanceStep
136 float GetWheelScrollDistanceStep() 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 Vector2 GetDomainSize() const;
224 * @copydoc Toolkit::Scrollable::GetCurrentScrollPosition
226 Vector2 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 Vector2& position, float duration)
241 void ScrollTo(const Vector2& 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);
293 * @copydoc Toolkit::ItemView::LayoutActivatedSignal()
295 LayoutActivatedSignalType& LayoutActivatedSignal()
297 return mLayoutActivatedSignal;
301 * Connects a callback function with the object's signals.
302 * @param[in] object The object providing the signal.
303 * @param[in] tracker Used to disconnect the signal.
304 * @param[in] signalName The signal to connect to.
305 * @param[in] functor A newly allocated FunctorDelegate.
306 * @return True if the signal was connected.
307 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
309 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
314 * Called when a property of an object of this type is set.
315 * @param[in] object The object whose property is set.
316 * @param[in] index The property index.
317 * @param[in] value The new property value.
319 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
322 * Called to retrieve a property of an object of this type.
323 * @param[in] object The object whose property is to be retrieved.
324 * @param[in] index The property index.
325 * @return The current value of the property.
327 static Property::Value GetProperty( BaseObject* object, Property::Index index );
332 * Remove an Actor if found in the ItemPool.
333 * @param[in] itemId The item to remove.
334 * @return True if the remaining actors were reordered.
336 bool RemoveActor( unsigned int itemId );
339 * Remove any Actors outside a given range.
340 * @param[in] @param[in] range The range of required items.
342 void RemoveActorsOutsideRange( ItemRange range );
345 * Add a range of Actors, if they are not already in the ItemPool.
346 * @param[in] range The range of Item IDs to associate with the new actors.
347 * @param[in] layoutSize The layout-size.
349 void AddActorsWithinRange( ItemRange range, const Vector3& layoutSize );
352 * Add a new Actor, if not already in the ItemPool.
353 * @param[in] item The ID for the new item.
354 * @param[in] layoutSize The layout-size.
356 void AddNewActor( ItemId item, const Vector3& layoutSize );
359 * Apply the constraints etc. that are required for ItemView children.
360 * @param[in] item The item to setup.
361 * @param[in] layoutSize The layout-size.
363 void SetupActor( Item item, const Vector3& layoutSize );
366 * Remove the Actor from the ItemPool and notify the ItemFactory the actor has been released by ItemView.
367 * @param[in] item The ID for the item to be released.
368 * @param[in] actor The actor to be removed from ItemView.
370 void ReleaseActor( ItemId item, Actor actor );
372 private: // From CustomActorImpl
375 * From CustomActorImpl; called after a child has been added to the owning actor.
376 * @param[in] child The child which has been added.
378 virtual void OnChildAdd(Actor& child);
381 * From CustomActorImpl; called after a touch-signal is received by the owning actor.
382 * @param[in] event The touch event.
383 * @return True if the event should be consumed.
385 virtual bool OnTouchEvent(const TouchEvent& event);
388 * From CustomActorImpl; called after a wheel-event is received by the owning actor.
389 * @param[in] event The wheel event.
390 * @return True if the event should be consumed.
392 virtual bool OnWheelEvent(const WheelEvent& event);
394 private: // From Control
397 * @copydoc Toolkit::Control::OnInitialize()
399 virtual void OnInitialize();
402 * @copydoc Toolkit::Control::OnAccessibilityPan()
404 virtual bool OnAccessibilityPan(PanGesture gesture);
407 * @copydoc Toolkit::Control::GetNextKeyboardFocusableActor()
409 virtual Actor GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled);
412 * @copydoc Toolkit::Control::OnKeyboardFocusChangeCommitted()
414 virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor);
419 * Construct a new ItemView.
420 * @param[in] factory The factory which provides ItemView with items.
422 ItemView(ItemFactory& factory);
425 * A reference counted object may only be deleted by calling Unreference()
432 ItemView(const ItemView&);
435 ItemView& operator=(const ItemView& rhs);
438 * Helper to re-apply all the constraints after items have been inserted, removed etc.
439 * @param[in] durationSeconds The time taken to fully constrain the actors.
441 void ReapplyAllConstraints();
444 * Helper to relayout after item(s) are removed.
446 void OnItemsRemoved();
449 * Helper to remove items outside a given range.
450 * @param[in] range The range of required items.
452 void RemoveItems(ItemRange range);
455 * Helper to add a range of items, if not already in the ItemPool.
456 * @param[in] layout The layout used to position the new items.
457 * @param[in] layoutSize The current size of the layout.
458 * @param[in] range The range of required items.
460 void AddItems(ItemLayout& layout, const Vector3& layoutSize, ItemRange range);
463 * Helper to find the range of items to populate with.
464 * @param[in] layout The current layout.
465 * @param[in] range The range of items.
466 * @param[in] reserveExtra True if reserve items should be included.
468 ItemRange GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra);
473 * Helper to handle pressed (Down) events.
474 * @param[in] x The X coordinate of the touch event.
475 * @param[in] y The Y coordinate of the touch event.
476 * @param[in] timeMs The time-stamp of the touch event.
478 void OnPressed(float x, float y, unsigned long timeMs);
481 * Helper to clamp the first-item position when dragging/swiping.
482 * @param[in] targetPosition The target position of the drag etc.
483 * @param[in] targetSize The target ItemView & layout size.
484 * @param[in] layout The current layout.
485 * @param[in] updateOvershoot False stops the current overshoot value from being clamped also.
486 * @return The clamped first-item position.
488 float ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout, bool updateOvershoot = true);
491 * Called upon pan gesture event.
493 * @param[in] gesture The gesture event.
495 void OnPan( const PanGesture& pan );
498 * Helper to handle anchoring animations.
499 * @return The animation, or an uninitialized handle if not necessary.
501 Animation DoAnchoring();
504 * Callback from scroll animations
505 * @param[in] animation The scroll-animation which has finished.
507 void OnScrollFinished(Animation& animation);
510 * Callback from layout activation scroll animations
511 * @param[in] animation The scroll-animation which has finished.
513 void OnLayoutActivationScrollFinished(Animation& animation);
516 * Called by animation system when overshoot has finished animating to maximum (either -1.0f or 1.0f)
518 * @param[in] animation the animation that has finished
520 void OnOvershootOnFinished(Animation& animation);
523 * This is called after a timeout when no new wheel event is received for a certain period of time.
524 * @return will return false; one-shot timer.
526 bool OnWheelEventFinished();
529 * Stops and removes animation if exists.
530 * @param[in,out] animation The animation handle to be removed.
532 void RemoveAnimation(Animation& animation);
535 * @copydoc Toolkit::Internal::Scrollable::EnableScrollOvershoot
537 virtual void EnableScrollOvershoot( bool enable );
540 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
541 * @return The scroll overshoot.
543 float CalculateScrollOvershoot();
546 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
548 * @param[in] overshootAmount amount to animate to
549 * @param[in] animateBack whether to animate back to zero immediately after
550 * @return The scroll overshoot.
552 void AnimateScrollOvershoot(float overshootAmount, bool animateBack = false);
555 * Gets the scroll position in pixels according to the logical layout position.
556 * @param[in] layoutSize The current size of the layout.
558 float GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const;
561 * Calculates the minimum and maximum positions for each axis to scroll to.
562 * @param[in] layoutSize The current size of the layout.
564 void CalculateDomainSize(const Vector3& layoutSize);
567 * Calculates whether we want to allow current item view to scroll.
568 * @param[in] layoutSize The current size of the layout.
569 * @return true if itemview is scrollable
571 bool IsLayoutScrollable(const Vector3& layoutSize);
574 * Callback when the current layout position of ItemView changes in both positive
575 * and negative directions by the specified amount. Refresh the ItemView to create
576 * newly visible items.
577 * @param[in] source the property notification that triggered this callback
579 void OnRefreshNotification(PropertyNotification& source);
583 typedef std::map<unsigned int, Actor> ItemPool;
584 typedef ItemPool::iterator ItemPoolIter;
585 typedef ItemPool::const_iterator ConstItemPoolIter;
588 ItemFactory& mItemFactory;
589 std::vector< ItemLayoutPtr > mLayouts; ///< Container of Dali::Toolkit::ItemLayout objects
590 Actor mOvershootOverlay; ///< The overlay actor for overshoot effect
591 Animation mResizeAnimation;
592 Animation mScrollAnimation;
593 Animation mScrollOvershootAnimation;
594 Timer mWheelEventFinishedTimer; ///< The timer to determine whether there is no wheel event received for a certain period of time.
595 PropertyNotification mRefreshNotification; ///< Stores the property notification used for item view refresh
596 LayoutActivatedSignalType mLayoutActivatedSignal;
597 Vector3 mActiveLayoutTargetSize;
598 Vector3 mItemsParentOrigin;
599 Vector3 mItemsAnchorPoint;
600 Vector2 mTotalPanDisplacement;
601 ItemLayout* mActiveLayout;
603 float mAnchoringDuration;
604 float mRefreshIntervalLayoutPositions; ///< Refresh item view when the layout position changes by this interval in both positive and negative directions.
605 float mMinimumSwipeSpeed;
606 float mMinimumSwipeDistance;
607 float mWheelScrollDistanceStep; ///< The step of scroll distance in actor coordinates for each wheel event received.
608 float mScrollDistance;
610 float mScrollOvershoot;
612 Dali::Gesture::State mGestureState : 3;
613 bool mAnimatingOvershootOn : 1; ///< Whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
614 bool mAnimateOvershootOff : 1; ///< Whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
615 bool mAnchoringEnabled : 1;
616 bool mRefreshOrderHint : 1; ///< True if scrolling towards the last item
617 bool mIsFlicking : 1;
618 bool mAddingItems : 1;
619 bool mRefreshEnabled : 1; ///< Whether to refresh the cache automatically
620 bool mInAnimation : 1; ///< Keeps track of whether an animation is controlling the overshoot property.
623 } // namespace Internal
625 // Helpers for public-api forwarding methods
627 inline Toolkit::Internal::ItemView& GetImpl(Toolkit::ItemView& itemView)
629 DALI_ASSERT_ALWAYS(itemView);
631 Dali::RefObject& handle = itemView.GetImplementation();
633 return static_cast<Toolkit::Internal::ItemView&>(handle);
636 inline const Toolkit::Internal::ItemView& GetImpl(const Toolkit::ItemView& itemView)
638 DALI_ASSERT_ALWAYS(itemView);
640 const Dali::RefObject& handle = itemView.GetImplementation();
642 return static_cast<const Toolkit::Internal::ItemView&>(handle);
645 } // namespace Toolkit
649 #endif // __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__