1 /*! \page constraints Constraints
4 <h1 class="pg">Introduction</h1>
6 Constraints are used to modify the property of an actor, based on other properties of the same actor; properties of the actor's parent; or properties of another actor altogether, when the modification needs to be at run-time.
7 Custom functions or functors can be supplied, where the desired value of the property can be calculated.
8 These functions (or functors) are called in every frame so should be fast and not too complex otherwise it will hit performance.
10 Multiple constraints can be applied to the same actor at the same time.
11 The order in which constraints are applied is important as this is the order in which they are processed in the update thread.
13 Constraints are applied after animations have been applied.
14 This means that Constraints override the values set by Animations.
16 Not all properties can be used as a constraint input, please see Dali::Handle::IsPropertyAConstraintInput() for more details.
18 <h1 class="pg">When to use a Constraint</h1>
20 Constraints are designed as a way of modifying properties that cannot be modified by any existing built in functionality; Like Animations, Size negotiation or Parent anchor, origin settings.
21 As they provide the ability for the application developer to execute their own code within the update thread, DALi can no-longer guarantee the timeliness of this code, or how optimised it may be.
23 Generally, you should not use constraints with the SIZE property as constraining the size and size negotiation are mutually exclusive.
24 Consider the following use cases as an example of when and when not to use a constraint:
28 <td><b>Requirement:</b></td>
29 <td><b>Desired Solution:</b></td>
32 <td>Need a child to be 50% the size of it's parent.</td>
33 <td>Use Size negotiation.</td>
36 <td>Need to zoom an actor in to the screen via it's scale property.</td>
37 <td>Use an Animation.</td>
40 <td>Need an actor to appear centered around the bottom-right corner of it's parent.</td>
41 <td>Use ParentOrigin & AnchorPoint.</td>
44 <td>Need to lay out a series of controls with various alignment requirements.</td>
45 <td>Use either Anchor & origin settings, or a TableView.</td>
48 <td>Need to automatically modify the position property of one actor based on the position property of another actor, that is neither a parent OR a child.</td>
49 <td>Use a Constraint.</td>
52 <td>Need to position an actor relative to it's parent actor in a NON-UNIFORM way, IE. a non-linear calculation needs to be performed that requires a functor.</td>
53 <td>Use a Constraint.</td>
56 <td>Need to modify an actor's property in real time based on some calculations that require additional data to be stored in-between frames.</td>
57 <td>Use a Constraint. The constraint functor can hold any variables within it that need to be preserved frame-to-frame.</td>
61 For most general cases, the position and size requirements of a child or parent actor (from it's child or parent) can be calculated with Size Negotiation.
63 <h1 class="pg">Constraint Sources</h1>
65 These are properties of this (or another actor) that are used as inputs into the constraint.
66 The constraint will take these values, optionally perform a calculation on them (if using a custom functor) and write the result to the specified property of the target actor.
67 The source actor is specified as either the same actor, it's parent or another actor.
69 <h2 class="pg">Local Source</h2>
71 A local source is based on the local properties (i.e. size, position, scale, orientation, color) of an actor.
72 For example, the actor's orientation could be used as a constraint input source.
75 Dali::ConstraintSource source( Dali::LocalSource( Dali::Actor::Property::ORIENTATION ) );
78 <h2 class="pg">Parent Source</h2>
80 A parent source is based on properties of the actor's parent.
81 For example, a parent's position can be used as a constraint input source.
84 Dali::ConstraintSource source( Dali::ParentSource( Dali::Actor::Property::POSITION ) );
87 <h2 class="pg">Source</h2>
89 Finally, you can base your source on the properties of another handle altogether.
90 For example, a sibling actor's color could be used as a constraint input source.
93 Dali::ConstraintSource source( Dali::Source( anotherHandle, Dali::Actor::Property::COLOR ) );
96 <h1 class="pg">The Constraint Function</h1>
98 The signature of the constraint function is:
101 void Function( PropertyType& current, const Dali::PropertyInputContainer& inputs );
104 Here 'current' is a reference to the target property type, e.g. float, Vector2, Vector3 etc.
105 This is an in/out parameter.
106 It represents the current value of the property and the expectation is that it will be modified by the function to the desired value.
108 The 'inputs' parameter holds all the constraint input sources.
109 Each element is a pointer to the property-input and can be accessed using the indexing operator[].
110 The order in which the sources are added is the order in which the property-inputs are sorted in the container. For example:
113 constraint.AddSource( Dali::LocalSource( Dali::Actor::Property::POSITION ) );
114 constraint.AddSource( Dali::LocalSource( Dali::Actor::Property::SIZE ) );
115 constraint.AddSource( Dali::ParentSource( Dali::Actor::Property::POSITION ) );
116 constraint.AddSource( Dali::ParentSource( Dali::Actor::Property::SIZE ) );
119 In the constraint function this equates to:
121 const Dali::Vector3& position( inputs[0]->GetVector3() );
122 const Dali::Vector3& size( inputs[1]->GetVector3() );
123 const Dali::Vector3& parentPosition( inputs[2]->GetVector3() );
124 const Dali::Vector3& parentSize( inputs[3]->GetVector3() );
127 <h1 class="pg">Creating a Constraint</h1>
129 <h2 class="pg">Using C Functions</h2>
131 If you do not have any data that is changed at runtime, then C functions should be used.
132 For example, the color of an actor could be changed based on its position along the x-axis till a preset distance of 100, beyond which it is transparent.
135 Dali::Actor actor = Actor::New();
137 Dali::Constraint constraint = Dali::Constraint::New< Vector4 >( actor, Dali::Actor::Property::COLOR, MyConstraintFunction ); // Creates a constraint that targets actor
138 constraint.AddSource( Dali::LocalSource( Dali::Actor::Property::POSITION ) ); // Adds the POSITION property as a constraint input
139 constraint.Apply(); // The constraint is applied
142 And the actual C Function:
145 void MyConstraintFunction( Dali::Vector4& current, const Dali::PropertyInputContainer& inputs )
147 const Dali::Vector3& position( inputs[0]->GetVector3() );
149 float distance = fabs( position.x );
151 // More than 100.0f away, opacity is 0.0f
152 if ( distance > 100.0f )
158 // Otherwise it will blend between fully opaque and transparent
159 current.a = ( 100.0f - distance ) / 100.0f;
164 Please have a look at Dali::Constraint::New() for more details.
166 <h2 class="pg">Using Functors</h2>
168 If you need to store some data in a struct/class, then a functor can be used.
169 Reusing the last example, the color of an actor is changed based on its position along the x-axis, but the distance when it is transparent is different for each applied constraint.
172 Dali::Actor actor = Actor::New();
174 Dali::Constraint constraint = Dali::Constraint::New< Vector4 >( actor, Dali::Actor::Property::COLOR, MyFunctor( 200 ) ); // Creates a constraint that targets actor, and uses MyFunctor with a distance of 200
175 constraint.AddSource( Dali::LocalSource( Dali::Actor::Property::POSITION ) ); // Adds the POSITION property as a constraint input
176 constraint.Apply(); // The constraint is applied
184 /// Constructor which takes the distance at which the actor will be fully transparent
185 MyFunctor( float distance )
186 : mDistance( distance )
191 void operator()( Dali::Vector4& current, const Dali::PropertyInputContainer& inputs )
193 const Dali::Vector3& position( inputs[0]->GetVector3() );
195 float distance = fabs( position.x );
197 // More than mDistance away, opacity is 0.0f
198 if ( distance > mDistance )
204 // Otherwise it will blend between fully opaque and transparent
205 current.a = ( mDistance - distance ) / mDistance;
210 const float mDistance;
214 MyFunctor could then be used with another constraint with a different distance.
216 Please have a look at Dali::Constraint::New(Handle, Property::Index, const T&) for more details.
218 Instead of using the default functor, another method can be declared in the class or struct and used as the constraint function.
219 Please have a look at the appropriate Dali::Constraint::New() method for more details.
221 <h1 class="pg">Removing Constraints</h1>
223 The actor's constraints can later be removed in several ways:
226 mConstraint.Remove(); // mConstraint is a base-handle to a constraint
227 actor.RemoveConstraints(); // Removes ALL constraints applied to an actor
228 actor.RemoveConstraint( tag ); // All constraints with the tag are removed from the actor (tag can be set using SetTag)
231 \section constraints-equal-to Equal To Constraint
233 The built in Dali::EqualToConstraint can be used if all that is required is setting a property equal to another property:
235 Dali::Constraint constraint = Dali::Constraint::New< Vector3 >( actor, Dali::Actor::Property::POSITION, Dali::EqualToConstraint() );
236 constraint.AddSource( Dali::Source( anotherActor, Dali::Actor::Property::POSITION ) );
239 Here actor's position is set to equal the position of anotherActor.
241 \section constraints-relative-to Relative To Constraint
243 The built in Dali::RelativeToConstraint and Dali::RelativeToConstraintFloat can be used if all that is required is setting a property relative to another property:
245 Dali::Constraint constraint = Dali::Constraint::New< Vector3 >( actor, Dali::Actor::Property::POSITION, Dali::RelativeToConstraint( 2.0f ) );
246 constraint.AddSource( Dali::Source( anotherActor, Dali::Actor::Property::POSITION ) );
249 Here actor's position is relative to the position of anotherActor, i.e., if anotherActor is at (10.0f, 20.0f, 30.0f), actor will be at (20.0f, 40.0f, 60.0f).