1 #ifndef __DALI_INTERNAL_ACTIVE_CONSTRAINT_H__
2 #define __DALI_INTERNAL_ACTIVE_CONSTRAINT_H__
5 * Copyright (c) 2017 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 <dali/internal/common/message.h>
23 #include <dali/internal/event/common/event-thread-services.h>
24 #include <dali/internal/event/common/object-impl.h>
25 #include <dali/internal/event/common/thread-local-storage.h>
26 #include <dali/internal/event/common/stage-impl.h>
27 #include <dali/internal/event/animation/constraint-base.h>
28 #include <dali/internal/event/animation/constraint-source-impl.h>
29 #include <dali/internal/event/animation/property-constraint-ptr.h>
30 #include <dali/internal/update/common/animatable-property.h>
31 #include <dali/internal/update/common/property-owner.h>
32 #include <dali/internal/update/common/property-owner-messages.h>
33 #include <dali/internal/update/animation/scene-graph-constraint.h>
34 #include <dali/internal/update/animation/property-accessor.h>
35 #include <dali/internal/update/animation/property-component-accessor.h>
44 * Helper to add only unique entries to the propertyOwner container
45 * @param propertyOwners to add the entries to
46 * @param object to add
48 inline void AddUnique( SceneGraph::PropertyOwnerContainer& propertyOwners, SceneGraph::PropertyOwner* object )
50 const SceneGraph::PropertyOwnerIter iter = std::find( propertyOwners.Begin(), propertyOwners.End(), object );
51 if( iter == propertyOwners.End() )
53 // each owner should only be added once
54 propertyOwners.PushBack( object );
59 * Connects a constraint which takes another property as an input.
61 template < typename PropertyType >
62 class Constraint : public ConstraintBase
66 typedef SceneGraph::Constraint< PropertyType, PropertyAccessor<PropertyType> > SceneGraphConstraint;
67 typedef const SceneGraph::AnimatableProperty<PropertyType>* ScenePropertyPtr;
68 typedef typename PropertyConstraintPtr<PropertyType>::Type ConstraintFunctionPtr;
69 typedef const SceneGraph::TransformManagerPropertyHandler<PropertyType> TransformManagerProperty;
72 * Construct a new constraint.
73 * @param[in] object The property-owning object.
74 * @param[in] targetIndex The index of the property to constrain.
75 * @param[in] sources The sources of the input properties passed to func.
76 * @param[in] func The constraint function.
77 * @return A newly allocated active-constraint.
79 static ConstraintBase* New( Object& object,
80 Property::Index targetIndex,
81 SourceContainer& sources,
82 ConstraintFunctionPtr func )
84 return new Constraint< PropertyType >( object, targetIndex, sources, func );
88 * @copydoc ConstraintBase::Clone()
90 virtual ConstraintBase* Clone( Object& object )
92 DALI_ASSERT_ALWAYS( !mSourceDestroyed && "An input source object has been destroyed" );
94 ConstraintBase* clone( NULL );
96 ConstraintFunctionPtr funcPtr( mUserFunction->Clone() );
98 clone = new Constraint< PropertyType >( object,
103 clone->SetRemoveAction(mRemoveAction);
104 clone->SetTag( mTag );
111 * Virtual destructor.
113 virtual ~Constraint()
115 // This is not responsible for removing constraints.
121 * Private constructor; see also Constraint::New().
123 Constraint( Object& object,
124 Property::Index targetIndex,
125 SourceContainer& sources,
126 ConstraintFunctionPtr& func )
127 : ConstraintBase( object, targetIndex, sources ),
128 mTargetIndex( targetIndex ),
129 mUserFunction( func )
134 Constraint( const Constraint& );
137 Constraint& operator=( const Constraint& rhs );
140 * Create and connect a constraint for a scene-object.
142 void ConnectConstraint()
144 // Should not come here if target-object has been destroyed
145 DALI_ASSERT_DEBUG( NULL != mTargetObject );
147 // Guard against double connections
148 DALI_ASSERT_DEBUG( NULL == mSceneGraphConstraint );
150 // Short-circuit until the target scene-object exists
151 SceneGraph::PropertyOwner* targetObject = const_cast< SceneGraph::PropertyOwner* >( mTargetObject->GetSceneObject() );
152 if ( NULL == targetObject )
157 // Build a container of property-owners, providing the scene-graph properties
158 SceneGraph::PropertyOwnerContainer propertyOwners;
159 propertyOwners.PushBack( targetObject );
161 // Build the constraint function; this requires a scene-graph property from each source
162 ConstraintFunctionPtr func( ConnectConstraintFunction( propertyOwners ) );
166 // Create the SceneGraphConstraint, and connect to the scene-graph
168 const SceneGraph::PropertyBase* targetProperty = mTargetObject->GetSceneObjectAnimatableProperty( mTargetIndex );
170 // The targetProperty should exist, when targetObject exists
171 DALI_ASSERT_ALWAYS( NULL != targetProperty && "Constraint target property does not exist" );
173 if( targetProperty->IsTransformManagerProperty() ) //It is a property managed by the transform manager
175 // Connect the constraint
176 mSceneGraphConstraint = SceneGraph::Constraint<PropertyType,TransformManagerPropertyAccessor<PropertyType> >::New( *targetProperty,
181 else //SceneGraph property
183 // Connect the constraint
184 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty,
189 OwnerPointer< SceneGraph::ConstraintBase > transferOwnership( const_cast< SceneGraph::ConstraintBase* >( mSceneGraphConstraint ) );
190 ApplyConstraintMessage( GetEventThreadServices(), *targetObject, transferOwnership );
195 * Helper for ConnectConstraint. Creates a connected constraint-function.
196 * Also populates the property-owner container, for each input connected to the constraint-function.
197 * @param[out] propertyOwners The container of property-owners providing the scene-graph properties.
198 * @return A connected constraint-function, or NULL if the scene-graph properties are not available.
200 PropertyConstraint<PropertyType>* ConnectConstraintFunction( SceneGraph::PropertyOwnerContainer& propertyOwners )
202 PropertyConstraint<PropertyType>* func = mUserFunction->Clone();
204 for ( SourceIter iter = mSources.begin(); mSources.end() != iter; ++iter )
206 Source& source = *iter;
208 PropertyInputImpl* inputProperty( NULL );
209 int componentIndex( Property::INVALID_COMPONENT_INDEX );
211 if ( OBJECT_PROPERTY == source.sourceType )
213 DALI_ASSERT_ALWAYS( source.object->IsPropertyAConstraintInput( source.propertyIndex ) );
215 SceneGraph::PropertyOwner* owner = const_cast< SceneGraph::PropertyOwner* >( source.object->GetSceneObject() );
217 // The property owner will not exist, if the target object is off-stage
220 AddUnique( propertyOwners, owner );
221 inputProperty = const_cast< PropertyInputImpl* >( source.object->GetSceneObjectInputProperty( source.propertyIndex ) );
222 componentIndex = source.object->GetPropertyComponentIndex( source.propertyIndex );
224 // The scene-object property should exist, when the property owner exists
225 DALI_ASSERT_ALWAYS( NULL != inputProperty && "Constraint source property does not exist" );
228 else if ( LOCAL_PROPERTY == source.sourceType )
230 DALI_ASSERT_ALWAYS( mTargetObject->IsPropertyAConstraintInput( source.propertyIndex ) );
232 inputProperty = const_cast< PropertyInputImpl* >( mTargetObject->GetSceneObjectInputProperty( source.propertyIndex ) );
233 componentIndex = mTargetObject->GetPropertyComponentIndex( source.propertyIndex );
235 // The target scene-object should provide this property
236 DALI_ASSERT_ALWAYS( NULL != inputProperty && "Constraint source property does not exist" );
240 DALI_ASSERT_ALWAYS( PARENT_PROPERTY == source.sourceType && "Constraint source property type is invalid" );
242 Object* objectParent = dynamic_cast< Actor& >( *mTargetObject ).GetParent();
244 // This will not exist, if the target object is off-stage
245 if ( NULL != objectParent )
247 DALI_ASSERT_ALWAYS( objectParent->IsPropertyAConstraintInput( source.propertyIndex ) );
249 SceneGraph::PropertyOwner* owner = const_cast< SceneGraph::PropertyOwner* >( objectParent->GetSceneObject() );
251 // The property owner will not exist, if the parent object is off-stage
254 AddUnique( propertyOwners, owner );
255 inputProperty = const_cast< PropertyInputImpl* >( objectParent->GetSceneObjectInputProperty( source.propertyIndex ) );
256 componentIndex = objectParent->GetPropertyComponentIndex( source.propertyIndex );
258 // The scene-object property should exist, when the property owner exists
259 DALI_ASSERT_ALWAYS( NULL != inputProperty && "Constraint source property does not exist" );
264 if ( NULL == inputProperty )
269 // Exit if a scene-graph object is not available from one of the sources
273 func->SetInput( ( iter - mSources.begin() ), componentIndex, *inputProperty );
281 Property::Index mTargetIndex;
283 ConstraintFunctionPtr mUserFunction;
287 * Variant which allows float components to be animated individually.
290 class Constraint<float> : public ConstraintBase
294 typedef typename PropertyConstraintPtr<float>::Type ConstraintFunctionPtr;
297 * Construct a new constraint.
298 * @param[in] object The property-owning object.
299 * @param[in] targetIndex The index of the property to constrain.
300 * @param[in] sources The sources of the input properties passed to func.
301 * @param[in] func The constraint function.
302 * @return A newly allocated constraint.
304 static ConstraintBase* New( Object& object,
305 Property::Index targetIndex,
306 SourceContainer& sources,
307 ConstraintFunctionPtr func )
309 return new Constraint< float >( object, targetIndex, sources, func );
313 * @copydoc ConstraintBase::Clone()
315 virtual ConstraintBase* Clone( Object& object )
317 DALI_ASSERT_ALWAYS( !mSourceDestroyed && "An input source object has been destroyed" );
319 ConstraintBase* clone( NULL );
321 ConstraintFunctionPtr funcPtr( mUserFunction->Clone() );
323 clone = new Constraint< float >( object,
328 clone->SetRemoveAction(mRemoveAction);
329 clone->SetTag( mTag );
335 * Virtual destructor.
337 virtual ~Constraint()
339 // This is not responsible for removing constraints.
345 * Private constructor; see also Constraint::New().
347 Constraint( Object& object,
348 Property::Index targetIndex,
349 SourceContainer& sources,
350 ConstraintFunctionPtr& func )
351 : ConstraintBase( object, targetIndex, sources ),
352 mTargetIndex( targetIndex ),
353 mUserFunction( func )
358 Constraint( const Constraint& );
361 Constraint& operator=( const Constraint& rhs );
364 * Create and connect a constraint for a scene-object.
366 void ConnectConstraint()
368 // Should not come here if target-object has been destroyed
369 DALI_ASSERT_DEBUG( NULL != mTargetObject );
371 // Guard against double connections
372 DALI_ASSERT_DEBUG( NULL == mSceneGraphConstraint );
374 // Short-circuit until the target scene-object exists
375 SceneGraph::PropertyOwner* targetObject = const_cast< SceneGraph::PropertyOwner* >( mTargetObject->GetSceneObject() );
376 if ( NULL == targetObject )
381 // Build a container of property-owners, providing the scene-graph properties
382 SceneGraph::PropertyOwnerContainer propertyOwners;
383 propertyOwners.PushBack( targetObject );
385 // Build the constraint function; this requires a scene-graph property from each source
386 ConstraintFunctionPtr func( ConnectConstraintFunction( propertyOwners ) );
390 // Create the SceneGraphConstraint, and connect to the scene-graph
392 const SceneGraph::PropertyBase* targetProperty = mTargetObject->GetSceneObjectAnimatableProperty( mTargetIndex );
394 // The targetProperty should exist, when targetObject exists
395 DALI_ASSERT_ALWAYS( NULL != targetProperty && "Constraint target property does not exist" );
397 const int componentIndex = mTargetObject->GetPropertyComponentIndex( mTargetIndex );
399 if ( Property::INVALID_COMPONENT_INDEX == componentIndex )
401 // Not a Vector2, Vector3 or Vector4 component, expecting float type
402 DALI_ASSERT_DEBUG( PropertyTypes::Get< float >() == targetProperty->GetType() );
404 typedef SceneGraph::Constraint< float, PropertyAccessor<float> > SceneGraphConstraint;
406 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
410 // Expecting Vector2, Vector3 or Vector4 type
412 if ( PropertyTypes::Get< Vector2 >() == targetProperty->GetType() )
414 // Constrain float component of Vector2 property
416 if ( 0 == componentIndex )
418 typedef SceneGraph::Constraint< float, PropertyComponentAccessorX<Vector2> > SceneGraphConstraint;
419 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
421 else if ( 1 == componentIndex )
423 typedef SceneGraph::Constraint< float, PropertyComponentAccessorY<Vector2> > SceneGraphConstraint;
424 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
427 else if ( PropertyTypes::Get< Vector3 >() == targetProperty->GetType() )
429 // Constrain float component of Vector3 property
430 if( targetProperty->IsTransformManagerProperty() )
432 if ( 0 == componentIndex )
434 typedef SceneGraph::Constraint< float, TransformManagerPropertyComponentAccessor<Vector3,0> > SceneGraphConstraint;
435 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
437 else if ( 1 == componentIndex )
439 typedef SceneGraph::Constraint< float, TransformManagerPropertyComponentAccessor<Vector3,1> > SceneGraphConstraint;
440 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
442 else if ( 2 == componentIndex )
444 typedef SceneGraph::Constraint< float, TransformManagerPropertyComponentAccessor<Vector3,2> > SceneGraphConstraint;
445 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
450 if ( 0 == componentIndex )
452 typedef SceneGraph::Constraint< float, PropertyComponentAccessorX<Vector3> > SceneGraphConstraint;
453 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
455 else if ( 1 == componentIndex )
457 typedef SceneGraph::Constraint< float, PropertyComponentAccessorY<Vector3> > SceneGraphConstraint;
458 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
460 else if ( 2 == componentIndex )
462 typedef SceneGraph::Constraint< float, PropertyComponentAccessorZ<Vector3> > SceneGraphConstraint;
463 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
467 else if ( PropertyTypes::Get< Vector4 >() == targetProperty->GetType() )
469 // Constrain float component of Vector4 property
471 if ( 0 == componentIndex )
473 typedef SceneGraph::Constraint< float, PropertyComponentAccessorX<Vector4> > SceneGraphConstraint;
474 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
476 else if ( 1 == componentIndex )
478 typedef SceneGraph::Constraint< float, PropertyComponentAccessorY<Vector4> > SceneGraphConstraint;
479 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
481 else if ( 2 == componentIndex )
483 typedef SceneGraph::Constraint< float, PropertyComponentAccessorZ<Vector4> > SceneGraphConstraint;
484 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
486 else if ( 3 == componentIndex )
488 typedef SceneGraph::Constraint< float, PropertyComponentAccessorW<Vector4> > SceneGraphConstraint;
489 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
494 if( mSceneGraphConstraint )
496 OwnerPointer< SceneGraph::ConstraintBase > transferOwnership( const_cast< SceneGraph::ConstraintBase* >( mSceneGraphConstraint ) );
497 ApplyConstraintMessage( GetEventThreadServices(), *targetObject, transferOwnership );
503 * Helper for ConnectConstraint. Creates a connected constraint-function.
504 * Also populates the property-owner container, for each input connected to the constraint-function.
505 * @param[out] propertyOwners The container of property-owners providing the scene-graph properties.
506 * @return A connected constraint-function, or NULL if the scene-graph properties are not available.
508 PropertyConstraint<float>* ConnectConstraintFunction( SceneGraph::PropertyOwnerContainer& propertyOwners )
510 PropertyConstraint<float>* func = mUserFunction->Clone();
512 for ( SourceIter iter = mSources.begin(); mSources.end() != iter; ++iter )
514 Source& source = *iter;
516 PropertyInputImpl* inputProperty( NULL );
517 int componentIndex( Property::INVALID_COMPONENT_INDEX );
519 if ( OBJECT_PROPERTY == source.sourceType )
521 DALI_ASSERT_ALWAYS( source.object->IsPropertyAConstraintInput( source.propertyIndex ) );
523 SceneGraph::PropertyOwner* owner = const_cast< SceneGraph::PropertyOwner* >( source.object->GetSceneObject() );
525 // The property owner will not exist, if the target object is off-stage
528 AddUnique( propertyOwners, owner );
529 inputProperty = const_cast< PropertyInputImpl* >( source.object->GetSceneObjectInputProperty( source.propertyIndex ) );
530 componentIndex = source.object->GetPropertyComponentIndex( source.propertyIndex );
532 // The scene-object property should exist, when the property owner exists
533 DALI_ASSERT_ALWAYS( NULL != inputProperty && "Constraint source property does not exist" );
536 else if ( LOCAL_PROPERTY == source.sourceType )
538 DALI_ASSERT_ALWAYS( mTargetObject->IsPropertyAConstraintInput( source.propertyIndex ) );
540 inputProperty = const_cast< PropertyInputImpl* >( mTargetObject->GetSceneObjectInputProperty( source.propertyIndex ) );
541 componentIndex = mTargetObject->GetPropertyComponentIndex( source.propertyIndex );
543 // The target scene-object should provide this property
544 DALI_ASSERT_ALWAYS( NULL != inputProperty && "Constraint source property does not exist" );
548 DALI_ASSERT_ALWAYS( PARENT_PROPERTY == source.sourceType && "Constraint source property type is invalid" );
550 Object* objectParent = dynamic_cast< Actor& >( *mTargetObject ).GetParent();
552 // This will not exist, if the target object is off-stage
553 if ( NULL != objectParent )
555 DALI_ASSERT_ALWAYS( objectParent->IsPropertyAConstraintInput( source.propertyIndex ) );
557 SceneGraph::PropertyOwner* owner = const_cast< SceneGraph::PropertyOwner* >( objectParent->GetSceneObject() );
559 // The property owner will not exist, if the parent object is off-stage
562 AddUnique( propertyOwners, owner );
563 inputProperty = const_cast< PropertyInputImpl* >( objectParent->GetSceneObjectInputProperty( source.propertyIndex ) );
564 componentIndex = objectParent->GetPropertyComponentIndex( source.propertyIndex );
566 // The scene-object property should exist, when the property owner exists
567 DALI_ASSERT_ALWAYS( NULL != inputProperty && "Constraint source property does not exist" );
572 if ( NULL == inputProperty )
577 // Exit if a scene-graph object is not available from one of the sources
581 func->SetInput( ( iter - mSources.begin() ), componentIndex, *inputProperty );
589 Property::Index mTargetIndex;
591 ConstraintFunctionPtr mUserFunction;
594 } // namespace Internal
598 #endif // __DALI_INTERNAL_ACTIVE_CONSTRAINT_H__