1f33626811b40009ae0ba35031751e325b3ac3c2
[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
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    * @copydoc Toolkit::Internal::Scrollable::SetOvershootEffectColor
248    */
249   void SetOvershootEffectColor( const Vector4& color );
250
251   /**
252    * @brief Set whether to enable automatic refresh or not. When refresh is disabled,
253    * ItemView will not automatically refresh the cache in the given interval when the
254    * layout position is changed. This is useful in some cases, for example, automatic
255    * refresh is not needed during fast scrolling, otherwise it will cache unneeded
256    * items when the layout position changes quickly.
257    *
258    * @param[in] enabled True to enable automatic refresh or false to disable it.
259    */
260   void SetRefreshEnabled(bool enabled);
261
262   /**
263    * @brief Helper to perform the refresh.
264    *
265    * @param[in] currentLayoutPosition The current layout position.
266    * @param[in] cacheExtra Whether to cache extra items during refresh.
267    */
268   void DoRefresh(float currentLayoutPosition, bool cacheExtra);
269
270
271   /**
272    * @copydoc Toolkit::ItemView::SetItemsParentOrigin
273    */
274   void SetItemsParentOrigin( const Vector3& parentOrigin );
275
276   /**
277    * @copydoc Toolkit::ItemView::GetItemsParentOrigin
278    */
279   Vector3 GetItemsParentOrigin() const;
280
281   /**
282    * @copydoc Toolkit::ItemView::SetItemsAnchorPoint
283    */
284   void SetItemsAnchorPoint( const Vector3& anchorPoint );
285
286   /**
287    * @copydoc Toolkit::ItemView::GetItemsAnchorPoint
288    */
289   Vector3 GetItemsAnchorPoint() const;
290
291   /**
292    * @copydoc Toolkit::ItemView::GetItemsRange
293    */
294   void GetItemsRange(ItemRange& range);
295
296 private:
297
298   /**
299    * Remove an Actor if found in the ItemPool.
300    * @param[in] itemId The item to remove.
301    * @return True if an actor was removed.
302    */
303   bool RemoveActor( unsigned int itemId );
304
305   /**
306    * Remove any Actors outside a given range.
307    * @param[in] @param[in] range The range of required items.
308    */
309   void RemoveActorsOutsideRange( ItemRange range );
310
311   /**
312    * Add a range of Actors, if they are not already in the ItemPool.
313    * @param[in] range The range of Item IDs to associate with the new actors.
314    * @param[in] durationSeconds The time taken to fully constrain the newly added actor.
315    */
316   void AddActorsWithinRange( ItemRange range, float durationSeconds );
317
318   /**
319    * Add a new Actor, if not already in the ItemPool.
320    * @param[in] item The ID for the new item.
321    * @param[in] durationSeconds The time taken to fully constrain the new actor.
322    */
323   void AddNewActor( ItemId item, float durationSeconds );
324
325   /**
326    * Apply the constraints etc. that are required for ItemView children.
327    * @param[in] item The item to setup.
328    * @param[in] durationSeconds The time taken to fully constrain the actor.
329    */
330   void SetupActor( Item item, float durationSeconds );
331
332   /**
333    * Remove the Actor from the ItemPool and notify the ItemFactory the actor has been released by ItemView.
334    * @param[in] item The ID for the item to be released.
335    * @param[in] actor The actor to be removed from ItemView.
336    */
337   void ReleaseActor( ItemId item, Actor actor );
338
339 private: // From CustomActorImpl
340
341   /**
342    * From CustomActorImpl; called after a child has been added to the owning actor.
343    * @param[in] child The child which has been added.
344    */
345   virtual void OnChildAdd(Actor& child);
346
347   /**
348    * From CustomActorImpl; called after a touch-signal is received by the owning actor.
349    * @param[in] event The touch event.
350    * @return True if the event should be consumed.
351    */
352   virtual bool OnTouchEvent(const TouchEvent& event);
353
354   /**
355    * From CustomActorImpl; called after a mouse-wheel-event is received by the owning actor.
356    * @param[in] event The mouse wheel event.
357    * @return True if the event should be consumed.
358    */
359   virtual bool OnMouseWheelEvent(const MouseWheelEvent& event);
360
361 private: // From Control
362
363   /**
364    * @copydoc Toolkit::Control::OnInitialize()
365    */
366   virtual void OnInitialize();
367
368   /**
369    * @copydoc Toolkit::Control::OnAccessibilityPan()
370    */
371   virtual bool OnAccessibilityPan(PanGesture gesture);
372
373   /**
374    * @copydoc Toolkit::Control::GetNextKeyboardFocusableActor()
375    */
376   virtual Actor GetNextKeyboardFocusableActor(Actor actor, Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled);
377
378   /**
379    * @copydoc Toolkit::Control::OnKeyboardFocusChangeCommitted()
380    */
381   virtual void OnKeyboardFocusChangeCommitted(Actor commitedFocusableActor);
382
383 protected:
384
385   /**
386    * Construct a new ItemView.
387    * @param[in] factory The factory which provides ItemView with items.
388    */
389   ItemView(ItemFactory& factory);
390
391   /**
392    * A reference counted object may only be deleted by calling Unreference()
393    */
394   virtual ~ItemView();
395
396 private:
397
398   // Undefined
399   ItemView(const ItemView&);
400
401   // Undefined
402   ItemView& operator=(const ItemView& rhs);
403
404   /**
405    * Helper to apply constraints to an actor.
406    * @param[in] actor The actor to constrain.
407    * @param[in] layout The active layout.
408    * @param[in] itemId The ID of the item represented by the actor.
409    * @param[in] durationSeconds The time taken to fully constrain the actors.
410    */
411   void ApplyConstraints(Actor& actor, ItemLayout& layout, unsigned int itemId, float durationSeconds);
412
413   /**
414    * Helper to re-apply all the constraints after items have been inserted, removed etc.
415    * @param[in] durationSeconds The time taken to fully constrain the actors.
416    */
417   void ReapplyAllConstraints( float durationSeconds );
418
419   /**
420    * Helper to remove items outside a given range.
421    * @param[in] range The range of required items.
422    */
423   void RemoveItems(ItemRange range);
424
425   /**
426    * Helper to add a range of items, if not already in the ItemPool.
427    * @param[in] layout The layout used to position the new items.
428    * @param[in] layoutSize The current size of the layout.
429    * @param[in] range The range of required items.
430    */
431   void AddItems(ItemLayout& layout, const Vector3& layoutSize, ItemRange range);
432
433   /**
434    * Helper to find the range of items to populate with.
435    * @param[in] layout The current layout.
436    * @param[in] range The range of items.
437    * @param[in] reserveExtra True if reserve items should be included.
438    */
439   ItemRange GetItemRange(ItemLayout& layout, const Vector3& layoutSize, float layoutPosition, bool reserveExtra);
440
441   // Input Handling
442
443   /**
444    * Helper to handle pressed (Down) events.
445    * @param[in] x The X coordinate of the touch event.
446    * @param[in] y The Y coordinate of the touch event.
447    * @param[in] timeMs The time-stamp of the touch event.
448    */
449   void OnPressed(float x, float y, unsigned long timeMs);
450
451   /**
452    * Helper to clamp the first-item position when dragging/swiping.
453    * @param[in] targetPosition The target position of the drag etc.
454    * @param[in] targetSize The target ItemView & layout size.
455    * @param[in] layout The current layout.
456    * @return The clamped first-item position.
457    */
458   float ClampFirstItemPosition(float targetPosition, const Vector3& targetSize, ItemLayout& layout);
459
460   /**
461    * Called upon pan gesture event.
462    *
463    * @param[in] gesture The gesture event.
464    */
465   void OnPan(PanGesture pan);
466
467   /**
468    * Helper to handle anchoring animations.
469    * @return The animation, or an uninitialized handle if not necessary.
470    */
471   Animation DoAnchoring();
472
473   /**
474    * Callback from scroll animations
475    * @param[in] animation The scroll-animation which has finished.
476    */
477   void OnScrollFinished(Animation& animation);
478
479   /**
480    * Callback from layout activation scroll animations
481    * @param[in] animation The scroll-animation which has finished.
482    */
483   void OnLayoutActivationScrollFinished(Animation& animation);
484
485   /**
486    * Called by animation system when overshoot has finished animating to maximum (either -1.0f or 1.0f)
487    *
488    * @param[in] animation the animation that has finished
489    */
490   void OnOvershootOnFinished(Animation& animation);
491
492   /**
493    * This is called after a timeout when no new mouse wheel event is received for a certain period of time.
494    * @return will return false; one-shot timer.
495    */
496   bool OnMouseWheelEventFinished();
497
498   /**
499    * Stops and removes animation if exists.
500    * @param[in,out] animation The animation handle to be removed.
501    */
502   void RemoveAnimation(Animation& animation);
503
504   /**
505    * @copydoc Toolkit::Internal::Scrollable::SetOvershootEnabled
506    */
507   virtual void SetOvershootEnabled( bool enable );
508
509   /**
510    * Helper to calculate the scroll overshoot according to the pan gesture displacement.
511    * @return The scroll overshoot.
512    */
513   float CalculateScrollOvershoot();
514
515   /**
516    * Helper to calculate the scroll overshoot according to the pan gesture displacement.
517    *
518    * @param[in] overshootAmount amount to animate to
519    * @param[in] animateBack whether to animate back to zero immediately after
520    * @return The scroll overshoot.
521    */
522   void AnimateScrollOvershoot(float overshootAmount, bool animateBack = false);
523
524   /**
525    * Gets the scroll position in pixels according to the logical layout position.
526    * @param[in] layoutSize The current size of the layout.
527    */
528   float GetScrollPosition(float layoutPosition, const Vector3& layoutSize) const;
529
530   /**
531    * Calculates the minimum and maximum positions for each axis to scroll to.
532    * @param[in] layoutSize The current size of the layout.
533    */
534   void CalculateDomainSize(const Vector3& layoutSize);
535
536   /**
537    * Calculates whether we want to allow current item view to scroll.
538    * @param[in] layoutSize The current size of the layout.
539    * @return    true if itemview is scrollable
540    */
541   bool IsLayoutScrollable(const Vector3& layoutSize);
542
543   /**
544    * Callback when the current layout position of ItemView changes in both positive
545    * and negative directions by the specified amount. Refresh the ItemView to create
546    * newly visible items.
547    * @param[in] source the property notification that triggered this callback
548    */
549   void OnRefreshNotification(PropertyNotification& source);
550
551 private:
552
553   ItemFactory& mItemFactory;
554
555   typedef std::map<unsigned int, Actor> ItemPool;
556   typedef ItemPool::iterator            ItemPoolIter;
557   typedef ItemPool::const_iterator      ConstItemPoolIter;
558
559   ItemPool mItemPool;
560
561   ItemLayoutContainer mLayouts;
562   ItemLayout* mActiveLayout;
563   Vector3 mActiveLayoutTargetSize;
564
565   AlphaFunction mDefaultAlphaFunction;
566
567   Animation mResizeAnimation;
568   Animation mScrollAnimation;
569   Animation mScrollOvershootAnimation;
570   bool      mAnimatingOvershootOn;          ///< whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
571   bool      mAnimateOvershootOff;         ///< whether we are currently animating overshoot to 1.0f/-1.0f (on) or to 0.0f (off)
572
573   bool mAnchoringEnabled;
574   float mAnchoringDuration;
575
576   float mRefreshIntervalLayoutPositions;  ///< Refresh item view when the layout position changes by this interval in both positive and negative directions.
577   PropertyNotification mRefreshNotification; // stores the property notification used for item view refresh
578   bool mRefreshOrderHint; ///< True if scrolling towards the last item
579
580   // Input handling
581
582   float mMinimumSwipeSpeed;
583   float mMinimumSwipeDistance;
584   float mMouseWheelScrollDistanceStep; ///< The step of scroll distance in actor coordinates for each mouse wheel event received.
585
586   float mScrollDistance;
587   float mScrollSpeed;
588   Vector2 mTotalPanDisplacement;
589
590   float mScrollOvershoot;
591   bool mIsFlicking;
592
593   Timer mMouseWheelEventFinishedTimer; ///< The timer to determine whether there is no mouse wheel event received for a certain period of time.
594
595   Dali::Gesture::State mGestureState;
596
597   Actor mOvershootOverlay;           ///< The overlay actor for overshoot effect
598
599   Dali::Toolkit::ScrollConnector mScrollConnector; ///< Connects ItemView with scrollable components e.g. scroll bars
600   Constrainable   mScrollPositionObject;     ///< From mScrollConnector
601
602   bool mAddingItems;
603
604   Property::Index mPropertyPosition; ///< The physical position of the first item within the layout
605   Property::Index mPropertyMinimumLayoutPosition; ///< The minimum valid layout position in the layout.
606   Property::Index mPropertyScrollSpeed; ///< The current scroll speed of item view
607
608   bool mRefreshEnabled; ///< Whether to refresh the cache automatically
609
610   Vector3 mItemsParentOrigin;
611   Vector3 mItemsAnchorPoint;
612 };
613
614 } // namespace Internal
615
616 // Helpers for public-api forwarding methods
617
618 inline Toolkit::Internal::ItemView& GetImpl(Toolkit::ItemView& itemView)
619 {
620   DALI_ASSERT_ALWAYS(itemView);
621
622   Dali::RefObject& handle = itemView.GetImplementation();
623
624   return static_cast<Toolkit::Internal::ItemView&>(handle);
625 }
626
627 inline const Toolkit::Internal::ItemView& GetImpl(const Toolkit::ItemView& itemView)
628 {
629   DALI_ASSERT_ALWAYS(itemView);
630
631   const Dali::RefObject& handle = itemView.GetImplementation();
632
633   return static_cast<const Toolkit::Internal::ItemView&>(handle);
634 }
635
636 } // namespace Toolkit
637
638 } // namespace Dali
639
640 #endif // __DALI_TOOLKIT_INTERNAL_ITEM_VIEW_H__