1 #ifndef DALI_INTERNAL_ANIMATOR_CONNECTOR_H
2 #define DALI_INTERNAL_ANIMATOR_CONNECTOR_H
5 * Copyright (c) 2019 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>
35 * AnimatorConnector is used to connect SceneGraph::Animators.
37 * SceneGraph::Animators weakly reference scene objects, and are automatically deleted when orphaned.
38 * Therefore the AnimatorConnector is NOT responsible for disconnecting animators.
39 * This is the common template for non float properties, there's a specialization for float properties as they can be component properties
41 template < typename PropertyType >
42 class AnimatorConnector : public AnimatorConnectorBase
46 using AnimatorType = SceneGraph::Animator< PropertyType, PropertyAccessor<PropertyType> >;
47 using PropertyInterfaceType = SceneGraph::AnimatableProperty< PropertyType >;
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,
61 int32_t componentIndex,
62 AnimatorFunctionBase* animatorFunction,
64 const TimePeriod& period )
66 return new AnimatorConnector( object,
77 ~AnimatorConnector() override
84 * Private constructor; see also AnimatorConnector::New().
86 AnimatorConnector( Object& object,
87 Property::Index propertyIndex,
88 int32_t componentIndex,
89 Internal::AnimatorFunctionBase* animatorFunction,
91 const TimePeriod& period )
92 : AnimatorConnectorBase( object, propertyIndex, componentIndex, animatorFunction, alpha, period )
97 AnimatorConnector() = delete;
98 AnimatorConnector( const AnimatorConnector& ) = delete;
99 AnimatorConnector& operator=( const AnimatorConnector& rhs ) = delete;
102 * @copydoc AnimatorConnectorBase::DoCreateAnimator()
104 bool DoCreateAnimator( const SceneGraph::PropertyOwner& propertyOwner, const SceneGraph::PropertyBase& baseProperty ) final
106 bool resetterRequired = false;
107 // components only supported for float property type
108 DALI_ASSERT_DEBUG( mComponentIndex == Property::INVALID_COMPONENT_INDEX );
109 // Animating the whole property
111 // Cast to AnimatableProperty
112 const PropertyInterfaceType* animatableProperty = dynamic_cast< const PropertyInterfaceType* >( &baseProperty );
114 if( animatableProperty == nullptr )
116 if( baseProperty.IsTransformManagerProperty() )
118 mAnimator = SceneGraph::AnimatorTransformProperty< PropertyType,TransformManagerPropertyAccessor<PropertyType> >::New( propertyOwner, baseProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
119 // Don't reset transform manager properties - TransformManager will do it more efficiently
123 DALI_ASSERT_DEBUG( animatableProperty && "Animating non-animatable property" );
128 // Create the animator and resetter
129 mAnimator = AnimatorType::New( propertyOwner, *animatableProperty, mAnimatorFunction,
130 mAlphaFunction, mTimePeriod );
131 resetterRequired = true;
133 return resetterRequired;
138 * Specialization for float as that type supports component properties
141 class AnimatorConnector< float > : public AnimatorConnectorBase
145 using AnimatorType = SceneGraph::Animator< float, PropertyAccessor<float> >;
146 using PropertyInterfaceType = SceneGraph::AnimatableProperty< float >;
149 * Construct a new animator connector.
150 * @param[in] object The object for a scene-graph object to animate.
151 * @param[in] propertyIndex The index of a property provided by the object.
152 * @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)
153 * @param[in] animatorFunction A function used to animate the property.
154 * @param[in] alpha The alpha function to apply.
155 * @param[in] period The time period of the animator.
156 * @return A pointer to a newly allocated animator connector.
158 static AnimatorConnectorBase* New( Object& object,
159 Property::Index propertyIndex,
160 int32_t componentIndex,
161 AnimatorFunctionBase* animatorFunction,
163 const TimePeriod& period )
165 return new AnimatorConnector( object,
174 * Virtual destructor.
176 ~AnimatorConnector() override
183 * Private constructor; see also AnimatorConnector::New().
185 AnimatorConnector( Object& object,
186 Property::Index propertyIndex,
187 int32_t componentIndex,
188 Internal::AnimatorFunctionBase* animatorFunction,
190 const TimePeriod& period )
191 : AnimatorConnectorBase( object, propertyIndex, componentIndex, animatorFunction, alpha, period )
196 AnimatorConnector() = delete;
197 AnimatorConnector( const AnimatorConnector& ) = delete;
198 AnimatorConnector& operator=( const AnimatorConnector& rhs ) = delete;
201 * @copydoc AnimatorConnectorBase::DoCreateAnimator()
203 bool DoCreateAnimator( const SceneGraph::PropertyOwner& propertyOwner, const SceneGraph::PropertyBase& baseProperty ) final
205 bool resetterRequired = false;
206 if( mComponentIndex == Property::INVALID_COMPONENT_INDEX )
208 // Animating the whole property
210 // Cast to AnimatableProperty
211 const PropertyInterfaceType* animatableProperty = dynamic_cast< const PropertyInterfaceType* >( &baseProperty );
213 if( animatableProperty == nullptr )
215 if( baseProperty.IsTransformManagerProperty() )
217 mAnimator = SceneGraph::AnimatorTransformProperty< float,TransformManagerPropertyAccessor<float> >::New( propertyOwner, baseProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
218 // Don't reset transform manager properties - TransformManager will do it more efficiently
222 DALI_ASSERT_DEBUG( animatableProperty && "Animating non-animatable property" );
227 // Create the animator and resetter
228 mAnimator = AnimatorType::New( propertyOwner, *animatableProperty, mAnimatorFunction,
229 mAlphaFunction, mTimePeriod );
230 resetterRequired = true;
236 // Animating a component of the property
237 if ( PropertyTypes::Get< Vector2 >() == baseProperty.GetType() )
239 // Animate float component of Vector2 property
241 // Cast to AnimatableProperty of type Vector2
242 const SceneGraph::AnimatableProperty<Vector2>* animatableProperty = dynamic_cast< const SceneGraph::AnimatableProperty<Vector2>* >( &baseProperty );
243 DALI_ASSERT_DEBUG( animatableProperty && "Animating non-animatable property" );
245 switch( mComponentIndex )
249 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorX<Vector2> >::New( propertyOwner,
258 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorY<Vector2> >::New( propertyOwner,
271 resetterRequired = ( mAnimator != nullptr );
274 else if ( PropertyTypes::Get< Vector3 >() == baseProperty.GetType() )
276 // Animate float component of Vector3 property
277 // Cast to AnimatableProperty of type Vector3
278 const SceneGraph::AnimatableProperty<Vector3>* animatableProperty = dynamic_cast< const SceneGraph::AnimatableProperty<Vector3>* >( &baseProperty );
280 if( animatableProperty == nullptr )
282 if( baseProperty.IsTransformManagerProperty() )
284 if( mComponentIndex == 0 )
286 mAnimator = SceneGraph::AnimatorTransformProperty< float,TransformManagerPropertyComponentAccessor<Vector3,0> >::New( propertyOwner, baseProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
288 else if( mComponentIndex == 1 )
290 mAnimator = SceneGraph::AnimatorTransformProperty< float,TransformManagerPropertyComponentAccessor<Vector3,1> >::New( propertyOwner, baseProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
292 else if( mComponentIndex == 2 )
294 mAnimator = SceneGraph::AnimatorTransformProperty< float,TransformManagerPropertyComponentAccessor<Vector3,2> >::New( propertyOwner, baseProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
299 DALI_ASSERT_DEBUG( animatableProperty && "Animating non-animatable property" );
301 // Don't manually reset transform property - TransformManager will do it more efficiently
305 // Dynamic cast will fail if BaseProperty is not a Vector3 AnimatableProperty
306 DALI_ASSERT_DEBUG( animatableProperty && "Animating non-animatable property" );
308 switch( mComponentIndex )
312 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorX<Vector3> >::New( propertyOwner,
321 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorY<Vector3> >::New( propertyOwner,
330 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorZ<Vector3> >::New( propertyOwner,
343 resetterRequired = ( mAnimator != nullptr );
346 else if ( PropertyTypes::Get< Vector4 >() == baseProperty.GetType() )
348 // Animate float component of Vector4 property
350 // Cast to AnimatableProperty of type Vector4
351 const SceneGraph::AnimatableProperty<Vector4>* animatableProperty = dynamic_cast< const SceneGraph::AnimatableProperty<Vector4>* >( &baseProperty );
353 //Dynamic cast will fail if BaseProperty is not a Vector4 AnimatableProperty
354 DALI_ASSERT_DEBUG( animatableProperty && "Animating non-animatable property" );
356 switch( mComponentIndex )
360 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorX<Vector4> >::New( propertyOwner,
369 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorY<Vector4> >::New( propertyOwner,
378 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorZ<Vector4> >::New( propertyOwner,
387 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorW<Vector4> >::New( propertyOwner,
400 resetterRequired = ( mAnimator != nullptr );
404 return resetterRequired;
408 } // namespace Internal
412 #endif // DALI_INTERNAL_ANIMATOR_CONNECTOR_H