From: Yoonsang Lee Date: Thu, 22 Oct 2015 04:28:09 +0000 (+0900) Subject: [DALi] Revise Constraints X-Git-Tag: tizen_3.0/TD_SYNC/20161201~348 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=226e2335c14062b30fd17851b86294e0adf9b520;p=sdk%2Fonline-doc.git [DALi] Revise Constraints Signed-off-by: Yoonsang Lee Change-Id: I38a953f90b85c3978bb8211a96492307ad7ba17c --- diff --git a/org.tizen.ui.practices/html/images/constraint-concept.png b/org.tizen.ui.practices/html/images/constraint-concept.png new file mode 100755 index 0000000..733824a Binary files /dev/null and b/org.tizen.ui.practices/html/images/constraint-concept.png differ diff --git a/org.tizen.ui.practices/html/native/dali/constraints_n.htm b/org.tizen.ui.practices/html/native/dali/constraints_n.htm index d571f7a..db07e4f 100644 --- a/org.tizen.ui.practices/html/native/dali/constraints_n.htm +++ b/org.tizen.ui.practices/html/native/dali/constraints_n.htm @@ -23,13 +23,12 @@

Content

Related Info

    @@ -42,139 +41,125 @@

    Constraints: Imposing Your Own Constraints on Actors

    -

    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, therefore they must be fast and not too complex, otherwise they can affect performance.

    +

    Constraints are used to modify the property of an actor (target property), based on other properties of the same actor; properties of the actor's parent; or properties of another actor altogether (property input or constraint source), 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, therefore they must be fast and not too complex, otherwise they can affect performance (constraint function).

    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. Constraints are not applied to off-stage actors.

    -

    Not all properties can be used as a constraint input. For more details, see the IsPropertyAConstraintInput() function in the Dali::Handle class (in mobile and wearable applications).

    +

    Not all properties can be used as a constraint input. To check if a property can be a constraint input, use Dali::Handle::IsPropertyAConstraintInput() function (in mobile and wearable applications).

    -

    Using a Constraint

    +

    Figure: Conceptual diagram for DALi constraint system

    +

    DALi modules

    -

    Constraints are designed as a way of modifying properties that cannot be modified by any existing built-in functionality, such as animations, size negotiation, parent anchor, or 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 optimized it can be.

    -

    Generally, you must 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:

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    - Table: Examples of constraint use -
    RequirementSolution
    Need a child to be 50% the size of it's parent.Use size negotiation.
    Need to zoom an actor in to the screen using its scale property.Use an animation.
    Need an actor to appear centered around the bottom-right corner of its parent.Use ParentOrigin and AnchorPoint.
    Need to lay out a series of controls with various alignment requirements.Use either Anchor and 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 its parent actor in a NON-UNIFORM way, or 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.
    +

    The following pseudocode fragment shows how to set up and apply a constraint:

    +
    +Constraint constraint = Constraint::New< target-property-type >( target-handle, target-property, constraint-function );
    +constraint.AddSource( property-input-1 ); 
    +constraint.AddSource( property-input-2 ); 
    +...
    +constraint.Apply(); 
    +
    -

    For most general cases, the position and size requirements of a child or parent actor (from its child or parent) can be calculated with size negotiation.

    -

    Defining Constraint Sources

    +

    Constraint Source

    These properties are used as input sources to the constraint. The constraint takes these values, optionally performs a calculation on them (if using a custom functor), and writes the result to the specified property of the target actor. The source actor is specified as either the same actor, its parent, or another actor.

    +

    A constraint input source can be one of the following types:

      -
    • Local source +
    • Local source - use Dali::LocalSource

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

      -Dali::ConstraintSource source(Dali::LocalSource(Dali::Actor::Property::ORIENTATION));
      +ConstraintSource source( LocalSource( Actor::Property::ORIENTATION ) );
       
    • -
    • Parent Source +
    • Parent Source - use Dali::ParentSource

      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.

      -Dali::ConstraintSource source(Dali::ParentSource(Dali::Actor::Property::POSITION));
      +ConstraintSource source( ParentSource( Actor::Property::POSITION ) );
       
    • -
    • Other Handle Source +
    • Other Handle Source - use Dali::Source

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

      -Dali::ConstraintSource source(Dali::Source(anotherHandle, Dali::Actor::Property::COLOR));
      +ConstraintSource source( Source( anotherHandle, Actor::Property::COLOR ) );
       
    -

    Defining the Constraint Function

    +

    Constraint Function

    The signature of the constraint function is:

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

    In this function the current parameter is a reference to the target property type, such as float, Vector2, or Vector3. This is an in or out parameter. It represents the current value of the property and the expectation is that it is 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.

    -constraint.AddSource(Dali::LocalSource(Dali::Actor::Property::POSITION));
    -constraint.AddSource(Dali::LocalSource(Dali::Actor::Property::SIZE));
    -constraint.AddSource(Dali::ParentSource(Dali::Actor::Property::POSITION));
    -constraint.AddSource(Dali::ParentSource(Dali::Actor::Property::SIZE));
    +constraint.AddSource( LocalSource( Actor::Property::POSITION ) );
    +constraint.AddSource( LocalSource( Actor::Property::SIZE ) );
    +constraint.AddSource( ParentSource( Actor::Property::POSITION ) );
    +constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
     

    In the constraint function this equals 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());
    +const Vector3& position( inputs[0]->GetVector3() );
    +const Vector3& size( inputs[1]->GetVector3() );
    +const Vector3& parentPosition( inputs[2]->GetVector3() );
    +const Vector3& parentSize( inputs[3]->GetVector3() );
     
    -

    Creating a Constraint

    +

    Constraint Function Examples

    Using C Functions

    -

    If you do not have any data that is changed at runtime, C functions must be used. For example, the color of an actor can be changed based on its position along the x axis to a preset distance of 100, beyond which it is transparent.

    +

    If you do not have any data that is changed at runtime, a C function is preferred as a constraint function.

    +

    For example, the color of an actor can be changed based on its position along the x axis. +In the following sample, the color of the control is white when its x position is 0.0f, red when its x position is 100.0f, and interpolated color inbetween them:

    -Dali::Actor actor = Actor::New();
    +// This sample code is for the HelloWorldExample class
    +// in the 'Basic DALi Application' code from the 'DALi Overview' section.
     
    -// Create a constraint that targets actor
    -Dali::Constraint constraint = Dali::Constraint::New< Vector4 >(actor, Dali::Actor::Property::COLOR, MyConstraintFunction); 
    -
    -// Add the POSITION property as a constraint input
    -constraint.AddSource(Dali::LocalSource(Dali::Actor::Property::POSITION)); 
    +// C function
    +void MyConstraintFunction( Vector4& current, const PropertyInputContainer& inputs )
    +{
    +  const Vector3& position( inputs[0]->GetVector3() );
    +
    +  float distance = fabs( position.x );
    +  if( distance > 100.0f ) // More than 100.0f away, color is red
    +  {
    +    current.g = current.b = 0.0f;
    +  }
    +  else // Otherwise it blends between white and red
    +  {
    +    current.g = current.b = ( 100.0f - distance ) / 100.0f;
    +  }
    +}
     
    -// Apply the constraint
    -constraint.Apply(); 
    -
    +void HelloWorldExample::Create( Application& application ) +{ + // Create a Control + Control control = Control::New(); + control.SetParentOrigin( ParentOrigin::CENTER ); + control.SetSize( 100.0f, 100.0f ); + control.SetBackgroundColor( Color::WHITE ); + Stage::GetCurrent().Add( control ); + + // Use PanGestureDetector to move the control with touch panning + mDetector = PanGestureDetector::New(); + mDetector.Attach( control ); + mDetector.DetectedSignal().Connect( this, &HelloWorldExample::OnPan ); + + // Create a Constraint that targets the control + Constraint constraint = Constraint::New< Vector4 >( control, Actor::Property::COLOR, MyConstraintFunction ); + + // Add the POSITION property of the control as a constraint input + constraint.AddSource( LocalSource( Actor::Property::POSITION ) ); + + // Apply the constraint + constraint.Apply(); +} -

    The following example shows the actual C function:

    -
    -void MyConstraintFunction(Dali::Vector4& current, const Dali::PropertyInputContainer& inputs)
    +void HelloWorldExample::OnPan( Actor actor, const PanGesture& gesture )
     {
    -   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 blends between fully opaque and transparent
    -      current.a = (100.0f - distance) / 100.0f;
    -   }
    +  // Move the button using detected gesture
    +  actor.TranslateBy( Vector3( gesture.displacement ) );
     }
     
    @@ -182,89 +167,140 @@ void MyConstraintFunction(Dali::Vector4& current, const Dali::PropertyInputC

    Using Functors

    -

    If you need to store some data in a struct or class, 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.

    +

    If you need to store some data in a struct or class, 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 red can be different for each constraint object.

    -Dali::Actor actor = Actor::New();
    -
    -// Create a constraint that targets actor, and uses MyFunctor with a distance of 200
    -Dali::Constraint constraint = Dali::Constraint::New< Vector4 >(actor, Dali::Actor::Property::COLOR, MyFunctor(200)); 
    +struct MyConstraintFunctor
    +{
    +  // Constructor which takes the distance at which the actor is red
    +  MyConstraintFunctor(float distance)
    +    : mDistance(distance)  {}
    +
    +  // Functor
    +  void operator()( Vector4& current, const PropertyInputContainer& inputs )
    +  {
    +    const Vector3& position( inputs[0]->GetVector3() );
    +
    +    float distance = fabs( position.x );
    +    if( distance > mDistance ) // More than mDistance away, color is red
    +    {
    +      current.g = current.b = 0.0f;
    +    }
    +    else // Otherwise it blends between white and red
    +    {
    +      current.g = current.b = ( mDistance - distance ) / mDistance;
    +    }
    +  }
    +
    +  const float mDistance; // Data
    +};
    +
    -// Add the POSITION property as a constraint input -constraint.AddSource(Dali::LocalSource(Dali::Actor::Property::POSITION)); +

    MyConstraintFunctor() can be applied to control as follows: -// Apply the constraint -constraint.Apply(); - -

    The following example shows the structure:

    -struct MyFunctor
    -{
    -   // Constructor which takes the distance at which the actor is fully transparent
    -   MyFunctor(float distance)
    -   : mDistance(distance)
    -   {
    -   }
    -
    -   // Functor
    -   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 blends between fully opaque and transparent
    -         current.a = (100.0f - mDistance) / 100.0f;
    -      }
    -   }
    -
    -   // Data
    -   const float mDistance;
    -};
    +Constraint constraint = Constraint::New< Vector4 >( control, Actor::Property::COLOR, MyConstraintFunctor( 200.0f ) );
     
    -

    MyFunctor() can be used also with another constraint with a different distance.

    -

    Instead of using the default functor, another function can be declared in the class or struct and be used as the constraint function.

    -

    For more information, see the New() function in the Dali::Constraint class (in mobile and wearable applications).

    + +

    MyConstraintFunctor() can be used also with another constraint with a different distance.

    +

    Instead of using the default functor (operator()), another function can be declared in the class or struct and be used as the constraint function. +For more information, see the New() function in the Dali::Constraint class (in mobile and wearable applications).

    Removing Constraints

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

    -mConstraint.Remove(); // mConstraint is a base-handle to a constraint
    +constraint.Remove(); // Remove the constraint itself and stop applying it
     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)
    +actor.RemoveConstraints( tag ); // All constraints with the tag are removed from an actor (tag can be set using SetTag)
     
    -

    Setting an Equal To Constraint

    +

    Built-in Constraint Functions

    + +

    EqualToConstraint

    The built-in Dali::EqualToConstraint function can be used if only setting a property equal to another property is required:

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

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

    +

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

    -

    Setting a Relative To Constraint

    +

    RelativeToConstraint

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

    -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 constraint = Constraint::New< Vector3 >( actor, Actor::Property::POSITION, RelativeToConstraint( 2.0f ) );
    +constraint.AddSource( Source( anotherActor, Actor::Property::POSITION ) );
     constraint.Apply();
     
    -

    Here the actor's position is relative to the position of anotherActor. If anotherActor is at (10.0f, 20.0f, 30.0f), actor is at (20.0f, 40.0f, 60.0f).

    +

    Here the actor's position is relative to the position of anotherActor (multiplied by a given scale). If anotherActor is at (10.0f, 20.0f, 30.0f), actor is at (20.0f, 40.0f, 60.0f).

    + + + + + + + + + + +
    Note
    + +

    When and When Not to use a Constraint

    + +

    Constraints are designed as a way of modifying properties that cannot be modified by any existing built-in functionality, such as animations, size negotiation, parent anchor, or 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 optimized it can be.

    +

    Generally, you must 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:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    + Table: Examples of constraint use +
    RequirementSolution
    Need a child to be 50% the size of it's parent.Use size negotiation.
    (See Layout Management for more information)
    Need to zoom an actor in to the screen using its scale property.Use an animation.
    (See Animation for more information)
    Need an actor to appear centered around the bottom-right corner of its parent.Use ParentOrigin and AnchorPoint.
    (See Positioning Actors for more information)
    Need to lay out a series of controls with various alignment requirements.Use either ParentOrigin and AnchorPoint settings, or a Dali::Toolkit::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 its parent actor in a NON-UNIFORM way, or 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 its child or parent) can be calculated with size negotiation.

    + +
    diff --git a/org.tizen.ui.practices/html/native/dali/event_handling_n.htm b/org.tizen.ui.practices/html/native/dali/event_handling_n.htm index f79b602..582d060 100644 --- a/org.tizen.ui.practices/html/native/dali/event_handling_n.htm +++ b/org.tizen.ui.practices/html/native/dali/event_handling_n.htm @@ -554,7 +554,6 @@ void HelloWorldExample::OnPan( Actor actor, const PanGesture& gesture ) // Move the button using detected gesture actor.TranslateBy( Vector3( gesture.displacement ) ); } -

    Automatic Connection Management