8119063402813103658cc775c4ef529767f39c17
[platform/core/uifw/dali-core.git] / dali / internal / event / animation / animator-connector.h
1 #ifndef __DALI_INTERNAL_ANIMATOR_CONNECTOR_H__
2 #define __DALI_INTERNAL_ANIMATOR_CONNECTOR_H__
3
4 /*
5  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // INTERNAL INCLUDES
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>
29
30 namespace Dali
31 {
32
33 namespace Internal
34 {
35
36 /**
37  * AnimatorConnector is used to connect SceneGraph::Animators and
38  * PropertyResetters for newly created scene-graph objects.
39  *
40  * SceneGraph::Animators weakly reference scene objects, and are automatically deleted when orphaned.
41  * Therefore the AnimatorConnector is NOT responsible for disconnecting animators.
42  */
43 template < typename PropertyType >
44 class AnimatorConnector : public AnimatorConnectorBase
45 {
46 public:
47
48   typedef SceneGraph::Animator< PropertyType, PropertyAccessor<PropertyType> > AnimatorType;
49   typedef SceneGraph::AnimatableProperty< PropertyType > PropertyInterfaceType;
50
51   /**
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.
60    */
61   static AnimatorConnectorBase* New( Object& object,
62                                      Property::Index propertyIndex,
63                                      int componentIndex,
64                                      AnimatorFunctionBase* animatorFunction,
65                                      AlphaFunction alpha,
66                                      const TimePeriod& period )
67   {
68     return new AnimatorConnector< PropertyType >( object,
69                                                   propertyIndex,
70                                                   componentIndex,
71                                                   animatorFunction,
72                                                   alpha,
73                                                   period );
74   }
75
76   /**
77    * Virtual destructor.
78    */
79   virtual ~AnimatorConnector()
80   {
81     if( mObject )
82     {
83       mObject->RemoveObserver( *this );
84     }
85
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
88     if( !mAnimator )
89     {
90       delete mAnimatorFunction;
91       mAnimatorFunction = 0;
92     }
93   }
94
95   /**
96    * From AnimatorConnectorBase.
97    * This is only expected to be called once, when added to an Animation.
98    */
99   void SetParent( Animation& parent )
100   {
101     DALI_ASSERT_ALWAYS( mParent == NULL && "AnimationConnector already has a parent" );
102     mParent = &parent;
103
104     if( mObject && mObject->GetSceneObject() )
105     {
106       CreateAnimator();
107     }
108   }
109
110 private:
111
112   /**
113    * Private constructor; see also AnimatorConnector::New().
114    */
115   AnimatorConnector( Object& object,
116                      Property::Index propertyIndex,
117                      int componentIndex,
118                      Internal::AnimatorFunctionBase* animatorFunction,
119                      AlphaFunction alpha,
120                      const TimePeriod& period )
121   : AnimatorConnectorBase( object, propertyIndex, componentIndex, alpha, period ),
122     mAnimator(0),
123     mAnimatorFunction( animatorFunction )
124   {
125   }
126
127   // Undefined
128   AnimatorConnector( const AnimatorConnector& );
129
130   // Undefined
131   AnimatorConnector& operator=( const AnimatorConnector& rhs );
132
133   /**
134    * From Object::Observer
135    */
136   virtual void SceneObjectAdded( Object& object )
137   {
138     //If the animator has not been created yet, create it now.
139     if( !mAnimator && mObject )
140     {
141       CreateAnimator();
142     }
143   }
144
145    /**
146    * Helper function to create a Scenegraph::Animator and PropertyResetter and add it to its correspondent
147    * SceneGraph::Animation.
148    *
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
151    */
152   void CreateAnimator()
153   {
154     DALI_ASSERT_DEBUG( mAnimator == NULL );
155     DALI_ASSERT_DEBUG( mAnimatorFunction != NULL );
156     DALI_ASSERT_DEBUG( mParent != NULL );
157
158     //Get the PropertyOwner the animator is going to animate
159     const SceneGraph::PropertyOwner* propertyOwner = mObject->GetSceneObject();
160
161     // Get SceneGraph::BaseProperty
162     const SceneGraph::PropertyBase* baseProperty = mObject->GetSceneObjectAnimatableProperty( mPropertyIndex );
163
164     OwnerPointer<SceneGraph::PropertyResetterBase> resetter;
165
166     // Check if property is a component of another property
167     const int componentIndex = mObject->GetPropertyComponentIndex( mPropertyIndex );
168     if( componentIndex != Property::INVALID_COMPONENT_INDEX )
169     {
170       mComponentIndex = componentIndex;
171     }
172
173     if( mComponentIndex == Property::INVALID_COMPONENT_INDEX )
174     {
175       // Animating the whole property
176
177       // Cast to AnimatableProperty
178       const PropertyInterfaceType* animatableProperty = dynamic_cast< const PropertyInterfaceType* >( baseProperty );
179
180       if( animatableProperty == NULL )
181       {
182         if( baseProperty->IsTransformManagerProperty() )
183         {
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
186         }
187         else
188         {
189           DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
190         }
191
192       }
193       else
194       {
195         // Create the animator and resetter
196         mAnimator = AnimatorType::New( *propertyOwner, *animatableProperty, mAnimatorFunction,
197                                        mAlphaFunction, mTimePeriod );
198         resetter = SceneGraph::AnimatorResetter::New( *propertyOwner, *baseProperty, *mAnimator );
199       }
200     }
201     else
202     {
203       {
204         // Animating a component of the property
205         if ( PropertyTypes::Get< Vector2 >() == baseProperty->GetType() )
206         {
207           // Animate float component of Vector2 property
208
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" );
212
213           switch( mComponentIndex )
214           {
215             case 0:
216             {
217               mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorX<Vector2> >::New( *propertyOwner,
218                                                                                                    *animatableProperty,
219                                                                                                    mAnimatorFunction,
220                                                                                                    mAlphaFunction,
221                                                                                                    mTimePeriod );
222               break;
223             }
224             case 1:
225             {
226               mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorY<Vector2> >::New( *propertyOwner,
227                                                                                                    *animatableProperty,
228                                                                                                    mAnimatorFunction,
229                                                                                                    mAlphaFunction,
230                                                                                                    mTimePeriod );
231               break;
232             }
233             default:
234             {
235               break;
236             }
237           }
238
239           if( mAnimator != nullptr )
240           {
241             resetter = SceneGraph::AnimatorResetter::New( *propertyOwner, *baseProperty, *mAnimator );
242           }
243         }
244
245         else if ( PropertyTypes::Get< Vector3 >() == baseProperty->GetType() )
246         {
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 );
250
251           if( animatableProperty == NULL )
252           {
253             if( baseProperty->IsTransformManagerProperty() )
254             {
255               if( mComponentIndex == 0 )
256               {
257                 mAnimator = SceneGraph::AnimatorTransformProperty< float,TransformManagerPropertyComponentAccessor<Vector3,0> >::New( *propertyOwner, *baseProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
258               }
259               else if( mComponentIndex == 1 )
260               {
261                 mAnimator = SceneGraph::AnimatorTransformProperty< float,TransformManagerPropertyComponentAccessor<Vector3,1> >::New( *propertyOwner, *baseProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
262               }
263               else if( mComponentIndex == 2 )
264               {
265                 mAnimator = SceneGraph::AnimatorTransformProperty< float,TransformManagerPropertyComponentAccessor<Vector3,2> >::New( *propertyOwner, *baseProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
266               }
267             }
268             else
269             {
270               DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
271             }
272             // Don't manually reset transform property - TransformManager will do it more efficiently
273           }
274           else
275           {
276             // Dynamic cast will fail if BaseProperty is not a Vector3 AnimatableProperty
277             DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
278
279             switch( mComponentIndex )
280             {
281               case 0:
282               {
283                 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorX<Vector3> >::New( *propertyOwner,
284                                                                                                      *animatableProperty,
285                                                                                                      mAnimatorFunction,
286                                                                                                      mAlphaFunction,
287                                                                                                      mTimePeriod );
288                 break;
289               }
290               case 1:
291               {
292                 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorY<Vector3> >::New( *propertyOwner,
293                                                                                                      *animatableProperty,
294                                                                                                      mAnimatorFunction,
295                                                                                                      mAlphaFunction,
296                                                                                                      mTimePeriod );
297                 break;
298               }
299               case 2:
300               {
301                 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorZ<Vector3> >::New( *propertyOwner,
302                                                                                                      *animatableProperty,
303                                                                                                      mAnimatorFunction,
304                                                                                                      mAlphaFunction,
305                                                                                                      mTimePeriod );
306                 break;
307               }
308               default:
309               {
310                 break;
311               }
312             }
313
314             if( mAnimator != nullptr )
315             {
316               resetter = SceneGraph::AnimatorResetter::New( *propertyOwner, *baseProperty, *mAnimator );
317             }
318           }
319         }
320         else if ( PropertyTypes::Get< Vector4 >() == baseProperty->GetType() )
321         {
322           // Animate float component of Vector4 property
323
324           // Cast to AnimatableProperty of type Vector4
325           const SceneGraph::AnimatableProperty<Vector4>* animatableProperty = dynamic_cast< const SceneGraph::AnimatableProperty<Vector4>* >( baseProperty );
326
327           //Dynamic cast will fail if BaseProperty is not a Vector4 AnimatableProperty
328           DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
329
330           switch( mComponentIndex )
331           {
332             case 0:
333             {
334               mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorX<Vector4> >::New( *propertyOwner,
335                                                                                                    *animatableProperty,
336                                                                                                    mAnimatorFunction,
337                                                                                                    mAlphaFunction,
338                                                                                                    mTimePeriod );
339               break;
340             }
341             case 1:
342             {
343               mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorY<Vector4> >::New( *propertyOwner,
344                                                                                                    *animatableProperty,
345                                                                                                    mAnimatorFunction,
346                                                                                                    mAlphaFunction,
347                                                                                                    mTimePeriod );
348               break;
349             }
350             case 2:
351             {
352               mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorZ<Vector4> >::New( *propertyOwner,
353                                                                                                    *animatableProperty,
354                                                                                                    mAnimatorFunction,
355                                                                                                    mAlphaFunction,
356                                                                                                    mTimePeriod );
357               break;
358             }
359             case 3:
360             {
361               mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorW<Vector4> >::New( *propertyOwner,
362                                                                                                    *animatableProperty,
363                                                                                                    mAnimatorFunction,
364                                                                                                    mAlphaFunction,
365                                                                                                    mTimePeriod );
366               break;
367             }
368
369             default:
370             {
371               break;
372             }
373           }
374           if( mAnimator != nullptr )
375           {
376             resetter = SceneGraph::AnimatorResetter::New( *propertyOwner, *baseProperty, *mAnimator );
377           }
378         }
379       }
380     }
381
382     DALI_ASSERT_DEBUG( mAnimator != NULL );
383
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 );
388
389     // Add the new SceneGraph::PropertyResetter to the update manager via message
390     if( resetter != nullptr )
391     {
392       AddResetterMessage( mParent->GetEventThreadServices().GetUpdateManager(), resetter );
393     }
394   }
395
396 protected:
397
398   SceneGraph::AnimatorBase* mAnimator;
399
400   Internal::AnimatorFunctionBase* mAnimatorFunction;  ///< Owned by the animator connector until an Scenegraph::Animator is created
401 };
402
403
404 } // namespace Internal
405
406 } // namespace Dali
407
408 #endif // __DALI_INTERNAL_ANIMATOR_CONNECTOR_H__