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 );
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 Toolkit::JsonParser memento;
131 void operator()(void)
133 Animation anim = Toolkit::Internal::CreateAnimation(*memento.GetRoot());
142 * Gets Property::Value from child
144 Property::Value GetPropertyValue(const TreeNode &child)
146 size_t nChildren = child.Size();
152 // cast away unused return for static analyzers
153 static_cast<void>( Dali::Toolkit::Internal::SetPropertyFromNode( child, ret ) );
155 else if(1 == nChildren)
157 // {"property": {"quaternion":[1,2,3,4]} }
158 // {"property": {"angle":22, "axis": [1,2,3]} }
160 OptionalChild quaternion = IsChild(&child, "quaternion");
161 OptionalChild axis = IsChild(&child, "axis");
162 OptionalChild angle = IsChild(&child, "angle");
166 ret = Property::Value(Quaternion(GetVector4(*quaternion)));
168 else if(axis && angle)
170 ret = Property::Value(AngleAxis(Degree(GetFloat(*angle)), GetVector3(*axis)));
173 else if(2 == nChildren)
175 // {"property": [1,2]}
176 ret = Property::Value(GetVector2(child));
178 else if(3 == nChildren)
180 // {"property": [1,2,3]}
181 ret = Property::Value(GetVector3(child));
183 else if(4 == nChildren)
185 // {"property": [1,2,3,4]}
186 ret = Property::Value(GetVector4(child));
194 * Gets Parmeter list from child
195 * params is be cleared before insertion
197 void GetParameters(const TreeNode& child, PropertyValueContainer& params)
199 if( OptionalChild c = IsChild(child, "parameters") )
201 const TreeNode& node = *c;
205 GetPropertyValue(node);
210 params.reserve(node.Size());
212 for(TreeNode::ConstIterator iter(node.CBegin()); iter != node.CEnd(); ++iter)
214 params.push_back( GetPropertyValue( (*iter).second ) );
220 void DoNothing(void) {};
223 * Get an action as boost function callback
225 boost::function<void (void)> GetAction(const TreeNode &root, const TreeNode &child, Actor actor, boost::function<void (void)> quitAction)
227 OptionalString childActorName(IsString( IsChild(&child, "child-actor")) );
228 OptionalString actorName(IsString( IsChild(&child, "actor")) );
229 OptionalString propertyName(IsString( IsChild(&child, "property")) );
230 OptionalChild valueChild( IsChild(&child, "value") );
232 OptionalString actionName = IsString( IsChild(&child, "action") );
233 DALI_ASSERT_ALWAYS(actionName && "Signal must have an action");
235 boost::function<void(void)> callback = DoNothing;
239 ChildActorAction action;
240 action.actorName = *actorName;
241 action.childAlias = *childActorName;
242 action.actionName = *actionName;
243 GetParameters(child, action.parameters);
248 if(propertyName && valueChild && ("set" == *actionName) )
250 PropertySetAction action;
251 action.actorName = *actorName;
252 action.propertyName = *propertyName;
253 // actor may not exist yet so we can't check the property type
254 if( !Dali::Toolkit::Internal::SetPropertyFromNode( *valueChild, action.value ) )
256 DALI_SCRIPT_WARNING("Cannot set property for set property action\n");
262 GenericAction action;
263 action.actorName = *actorName;
264 action.actionName = *actionName;
265 GetParameters(child, action.parameters);
269 else if("quit" == *actionName)
271 callback = quitAction;
273 else if("play" == *actionName)
275 OptionalChild animations = IsChild( root, "animations" );
276 OptionalString animationName = IsString( IsChild(child, "animation") );
277 if( animations && animationName )
279 if( OptionalChild animNode = IsChild(*animations, *animationName) )
281 DelayedAnimationPlay action;
282 action.memento = Toolkit::JsonParser::New(*animNode);
283 // @todo; put constants into the map
288 DALI_SCRIPT_WARNING("Cannot find animation '%s'\n", (*animationName).c_str());
293 DALI_SCRIPT_WARNING("Cannot find animations section\n");
298 // no named actor; presume self
299 GenericAction action;
300 action.actorName = actor.GetName();
301 action.actionName = *actionName;
302 GetParameters(child, action.parameters);
311 * Get a notification condition argument0 as 'arg0' 'value' or 'min'
313 float GetConditionArg0(const TreeNode &child)
315 OptionalFloat f = IsFloat( IsChild(child, "arg0") );
316 // allowing some human preferable alternatives
319 f = IsFloat( IsChild(child, "value") );
323 f = IsFloat( IsChild(child, "min") );
326 DALI_ASSERT_ALWAYS(f && "Notification condition for arg0 not specified");
332 * Get a notification condition argument1 as 'arg1' or 'max'
334 float GetConditionArg1(const TreeNode &child)
336 OptionalFloat f = IsFloat( IsChild(child, "arg1") );
337 // allowing some human preferable alternatives
340 f = IsFloat( IsChild(child, "max") );
343 DALI_ASSERT_ALWAYS(f && "Notification condition for arg1 not specified");
359 Actor SetupSignalAction(const TreeNode &child, Actor actor);
360 Actor SetupPropertyNotification(const TreeNode &child, Actor actor);
363 * Setup signals and actions on an actor
365 Actor SetupSignalAction(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, boost::function<void (void)> quitAction)
367 DALI_ASSERT_ALWAYS(actor);
369 if(OptionalChild signalsChild = IsChild(child, "signals"))
371 const TreeNode& signalsNode = *signalsChild;
372 const TreeConstIter endIter = signalsNode.CEnd();
373 for( TreeConstIter iter = signalsNode.CBegin(); endIter != iter; ++iter )
375 const TreeNode::KeyNodePair& key_child = *iter;
377 DALI_SCRIPT_INFO(" Creating Signal for: %s\n", actor.GetName().c_str());
379 OptionalString name( IsString( IsChild( key_child.second, "name")) );
380 DALI_ASSERT_ALWAYS(name && "Signal must have a name");
382 boost::function<void (void)> callback = GetAction(root, key_child.second, actor, quitAction);
384 actor.ConnectSignal(tracker, *name, callback);
392 * Setup Property notifications for an actor
394 Actor SetupPropertyNotification(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, boost::function<void (void)> quitAction)
396 DALI_ASSERT_ALWAYS(actor);
398 if(OptionalChild notificationsChild = IsChild(child,"notifications"))
400 const TreeNode& notificationsNode = *notificationsChild;
401 const TreeNode::ConstIterator endIter = notificationsNode.CEnd();
402 for( TreeNode::ConstIterator iter = notificationsNode.CBegin(); endIter != iter; ++iter )
404 const TreeNode::KeyNodePair& key_child = *iter;
406 // Actor actions reference by pointer because of circular reference actor->signal
407 // So this callback should only go onto the actor maintained list.
408 boost::function<void (void)> callback = GetAction(root, key_child.second, actor, quitAction);
410 OptionalString prop(IsString( IsChild(key_child.second, "property")) );
411 DALI_ASSERT_ALWAYS(prop && "Notification signal must specify a property");
413 Property::Index prop_index = actor.GetPropertyIndex(*prop);
414 DALI_ASSERT_ALWAYS(prop_index != Property::INVALID_INDEX && "Notification signal specifies an unknown property");
416 OptionalString cond(IsString( IsChild(key_child.second, "condition")));
417 DALI_ASSERT_ALWAYS(cond && "Notification signal must specify a condition");
421 PropertyNotification notification = actor.AddPropertyNotification( actor.GetPropertyIndex(*prop),
422 LessThanCondition(1.f) );
423 notification.NotifySignal().Connect( tracker, FunctorDelegate::New(callback) );
425 else if("LessThan" == *cond)
427 PropertyNotification notification = actor.AddPropertyNotification( actor.GetPropertyIndex(*prop),
428 LessThanCondition(GetConditionArg0(key_child.second)) );
429 notification.NotifySignal().Connect( tracker, FunctorDelegate::New(callback) );
431 else if("GreaterThan" == *cond)
433 PropertyNotification notification = actor.AddPropertyNotification( actor.GetPropertyIndex(*prop),
434 GreaterThanCondition(GetConditionArg0(key_child.second)) );
435 notification.NotifySignal().Connect( tracker, FunctorDelegate::New(callback) );
437 else if("Inside" == *cond)
439 PropertyNotification notification = actor.AddPropertyNotification( actor.GetPropertyIndex(*prop),
440 InsideCondition(GetConditionArg0(key_child.second),
441 GetConditionArg1(key_child.second)) );
442 notification.NotifySignal().Connect( tracker, FunctorDelegate::New(callback) );
444 else if("Outside" == *cond)
446 PropertyNotification notification = actor.AddPropertyNotification( actor.GetPropertyIndex(*prop),
447 OutsideCondition(GetConditionArg0(key_child.second),
448 GetConditionArg1(key_child.second)) );
449 notification.NotifySignal().Connect( tracker, FunctorDelegate::New(callback) );
453 DALI_ASSERT_ALWAYS(!"Unknown condition");
456 } // if notifications
460 } // AddPropertyNotification
463 } // namespace Internal
464 } // namespace Toolkit