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