Split dali-toolkit into Base & Optional
[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 private:
266
267   /**
268    * Remove an Actor if found in the ItemPool.
269    * @param[in] itemId The item to remove.
270    * @return True if an actor was removed.
271    */
272   bool RemoveActor( unsigned int itemId );
273
274   /**
275    * Remove any Actors outside a given range.
276    * @param[in] @param[in] range The range of required items.
277    */
278   void RemoveActorsOutsideRange( ItemRange range );
279
280   /**
281    * Add a range of Actors, if they are not already in the ItemPool.
282    * @param[in] range The range of Item IDs to associate with the new actors.
283    * @param[in] durationSeconds The time taken to fully constrain the newly added actor.
284    */
285   void AddActorsWithinRange( ItemRange range, float durationSeconds );
286
287   /**
288    * Add a new Actor, if not already in the ItemPool.
289    * @param[in] item The ID for the new item.
290    * @param[in] durationSeconds The time taken to fully constrain the new actor.
291    */
292   void AddNewActor( ItemId item, float durationSeconds );
293
294   /**
295    * Apply the constraints etc. that are required for ItemView children.
296    * @param[in] item The item to setup.
297    * @param[in] durationSeconds The time taken to fully constrain the actor.
298    */
299   void SetupActor( Item item, float durationSeconds );
300
301 private: // From CustomActorImpl
302
303   /**
304    * From CustomActorImpl; called after a child has been added to the owning actor.
305    * @param[in] child The child which has been added.
306    */
307   virtual void OnChildAdd(Actor& child);
308
309   /**
310    * From CustomActorImpl; called after a touch-signal is received by the owning actor.
311    * @param[in] event The touch event.
312    * @return True if the event should be consumed.
313    */
314   virtual bool OnTouchEvent(const TouchEvent& event);
315
316   /**
317    * From CustomActorImpl; called after a mouse-wheel-event is received by the owning actor.
318    * @param[in] event The mouse wheel event.
319    * @return True if the event should be consumed.
320    */
321   virtual bool OnMouseWheelEvent(const MouseWheelEvent& event);
322
323 private: // From ControlImpl
324
325   /**
326    * @copydoc Toolkit::Control::OnInitialize()
327    */
328   virtual void OnInitialize();
329
330   /**
331    * @copydoc Toolkit::Control::OnAccessibilityPan()
332    */
333   virtual bool OnAccessibilityPan(PanGesture gesture);
334
335   /**
336    * @copydoc Toolkit::Control::GetNextKeyboardFocusableActor()
337    */
338   virtual Actor GetNextKeyboardFocusableActor(Actor actor, Control::KeyboardFocusNavigationDirection direction, bool loopEnabled);
339
340   /**
341    * @copydoc Toolkit::Control::OnKeyboardFocusChangeCommitted()
342    */
343   virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor);
344
345 protected:
346
347   /**
348    * Construct a new ItemView.
349    * @param[in] factory The factory which provides ItemView with items.
350    */
351   ItemView(ItemFactory& factory);
352
353   /**
354    * A reference counted object may only be deleted by calling Unreference()
355    */
356   virtual ~ItemView();
357
358 private:
359
360   // Undefined
361   ItemView(const ItemView&);
362
363   // Undefined
364   ItemView& operator=(const ItemView& rhs);
365
366   /**
367    * Helper to apply constraints to an actor.
368    * @param[in] actor The actor to constrain.
369    * @param[in] layout The active layout.
370    * @param[in] itemId The ID of the item represented by the actor.
371    * @param[in] durationSeconds The time taken to fully constrain the actors.
372    */
373   void ApplyConstraints(Actor& actor, ItemLayout& layout, unsigned int itemId, float durationSeconds);
374
375   /**
376    * Helper to re-apply all the constraints after items have been inserted, removed etc.
377    * @param[in] durationSeconds The time taken to fully constrain the actors.
378    */
379   void ReapplyAllConstraints( float durationSeconds );
380
381   /**
382    * Helper to remove items outside a given range.
383    * @param[in] range The range of required items.
384    */
385   void RemoveItems(ItemRange range);
386
387   /**
388    * Helper to add a range of items, if not already in the ItemPool.
389    * @param[in] layout The layout used to position the new items.
390    * @param[in] layoutSize The current size of the layout.
391    * @param[in] range The range of required items.
392    */
393   void AddItems(ItemLayout& layout, const Vector3& layoutSize, ItemRange range);
394
395   /**
396    * Helper to find the range of items to populate with.
397    * @param[in] layout The current layout.
398    * @param[in] range The range of items.
399    * @param[in] reserveExtra True if reserve items should be included.
400    */
401   ItemRange GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra);
402
403   // Input Handling
404
405   /**
406    * Helper to handle pressed (Down) events.
407    * @param[in] x The X coordinate of the touch event.
408    * @param[in] y The Y coordinate of the touch event.
409    * @param[in] timeMs The time-stamp of the touch event.
410    */
411   void OnPressed(float x, float y, unsigned long timeMs);
412
413   /**
414    * Helper to clamp the first-item position when dragging/swiping.
415    * @param[in] targetPosition The target position of the drag etc.
416    * @param[in] targetSize The target ItemView & layout size.
417    * @param[in] layout The current layout.
418    * @return The clamped first-item position.
419    */
420   float ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout);
421
422   /**
423    * Called upon pan gesture event.
424    *
425    * @param[in] gesture The gesture event.
426    */
427   void OnPan(PanGesture pan);
428
429   /**
430    * Helper to handle anchoring animations.
431    * @return The animation, or an uninitialized handle if not necessary.
432    */
433   Animation DoAnchoring();
434
435   /**
436    * Callback from scroll animations
437    * @param[in] animation The scroll-animation which has finished.
438    */
439   void OnScrollFinished(Animation& animation);
440
441   /**
442    * Called by animation system when overshoot has finished animating to maximum (either -1.0f or 1.0f)
443    *
444    * @param[in] animation the animation that has finished
445    */
446   void OnOvershootOnFinished(Animation& animation);
447
448   /**
449    * This is called after a timeout when no new mouse wheel event is received for a certain period of time.
450    * @return will return false; one-shot timer.
451    */
452   bool OnMouseWheelEventFinished();
453
454   /**
455    * Stops and removes animation if exists.
456    * @param[in,out] animation The animation handle to be removed.
457    */
458   void RemoveAnimation(Animation& animation);
459
460   /**
461    * Helper to apply constraints to the overshoot overlay actor.
462    */
463   void ApplyOvershootOverlayConstraints();
464
465   /**
466    * Helper to calculate the scroll overshoot according to the pan gesture displacement.
467    * @return The scroll overshoot.
468    */
469   float CalculateScrollOvershoot();
470
471   /**
472    * Helper to calculate the scroll overshoot according to the pan gesture displacement.
473    *
474    * @param[in] overshootAmount amount to animate to
475    * @param[in] animateBack whether to animate back to zero immediately after
476    * @return The scroll overshoot.
477    */
478   void AnimateScrollOvershoot(float overshootAmount, bool animateBack = false);
479
480   /**
481    * Gets the scroll position in pixels according to the logical layout position.
482    * @param[in] layoutSize The current size of the layout.
483    */
484   float GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const;
485
486   /**
487    * Calculates the minimum and maximum positions for each axis to scroll to.
488    * @param[in] layoutSize The current size of the layout.
489    */
490   void CalculateDomainSize(const Vector3& layoutSize);
491
492   /**
493    * Calculates whether we want to allow current item view to scroll.
494    * @param[in] layoutSize The current size of the layout.
495    * @return    true if itemview is scrollable
496    */
497   bool IsLayoutScrollable(const Vector3& layoutSize);
498
499   /**
500    * Callback when the current layout position of ItemView changes in both positive
501    * and negative directions by the specified amount. Refresh the ItemView to create
502    * newly visible items.
503    * @param[in] source the property notification that triggered this callback
504    */
505   void OnRefreshNotification(PropertyNotification& source);
506
507 private:
508
509   ItemFactory& mItemFactory;
510
511   typedef std::map<unsigned int, Actor> ItemPool;
512   typedef ItemPool::iterator            ItemPoolIter;
513   typedef ItemPool::const_iterator      ConstItemPoolIter;
514
515   ItemPool mItemPool;
516
517   ItemLayoutContainer mLayouts;
518   ItemLayout* mActiveLayout;
519   Vector3 mActiveLayoutTargetSize;
520
521   AlphaFunction mDefaultAlphaFunction;
522
523   Animation mResizeAnimation;
524   Animation mScrollAnimation;
525   Animation mScrollSpeedAnimation;
526   Animation mScrollOvershootAnimation;
527   bool      mAnimatingOvershootOn;          ///< whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
528   bool      mAnimateOvershootOff;         ///< whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
529
530   bool mAnchoringEnabled;
531   float mAnchoringDuration;
532
533   float mRefreshIntervalLayoutPositions;  ///< Refresh item view when the layout position changes by this interval in both positive and negative directions.
534   PropertyNotification mRefreshNotification; // stores the property notification used for item view refresh
535   bool mRefreshOrderHint; ///< True if scrolling towards the last item
536
537   // Input handling
538
539   float mMinimumSwipeSpeed;
540   float mMinimumSwipeDistance;
541   float mMouseWheelScrollDistanceStep; ///< The step of scroll distance in actor coordinates for each mouse wheel event received.
542
543   float mScrollDistance;
544   float mScrollSpeed;
545   Vector2 mTotalPanDisplacement;
546
547   float mScrollOvershoot;
548   bool mIsFlicking;
549
550   Timer mMouseWheelEventFinishedTimer; ///< The timer to determine whether there is no mouse wheel event received for a certain period of time.
551
552   Dali::Gesture::State mGestureState;
553
554   ImageActor mOvershootOverlay;           ///< The overlay actor for overshoot effect
555   OvershootRippleEffect mOvershootEffect; ///< The vertex/fragment shader used to display the overshoot ripple effect
556
557   Dali::Toolkit::ScrollConnector mScrollConnector; ///< Connects ItemView with scrollable components e.g. scroll bars
558   Constrainable   mScrollPositionObject;     ///< From mScrollConnector
559
560   bool mAddingItems;
561
562   Property::Index mPropertyPosition; ///< The physical position of the first item within the layout
563   Property::Index mPropertyMinimumLayoutPosition; ///< The minimum valid layout position in the layout.
564   Property::Index mPropertyScrollSpeed; ///< The current scroll speed of item view
565
566   bool mRefreshEnabled; ///< Whether to refresh the cache automatically
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__