ScrollBar refactoring
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / controls / scrollable / item-view / item-layout.h
1 #ifndef __DALI_TOOLKIT_ITEM_LAYOUT_H__
2 #define __DALI_TOOLKIT_ITEM_LAYOUT_H__
3
4 /*
5  * Copyright (c) 2015 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 <boost/function.hpp>
23 #include <dali/public-api/animation/alpha-function.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/public-api/enums.h>
27 #include <dali-toolkit/public-api/controls/control.h>
28
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 class ItemLayout;
36
37 typedef IntrusivePtr<ItemLayout> ItemLayoutPtr; ///< Pointer to a Dali::Toolkit::ItemLayout object
38
39 /**
40  * @brief A support class for managing ranges of items.
41  */
42 struct ItemRange
43 {
44   /**
45    * @brief Create a range of item identifiers.
46    *
47    * @param[in] beginItem The first item within the range.
48    * @param[in] endItem The past-the-end item.
49    */
50   ItemRange(unsigned int beginItem, unsigned int endItem)
51   : begin(beginItem),
52     end(endItem)
53   {
54   }
55
56   /**
57    * @brief Copy Constructor.
58    *
59    * @param[in] copy ItemRange we should copy from.
60    */
61   ItemRange(const ItemRange& copy)
62   : begin(copy.begin),
63     end(copy.end)
64   {
65   }
66
67   /**
68    * @brief Assignment operator.
69    *
70    * @param[in] range The Range to assign from.
71    * @return The updated range.
72    */
73   ItemRange& operator=(const ItemRange& range)
74   {
75     begin = range.begin;
76     end = range.end;
77     return *this;
78   }
79
80   /**
81    * @brief Test whether an item is within the range.
82    *
83    * @param[in] itemId The item identifier.
84    * @return True if the item is within the range.
85    */
86   bool Within(unsigned int itemId)
87   {
88     return itemId >= begin &&
89            itemId < end;
90   }
91
92   /**
93    * @brief Create the intersection of two ranges.
94    *
95    * @param[in] second The second range.
96    * @return The intersection.
97    */
98   ItemRange Intersection(const ItemRange& second)
99   {
100     ItemRange intersection(0u, 0u);
101
102     // If the ranges intersect
103     if ( (begin < second.end && end > second.begin) ||
104          (second.begin < end && second.end > begin) )
105     {
106       intersection.begin = std::max(begin, second.begin);
107       intersection.end   = std::min(end, second.end);
108     }
109
110     return intersection;
111   }
112
113   unsigned int begin; ///< The start of the range
114   unsigned int end;   ///< The end of the range
115 };
116
117 /**
118  * @brief An ItemLayout describes the constraints which are imposed on items in the layout.
119  *
120  *   - Potentially visible items are represented by Actors, created for ItemView by the ItemFactory.
121  *   - Constraints are applied after ItemView activates a layout.
122  *
123  * An ItemLayout also describes the direction of input gestures, used to scroll through the layout.
124  * Whilst scrolling, the layout provides a range of items that are within a layout-area (3D bounding volume).
125  */
126 class DALI_IMPORT_API ItemLayout : public RefObject
127 {
128 public:
129
130   /// @brief Function signature of a boolean constraint
131   typedef boost::function<bool       (const bool&       current, const float& layoutPosition, const float& scrollSpeed, const Vector3& layoutSize)> BoolFunction;
132
133   /// @brief Function signature of a Vector3 constraint
134   typedef boost::function<Vector3    (const Vector3&    current, const float& layoutPosition, const float& scrollSpeed, const Vector3& layoutSize)> Vector3Function;
135
136   /// @brief Function signature of a Vector4 constraint
137   typedef boost::function<Vector4    (const Vector4&    current, const float& layoutPosition, const float& scrollSpeed, const Vector3& layoutSize)> Vector4Function;
138
139   /// @brief Function signature of a Quaternion constraint
140   typedef boost::function<Quaternion (const Quaternion& current, const float& layoutPosition, const float& scrollSpeed, const Vector3& layoutSize)> QuaternionFunction;
141
142   /**
143    * @brief Virtual destructor.
144    */
145   DALI_IMPORT_API virtual ~ItemLayout();
146
147   /**
148    * @brief Set the orientation of the layout.
149    *
150    * @param[in] orientation The orientation of the layout.
151    */
152   DALI_IMPORT_API void SetOrientation(ControlOrientation::Type orientation);
153
154   /**
155    * @brief Query the orientation of the layout.
156    *
157    * @return the orientation of the layout.
158    */
159   DALI_IMPORT_API ControlOrientation::Type GetOrientation() const;
160
161   /**
162    * @brief Query the minimum valid layout position; this is a negative value.
163    *
164    * When scrolling, the first item will move within the range 0 to GetMinimumLayoutPosition().
165    * @param[in] numberOfItems The current number of items in the layout.
166    * @param[in] layoutSize The size of the layout area.
167    * @return The minimum layout position.
168    */
169   virtual float GetMinimumLayoutPosition(unsigned int numberOfItems, Vector3 layoutSize) const = 0;
170
171   /**
172    * @brief Query the closest anchor position for the given layout position.
173    *
174    * This anchor position is the position where all the items in the layout are aligned to
175    * their rounded layout positions in integer.
176    * @param[in] layoutPosition The layout position.
177    * @return The closest anchor position for the given layout position.
178    */
179   virtual float GetClosestAnchorPosition(float layoutPosition) const = 0;
180
181   /**
182    * @brief Query the layout position for the first item in the layout to move to when the layout
183    * needs to scroll to a particular item.
184    *
185    * @param[in] itemId The ID of an item in the layout.
186    * @return The layout position for the first item in the layout to move to.
187    */
188   virtual float GetItemScrollToPosition(unsigned int itemId) const = 0;
189
190   /**
191    * @brief Query the items within a given layout-area.
192    *
193    * @param[in] firstItemPosition The layout-position of the first item in the layout.
194    * @param[in] layoutSize The size of the layout area.
195    * @return The ID of the first & last visible item.
196    */
197   virtual ItemRange GetItemsWithinArea(float firstItemPosition, Vector3 layoutSize) const = 0;
198
199   /**
200    * @brief Get the closest layout position to bring an item onto the screen.
201    *
202    * If the item is already fully on the screen this function will
203    * return the current layout position.
204    *
205    * This function is used by systems such as KeyboardFocusManager to
206    * bring the next focusable item into view and all layout
207    * implementations should provide their own version of this function
208    * to ensure proper functionality of internal toolkit systems.
209    *
210    * @param[in] itemID id of the item to bring within the viewable screen area
211    * @param[in] currentLayoutPosition the current layout position of the item view instance
212    * @param[in] layoutSize the current size of the item view instance
213    * @return The layout position
214    */
215   DALI_IMPORT_API virtual float GetClosestOnScreenLayoutPosition(int itemID, float currentLayoutPosition, const Vector3& layoutSize);
216
217   /**
218    * @brief Query the number of items that should be reserved, for scrolling purposes.
219    *
220    * @param[in] layoutSize The size of the layout area.
221    * @return The number of extra items. ItemView will populate itself with actors within the layout-area
222    * (see GetItemsWithinArea), plus this number of additional items on either-side.
223    */
224   virtual unsigned int GetReserveItemCount(Vector3 layoutSize) const = 0;
225
226   /**
227    * @brief Retrieve the target size of an item in the layout.
228    *
229    * @note layout-position is not provided as a parameter, since applying size constraints is not recommended.
230    * Animating to target-sizes is preferable, since this allows controls to perform layouting without constraints.
231    * @param[in] itemId The ID of an item in the layout.
232    * @param[in] layoutSize The layout size
233    * @param[out] itemSize The target size of an item, or an uninitialized value.
234    * @return Whether the item size is available or not
235    */
236   virtual bool GetItemSize(unsigned int itemId, Vector3 layoutSize, Vector3& itemSize) const = 0;
237
238   /**
239    * @brief Retrieve the resize animation in the layout.
240    *
241    * @note This allows the layout to provide its own resize animation.
242    * @param[in] animation The resize animation, not owned by the layout
243    * @param[in] actor The actor to animate
244    * @param [in] size The target size.
245    * @param [in] durationSeconds The duration of the resizing.
246    */
247   virtual void GetResizeAnimation(Animation& animation, Actor actor, Vector3 size, float durationSeconds) const = 0;
248
249   /**
250    * @brief Retrieve the position constraint function of an item in the layout.
251    *
252    * The constraint will be applied when the item is created or the layout is activated.
253    * @param[in] itemId The ID of an item in the layout.
254    * @param[out] constraint The position constraint function of an item, or an uninitialized function pointer.
255    * @return Whether the position constraint function of an item is available or not
256    */
257   virtual bool GetPositionConstraint(unsigned int itemId, Vector3Function& constraint) const = 0;
258
259   /**
260    * @brief Retrieve the rotation constraint function of an item in the layout.
261    *
262    * The constraint will be applied when the item is created or the layout is activated.
263    * @param[in] itemId The ID of an item in the layout.
264    * @param[out] constraint The rotation constraint function of an item, or an uninitialized function pointer.
265    * @return Whether the rotation constraint function of an item is available or not
266    */
267   virtual bool GetRotationConstraint(unsigned int itemId, QuaternionFunction& constraint) const = 0;
268
269   /**
270    * @brief Retrieve the scale constraint function of an item in the layout.
271    *
272    * The constraint will be applied when the item is created or the layout is activated.
273    * @param[in] itemId The ID of an item in the layout.
274    * @param[out] constraint The scale constraint function of an item, or an uninitialized function pointer.
275    * @return Whether the scale constraint function of an item is available or not
276    */
277   virtual bool GetScaleConstraint(unsigned int itemId, Vector3Function& constraint) const = 0;
278
279   /**
280    * @brief Retrieve the color constraint function of an item in the layout.
281    *
282    * The constraint will be applied when the item is created or the layout is activated.
283    * @param[in] itemId The ID of an item in the layout.
284    * @param[out] constraint The color constraint function of an item, or an uninitialized function pointer.
285    * @return Whether the color constraint function of an item is available or not
286    */
287   virtual bool GetColorConstraint(unsigned int itemId, Vector4Function& constraint) const = 0;
288
289   /**
290    * @brief Retrieve the visibility constraint function of an item in the layout.
291    *
292    * The constraint will be applied when the item is created or the layout is activated.
293    * @param[in] itemId The ID of an item in the layout.
294    * @param[out] constraint The visibility constraint function of an item, or an uninitialized function pointer.
295    * @return Whether the visibility constraint function of an item is available or not
296    */
297   virtual bool GetVisibilityConstraint(unsigned int itemId, BoolFunction& constraint) const = 0;
298
299   /**
300    * @brief Query the scroll direction of the layout.
301    *
302    * When an input gesture follows this direction, the layout-position of items will be increased.
303    * If the input gesture points in the opposite direction, then the layout-positions will decrease.
304    * @return The scroll direction in degrees.
305    */
306   virtual Degree GetScrollDirection() const = 0;
307
308   /**
309    * @brief Query the scroll speed factor of the layout while dragging.
310    *
311    * This factor is used by the layout to customise its scroll speed while dragging.
312    * The factor will be multiplied with the scroll distance of how many pixels in actor coordinate,
313    * and the layout position of the actors in ItemView will be moved by this result.
314    * For example, when the speed factor is 0.01, if the scroll distance is 100 pixels, the layout
315    * position of actors will be moved by 1.
316    * Therefore, the bigger the factor is, the faster the scroll speed will be.
317    *
318    * @return The scroll speed factor of the layout.
319    */
320   virtual float GetScrollSpeedFactor() const = 0;
321
322   /**
323    * @brief Query the maximum swipe speed in pixels per second.
324    *
325    * Swipe gestures will be clamped when exceeding this speed limit.
326    * @return speed The maximum swipe speed.
327    */
328   virtual float GetMaximumSwipeSpeed() const = 0;
329
330   /**
331    * @brief Get the duration of the flick animation in second.
332    *
333    * This is the time taken to animate each
334    * item to its next layout position (e.g. from 1.0 to 2.0) when a flick animation is triggered
335    * by a swipe gesture.
336    * @return The duration of the flick animation.
337    */
338   virtual float GetItemFlickAnimationDuration() const = 0;
339
340   /**
341    * @brief Gets the id of the next item for KeyboardFocusManager to focus on depending on the inputted item ID.
342    *
343    * @param[in] itemID The current focused item
344    * @param[in] maxItems The maximum number of items in the list
345    * @param[in] direction The directional key pressed on the keyboard
346    * @param[in] loopEnabled Whether the KeyboardFocusManager is set to wrap around between first and last item
347    * @return The next item ID.
348    */
349   DALI_IMPORT_API virtual int GetNextFocusItemID(int itemID, int maxItems, Dali::Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled);
350
351   /**
352    * @brief Query the flick speed factor of the layout while swipping.
353    *
354    * This factor is used by the layout to customise its scroll speed while swiping.
355    * The factor will be multiplied with the scroll distance of how many pixels in actor coordinate,
356    * and the layout position of the actors in ItemView will be moved by this result.
357    * For example, when the speed factor is 0.01, if the scroll distance is 100 pixels, the layout
358    * position of actors will be moved by 1.
359    * Therefore, the bigger the factor is, the faster the flick speed will be.
360    *
361    * @return The scroll speed factor of the layout.
362    */
363   DALI_IMPORT_API virtual float GetFlickSpeedFactor() const;
364
365   /*
366    * @brief Applies constraints defined by the layout to an actor.
367    *
368    * @param[in] actor The actor to constrain.
369    * @param[in] itemId The ID of the item represented by the actor.
370    * @param[in] durationSeconds The time taken to fully constrain the actors.
371    * @param[in] itemViewActor The item view instance which requests the application of constraints.
372    */
373   DALI_IMPORT_API virtual void ApplyConstraints( Actor& actor, const int itemId, const float durationSeconds, const Actor& itemViewActor );
374
375   /**
376    * @brief Gets the position of a given item
377    *
378    * @param[in] itemID id of the item we want to get its position
379    * @param[in] currentLayoutPosition the current layout position of the item view instance
380    * @param[in] layoutSize the current size of the item view instance
381    * @return The item position (x,y,z)
382    */
383   DALI_IMPORT_API virtual Vector3 GetItemPosition(int itemID, float currentLayoutPosition, const Vector3& layoutSize) const;
384
385   /**
386    * @brief Set the alpha function used when applying constraints
387    *
388    * @param[in] func The alpha function to use.
389    */
390   DALI_IMPORT_API void SetAlphaFunction(AlphaFunction func);
391
392   /**
393    * @brief Retrieve the alpha function used when applying constraints
394    *
395    * @return The alpha function.
396    */
397   DALI_IMPORT_API AlphaFunction GetAlphaFunction() const;
398
399 protected:
400
401   /**
402    * @brief Create a new ItemLayout; Only derived versions are instantiatable.
403    */
404   DALI_IMPORT_API ItemLayout();
405
406 protected:
407
408   ControlOrientation::Type mOrientation;   ///< the orientation of the layout.
409   AlphaFunction            mAlphaFunction; ///< Alpha function to be applied when removing/adding constraints
410   Handle                   mWeightObject;  ///< Weight object gets created to apply the constraints over a certain time
411 };
412
413 } // namespace Toolkit
414
415 } // namespace Dali
416
417 #endif // __DALI_TOOLKIT_ITEM_LAYOUT_H__