Merge "Implemented the Handle assignment operators properly" into tizen
[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       return Property::Value();
100     }
101   }
102 }
103
104 AlphaFunction GetAlphaFunction( const std::string& alphaFunction )
105 {
106   typedef std::map< const std::string, Dali::AlphaFunction > AlphaFunctionLut;
107   static AlphaFunctionLut alphaFunctionLut;
108
109   if( 0 == alphaFunctionLut.size() )
110   {
111     // coding convention is uppercase enums
112     alphaFunctionLut["DEFAULT"]         = Dali::AlphaFunctions::Default;
113     alphaFunctionLut["LINEAR"]          = Dali::AlphaFunctions::Linear;
114     alphaFunctionLut["REVERSE"]         = Dali::AlphaFunctions::Reverse;
115     alphaFunctionLut["EASE_IN"]         = Dali::AlphaFunctions::EaseIn;
116     alphaFunctionLut["EASE_OUT"]        = Dali::AlphaFunctions::EaseOut;
117     alphaFunctionLut["EASE_IN_OUT"]     = Dali::AlphaFunctions::EaseInOut;
118     alphaFunctionLut["EASE_IN_SINE"]    = Dali::AlphaFunctions::EaseInSine;
119     alphaFunctionLut["EASE_OUT_SINE"]   = Dali::AlphaFunctions::EaseOutSine;
120     alphaFunctionLut["EASE_IN_OUT_SINE"]= Dali::AlphaFunctions::EaseInOutSine;
121     alphaFunctionLut["BOUNCE"]          = Dali::AlphaFunctions::Bounce;
122     alphaFunctionLut["BOUNCE_BACK"]     = Dali::AlphaFunctions::BounceBack;
123     alphaFunctionLut["EASE_OUT_BACK"]   = Dali::AlphaFunctions::EaseOutBack;
124     alphaFunctionLut["SIN"]             = Dali::AlphaFunctions::Sin;
125     alphaFunctionLut["SIN2X"]           = Dali::AlphaFunctions::Sin;
126   }
127
128   const AlphaFunctionLut::const_iterator iter( alphaFunctionLut.find( alphaFunction ) );
129
130   if( iter != alphaFunctionLut.end() )
131   {
132     return iter->second;
133   }
134   else
135   {
136     DALI_ASSERT_ALWAYS( iter != alphaFunctionLut.end() && "Unknown Anchor Constant" );
137     return Dali::AlphaFunctions::Default;
138   }
139 }
140
141 } // unnamed namespace
142
143
144 namespace Dali
145 {
146
147 namespace Toolkit
148 {
149
150 namespace Internal
151 {
152
153 Animation CreateAnimation( const TreeNode& child, const Replacement& constant, Dali::Actor searchRoot )
154 {
155   float durationSum = 0.f;
156
157   Dali::Actor searchActor = searchRoot ? searchRoot : Dali::Stage::GetCurrent().GetRootLayer();
158
159   Animation animation( Animation::New( 0.f ) );
160
161   // duration needs to be set before AnimateTo calls for correct operation when AnimateTo has no "time-period".
162   OptionalFloat duration = constant.IsFloat( IsChild(child, "duration" ) );
163
164   if( duration )
165   {
166     animation.SetDuration( *duration );
167   }
168
169   if( OptionalBoolean looping = constant.IsBoolean(  IsChild(child, "loop" ) ) )
170   {
171     animation.SetLooping( *looping );
172   }
173
174   if( OptionalString endAction = constant.IsString(  IsChild(child, "end-action" ) ) )
175   {
176     if("BAKE" == *endAction)
177     {
178       animation.SetEndAction( Animation::Bake );
179     }
180     else if("DISCARD" == *endAction)
181     {
182       animation.SetEndAction( Animation::Discard );
183     }
184     else if("BAKE_FINAL" == *endAction)
185     {
186       animation.SetEndAction( Animation::BakeFinal );
187     }
188   }
189
190   if( OptionalString endAction = constant.IsString(  IsChild(child, "destroy-action" ) ) )
191   {
192     if("BAKE" == *endAction)
193     {
194       animation.SetDestroyAction( Animation::Bake );
195     }
196     else if("DISCARD" == *endAction)
197     {
198       animation.SetDestroyAction( Animation::Discard );
199     }
200     else if("BAKE_FINAL" == *endAction)
201     {
202       animation.SetDestroyAction( Animation::BakeFinal );
203     }
204   }
205
206   OptionalChild propertiesNode = IsChild(child, "properties" );
207   if(propertiesNode)
208   {
209     const TreeNode::ConstIterator endIter = (*propertiesNode).CEnd();
210     for( TreeNode::ConstIterator iter = (*propertiesNode).CBegin(); endIter != iter; ++iter )
211     {
212       const TreeNode::KeyNodePair& pKeyChild = *iter;
213
214       OptionalString actorName( constant.IsString( IsChild(pKeyChild.second, "actor" ) ) );
215       OptionalString property(  constant.IsString( IsChild(pKeyChild.second, "property" ) ) );
216       DALI_ASSERT_ALWAYS( actorName && "Animation must specify actor name" );
217       DALI_ASSERT_ALWAYS( property  && "Animation must specify a property name" );
218
219       Handle targetHandle = searchActor.FindChildByName( *actorName );
220       DALI_ASSERT_ALWAYS( targetHandle && "Actor must exist for property" );
221
222       Property::Index idx( targetHandle.GetPropertyIndex( *property ) );
223
224       // if the property is not found from the (actor) handle, try to downcast it to renderable actor
225       // to allow animating shader uniforms
226       if( idx == Property::INVALID_INDEX )
227       {
228         RenderableActor renderable = RenderableActor::DownCast( targetHandle );
229         if( renderable )
230         {
231           // A limitation here is that its possible that between creation of animation
232           // and running it the ShaderEffect of the actor has been changed.
233           // However this is a unlikely use case especially when using scripts.
234           if( ShaderEffect effect = renderable.GetShaderEffect() )
235           {
236             idx = effect.GetPropertyIndex( *property );
237             if(idx != Property::INVALID_INDEX)
238             {
239               targetHandle = effect;
240             }
241             else
242             {
243               DALI_SCRIPT_WARNING( "Cannot find property on object or ShaderEffect\n" );
244               continue;
245             }
246           }
247         }
248         else
249         {
250           DALI_SCRIPT_WARNING( "Cannot find property on object or ShaderEffect\n" );
251           continue;
252         }
253       }
254
255       if( idx == Property::INVALID_INDEX)
256       {
257         DALI_ASSERT_ALWAYS( idx != Property::INVALID_INDEX && "Animation must specify a valid property" );
258         continue;
259       }
260
261       Property prop( Property( targetHandle, idx ) );
262       Property::Value propValue;
263
264       // these are the defaults
265       AlphaFunction alphaFunction( AlphaFunctions::Default );
266       TimePeriod timePeriod( 0.f );
267
268       OptionalChild timeChild = IsChild( pKeyChild.second, "time-period" );
269
270       if( timeChild )
271       {
272         timePeriod = GetTimePeriod( *timeChild, constant );
273       }
274
275       durationSum = std::max( durationSum, timePeriod.delaySeconds + timePeriod.durationSeconds );
276
277       if( OptionalString alphaChild = constant.IsString( IsChild(pKeyChild.second, "alpha-function" ) ) )
278       {
279         alphaFunction = GetAlphaFunction( *alphaChild );
280       }
281
282       if( OptionalChild keyFrameChild = IsChild(pKeyChild.second, "key-frames") )
283       {
284         KeyFrames keyframes = KeyFrames::New();
285
286         const TreeNode::ConstIterator endIter = (*keyFrameChild).CEnd();
287         for( TreeNode::ConstIterator iter = (*keyFrameChild).CBegin(); endIter != iter; ++iter )
288         {
289           const TreeNode::KeyNodePair& kfKeyChild = *iter;
290
291           OptionalFloat kfProgress = constant.IsFloat( IsChild(kfKeyChild.second, "progress" ) );
292           DALI_ASSERT_ALWAYS( kfProgress && "Key frame entry must have 'progress'" );
293
294           OptionalChild kfValue = IsChild( kfKeyChild.second, "value" );
295           DALI_ASSERT_ALWAYS( kfValue && "Key frame entry must have 'value'" );
296
297           try
298           {
299             propValue = GetPropertyValue( prop.object.GetPropertyType(prop.propertyIndex), *kfValue );
300           }
301           catch(...)
302           {
303             DALI_LOG_WARNING( "Property:'%s' type does not match value type '%s'\n",
304                               (*property).c_str(),
305                               PropertyTypes::GetName(prop.object.GetPropertyType(prop.propertyIndex)) );
306
307             throw;
308           }
309
310           AlphaFunction kfAlphaFunction( AlphaFunctions::Default );
311           if( OptionalString alphaFuncStr = constant.IsString( IsChild(pKeyChild.second, "alpha-function") ) )
312           {
313             kfAlphaFunction = GetAlphaFunction( *alphaFuncStr );
314           }
315
316           keyframes.Add( *kfProgress, propValue, kfAlphaFunction );
317         }
318
319         if( timeChild )
320         {
321           animation.AnimateBetween( prop, keyframes, alphaFunction, timePeriod );
322         }
323         else
324         {
325           animation.AnimateBetween( prop, keyframes, alphaFunction );
326         }
327       }
328       else
329       {
330         try
331         {
332           propValue = GetPropertyValue( prop.object.GetPropertyType(prop.propertyIndex), *IsChild(pKeyChild.second, "value") );
333         }
334         catch(...)
335         {
336           DALI_LOG_WARNING( "Property:'%s' type does not match value type '%s'\n", (*property).c_str(),
337                             PropertyTypes::GetName( prop.object.GetPropertyType(prop.propertyIndex) ) );
338
339           throw;
340         }
341
342         if( OptionalBoolean relative = constant.IsBoolean( IsChild(pKeyChild.second, "relative") ) )
343         {
344           if( timeChild )
345           {
346             animation.AnimateBy( prop, propValue, alphaFunction, timePeriod );
347           }
348           else
349           {
350             animation.AnimateBy( prop, propValue, alphaFunction );
351           }
352         }
353         else
354         {
355           if( timeChild )
356           {
357             animation.AnimateTo( prop, propValue, alphaFunction, timePeriod );
358           }
359           else
360           {
361             animation.AnimateTo( prop, propValue, alphaFunction );
362           }
363         }
364       }
365     }
366   }
367
368   if( !duration )
369   {
370     animation.SetDuration( durationSum );
371   }
372
373   return animation;
374 }
375
376 Animation CreateAnimation( const TreeNode& child )
377 {
378   Replacement replacement;
379   return CreateAnimation( child, replacement, Stage::GetCurrent().GetRootLayer() );
380 }
381
382 } // namespace Internal
383
384 } // namespace Toolkit
385
386 } // namespace Dali
387