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