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