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