1 #ifndef __DALI_INTERNAL_ANIMATOR_CONNECTOR_H__
2 #define __DALI_INTERNAL_ANIMATOR_CONNECTOR_H__
5 * Copyright (c) 2015 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/manager/update-manager.h>
36 * AnimatorConnector is used to connect SceneGraph::Animators for newly created scene-graph objects.
38 * SceneGraph::Animators weakly reference scene objects, and are automatically deleted when orphaned.
39 * Therefore the AnimatorConnector is NOT responsible for disconnecting animators.
41 template < typename PropertyType >
42 class AnimatorConnector : public AnimatorConnectorBase
46 typedef SceneGraph::Animator< PropertyType, PropertyAccessor<PropertyType> > AnimatorType;
47 typedef SceneGraph::AnimatableProperty< PropertyType > PropertyInterfaceType;
50 * Construct a new animator connector.
51 * @param[in] object The object for a scene-graph object to animate.
52 * @param[in] propertyIndex The index of a property provided by the object.
53 * @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)
54 * @param[in] animatorFunction A function used to animate the property.
55 * @param[in] alpha The alpha function to apply.
56 * @param[in] period The time period of the animator.
57 * @return A pointer to a newly allocated animator connector.
59 static AnimatorConnectorBase* New( Object& object,
60 Property::Index propertyIndex,
62 AnimatorFunctionBase* animatorFunction,
64 const TimePeriod& period )
66 return new AnimatorConnector< PropertyType >( object,
77 virtual ~AnimatorConnector()
81 mObject->RemoveObserver( *this );
84 //If there is not a SceneGraph::Animator, the AnimatorConnector is responsible for deleting the mAnimatorFunction
85 //otherwise, the animator function ownership is transferred to the SceneGraph::Animator
88 delete mAnimatorFunction;
89 mAnimatorFunction = 0;
94 * From AnimatorConnectorBase.
95 * This is only expected to be called once, when added to an Animation.
97 void SetParent( Animation& parent )
99 DALI_ASSERT_ALWAYS( mParent == NULL && "AnimationConnector already has a parent" );
102 if( mObject && mObject->GetSceneObject() )
111 * Private constructor; see also AnimatorConnector::New().
113 AnimatorConnector( Object& object,
114 Property::Index propertyIndex,
116 Internal::AnimatorFunctionBase* animatorFunction,
118 const TimePeriod& period )
119 : AnimatorConnectorBase( object, propertyIndex, componentIndex, alpha, period ),
121 mAnimatorFunction( animatorFunction )
126 AnimatorConnector( const AnimatorConnector& );
129 AnimatorConnector& operator=( const AnimatorConnector& rhs );
132 * From Object::Observer
134 virtual void SceneObjectAdded( Object& object )
136 //If the animator has not been created yet, create it now.
137 if( !mAnimator && mObject )
144 * Helper function to create a Scenegraph::Animator and add it to its correspondent SceneGraph::Animation.
145 * @note This function will only be called the first time the object is added to the scene or at creation time if
146 * the object was already in the scene
148 void CreateAnimator()
150 DALI_ASSERT_DEBUG( mAnimator == NULL );
151 DALI_ASSERT_DEBUG( mAnimatorFunction != NULL );
152 DALI_ASSERT_DEBUG( mParent != NULL );
154 //Get the PropertyOwner the animator is going to animate
155 const SceneGraph::PropertyOwner* propertyOwner = mObject->GetSceneObject();
157 //Get SceneGraph::BaseProperty
158 const SceneGraph::PropertyBase* baseProperty = mObject->GetSceneObjectAnimatableProperty( mPropertyIndex );
160 //Check if property is a component of another property
161 const int componentIndex = mObject->GetPropertyComponentIndex( mPropertyIndex );
162 if( componentIndex != Property::INVALID_COMPONENT_INDEX )
164 mComponentIndex = componentIndex;
167 if( mComponentIndex == Property::INVALID_COMPONENT_INDEX )
169 ///Animating the whole property
171 //Cast to AnimatableProperty
172 const PropertyInterfaceType* animatableProperty = dynamic_cast< const PropertyInterfaceType* >( baseProperty );
174 if( animatableProperty == NULL )
176 if( baseProperty->IsTransformManagerProperty() )
178 mAnimator = SceneGraph::AnimatorTransformProperty< PropertyType,TransformManagerPropertyAccessor<PropertyType> >::New( *propertyOwner, *baseProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
182 DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
188 //Create the animator
189 mAnimator = AnimatorType::New( *propertyOwner, *animatableProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
196 ///Animating a component of the property
197 if ( PropertyTypes::Get< Vector2 >() == baseProperty->GetType() )
199 // Animate float component of Vector2 property
201 // Cast to AnimatableProperty of type Vector2
202 const SceneGraph::AnimatableProperty<Vector2>* animatableProperty = dynamic_cast< const SceneGraph::AnimatableProperty<Vector2>* >( baseProperty );
203 DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
205 switch( mComponentIndex )
209 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorX<Vector2> >::New( *propertyOwner,
218 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorY<Vector2> >::New( *propertyOwner,
232 else if ( PropertyTypes::Get< Vector3 >() == baseProperty->GetType() )
234 // Animate float component of Vector3 property
235 // Cast to AnimatableProperty of type Vector3
236 const SceneGraph::AnimatableProperty<Vector3>* animatableProperty = dynamic_cast< const SceneGraph::AnimatableProperty<Vector3>* >( baseProperty );
238 if( animatableProperty == NULL )
240 if( baseProperty->IsTransformManagerProperty() )
242 if( mComponentIndex == 0 )
244 mAnimator = SceneGraph::AnimatorTransformProperty< float,TransformManagerPropertyComponentAccessor<Vector3,0> >::New( *propertyOwner, *baseProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
246 else if( mComponentIndex == 1 )
248 mAnimator = SceneGraph::AnimatorTransformProperty< float,TransformManagerPropertyComponentAccessor<Vector3,1> >::New( *propertyOwner, *baseProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
250 else if( mComponentIndex == 2 )
252 mAnimator = SceneGraph::AnimatorTransformProperty< float,TransformManagerPropertyComponentAccessor<Vector3,2> >::New( *propertyOwner, *baseProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
257 DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
262 //Dynamic cast will fail if BaseProperty is not a Vector3 AnimatableProperty
263 DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
265 switch( mComponentIndex )
269 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorX<Vector3> >::New( *propertyOwner,
278 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorY<Vector3> >::New( *propertyOwner,
287 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorZ<Vector3> >::New( *propertyOwner,
301 else if ( PropertyTypes::Get< Vector4 >() == baseProperty->GetType() )
303 // Animate float component of Vector4 property
305 // Cast to AnimatableProperty of type Vector4
306 const SceneGraph::AnimatableProperty<Vector4>* animatableProperty = dynamic_cast< const SceneGraph::AnimatableProperty<Vector4>* >( baseProperty );
308 //Dynamic cast will fail if BaseProperty is not a Vector4 AnimatableProperty
309 DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
311 switch( mComponentIndex )
315 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorX<Vector4> >::New( *propertyOwner,
324 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorY<Vector4> >::New( *propertyOwner,
333 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorZ<Vector4> >::New( *propertyOwner,
342 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorW<Vector4> >::New( *propertyOwner,
359 DALI_ASSERT_DEBUG( mAnimator != NULL );
361 // Add the new SceneGraph::Animator to its correspondent SceneGraph::Animation via message
362 const SceneGraph::Animation* animation = mParent->GetSceneObject();
363 DALI_ASSERT_DEBUG( NULL != animation );
364 AddAnimatorMessage( mParent->GetEventThreadServices(), *animation, *mAnimator );
369 SceneGraph::AnimatorBase* mAnimator;
371 Internal::AnimatorFunctionBase* mAnimatorFunction; ///< Owned by the animator connector until an Scenegraph::Animator is created
375 } // namespace Internal
379 #endif // __DALI_INTERNAL_ANIMATOR_CONNECTOR_H__