2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
18 #include <dali/integration-api/debug.h>
21 #include <dali-toolkit/internal/builder/builder-impl.h>
22 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
30 extern Animation CreateAnimation( const TreeNode& child );
31 extern bool SetPropertyFromNode( const TreeNode& node, Property::Value& value );
44 // Action quit; connected to signals
45 // TODO: MOVE TO BUILDER TEMPLATE
50 void operator()(void) {
51 // Dali::Application::Get().Quit();
55 // Action on child actor. The child is found by alias so can be 'previous' etc.
56 struct ChildActorAction
58 std::string actorName;
59 std::string actionName;
60 std::string childAlias;
61 std::vector<Property::Value> parameters;
65 Actor actor = Stage::GetCurrent().GetRootLayer().FindChildByName(actorName);
69 Actor child_actor = actor.FindChildByAlias(childAlias);
73 child_actor.DoAction(actionName, parameters);
77 DALI_SCRIPT_WARNING("Could not find child by alias '%s'\n", childAlias.c_str());
83 // Action to set a property
84 struct PropertySetAction
86 std::string actorName;
87 std::string propertyName;
88 Property::Value value;
92 Actor actor = Stage::GetCurrent().GetRootLayer().FindChildByName(actorName);
96 Property::Index idx = actor.GetPropertyIndex(propertyName);
98 if( idx != Property::INVALID_INDEX )
100 actor.SetProperty( idx, value );
106 // Generic action on a handle (Animation & Actor)
109 std::string actorName;
110 std::string actionName;
111 std::vector<Property::Value> parameters;
113 void operator()(void)
115 Actor actor = Stage::GetCurrent().GetRootLayer().FindChildByName(actorName);
118 actor.DoAction(actionName, parameters);
124 // Delay an animation play; ie wait as its not on stage yet
125 struct DelayedAnimationPlay
127 Toolkit::JsonParser memento;
129 void operator()(void)
131 Animation anim = Toolkit::Internal::CreateAnimation(*memento.GetRoot());
140 * Gets Property::Value from child
142 Property::Value GetPropertyValue(const TreeNode &child)
144 size_t nChildren = child.Size();
150 // cast away unused return for static analyzers
151 static_cast<void>( Dali::Toolkit::Internal::SetPropertyFromNode( child, ret ) );
153 else if(1 == nChildren)
155 // {"property": {"quaternion":[1,2,3,4]} }
156 // {"property": {"angle":22, "axis": [1,2,3]} }
158 OptionalChild quaternion = IsChild(&child, "quaternion");
159 OptionalChild axis = IsChild(&child, "axis");
160 OptionalChild angle = IsChild(&child, "angle");
164 ret = Property::Value(Quaternion(GetVector4(*quaternion)));
166 else if(axis && angle)
168 ret = Property::Value(AngleAxis(Degree(GetFloat(*angle)), GetVector3(*axis)));
171 else if(2 == nChildren)
173 // {"property": [1,2]}
174 ret = Property::Value(GetVector2(child));
176 else if(3 == nChildren)
178 // {"property": [1,2,3]}
179 ret = Property::Value(GetVector3(child));
181 else if(4 == nChildren)
183 // {"property": [1,2,3,4]}
184 ret = Property::Value(GetVector4(child));
192 * Gets Parmeter list from child
193 * params is be cleared before insertion
195 void GetParameters(const TreeNode &child, std::vector<Property::Value> ¶ms)
197 if( OptionalChild c = IsChild(child, "parameters") )
199 const TreeNode& node = *c;
203 GetPropertyValue(node);
208 params.reserve(node.Size());
210 for(TreeNode::ConstIterator iter(node.CBegin()); iter != node.CEnd(); ++iter)
212 params.push_back( GetPropertyValue( (*iter).second ) );
218 void DoNothing(void) {};
221 * Get an action as boost function callback
223 boost::function<void (void)> GetAction(const TreeNode &root, const TreeNode &child, Actor actor)
225 OptionalString childActorName(IsString( IsChild(&child, "child-actor")) );
226 OptionalString actorName(IsString( IsChild(&child, "actor")) );
227 OptionalString propertyName(IsString( IsChild(&child, "property")) );
228 OptionalString valueChild(IsString( IsChild(&child, "value")) );
230 OptionalString actionName = IsString( IsChild(&child, "action") );
231 DALI_ASSERT_ALWAYS(actionName && "Signal must have an action");
233 boost::function<void(void)> callback = DoNothing;
237 ChildActorAction action;
238 action.actorName = *actorName;
239 action.childAlias = *childActorName;
240 action.actionName = *actionName;
241 GetParameters(child, action.parameters);
246 if(propertyName && valueChild)
248 PropertySetAction action;
249 action.actorName = *actorName;
250 action.propertyName = *propertyName;
255 GenericAction action;
256 action.actorName = *actorName;
257 action.actionName = *actionName;
258 GetParameters(child, action.parameters);
262 else if("quit" == *actionName)
264 callback = ActionQuit();
266 else if("play" == *actionName)
268 OptionalChild animations = IsChild( root, "animations" );
269 OptionalString animationName = IsString( IsChild(child, "animation") );
270 if( animations && animationName )
272 if( OptionalChild animNode = IsChild(*animations, *animationName) )
274 DelayedAnimationPlay action;
275 action.memento = Toolkit::JsonParser::New(*animNode);
280 DALI_SCRIPT_WARNING("Cannot find animation '%s'\n", (*animationName).c_str());
285 DALI_SCRIPT_WARNING("Cannot find animations section\n");
290 // no named actor; presume self
291 GenericAction action;
292 action.actorName = actor.GetName();
293 action.actionName = *actionName;
294 GetParameters(child, action.parameters);
303 * Get a notification condition argument0 as 'arg0' 'value' or 'min'
305 float GetConditionArg0(const TreeNode &child)
307 OptionalFloat f = IsFloat( IsChild(child, "arg0") );
308 // allowing some human preferable alternatives
311 f = IsFloat( IsChild(child, "value") );
315 f = IsFloat( IsChild(child, "min") );
317 DALI_ASSERT_ALWAYS(f && "Notification condition for arg0 not specified");
329 * Get a notification condition argument1 as 'arg1' or 'max'
331 float GetConditionArg1(const TreeNode &child)
333 OptionalFloat f = IsFloat( IsChild(child, "arg1") );
334 // allowing some human preferable alternatives
337 f = IsFloat( IsChild(child, "max") );
339 DALI_ASSERT_ALWAYS(f && "Notification condition for arg1 not specified");
361 Actor SetupSignalAction(const TreeNode &child, Actor actor);
362 Actor SetupPropertyNotification(const TreeNode &child, Actor actor);
365 * Setup signals and actions on an actor
367 Actor SetupSignalAction(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor)
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);
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)
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);
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