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/public-api/controls/scrollable/scroll-connector.h>
28 #include <dali-toolkit/internal/controls/scrollable/scrollable-impl.h>
29 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
30 #include <dali-toolkit/internal/controls/scrollable/scroll-view/scroll-overshoot-indicator-impl.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(unsigned int intervalMilliseconds);
177 * @copydoc Toolkit::ItemView::GetRefreshInterval
179 unsigned int 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);
249 * Remove an Actor if found in the ItemPool.
250 * @param[in] itemId The item to remove.
251 * @return True if an actor was removed.
253 bool RemoveActor( unsigned int itemId );
256 * Remove any Actors outside a given range.
257 * @param[in] @param[in] range The range of required items.
259 void RemoveActorsOutsideRange( ItemRange range );
262 * Add a range of Actors, if they are not already in the ItemPool.
263 * @param[in] range The range of Item IDs to associate with the new actors.
264 * @param[in] durationSeconds The time taken to fully constrain the newly added actor.
266 void AddActorsWithinRange( ItemRange range, float durationSeconds );
269 * Add a new Actor, if not already in the ItemPool.
270 * @param[in] item The ID for the new item.
271 * @param[in] durationSeconds The time taken to fully constrain the new actor.
273 void AddNewActor( ItemId item, float durationSeconds );
276 * Apply the constraints etc. that are required for ItemView children.
277 * @param[in] item The item to setup.
278 * @param[in] durationSeconds The time taken to fully constrain the actor.
280 void SetupActor( Item item, float durationSeconds );
282 private: // From CustomActorImpl
285 * From CustomActorImpl; called after a touch-signal is received by the owning actor.
286 * @param[in] event The touch event.
287 * @return True if the event should be consumed.
289 virtual bool OnTouchEvent(const TouchEvent& event);
292 * From CustomActorImpl; called after a mouse-wheel-event is received by the owning actor.
293 * @param[in] event The mouse wheel event.
294 * @return True if the event should be consumed.
296 virtual bool OnMouseWheelEvent(const MouseWheelEvent& event);
298 private: // From ControlImpl
301 * @copydoc Toolkit::Control::OnInitialize()
303 virtual void OnInitialize();
306 * @copydoc Toolkit::Control::OnAccessibilityPan()
308 virtual bool OnAccessibilityPan(PanGesture gesture);
311 * @copydoc Toolkit::Control::GetNextKeyboardFocusableActor()
313 virtual Actor GetNextKeyboardFocusableActor(Actor actor, Control::KeyboardFocusNavigationDirection direction, bool loopEnabled);
316 * @copydoc Toolkit::Control::OnKeyboardFocusChangeCommitted()
318 virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor);
323 * Construct a new ItemView.
324 * @param[in] factory The factory which provides ItemView with items.
326 ItemView(ItemFactory& factory);
329 * A reference counted object may only be deleted by calling Unreference()
336 ItemView(const ItemView&);
339 ItemView& operator=(const ItemView& rhs);
342 * Helper to apply constraints to an actor.
343 * @param[in] actor The actor to constrain.
344 * @param[in] layout The active layout.
345 * @param[in] itemId The ID of the item represented by the actor.
346 * @param[in] durationSeconds The time taken to fully constrain the actors.
348 void ApplyConstraints(Actor& actor, ItemLayout& layout, unsigned int itemId, float durationSeconds);
351 * Helper to re-apply all the constraints after items have been inserted, removed etc.
352 * @param[in] durationSeconds The time taken to fully constrain the actors.
354 void ReapplyAllConstraints( float durationSeconds );
357 * Helper to remove items outside a given range.
358 * @param[in] range The range of required items.
360 void RemoveItems(ItemRange range);
363 * Helper to add a range of items, if not already in the ItemPool.
364 * @param[in] layout The layout used to position the new items.
365 * @param[in] layoutSize The current size of the layout.
366 * @param[in] range The range of required items.
368 void AddItems(ItemLayout& layout, const Vector3& layoutSize, ItemRange range);
371 * Helper to find the range of items to populate with.
372 * @param[in] layout The current layout.
373 * @param[in] range The range of items.
374 * @param[in] reserveExtra True if reserve items should be included.
376 ItemRange GetItemRange(ItemLayout& layout, const Vector3& layoutSize, bool reserveExtra);
381 * Helper to handle pressed (Down) events.
382 * @param[in] x The X coordinate of the touch event.
383 * @param[in] y The Y coordinate of the touch event.
384 * @param[in] timeMs The time-stamp of the touch event.
386 void OnPressed(float x, float y, unsigned long timeMs);
389 * Helper to clamp the first-item position when dragging/swiping.
390 * @param[in] targetPosition The target position of the drag etc.
391 * @param[in] targetSize The target ItemView & layout size.
392 * @param[in] layout The current layout.
393 * @return The clamped first-item position.
395 float ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout);
398 * Called upon pan gesture event.
400 * @param[in] gesture The gesture event.
402 void OnPan(PanGesture pan);
405 * Helper to handle anchoring animations.
406 * @return The animation, or an uninitialized handle if not necessary.
408 Animation DoAnchoring();
411 * Callback from scroll animations
412 * @param[in] animation The scroll-animation which has finished.
414 void OnScrollFinished(Animation& animation);
417 * Called by animation system when overshoot has finished animating to maximum (either -1.0f or 1.0f)
419 * @param[in] animation the animation that has finished
421 void OnOvershootOnFinished(Animation& animation);
424 * Helper to start the refresh timer.
426 void StartRefreshTimer();
429 * Helper to cancel the refresh timer.
431 void CancelRefreshTimer();
434 * Refresh the ItemView; this is called after a timeout when scrolling.
435 * During a refresh, ItemFactory::NewItem() will be called to create newly visible items.
436 * @return True if the refresh timer should be kept running.
438 bool OnRefreshTick();
441 * This is called after a timeout when no new mouse wheel event is received for a certain period of time.
442 * @return will return false; one-shot timer.
444 bool OnMouseWheelEventFinished();
447 * Stops and removes animation if exists.
448 * @param[in,out] animation The animation handle to be removed.
450 void RemoveAnimation(Animation& animation);
453 * Helper to apply constraints to the overshoot overlay actor.
455 void ApplyOvershootOverlayConstraints();
458 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
459 * @return The scroll overshoot.
461 float CalculateScrollOvershoot();
464 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
466 * @param[in] overshootAmount amount to animate to
467 * @param[in] animateBack whether to animate back to zero immediately after
468 * @return The scroll overshoot.
470 void AnimateScrollOvershoot(float overshootAmount, bool animateBack = false);
473 * Gets the scroll position in pixels according to the logical layout position.
474 * @param[in] layoutSize The current size of the layout.
476 float GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const;
479 * Calculates the minimum and maximum positions for each axis to scroll to.
480 * @param[in] layoutSize The current size of the layout.
482 void CalculateDomainSize(const Vector3& layoutSize);
485 * Calculates whether we want to allow current item view to scroll.
486 * @param[in] layoutSize The current size of the layout.
487 * @return true if itemview is scrollable
489 bool IsLayoutScrollable(const Vector3& layoutSize);
493 ItemFactory& mItemFactory;
495 typedef std::map<unsigned int, Actor> ItemPool;
496 typedef ItemPool::iterator ItemPoolIter;
497 typedef ItemPool::const_iterator ConstItemPoolIter;
501 ItemLayoutContainer mLayouts;
502 ItemLayout* mActiveLayout;
503 Vector3 mActiveLayoutTargetSize;
505 AlphaFunction mDefaultAlphaFunction;
507 Animation mResizeAnimation;
508 Animation mScrollAnimation;
509 Animation mScrollSpeedAnimation;
510 Animation mScrollOvershootAnimation;
511 bool mAnimatingOvershootOn; ///< whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
512 bool mAnimateOvershootOff; ///< whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
514 bool mAnchoringEnabled;
515 float mAnchoringDuration;
518 int mRefreshIntervalMilliseconds;
519 bool mRefreshOrderHint; ///< True if scrolling towards the last item
523 float mMinimumSwipeSpeed;
524 float mMinimumSwipeDistance;
525 float mMouseWheelScrollDistanceStep; ///< The step of scroll distance in actor coordinates for each mouse wheel event received.
527 float mScrollDistance;
529 Vector2 mTotalPanDisplacement;
531 float mScrollOvershoot;
534 Timer mMouseWheelEventFinishedTimer; ///< The timer to determine whether there is no mouse wheel event received for a certain period of time.
536 Dali::Gesture::State mGestureState;
538 ImageActor mOvershootOverlay; ///< The overlay actor for overshoot effect
539 OvershootRippleEffect mOvershootEffect; ///< The vertex/fragment shader used to display the overshoot ripple effect
541 Dali::Toolkit::ScrollConnector mScrollConnector; ///< Connects ItemView with scrollable components e.g. scroll bars
542 Constrainable mScrollPositionObject; ///< From mScrollConnector
544 Property::Index mPropertyPosition; ///< The physical position of the first item within the layout
545 Property::Index mPropertyMinimumLayoutPosition; ///< The minimum valid layout position in the layout.
546 Property::Index mPropertyScrollSpeed; ///< The current scroll speed of item view
547 Property::Index mPropertyOvershoot; ///< The scroll overshoot (difference of the layout position before and after clamping)
550 } // namespace Internal
552 // Helpers for public-api forwarding methods
554 inline Toolkit::Internal::ItemView& GetImpl(Toolkit::ItemView& itemView)
556 DALI_ASSERT_ALWAYS(itemView);
558 Dali::RefObject& handle = itemView.GetImplementation();
560 return static_cast<Toolkit::Internal::ItemView&>(handle);
563 inline const Toolkit::Internal::ItemView& GetImpl(const Toolkit::ItemView& itemView)
565 DALI_ASSERT_ALWAYS(itemView);
567 const Dali::RefObject& handle = itemView.GetImplementation();
569 return static_cast<const Toolkit::Internal::ItemView&>(handle);
572 } // namespace Toolkit
576 #endif // __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__