License conversion from Flora to Apache 2.0
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / controls / scrollable / item-view / item-view-impl.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__
2 #define __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/dali.h>
23
24 // INTERNAL INCLUDES
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>
32
33 namespace Dali
34 {
35
36 namespace Toolkit
37 {
38
39 namespace Internal
40 {
41
42 class ItemView;
43
44 typedef IntrusivePtr<ItemView> ItemViewPtr;
45
46 /**
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.
50  */
51 class ItemView : public Scrollable
52 {
53 public:
54
55   /**
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.
59    */
60   static Dali::Toolkit::ItemView New(ItemFactory& factory);
61
62   /**
63    * @copydoc Toolkit::ItemView::GetScrollConnector
64    */
65   Dali::Toolkit::ScrollConnector GetScrollConnector() const;
66
67   /**
68    * @copydoc Toolkit::ItemView::GetLayoutCount
69    */
70   unsigned int GetLayoutCount() const;
71
72   /**
73    * @copydoc Toolkit::ItemView::AddLayout
74    */
75   void AddLayout(ItemLayout& layout);
76
77   /**
78    * @copydoc Toolkit::ItemView::RemoveLayout
79    */
80   void RemoveLayout(unsigned int layoutIndex);
81
82   /**
83    * @copydoc Toolkit::ItemView::GetLayout
84    */
85   ItemLayoutPtr GetLayout(unsigned int layoutIndex) const;
86
87   /**
88    * @copydoc Toolkit::ItemView::GetActiveLayout
89    */
90   ItemLayoutPtr GetActiveLayout() const;
91
92   /**
93    * @copydoc Toolkit::ItemView::GetCurrentLayoutPosition
94    */
95   float GetCurrentLayoutPosition(unsigned int itemId) const;
96
97   /**
98    * @copydoc Toolkit::ItemView::ActivateLayout
99    */
100   void ActivateLayout(unsigned int layoutIndex, const Vector3& targetSize, float durationSeconds);
101
102   /**
103    * @copydoc Toolkit::ItemView::SetDefaultAlphaFunction
104    */
105   void SetDefaultAlphaFunction(AlphaFunction func);
106
107   /**
108    * @copydoc Toolkit::ItemView::GetDefaultAlphaFunction
109    */
110   AlphaFunction GetDefaultAlphaFunction() const;
111
112   /**
113    * @copydoc Toolkit::ItemView::DeactivateCurrentLayout
114    */
115   void DeactivateCurrentLayout();
116
117   /**
118    * @copydoc Toolkit::ItemView::SetMinimumSwipeSpeed
119    */
120   void SetMinimumSwipeSpeed(float speed);
121
122   /**
123    * @copydoc Toolkit::ItemView::GetMinimumSwipeSpeed
124    */
125   float GetMinimumSwipeSpeed() const;
126
127   /**
128    * @copydoc Toolkit::ItemView::SetMinimumSwipeDistance
129    */
130   void SetMinimumSwipeDistance(float distance);
131
132   /**
133    * @copydoc Toolkit::ItemView::GetMinimumSwipeDistance
134    */
135   float GetMinimumSwipeDistance() const;
136
137   /**
138    * @copydoc Toolkit::ItemView::SetMouseWheelScrollDistanceStep
139    */
140   void SetMouseWheelScrollDistanceStep(float step);
141
142   /**
143    * @copydoc Toolkit::ItemView::GetMouseWheelScrollDistanceStep
144    */
145   float GetMouseWheelScrollDistanceStep() const;
146
147   /**
148    * @copydoc Toolkit::ItemView::SetAnchoring
149    */
150   void SetAnchoring(bool enabled);
151
152   /**
153    * @copydoc Toolkit::ItemView::GetAnchoring
154    */
155   bool GetAnchoring() const;
156
157   /**
158    * @copydoc Toolkit::ItemView::SetAnchoringDuration
159    */
160   void SetAnchoringDuration(float durationSeconds);
161
162   /**
163    * @copydoc Toolkit::ItemView::GetAnchoringDuration
164    */
165   float GetAnchoringDuration() const;
166
167   /**
168    * @copydoc Toolkit::ItemView::ScrollToItem
169    */
170   void ScrollToItem(unsigned int itemId, float durationSeconds);
171
172   /**
173    * @copydoc Toolkit::ItemView::SetRefreshInterval
174    */
175   void SetRefreshInterval(float intervalLayoutPositions);
176
177   /**
178    * @copydoc Toolkit::ItemView::GetRefreshInterval
179    */
180   float GetRefreshInterval() const;
181
182   /**
183    * @copydoc Toolkit::ItemView::GetItem
184    */
185   Actor GetItem(unsigned int itemId) const;
186
187   /**
188    * @copydoc Toolkit::ItemView::GetItemId
189    */
190   unsigned int GetItemId(Actor actor) const;
191
192   /**
193    * @copydoc Toolkit::ItemView::InsertItem
194    */
195   void InsertItem(Item newItem, float durationSeconds);
196
197   /**
198    * @copydoc Toolkit::ItemView::InsertItem
199    */
200   void InsertItems(const ItemContainer& newItems, float durationSeconds);
201
202   /**
203    * @copydoc Toolkit::ItemView::RemoveItem
204    */
205   void RemoveItem(ItemId itemId, float durationSeconds);
206
207   /**
208    * @copydoc Toolkit::ItemView::InsertItem
209    */
210   void RemoveItems(const ItemIdContainer& itemIds, float durationSeconds);
211
212   /**
213    * @copydoc Toolkit::ItemView::InsertItem
214    */
215   void ReplaceItem(Item replacementItem, float durationSeconds);
216
217   /**
218    * @copydoc Toolkit::ItemView::InsertItem
219    */
220   void ReplaceItems(const ItemContainer& replacementItems, float durationSeconds);
221
222   /**
223    * @copydoc Toolkit::Scrollable::GetDomainSize
224    */
225   Vector3 GetDomainSize() const;
226
227   /**
228    * @copydoc Toolkit::Scrollable::GetCurrentScrollPosition
229    */
230   Vector3 GetCurrentScrollPosition() const;
231
232   /**
233    * @copydoc Toolkit::Scrollable::AddOverlay()
234    */
235   void AddOverlay(Actor actor);
236
237   /**
238    * @copydoc Toolkit::Scrollable::RemoveOverlay()
239    */
240   void RemoveOverlay(Actor actor);
241
242   /**
243    * @copydoc Toolkit::Scrollable::ScrollTo(const Vector3& position, float duration)
244    */
245   void ScrollTo(const Vector3& position, float duration);
246
247   /**
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.
253    *
254    * @param[in] enabled True to enable automatic refresh or false to disable it.
255    */
256   void SetRefreshEnabled(bool enabled);
257
258   /**
259    * @brief Helper to perform the refresh.
260    *
261    * @param[in] currentLayoutPosition The current layout position.
262    * @param[in] cacheExtra Whether to cache extra items during refresh.
263    */
264   void DoRefresh(float currentLayoutPosition, bool cacheExtra);
265
266
267   /**
268    * @copydoc Toolkit::ItemView::SetItemsParentOrigin
269    */
270   void SetItemsParentOrigin( const Vector3& parentOrigin );
271
272   /**
273    * @copydoc Toolkit::ItemView::GetItemsParentOrigin
274    */
275   Vector3 GetItemsParentOrigin() const;
276
277   /**
278    * @copydoc Toolkit::ItemView::SetItemsAnchorPoint
279    */
280   void SetItemsAnchorPoint( const Vector3& anchorPoint );
281
282   /**
283    * @copydoc Toolkit::ItemView::GetItemsAnchorPoint
284    */
285   Vector3 GetItemsAnchorPoint() const;
286
287 private:
288
289   /**
290    * Remove an Actor if found in the ItemPool.
291    * @param[in] itemId The item to remove.
292    * @return True if an actor was removed.
293    */
294   bool RemoveActor( unsigned int itemId );
295
296   /**
297    * Remove any Actors outside a given range.
298    * @param[in] @param[in] range The range of required items.
299    */
300   void RemoveActorsOutsideRange( ItemRange range );
301
302   /**
303    * Add a range of Actors, if they are not already in the ItemPool.
304    * @param[in] range The range of Item IDs to associate with the new actors.
305    * @param[in] durationSeconds The time taken to fully constrain the newly added actor.
306    */
307   void AddActorsWithinRange( ItemRange range, float durationSeconds );
308
309   /**
310    * Add a new Actor, if not already in the ItemPool.
311    * @param[in] item The ID for the new item.
312    * @param[in] durationSeconds The time taken to fully constrain the new actor.
313    */
314   void AddNewActor( ItemId item, float durationSeconds );
315
316   /**
317    * Apply the constraints etc. that are required for ItemView children.
318    * @param[in] item The item to setup.
319    * @param[in] durationSeconds The time taken to fully constrain the actor.
320    */
321   void SetupActor( Item item, float durationSeconds );
322
323 private: // From CustomActorImpl
324
325   /**
326    * From CustomActorImpl; called after a child has been added to the owning actor.
327    * @param[in] child The child which has been added.
328    */
329   virtual void OnChildAdd(Actor& child);
330
331   /**
332    * From CustomActorImpl; called after a touch-signal is received by the owning actor.
333    * @param[in] event The touch event.
334    * @return True if the event should be consumed.
335    */
336   virtual bool OnTouchEvent(const TouchEvent& event);
337
338   /**
339    * From CustomActorImpl; called after a mouse-wheel-event is received by the owning actor.
340    * @param[in] event The mouse wheel event.
341    * @return True if the event should be consumed.
342    */
343   virtual bool OnMouseWheelEvent(const MouseWheelEvent& event);
344
345 private: // From ControlImpl
346
347   /**
348    * @copydoc Toolkit::Control::OnInitialize()
349    */
350   virtual void OnInitialize();
351
352   /**
353    * @copydoc Toolkit::Control::OnAccessibilityPan()
354    */
355   virtual bool OnAccessibilityPan(PanGesture gesture);
356
357   /**
358    * @copydoc Toolkit::Control::GetNextKeyboardFocusableActor()
359    */
360   virtual Actor GetNextKeyboardFocusableActor(Actor actor, Control::KeyboardFocusNavigationDirection direction, bool loopEnabled);
361
362   /**
363    * @copydoc Toolkit::Control::OnKeyboardFocusChangeCommitted()
364    */
365   virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor);
366
367 protected:
368
369   /**
370    * Construct a new ItemView.
371    * @param[in] factory The factory which provides ItemView with items.
372    */
373   ItemView(ItemFactory& factory);
374
375   /**
376    * A reference counted object may only be deleted by calling Unreference()
377    */
378   virtual ~ItemView();
379
380 private:
381
382   // Undefined
383   ItemView(const ItemView&);
384
385   // Undefined
386   ItemView& operator=(const ItemView& rhs);
387
388   /**
389    * Helper to apply constraints to an actor.
390    * @param[in] actor The actor to constrain.
391    * @param[in] layout The active layout.
392    * @param[in] itemId The ID of the item represented by the actor.
393    * @param[in] durationSeconds The time taken to fully constrain the actors.
394    */
395   void ApplyConstraints(Actor& actor, ItemLayout& layout, unsigned int itemId, float durationSeconds);
396
397   /**
398    * Helper to re-apply all the constraints after items have been inserted, removed etc.
399    * @param[in] durationSeconds The time taken to fully constrain the actors.
400    */
401   void ReapplyAllConstraints( float durationSeconds );
402
403   /**
404    * Helper to remove items outside a given range.
405    * @param[in] range The range of required items.
406    */
407   void RemoveItems(ItemRange range);
408
409   /**
410    * Helper to add a range of items, if not already in the ItemPool.
411    * @param[in] layout The layout used to position the new items.
412    * @param[in] layoutSize The current size of the layout.
413    * @param[in] range The range of required items.
414    */
415   void AddItems(ItemLayout& layout, const Vector3& layoutSize, ItemRange range);
416
417   /**
418    * Helper to find the range of items to populate with.
419    * @param[in] layout The current layout.
420    * @param[in] range The range of items.
421    * @param[in] reserveExtra True if reserve items should be included.
422    */
423   ItemRange GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra);
424
425   // Input Handling
426
427   /**
428    * Helper to handle pressed (Down) events.
429    * @param[in] x The X coordinate of the touch event.
430    * @param[in] y The Y coordinate of the touch event.
431    * @param[in] timeMs The time-stamp of the touch event.
432    */
433   void OnPressed(float x, float y, unsigned long timeMs);
434
435   /**
436    * Helper to clamp the first-item position when dragging/swiping.
437    * @param[in] targetPosition The target position of the drag etc.
438    * @param[in] targetSize The target ItemView & layout size.
439    * @param[in] layout The current layout.
440    * @return The clamped first-item position.
441    */
442   float ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout);
443
444   /**
445    * Called upon pan gesture event.
446    *
447    * @param[in] gesture The gesture event.
448    */
449   void OnPan(PanGesture pan);
450
451   /**
452    * Helper to handle anchoring animations.
453    * @return The animation, or an uninitialized handle if not necessary.
454    */
455   Animation DoAnchoring();
456
457   /**
458    * Callback from scroll animations
459    * @param[in] animation The scroll-animation which has finished.
460    */
461   void OnScrollFinished(Animation& animation);
462
463   /**
464    * Called by animation system when overshoot has finished animating to maximum (either -1.0f or 1.0f)
465    *
466    * @param[in] animation the animation that has finished
467    */
468   void OnOvershootOnFinished(Animation& animation);
469
470   /**
471    * This is called after a timeout when no new mouse wheel event is received for a certain period of time.
472    * @return will return false; one-shot timer.
473    */
474   bool OnMouseWheelEventFinished();
475
476   /**
477    * Stops and removes animation if exists.
478    * @param[in,out] animation The animation handle to be removed.
479    */
480   void RemoveAnimation(Animation& animation);
481
482   /**
483    * @copydoc Toolkit::Internal::Scrollable::SetOvershootEnabled
484    */
485   virtual void SetOvershootEnabled( bool enable );
486
487   /**
488    * Helper to calculate the scroll overshoot according to the pan gesture displacement.
489    * @return The scroll overshoot.
490    */
491   float CalculateScrollOvershoot();
492
493   /**
494    * Helper to calculate the scroll overshoot according to the pan gesture displacement.
495    *
496    * @param[in] overshootAmount amount to animate to
497    * @param[in] animateBack whether to animate back to zero immediately after
498    * @return The scroll overshoot.
499    */
500   void AnimateScrollOvershoot(float overshootAmount, bool animateBack = false);
501
502   /**
503    * Gets the scroll position in pixels according to the logical layout position.
504    * @param[in] layoutSize The current size of the layout.
505    */
506   float GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const;
507
508   /**
509    * Calculates the minimum and maximum positions for each axis to scroll to.
510    * @param[in] layoutSize The current size of the layout.
511    */
512   void CalculateDomainSize(const Vector3& layoutSize);
513
514   /**
515    * Calculates whether we want to allow current item view to scroll.
516    * @param[in] layoutSize The current size of the layout.
517    * @return    true if itemview is scrollable
518    */
519   bool IsLayoutScrollable(const Vector3& layoutSize);
520
521   /**
522    * Callback when the current layout position of ItemView changes in both positive
523    * and negative directions by the specified amount. Refresh the ItemView to create
524    * newly visible items.
525    * @param[in] source the property notification that triggered this callback
526    */
527   void OnRefreshNotification(PropertyNotification& source);
528
529 private:
530
531   ItemFactory& mItemFactory;
532
533   typedef std::map<unsigned int, Actor> ItemPool;
534   typedef ItemPool::iterator            ItemPoolIter;
535   typedef ItemPool::const_iterator      ConstItemPoolIter;
536
537   ItemPool mItemPool;
538
539   ItemLayoutContainer mLayouts;
540   ItemLayout* mActiveLayout;
541   Vector3 mActiveLayoutTargetSize;
542
543   AlphaFunction mDefaultAlphaFunction;
544
545   Animation mResizeAnimation;
546   Animation mScrollAnimation;
547   Animation mScrollSpeedAnimation;
548   Animation mScrollOvershootAnimation;
549   bool      mAnimatingOvershootOn;          ///< whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
550   bool      mAnimateOvershootOff;         ///< whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
551
552   bool mAnchoringEnabled;
553   float mAnchoringDuration;
554
555   float mRefreshIntervalLayoutPositions;  ///< Refresh item view when the layout position changes by this interval in both positive and negative directions.
556   PropertyNotification mRefreshNotification; // stores the property notification used for item view refresh
557   bool mRefreshOrderHint; ///< True if scrolling towards the last item
558
559   // Input handling
560
561   float mMinimumSwipeSpeed;
562   float mMinimumSwipeDistance;
563   float mMouseWheelScrollDistanceStep; ///< The step of scroll distance in actor coordinates for each mouse wheel event received.
564
565   float mScrollDistance;
566   float mScrollSpeed;
567   Vector2 mTotalPanDisplacement;
568
569   float mScrollOvershoot;
570   bool mIsFlicking;
571
572   Timer mMouseWheelEventFinishedTimer; ///< The timer to determine whether there is no mouse wheel event received for a certain period of time.
573
574   Dali::Gesture::State mGestureState;
575
576   ImageActor mOvershootOverlay;           ///< The overlay actor for overshoot effect
577   BouncingEffect mOvershootEffect; ///< The vertex/fragment shader used to display the overshoot ripple effect
578
579   Dali::Toolkit::ScrollConnector mScrollConnector; ///< Connects ItemView with scrollable components e.g. scroll bars
580   Constrainable   mScrollPositionObject;     ///< From mScrollConnector
581
582   bool mAddingItems;
583
584   Property::Index mPropertyPosition; ///< The physical position of the first item within the layout
585   Property::Index mPropertyMinimumLayoutPosition; ///< The minimum valid layout position in the layout.
586   Property::Index mPropertyScrollSpeed; ///< The current scroll speed of item view
587
588   bool mRefreshEnabled; ///< Whether to refresh the cache automatically
589
590   Vector3 mItemsParentOrigin;
591   Vector3 mItemsAnchorPoint;
592 };
593
594 } // namespace Internal
595
596 // Helpers for public-api forwarding methods
597
598 inline Toolkit::Internal::ItemView& GetImpl(Toolkit::ItemView& itemView)
599 {
600   DALI_ASSERT_ALWAYS(itemView);
601
602   Dali::RefObject& handle = itemView.GetImplementation();
603
604   return static_cast<Toolkit::Internal::ItemView&>(handle);
605 }
606
607 inline const Toolkit::Internal::ItemView& GetImpl(const Toolkit::ItemView& itemView)
608 {
609   DALI_ASSERT_ALWAYS(itemView);
610
611   const Dali::RefObject& handle = itemView.GetImplementation();
612
613   return static_cast<const Toolkit::Internal::ItemView&>(handle);
614 }
615
616 } // namespace Toolkit
617
618 } // namespace Dali
619
620 #endif // __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__