1 #ifndef __DALI_TOOLKIT_ITEM_LAYOUT_H__
2 #define __DALI_TOOLKIT_ITEM_LAYOUT_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <dali/dali.h>
23 #include <dali-toolkit/public-api/enums.h>
24 #include <dali-toolkit/public-api/controls/control.h>
26 namespace Dali DALI_IMPORT_API
34 typedef IntrusivePtr<ItemLayout> ItemLayoutPtr; ///< Pointer to a Dali::Toolkit::ItemLayout object
36 typedef std::vector<ItemLayoutPtr> ItemLayoutContainer; ///< Container of Dali::Toolkit::ItemLayout objects
37 typedef ItemLayoutContainer::iterator ItemLayoutIter; ///< Iterator for Dali::Toolkit::ItemLayoutContainer
38 typedef ItemLayoutContainer::const_iterator ItemLayoutConstIter; ///< Const Iterator for Dali::Toolkit::ItemLayoutContainer
42 * @brief A support class for managing ranges of items.
47 * @brief Create a range of item identifiers.
49 * @param[in] beginItem The first item within the range.
50 * @param[in] endItem The past-the-end item.
52 ItemRange(unsigned int beginItem, unsigned int endItem)
59 * @brief Copy Constructor.
61 * @param[in] copy ItemRange we should copy from.
63 ItemRange(const ItemRange& copy)
70 * @brief Assignment operator.
72 * @param[in] range The Range to assign from.
73 * @return The updated range.
75 ItemRange& operator=(const ItemRange& range)
83 * @brief Test whether an item is within the range.
85 * @param[in] itemId The item identifier.
86 * @return True if the item is within the range.
88 bool Within(unsigned int itemId)
90 return itemId >= begin &&
95 * @brief Create the intersection of two ranges.
97 * @param[in] second The second range.
98 * @return The intersection.
100 ItemRange Intersection(const ItemRange& second)
102 ItemRange intersection(0u, 0u);
104 // If the ranges intersect
105 if ( (begin < second.end && end > second.begin) ||
106 (second.begin < end && second.end > begin) )
108 intersection.begin = std::max(begin, second.begin);
109 intersection.end = std::min(end, second.end);
115 unsigned int begin; ///< The start of the range
116 unsigned int end; ///< The end of the range
120 * @brief An ItemLayout describes the constraints which are imposed on items in the layout.
122 * - Potentially visible items are represented by Actors, created for ItemView by the ItemFactory.
123 * - Constraints are applied after ItemView activates a layout.
125 * An ItemLayout also describes the direction of input gestures, used to scroll through the layout.
126 * Whilst scrolling, the layout provides a range of items that are within a layout-area (3D bounding volume).
128 class ItemLayout : public RefObject
132 /// @brief Function signature of a boolean constraint
133 typedef boost::function<bool (const bool& current, const float& layoutPosition, const float& scrollSpeed, const Vector3& layoutSize)> BoolFunction;
135 /// @brief Function signature of a Vector3 constraint
136 typedef boost::function<Vector3 (const Vector3& current, const float& layoutPosition, const float& scrollSpeed, const Vector3& layoutSize)> Vector3Function;
138 /// @brief Function signature of a Vector4 constraint
139 typedef boost::function<Vector4 (const Vector4& current, const float& layoutPosition, const float& scrollSpeed, const Vector3& layoutSize)> Vector4Function;
141 /// @brief Function signature of a Quaternion constraint
142 typedef boost::function<Quaternion (const Quaternion& current, const float& layoutPosition, const float& scrollSpeed, const Vector3& layoutSize)> QuaternionFunction;
145 * @brief Virtual destructor.
147 virtual ~ItemLayout();
150 * @brief Set the orientation of the layout.
152 * @param[in] orientation The orientation of the layout.
154 void SetOrientation(ControlOrientation::Type orientation);
157 * @brief Query the orientation of the layout.
159 * @return the orientation of the layout.
161 ControlOrientation::Type GetOrientation() const;
164 * @brief Query the minimum valid layout position; this is a negative value.
166 * When scrolling, the first item will move within the range 0 to GetMinimumLayoutPosition().
167 * @param[in] numberOfItems The current number of items in the layout.
168 * @param[in] layoutSize The size of the layout area.
169 * @return The minimum layout position.
171 virtual float GetMinimumLayoutPosition(unsigned int numberOfItems, Vector3 layoutSize) const = 0;
174 * @brief Query the closest anchor position for the given layout position.
176 * This anchor position is the position where all the items in the layout are aligned to
177 * their rounded layout positions in integer.
178 * @param[in] layoutPosition The layout position.
179 * @return The closest anchor position for the given layout position.
181 virtual float GetClosestAnchorPosition(float layoutPosition) const = 0;
184 * @brief Query the layout position for the first item in the layout to move to when the layout
185 * needs to scroll to a particular item.
187 * @param[in] itemId The ID of an item in the layout.
188 * @return The layout position for the first item in the layout to move to.
190 virtual float GetItemScrollToPosition(unsigned int itemId) const = 0;
193 * @brief Query the items within a given layout-area.
195 * @param[in] firstItemPosition The layout-position of the first item in the layout.
196 * @param[in] layoutSize The size of the layout area.
197 * @return The ID of the first & last visible item.
199 virtual ItemRange GetItemsWithinArea(float firstItemPosition, Vector3 layoutSize) const = 0;
202 * @brief Get the closest layout position to bring an item onto the screen.
204 * If the item is already fully on the screen this function will
205 * return the current layout position.
207 * This function is used by systems such as KeyboardFocusManager to
208 * bring the next focusable item into view and all layout
209 * implementations should provide their own version of this function
210 * to ensure proper functionality of internal toolkit systems.
212 * @param[in] itemID id of the item to bring within the viewable screen area
213 * @param[in] currentLayoutPosition the current layout position of the item view instance
214 * @param[in] layoutSize the current size of the item view instance
215 * @return The layout position
217 virtual float GetClosestOnScreenLayoutPosition(int itemID, float currentLayoutPosition, const Vector3& layoutSize);
220 * @brief Query the number of items that should be reserved, for scrolling purposes.
222 * @param[in] layoutSize The size of the layout area.
223 * @return The number of extra items. ItemView will populate itself with actors within the layout-area
224 * (see GetItemsWithinArea), plus this number of additional items on either-side.
226 virtual unsigned int GetReserveItemCount(Vector3 layoutSize) const = 0;
229 * @brief Retrieve the target size of an item in the layout.
231 * @note layout-position is not provided as a parameter, since applying size constraints is not recommended.
232 * Animating to target-sizes is preferable, since this allows controls to perform layouting without constraints.
233 * @param[in] itemId The ID of an item in the layout.
234 * @param[in] layoutSize The layout size
235 * @param[out] itemSize The target size of an item, or an uninitialized value.
236 * @return Whether the item size is available or not
238 virtual bool GetItemSize(unsigned int itemId, Vector3 layoutSize, Vector3& itemSize) const = 0;
241 * @brief Retrieve the resize animation in the layout.
243 * @note This allows the layout to provide its own resize animation.
244 * @param[in] animation The resize animation, not owned by the layout
245 * @param[in] actor The actor to animate
246 * @param [in] size The target size.
247 * @param [in] durationSeconds The duration of the resizing.
249 virtual void GetResizeAnimation(Animation& animation, Actor actor, Vector3 size, float durationSeconds) const = 0;
252 * @brief Retrieve the position constraint function of an item in the layout.
254 * The constraint will be applied when the item is created or the layout is activated.
255 * @param[in] itemId The ID of an item in the layout.
256 * @param[out] constraint The position constraint function of an item, or an uninitialized function pointer.
257 * @return Whether the position constraint function of an item is available or not
259 virtual bool GetPositionConstraint(unsigned int itemId, Vector3Function& constraint) const = 0;
262 * @brief Retrieve the rotation constraint function of an item in the layout.
264 * The constraint will be applied when the item is created or the layout is activated.
265 * @param[in] itemId The ID of an item in the layout.
266 * @param[out] constraint The rotation constraint function of an item, or an uninitialized function pointer.
267 * @return Whether the rotation constraint function of an item is available or not
269 virtual bool GetRotationConstraint(unsigned int itemId, QuaternionFunction& constraint) const = 0;
272 * @brief Retrieve the scale constraint function of an item in the layout.
274 * The constraint will be applied when the item is created or the layout is activated.
275 * @param[in] itemId The ID of an item in the layout.
276 * @param[out] constraint The scale constraint function of an item, or an uninitialized function pointer.
277 * @return Whether the scale constraint function of an item is available or not
279 virtual bool GetScaleConstraint(unsigned int itemId, Vector3Function& constraint) const = 0;
282 * @brief Retrieve the color constraint function of an item in the layout.
284 * The constraint will be applied when the item is created or the layout is activated.
285 * @param[in] itemId The ID of an item in the layout.
286 * @param[out] constraint The color constraint function of an item, or an uninitialized function pointer.
287 * @return Whether the color constraint function of an item is available or not
289 virtual bool GetColorConstraint(unsigned int itemId, Vector4Function& constraint) const = 0;
292 * @brief Retrieve the visibility constraint function of an item in the layout.
294 * The constraint will be applied when the item is created or the layout is activated.
295 * @param[in] itemId The ID of an item in the layout.
296 * @param[out] constraint The visibility constraint function of an item, or an uninitialized function pointer.
297 * @return Whether the visibility constraint function of an item is available or not
299 virtual bool GetVisibilityConstraint(unsigned int itemId, BoolFunction& constraint) const = 0;
302 * @brief Query the scroll direction of the layout.
304 * When an input gesture follows this direction, the layout-position of items will be increased.
305 * If the input gesture points in the opposite direction, then the layout-positions will decrease.
306 * @return The scroll direction in degrees.
308 virtual Degree GetScrollDirection() const = 0;
311 * @brief Tells scroll components how to interpolate our logical scroll position as a screen x/y direction.
313 * Application developer wants to use -ve y, +ve x as up direction and +ve y, -ve x as down direction scroll values in a
314 * vertical scroll type effect (SpiralLayout). This means that scroll bar/overshoot indicator should be affected by y-axis.
315 * 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
316 * 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
318 * This function is used by numerous objects such as scroll indicators and scroll overshoot indicators and all
319 * layout implementations should provide their own version of this function to ensure proper functionality of
320 * internal toolkit systems.
322 * @param[out] scrollHint Vector2 describing how x and y scroll values should be used for x-axis scrolling
324 virtual void GetXAxisScrollHint(Vector2& scrollHint) const;
327 * @brief Tells scroll components how to interpolate our logical scroll position as a screen x/y direction.
329 * Application developer wants to use -ve y, +ve x as up direction and +ve y, -ve x as down direction scroll values in a
330 * vertical scroll type effect (SpiralLayout). This means that scroll bar/overshoot indicator should be affected by y-axis.
331 * 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
332 * 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
334 * This function is used by numerous objects such as scroll indicators and scroll overshoot indicators and all
335 * layout implementations should provide their own version of this function to ensure proper functionality of
336 * internal toolkit systems.
338 * @param[out] scrollHint Vector2 describing how x and y scroll values should be used for y-axis scrolling
340 virtual void GetYAxisScrollHint(Vector2& scrollHint) const;
343 * @brief Query the scroll speed factor of the layout while dragging.
345 * This factor is used by the layout to customise its scroll speed while dragging.
346 * The factor will be multiplied with the scroll distance of how many pixels in actor coordinate,
347 * and the layout position of the actors in ItemView will be moved by this result.
348 * For example, when the speed factor is 0.01, if the scroll distance is 100 pixels, the layout
349 * position of actors will be moved by 1.
350 * Therefore, the bigger the factor is, the faster the scroll speed will be.
352 * @return The scroll speed factor of the layout.
354 virtual float GetScrollSpeedFactor() const = 0;
357 * @brief Query the maximum swipe speed in pixels per second.
359 * Swipe gestures will be clamped when exceeding this speed limit.
360 * @return speed The maximum swipe speed.
362 virtual float GetMaximumSwipeSpeed() const = 0;
365 * @brief Get the duration of the flick animation in second.
367 * This is the time taken to animate each
368 * item to its next layout position (e.g. from 1.0 to 2.0) when a flick animation is triggered
369 * by a swipe gesture.
370 * @return The duration of the flick animation.
372 virtual float GetItemFlickAnimationDuration() const = 0;
375 * @brief Gets the id of the next item for KeyboardFocusManager to focus on depending on the inputted item ID.
377 * @param[in] itemID The current focused item
378 * @param[in] maxItems The maximum number of items in the list
379 * @param[in] direction The directional key pressed on the keyboard
380 * @param[in] loopEnabled Whether the KeyboardFocusManager is set to wrap around between first and last item
381 * @return The next item ID.
383 virtual int GetNextFocusItemID(int itemID, int maxItems, Dali::Toolkit::Control::KeyboardFocusNavigationDirection direction, bool loopEnabled);
386 * @brief Query the flick speed factor of the layout while swipping.
388 * This factor is used by the layout to customise its scroll speed while swiping.
389 * The factor will be multiplied with the scroll distance of how many pixels in actor coordinate,
390 * and the layout position of the actors in ItemView will be moved by this result.
391 * For example, when the speed factor is 0.01, if the scroll distance is 100 pixels, the layout
392 * position of actors will be moved by 1.
393 * Therefore, the bigger the factor is, the faster the flick speed will be.
395 * @return The scroll speed factor of the layout.
397 virtual float GetFlickSpeedFactor() const;
400 * @brief Applies constraints defined by the layout to an actor.
402 * @param[in] actor The actor to constrain.
403 * @param[in] itemId The ID of the item represented by the actor.
404 * @param[in] durationSeconds The time taken to fully constrain the actors.
405 * @param[in] scrollPositionObject The object which provides the layout position property.
406 * @param[in] itemViewActor The item view instance which requests the application of constraints.
408 virtual void ApplyConstraints( Actor& actor, const int itemId, const float durationSeconds, Constrainable scrollPositionObject, const Actor& itemViewActor );
411 * @brief Gets the position of a given item
413 * @param[in] itemID id of the item we want to get its position
414 * @param[in] currentLayoutPosition the current layout position of the item view instance
415 * @param[in] layoutSize the current size of the item view instance
416 * @return The item position (x,y,z)
418 virtual Vector3 GetItemPosition(int itemID, float currentLayoutPosition, const Vector3& layoutSize) const;
421 * @brief Set the alpha function used when applying constraints
423 * @param[in] func The alpha function to use.
425 void SetAlphaFunction(AlphaFunction func);
428 * @brief Retrieve the alpha function used when applying constraints
430 * @return The alpha function.
432 AlphaFunction GetAlphaFunction() const;
437 * @brief Create a new ItemLayout; Only derived versions are instantiatable.
443 ControlOrientation::Type mOrientation; ///< the orientation of the layout.
444 AlphaFunction mAlphaFunction; ///<Alpha function to be applied when removing/adding constraints
447 } // namespace Toolkit
451 #endif // __DALI_TOOLKIT_ITEM_LAYOUT_H__