Dali 3D User Interface Engine
Constraints

Introduction

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. Custom functions or functors can be supplied, where the desired value of the property can be calculated. These functions (or functors) are called in every frame so should be fast and not too complex otherwise it will hit performance.

Multiple constraints can be applied to the same actor at the same time. The order in which constraints are applied is important as this is the order in which they are processed in the update thread.

Constraints are applied after animations have been applied. This means that Constraints override the values set by Animations.

Not all properties can be used as a constraint input, please see Dali::Handle::IsPropertyAConstraintInput() for more details.

When to use a Constraint

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. 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.

Generally, you should not use constraints with the SIZE property as constraining the size and size negotiation are mutually exclusive. Consider the following use cases as an example of when and when not to use a constraint:

Requirement: Desired Solution:
Need a child to be 50% the size of it's parent. Use Size negotiation.
Need to zoom an actor in to the screen via it's scale property. Use an Animation.
Need an actor to appear centered around the bottom-right corner of it's parent. Use ParentOrigin & AnchorPoint.
Need to lay out a series of controls with various alignment requirements. Use either Anchor & origin settings, or a TableView.
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. Use a Constraint.
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. Use a Constraint.
Need to modify an actor's property in real time based on some calculations that require additional data to be stored in-between frames. Use a Constraint. The constraint functor can hold any variables within it that need to be preserved frame-to-frame.

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.

Constraint Sources

These are properties of this (or another actor) that are used as inputs into the constraint. 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. The source actor is specified as either the same actor, it's parent or another actor.

Local Source

A local source is based on the local properties (i.e. size, position, scale, orientation, color) of an actor. For example, the actor's orientation could be used as a constraint input source.

Parent Source

A parent source is based on properties of the actor's parent. For example, a parent's position can be used as a constraint input source.

Source

Finally, you can base your source on the properties of another handle altogether. For example, a sibling actor's color could be used as a constraint input source.

The Constraint Function

The signature of the constraint function is:

void Function( PropertyType& current, const Dali::PropertyInputContainer& inputs );

Here 'current' is a reference to the target property type, e.g. float, Vector2, Vector3 etc. This is an in/out parameter. It represents the current value of the property and the expectation is that it will be modified by the function to the desired value.

The 'inputs' parameter holds all the constraint input sources. Each element is a pointer to the property-input and can be accessed using the indexing operator[]. The order in which the sources are added is the order in which the property-inputs are sorted in the container. For example:

In the constraint function this equates to:

const Dali::Vector3& position( inputs[0]->GetVector3() );
const Dali::Vector3& size( inputs[1]->GetVector3() );
const Dali::Vector3& parentPosition( inputs[2]->GetVector3() );
const Dali::Vector3& parentSize( inputs[3]->GetVector3() );

Creating a Constraint

Using C Functions

If you do not have any data that is changed at runtime, then C functions should be used. 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.

Dali::Constraint constraint = Dali::Constraint::New< Vector4 >( actor, Dali::Actor::Property::COLOR, MyConstraintFunction ); // Creates a constraint that targets actor
constraint.AddSource( Dali::LocalSource( Dali::Actor::Property::POSITION ) ); // Adds the POSITION property as a constraint input
constraint.Apply(); // The constraint is applied

And the actual C Function:

void MyConstraintFunction( Dali::Vector4& current, const Dali::PropertyInputContainer& inputs )
{
const Dali::Vector3& position( inputs[0]->GetVector3() );
float distance = fabs( position.x );
// More than 100.0f away, opacity is 0.0f
if ( distance > 100.0f )
{
current.a = 0.0f;
}
else
{
// Otherwise it will blend between fully opaque and transparent
current.a = ( 100.0f - distance ) / 100.0f;
}
}

Please have a look at Dali::Constraint::New() for more details.

Using Functors

If you need to store some data in a struct/class, then a functor can be used. 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.

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
constraint.AddSource( Dali::LocalSource( Dali::Actor::Property::POSITION ) ); // Adds the POSITION property as a constraint input
constraint.Apply(); // The constraint is applied

And the struct:

struct MyFunctor
{
MyFunctor( float distance )
: mDistance( distance )
{
}
void operator()( Dali::Vector4& current, const Dali::PropertyInputContainer& inputs )
{
const Dali::Vector3& position( inputs[0]->GetVector3() );
float distance = fabs( position.x );
// More than mDistance away, opacity is 0.0f
if ( distance > mDistance )
{
current.a = 0.0f;
}
else
{
// Otherwise it will blend between fully opaque and transparent
current.a = ( 100.0f - mDistance ) / 100.0f;
}
}
// Data
const float mDistance;
};

MyFunctor could then be used with another constraint with a different distance.

Please have a look at Dali::Constraint::New(Handle, Property::Index, const T&) for more details.

Instead of using the default functor, another method can be declared in the class or struct and used as the constraint function. Please have a look at the appropriate Dali::Constraint::New() method for more details.

Removing Constraints

The actor's constraints can later be removed in several ways:

mConstraint.Remove(); // mConstraint is a base-handle to a constraint
actor.RemoveConstraints(); // Removes ALL constraints applied to an actor
actor.RemoveConstraint( tag ); // All constraints with the tag are removed from the actor (tag can be set using SetTag)

Equal To Constraint

The built in Dali::EqualToConstraint can be used if all that is required is setting a property equal to another property:

Dali::Constraint constraint = Dali::Constraint::New< Vector3 >( actor, Dali::Actor::Property::POSITION, Dali::EqualToConstraint() );
constraint.AddSource( Dali::Source( anotherActor, Dali::Actor::Property::POSITION ) );
constraint.Apply();

Here actor's position is set to equal the position of anotherActor.

Relative To Constraint

The built in Dali::RelativeToConstraint and Dali::RelativeToConstraintFloat can be used if all that is required is setting a property relative to another property:

Dali::Constraint constraint = Dali::Constraint::New< Vector3 >( actor, Dali::Actor::Property::POSITION, Dali::RelativeToConstraint( 2.0f ) );
constraint.AddSource( Dali::Source( anotherActor, Dali::Actor::Property::POSITION ) );
constraint.Apply();

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).

Dali Docs Home
Read more about Dali