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/integration-api/debug.h>
23 #include <dali-toolkit/internal/builder/builder-impl.h>
24 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
25 #include <dali-toolkit/internal/builder/replacement.h>
27 namespace // unnamed namespace
32 TimePeriod GetTimePeriod( const TreeNode& child, const Toolkit::Internal::Replacement& constant )
34 OptionalFloat delay = constant.IsFloat( IsChild(child, "delay" ) );
35 OptionalFloat duration = constant.IsFloat( IsChild(child, "duration" ) );
36 DALI_ASSERT_ALWAYS( duration && "Time period must have at least a duration" );
40 return TimePeriod( *delay, *duration );
44 return TimePeriod( *duration );
48 Property::Value GetPropertyValue( const Property::Type& propType, const TreeNode& child )
52 case Property::BOOLEAN:
54 return Property::Value( GetBoolean( child ) );
59 return Property::Value( GetFloat( child ) );
62 case Property::VECTOR2:
64 return Property::Value( GetVector2( child ) );
67 case Property::VECTOR3:
69 return Property::Value( GetVector3( child ) );
72 case Property::VECTOR4:
74 return Property::Value( GetVector4( child ) );
77 case Property::ROTATION:
79 if( 4 == child.Size() )
81 Vector4 v(GetVector4(child));
82 // angle, axis as per spec
83 return Property::Value( Quaternion(Radian(Degree(v[3])),
84 Vector3(v[0],v[1],v[2])) );
88 // degrees as per spec
89 Vector3 rotation = GetVector3( child );
90 return Property::Value( Quaternion(Radian(Degree(rotation.x)),
91 Radian(Degree(rotation.y)),
92 Radian(Degree(rotation.z))) );
96 case Property::NONE: // fall
99 DALI_ASSERT_ALWAYS( !"Property type incorrect" );
104 AlphaFunction GetAlphaFunction( const std::string& alphaFunction )
106 typedef std::map< const std::string, Dali::AlphaFunction > AlphaFunctionLut;
107 static AlphaFunctionLut alphaFunctionLut;
109 if( 0 == alphaFunctionLut.size() )
111 // coding convention is uppercase enums
112 alphaFunctionLut["DEFAULT"] = AlphaFunction(AlphaFunction::DEFAULT);
113 alphaFunctionLut["LINEAR"] = AlphaFunction(AlphaFunction::LINEAR);
114 alphaFunctionLut["REVERSE"] = AlphaFunction(AlphaFunction::REVERSE);
115 alphaFunctionLut["EASE_IN_SQUARE"] = AlphaFunction(AlphaFunction::EASE_IN_SQUARE);
116 alphaFunctionLut["EASE_OUT_SQUARE"] = AlphaFunction(AlphaFunction::EASE_OUT_SQUARE);
117 alphaFunctionLut["EASE_IN"] = AlphaFunction(AlphaFunction::EASE_IN);
118 alphaFunctionLut["EASE_OUT"] = AlphaFunction(AlphaFunction::EASE_OUT);
119 alphaFunctionLut["EASE_IN_OUT"] = AlphaFunction(AlphaFunction::EASE_IN_OUT);
120 alphaFunctionLut["EASE_IN_SINE"] = AlphaFunction(AlphaFunction::EASE_IN_SINE);
121 alphaFunctionLut["EASE_OUT_SINE"] = AlphaFunction(AlphaFunction::EASE_OUT_SINE);
122 alphaFunctionLut["EASE_IN_OUT_SINE"] = AlphaFunction(AlphaFunction::EASE_IN_OUT_SINE);
123 alphaFunctionLut["BOUNCE"] = AlphaFunction(AlphaFunction::BOUNCE);
124 alphaFunctionLut["SIN"] = AlphaFunction(AlphaFunction::SIN);
125 alphaFunctionLut["EASE_OUT_BACK"] = AlphaFunction(AlphaFunction::EASE_OUT_BACK);
128 const AlphaFunctionLut::const_iterator iter( alphaFunctionLut.find( alphaFunction ) );
130 if( iter != alphaFunctionLut.end() )
136 DALI_ASSERT_ALWAYS( iter != alphaFunctionLut.end() && "Unknown Alpha Constant" );
137 return Dali::AlphaFunction::DEFAULT;
141 } // unnamed namespace
153 Animation CreateAnimation( const TreeNode& child, const Replacement& constant, Dali::Actor searchRoot, Builder* const builder )
155 float durationSum = 0.f;
157 Dali::Actor searchActor = searchRoot ? searchRoot : Dali::Stage::GetCurrent().GetRootLayer();
159 Animation animation( Animation::New( 0.f ) );
161 // duration needs to be set before AnimateTo calls for correct operation when AnimateTo has no "timePeriod".
162 OptionalFloat duration = constant.IsFloat( IsChild(child, "duration" ) );
166 animation.SetDuration( *duration );
169 if( OptionalBoolean looping = constant.IsBoolean( IsChild(child, "loop" ) ) )
171 animation.SetLooping( *looping );
174 if( OptionalString endAction = constant.IsString( IsChild(child, "endAction" ) ) )
176 if("BAKE" == *endAction)
178 animation.SetEndAction( Animation::Bake );
180 else if("DISCARD" == *endAction)
182 animation.SetEndAction( Animation::Discard );
184 else if("BAKE_FINAL" == *endAction)
186 animation.SetEndAction( Animation::BakeFinal );
190 if( OptionalString endAction = constant.IsString( IsChild(child, "disconnectAction" ) ) )
192 if("BAKE" == *endAction)
194 animation.SetDisconnectAction( Animation::Bake );
196 else if("DISCARD" == *endAction)
198 animation.SetDisconnectAction( Animation::Discard );
200 else if("BAKE_FINAL" == *endAction)
202 animation.SetDisconnectAction( Animation::BakeFinal );
206 OptionalChild propertiesNode = IsChild(child, "properties" );
209 const TreeNode::ConstIterator endIter = (*propertiesNode).CEnd();
210 for( TreeNode::ConstIterator iter = (*propertiesNode).CBegin(); endIter != iter; ++iter )
212 const TreeNode::KeyNodePair& pKeyChild = *iter;
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" );
218 Handle targetHandle = searchActor.FindChildByName( *actorName );
219 DALI_ASSERT_ALWAYS( targetHandle && "Actor must exist for property" );
221 Property::Value propValue;
222 Property::Index propIndex = Property::INVALID_INDEX;
225 propIndex = targetHandle.GetPropertyIndex( *property );
227 // if the property is not found from the (actor) handle, try to downcast it to renderable actor
228 // to allow animating shader uniforms
229 if( propIndex == Property::INVALID_INDEX )
231 DALI_SCRIPT_WARNING( "Cannot find property on object\n" );
236 // these are the defaults
237 AlphaFunction alphaFunction( AlphaFunction::DEFAULT );
238 TimePeriod timePeriod( 0.f );
240 OptionalChild timeChild = IsChild( pKeyChild.second, "timePeriod" );
244 timePeriod = GetTimePeriod( *timeChild, constant );
247 durationSum = std::max( durationSum, timePeriod.delaySeconds + timePeriod.durationSeconds );
249 if( OptionalString alphaChild = constant.IsString( IsChild(pKeyChild.second, "alphaFunction" ) ) )
251 alphaFunction = GetAlphaFunction( *alphaChild );
254 if( OptionalChild keyFrameChild = IsChild(pKeyChild.second, "keyFrames") )
256 DALI_ASSERT_ALWAYS( property && "Animation must specify a property name" );
257 Property prop = Property( targetHandle, propIndex );
259 KeyFrames keyframes = KeyFrames::New();
261 const TreeNode::ConstIterator endIter = (*keyFrameChild).CEnd();
262 for( TreeNode::ConstIterator iter = (*keyFrameChild).CBegin(); endIter != iter; ++iter )
264 const TreeNode::KeyNodePair& kfKeyChild = *iter;
266 OptionalFloat kfProgress = constant.IsFloat( IsChild(kfKeyChild.second, "progress" ) );
267 DALI_ASSERT_ALWAYS( kfProgress && "Key frame entry must have 'progress'" );
269 OptionalChild kfValue = IsChild( kfKeyChild.second, "value" );
270 DALI_ASSERT_ALWAYS( kfValue && "Key frame entry must have 'value'" );
274 propValue = GetPropertyValue( prop.object.GetPropertyType(prop.propertyIndex), *kfValue );
278 DALI_LOG_WARNING( "Property:'%s' type does not match value type '%s'\n",
280 PropertyTypes::GetName(prop.object.GetPropertyType(prop.propertyIndex)) );
285 AlphaFunction kfAlphaFunction( AlphaFunction::DEFAULT );
286 if( OptionalString alphaFuncStr = constant.IsString( IsChild(pKeyChild.second, "alphaFunction") ) )
288 kfAlphaFunction = GetAlphaFunction( *alphaFuncStr );
291 keyframes.Add( *kfProgress, propValue, kfAlphaFunction );
296 animation.AnimateBetween( prop, keyframes, alphaFunction, timePeriod );
300 animation.AnimateBetween( prop, keyframes, alphaFunction );
303 else if( OptionalString pathChild = IsString(pKeyChild.second, "path") )
306 Path path = builder->GetPath(*pathChild);
309 //Get forward vector if specified
310 Vector3 forward( 0.0f, 0.0f, 0.0f );
311 OptionalVector3 forwardProperty = constant.IsVector3( IsChild(pKeyChild.second, "forward" ) );
312 if( forwardProperty )
314 forward = *forwardProperty;
317 Actor actor = Actor::DownCast( targetHandle );
322 animation.Animate( actor, path, forward, alphaFunction, timePeriod );
326 animation.Animate( actor, path, forward, alphaFunction );
334 DALI_SCRIPT_WARNING( "Cannot find animation path '%s'\n", (*pathChild).c_str() );
339 DALI_ASSERT_ALWAYS( property && "Animation must specify a property name" );
341 Property prop = Property( targetHandle, propIndex );
344 propValue = GetPropertyValue( prop.object.GetPropertyType(prop.propertyIndex), *IsChild(pKeyChild.second, "value") );
348 DALI_LOG_WARNING( "Property:'%s' type does not match value type '%s'\n", (*property).c_str(),
349 PropertyTypes::GetName( prop.object.GetPropertyType(prop.propertyIndex) ) );
354 if( OptionalBoolean relative = constant.IsBoolean( IsChild(pKeyChild.second, "relative") ) )
358 animation.AnimateBy( prop, propValue, alphaFunction, timePeriod );
362 animation.AnimateBy( prop, propValue, alphaFunction );
369 animation.AnimateTo( prop, propValue, alphaFunction, timePeriod );
373 animation.AnimateTo( prop, propValue, alphaFunction );
382 animation.SetDuration( durationSum );
388 Animation CreateAnimation( const TreeNode& child, Builder* const builder )
390 Replacement replacement;
391 return CreateAnimation( child, replacement, Stage::GetCurrent().GetRootLayer(), builder );
394 } // namespace Internal
396 } // namespace Toolkit