Clean up the code to build successfully on macOS
[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) 2019 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
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 /**
35  * AnimatorConnector is used to connect SceneGraph::Animators.
36  *
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
40  */
41 template < typename PropertyType >
42 class AnimatorConnector : public AnimatorConnectorBase
43 {
44 public:
45
46   using AnimatorType = SceneGraph::Animator< PropertyType, PropertyAccessor<PropertyType> >;
47   using PropertyInterfaceType = SceneGraph::AnimatableProperty< PropertyType >;
48
49   /**
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.
58    */
59   static AnimatorConnectorBase* New( Object& object,
60                                      Property::Index propertyIndex,
61                                      int32_t componentIndex,
62                                      AnimatorFunctionBase* animatorFunction,
63                                      AlphaFunction alpha,
64                                      const TimePeriod& period )
65   {
66     return new AnimatorConnector( object,
67                                   propertyIndex,
68                                   componentIndex,
69                                   animatorFunction,
70                                   alpha,
71                                   period );
72   }
73
74   /**
75    * Virtual destructor.
76    */
77   ~AnimatorConnector() override
78   {
79   }
80
81 private:
82
83   /**
84    * Private constructor; see also AnimatorConnector::New().
85    */
86   AnimatorConnector( Object& object,
87                      Property::Index propertyIndex,
88                      int32_t componentIndex,
89                      Internal::AnimatorFunctionBase* animatorFunction,
90                      AlphaFunction alpha,
91                      const TimePeriod& period )
92   : AnimatorConnectorBase( object, propertyIndex, componentIndex, animatorFunction, alpha, period )
93   {
94   }
95
96   // Undefined
97   AnimatorConnector() = delete;
98   AnimatorConnector( const AnimatorConnector& ) = delete;
99   AnimatorConnector& operator=( const AnimatorConnector& rhs ) = delete;
100
101   /**
102    * @copydoc AnimatorConnectorBase::DoCreateAnimator()
103    */
104   bool DoCreateAnimator( const SceneGraph::PropertyOwner& propertyOwner, const SceneGraph::PropertyBase& baseProperty ) final
105   {
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
110
111     // Cast to AnimatableProperty
112     const PropertyInterfaceType* animatableProperty = dynamic_cast< const PropertyInterfaceType* >( &baseProperty );
113
114     if( animatableProperty == nullptr )
115     {
116       if( baseProperty.IsTransformManagerProperty() )
117       {
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
120       }
121       else
122       {
123         DALI_ASSERT_DEBUG( animatableProperty && "Animating non-animatable property" );
124       }
125     }
126     else
127     {
128       // Create the animator and resetter
129       mAnimator = AnimatorType::New( propertyOwner, *animatableProperty, mAnimatorFunction,
130                                      mAlphaFunction, mTimePeriod );
131       resetterRequired = true;
132     }
133     return resetterRequired;
134   }
135 };
136
137 /**
138  * Specialization for float as that type supports component properties
139  */
140 template <>
141 class AnimatorConnector< float > : public AnimatorConnectorBase
142 {
143 public:
144
145   using AnimatorType = SceneGraph::Animator< float, PropertyAccessor<float> >;
146   using PropertyInterfaceType = SceneGraph::AnimatableProperty< float >;
147
148   /**
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.
157    */
158   static AnimatorConnectorBase* New( Object& object,
159                                      Property::Index propertyIndex,
160                                      int32_t componentIndex,
161                                      AnimatorFunctionBase* animatorFunction,
162                                      AlphaFunction alpha,
163                                      const TimePeriod& period )
164   {
165     return new AnimatorConnector( object,
166                                   propertyIndex,
167                                   componentIndex,
168                                   animatorFunction,
169                                   alpha,
170                                   period );
171   }
172
173   /**
174    * Virtual destructor.
175    */
176   ~AnimatorConnector() override
177   {
178   }
179
180 private:
181
182   /**
183    * Private constructor; see also AnimatorConnector::New().
184    */
185   AnimatorConnector( Object& object,
186                      Property::Index propertyIndex,
187                      int32_t componentIndex,
188                      Internal::AnimatorFunctionBase* animatorFunction,
189                      AlphaFunction alpha,
190                      const TimePeriod& period )
191   : AnimatorConnectorBase( object, propertyIndex, componentIndex, animatorFunction, alpha, period )
192   {
193   }
194
195   // Undefined
196   AnimatorConnector() = delete;
197   AnimatorConnector( const AnimatorConnector& ) = delete;
198   AnimatorConnector& operator=( const AnimatorConnector& rhs ) = delete;
199
200   /**
201    * @copydoc AnimatorConnectorBase::DoCreateAnimator()
202    */
203   bool DoCreateAnimator( const SceneGraph::PropertyOwner& propertyOwner, const SceneGraph::PropertyBase& baseProperty ) final
204   {
205     bool resetterRequired = false;
206     if( mComponentIndex == Property::INVALID_COMPONENT_INDEX )
207     {
208       // Animating the whole property
209
210       // Cast to AnimatableProperty
211       const PropertyInterfaceType* animatableProperty = dynamic_cast< const PropertyInterfaceType* >( &baseProperty );
212
213       if( animatableProperty == nullptr )
214       {
215         if( baseProperty.IsTransformManagerProperty() )
216         {
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
219         }
220         else
221         {
222           DALI_ASSERT_DEBUG( animatableProperty && "Animating non-animatable property" );
223         }
224       }
225       else
226       {
227         // Create the animator and resetter
228         mAnimator = AnimatorType::New( propertyOwner, *animatableProperty, mAnimatorFunction,
229                                        mAlphaFunction, mTimePeriod );
230         resetterRequired = true;
231       }
232     }
233     else
234     {
235       {
236         // Animating a component of the property
237         if ( PropertyTypes::Get< Vector2 >() == baseProperty.GetType() )
238         {
239           // Animate float component of Vector2 property
240
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" );
244
245           switch( mComponentIndex )
246           {
247             case 0:
248             {
249               mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorX<Vector2> >::New( propertyOwner,
250                                                                                                    *animatableProperty,
251                                                                                                    mAnimatorFunction,
252                                                                                                    mAlphaFunction,
253                                                                                                    mTimePeriod );
254               break;
255             }
256             case 1:
257             {
258               mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorY<Vector2> >::New( propertyOwner,
259                                                                                                    *animatableProperty,
260                                                                                                    mAnimatorFunction,
261                                                                                                    mAlphaFunction,
262                                                                                                    mTimePeriod );
263               break;
264             }
265             default:
266             {
267               break;
268             }
269           }
270
271           resetterRequired = ( mAnimator != nullptr );
272         }
273
274         else if ( PropertyTypes::Get< Vector3 >() == baseProperty.GetType() )
275         {
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 );
279
280           if( animatableProperty == nullptr )
281           {
282             if( baseProperty.IsTransformManagerProperty() )
283             {
284               if( mComponentIndex == 0 )
285               {
286                 mAnimator = SceneGraph::AnimatorTransformProperty< float,TransformManagerPropertyComponentAccessor<Vector3,0> >::New( propertyOwner, baseProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
287               }
288               else if( mComponentIndex == 1 )
289               {
290                 mAnimator = SceneGraph::AnimatorTransformProperty< float,TransformManagerPropertyComponentAccessor<Vector3,1> >::New( propertyOwner, baseProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
291               }
292               else if( mComponentIndex == 2 )
293               {
294                 mAnimator = SceneGraph::AnimatorTransformProperty< float,TransformManagerPropertyComponentAccessor<Vector3,2> >::New( propertyOwner, baseProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
295               }
296             }
297             else
298             {
299               DALI_ASSERT_DEBUG( animatableProperty && "Animating non-animatable property" );
300             }
301             // Don't manually reset transform property - TransformManager will do it more efficiently
302           }
303           else
304           {
305             // Dynamic cast will fail if BaseProperty is not a Vector3 AnimatableProperty
306             DALI_ASSERT_DEBUG( animatableProperty && "Animating non-animatable property" );
307
308             switch( mComponentIndex )
309             {
310               case 0:
311               {
312                 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorX<Vector3> >::New( propertyOwner,
313                                                                                                      *animatableProperty,
314                                                                                                      mAnimatorFunction,
315                                                                                                      mAlphaFunction,
316                                                                                                      mTimePeriod );
317                 break;
318               }
319               case 1:
320               {
321                 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorY<Vector3> >::New( propertyOwner,
322                                                                                                      *animatableProperty,
323                                                                                                      mAnimatorFunction,
324                                                                                                      mAlphaFunction,
325                                                                                                      mTimePeriod );
326                 break;
327               }
328               case 2:
329               {
330                 mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorZ<Vector3> >::New( propertyOwner,
331                                                                                                      *animatableProperty,
332                                                                                                      mAnimatorFunction,
333                                                                                                      mAlphaFunction,
334                                                                                                      mTimePeriod );
335                 break;
336               }
337               default:
338               {
339                 break;
340               }
341             }
342
343             resetterRequired = ( mAnimator != nullptr );
344           }
345         }
346         else if ( PropertyTypes::Get< Vector4 >() == baseProperty.GetType() )
347         {
348           // Animate float component of Vector4 property
349
350           // Cast to AnimatableProperty of type Vector4
351           const SceneGraph::AnimatableProperty<Vector4>* animatableProperty = dynamic_cast< const SceneGraph::AnimatableProperty<Vector4>* >( &baseProperty );
352
353           //Dynamic cast will fail if BaseProperty is not a Vector4 AnimatableProperty
354           DALI_ASSERT_DEBUG( animatableProperty && "Animating non-animatable property" );
355
356           switch( mComponentIndex )
357           {
358             case 0:
359             {
360               mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorX<Vector4> >::New( propertyOwner,
361                                                                                                    *animatableProperty,
362                                                                                                    mAnimatorFunction,
363                                                                                                    mAlphaFunction,
364                                                                                                    mTimePeriod );
365               break;
366             }
367             case 1:
368             {
369               mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorY<Vector4> >::New( propertyOwner,
370                                                                                                    *animatableProperty,
371                                                                                                    mAnimatorFunction,
372                                                                                                    mAlphaFunction,
373                                                                                                    mTimePeriod );
374               break;
375             }
376             case 2:
377             {
378               mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorZ<Vector4> >::New( propertyOwner,
379                                                                                                    *animatableProperty,
380                                                                                                    mAnimatorFunction,
381                                                                                                    mAlphaFunction,
382                                                                                                    mTimePeriod );
383               break;
384             }
385             case 3:
386             {
387               mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorW<Vector4> >::New( propertyOwner,
388                                                                                                    *animatableProperty,
389                                                                                                    mAnimatorFunction,
390                                                                                                    mAlphaFunction,
391                                                                                                    mTimePeriod );
392               break;
393             }
394
395             default:
396             {
397               break;
398             }
399           }
400           resetterRequired = ( mAnimator != nullptr );
401         }
402       }
403     }
404     return resetterRequired;
405   }
406 };
407
408 } // namespace Internal
409
410 } // namespace Dali
411
412 #endif // DALI_INTERNAL_ANIMATOR_CONNECTOR_H