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