Dali 3D User Interface Engine
animator-connector.h
Go to the documentation of this file.
1 #ifndef __DALI_INTERNAL_ANIMATOR_CONNECTOR_H__
2 #define __DALI_INTERNAL_ANIMATOR_CONNECTOR_H__
3 
4 /*
5  * Copyright (c) 2015 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
30 
31 namespace Dali
32 {
33 
34 namespace Internal
35 {
36 
46 template < typename PropertyType >
48 {
49 public:
50 
53 
64  static AnimatorConnectorBase* New( Object& object,
65  Property::Index propertyIndex,
66  int componentIndex,
67  AnimatorFunctionBase* animatorFunction,
68  AlphaFunction alpha,
69  const TimePeriod& period )
70  {
71  return new AnimatorConnector< PropertyType >( object,
72  propertyIndex,
73  componentIndex,
74  animatorFunction,
75  alpha,
76  period );
77  }
78 
83  {
84  if( mObject )
85  {
86  mObject->RemoveObserver( *this );
87  }
88 
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
91  if( !mAnimator )
92  {
93  delete mAnimatorFunction;
95  }
96  }
97 
102  void SetParent( Animation& parent )
103  {
104  DALI_ASSERT_ALWAYS( mParent == NULL && "AnimationConnector already has a parent" );
105  mParent = &parent;
106 
107  if( mObject && mObject->GetSceneObject() )
108  {
109  CreateAnimator();
110  }
111  }
112 
113 private:
114 
119  Property::Index propertyIndex,
120  int componentIndex,
121  Internal::AnimatorFunctionBase* animatorFunction,
122  AlphaFunction alpha,
123  const TimePeriod& period )
124  : AnimatorConnectorBase( alpha, period ),
125  mObject( &object ),
126  mAnimator(0),
127  mPropertyIndex( propertyIndex ),
128  mComponentIndex( componentIndex ),
129  mAnimatorFunction( animatorFunction )
130  {
131  object.AddObserver( *this );
132  }
133 
134  // Undefined
136 
137  // Undefined
139 
143  virtual void SceneObjectAdded( Object& object )
144  {
145  //If the animator has not been created yet, create it now.
146  if( !mAnimator )
147  {
148  CreateAnimator();
149  }
150  }
151 
155  virtual void SceneObjectRemoved( Object& object )
156  {
157  }
158 
162  virtual void ObjectDestroyed( Object& object )
163  {
164  mObject = NULL;
165  }
166 
173  {
174  DALI_ASSERT_DEBUG( mAnimator == NULL );
176  DALI_ASSERT_DEBUG( mParent != NULL );
177 
178  //Get the PropertyOwner the animator is going to animate
179  const SceneGraph::PropertyOwner* propertyOwner = mObject->GetSceneObject();
180 
181  //Get SceneGraph::BaseProperty
183 
184  //Check if property is a component of another property
185  const int componentIndex = mObject->GetPropertyComponentIndex( mPropertyIndex );
186  if( componentIndex != Property::INVALID_COMPONENT_INDEX )
187  {
188  mComponentIndex = componentIndex;
189  }
190 
192  {
194 
195  //Cast to AnimatableProperty
196  const PropertyInterfaceType* animatableProperty = dynamic_cast< const PropertyInterfaceType* >( baseProperty );
197 
198  //Dynamic cast will fail if BaseProperty is not an AnimatableProperty
199  DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
200 
201  //Create the animator
202  mAnimator = AnimatorType::New( *propertyOwner, *animatableProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
203 
204  }
205  else
206  {
208  if ( PropertyTypes::Get< Vector2 >() == baseProperty->GetType() )
209  {
210  // Animate float component of Vector2 property
211 
212  // Cast to AnimatableProperty of type Vector2
213  const SceneGraph::AnimatableProperty<Vector2>* animatableProperty = dynamic_cast< const SceneGraph::AnimatableProperty<Vector2>* >( baseProperty );
214 
215  //Dynamic cast will fail if BaseProperty is not a Vector2 AnimatableProperty
216  DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
217 
218  switch( mComponentIndex )
219  {
220  case 0:
221  {
223  *animatableProperty,
226  mTimePeriod );
227  break;
228  }
229  case 1:
230  {
232  *animatableProperty,
235  mTimePeriod );
236  break;
237  }
238  default:
239  {
240  break;
241  }
242  }
243  }
244  else if ( PropertyTypes::Get< Vector3 >() == baseProperty->GetType() )
245  {
246  // Animate float component of Vector3 property
247 
248  // Cast to AnimatableProperty of type Vector3
249  const SceneGraph::AnimatableProperty<Vector3>* animatableProperty = dynamic_cast< const SceneGraph::AnimatableProperty<Vector3>* >( baseProperty );
250 
251  //Dynamic cast will fail if BaseProperty is not a Vector3 AnimatableProperty
252  DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
253 
254  switch( mComponentIndex )
255  {
256  case 0:
257  {
259  *animatableProperty,
262  mTimePeriod );
263  break;
264  }
265  case 1:
266  {
268  *animatableProperty,
271  mTimePeriod );
272  break;
273  }
274  case 2:
275  {
277  *animatableProperty,
280  mTimePeriod );
281  break;
282  }
283  default:
284  {
285  break;
286  }
287  }
288  }
289  else if ( PropertyTypes::Get< Vector4 >() == baseProperty->GetType() )
290  {
291  // Animate float component of Vector4 property
292 
293  // Cast to AnimatableProperty of type Vector4
294  const SceneGraph::AnimatableProperty<Vector4>* animatableProperty = dynamic_cast< const SceneGraph::AnimatableProperty<Vector4>* >( baseProperty );
295 
296  //Dynamic cast will fail if BaseProperty is not a Vector4 AnimatableProperty
297  DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
298 
299  switch( mComponentIndex )
300  {
301  case 0:
302  {
304  *animatableProperty,
307  mTimePeriod );
308  break;
309  }
310  case 1:
311  {
313  *animatableProperty,
316  mTimePeriod );
317  break;
318  }
319  case 2:
320  {
322  *animatableProperty,
325  mTimePeriod );
326  break;
327  }
328  case 3:
329  {
331  *animatableProperty,
334  mTimePeriod );
335  break;
336  }
337 
338  default:
339  {
340  break;
341  }
342  }
343  }
344  }
345 
346  DALI_ASSERT_DEBUG( mAnimator != NULL );
347 
348  // Add the new SceneGraph::Animator to its correspondent SceneGraph::Animation via message
349  const SceneGraph::Animation* animation = mParent->GetSceneObject();
350  DALI_ASSERT_DEBUG( NULL != animation );
352  }
353 
354 protected:
355 
358 
361 
363 };
364 
365 
366 } // namespace Internal
367 
368 } // namespace Dali
369 
370 #endif // __DALI_INTERNAL_ANIMATOR_CONNECTOR_H__
Dali Docs Home
Read more about Dali