2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/public-api/actors/layer.h>
20 #include <dali/public-api/actors/renderable-actor.h>
21 #include <dali/integration-api/debug.h>
24 #include <dali-toolkit/internal/builder/builder-impl.h>
25 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
26 #include <dali-toolkit/internal/builder/replacement.h>
28 namespace // unnamed namespace
33 TimePeriod GetTimePeriod( const TreeNode& child, const Toolkit::Internal::Replacement& constant )
35 OptionalFloat delay = constant.IsFloat( IsChild(child, "delay" ) );
36 OptionalFloat duration = constant.IsFloat( IsChild(child, "duration" ) );
37 DALI_ASSERT_ALWAYS( duration && "Time period must have at least a duration" );
41 return TimePeriod( *delay, *duration );
45 return TimePeriod( *duration );
49 Property::Value GetPropertyValue( const Property::Type& propType, const TreeNode& child )
53 case Property::BOOLEAN:
55 return Property::Value( GetBoolean( child ) );
60 return Property::Value( GetFloat( child ) );
63 case Property::VECTOR2:
65 return Property::Value( GetVector2( child ) );
68 case Property::VECTOR3:
70 return Property::Value( GetVector3( child ) );
73 case Property::VECTOR4:
75 return Property::Value( GetVector4( child ) );
78 case Property::ROTATION:
80 if( 4 == child.Size() )
82 Vector4 v(GetVector4(child));
83 // angle, axis as per spec
84 return Property::Value( Quaternion(Radian(Degree(v[3])),
85 Vector3(v[0],v[1],v[2])) );
89 // degrees as per spec
90 Vector3 rotation = GetVector3( child );
91 return Property::Value( Quaternion(Radian(Degree(rotation.x)),
92 Radian(Degree(rotation.y)),
93 Radian(Degree(rotation.z))) );
97 case Property::NONE: // fall
100 DALI_ASSERT_ALWAYS( !"Property type incorrect" );
105 AlphaFunction GetAlphaFunction( const std::string& alphaFunction )
107 typedef std::map< const std::string, Dali::AlphaFunction > AlphaFunctionLut;
108 static AlphaFunctionLut alphaFunctionLut;
110 if( 0 == alphaFunctionLut.size() )
112 // coding convention is uppercase enums
113 alphaFunctionLut["DEFAULT"] = AlphaFunctions::Default;
114 alphaFunctionLut["LINEAR"] = AlphaFunctions::Linear;
115 alphaFunctionLut["SQUARE"] = AlphaFunctions::Square;
116 alphaFunctionLut["REVERSE"] = AlphaFunctions::Reverse;
117 alphaFunctionLut["EASE_IN"] = AlphaFunctions::EaseIn;
118 alphaFunctionLut["EASE_OUT"] = AlphaFunctions::EaseOut;
119 alphaFunctionLut["EASE_IN_OUT"] = AlphaFunctions::EaseInOut;
120 alphaFunctionLut["EASE_IN_SINE"] = AlphaFunctions::EaseInSine;
121 alphaFunctionLut["EASE_OUT_SINE"] = AlphaFunctions::EaseOutSine;
122 alphaFunctionLut["EASE_IN_OUT_SINE"] = AlphaFunctions::EaseInOutSine;
123 alphaFunctionLut["EASE_IN_SINE_33"] = AlphaFunctions::EaseInSine33;
124 alphaFunctionLut["EASE_OUT_SINE_33"] = AlphaFunctions::EaseOutSine33;
125 alphaFunctionLut["EASE_IN_OUT_SINE_33"] = AlphaFunctions::EaseInOutSine33;
126 alphaFunctionLut["EASE_IN_OUT_SINE_50"] = AlphaFunctions::EaseInOutSine50;
127 alphaFunctionLut["EASE_IN_OUT_SINE_60"] = AlphaFunctions::EaseInOutSine60;
128 alphaFunctionLut["EASE_IN_OUT_SINE_70"] = AlphaFunctions::EaseInOutSine70;
129 alphaFunctionLut["EASE_IN_OUT_SINE_80"] = AlphaFunctions::EaseInOutSine80;
130 alphaFunctionLut["EASE_IN_OUT_SINE_90"] = AlphaFunctions::EaseInOutSine90;
131 alphaFunctionLut["DOUBLE_EASE_IN_OUT_SINE_60"] = AlphaFunctions::DoubleEaseInOutSine60;
132 alphaFunctionLut["EASE_OUT_QUINT_50"] = AlphaFunctions::EaseOutQuint50;
133 alphaFunctionLut["EASE_OUT_QUINT_80"] = AlphaFunctions::EaseOutQuint80;
134 alphaFunctionLut["BOUNCE"] = AlphaFunctions::Bounce;
135 alphaFunctionLut["BOUNCE_BACK"] = AlphaFunctions::BounceBack;
136 alphaFunctionLut["EASE_IN_BACK"] = AlphaFunctions::EaseInBack;
137 alphaFunctionLut["EASE_OUT_BACK"] = AlphaFunctions::EaseOutBack;
138 alphaFunctionLut["EASE_IN_OUT_BACK"] = AlphaFunctions::EaseInOutBack;
139 alphaFunctionLut["SIN"] = AlphaFunctions::Sin;
140 alphaFunctionLut["SIN2X"] = AlphaFunctions::Sin2x;
143 const AlphaFunctionLut::const_iterator iter( alphaFunctionLut.find( alphaFunction ) );
145 if( iter != alphaFunctionLut.end() )
151 DALI_ASSERT_ALWAYS( iter != alphaFunctionLut.end() && "Unknown Anchor Constant" );
152 return Dali::AlphaFunctions::Default;
156 } // unnamed namespace
168 Animation CreateAnimation( const TreeNode& child, const Replacement& constant, Dali::Actor searchRoot, Builder* const builder )
170 float durationSum = 0.f;
172 Dali::Actor searchActor = searchRoot ? searchRoot : Dali::Stage::GetCurrent().GetRootLayer();
174 Animation animation( Animation::New( 0.f ) );
176 // duration needs to be set before AnimateTo calls for correct operation when AnimateTo has no "time-period".
177 OptionalFloat duration = constant.IsFloat( IsChild(child, "duration" ) );
181 animation.SetDuration( *duration );
184 if( OptionalBoolean looping = constant.IsBoolean( IsChild(child, "loop" ) ) )
186 animation.SetLooping( *looping );
189 if( OptionalString endAction = constant.IsString( IsChild(child, "end-action" ) ) )
191 if("BAKE" == *endAction)
193 animation.SetEndAction( Animation::Bake );
195 else if("DISCARD" == *endAction)
197 animation.SetEndAction( Animation::Discard );
199 else if("BAKE_FINAL" == *endAction)
201 animation.SetEndAction( Animation::BakeFinal );
205 if( OptionalString endAction = constant.IsString( IsChild(child, "disconnect-action" ) ) )
207 if("BAKE" == *endAction)
209 animation.SetDisconnectAction( Animation::Bake );
211 else if("DISCARD" == *endAction)
213 animation.SetDisconnectAction( Animation::Discard );
215 else if("BAKE_FINAL" == *endAction)
217 animation.SetDisconnectAction( Animation::BakeFinal );
221 OptionalChild propertiesNode = IsChild(child, "properties" );
224 const TreeNode::ConstIterator endIter = (*propertiesNode).CEnd();
225 for( TreeNode::ConstIterator iter = (*propertiesNode).CBegin(); endIter != iter; ++iter )
227 const TreeNode::KeyNodePair& pKeyChild = *iter;
229 OptionalString actorName( constant.IsString( IsChild(pKeyChild.second, "actor" ) ) );
230 OptionalString property( constant.IsString( IsChild(pKeyChild.second, "property" ) ) );
231 DALI_ASSERT_ALWAYS( actorName && "Animation must specify actor name" );
233 Handle targetHandle = searchActor.FindChildByName( *actorName );
234 DALI_ASSERT_ALWAYS( targetHandle && "Actor must exist for property" );
236 Property::Value propValue;
237 Property::Index propIndex = Property::INVALID_INDEX;
240 propIndex = targetHandle.GetPropertyIndex( *property );
242 // if the property is not found from the (actor) handle, try to downcast it to renderable actor
243 // to allow animating shader uniforms
244 if( propIndex == Property::INVALID_INDEX )
246 RenderableActor renderable = RenderableActor::DownCast( targetHandle );
249 // A limitation here is that its possible that between creation of animation
250 // and running it the ShaderEffect of the actor has been changed.
251 // However this is a unlikely use case especially when using scripts.
252 if( ShaderEffect effect = renderable.GetShaderEffect() )
254 propIndex = effect.GetPropertyIndex( *property );
255 if(propIndex != Property::INVALID_INDEX)
257 targetHandle = effect;
261 DALI_SCRIPT_WARNING( "Cannot find property on object or ShaderEffect\n" );
268 DALI_SCRIPT_WARNING( "Cannot find property on object or ShaderEffect\n" );
273 if( propIndex == Property::INVALID_INDEX)
275 DALI_ASSERT_ALWAYS( propIndex != Property::INVALID_INDEX && "Animation must specify a valid property" );
280 // these are the defaults
281 AlphaFunction alphaFunction( AlphaFunctions::Default );
282 TimePeriod timePeriod( 0.f );
284 OptionalChild timeChild = IsChild( pKeyChild.second, "time-period" );
288 timePeriod = GetTimePeriod( *timeChild, constant );
291 durationSum = std::max( durationSum, timePeriod.delaySeconds + timePeriod.durationSeconds );
293 if( OptionalString alphaChild = constant.IsString( IsChild(pKeyChild.second, "alpha-function" ) ) )
295 alphaFunction = GetAlphaFunction( *alphaChild );
298 if( OptionalChild keyFrameChild = IsChild(pKeyChild.second, "key-frames") )
300 DALI_ASSERT_ALWAYS( property && "Animation must specify a property name" );
301 Property prop = Property( targetHandle, propIndex );
303 KeyFrames keyframes = KeyFrames::New();
305 const TreeNode::ConstIterator endIter = (*keyFrameChild).CEnd();
306 for( TreeNode::ConstIterator iter = (*keyFrameChild).CBegin(); endIter != iter; ++iter )
308 const TreeNode::KeyNodePair& kfKeyChild = *iter;
310 OptionalFloat kfProgress = constant.IsFloat( IsChild(kfKeyChild.second, "progress" ) );
311 DALI_ASSERT_ALWAYS( kfProgress && "Key frame entry must have 'progress'" );
313 OptionalChild kfValue = IsChild( kfKeyChild.second, "value" );
314 DALI_ASSERT_ALWAYS( kfValue && "Key frame entry must have 'value'" );
318 propValue = GetPropertyValue( prop.object.GetPropertyType(prop.propertyIndex), *kfValue );
322 DALI_LOG_WARNING( "Property:'%s' type does not match value type '%s'\n",
324 PropertyTypes::GetName(prop.object.GetPropertyType(prop.propertyIndex)) );
329 AlphaFunction kfAlphaFunction( AlphaFunctions::Default );
330 if( OptionalString alphaFuncStr = constant.IsString( IsChild(pKeyChild.second, "alpha-function") ) )
332 kfAlphaFunction = GetAlphaFunction( *alphaFuncStr );
335 keyframes.Add( *kfProgress, propValue, kfAlphaFunction );
340 animation.AnimateBetween( prop, keyframes, alphaFunction, timePeriod );
344 animation.AnimateBetween( prop, keyframes, alphaFunction );
347 else if( OptionalString pathChild = IsString(pKeyChild.second, "path") )
350 Path path = builder->GetPath(*pathChild);
353 //Get forward vector if specified
354 Vector3 forward( 0.0f, 0.0f, 0.0f );
355 OptionalVector3 forwardProperty = constant.IsVector3( IsChild(pKeyChild.second, "forward" ) );
356 if( forwardProperty )
358 forward = *forwardProperty;
361 Actor actor = Actor::DownCast( targetHandle );
366 animation.Animate( actor, path, forward, alphaFunction, timePeriod );
370 animation.Animate( actor, path, forward, alphaFunction );
378 DALI_SCRIPT_WARNING( "Cannot find animation path '%s'\n", (*pathChild).c_str() );
383 DALI_ASSERT_ALWAYS( property && "Animation must specify a property name" );
385 Property prop = Property( targetHandle, propIndex );
388 propValue = GetPropertyValue( prop.object.GetPropertyType(prop.propertyIndex), *IsChild(pKeyChild.second, "value") );
392 DALI_LOG_WARNING( "Property:'%s' type does not match value type '%s'\n", (*property).c_str(),
393 PropertyTypes::GetName( prop.object.GetPropertyType(prop.propertyIndex) ) );
398 if( OptionalBoolean relative = constant.IsBoolean( IsChild(pKeyChild.second, "relative") ) )
402 animation.AnimateBy( prop, propValue, alphaFunction, timePeriod );
406 animation.AnimateBy( prop, propValue, alphaFunction );
413 animation.AnimateTo( prop, propValue, alphaFunction, timePeriod );
417 animation.AnimateTo( prop, propValue, alphaFunction );
426 animation.SetDuration( durationSum );
432 Animation CreateAnimation( const TreeNode& child, Builder* const builder )
434 Replacement replacement;
435 return CreateAnimation( child, replacement, Stage::GetCurrent().GetRootLayer(), builder );
438 } // namespace Internal
440 } // namespace Toolkit