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>
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/internal/controls/scrollable/scroll-view/scroll-overshoot-indicator-impl.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::DeactivateCurrentLayout
105 void DeactivateCurrentLayout();
108 * @copydoc Toolkit::ItemView::SetMinimumSwipeSpeed
110 void SetMinimumSwipeSpeed(float speed);
113 * @copydoc Toolkit::ItemView::GetMinimumSwipeSpeed
115 float GetMinimumSwipeSpeed() const;
118 * @copydoc Toolkit::ItemView::SetMinimumSwipeDistance
120 void SetMinimumSwipeDistance(float distance);
123 * @copydoc Toolkit::ItemView::GetMinimumSwipeDistance
125 float GetMinimumSwipeDistance() const;
128 * @copydoc Toolkit::ItemView::SetMouseWheelScrollDistanceStep
130 void SetMouseWheelScrollDistanceStep(float step);
133 * @copydoc Toolkit::ItemView::GetMouseWheelScrollDistanceStep
135 float GetMouseWheelScrollDistanceStep() const;
138 * @copydoc Toolkit::ItemView::SetAnchoring
140 void SetAnchoring(bool enabled);
143 * @copydoc Toolkit::ItemView::GetAnchoring
145 bool GetAnchoring() const;
148 * @copydoc Toolkit::ItemView::SetAnchoringDuration
150 void SetAnchoringDuration(float durationSeconds);
153 * @copydoc Toolkit::ItemView::GetAnchoringDuration
155 float GetAnchoringDuration() const;
158 * @copydoc Toolkit::ItemView::ScrollToItem
160 void ScrollToItem(unsigned int itemId, float durationSeconds);
163 * @copydoc Toolkit::ItemView::SetRefreshInterval
165 void SetRefreshInterval(unsigned int intervalMilliseconds);
168 * @copydoc Toolkit::ItemView::GetRefreshInterval
170 unsigned int GetRefreshInterval() const;
173 * @copydoc Toolkit::ItemView::GetItem
175 Actor GetItem(unsigned int itemId) const;
178 * @copydoc Toolkit::ItemView::GetItemId
180 unsigned int GetItemId(Actor actor) const;
183 * @copydoc Toolkit::ItemView::InsertItem
185 void InsertItem(Item newItem, float durationSeconds);
188 * @copydoc Toolkit::ItemView::InsertItem
190 void InsertItems(const ItemContainer& newItems, float durationSeconds);
193 * @copydoc Toolkit::ItemView::RemoveItem
195 void RemoveItem(ItemId itemId, float durationSeconds);
198 * @copydoc Toolkit::ItemView::InsertItem
200 void RemoveItems(const ItemIdContainer& itemIds, float durationSeconds);
203 * @copydoc Toolkit::ItemView::InsertItem
205 void ReplaceItem(Item replacementItem, float durationSeconds);
208 * @copydoc Toolkit::ItemView::InsertItem
210 void ReplaceItems(const ItemContainer& replacementItems, float durationSeconds);
213 * @copydoc Toolkit::Scrollable::GetDomainSize
215 Vector3 GetDomainSize() const;
218 * @copydoc Toolkit::Scrollable::GetCurrentScrollPosition
220 Vector3 GetCurrentScrollPosition() const;
223 * @copydoc Toolkit::Scrollable::AddOverlay()
225 void AddOverlay(Actor actor);
228 * @copydoc Toolkit::Scrollable::RemoveOverlay()
230 void RemoveOverlay(Actor actor);
233 * @copydoc Toolkit::Scrollable::ScrollTo(const Vector3& position, float duration)
235 void ScrollTo(const Vector3& position, float duration);
240 * Remove an Actor if found in the ItemPool.
241 * @param[in] itemId The item to remove.
242 * @return True if an actor was removed.
244 bool RemoveActor( unsigned int itemId );
247 * Remove any Actors outside a given range.
248 * @param[in] @param[in] range The range of required items.
250 void RemoveActorsOutsideRange( ItemRange range );
253 * Add a range of Actors, if they are not already in the ItemPool.
254 * @param[in] layout The active layout.
255 * @param[in] range The range of Item IDs to associate with the new actors.
257 void AddActorsWithinRange( ItemRange range );
260 * Add a new Actor, if not already in the ItemPool.
261 * @param[in] item The ID for the new item.
263 void AddNewActor( ItemId item );
266 * Apply the constraints etc. that are required for ItemView children.
267 * @param[in] item The item to setup.
268 * @param[in] durationSeconds The time taken to fully constrain the actor.
270 void SetupActor( Item item, float durationSeconds );
272 private: // From CustomActorImpl
275 * From CustomActorImpl; called after a touch-signal is received by the owning actor.
276 * @param[in] event The touch event.
277 * @return True if the event should be consumed.
279 virtual bool OnTouchEvent(const TouchEvent& event);
282 * From CustomActorImpl; called after a mouse-wheel-event is received by the owning actor.
283 * @param[in] event The mouse wheel event.
284 * @return True if the event should be consumed.
286 virtual bool OnMouseWheelEvent(const MouseWheelEvent& event);
288 private: // From ControlImpl
291 * @copydoc Toolkit::Control::OnInitialize()
293 virtual void OnInitialize();
296 * @copydoc Toolkit::Control::OnAccessibilityPan()
298 virtual bool OnAccessibilityPan(PanGesture gesture);
301 * @copydoc Toolkit::Control::GetNextKeyboardFocusableActor()
303 virtual Actor GetNextKeyboardFocusableActor(Actor actor, Control::KeyboardFocusNavigationDirection direction, bool loopEnabled);
306 * @copydoc Toolkit::Control::OnKeyboardFocusChangeCommitted()
308 virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor);
313 * Construct a new ItemView.
314 * @param[in] factory The factory which provides ItemView with items.
316 ItemView(ItemFactory& factory);
319 * A reference counted object may only be deleted by calling Unreference()
326 ItemView(const ItemView&);
329 ItemView& operator=(const ItemView& rhs);
332 * Helper to apply constraints to an actor.
333 * @param[in] actor The actor to constrain.
334 * @param[in] layout The active layout.
335 * @param[in] itemId The ID of the item represented by the actor.
336 * @param[in] durationSeconds The time taken to fully constrain the actors.
338 void ApplyConstraints(Actor& actor, ItemLayout& layout, unsigned int itemId, float durationSeconds);
341 * Helper to re-apply all the constraints after items have been inserted, removed etc.
342 * @param[in] durationSeconds The time taken to fully constrain the actors.
344 void ReapplyAllConstraints( float durationSeconds );
347 * Helper to remove items outside a given range.
348 * @param[in] range The range of required items.
350 void RemoveItems(ItemRange range);
353 * Helper to add a range of items, if not already in the ItemPool.
354 * @param[in] layout The layout used to position the new items.
355 * @param[in] layoutSize The current size of the layout.
356 * @param[in] range The range of required items.
358 void AddItems(ItemLayout& layout, const Vector3& layoutSize, ItemRange range);
361 * Helper to find the range of items to populate with.
362 * @param[in] layout The current layout.
363 * @param[in] range The range of items.
364 * @param[in] reserveExtra True if reserve items should be included.
366 ItemRange GetItemRange(ItemLayout& layout, const Vector3& layoutSize, bool reserveExtra);
371 * Helper to handle pressed (Down) events.
372 * @param[in] x The X coordinate of the touch event.
373 * @param[in] y The Y coordinate of the touch event.
374 * @param[in] timeMs The time-stamp of the touch event.
376 void OnPressed(float x, float y, unsigned long timeMs);
379 * Helper to clamp the first-item position when dragging/swiping.
380 * @param[in] targetPosition The target position of the drag etc.
381 * @param[in] targetSize The target ItemView & layout size.
382 * @param[in] layout The current layout.
383 * @return The clamped first-item position.
385 float ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout);
388 * Called upon pan gesture event.
390 * @param[in] gesture The gesture event.
392 void OnPan(PanGesture pan);
395 * Helper to handle anchoring animations.
396 * @return The animation, or an uninitialized handle if not necessary.
398 Animation DoAnchoring();
401 * Callback from scroll animations
402 * @param[in] animation The scroll-animation which has finished.
404 void OnScrollFinished(Animation& animation);
407 * Called by animation system when overshoot has finished animating to maximum (either -1.0f or 1.0f)
409 * @param[in] animation the animation that has finished
411 void OnOvershootOnFinished(Animation& animation);
414 * Helper to start the refresh timer.
416 void StartRefreshTimer();
419 * Helper to cancel the refresh timer.
421 void CancelRefreshTimer();
424 * Refresh the ItemView; this is called after a timeout when scrolling.
425 * During a refresh, ItemFactory::NewItem() will be called to create newly visible items.
426 * @return True if the refresh timer should be kept running.
428 bool OnRefreshTick();
431 * This is called after a timeout when no new mouse wheel event is received for a certain period of time.
432 * @return will return false; one-shot timer.
434 bool OnMouseWheelEventFinished();
437 * Stops and removes animation if exists.
438 * @param[in,out] animation The animation handle to be removed.
440 void RemoveAnimation(Animation& animation);
443 * Helper to apply constraints to the overshoot overlay actor.
445 void ApplyOvershootOverlayConstraints();
448 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
449 * @return The scroll overshoot.
451 float CalculateScrollOvershoot();
454 * Helper to calculate the scroll overshoot according to the pan gesture displacement.
456 * @param[in] overshootAmount amount to animate to
457 * @param[in] animateBack whether to animate back to zero immediately after
458 * @return The scroll overshoot.
460 void AnimateScrollOvershoot(float overshootAmount, bool animateBack = false);
463 * Gets the scroll position in pixels according to the logical layout position.
464 * @param[in] layoutSize The current size of the layout.
466 float GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const;
469 * Calculates the minimum and maximum positions for each axis to scroll to.
470 * @param[in] layoutSize The current size of the layout.
472 void CalculateDomainSize(const Vector3& layoutSize);
475 * Calculates whether we want to allow current item view to scroll.
476 * @param[in] layoutSize The current size of the layout.
477 * @return true if itemview is scrollable
479 bool IsLayoutScrollable(const Vector3& layoutSize);
483 ItemFactory& mItemFactory;
485 typedef std::map<unsigned int, Actor> ItemPool;
486 typedef ItemPool::iterator ItemPoolIter;
487 typedef ItemPool::const_iterator ConstItemPoolIter;
491 ItemLayoutContainer mLayouts;
492 ItemLayout* mActiveLayout;
493 Vector3 mActiveLayoutTargetSize;
495 Animation mResizeAnimation;
496 Animation mScrollAnimation;
497 Animation mScrollSpeedAnimation;
498 Animation mScrollOvershootAnimation;
499 bool mAnimatingOvershootOn; ///< whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
500 bool mAnimateOvershootOff; ///< whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
502 bool mAnchoringEnabled;
503 float mAnchoringDuration;
506 int mRefreshIntervalMilliseconds;
507 bool mRefreshOrderHint; ///< True if scrolling towards the last item
511 float mMinimumSwipeSpeed;
512 float mMinimumSwipeDistance;
513 float mMouseWheelScrollDistanceStep; ///< The step of scroll distance in actor coordinates for each mouse wheel event received.
515 float mScrollDistance;
517 Vector2 mTotalPanDisplacement;
519 float mScrollOvershoot;
522 Timer mMouseWheelEventFinishedTimer; ///< The timer to determine whether there is no mouse wheel event received for a certain period of time.
524 Dali::Gesture::State mGestureState;
526 ImageActor mOvershootOverlay; ///< The overlay actor for overshoot effect
527 OvershootRippleEffect mOvershootEffect; ///< The vertex/fragment shader used to display the overshoot ripple effect
529 Dali::Toolkit::ScrollConnector mScrollConnector; ///< Connects ItemView with scrollable components e.g. scroll bars
530 Constrainable mScrollPositionObject; ///< From mScrollConnector
532 Property::Index mPropertyPosition; ///< The physical position of the first item within the layout
533 Property::Index mPropertyMinimumLayoutPosition; ///< The minimum valid layout position in the layout.
534 Property::Index mPropertyScrollSpeed; ///< The current scroll speed of item view
535 Property::Index mPropertyOvershoot; ///< The scroll overshoot (difference of the layout position before and after clamping)
538 } // namespace Internal
540 // Helpers for public-api forwarding methods
542 inline Toolkit::Internal::ItemView& GetImpl(Toolkit::ItemView& itemView)
544 DALI_ASSERT_ALWAYS(itemView);
546 Dali::RefObject& handle = itemView.GetImplementation();
548 return static_cast<Toolkit::Internal::ItemView&>(handle);
551 inline const Toolkit::Internal::ItemView& GetImpl(const Toolkit::ItemView& itemView)
553 DALI_ASSERT_ALWAYS(itemView);
555 const Dali::RefObject& handle = itemView.GetImplementation();
557 return static_cast<const Toolkit::Internal::ItemView&>(handle);
560 } // namespace Toolkit
564 #endif // __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__