fix Klocwork warning of unintialized data members, unneeded assignment & unreachable...
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / builder / builder-animations.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // EXTERNAL INCLUDES
19 #include <dali/integration-api/debug.h>
20
21 // INTERNAL INCLUDES
22 #include <dali-toolkit/internal/builder/builder-impl.h>
23 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
24 #include <dali-toolkit/internal/builder/replacement.h>
25
26 namespace // unnamed namespace
27 {
28
29 using namespace Dali;
30
31 TimePeriod GetTimePeriod( const TreeNode& child, const Toolkit::Internal::Replacement& constant )
32 {
33   OptionalFloat delay      = constant.IsFloat( IsChild(child, "delay" ) );
34   OptionalFloat duration   = constant.IsFloat( IsChild(child, "duration" ) );
35   DALI_ASSERT_ALWAYS( duration && "Time period must have at least a duration" );
36
37   if( delay )
38   {
39     return TimePeriod( *delay, *duration );
40   }
41   else
42   {
43     return TimePeriod( *duration );
44   }
45 }
46
47 Property::Value GetPropertyValue( const Property::Type& propType, const TreeNode& child )
48 {
49   switch ( propType )
50   {
51     case Property::BOOLEAN:
52     {
53       return Property::Value( GetBoolean( child ) );
54     }
55
56     case Property::FLOAT:
57     {
58       return Property::Value( GetFloat( child ) );
59     }
60
61     case Property::VECTOR2:
62     {
63       return Property::Value( GetVector2( child ) );
64     }
65
66     case Property::VECTOR3:
67     {
68       return Property::Value( GetVector3( child ) );
69     }
70
71     case Property::VECTOR4:
72     {
73       return Property::Value( GetVector4( child ) );
74     }
75
76     case Property::ROTATION:
77     {
78       if( 4 == child.Size() )
79       {
80         Vector4 v(GetVector4(child));
81         // angle, axis as per spec
82         return Property::Value( Quaternion(Radian(Degree(v[3])),
83                                            Vector3(v[0],v[1],v[2])) );
84       }
85       else
86       {
87         // degrees as per spec
88         Vector3 rotation = GetVector3( child );
89         return Property::Value( Quaternion(Radian(Degree(rotation.x)),
90                                            Radian(Degree(rotation.y)),
91                                            Radian(Degree(rotation.z))) );
92       }
93     }
94
95     case Property::NONE: // fall
96     default:
97     {
98       DALI_ASSERT_ALWAYS( !"Property type incorrect" );
99     }
100   }
101 }
102
103 AlphaFunction GetAlphaFunction( const std::string& alphaFunction )
104 {
105   typedef std::map< const std::string, Dali::AlphaFunction > AlphaFunctionLut;
106   static AlphaFunctionLut alphaFunctionLut;
107
108   if( 0 == alphaFunctionLut.size() )
109   {
110     // coding convention is uppercase enums
111     alphaFunctionLut["DEFAULT"]                    = AlphaFunctions::Default;
112     alphaFunctionLut["LINEAR"]                     = AlphaFunctions::Linear;
113     alphaFunctionLut["SQUARE"]                     = AlphaFunctions::Square;
114     alphaFunctionLut["REVERSE"]                    = AlphaFunctions::Reverse;
115     alphaFunctionLut["EASE_IN"]                    = AlphaFunctions::EaseIn;
116     alphaFunctionLut["EASE_OUT"]                   = AlphaFunctions::EaseOut;
117     alphaFunctionLut["EASE_IN_OUT"]                = AlphaFunctions::EaseInOut;
118     alphaFunctionLut["EASE_IN_SINE"]               = AlphaFunctions::EaseInSine;
119     alphaFunctionLut["EASE_OUT_SINE"]              = AlphaFunctions::EaseOutSine;
120     alphaFunctionLut["EASE_IN_OUT_SINE"]           = AlphaFunctions::EaseInOutSine;
121     alphaFunctionLut["EASE_IN_SINE_33"]            = AlphaFunctions::EaseInSine33;
122     alphaFunctionLut["EASE_OUT_SINE_33"]           = AlphaFunctions::EaseOutSine33;
123     alphaFunctionLut["EASE_IN_OUT_SINE_33"]        = AlphaFunctions::EaseInOutSine33;
124     alphaFunctionLut["EASE_IN_OUT_SINE_50"]        = AlphaFunctions::EaseInOutSine50;
125     alphaFunctionLut["EASE_IN_OUT_SINE_60"]        = AlphaFunctions::EaseInOutSine60;
126     alphaFunctionLut["EASE_IN_OUT_SINE_70"]        = AlphaFunctions::EaseInOutSine70;
127     alphaFunctionLut["EASE_IN_OUT_SINE_80"]        = AlphaFunctions::EaseInOutSine80;
128     alphaFunctionLut["EASE_IN_OUT_SINE_90"]        = AlphaFunctions::EaseInOutSine90;
129     alphaFunctionLut["DOUBLE_EASE_IN_OUT_SINE_60"] = AlphaFunctions::DoubleEaseInOutSine60;
130     alphaFunctionLut["EASE_OUT_QUINT_50"]          = AlphaFunctions::EaseOutQuint50;
131     alphaFunctionLut["EASE_OUT_QUINT_80"]          = AlphaFunctions::EaseOutQuint80;
132     alphaFunctionLut["BOUNCE"]                     = AlphaFunctions::Bounce;
133     alphaFunctionLut["BOUNCE_BACK"]                = AlphaFunctions::BounceBack;
134     alphaFunctionLut["EASE_IN_BACK"]               = AlphaFunctions::EaseInBack;
135     alphaFunctionLut["EASE_OUT_BACK"]              = AlphaFunctions::EaseOutBack;
136     alphaFunctionLut["EASE_IN_OUT_BACK"]           = AlphaFunctions::EaseInOutBack;
137     alphaFunctionLut["SIN"]                        = AlphaFunctions::Sin;
138     alphaFunctionLut["SIN2X"]                      = AlphaFunctions::Sin2x;
139   }
140
141   const AlphaFunctionLut::const_iterator iter( alphaFunctionLut.find( alphaFunction ) );
142
143   if( iter != alphaFunctionLut.end() )
144   {
145     return iter->second;
146   }
147   else
148   {
149     DALI_ASSERT_ALWAYS( iter != alphaFunctionLut.end() && "Unknown Anchor Constant" );
150     return Dali::AlphaFunctions::Default;
151   }
152 }
153
154 } // unnamed namespace
155
156
157 namespace Dali
158 {
159
160 namespace Toolkit
161 {
162
163 namespace Internal
164 {
165
166 Animation CreateAnimation( const TreeNode& child, const Replacement& constant, Dali::Actor searchRoot )
167 {
168   float durationSum = 0.f;
169
170   Dali::Actor searchActor = searchRoot ? searchRoot : Dali::Stage::GetCurrent().GetRootLayer();
171
172   Animation animation( Animation::New( 0.f ) );
173
174   // duration needs to be set before AnimateTo calls for correct operation when AnimateTo has no "time-period".
175   OptionalFloat duration = constant.IsFloat( IsChild(child, "duration" ) );
176
177   if( duration )
178   {
179     animation.SetDuration( *duration );
180   }
181
182   if( OptionalBoolean looping = constant.IsBoolean(  IsChild(child, "loop" ) ) )
183   {
184     animation.SetLooping( *looping );
185   }
186
187   if( OptionalString endAction = constant.IsString(  IsChild(child, "end-action" ) ) )
188   {
189     if("BAKE" == *endAction)
190     {
191       animation.SetEndAction( Animation::Bake );
192     }
193     else if("DISCARD" == *endAction)
194     {
195       animation.SetEndAction( Animation::Discard );
196     }
197     else if("BAKE_FINAL" == *endAction)
198     {
199       animation.SetEndAction( Animation::BakeFinal );
200     }
201   }
202
203   if( OptionalString endAction = constant.IsString(  IsChild(child, "disconnect-action" ) ) )
204   {
205     if("BAKE" == *endAction)
206     {
207       animation.SetDisconnectAction( Animation::Bake );
208     }
209     else if("DISCARD" == *endAction)
210     {
211       animation.SetDisconnectAction( Animation::Discard );
212     }
213     else if("BAKE_FINAL" == *endAction)
214     {
215       animation.SetDisconnectAction( Animation::BakeFinal );
216     }
217   }
218
219   OptionalChild propertiesNode = IsChild(child, "properties" );
220   if(propertiesNode)
221   {
222     const TreeNode::ConstIterator endIter = (*propertiesNode).CEnd();
223     for( TreeNode::ConstIterator iter = (*propertiesNode).CBegin(); endIter != iter; ++iter )
224     {
225       const TreeNode::KeyNodePair& pKeyChild = *iter;
226
227       OptionalString actorName( constant.IsString( IsChild(pKeyChild.second, "actor" ) ) );
228       OptionalString property(  constant.IsString( IsChild(pKeyChild.second, "property" ) ) );
229       DALI_ASSERT_ALWAYS( actorName && "Animation must specify actor name" );
230       DALI_ASSERT_ALWAYS( property  && "Animation must specify a property name" );
231
232       Handle targetHandle = searchActor.FindChildByName( *actorName );
233       DALI_ASSERT_ALWAYS( targetHandle && "Actor must exist for property" );
234
235       Property::Index idx( targetHandle.GetPropertyIndex( *property ) );
236
237       // if the property is not found from the (actor) handle, try to downcast it to renderable actor
238       // to allow animating shader uniforms
239       if( idx == Property::INVALID_INDEX )
240       {
241         RenderableActor renderable = RenderableActor::DownCast( targetHandle );
242         if( renderable )
243         {
244           // A limitation here is that its possible that between creation of animation
245           // and running it the ShaderEffect of the actor has been changed.
246           // However this is a unlikely use case especially when using scripts.
247           if( ShaderEffect effect = renderable.GetShaderEffect() )
248           {
249             idx = effect.GetPropertyIndex( *property );
250             if(idx != Property::INVALID_INDEX)
251             {
252               targetHandle = effect;
253             }
254             else
255             {
256               DALI_SCRIPT_WARNING( "Cannot find property on object or ShaderEffect\n" );
257               continue;
258             }
259           }
260         }
261         else
262         {
263           DALI_SCRIPT_WARNING( "Cannot find property on object or ShaderEffect\n" );
264           continue;
265         }
266       }
267
268       if( idx == Property::INVALID_INDEX)
269       {
270         DALI_ASSERT_ALWAYS( idx != Property::INVALID_INDEX && "Animation must specify a valid property" );
271         continue;
272       }
273
274       Property prop( Property( targetHandle, idx ) );
275       Property::Value propValue;
276
277       // these are the defaults
278       AlphaFunction alphaFunction( AlphaFunctions::Default );
279       TimePeriod timePeriod( 0.f );
280
281       OptionalChild timeChild = IsChild( pKeyChild.second, "time-period" );
282
283       if( timeChild )
284       {
285         timePeriod = GetTimePeriod( *timeChild, constant );
286       }
287
288       durationSum = std::max( durationSum, timePeriod.delaySeconds + timePeriod.durationSeconds );
289
290       if( OptionalString alphaChild = constant.IsString( IsChild(pKeyChild.second, "alpha-function" ) ) )
291       {
292         alphaFunction = GetAlphaFunction( *alphaChild );
293       }
294
295       if( OptionalChild keyFrameChild = IsChild(pKeyChild.second, "key-frames") )
296       {
297         KeyFrames keyframes = KeyFrames::New();
298
299         const TreeNode::ConstIterator endIter = (*keyFrameChild).CEnd();
300         for( TreeNode::ConstIterator iter = (*keyFrameChild).CBegin(); endIter != iter; ++iter )
301         {
302           const TreeNode::KeyNodePair& kfKeyChild = *iter;
303
304           OptionalFloat kfProgress = constant.IsFloat( IsChild(kfKeyChild.second, "progress" ) );
305           DALI_ASSERT_ALWAYS( kfProgress && "Key frame entry must have 'progress'" );
306
307           OptionalChild kfValue = IsChild( kfKeyChild.second, "value" );
308           DALI_ASSERT_ALWAYS( kfValue && "Key frame entry must have 'value'" );
309
310           try
311           {
312             propValue = GetPropertyValue( prop.object.GetPropertyType(prop.propertyIndex), *kfValue );
313           }
314           catch(...)
315           {
316             DALI_LOG_WARNING( "Property:'%s' type does not match value type '%s'\n",
317                               (*property).c_str(),
318                               PropertyTypes::GetName(prop.object.GetPropertyType(prop.propertyIndex)) );
319
320             throw;
321           }
322
323           AlphaFunction kfAlphaFunction( AlphaFunctions::Default );
324           if( OptionalString alphaFuncStr = constant.IsString( IsChild(pKeyChild.second, "alpha-function") ) )
325           {
326             kfAlphaFunction = GetAlphaFunction( *alphaFuncStr );
327           }
328
329           keyframes.Add( *kfProgress, propValue, kfAlphaFunction );
330         }
331
332         if( timeChild )
333         {
334           animation.AnimateBetween( prop, keyframes, alphaFunction, timePeriod );
335         }
336         else
337         {
338           animation.AnimateBetween( prop, keyframes, alphaFunction );
339         }
340       }
341       else
342       {
343         try
344         {
345           propValue = GetPropertyValue( prop.object.GetPropertyType(prop.propertyIndex), *IsChild(pKeyChild.second, "value") );
346         }
347         catch(...)
348         {
349           DALI_LOG_WARNING( "Property:'%s' type does not match value type '%s'\n", (*property).c_str(),
350                             PropertyTypes::GetName( prop.object.GetPropertyType(prop.propertyIndex) ) );
351
352           throw;
353         }
354
355         if( OptionalBoolean relative = constant.IsBoolean( IsChild(pKeyChild.second, "relative") ) )
356         {
357           if( timeChild )
358           {
359             animation.AnimateBy( prop, propValue, alphaFunction, timePeriod );
360           }
361           else
362           {
363             animation.AnimateBy( prop, propValue, alphaFunction );
364           }
365         }
366         else
367         {
368           if( timeChild )
369           {
370             animation.AnimateTo( prop, propValue, alphaFunction, timePeriod );
371           }
372           else
373           {
374             animation.AnimateTo( prop, propValue, alphaFunction );
375           }
376         }
377       }
378     }
379   }
380
381   if( !duration )
382   {
383     animation.SetDuration( durationSum );
384   }
385
386   return animation;
387 }
388
389 Animation CreateAnimation( const TreeNode& child )
390 {
391   Replacement replacement;
392   return CreateAnimation( child, replacement, Stage::GetCurrent().GetRootLayer() );
393 }
394
395 } // namespace Internal
396
397 } // namespace Toolkit
398
399 } // namespace Dali
400