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>
20 #include <boost/function.hpp>
23 #include <dali-toolkit/internal/builder/builder-impl.h>
24 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
32 extern Animation CreateAnimation( const TreeNode& child, Dali::Toolkit::Internal::Builder* const builder );
33 extern bool SetPropertyFromNode( const TreeNode& node, Property::Value& value );
46 // Action on child actor. The child is found by alias so can be 'previous' etc.
47 struct ChildActorAction
49 std::string actorName;
50 std::string actionName;
51 std::string childAlias;
52 PropertyValueContainer parameters;
56 Actor actor = Stage::GetCurrent().GetRootLayer().FindChildByName(actorName);
60 Actor child_actor = actor.FindChildByAlias(childAlias);
64 child_actor.DoAction(actionName, parameters);
68 DALI_SCRIPT_WARNING("Could not find child by alias '%s'\n", childAlias.c_str());
74 // Action to set a property
75 struct PropertySetAction
77 std::string actorName;
78 std::string propertyName;
79 Property::Value value;
83 Actor actor = Stage::GetCurrent().GetRootLayer().FindChildByName(actorName);
87 Property::Index idx = actor.GetPropertyIndex(propertyName);
89 if( idx != Property::INVALID_INDEX )
91 if( actor.GetPropertyType(idx) != value.GetType() )
93 DALI_SCRIPT_WARNING("Set property action has different type for property '%s'\n", propertyName.c_str());
97 actor.SetProperty( idx, value );
102 DALI_SCRIPT_WARNING("Set property action cannot find property '%s'\n", propertyName.c_str());
108 // Generic action on a handle (Animation & Actor)
111 std::string actorName;
112 std::string actionName;
113 PropertyValueContainer parameters;
115 void operator()(void)
117 Actor actor = Stage::GetCurrent().GetRootLayer().FindChildByName(actorName);
120 actor.DoAction(actionName, parameters);
126 // Delay an animation play; ie wait as its not on stage yet
127 struct DelayedAnimationPlay
129 OptionalChild animNode;
130 Dali::IntrusivePtr<Dali::Toolkit::Internal::Builder> builder;
132 void operator()(void)
134 Animation anim = Toolkit::Internal::CreateAnimation(*animNode, builder.Get() );
143 * Gets Property::Value from child
145 Property::Value GetPropertyValue(const TreeNode &child)
147 size_t nChildren = child.Size();
153 // cast away unused return for static analyzers
154 static_cast<void>( Dali::Toolkit::Internal::SetPropertyFromNode( child, ret ) );
156 else if(1 == nChildren)
158 // {"property": {"quaternion":[1,2,3,4]} }
159 // {"property": {"angle":22, "axis": [1,2,3]} }
161 OptionalChild quaternion = IsChild(&child, "quaternion");
162 OptionalChild axis = IsChild(&child, "axis");
163 OptionalChild angle = IsChild(&child, "angle");
167 ret = Property::Value(Quaternion(GetVector4(*quaternion)));
169 else if(axis && angle)
171 ret = Property::Value(AngleAxis(Degree(GetFloat(*angle)), GetVector3(*axis)));
174 else if(2 == nChildren)
176 // {"property": [1,2]}
177 ret = Property::Value(GetVector2(child));
179 else if(3 == nChildren)
181 // {"property": [1,2,3]}
182 ret = Property::Value(GetVector3(child));
184 else if(4 == nChildren)
186 // {"property": [1,2,3,4]}
187 ret = Property::Value(GetVector4(child));
195 * Gets Parmeter list from child
196 * params is be cleared before insertion
198 void GetParameters(const TreeNode& child, PropertyValueContainer& params)
200 if( OptionalChild c = IsChild(child, "parameters") )
202 const TreeNode& node = *c;
206 GetPropertyValue(node);
211 params.reserve(node.Size());
213 for(TreeNode::ConstIterator iter(node.CBegin()); iter != node.CEnd(); ++iter)
215 params.push_back( GetPropertyValue( (*iter).second ) );
221 void DoNothing(void) {};
224 * Get an action as boost function callback
226 boost::function<void (void)> GetAction(const TreeNode &root, const TreeNode &child, Actor actor, boost::function<void (void)> quitAction, Dali::Toolkit::Internal::Builder* const builder)
228 OptionalString childActorName(IsString( IsChild(&child, "child-actor")) );
229 OptionalString actorName(IsString( IsChild(&child, "actor")) );
230 OptionalString propertyName(IsString( IsChild(&child, "property")) );
231 OptionalChild valueChild( IsChild(&child, "value") );
233 OptionalString actionName = IsString( IsChild(&child, "action") );
234 DALI_ASSERT_ALWAYS(actionName && "Signal must have an action");
236 boost::function<void(void)> callback = DoNothing;
240 ChildActorAction action;
241 action.actorName = *actorName;
242 action.childAlias = *childActorName;
243 action.actionName = *actionName;
244 GetParameters(child, action.parameters);
249 if(propertyName && valueChild && ("set" == *actionName) )
251 PropertySetAction action;
252 action.actorName = *actorName;
253 action.propertyName = *propertyName;
254 // actor may not exist yet so we can't check the property type
255 if( !Dali::Toolkit::Internal::SetPropertyFromNode( *valueChild, action.value ) )
257 DALI_SCRIPT_WARNING("Cannot set property for set property action\n");
263 GenericAction action;
264 action.actorName = *actorName;
265 action.actionName = *actionName;
266 GetParameters(child, action.parameters);
270 else if("quit" == *actionName)
272 callback = quitAction;
274 else if("play" == *actionName)
276 OptionalChild animations = IsChild( root, "animations" );
277 OptionalString animationName = IsString( IsChild(child, "animation") );
278 if( animations && animationName )
280 if( OptionalChild animNode = IsChild(*animations, *animationName) )
282 DelayedAnimationPlay action;
283 action.animNode = animNode;
284 action.builder = builder;
285 // @todo; put constants into the map
290 DALI_SCRIPT_WARNING("Cannot find animation '%s'\n", (*animationName).c_str());
295 DALI_SCRIPT_WARNING("Cannot find animations section\n");
300 // no named actor; presume self
301 GenericAction action;
302 action.actorName = actor.GetName();
303 action.actionName = *actionName;
304 GetParameters(child, action.parameters);
313 * Get a notification condition argument0 as 'arg0' 'value' or 'min'
315 float GetConditionArg0(const TreeNode &child)
317 OptionalFloat f = IsFloat( IsChild(child, "arg0") );
318 // allowing some human preferable alternatives
321 f = IsFloat( IsChild(child, "value") );
325 f = IsFloat( IsChild(child, "min") );
328 DALI_ASSERT_ALWAYS(f && "Notification condition for arg0 not specified");
334 * Get a notification condition argument1 as 'arg1' or 'max'
336 float GetConditionArg1(const TreeNode &child)
338 OptionalFloat f = IsFloat( IsChild(child, "arg1") );
339 // allowing some human preferable alternatives
342 f = IsFloat( IsChild(child, "max") );
345 DALI_ASSERT_ALWAYS(f && "Notification condition for arg1 not specified");
361 Actor SetupSignalAction(const TreeNode &child, Actor actor, Dali::Toolkit::Internal::Builder* const builder );
362 Actor SetupPropertyNotification(const TreeNode &child, Actor actor, Dali::Toolkit::Internal::Builder* const builder );
365 * Setup signals and actions on an actor
367 Actor SetupSignalAction(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, boost::function<void (void)> quitAction, Dali::Toolkit::Internal::Builder* const builder )
369 DALI_ASSERT_ALWAYS(actor);
371 if(OptionalChild signalsChild = IsChild(child, "signals"))
373 const TreeNode& signalsNode = *signalsChild;
374 const TreeConstIter endIter = signalsNode.CEnd();
375 for( TreeConstIter iter = signalsNode.CBegin(); endIter != iter; ++iter )
377 const TreeNode::KeyNodePair& key_child = *iter;
379 DALI_SCRIPT_INFO(" Creating Signal for: %s\n", actor.GetName().c_str());
381 OptionalString name( IsString( IsChild( key_child.second, "name")) );
382 DALI_ASSERT_ALWAYS(name && "Signal must have a name");
384 boost::function<void (void)> callback = GetAction(root, key_child.second, actor, quitAction, builder );
386 actor.ConnectSignal(tracker, *name, callback);
394 * Setup Property notifications for an actor
396 Actor SetupPropertyNotification(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, boost::function<void (void)> quitAction, Dali::Toolkit::Internal::Builder* const builder )
398 DALI_ASSERT_ALWAYS(actor);
400 if(OptionalChild notificationsChild = IsChild(child,"notifications"))
402 const TreeNode& notificationsNode = *notificationsChild;
403 const TreeNode::ConstIterator endIter = notificationsNode.CEnd();
404 for( TreeNode::ConstIterator iter = notificationsNode.CBegin(); endIter != iter; ++iter )
406 const TreeNode::KeyNodePair& key_child = *iter;
408 // Actor actions reference by pointer because of circular reference actor->signal
409 // So this callback should only go onto the actor maintained list.
410 boost::function<void (void)> callback = GetAction(root, key_child.second, actor, quitAction, builder );
412 OptionalString prop(IsString( IsChild(key_child.second, "property")) );
413 DALI_ASSERT_ALWAYS(prop && "Notification signal must specify a property");
415 Property::Index prop_index = actor.GetPropertyIndex(*prop);
416 DALI_ASSERT_ALWAYS(prop_index != Property::INVALID_INDEX && "Notification signal specifies an unknown property");
418 OptionalString cond(IsString( IsChild(key_child.second, "condition")));
419 DALI_ASSERT_ALWAYS(cond && "Notification signal must specify a condition");
423 PropertyNotification notification = actor.AddPropertyNotification( actor.GetPropertyIndex(*prop),
424 LessThanCondition(1.f) );
425 notification.NotifySignal().Connect( tracker, FunctorDelegate::New(callback) );
427 else if("LessThan" == *cond)
429 PropertyNotification notification = actor.AddPropertyNotification( actor.GetPropertyIndex(*prop),
430 LessThanCondition(GetConditionArg0(key_child.second)) );
431 notification.NotifySignal().Connect( tracker, FunctorDelegate::New(callback) );
433 else if("GreaterThan" == *cond)
435 PropertyNotification notification = actor.AddPropertyNotification( actor.GetPropertyIndex(*prop),
436 GreaterThanCondition(GetConditionArg0(key_child.second)) );
437 notification.NotifySignal().Connect( tracker, FunctorDelegate::New(callback) );
439 else if("Inside" == *cond)
441 PropertyNotification notification = actor.AddPropertyNotification( actor.GetPropertyIndex(*prop),
442 InsideCondition(GetConditionArg0(key_child.second),
443 GetConditionArg1(key_child.second)) );
444 notification.NotifySignal().Connect( tracker, FunctorDelegate::New(callback) );
446 else if("Outside" == *cond)
448 PropertyNotification notification = actor.AddPropertyNotification( actor.GetPropertyIndex(*prop),
449 OutsideCondition(GetConditionArg0(key_child.second),
450 GetConditionArg1(key_child.second)) );
451 notification.NotifySignal().Connect( tracker, FunctorDelegate::New(callback) );
455 DALI_ASSERT_ALWAYS(!"Unknown condition");
458 } // if notifications
462 } // AddPropertyNotification
465 } // namespace Internal
466 } // namespace Toolkit