1 #ifndef __DALI_CONSTRAINT_H__
2 #define __DALI_CONSTRAINT_H__
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
21 * @addtogroup CAPI_DALI_ANIMATION_MODULE
26 #include <boost/any.hpp>
27 #include <boost/function.hpp>
30 #include <dali/public-api/animation/alpha-functions.h>
31 #include <dali/public-api/animation/constraint-source.h>
32 #include <dali/public-api/animation/interpolator-functions.h>
33 #include <dali/public-api/object/handle.h>
34 #include <dali/public-api/object/property.h>
35 #include <dali/public-api/object/property-input.h>
37 namespace Dali DALI_IMPORT_API
41 namespace Internal DALI_INTERNAL
47 * @brief An abstract base class for Constraints.
48 * This can be used to constrain a property of an actor, after animations have been applied.
49 * Constraints are applied in the following order:
50 * - Constraints are applied to on-stage actors in a depth-first traversal.
51 * - For each actor, the constraints are applied in the same order as the calls to Actor::ApplyConstraint().
52 * - Constraints are not applied to off-stage actors.
54 class DALI_IMPORT_API Constraint : public BaseHandle
58 typedef boost::any AnyFunction; ///< Generic function pointer for constraint and interpolator functions
61 * @brief The action that will happen when the constraint is removed.
63 * Constraints can be applied gradually; see SetApplyTime() for more details.
64 * When a constraint is fully-applied the final value may be "baked" i.e. saved permanently.
65 * Alternatively the constrained value may be discarded when the constraint is removed.
69 Bake, ///< When the constraint is fully-applied, the constrained value is saved.
70 Discard ///< When the constraint is removed, the constrained value is discarded.
73 static const AlphaFunction DEFAULT_ALPHA_FUNCTION; ///< AlphaFunctions::Linear
74 static const RemoveAction DEFAULT_REMOVE_ACTION; ///< Bake
77 * @brief Create an uninitialized Constraint; this can be initialized with Constraint::New().
79 * Calling member functions with an uninitialized Dali::Object is not allowed.
84 * @brief Create a constraint which targets a property.
86 * The templated parameter P, is the type of the property to constrain.
87 * Animation will be performed using the default interpolator.
88 * @param [in] target The index of the property to constrain.
89 * @param [in] func A function which returns the constrained property value.
90 * @return The new constraint.
93 static Constraint New( Property::Index target,
94 boost::function<P (const P& current)> func )
97 PropertyTypes::Get<P>(),
99 GetDefaultInterpolator( PropertyTypes::Get<P>() ) );
103 * @brief Create a constraint which targets a property.
105 * The templated parameter P, is the type of the property to constrain.
106 * @param [in] target The index of the property to constrain.
107 * @param [in] func A function which returns the constrained property value.
108 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
109 * @return The new constraint.
112 static Constraint NewWithInterpolator( Property::Index target,
113 boost::function<P (const P& current)> func,
114 boost::function<P (const P&, const P&, float)> interpolator )
117 PropertyTypes::Get<P>(),
123 * @brief Create a constraint which targets a property.
125 * The templated parameter P, is the type of the property to constrain.
126 * Animation will be performed using the default interpolator.
127 * @param [in] target The index of the property to constrain.
128 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
129 * @param [in] func A function which returns the constrained property value.
130 * @return The new constraint.
133 static Constraint New( Property::Index target,
134 ConstraintSource source1,
135 boost::function<P (const P& current, const PropertyInput& input1)> func )
138 PropertyTypes::Get<P>(),
141 GetDefaultInterpolator( PropertyTypes::Get<P>() ) );
145 * @brief Create a constraint which targets a property.
147 * The templated parameter P, is the type of the property to constrain.
148 * @param [in] target The index of the property to constrain.
149 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
150 * @param [in] func A function which returns the constrained property value.
151 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
152 * @return The new constraint.
155 static Constraint NewWithInterpolator( Property::Index target,
156 ConstraintSource source1,
157 boost::function<P (const P& current, const PropertyInput& input1)> func,
158 boost::function<P (const P&, const P&, float)> interpolator )
161 PropertyTypes::Get<P>(),
168 * @brief Create a constraint which targets a property.
170 * The templated parameter P, is the type of the property to constrain.
171 * Animation will be performed using the default interpolator.
172 * @param [in] target The index of the property to constrain.
173 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
174 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
175 * @param [in] func A function which returns the constrained property value.
176 * @return The new constraint.
179 static Constraint New( Property::Index target,
180 ConstraintSource source1,
181 ConstraintSource source2,
182 boost::function<P (const P& current, const PropertyInput& input1, const PropertyInput& input2)> func )
185 PropertyTypes::Get<P>(),
189 GetDefaultInterpolator(PropertyTypes::Get<P>()) );
193 * @brief Create a constraint which targets a property.
195 * The templated parameter P, is the type of the property to constrain.
196 * @param [in] target The index of the property to constrain.
197 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
198 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
199 * @param [in] func A function which returns the constrained property value.
200 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
201 * @return The new constraint.
204 static Constraint NewWithInterpolator( Property::Index target,
205 ConstraintSource source1,
206 ConstraintSource source2,
207 boost::function<P (const P& current, const PropertyInput& input1, const PropertyInput& input2)> func,
208 boost::function<P (const P&, const P&, float)> interpolator )
211 PropertyTypes::Get<P>(),
219 * @brief Create a constraint which targets a property.
221 * The templated parameter P, is the type of the property to constrain.
222 * Animation will be performed using the default interpolator.
223 * @param [in] target The index of the property to constrain.
224 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
225 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
226 * @param [in] source3 The source of a property; the current value will be passed as the 4th parameter of func.
227 * @param [in] func A function which returns the constrained property value.
228 * @return The new constraint.
231 static Constraint New( Property::Index target,
232 ConstraintSource source1,
233 ConstraintSource source2,
234 ConstraintSource source3,
235 boost::function<P (const P& current, const PropertyInput& input1, const PropertyInput& input2, const PropertyInput& input3)> func )
238 PropertyTypes::Get<P>(),
243 GetDefaultInterpolator(PropertyTypes::Get<P>()) );
247 * @brief Create a constraint which targets a property.
249 * The templated parameter P, is the type of the property to constrain.
250 * @param [in] target The index of the property to constrain.
251 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
252 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
253 * @param [in] source3 The source of a property; the current value will be passed as the 4th parameter of func.
254 * @param [in] func A function which returns the constrained property value.
255 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
256 * @return The new constraint.
259 static Constraint NewWithInterpolator( Property::Index target,
260 ConstraintSource source1,
261 ConstraintSource source2,
262 ConstraintSource source3,
263 boost::function<P (const P& current, const PropertyInput& input1, const PropertyInput& input2, const PropertyInput& input3)> func,
264 boost::function<P (const P&, const P&, float)> interpolator )
267 PropertyTypes::Get<P>(),
276 * @brief Create a constraint which targets a property.
278 * The templated parameter P, is the type of the property to constrain.
279 * Animation will be performed using the default interpolator.
280 * @param [in] target The index of the property to constrain.
281 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
282 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
283 * @param [in] source3 The source of a property; the current value will be passed as the 4th parameter of func.
284 * @param [in] source4 The source of a property; the current value will be passed as the 5th parameter of func.
285 * @param [in] func A function which returns the constrained property value.
286 * @return The new constraint.
289 static Constraint New( Property::Index target,
290 ConstraintSource source1,
291 ConstraintSource source2,
292 ConstraintSource source3,
293 ConstraintSource source4,
294 boost::function<P (const P& current,
295 const PropertyInput& input1,
296 const PropertyInput& input2,
297 const PropertyInput& input3,
298 const PropertyInput& input4)> func )
301 PropertyTypes::Get<P>(),
307 GetDefaultInterpolator(PropertyTypes::Get<P>()) );
311 * @brief Create a constraint which targets a property.
313 * The templated parameter P, is the type of the property to constrain.
314 * @param [in] target The index of the property to constrain.
315 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
316 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
317 * @param [in] source3 The source of a property; the current value will be passed as the 4th parameter of func.
318 * @param [in] source4 The source of a property; the current value will be passed as the 5th parameter of func.
319 * @param [in] func A function which returns the constrained property value.
320 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
321 * @return The new constraint.
324 static Constraint NewWithInterpolator( Property::Index target,
325 ConstraintSource source1,
326 ConstraintSource source2,
327 ConstraintSource source3,
328 ConstraintSource source4,
329 boost::function<P (const P& current,
330 const PropertyInput& input1,
331 const PropertyInput& input2,
332 const PropertyInput& input3,
333 const PropertyInput& input4)> func,
334 boost::function<P (const P&, const P&, float)> interpolator )
337 PropertyTypes::Get<P>(),
347 * @brief Create a constraint which targets a property.
349 * The templated parameter P, is the type of the property to constrain.
350 * Animation will be performed using the default interpolator.
351 * @param [in] target The index of the property to constrain.
352 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
353 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
354 * @param [in] source3 The source of a property; the current value will be passed as the 4th parameter of func.
355 * @param [in] source4 The source of a property; the current value will be passed as the 5th parameter of func.
356 * @param [in] source5 The source of a property; the current value will be passed as the 6th parameter of func.
357 * @param [in] func A function which returns the constrained property value.
358 * @return The new constraint.
361 static Constraint New( Property::Index target,
362 ConstraintSource source1,
363 ConstraintSource source2,
364 ConstraintSource source3,
365 ConstraintSource source4,
366 ConstraintSource source5,
367 boost::function<P (const P& current,
368 const PropertyInput& input1,
369 const PropertyInput& input2,
370 const PropertyInput& input3,
371 const PropertyInput& input4,
372 const PropertyInput& input5)> func )
375 PropertyTypes::Get<P>(),
382 GetDefaultInterpolator(PropertyTypes::Get<P>()) );
386 * @brief Create a constraint which targets a property.
388 * The templated parameter P, is the type of the property to constrain.
389 * @param [in] target The index of the property to constrain.
390 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
391 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
392 * @param [in] source3 The source of a property; the current value will be passed as the 4th parameter of func.
393 * @param [in] source4 The source of a property; the current value will be passed as the 5th parameter of func.
394 * @param [in] source5 The source of a property; the current value will be passed as the 6th parameter of func.
395 * @param [in] func A function which returns the constrained property value.
396 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
397 * @return The new constraint.
400 static Constraint NewWithInterpolator( Property::Index target,
401 ConstraintSource source1,
402 ConstraintSource source2,
403 ConstraintSource source3,
404 ConstraintSource source4,
405 ConstraintSource source5,
406 boost::function<P (const P& current,
407 const PropertyInput& input1,
408 const PropertyInput& input2,
409 const PropertyInput& input3,
410 const PropertyInput& input4,
411 const PropertyInput& input5)> func,
412 boost::function<P (const P&, const P&, float)> interpolator )
415 PropertyTypes::Get<P>(),
426 * @brief Create a constraint which targets a property.
428 * The templated parameter P, is the type of the property to constrain.
429 * Animation will be performed using the default interpolator.
430 * @param [in] target The index of the property to constrain.
431 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
432 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
433 * @param [in] source3 The source of a property; the current value will be passed as the 4th parameter of func.
434 * @param [in] source4 The source of a property; the current value will be passed as the 5th parameter of func.
435 * @param [in] source5 The source of a property; the current value will be passed as the 6th parameter of func.
436 * @param [in] source6 The source of a property; the current value will be passed as the 7th parameter of func.
437 * @param [in] func A function which returns the constrained property value.
438 * @return The new constraint.
441 static Constraint New( Property::Index target,
442 ConstraintSource source1,
443 ConstraintSource source2,
444 ConstraintSource source3,
445 ConstraintSource source4,
446 ConstraintSource source5,
447 ConstraintSource source6,
448 boost::function<P (const P& current,
449 const PropertyInput& input1,
450 const PropertyInput& input2,
451 const PropertyInput& input3,
452 const PropertyInput& input4,
453 const PropertyInput& input5,
454 const PropertyInput& input6)> func )
457 PropertyTypes::Get<P>(),
465 GetDefaultInterpolator(PropertyTypes::Get<P>()) );
469 * @brief Create a constraint which targets a property.
471 * The templated parameter P, is the type of the property to constrain.
472 * @param [in] target The index of the property to constrain.
473 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
474 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
475 * @param [in] source3 The source of a property; the current value will be passed as the 4th parameter of func.
476 * @param [in] source4 The source of a property; the current value will be passed as the 5th parameter of func.
477 * @param [in] source5 The source of a property; the current value will be passed as the 6th parameter of func.
478 * @param [in] source6 The source of a property; the current value will be passed as the 7th parameter of func.
479 * @param [in] func A function which returns the constrained property value.
480 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
481 * @return The new constraint.
484 static Constraint NewWithInterpolator( Property::Index target,
485 ConstraintSource source1,
486 ConstraintSource source2,
487 ConstraintSource source3,
488 ConstraintSource source4,
489 ConstraintSource source5,
490 ConstraintSource source6,
491 boost::function<P (const P& current,
492 const PropertyInput& input1,
493 const PropertyInput& input2,
494 const PropertyInput& input3,
495 const PropertyInput& input4,
496 const PropertyInput& input5,
497 const PropertyInput& input6)> func,
498 boost::function<P (const P&, const P&, float)> interpolator )
501 PropertyTypes::Get<P>(),
513 * @brief Downcast an Object handle to Constraint handle.
515 * If handle points to a Constraint object the
516 * downcast produces valid handle. If not the returned handle is left uninitialized.
517 * @param[in] handle to An object
518 * @return handle to a Constraint object or an uninitialized handle
520 static Constraint DownCast( BaseHandle handle );
526 virtual ~Constraint();
529 * @copydoc Dali::BaseHandle::operator=
531 using BaseHandle::operator=;
534 * @brief Set the time taken for the constraint to be fully applied.
536 * The default is zero, meaning that the constraint is applied immediately.
537 * @param [in] timePeriod The constraint will be applied during this time period.
539 void SetApplyTime( TimePeriod timePeriod );
542 * @brief Retrieve the time taken for the constraint to be fully applied.
544 * @return The apply time.
546 TimePeriod GetApplyTime() const;
549 * @brief Set the time taken for the constraint to be fully removed.
551 * The default is zero, meaning that the constraint is removed immediately.
552 * @param [in] timePeriod The constraint will be removed during this time period.
554 void SetRemoveTime( TimePeriod timePeriod );
557 * @brief Retrieve the time taken for the constraint to be fully removed.
559 * @return The remove time.
561 TimePeriod GetRemoveTime() const;
564 * @brief Set the alpha function for a constraint; the default is AlphaFunctions::Linear.
566 * @param [in] func The alpha function to use when applying/removing the constraint.
568 void SetAlphaFunction( AlphaFunction func );
571 * @brief Retrieve the alpha function of a constraint.
573 * @return The function.
575 AlphaFunction GetAlphaFunction();
578 * @brief Set whether the constraint will "bake" a value when fully-applied.
580 * Otherwise the constrained value will be discarded, when the constraint is removed.
581 * The default value is Constraint::Bake.
582 * @param[in] action The remove-action.
584 void SetRemoveAction( RemoveAction action );
587 * @brief Query whether the constraint will "bake" a value when fully-applied.
589 * Otherwise the constrained value will be discarded, when the constraint is removed.
590 * @return The apply-action.
592 RemoveAction GetRemoveAction() const;
594 public: // Not intended for use by Application developers
597 * @brief This constructor is used by Dali New() methods
598 * @param [in] constraint A pointer to a newly allocated Dali resource
600 explicit DALI_INTERNAL Constraint( Internal::Constraint* constraint );
602 private: // Not intended for use by Application developers
605 * @brief Construct a new constraint which targets a property.
607 * @param [in] target The index of the property to constrain.
608 * @param [in] targetType The type of the constrained property.
609 * @param [in] func The constraint function.
610 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
611 * @return The new constraint.
613 static Constraint New( Property::Index target,
614 Property::Type targetType,
616 AnyFunction interpolator );
619 * @brief Construct a new constraint which targets a property.
621 * @param [in] target The index of the property to constrain.
622 * @param [in] targetType The type of the constrained property.
623 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
624 * @param [in] func The constraint function.
625 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
626 * @return The new constraint.
628 static Constraint New( Property::Index target,
629 Property::Type targetType,
630 ConstraintSource source1,
632 AnyFunction interpolator );
635 * @brief Construct a new constraint which targets a property.
637 * @param [in] target The index of the property to constrain.
638 * @param [in] targetType The type of the constrained property.
639 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
640 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
641 * @param [in] func The constraint function.
642 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
643 * @return The new constraint.
645 static Constraint New( Property::Index target,
646 Property::Type targetType,
647 ConstraintSource source1,
648 ConstraintSource source2,
650 AnyFunction interpolator );
653 * @brief Construct a new constraint which targets a property.
655 * @param [in] target The index of the property to constrain.
656 * @param [in] targetType The type of the constrained property.
657 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
658 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
659 * @param [in] source3 The source of a property; the current value will be passed as the 4th parameter of func.
660 * @param [in] func The constraint function.
661 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
662 * @return The new constraint.
664 static Constraint New( Property::Index target,
665 Property::Type targetType,
666 ConstraintSource source1,
667 ConstraintSource source2,
668 ConstraintSource source3,
670 AnyFunction interpolator );
673 * @brief Construct a new constraint which targets a property.
675 * @param [in] target The index of the property to constrain.
676 * @param [in] targetType The type of the constrained property.
677 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
678 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
679 * @param [in] source3 The source of a property; the current value will be passed as the 4th parameter of func.
680 * @param [in] source4 The source of a property; the current value will be passed as the 5th parameter of func.
681 * @param [in] func The constraint function.
682 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
683 * @return The new constraint.
685 static Constraint New( Property::Index target,
686 Property::Type targetType,
687 ConstraintSource source1,
688 ConstraintSource source2,
689 ConstraintSource source3,
690 ConstraintSource source4,
692 AnyFunction interpolator );
695 * @brief Construct a new constraint which targets a property.
697 * @param [in] target The index of the property to constrain.
698 * @param [in] targetType The type of the constrained property.
699 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
700 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
701 * @param [in] source3 The source of a property; the current value will be passed as the 4th parameter of func.
702 * @param [in] source4 The source of a property; the current value will be passed as the 5th parameter of func.
703 * @param [in] source5 The source of a property; the current value will be passed as the 6th parameter of func.
704 * @param [in] func The constraint function.
705 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
706 * @return The new constraint.
708 static Constraint New( Property::Index target,
709 Property::Type targetType,
710 ConstraintSource source1,
711 ConstraintSource source2,
712 ConstraintSource source3,
713 ConstraintSource source4,
714 ConstraintSource source5,
716 AnyFunction interpolator );
719 * @brief Construct a new constraint which targets a property.
721 * @param [in] target The index of the property to constrain.
722 * @param [in] targetType The type of the constrained property.
723 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
724 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
725 * @param [in] source3 The source of a property; the current value will be passed as the 4th parameter of func.
726 * @param [in] source4 The source of a property; the current value will be passed as the 5th parameter of func.
727 * @param [in] source5 The source of a property; the current value will be passed as the 6th parameter of func.
728 * @param [in] source6 The source of a property; the current value will be passed as the 7th parameter of func.
729 * @param [in] func The constraint function.
730 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
731 * @return The new constraint.
733 static Constraint New( Property::Index target,
734 Property::Type targetType,
735 ConstraintSource source1,
736 ConstraintSource source2,
737 ConstraintSource source3,
738 ConstraintSource source4,
739 ConstraintSource source5,
740 ConstraintSource source6,
742 AnyFunction interpolator );
750 #endif // __DALI_CONSTRAINT_H__