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 quit; connected to signals
47 // TODO: MOVE TO BUILDER TEMPLATE
52 void operator()(void) {
53 // Dali::Application::Get().Quit();
57 // Action on child actor. The child is found by alias so can be 'previous' etc.
58 struct ChildActorAction
60 std::string actorName;
61 std::string actionName;
62 std::string childAlias;
63 std::vector<Property::Value> parameters;
67 Actor actor = Stage::GetCurrent().GetRootLayer().FindChildByName(actorName);
71 Actor child_actor = actor.FindChildByAlias(childAlias);
75 child_actor.DoAction(actionName, parameters);
79 DALI_SCRIPT_WARNING("Could not find child by alias '%s'\n", childAlias.c_str());
85 // Action to set a property
86 struct PropertySetAction
88 std::string actorName;
89 std::string propertyName;
90 Property::Value value;
94 Actor actor = Stage::GetCurrent().GetRootLayer().FindChildByName(actorName);
98 Property::Index idx = actor.GetPropertyIndex(propertyName);
100 if( idx != Property::INVALID_INDEX )
102 if( actor.GetPropertyType(idx) != value.GetType() )
104 DALI_SCRIPT_WARNING("Set property action has different type for property '%s'\n", propertyName.c_str());
108 actor.SetProperty( idx, value );
113 DALI_SCRIPT_WARNING("Set property action cannot find property '%s'\n", propertyName.c_str());
119 // Generic action on a handle (Animation & Actor)
122 std::string actorName;
123 std::string actionName;
124 std::vector<Property::Value> parameters;
126 void operator()(void)
128 Actor actor = Stage::GetCurrent().GetRootLayer().FindChildByName(actorName);
131 actor.DoAction(actionName, parameters);
137 // Delay an animation play; ie wait as its not on stage yet
138 struct DelayedAnimationPlay
140 Toolkit::JsonParser memento;
142 void operator()(void)
144 Animation anim = Toolkit::Internal::CreateAnimation(*memento.GetRoot());
153 * Gets Property::Value from child
155 Property::Value GetPropertyValue(const TreeNode &child)
157 size_t nChildren = child.Size();
163 // cast away unused return for static analyzers
164 static_cast<void>( Dali::Toolkit::Internal::SetPropertyFromNode( child, ret ) );
166 else if(1 == nChildren)
168 // {"property": {"quaternion":[1,2,3,4]} }
169 // {"property": {"angle":22, "axis": [1,2,3]} }
171 OptionalChild quaternion = IsChild(&child, "quaternion");
172 OptionalChild axis = IsChild(&child, "axis");
173 OptionalChild angle = IsChild(&child, "angle");
177 ret = Property::Value(Quaternion(GetVector4(*quaternion)));
179 else if(axis && angle)
181 ret = Property::Value(AngleAxis(Degree(GetFloat(*angle)), GetVector3(*axis)));
184 else if(2 == nChildren)
186 // {"property": [1,2]}
187 ret = Property::Value(GetVector2(child));
189 else if(3 == nChildren)
191 // {"property": [1,2,3]}
192 ret = Property::Value(GetVector3(child));
194 else if(4 == nChildren)
196 // {"property": [1,2,3,4]}
197 ret = Property::Value(GetVector4(child));
205 * Gets Parmeter list from child
206 * params is be cleared before insertion
208 void GetParameters(const TreeNode &child, std::vector<Property::Value> ¶ms)
210 if( OptionalChild c = IsChild(child, "parameters") )
212 const TreeNode& node = *c;
216 GetPropertyValue(node);
221 params.reserve(node.Size());
223 for(TreeNode::ConstIterator iter(node.CBegin()); iter != node.CEnd(); ++iter)
225 params.push_back( GetPropertyValue( (*iter).second ) );
231 void DoNothing(void) {};
234 * Get an action as boost function callback
236 boost::function<void (void)> GetAction(const TreeNode &root, const TreeNode &child, Actor actor)
238 OptionalString childActorName(IsString( IsChild(&child, "child-actor")) );
239 OptionalString actorName(IsString( IsChild(&child, "actor")) );
240 OptionalString propertyName(IsString( IsChild(&child, "property")) );
241 OptionalChild valueChild( IsChild(&child, "value") );
243 OptionalString actionName = IsString( IsChild(&child, "action") );
244 DALI_ASSERT_ALWAYS(actionName && "Signal must have an action");
246 boost::function<void(void)> callback = DoNothing;
250 ChildActorAction action;
251 action.actorName = *actorName;
252 action.childAlias = *childActorName;
253 action.actionName = *actionName;
254 GetParameters(child, action.parameters);
259 if(propertyName && valueChild && ("set" == *actionName) )
261 PropertySetAction action;
262 action.actorName = *actorName;
263 action.propertyName = *propertyName;
264 // actor may not exist yet so we can't check the property type
265 if( !Dali::Toolkit::Internal::SetPropertyFromNode( *valueChild, action.value ) )
267 DALI_SCRIPT_WARNING("Cannot set property for set property action\n");
273 GenericAction action;
274 action.actorName = *actorName;
275 action.actionName = *actionName;
276 GetParameters(child, action.parameters);
280 else if("quit" == *actionName)
282 callback = ActionQuit();
284 else if("play" == *actionName)
286 OptionalChild animations = IsChild( root, "animations" );
287 OptionalString animationName = IsString( IsChild(child, "animation") );
288 if( animations && animationName )
290 if( OptionalChild animNode = IsChild(*animations, *animationName) )
292 DelayedAnimationPlay action;
293 action.memento = Toolkit::JsonParser::New(*animNode);
294 // @todo; put constants into the map
299 DALI_SCRIPT_WARNING("Cannot find animation '%s'\n", (*animationName).c_str());
304 DALI_SCRIPT_WARNING("Cannot find animations section\n");
309 // no named actor; presume self
310 GenericAction action;
311 action.actorName = actor.GetName();
312 action.actionName = *actionName;
313 GetParameters(child, action.parameters);
322 * Get a notification condition argument0 as 'arg0' 'value' or 'min'
324 float GetConditionArg0(const TreeNode &child)
326 OptionalFloat f = IsFloat( IsChild(child, "arg0") );
327 // allowing some human preferable alternatives
330 f = IsFloat( IsChild(child, "value") );
334 f = IsFloat( IsChild(child, "min") );
336 DALI_ASSERT_ALWAYS(f && "Notification condition for arg0 not specified");
348 * Get a notification condition argument1 as 'arg1' or 'max'
350 float GetConditionArg1(const TreeNode &child)
352 OptionalFloat f = IsFloat( IsChild(child, "arg1") );
353 // allowing some human preferable alternatives
356 f = IsFloat( IsChild(child, "max") );
358 DALI_ASSERT_ALWAYS(f && "Notification condition for arg1 not specified");
380 Actor SetupSignalAction(const TreeNode &child, Actor actor);
381 Actor SetupPropertyNotification(const TreeNode &child, Actor actor);
384 * Setup signals and actions on an actor
386 Actor SetupSignalAction(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor)
388 DALI_ASSERT_ALWAYS(actor);
390 if(OptionalChild signalsChild = IsChild(child, "signals"))
392 const TreeNode& signalsNode = *signalsChild;
393 const TreeConstIter endIter = signalsNode.CEnd();
394 for( TreeConstIter iter = signalsNode.CBegin(); endIter != iter; ++iter )
396 const TreeNode::KeyNodePair& key_child = *iter;
398 DALI_SCRIPT_INFO(" Creating Signal for: %s\n", actor.GetName().c_str());
400 OptionalString name( IsString( IsChild( key_child.second, "name")) );
401 DALI_ASSERT_ALWAYS(name && "Signal must have a name");
403 boost::function<void (void)> callback = GetAction(root, key_child.second, actor);
405 actor.ConnectSignal(tracker, *name, callback);
413 * Setup Property notifications for an actor
415 Actor SetupPropertyNotification(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor)
417 DALI_ASSERT_ALWAYS(actor);
419 if(OptionalChild notificationsChild = IsChild(child,"notifications"))
421 const TreeNode& notificationsNode = *notificationsChild;
422 const TreeNode::ConstIterator endIter = notificationsNode.CEnd();
423 for( TreeNode::ConstIterator iter = notificationsNode.CBegin(); endIter != iter; ++iter )
425 const TreeNode::KeyNodePair& key_child = *iter;
427 // Actor actions reference by pointer because of circular reference actor->signal
428 // So this callback should only go onto the actor maintained list.
429 boost::function<void (void)> callback = GetAction(root, key_child.second, actor);
431 OptionalString prop(IsString( IsChild(key_child.second, "property")) );
432 DALI_ASSERT_ALWAYS(prop && "Notification signal must specify a property");
434 Property::Index prop_index = actor.GetPropertyIndex(*prop);
435 DALI_ASSERT_ALWAYS(prop_index != Property::INVALID_INDEX && "Notification signal specifies an unknown property");
437 OptionalString cond(IsString( IsChild(key_child.second, "condition")));
438 DALI_ASSERT_ALWAYS(cond && "Notification signal must specify a condition");
442 PropertyNotification notification = actor.AddPropertyNotification( actor.GetPropertyIndex(*prop),
443 LessThanCondition(1.f) );
444 notification.NotifySignal().Connect( tracker, FunctorDelegate::New(callback) );
446 else if("LessThan" == *cond)
448 PropertyNotification notification = actor.AddPropertyNotification( actor.GetPropertyIndex(*prop),
449 LessThanCondition(GetConditionArg0(key_child.second)) );
450 notification.NotifySignal().Connect( tracker, FunctorDelegate::New(callback) );
452 else if("GreaterThan" == *cond)
454 PropertyNotification notification = actor.AddPropertyNotification( actor.GetPropertyIndex(*prop),
455 GreaterThanCondition(GetConditionArg0(key_child.second)) );
456 notification.NotifySignal().Connect( tracker, FunctorDelegate::New(callback) );
458 else if("Inside" == *cond)
460 PropertyNotification notification = actor.AddPropertyNotification( actor.GetPropertyIndex(*prop),
461 InsideCondition(GetConditionArg0(key_child.second),
462 GetConditionArg1(key_child.second)) );
463 notification.NotifySignal().Connect( tracker, FunctorDelegate::New(callback) );
465 else if("Outside" == *cond)
467 PropertyNotification notification = actor.AddPropertyNotification( actor.GetPropertyIndex(*prop),
468 OutsideCondition(GetConditionArg0(key_child.second),
469 GetConditionArg1(key_child.second)) );
470 notification.NotifySignal().Connect( tracker, FunctorDelegate::New(callback) );
474 DALI_ASSERT_ALWAYS(!"Unknown condition");
477 } // if notifications
481 } // AddPropertyNotification
484 } // namespace Internal
485 } // namespace Toolkit