1 #ifndef __DALI_INTERNAL_ANIMATOR_CONNECTOR_H__
2 #define __DALI_INTERNAL_ANIMATOR_CONNECTOR_H__
5 * Copyright (c) 2014 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/common/object-impl.h>
23 #include <dali/internal/event/animation/animator-connector-base.h>
24 #include <dali/internal/event/animation/animation-impl.h>
25 #include <dali/internal/update/common/property-owner.h>
26 #include <dali/internal/update/animation/property-accessor.h>
27 #include <dali/internal/update/animation/property-component-accessor.h>
28 #include <dali/internal/update/animation/scene-graph-animator.h>
29 #include <dali/internal/update/manager/update-manager.h>
38 * AnimatorConnector is used to connect SceneGraph::Animators for newly created scene-graph objects.
40 * The scene-graph objects are created by a Object e.g. Actor is a proxy for SceneGraph::Node.
41 * AnimatorConnector observes the proxy object, in order to detect when a scene-graph object is created.
43 * SceneGraph::Animators weakly reference scene objects, and are automatically deleted when orphaned.
44 * Therefore the AnimatorConnector is NOT responsible for disconnecting animators.
46 template < typename PropertyType >
47 class AnimatorConnector : public AnimatorConnectorBase, public Object::Observer
51 typedef SceneGraph::Animator< PropertyType, PropertyAccessor<PropertyType> > AnimatorType;
52 typedef SceneGraph::AnimatableProperty< PropertyType > PropertyInterfaceType;
55 * Construct a new animator connector.
56 * @param[in] object The object for a scene-graph object to animate.
57 * @param[in] propertyIndex The index of a property provided by the object.
58 * @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)
59 * @param[in] animatorFunction A function used to animate the property.
60 * @param[in] alpha The alpha function to apply.
61 * @param[in] period The time period of the animator.
62 * @return A pointer to a newly allocated animator connector.
64 static AnimatorConnectorBase* New( Object& object,
65 Property::Index propertyIndex,
67 AnimatorFunctionBase* animatorFunction,
69 const TimePeriod& period )
71 return new AnimatorConnector< PropertyType >( object,
82 virtual ~AnimatorConnector()
86 mObject->RemoveObserver( *this );
89 //If there is not a SceneGraph::Animator, the AnimatorConnector is responsible for deleting the mAnimatorFunction
90 //otherwise, the animator function ownership is transferred to the SceneGraph::Animator
93 delete mAnimatorFunction;
94 mAnimatorFunction = 0;
99 * From AnimatorConnectorBase.
100 * This is only expected to be called once, when added to an Animation.
102 void SetParent( Animation& parent )
104 DALI_ASSERT_ALWAYS( mParent == NULL && "AnimationConnector already has a parent" );
107 if( mObject && mObject->GetSceneObject() )
116 * Private constructor; see also AnimatorConnector::New().
118 AnimatorConnector( Object& object,
119 Property::Index propertyIndex,
121 Internal::AnimatorFunctionBase* animatorFunction,
123 const TimePeriod& period )
124 : AnimatorConnectorBase( alpha, period ),
127 mPropertyIndex( propertyIndex ),
128 mComponentIndex( componentIndex ),
129 mAnimatorFunction( animatorFunction )
131 object.AddObserver( *this );
135 AnimatorConnector( const AnimatorConnector& );
138 AnimatorConnector& operator=( const AnimatorConnector& rhs );
141 * From Object::Observer
143 virtual void SceneObjectAdded( Object& object )
145 //If the animator has not been created yet, create it now.
153 * From Object::Observer
155 virtual void SceneObjectRemoved( Object& object )
160 * From Object::Observer
162 virtual void ObjectDestroyed( Object& object )
168 * Helper function to create a Scenegraph::Animator and add it to its correspondent SceneGraph::Animation.
169 * @note This function will only be called the first time the object is added to the scene or at creation time if
170 * the object was already in the scene
172 void CreateAnimator()
174 DALI_ASSERT_DEBUG( mAnimator == NULL );
175 DALI_ASSERT_DEBUG( mAnimatorFunction != NULL );
176 DALI_ASSERT_DEBUG( mParent != NULL );
178 //Get the PropertyOwner the animator is going to animate
179 const SceneGraph::PropertyOwner* propertyOwner = mObject->GetSceneObject();
181 //Get SceneGraph::BaseProperty
182 const SceneGraph::PropertyBase* baseProperty = mObject->GetSceneObjectAnimatableProperty( mPropertyIndex );
184 //Check if property is a component of another property
185 const int componentIndex = mObject->GetPropertyComponentIndex( mPropertyIndex );
186 if( componentIndex != Property::INVALID_COMPONENT_INDEX )
188 mComponentIndex = componentIndex;
191 if( mComponentIndex == Property::INVALID_COMPONENT_INDEX )
193 ///Animating the whole property
195 //Cast to AnimatableProperty
196 const PropertyInterfaceType* animatableProperty = dynamic_cast< const PropertyInterfaceType* >( baseProperty );
198 //Dynamic cast will fail if BaseProperty is not an AnimatableProperty
199 DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
201 //Create the animator
202 mAnimator = AnimatorType::New( *propertyOwner, *animatableProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
207 ///Animating a component of the property
210 if ( PropertyTypes::Get< Vector3 >() == baseProperty->GetType() )
212 // Cast to AnimatableProperty of type Vector3
213 const SceneGraph::AnimatableProperty<Vector3>* animatableProperty = dynamic_cast< const SceneGraph::AnimatableProperty<Vector3>* >( baseProperty );
215 //Dynamic cast will fail if BaseProperty is not a Vector3 AnimatableProperty
216 DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
218 switch( mComponentIndex )
222 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorX<Vector3> >::New( *propertyOwner,
231 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorY<Vector3> >::New( *propertyOwner,
240 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorZ<Vector3> >::New( *propertyOwner,
253 else if ( PropertyTypes::Get< Vector4 >() == baseProperty->GetType() )
255 // Animate float component of Vector4 property
257 // Cast to AnimatableProperty of type Vector4
258 const SceneGraph::AnimatableProperty<Vector4>* animatableProperty = dynamic_cast< const SceneGraph::AnimatableProperty<Vector4>* >( baseProperty );
260 //Dynamic cast will fail if BaseProperty is not a Vector4 AnimatableProperty
261 DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
263 switch( mComponentIndex )
267 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorX<Vector4> >::New( *propertyOwner,
276 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorY<Vector4> >::New( *propertyOwner,
285 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorZ<Vector4> >::New( *propertyOwner,
294 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorW<Vector4> >::New( *propertyOwner,
310 DALI_ASSERT_DEBUG( mAnimator != NULL );
312 //Add the new SceneGraph::Animator to its correspondent SceneGraph::Animation via message to UpdateManager
313 const SceneGraph::Animation* animation = mParent->GetSceneObject();
314 DALI_ASSERT_DEBUG( NULL != animation );
315 AddAnimatorMessage( mParent->GetUpdateManager().GetEventToUpdate(), *animation, *mAnimator );
320 Object* mObject; ///< Not owned by the animator connector. Valid until ObjectDestroyed() is called.
321 SceneGraph::AnimatorBase* mAnimator;
323 Property::Index mPropertyIndex;
326 Internal::AnimatorFunctionBase* mAnimatorFunction; ///< Owned by the animator connector until an Scenegraph::Animator is created
330 } // namespace Internal
334 #endif // __DALI_INTERNAL_ANIMATOR_CONNECTOR_H__