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/integration-api/debug.h>
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>
26 namespace // unnamed namespace
31 TimePeriod GetTimePeriod( const TreeNode& child, const Toolkit::Internal::Replacement& constant )
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" );
39 return TimePeriod( *delay, *duration );
43 return TimePeriod( *duration );
47 Property::Value GetPropertyValue( const Property::Type& propType, const TreeNode& child )
51 case Property::BOOLEAN:
53 return Property::Value( GetBoolean( child ) );
58 return Property::Value( GetFloat( child ) );
61 case Property::VECTOR2:
63 return Property::Value( GetVector2( child ) );
66 case Property::VECTOR3:
68 return Property::Value( GetVector3( child ) );
71 case Property::VECTOR4:
73 return Property::Value( GetVector4( child ) );
76 case Property::ROTATION:
78 if( 4 == child.Size() )
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])) );
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))) );
95 case Property::NONE: // fall
98 DALI_ASSERT_ALWAYS( !"Property type incorrect" );
99 return Property::Value();
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"] = 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;
128 const AlphaFunctionLut::const_iterator iter( alphaFunctionLut.find( alphaFunction ) );
130 if( iter != alphaFunctionLut.end() )
136 DALI_ASSERT_ALWAYS( iter != alphaFunctionLut.end() && "Unknown Anchor Constant" );
137 return Dali::AlphaFunctions::Default;
141 } // unnamed namespace
153 Animation CreateAnimation( const TreeNode& child, const Replacement& constant, Dali::Actor searchRoot )
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 "time-period".
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, "end-action" ) ) )
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, "destroy-action" ) ) )
192 if("BAKE" == *endAction)
194 animation.SetDestroyAction( Animation::Bake );
196 else if("DISCARD" == *endAction)
198 animation.SetDestroyAction( Animation::Discard );
200 else if("BAKE_FINAL" == *endAction)
202 animation.SetDestroyAction( 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" );
217 DALI_ASSERT_ALWAYS( property && "Animation must specify a property name" );
219 Actor targetActor = searchActor.FindChildByName( *actorName );
220 DALI_ASSERT_ALWAYS( targetActor && "Actor must exist for property" );
222 Property::Index idx( targetActor.GetPropertyIndex( *property ) );
224 // A limitation here is that its possible that between binding to the signal and
225 // the signal call that the ShaderEffect of the targetActor has been changed.
226 // However this is a unlikely use case especially when using scripts.
227 if( idx == Property::INVALID_INDEX )
229 if( ShaderEffect effect = targetActor.GetShaderEffect() )
231 idx = effect.GetPropertyIndex( *property );
232 if(idx != Property::INVALID_INDEX)
234 targetActor = effect;
238 DALI_SCRIPT_WARNING( "Cannot find property on object or ShaderEffect\n" );
244 DALI_SCRIPT_WARNING( "Cannot find property on object or ShaderEffect\n" );
249 if( idx == Property::INVALID_INDEX)
251 DALI_ASSERT_ALWAYS( idx != Property::INVALID_INDEX && "Animation must specify a valid property" );
255 Property prop( Property( targetActor, idx ) );
256 Property::Value propValue;
258 // these are the defaults
259 AlphaFunction alphaFunction( AlphaFunctions::Default );
260 TimePeriod timePeriod( 0.f );
262 OptionalChild timeChild = IsChild( pKeyChild.second, "time-period" );
266 timePeriod = GetTimePeriod( *timeChild, constant );
269 durationSum = std::max( durationSum, timePeriod.delaySeconds + timePeriod.durationSeconds );
271 if( OptionalString alphaChild = constant.IsString( IsChild(pKeyChild.second, "alpha-function" ) ) )
273 alphaFunction = GetAlphaFunction( *alphaChild );
276 if( OptionalChild keyFrameChild = IsChild(pKeyChild.second, "key-frames") )
278 KeyFrames keyframes = KeyFrames::New();
280 const TreeNode::ConstIterator endIter = (*keyFrameChild).CEnd();
281 for( TreeNode::ConstIterator iter = (*keyFrameChild).CBegin(); endIter != iter; ++iter )
283 const TreeNode::KeyNodePair& kfKeyChild = *iter;
285 OptionalFloat kfProgress = constant.IsFloat( IsChild(kfKeyChild.second, "progress" ) );
286 DALI_ASSERT_ALWAYS( kfProgress && "Key frame entry must have 'progress'" );
288 OptionalChild kfValue = IsChild( kfKeyChild.second, "value" );
289 DALI_ASSERT_ALWAYS( kfValue && "Key frame entry must have 'value'" );
293 propValue = GetPropertyValue( prop.object.GetPropertyType(prop.propertyIndex), *kfValue );
297 DALI_LOG_WARNING( "Property:'%s' type does not match value type '%s'\n",
299 PropertyTypes::GetName(prop.object.GetPropertyType(prop.propertyIndex)) );
304 AlphaFunction kfAlphaFunction( AlphaFunctions::Default );
305 if( OptionalString alphaFuncStr = constant.IsString( IsChild(pKeyChild.second, "alpha-function") ) )
307 kfAlphaFunction = GetAlphaFunction( *alphaFuncStr );
310 keyframes.Add( *kfProgress, propValue, kfAlphaFunction );
315 animation.AnimateBetween( prop, keyframes, alphaFunction, timePeriod );
319 animation.AnimateBetween( prop, keyframes, alphaFunction );
326 propValue = GetPropertyValue( prop.object.GetPropertyType(prop.propertyIndex), *IsChild(pKeyChild.second, "value") );
330 DALI_LOG_WARNING( "Property:'%s' type does not match value type '%s'\n", (*property).c_str(),
331 PropertyTypes::GetName( prop.object.GetPropertyType(prop.propertyIndex) ) );
336 if( OptionalBoolean relative = constant.IsBoolean( IsChild(pKeyChild.second, "relative") ) )
340 animation.AnimateBy( prop, propValue, alphaFunction, timePeriod );
344 animation.AnimateBy( prop, propValue, alphaFunction );
351 animation.AnimateTo( prop, propValue, alphaFunction, timePeriod );
355 animation.AnimateTo( prop, propValue, alphaFunction );
364 animation.SetDuration( durationSum );
370 Animation CreateAnimation( const TreeNode& child )
372 Replacement replacement;
373 return CreateAnimation( child, replacement, Stage::GetCurrent().GetRootLayer() );
376 } // namespace Internal
378 } // namespace Toolkit