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 std::vector<Property::Value> 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 std::vector<Property::Value> 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, std::vector<Property::Value> ¶ms)
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") );
325 DALI_ASSERT_ALWAYS(f && "Notification condition for arg0 not specified");
337 * Get a notification condition argument1 as 'arg1' or 'max'
339 float GetConditionArg1(const TreeNode &child)
341 OptionalFloat f = IsFloat( IsChild(child, "arg1") );
342 // allowing some human preferable alternatives
345 f = IsFloat( IsChild(child, "max") );
347 DALI_ASSERT_ALWAYS(f && "Notification condition for arg1 not specified");
369 Actor SetupSignalAction(const TreeNode &child, Actor actor);
370 Actor SetupPropertyNotification(const TreeNode &child, Actor actor);
373 * Setup signals and actions on an actor
375 Actor SetupSignalAction(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, boost::function<void (void)> quitAction)
377 DALI_ASSERT_ALWAYS(actor);
379 if(OptionalChild signalsChild = IsChild(child, "signals"))
381 const TreeNode& signalsNode = *signalsChild;
382 const TreeConstIter endIter = signalsNode.CEnd();
383 for( TreeConstIter iter = signalsNode.CBegin(); endIter != iter; ++iter )
385 const TreeNode::KeyNodePair& key_child = *iter;
387 DALI_SCRIPT_INFO(" Creating Signal for: %s\n", actor.GetName().c_str());
389 OptionalString name( IsString( IsChild( key_child.second, "name")) );
390 DALI_ASSERT_ALWAYS(name && "Signal must have a name");
392 boost::function<void (void)> callback = GetAction(root, key_child.second, actor, quitAction);
394 actor.ConnectSignal(tracker, *name, callback);
402 * Setup Property notifications for an actor
404 Actor SetupPropertyNotification(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, boost::function<void (void)> quitAction)
406 DALI_ASSERT_ALWAYS(actor);
408 if(OptionalChild notificationsChild = IsChild(child,"notifications"))
410 const TreeNode& notificationsNode = *notificationsChild;
411 const TreeNode::ConstIterator endIter = notificationsNode.CEnd();
412 for( TreeNode::ConstIterator iter = notificationsNode.CBegin(); endIter != iter; ++iter )
414 const TreeNode::KeyNodePair& key_child = *iter;
416 // Actor actions reference by pointer because of circular reference actor->signal
417 // So this callback should only go onto the actor maintained list.
418 boost::function<void (void)> callback = GetAction(root, key_child.second, actor, quitAction);
420 OptionalString prop(IsString( IsChild(key_child.second, "property")) );
421 DALI_ASSERT_ALWAYS(prop && "Notification signal must specify a property");
423 Property::Index prop_index = actor.GetPropertyIndex(*prop);
424 DALI_ASSERT_ALWAYS(prop_index != Property::INVALID_INDEX && "Notification signal specifies an unknown property");
426 OptionalString cond(IsString( IsChild(key_child.second, "condition")));
427 DALI_ASSERT_ALWAYS(cond && "Notification signal must specify a condition");
431 PropertyNotification notification = actor.AddPropertyNotification( actor.GetPropertyIndex(*prop),
432 LessThanCondition(1.f) );
433 notification.NotifySignal().Connect( tracker, FunctorDelegate::New(callback) );
435 else if("LessThan" == *cond)
437 PropertyNotification notification = actor.AddPropertyNotification( actor.GetPropertyIndex(*prop),
438 LessThanCondition(GetConditionArg0(key_child.second)) );
439 notification.NotifySignal().Connect( tracker, FunctorDelegate::New(callback) );
441 else if("GreaterThan" == *cond)
443 PropertyNotification notification = actor.AddPropertyNotification( actor.GetPropertyIndex(*prop),
444 GreaterThanCondition(GetConditionArg0(key_child.second)) );
445 notification.NotifySignal().Connect( tracker, FunctorDelegate::New(callback) );
447 else if("Inside" == *cond)
449 PropertyNotification notification = actor.AddPropertyNotification( actor.GetPropertyIndex(*prop),
450 InsideCondition(GetConditionArg0(key_child.second),
451 GetConditionArg1(key_child.second)) );
452 notification.NotifySignal().Connect( tracker, FunctorDelegate::New(callback) );
454 else if("Outside" == *cond)
456 PropertyNotification notification = actor.AddPropertyNotification( actor.GetPropertyIndex(*prop),
457 OutsideCondition(GetConditionArg0(key_child.second),
458 GetConditionArg1(key_child.second)) );
459 notification.NotifySignal().Connect( tracker, FunctorDelegate::New(callback) );
463 DALI_ASSERT_ALWAYS(!"Unknown condition");
466 } // if notifications
470 } // AddPropertyNotification
473 } // namespace Internal
474 } // namespace Toolkit