1 #ifndef __DALI_CONSTRAINT_H__
2 #define __DALI_CONSTRAINT_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 <boost/function.hpp>
25 #include <dali/public-api/animation/alpha-functions.h>
26 #include <dali/public-api/animation/constraint-source.h>
27 #include <dali/public-api/animation/interpolator-functions.h>
28 #include <dali/public-api/object/any.h>
29 #include <dali/public-api/object/handle.h>
30 #include <dali/public-api/object/property.h>
31 #include <dali/public-api/object/property-input.h>
33 namespace Dali DALI_IMPORT_API
37 namespace Internal DALI_INTERNAL
43 * @brief An abstract base class for Constraints.
44 * This can be used to constrain a property of an actor, after animations have been applied.
45 * Constraints are applied in the following order:
46 * - Constraints are applied to on-stage actors in a depth-first traversal.
47 * - For each actor, the constraints are applied in the same order as the calls to Actor::ApplyConstraint().
48 * - Constraints are not applied to off-stage actors.
50 class DALI_IMPORT_API Constraint : public BaseHandle
54 typedef Any AnyFunction; ///< Generic function pointer for constraint and interpolator functions
57 * @brief The action that will happen when the constraint is removed.
59 * Constraints can be applied gradually; see SetApplyTime() for more details.
60 * When a constraint is fully-applied the final value may be "baked" i.e. saved permanently.
61 * Alternatively the constrained value may be discarded when the constraint is removed.
65 Bake, ///< When the constraint is fully-applied, the constrained value is saved.
66 Discard ///< When the constraint is removed, the constrained value is discarded.
69 static const AlphaFunction DEFAULT_ALPHA_FUNCTION; ///< AlphaFunctions::Linear
70 static const RemoveAction DEFAULT_REMOVE_ACTION; ///< Bake
73 * @brief Create an uninitialized Constraint; this can be initialized with Constraint::New().
75 * Calling member functions with an uninitialized Dali::Object is not allowed.
80 * @brief Create a constraint which targets a property.
82 * The templated parameter P, is the type of the property to constrain.
83 * Animation will be performed using the default interpolator.
84 * @param [in] target The index of the property to constrain.
85 * @param [in] func A function which returns the constrained property value.
86 * @return The new constraint.
89 static Constraint New( Property::Index target,
90 boost::function<P (const P& current)> func )
93 PropertyTypes::Get<P>(),
95 GetDefaultInterpolator( PropertyTypes::Get<P>() ) );
99 * @brief Create a constraint which targets a property.
101 * The templated parameter P, is the type of the property to constrain.
102 * @param [in] target The index of the property to constrain.
103 * @param [in] func A function which returns the constrained property value.
104 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
105 * @return The new constraint.
108 static Constraint NewWithInterpolator( Property::Index target,
109 boost::function<P (const P& current)> func,
110 boost::function<P (const P&, const P&, float)> interpolator )
113 PropertyTypes::Get<P>(),
119 * @brief Create a constraint which targets a property.
121 * The templated parameter P, is the type of the property to constrain.
122 * Animation will be performed using the default interpolator.
123 * @param [in] target The index of the property to constrain.
124 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
125 * @param [in] func A function which returns the constrained property value.
126 * @return The new constraint.
129 static Constraint New( Property::Index target,
130 ConstraintSource source1,
131 boost::function<P (const P& current, const PropertyInput& input1)> func )
134 PropertyTypes::Get<P>(),
137 GetDefaultInterpolator( PropertyTypes::Get<P>() ) );
141 * @brief Create a constraint which targets a property.
143 * The templated parameter P, is the type of the property to constrain.
144 * @param [in] target The index of the property to constrain.
145 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
146 * @param [in] func A function which returns the constrained property value.
147 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
148 * @return The new constraint.
151 static Constraint NewWithInterpolator( Property::Index target,
152 ConstraintSource source1,
153 boost::function<P (const P& current, const PropertyInput& input1)> func,
154 boost::function<P (const P&, const P&, float)> interpolator )
157 PropertyTypes::Get<P>(),
164 * @brief Create a constraint which targets a property.
166 * The templated parameter P, is the type of the property to constrain.
167 * Animation will be performed using the default interpolator.
168 * @param [in] target The index of the property to constrain.
169 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
170 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
171 * @param [in] func A function which returns the constrained property value.
172 * @return The new constraint.
175 static Constraint New( Property::Index target,
176 ConstraintSource source1,
177 ConstraintSource source2,
178 boost::function<P (const P& current, const PropertyInput& input1, const PropertyInput& input2)> func )
181 PropertyTypes::Get<P>(),
185 GetDefaultInterpolator(PropertyTypes::Get<P>()) );
189 * @brief Create a constraint which targets a property.
191 * The templated parameter P, is the type of the property to constrain.
192 * @param [in] target The index of the property to constrain.
193 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
194 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
195 * @param [in] func A function which returns the constrained property value.
196 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
197 * @return The new constraint.
200 static Constraint NewWithInterpolator( Property::Index target,
201 ConstraintSource source1,
202 ConstraintSource source2,
203 boost::function<P (const P& current, const PropertyInput& input1, const PropertyInput& input2)> func,
204 boost::function<P (const P&, const P&, float)> interpolator )
207 PropertyTypes::Get<P>(),
215 * @brief Create a constraint which targets a property.
217 * The templated parameter P, is the type of the property to constrain.
218 * Animation will be performed using the default interpolator.
219 * @param [in] target The index of the property to constrain.
220 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
221 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
222 * @param [in] source3 The source of a property; the current value will be passed as the 4th parameter of func.
223 * @param [in] func A function which returns the constrained property value.
224 * @return The new constraint.
227 static Constraint New( Property::Index target,
228 ConstraintSource source1,
229 ConstraintSource source2,
230 ConstraintSource source3,
231 boost::function<P (const P& current, const PropertyInput& input1, const PropertyInput& input2, const PropertyInput& input3)> func )
234 PropertyTypes::Get<P>(),
239 GetDefaultInterpolator(PropertyTypes::Get<P>()) );
243 * @brief Create a constraint which targets a property.
245 * The templated parameter P, is the type of the property to constrain.
246 * @param [in] target The index of the property to constrain.
247 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
248 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
249 * @param [in] source3 The source of a property; the current value will be passed as the 4th parameter of func.
250 * @param [in] func A function which returns the constrained property value.
251 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
252 * @return The new constraint.
255 static Constraint NewWithInterpolator( Property::Index target,
256 ConstraintSource source1,
257 ConstraintSource source2,
258 ConstraintSource source3,
259 boost::function<P (const P& current, const PropertyInput& input1, const PropertyInput& input2, const PropertyInput& input3)> func,
260 boost::function<P (const P&, const P&, float)> interpolator )
263 PropertyTypes::Get<P>(),
272 * @brief Create a constraint which targets a property.
274 * The templated parameter P, is the type of the property to constrain.
275 * Animation will be performed using the default interpolator.
276 * @param [in] target The index of the property to constrain.
277 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
278 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
279 * @param [in] source3 The source of a property; the current value will be passed as the 4th parameter of func.
280 * @param [in] source4 The source of a property; the current value will be passed as the 5th parameter of func.
281 * @param [in] func A function which returns the constrained property value.
282 * @return The new constraint.
285 static Constraint New( Property::Index target,
286 ConstraintSource source1,
287 ConstraintSource source2,
288 ConstraintSource source3,
289 ConstraintSource source4,
290 boost::function<P (const P& current,
291 const PropertyInput& input1,
292 const PropertyInput& input2,
293 const PropertyInput& input3,
294 const PropertyInput& input4)> func )
297 PropertyTypes::Get<P>(),
303 GetDefaultInterpolator(PropertyTypes::Get<P>()) );
307 * @brief Create a constraint which targets a property.
309 * The templated parameter P, is the type of the property to constrain.
310 * @param [in] target The index of the property to constrain.
311 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
312 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
313 * @param [in] source3 The source of a property; the current value will be passed as the 4th parameter of func.
314 * @param [in] source4 The source of a property; the current value will be passed as the 5th parameter of func.
315 * @param [in] func A function which returns the constrained property value.
316 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
317 * @return The new constraint.
320 static Constraint NewWithInterpolator( Property::Index target,
321 ConstraintSource source1,
322 ConstraintSource source2,
323 ConstraintSource source3,
324 ConstraintSource source4,
325 boost::function<P (const P& current,
326 const PropertyInput& input1,
327 const PropertyInput& input2,
328 const PropertyInput& input3,
329 const PropertyInput& input4)> func,
330 boost::function<P (const P&, const P&, float)> interpolator )
333 PropertyTypes::Get<P>(),
343 * @brief Create a constraint which targets a property.
345 * The templated parameter P, is the type of the property to constrain.
346 * Animation will be performed using the default interpolator.
347 * @param [in] target The index of the property to constrain.
348 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
349 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
350 * @param [in] source3 The source of a property; the current value will be passed as the 4th parameter of func.
351 * @param [in] source4 The source of a property; the current value will be passed as the 5th parameter of func.
352 * @param [in] source5 The source of a property; the current value will be passed as the 6th parameter of func.
353 * @param [in] func A function which returns the constrained property value.
354 * @return The new constraint.
357 static Constraint New( Property::Index target,
358 ConstraintSource source1,
359 ConstraintSource source2,
360 ConstraintSource source3,
361 ConstraintSource source4,
362 ConstraintSource source5,
363 boost::function<P (const P& current,
364 const PropertyInput& input1,
365 const PropertyInput& input2,
366 const PropertyInput& input3,
367 const PropertyInput& input4,
368 const PropertyInput& input5)> func )
371 PropertyTypes::Get<P>(),
378 GetDefaultInterpolator(PropertyTypes::Get<P>()) );
382 * @brief Create a constraint which targets a property.
384 * The templated parameter P, is the type of the property to constrain.
385 * @param [in] target The index of the property to constrain.
386 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
387 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
388 * @param [in] source3 The source of a property; the current value will be passed as the 4th parameter of func.
389 * @param [in] source4 The source of a property; the current value will be passed as the 5th parameter of func.
390 * @param [in] source5 The source of a property; the current value will be passed as the 6th parameter of func.
391 * @param [in] func A function which returns the constrained property value.
392 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
393 * @return The new constraint.
396 static Constraint NewWithInterpolator( Property::Index target,
397 ConstraintSource source1,
398 ConstraintSource source2,
399 ConstraintSource source3,
400 ConstraintSource source4,
401 ConstraintSource source5,
402 boost::function<P (const P& current,
403 const PropertyInput& input1,
404 const PropertyInput& input2,
405 const PropertyInput& input3,
406 const PropertyInput& input4,
407 const PropertyInput& input5)> func,
408 boost::function<P (const P&, const P&, float)> interpolator )
411 PropertyTypes::Get<P>(),
422 * @brief Create a constraint which targets a property.
424 * The templated parameter P, is the type of the property to constrain.
425 * Animation will be performed using the default interpolator.
426 * @param [in] target The index of the property to constrain.
427 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
428 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
429 * @param [in] source3 The source of a property; the current value will be passed as the 4th parameter of func.
430 * @param [in] source4 The source of a property; the current value will be passed as the 5th parameter of func.
431 * @param [in] source5 The source of a property; the current value will be passed as the 6th parameter of func.
432 * @param [in] source6 The source of a property; the current value will be passed as the 7th parameter of func.
433 * @param [in] func A function which returns the constrained property value.
434 * @return The new constraint.
437 static Constraint New( Property::Index target,
438 ConstraintSource source1,
439 ConstraintSource source2,
440 ConstraintSource source3,
441 ConstraintSource source4,
442 ConstraintSource source5,
443 ConstraintSource source6,
444 boost::function<P (const P& current,
445 const PropertyInput& input1,
446 const PropertyInput& input2,
447 const PropertyInput& input3,
448 const PropertyInput& input4,
449 const PropertyInput& input5,
450 const PropertyInput& input6)> func )
453 PropertyTypes::Get<P>(),
461 GetDefaultInterpolator(PropertyTypes::Get<P>()) );
465 * @brief Create a constraint which targets a property.
467 * The templated parameter P, is the type of the property to constrain.
468 * @param [in] target The index of the property to constrain.
469 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
470 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
471 * @param [in] source3 The source of a property; the current value will be passed as the 4th parameter of func.
472 * @param [in] source4 The source of a property; the current value will be passed as the 5th parameter of func.
473 * @param [in] source5 The source of a property; the current value will be passed as the 6th parameter of func.
474 * @param [in] source6 The source of a property; the current value will be passed as the 7th parameter of func.
475 * @param [in] func A function which returns the constrained property value.
476 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
477 * @return The new constraint.
480 static Constraint NewWithInterpolator( Property::Index target,
481 ConstraintSource source1,
482 ConstraintSource source2,
483 ConstraintSource source3,
484 ConstraintSource source4,
485 ConstraintSource source5,
486 ConstraintSource source6,
487 boost::function<P (const P& current,
488 const PropertyInput& input1,
489 const PropertyInput& input2,
490 const PropertyInput& input3,
491 const PropertyInput& input4,
492 const PropertyInput& input5,
493 const PropertyInput& input6)> func,
494 boost::function<P (const P&, const P&, float)> interpolator )
497 PropertyTypes::Get<P>(),
509 * @brief Downcast an Object handle to Constraint handle.
511 * If handle points to a Constraint object the
512 * downcast produces valid handle. If not the returned handle is left uninitialized.
513 * @param[in] handle to An object
514 * @return handle to a Constraint object or an uninitialized handle
516 static Constraint DownCast( BaseHandle handle );
521 * This is non-virtual since derived Handle types must not contain data or virtual methods.
526 * @brief This copy constructor is required for (smart) pointer semantics.
528 * @param [in] handle A reference to the copied handle
530 Constraint(const Constraint& handle);
533 * @brief This assignment operator is required for (smart) pointer semantics.
535 * @param [in] rhs A reference to the copied handle
536 * @return A reference to this
538 Constraint& operator=(const Constraint& rhs);
541 * @brief This method is defined to allow assignment of the NULL value,
542 * and will throw an exception if passed any other value.
544 * Assigning to NULL is an alias for Reset().
545 * @param [in] rhs A NULL pointer
546 * @return A reference to this handle
548 Constraint& operator=(BaseHandle::NullType* rhs);
551 * @brief Set the time taken for the constraint to be fully applied.
553 * The default is zero, meaning that the constraint is applied immediately.
554 * @param [in] timePeriod The constraint will be applied during this time period.
556 void SetApplyTime( TimePeriod timePeriod );
559 * @brief Retrieve the time taken for the constraint to be fully applied.
561 * @return The apply time.
563 TimePeriod GetApplyTime() const;
566 * @brief Set the time taken for the constraint to be fully removed.
568 * The default is zero, meaning that the constraint is removed immediately.
569 * @param [in] timePeriod The constraint will be removed during this time period.
571 void SetRemoveTime( TimePeriod timePeriod );
574 * @brief Retrieve the time taken for the constraint to be fully removed.
576 * @return The remove time.
578 TimePeriod GetRemoveTime() const;
581 * @brief Set the alpha function for a constraint; the default is AlphaFunctions::Linear.
583 * @param [in] func The alpha function to use when applying/removing the constraint.
585 void SetAlphaFunction( AlphaFunction func );
588 * @brief Retrieve the alpha function of a constraint.
590 * @return The function.
592 AlphaFunction GetAlphaFunction();
595 * @brief Set whether the constraint will "bake" a value when fully-applied.
597 * Otherwise the constrained value will be discarded, when the constraint is removed.
598 * The default value is Constraint::Bake.
599 * @param[in] action The remove-action.
601 void SetRemoveAction( RemoveAction action );
604 * @brief Query whether the constraint will "bake" a value when fully-applied.
606 * Otherwise the constrained value will be discarded, when the constraint is removed.
607 * @return The apply-action.
609 RemoveAction GetRemoveAction() const;
612 * @brief Set a tag for the constraint so it can be identified later
614 * @param[in] tag An integer to identify the constraint
616 void SetTag( const unsigned int tag );
623 unsigned int GetTag() const;
625 public: // Not intended for use by Application developers
628 * @brief This constructor is used by Dali New() methods
629 * @param [in] constraint A pointer to a newly allocated Dali resource
631 explicit DALI_INTERNAL Constraint( Internal::Constraint* constraint );
633 private: // Not intended for use by Application developers
636 * @brief Construct a new constraint which targets a property.
638 * @param [in] target The index of the property to constrain.
639 * @param [in] targetType The type of the constrained property.
640 * @param [in] func The constraint function.
641 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
642 * @return The new constraint.
644 static Constraint New( Property::Index target,
645 Property::Type targetType,
647 AnyFunction interpolator );
650 * @brief Construct a new constraint which targets a property.
652 * @param [in] target The index of the property to constrain.
653 * @param [in] targetType The type of the constrained property.
654 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
655 * @param [in] func The constraint function.
656 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
657 * @return The new constraint.
659 static Constraint New( Property::Index target,
660 Property::Type targetType,
661 ConstraintSource source1,
663 AnyFunction interpolator );
666 * @brief Construct a new constraint which targets a property.
668 * @param [in] target The index of the property to constrain.
669 * @param [in] targetType The type of the constrained property.
670 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
671 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
672 * @param [in] func The constraint function.
673 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
674 * @return The new constraint.
676 static Constraint New( Property::Index target,
677 Property::Type targetType,
678 ConstraintSource source1,
679 ConstraintSource source2,
681 AnyFunction interpolator );
684 * @brief Construct a new constraint which targets a property.
686 * @param [in] target The index of the property to constrain.
687 * @param [in] targetType The type of the constrained property.
688 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
689 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
690 * @param [in] source3 The source of a property; the current value will be passed as the 4th parameter of func.
691 * @param [in] func The constraint function.
692 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
693 * @return The new constraint.
695 static Constraint New( Property::Index target,
696 Property::Type targetType,
697 ConstraintSource source1,
698 ConstraintSource source2,
699 ConstraintSource source3,
701 AnyFunction interpolator );
704 * @brief Construct a new constraint which targets a property.
706 * @param [in] target The index of the property to constrain.
707 * @param [in] targetType The type of the constrained property.
708 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
709 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
710 * @param [in] source3 The source of a property; the current value will be passed as the 4th parameter of func.
711 * @param [in] source4 The source of a property; the current value will be passed as the 5th parameter of func.
712 * @param [in] func The constraint function.
713 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
714 * @return The new constraint.
716 static Constraint New( Property::Index target,
717 Property::Type targetType,
718 ConstraintSource source1,
719 ConstraintSource source2,
720 ConstraintSource source3,
721 ConstraintSource source4,
723 AnyFunction interpolator );
726 * @brief Construct a new constraint which targets a property.
728 * @param [in] target The index of the property to constrain.
729 * @param [in] targetType The type of the constrained property.
730 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
731 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
732 * @param [in] source3 The source of a property; the current value will be passed as the 4th parameter of func.
733 * @param [in] source4 The source of a property; the current value will be passed as the 5th parameter of func.
734 * @param [in] source5 The source of a property; the current value will be passed as the 6th parameter of func.
735 * @param [in] func The constraint function.
736 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
737 * @return The new constraint.
739 static Constraint New( Property::Index target,
740 Property::Type targetType,
741 ConstraintSource source1,
742 ConstraintSource source2,
743 ConstraintSource source3,
744 ConstraintSource source4,
745 ConstraintSource source5,
747 AnyFunction interpolator );
750 * @brief Construct a new constraint which targets a property.
752 * @param [in] target The index of the property to constrain.
753 * @param [in] targetType The type of the constrained property.
754 * @param [in] source1 The source of a property; the current value will be passed as the 2nd parameter of func.
755 * @param [in] source2 The source of a property; the current value will be passed as the 3rd parameter of func.
756 * @param [in] source3 The source of a property; the current value will be passed as the 4th parameter of func.
757 * @param [in] source4 The source of a property; the current value will be passed as the 5th parameter of func.
758 * @param [in] source5 The source of a property; the current value will be passed as the 6th parameter of func.
759 * @param [in] source6 The source of a property; the current value will be passed as the 7th parameter of func.
760 * @param [in] func The constraint function.
761 * @param [in] interpolator A function used to interpolate between the start value, and the value returned by func.
762 * @return The new constraint.
764 static Constraint New( Property::Index target,
765 Property::Type targetType,
766 ConstraintSource source1,
767 ConstraintSource source2,
768 ConstraintSource source3,
769 ConstraintSource source4,
770 ConstraintSource source5,
771 ConstraintSource source6,
773 AnyFunction interpolator );
778 #endif // __DALI_CONSTRAINT_H__