2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
18 #include <dali/integration-api/debug.h>
21 #include <dali-toolkit/internal/builder/builder-impl.h>
22 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
23 #include <dali-toolkit/internal/builder/replacement.h>
25 namespace // unnamed namespace
30 TimePeriod GetTimePeriod( const TreeNode& child, const Toolkit::Internal::Replacement& constant )
32 OptionalFloat delay = constant.IsFloat( IsChild(child, "delay" ) );
33 OptionalFloat duration = constant.IsFloat( IsChild(child, "duration" ) );
34 DALI_ASSERT_ALWAYS( duration && "Time period must have at least a duration" );
38 return TimePeriod( *delay, *duration );
42 return TimePeriod( *duration );
46 Property::Value GetPropertyValue( const Property::Type& propType, const TreeNode& child )
50 case Property::BOOLEAN:
52 return Property::Value( GetBoolean( child ) );
57 return Property::Value( GetFloat( child ) );
60 case Property::VECTOR2:
62 return Property::Value( GetVector2( child ) );
65 case Property::VECTOR3:
67 return Property::Value( GetVector3( child ) );
70 case Property::VECTOR4:
72 return Property::Value( GetVector4( child ) );
75 case Property::ROTATION:
77 if( 4 == child.Size() )
79 Vector4 v(GetVector4(child));
80 // angle, axis as per spec
81 return Property::Value( Quaternion(Radian(Degree(v[3])),
82 Vector3(v[0],v[1],v[2])) );
86 // degrees as per spec
87 Vector3 rotation = GetVector3( child );
88 return Property::Value( Quaternion(Radian(Degree(rotation.x)),
89 Radian(Degree(rotation.y)),
90 Radian(Degree(rotation.z))) );
94 case Property::NONE: // fall
97 DALI_ASSERT_ALWAYS( !"Property type incorrect" );
98 return Property::Value();
103 AlphaFunction GetAlphaFunction( const std::string& alphaFunction )
105 typedef std::map< const std::string, Dali::AlphaFunction > AlphaFunctionLut;
106 static AlphaFunctionLut alphaFunctionLut;
108 if( 0 == alphaFunctionLut.size() )
110 // coding convention is uppercase enums
111 alphaFunctionLut["DEFAULT"] = Dali::AlphaFunctions::Default;
112 alphaFunctionLut["LINEAR"] = Dali::AlphaFunctions::Linear;
113 alphaFunctionLut["REVERSE"] = Dali::AlphaFunctions::Reverse;
114 alphaFunctionLut["EASE_IN"] = Dali::AlphaFunctions::EaseIn;
115 alphaFunctionLut["EASE_OUT"] = Dali::AlphaFunctions::EaseOut;
116 alphaFunctionLut["EASE_IN_OUT"] = Dali::AlphaFunctions::EaseInOut;
117 alphaFunctionLut["EASE_IN_SINE"] = Dali::AlphaFunctions::EaseInSine;
118 alphaFunctionLut["EASE_OUT_SINE"] = Dali::AlphaFunctions::EaseOutSine;
119 alphaFunctionLut["EASE_IN_OUT_SINE"]= Dali::AlphaFunctions::EaseInOutSine;
120 alphaFunctionLut["BOUNCE"] = Dali::AlphaFunctions::Bounce;
121 alphaFunctionLut["BOUNCE_BACK"] = Dali::AlphaFunctions::BounceBack;
122 alphaFunctionLut["EASE_OUT_BACK"] = Dali::AlphaFunctions::EaseOutBack;
123 alphaFunctionLut["SIN"] = Dali::AlphaFunctions::Sin;
124 alphaFunctionLut["SIN2X"] = Dali::AlphaFunctions::Sin;
127 const AlphaFunctionLut::const_iterator iter( alphaFunctionLut.find( alphaFunction ) );
129 if( iter != alphaFunctionLut.end() )
135 DALI_ASSERT_ALWAYS( iter != alphaFunctionLut.end() && "Unknown Anchor Constant" );
136 return Dali::AlphaFunctions::Default;
140 } // unnamed namespace
152 Animation CreateAnimation( const TreeNode& child, const Replacement& constant, Dali::Actor searchRoot )
154 float durationSum = 0.f;
156 Dali::Actor searchActor = searchRoot ? searchRoot : Dali::Stage::GetCurrent().GetRootLayer();
158 Animation animation( Animation::New( 0.f ) );
160 // duration needs to be set before AnimateTo calls for correct operation when AnimateTo has no "time-period".
161 OptionalFloat duration = constant.IsFloat( IsChild(child, "duration" ) );
165 animation.SetDuration( *duration );
168 if( OptionalBoolean looping = constant.IsBoolean( IsChild(child, "loop" ) ) )
170 animation.SetLooping( *looping );
173 if( OptionalString endAction = constant.IsString( IsChild(child, "end-action" ) ) )
175 if("BAKE" == *endAction)
177 animation.SetEndAction( Animation::Bake );
179 else if("DISCARD" == *endAction)
181 animation.SetEndAction( Animation::Discard );
183 else if("BAKE_FINAL" == *endAction)
185 animation.SetEndAction( Animation::BakeFinal );
189 if( OptionalString endAction = constant.IsString( IsChild(child, "destroy-action" ) ) )
191 if("BAKE" == *endAction)
193 animation.SetDestroyAction( Animation::Bake );
195 else if("DISCARD" == *endAction)
197 animation.SetDestroyAction( Animation::Discard );
199 else if("BAKE_FINAL" == *endAction)
201 animation.SetDestroyAction( Animation::BakeFinal );
205 OptionalChild propertiesNode = IsChild(child, "properties" );
208 const TreeNode::ConstIterator endIter = (*propertiesNode).CEnd();
209 for( TreeNode::ConstIterator iter = (*propertiesNode).CBegin(); endIter != iter; ++iter )
211 const TreeNode::KeyNodePair& pKeyChild = *iter;
213 OptionalString actorName( constant.IsString( IsChild(pKeyChild.second, "actor" ) ) );
214 OptionalString property( constant.IsString( IsChild(pKeyChild.second, "property" ) ) );
215 DALI_ASSERT_ALWAYS( actorName && "Animation must specify actor name" );
216 DALI_ASSERT_ALWAYS( property && "Animation must specify a property name" );
218 Actor targetActor = searchActor.FindChildByName( *actorName );
219 DALI_ASSERT_ALWAYS( targetActor && "Actor must exist for property" );
221 Property::Index idx( targetActor.GetPropertyIndex( *property ) );
223 // A limitation here is that its possible that between binding to the signal and
224 // the signal call that the ShaderEffect of the targetActor has been changed.
225 // However this is a unlikely use case especially when using scripts.
226 if( idx == Property::INVALID_INDEX )
228 if( ShaderEffect effect = targetActor.GetShaderEffect() )
230 idx = effect.GetPropertyIndex( *property );
231 if(idx != Property::INVALID_INDEX)
233 targetActor = effect;
237 DALI_SCRIPT_WARNING( "Cannot find property on object or ShaderEffect\n" );
243 DALI_SCRIPT_WARNING( "Cannot find property on object or ShaderEffect\n" );
248 if( idx == Property::INVALID_INDEX)
250 DALI_ASSERT_ALWAYS( idx != Property::INVALID_INDEX && "Animation must specify a valid property" );
254 Property prop( Property( targetActor, idx ) );
255 Property::Value propValue;
257 // these are the defaults
258 AlphaFunction alphaFunction( AlphaFunctions::Default );
259 TimePeriod timePeriod( 0.f );
261 OptionalChild timeChild = IsChild( pKeyChild.second, "time-period" );
265 timePeriod = GetTimePeriod( *timeChild, constant );
268 durationSum = std::max( durationSum, timePeriod.delaySeconds + timePeriod.durationSeconds );
270 if( OptionalString alphaChild = constant.IsString( IsChild(pKeyChild.second, "alpha-function" ) ) )
272 alphaFunction = GetAlphaFunction( *alphaChild );
275 if( OptionalChild keyFrameChild = IsChild(pKeyChild.second, "key-frames") )
277 KeyFrames keyframes = KeyFrames::New();
279 const TreeNode::ConstIterator endIter = (*keyFrameChild).CEnd();
280 for( TreeNode::ConstIterator iter = (*keyFrameChild).CBegin(); endIter != iter; ++iter )
282 const TreeNode::KeyNodePair& kfKeyChild = *iter;
284 OptionalFloat kfProgress = constant.IsFloat( IsChild(kfKeyChild.second, "progress" ) );
285 DALI_ASSERT_ALWAYS( kfProgress && "Key frame entry must have 'progress'" );
287 OptionalChild kfValue = IsChild( kfKeyChild.second, "value" );
288 DALI_ASSERT_ALWAYS( kfValue && "Key frame entry must have 'value'" );
292 propValue = GetPropertyValue( prop.object.GetPropertyType(prop.propertyIndex), *kfValue );
296 DALI_LOG_WARNING( "Property:'%s' type does not match value type '%s'\n",
298 PropertyTypes::GetName(prop.object.GetPropertyType(prop.propertyIndex)) );
303 AlphaFunction kfAlphaFunction( AlphaFunctions::Default );
304 if( OptionalString alphaFuncStr = constant.IsString( IsChild(pKeyChild.second, "alpha-function") ) )
306 kfAlphaFunction = GetAlphaFunction( *alphaFuncStr );
309 keyframes.Add( *kfProgress, propValue, kfAlphaFunction );
314 animation.AnimateBetween( prop, keyframes, alphaFunction, timePeriod );
318 animation.AnimateBetween( prop, keyframes, alphaFunction );
325 propValue = GetPropertyValue( prop.object.GetPropertyType(prop.propertyIndex), *IsChild(pKeyChild.second, "value") );
329 DALI_LOG_WARNING( "Property:'%s' type does not match value type '%s'\n", (*property).c_str(),
330 PropertyTypes::GetName( prop.object.GetPropertyType(prop.propertyIndex) ) );
335 if( OptionalBoolean relative = constant.IsBoolean( IsChild(pKeyChild.second, "relative") ) )
339 animation.AnimateBy( prop, propValue, alphaFunction, timePeriod );
343 animation.AnimateBy( prop, propValue, alphaFunction );
350 animation.AnimateTo( prop, propValue, alphaFunction, timePeriod );
354 animation.AnimateTo( prop, propValue, alphaFunction );
363 animation.SetDuration( durationSum );
369 Animation CreateAnimation( const TreeNode& child )
371 Replacement replacement;
372 return CreateAnimation( child, replacement, Stage::GetCurrent().GetRootLayer() );
375 } // namespace Internal
377 } // namespace Toolkit