1 #ifndef __DALI_INTERNAL_ANIMATOR_CONNECTOR_H__
2 #define __DALI_INTERNAL_ANIMATOR_CONNECTOR_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/event/animation/animator-connector-base.h>
23 #include <dali/internal/event/animation/animation-impl.h>
24 #include <dali/internal/update/common/property-owner.h>
25 #include <dali/internal/update/animation/property-accessor.h>
26 #include <dali/internal/update/animation/property-component-accessor.h>
27 #include <dali/internal/update/common/property-resetter.h>
28 #include <dali/internal/update/manager/update-manager.h>
37 * AnimatorConnector is used to connect SceneGraph::Animators and
38 * PropertyResetters for newly created scene-graph objects.
40 * SceneGraph::Animators weakly reference scene objects, and are automatically deleted when orphaned.
41 * Therefore the AnimatorConnector is NOT responsible for disconnecting animators.
43 template < typename PropertyType >
44 class AnimatorConnector : public AnimatorConnectorBase
48 typedef SceneGraph::Animator< PropertyType, PropertyAccessor<PropertyType> > AnimatorType;
49 typedef SceneGraph::AnimatableProperty< PropertyType > PropertyInterfaceType;
52 * Construct a new animator connector.
53 * @param[in] object The object for a scene-graph object to animate.
54 * @param[in] propertyIndex The index of a property provided by the object.
55 * @param[in] componentIndex Index to a sub component of a property, for use with Vector2, Vector3 and Vector4 (INVALID_PROPERTY_COMPONENTINDEX to use the whole property)
56 * @param[in] animatorFunction A function used to animate the property.
57 * @param[in] alpha The alpha function to apply.
58 * @param[in] period The time period of the animator.
59 * @return A pointer to a newly allocated animator connector.
61 static AnimatorConnectorBase* New( Object& object,
62 Property::Index propertyIndex,
64 AnimatorFunctionBase* animatorFunction,
66 const TimePeriod& period )
68 return new AnimatorConnector< PropertyType >( object,
79 virtual ~AnimatorConnector()
83 mObject->RemoveObserver( *this );
86 // If there is not a SceneGraph::Animator, the AnimatorConnector is responsible for deleting the mAnimatorFunction
87 // otherwise, the animator function ownership is transferred to the SceneGraph::Animator
90 delete mAnimatorFunction;
91 mAnimatorFunction = 0;
96 * From AnimatorConnectorBase.
97 * This is only expected to be called once, when added to an Animation.
99 void SetParent( Animation& parent )
101 DALI_ASSERT_ALWAYS( mParent == NULL && "AnimationConnector already has a parent" );
104 if( mObject && mObject->GetSceneObject() )
113 * Private constructor; see also AnimatorConnector::New().
115 AnimatorConnector( Object& object,
116 Property::Index propertyIndex,
118 Internal::AnimatorFunctionBase* animatorFunction,
120 const TimePeriod& period )
121 : AnimatorConnectorBase( object, propertyIndex, componentIndex, alpha, period ),
123 mAnimatorFunction( animatorFunction )
128 AnimatorConnector( const AnimatorConnector& );
131 AnimatorConnector& operator=( const AnimatorConnector& rhs );
134 * From Object::Observer
136 virtual void SceneObjectAdded( Object& object )
138 //If the animator has not been created yet, create it now.
139 if( !mAnimator && mObject )
146 * Helper function to create a Scenegraph::Animator and PropertyResetter and add it to its correspondent
147 * SceneGraph::Animation.
149 * @note This function will only be called the first time the object is added to the scene or at creation time if
150 * the object was already in the scene
152 void CreateAnimator()
154 DALI_ASSERT_DEBUG( mAnimator == NULL );
155 DALI_ASSERT_DEBUG( mAnimatorFunction != NULL );
156 DALI_ASSERT_DEBUG( mParent != NULL );
158 //Get the PropertyOwner the animator is going to animate
159 const SceneGraph::PropertyOwner* propertyOwner = mObject->GetSceneObject();
161 // Get SceneGraph::BaseProperty
162 const SceneGraph::PropertyBase* baseProperty = mObject->GetSceneObjectAnimatableProperty( mPropertyIndex );
164 OwnerPointer<SceneGraph::PropertyResetterBase> resetter;
166 // Check if property is a component of another property
167 const int componentIndex = mObject->GetPropertyComponentIndex( mPropertyIndex );
168 if( componentIndex != Property::INVALID_COMPONENT_INDEX )
170 mComponentIndex = componentIndex;
173 if( mComponentIndex == Property::INVALID_COMPONENT_INDEX )
175 // Animating the whole property
177 // Cast to AnimatableProperty
178 const PropertyInterfaceType* animatableProperty = dynamic_cast< const PropertyInterfaceType* >( baseProperty );
180 if( animatableProperty == NULL )
182 if( baseProperty->IsTransformManagerProperty() )
184 mAnimator = SceneGraph::AnimatorTransformProperty< PropertyType,TransformManagerPropertyAccessor<PropertyType> >::New( *propertyOwner, *baseProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
185 // Don't reset transform manager properties - TransformManager will do it more efficiently
189 DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
195 // Create the animator and resetter
196 mAnimator = AnimatorType::New( *propertyOwner, *animatableProperty, mAnimatorFunction,
197 mAlphaFunction, mTimePeriod );
198 resetter = SceneGraph::AnimatorResetter::New( *propertyOwner, *baseProperty, *mAnimator );
204 // Animating a component of the property
205 if ( PropertyTypes::Get< Vector2 >() == baseProperty->GetType() )
207 // Animate float component of Vector2 property
209 // Cast to AnimatableProperty of type Vector2
210 const SceneGraph::AnimatableProperty<Vector2>* animatableProperty = dynamic_cast< const SceneGraph::AnimatableProperty<Vector2>* >( baseProperty );
211 DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
213 switch( mComponentIndex )
217 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorX<Vector2> >::New( *propertyOwner,
226 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorY<Vector2> >::New( *propertyOwner,
239 if( mAnimator != nullptr )
241 resetter = SceneGraph::AnimatorResetter::New( *propertyOwner, *baseProperty, *mAnimator );
245 else if ( PropertyTypes::Get< Vector3 >() == baseProperty->GetType() )
247 // Animate float component of Vector3 property
248 // Cast to AnimatableProperty of type Vector3
249 const SceneGraph::AnimatableProperty<Vector3>* animatableProperty = dynamic_cast< const SceneGraph::AnimatableProperty<Vector3>* >( baseProperty );
251 if( animatableProperty == NULL )
253 if( baseProperty->IsTransformManagerProperty() )
255 if( mComponentIndex == 0 )
257 mAnimator = SceneGraph::AnimatorTransformProperty< float,TransformManagerPropertyComponentAccessor<Vector3,0> >::New( *propertyOwner, *baseProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
259 else if( mComponentIndex == 1 )
261 mAnimator = SceneGraph::AnimatorTransformProperty< float,TransformManagerPropertyComponentAccessor<Vector3,1> >::New( *propertyOwner, *baseProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
263 else if( mComponentIndex == 2 )
265 mAnimator = SceneGraph::AnimatorTransformProperty< float,TransformManagerPropertyComponentAccessor<Vector3,2> >::New( *propertyOwner, *baseProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
270 DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
272 // Don't manually reset transform property - TransformManager will do it more efficiently
276 // Dynamic cast will fail if BaseProperty is not a Vector3 AnimatableProperty
277 DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
279 switch( mComponentIndex )
283 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorX<Vector3> >::New( *propertyOwner,
292 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorY<Vector3> >::New( *propertyOwner,
301 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorZ<Vector3> >::New( *propertyOwner,
314 if( mAnimator != nullptr )
316 resetter = SceneGraph::AnimatorResetter::New( *propertyOwner, *baseProperty, *mAnimator );
320 else if ( PropertyTypes::Get< Vector4 >() == baseProperty->GetType() )
322 // Animate float component of Vector4 property
324 // Cast to AnimatableProperty of type Vector4
325 const SceneGraph::AnimatableProperty<Vector4>* animatableProperty = dynamic_cast< const SceneGraph::AnimatableProperty<Vector4>* >( baseProperty );
327 //Dynamic cast will fail if BaseProperty is not a Vector4 AnimatableProperty
328 DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
330 switch( mComponentIndex )
334 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorX<Vector4> >::New( *propertyOwner,
343 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorY<Vector4> >::New( *propertyOwner,
352 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorZ<Vector4> >::New( *propertyOwner,
361 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorW<Vector4> >::New( *propertyOwner,
374 if( mAnimator != nullptr )
376 resetter = SceneGraph::AnimatorResetter::New( *propertyOwner, *baseProperty, *mAnimator );
382 DALI_ASSERT_DEBUG( mAnimator != NULL );
384 // Add the new SceneGraph::Animator to its correspondent SceneGraph::Animation via message
385 const SceneGraph::Animation* animation = mParent->GetSceneObject();
386 DALI_ASSERT_DEBUG( NULL != animation );
387 AddAnimatorMessage( mParent->GetEventThreadServices(), *animation, *mAnimator );
389 // Add the new SceneGraph::PropertyResetter to the update manager via message
390 if( resetter != nullptr )
392 AddResetterMessage( mParent->GetEventThreadServices().GetUpdateManager(), resetter );
398 SceneGraph::AnimatorBase* mAnimator;
400 Internal::AnimatorFunctionBase* mAnimatorFunction; ///< Owned by the animator connector until an Scenegraph::Animator is created
404 } // namespace Internal
408 #endif // __DALI_INTERNAL_ANIMATOR_CONNECTOR_H__