1 #ifndef __DALI_INTERNAL_ACTIVE_CONSTRAINT_H__
2 #define __DALI_INTERNAL_ACTIVE_CONSTRAINT_H__
5 * Copyright (c) 2018 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/common/property-resetter.h>
34 #include <dali/internal/update/animation/scene-graph-constraint.h>
35 #include <dali/internal/update/animation/property-accessor.h>
36 #include <dali/internal/update/animation/property-component-accessor.h>
46 * Helper to add only unique entries to the propertyOwner container
47 * @param propertyOwners to add the entries to
48 * @param object to add
50 inline void AddUnique( SceneGraph::PropertyOwnerContainer& propertyOwners, SceneGraph::PropertyOwner* object )
52 const SceneGraph::PropertyOwnerIter iter = std::find( propertyOwners.Begin(), propertyOwners.End(), object );
53 if( iter == propertyOwners.End() )
55 // each owner should only be added once
56 propertyOwners.PushBack( object );
61 * Connects a constraint which takes another property as an input.
63 template < typename PropertyType >
64 class Constraint : public ConstraintBase
68 typedef SceneGraph::Constraint< PropertyType, PropertyAccessor<PropertyType> > SceneGraphConstraint;
69 typedef const SceneGraph::AnimatableProperty<PropertyType>* ScenePropertyPtr;
70 typedef typename PropertyConstraintPtr<PropertyType>::Type ConstraintFunctionPtr;
71 typedef const SceneGraph::TransformManagerPropertyHandler<PropertyType> TransformManagerProperty;
74 * Construct a new constraint.
75 * @param[in] object The property-owning object.
76 * @param[in] targetIndex The index of the property to constrain.
77 * @param[in] sources The sources of the input properties passed to func.
78 * @param[in] func The constraint function.
79 * @return A newly allocated active-constraint.
81 static ConstraintBase* New( Object& object,
82 Property::Index targetIndex,
83 SourceContainer& sources,
84 ConstraintFunctionPtr func )
86 return new Constraint< PropertyType >( object, targetIndex, sources, func );
90 * @copydoc ConstraintBase::Clone()
92 virtual ConstraintBase* Clone( Object& object )
94 DALI_ASSERT_ALWAYS( !mSourceDestroyed && "An input source object has been destroyed" );
96 ConstraintBase* clone( NULL );
98 ConstraintFunctionPtr funcPtr( mUserFunction->Clone() );
100 clone = new Constraint< PropertyType >( object,
105 clone->SetRemoveAction(mRemoveAction);
106 clone->SetTag( mTag );
113 * Virtual destructor.
115 virtual ~Constraint()
117 // This is not responsible for removing constraints.
123 * Private constructor; see also Constraint::New().
125 Constraint( Object& object,
126 Property::Index targetIndex,
127 SourceContainer& sources,
128 ConstraintFunctionPtr& func )
129 : ConstraintBase( object, targetIndex, sources ),
130 mTargetIndex( targetIndex ),
131 mUserFunction( func )
136 Constraint( const Constraint& );
139 Constraint& operator=( const Constraint& rhs );
142 * Create and connect a constraint and property resetter for a scene-graph property
144 void ConnectConstraint()
146 // Should not come here if target object has been destroyed
147 DALI_ASSERT_DEBUG( NULL != mTargetObject );
149 // Guard against double connections
150 DALI_ASSERT_DEBUG( NULL == mSceneGraphConstraint );
152 SceneGraph::PropertyOwner& targetObject = const_cast< SceneGraph::PropertyOwner& >( mTargetObject->GetSceneObject() );
154 // Build a container of property-owners, providing the scene-graph properties
155 SceneGraph::PropertyOwnerContainer propertyOwners;
156 propertyOwners.PushBack( &targetObject );
158 // Build the constraint function; this requires a scene-graph property from each source
159 ConstraintFunctionPtr func( ConnectConstraintFunction( propertyOwners ) );
163 // Create the SceneGraphConstraint and PropertyResetter, and connect them to the scene-graph
165 const SceneGraph::PropertyBase* targetProperty = mTargetObject->GetSceneObjectAnimatableProperty( mTargetIndex );
166 OwnerPointer<SceneGraph::PropertyResetterBase> resetter;
168 // The targetProperty should exist, when targetObject exists
169 DALI_ASSERT_ALWAYS( NULL != targetProperty && "Constraint target property does not exist" );
171 if( targetProperty->IsTransformManagerProperty() ) //It is a property managed by the transform manager
173 // Connect the constraint
174 mSceneGraphConstraint =
175 SceneGraph::Constraint<PropertyType, TransformManagerPropertyAccessor<PropertyType> >::New( *targetProperty,
179 // Don't create a resetter for transform manager property, it's less efficient
181 else //SceneGraph property
183 // Connect the constraint
184 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty,
188 // Connect the resetter
189 resetter = SceneGraph::ConstraintResetter::New( targetObject, *targetProperty, *mSceneGraphConstraint );
192 OwnerPointer< SceneGraph::ConstraintBase > transferOwnership( const_cast< SceneGraph::ConstraintBase* >( mSceneGraphConstraint ) );
193 ApplyConstraintMessage( GetEventThreadServices(), targetObject, transferOwnership );
195 if( resetter != nullptr )
197 AddResetterMessage( GetEventThreadServices().GetUpdateManager(), resetter );
203 * Helper for ConnectConstraint. Creates a connected constraint-function.
204 * Also populates the property-owner container, for each input connected to the constraint-function.
205 * @param[out] propertyOwners The container of property-owners providing the scene-graph properties.
206 * @return A connected constraint-function, or NULL if the scene-graph properties are not available.
208 PropertyConstraint<PropertyType>* ConnectConstraintFunction( SceneGraph::PropertyOwnerContainer& propertyOwners )
210 PropertyConstraint<PropertyType>* func = mUserFunction->Clone();
212 for ( SourceIter iter = mSources.begin(); mSources.end() != iter; ++iter )
214 Source& source = *iter;
216 PropertyInputImpl* inputProperty( NULL );
217 int componentIndex( Property::INVALID_COMPONENT_INDEX );
219 if ( OBJECT_PROPERTY == source.sourceType )
221 DALI_ASSERT_ALWAYS( source.object->IsPropertyAConstraintInput( source.propertyIndex ) );
223 SceneGraph::PropertyOwner& owner = const_cast< SceneGraph::PropertyOwner& >( source.object->GetSceneObject() );
225 AddUnique( propertyOwners, &owner );
226 inputProperty = const_cast< PropertyInputImpl* >( source.object->GetSceneObjectInputProperty( source.propertyIndex ) );
227 componentIndex = source.object->GetPropertyComponentIndex( source.propertyIndex );
229 // The scene-object property should exist, when the property owner exists
230 DALI_ASSERT_ALWAYS( NULL != inputProperty && "Constraint source property does not exist" );
232 else if ( LOCAL_PROPERTY == source.sourceType )
234 DALI_ASSERT_ALWAYS( mTargetObject->IsPropertyAConstraintInput( source.propertyIndex ) );
236 inputProperty = const_cast< PropertyInputImpl* >( mTargetObject->GetSceneObjectInputProperty( source.propertyIndex ) );
237 componentIndex = mTargetObject->GetPropertyComponentIndex( source.propertyIndex );
239 // The target scene-object should provide this property
240 DALI_ASSERT_ALWAYS( NULL != inputProperty && "Constraint source property does not exist" );
244 DALI_ASSERT_ALWAYS( PARENT_PROPERTY == source.sourceType && "Constraint source property type is invalid" );
246 Object* objectParent = dynamic_cast< Actor& >( *mTargetObject ).GetParent();
248 // This will not exist, if the target object is off-stage
249 if ( NULL != objectParent )
251 DALI_ASSERT_ALWAYS( objectParent->IsPropertyAConstraintInput( source.propertyIndex ) );
253 SceneGraph::PropertyOwner& owner = const_cast< SceneGraph::PropertyOwner& >( objectParent->GetSceneObject() );
255 AddUnique( propertyOwners, &owner );
256 inputProperty = const_cast< PropertyInputImpl* >( objectParent->GetSceneObjectInputProperty( source.propertyIndex ) );
257 componentIndex = objectParent->GetPropertyComponentIndex( source.propertyIndex );
259 // The scene-object property should exist, when the property owner exists
260 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() );
377 // Build a container of property-owners, providing the scene-graph properties
378 SceneGraph::PropertyOwnerContainer propertyOwners;
379 propertyOwners.PushBack( &targetObject );
381 // Build the constraint function; this requires a scene-graph property from each source
382 ConstraintFunctionPtr func( ConnectConstraintFunction( propertyOwners ) );
386 // Create the SceneGraphConstraint, and connect to the scene-graph
388 const SceneGraph::PropertyBase* targetProperty = mTargetObject->GetSceneObjectAnimatableProperty( mTargetIndex );
389 OwnerPointer<SceneGraph::PropertyResetterBase> resetter;
391 // The targetProperty should exist, when targetObject exists
392 DALI_ASSERT_ALWAYS( NULL != targetProperty && "Constraint target property does not exist" );
394 const int componentIndex = mTargetObject->GetPropertyComponentIndex( mTargetIndex );
396 if ( Property::INVALID_COMPONENT_INDEX == componentIndex )
398 // Not a Vector2, Vector3 or Vector4 component, expecting float type
399 DALI_ASSERT_DEBUG( PropertyTypes::Get< float >() == targetProperty->GetType() );
401 typedef SceneGraph::Constraint< float, PropertyAccessor<float> > SceneGraphConstraint;
403 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
404 resetter = SceneGraph::ConstraintResetter::New( targetObject, *targetProperty, *mSceneGraphConstraint );
408 // Expecting Vector2, Vector3 or Vector4 type
410 if ( PropertyTypes::Get< Vector2 >() == targetProperty->GetType() )
412 // Constrain float component of Vector2 property
414 if ( 0 == componentIndex )
416 typedef SceneGraph::Constraint< float, PropertyComponentAccessorX<Vector2> > SceneGraphConstraint;
417 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
419 else if ( 1 == componentIndex )
421 typedef SceneGraph::Constraint< float, PropertyComponentAccessorY<Vector2> > SceneGraphConstraint;
422 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
424 if( mSceneGraphConstraint )
426 resetter = SceneGraph::ConstraintResetter::New( targetObject, *targetProperty, *mSceneGraphConstraint );
429 else if ( PropertyTypes::Get< Vector3 >() == targetProperty->GetType() )
431 // Constrain float component of Vector3 property
432 if( targetProperty->IsTransformManagerProperty() )
434 if ( 0 == componentIndex )
436 typedef SceneGraph::Constraint< float, TransformManagerPropertyComponentAccessor<Vector3,0> > SceneGraphConstraint;
437 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
439 else if ( 1 == componentIndex )
441 typedef SceneGraph::Constraint< float, TransformManagerPropertyComponentAccessor<Vector3,1> > SceneGraphConstraint;
442 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
444 else if ( 2 == componentIndex )
446 typedef SceneGraph::Constraint< float, TransformManagerPropertyComponentAccessor<Vector3,2> > SceneGraphConstraint;
447 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
449 // Do not create a resetter for transform manager property
453 if ( 0 == componentIndex )
455 typedef SceneGraph::Constraint< float, PropertyComponentAccessorX<Vector3> > SceneGraphConstraint;
456 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
458 else if ( 1 == componentIndex )
460 typedef SceneGraph::Constraint< float, PropertyComponentAccessorY<Vector3> > SceneGraphConstraint;
461 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
463 else if ( 2 == componentIndex )
465 typedef SceneGraph::Constraint< float, PropertyComponentAccessorZ<Vector3> > SceneGraphConstraint;
466 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
468 if( mSceneGraphConstraint )
470 resetter = SceneGraph::ConstraintResetter::New( targetObject, *targetProperty, *mSceneGraphConstraint );
474 else if ( PropertyTypes::Get< Vector4 >() == targetProperty->GetType() )
476 // Constrain float component of Vector4 property
478 if ( 0 == componentIndex )
480 typedef SceneGraph::Constraint< float, PropertyComponentAccessorX<Vector4> > SceneGraphConstraint;
481 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
483 else if ( 1 == componentIndex )
485 typedef SceneGraph::Constraint< float, PropertyComponentAccessorY<Vector4> > SceneGraphConstraint;
486 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
488 else if ( 2 == componentIndex )
490 typedef SceneGraph::Constraint< float, PropertyComponentAccessorZ<Vector4> > SceneGraphConstraint;
491 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
493 else if ( 3 == componentIndex )
495 typedef SceneGraph::Constraint< float, PropertyComponentAccessorW<Vector4> > SceneGraphConstraint;
496 mSceneGraphConstraint = SceneGraphConstraint::New( *targetProperty, propertyOwners, func, mRemoveAction );
499 if( mSceneGraphConstraint )
501 resetter = SceneGraph::ConstraintResetter::New( targetObject, *targetProperty, *mSceneGraphConstraint );
506 if( mSceneGraphConstraint )
508 OwnerPointer< SceneGraph::ConstraintBase > transferOwnership( const_cast< SceneGraph::ConstraintBase* >( mSceneGraphConstraint ) );
509 ApplyConstraintMessage( GetEventThreadServices(), targetObject, transferOwnership );
511 if( resetter != nullptr )
513 AddResetterMessage( GetEventThreadServices().GetUpdateManager(), resetter );
519 * Helper for ConnectConstraint. Creates a connected constraint-function.
520 * Also populates the property-owner container, for each input connected to the constraint-function.
521 * @param[out] propertyOwners The container of property-owners providing the scene-graph properties.
522 * @return A connected constraint-function, or NULL if the scene-graph properties are not available.
524 PropertyConstraint<float>* ConnectConstraintFunction( SceneGraph::PropertyOwnerContainer& propertyOwners )
526 PropertyConstraint<float>* func = mUserFunction->Clone();
528 for ( SourceIter iter = mSources.begin(); mSources.end() != iter; ++iter )
530 Source& source = *iter;
532 PropertyInputImpl* inputProperty( NULL );
533 int componentIndex( Property::INVALID_COMPONENT_INDEX );
535 if ( OBJECT_PROPERTY == source.sourceType )
537 DALI_ASSERT_ALWAYS( source.object->IsPropertyAConstraintInput( source.propertyIndex ) );
539 SceneGraph::PropertyOwner& owner = const_cast< SceneGraph::PropertyOwner& >( source.object->GetSceneObject() );
541 // The property owner will not exist, if the target object is off-stage
542 AddUnique( propertyOwners, &owner );
543 inputProperty = const_cast< PropertyInputImpl* >( source.object->GetSceneObjectInputProperty( source.propertyIndex ) );
544 componentIndex = source.object->GetPropertyComponentIndex( source.propertyIndex );
546 // The scene-object property should exist, when the property owner exists
547 DALI_ASSERT_ALWAYS( NULL != inputProperty && "Constraint source property does not exist" );
549 else if ( LOCAL_PROPERTY == source.sourceType )
551 DALI_ASSERT_ALWAYS( mTargetObject->IsPropertyAConstraintInput( source.propertyIndex ) );
553 inputProperty = const_cast< PropertyInputImpl* >( mTargetObject->GetSceneObjectInputProperty( source.propertyIndex ) );
554 componentIndex = mTargetObject->GetPropertyComponentIndex( source.propertyIndex );
556 // The target scene-object should provide this property
557 DALI_ASSERT_ALWAYS( NULL != inputProperty && "Constraint source property does not exist" );
561 DALI_ASSERT_ALWAYS( PARENT_PROPERTY == source.sourceType && "Constraint source property type is invalid" );
563 Object* objectParent = dynamic_cast< Actor& >( *mTargetObject ).GetParent();
565 // This will not exist, if the target object is off-stage
566 if ( NULL != objectParent )
568 DALI_ASSERT_ALWAYS( objectParent->IsPropertyAConstraintInput( source.propertyIndex ) );
570 SceneGraph::PropertyOwner& owner = const_cast< SceneGraph::PropertyOwner& >( objectParent->GetSceneObject() );
572 // The property owner will not exist, if the parent object is off-stage
573 AddUnique( propertyOwners, &owner );
574 inputProperty = const_cast< PropertyInputImpl* >( objectParent->GetSceneObjectInputProperty( source.propertyIndex ) );
575 componentIndex = objectParent->GetPropertyComponentIndex( source.propertyIndex );
577 // The scene-object property should exist, when the property owner exists
578 DALI_ASSERT_ALWAYS( NULL != inputProperty && "Constraint source property does not exist" );
582 if ( NULL == inputProperty )
587 // Exit if a scene-graph object is not available from one of the sources
591 func->SetInput( ( iter - mSources.begin() ), componentIndex, *inputProperty );
599 Property::Index mTargetIndex;
601 ConstraintFunctionPtr mUserFunction;
604 } // namespace Internal
608 #endif // __DALI_INTERNAL_ACTIVE_CONSTRAINT_H__