Remove dali-any from Property::Value
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / builder / builder-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/builder/builder-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <sys/stat.h>
23 #include <sstream>
24 #include <dali/public-api/render-tasks/render-task-list.h>
25 #include <dali/public-api/object/type-info.h>
26 #include <dali/public-api/object/type-registry.h>
27 #include <dali/public-api/object/property-array.h>
28 #include <dali/public-api/actors/layer.h>
29 #include <dali/public-api/actors/image-actor.h>
30 #include <dali/public-api/actors/camera-actor.h>
31 #include <dali/devel-api/scripting/scripting.h>
32 #include <dali/public-api/signals/functor-delegate.h>
33 #include <dali/integration-api/debug.h>
34
35 // INTERNAL INCLUDES
36 #include <dali-toolkit/public-api/controls/control.h>
37 #include <dali-toolkit/devel-api/builder/json-parser.h>
38
39 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
40 #include <dali-toolkit/internal/builder/builder-filesystem.h>
41 #include <dali-toolkit/internal/builder/builder-declarations.h>
42 #include <dali-toolkit/internal/builder/replacement.h>
43
44 namespace Dali
45 {
46
47 namespace Toolkit
48 {
49
50 namespace Internal
51 {
52 class Replacement;
53
54 extern Animation CreateAnimation(const TreeNode& child, const Replacement& replacements, const Dali::Actor searchRoot, Builder* const builder );
55 extern bool SetPropertyFromNode( const TreeNode& node, Property::Value& value );
56 extern bool SetPropertyFromNode( const TreeNode& node, Property::Value& value, const Replacement& replacements );
57 extern bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value );
58 extern bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value, const Replacement& replacements );
59 extern Actor SetupSignalAction(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, Dali::Toolkit::Internal::Builder* const builder);
60 extern Actor SetupPropertyNotification(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, Dali::Toolkit::Internal::Builder* const builder);
61 extern Actor SetupActor( const TreeNode& node, Actor& actor, const Replacement& constant );
62
63 #if defined(DEBUG_ENABLED)
64 Integration::Log::Filter* gFilterScript  = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_SCRIPT");
65 #endif
66
67 namespace
68 {
69
70 const std::string KEYNAME_STYLES    = "styles";
71 const std::string KEYNAME_TYPE      = "type";
72 const std::string KEYNAME_ACTORS    = "actors";
73 const std::string KEYNAME_SIGNALS   = "signals";
74 const std::string KEYNAME_NAME      = "name";
75 const std::string KEYNAME_TEMPLATES = "templates";
76 const std::string KEYNAME_INCLUDES  = "includes";
77
78 typedef std::vector<const TreeNode*> TreeNodeList;
79
80 template <typename T>
81 std::string ToString(const T& value)
82 {
83   std::stringstream ss;
84   ss << value;
85   return ss.str();
86 }
87
88 template <>
89 std::string ToString(const Rect<int>& value)
90 {
91   std::stringstream ss;
92   ss << value.x << "," << value.y << "," << value.width << "," << value.height;
93   return ss.str();
94 }
95
96 #if defined(DEBUG_ENABLED)
97
98 std::string PropertyValueToString( const Property::Value& value )
99 {
100   std::string ret;
101
102   switch( value.GetType() )
103   {
104     case Property::NONE:
105     {
106       ret = "NONE";
107       break;
108     }            ///< No type
109     case Property::BOOLEAN:
110     {
111       ret = value.Get<bool>() ? "True" : "False";
112       break;
113     }
114     case Property::FLOAT:
115     {
116
117       ret = ToString( value.Get<float>() );
118       break;
119     }
120     case Property::INTEGER:
121     {
122       ret = ToString( value.Get<int>() );
123       break;
124     }
125     case Property::UNSIGNED_INTEGER:
126     {
127       ret = ToString( value.Get<unsigned int>() );
128       break;
129     }
130     case Property::VECTOR2:
131     {
132       ret = ToString( value.Get<Vector2>() );
133       break;
134     }
135     case Property::VECTOR3:
136     {
137       ret = ToString( value.Get<Vector3>() );
138       break;
139     }
140     case Property::VECTOR4:
141     {
142       ret = ToString( value.Get<Vector4>() );
143       break;
144     }
145     case Property::MATRIX3:
146     {
147       ret = ToString( value.Get<Matrix3>() );
148       break;
149     }
150     case Property::MATRIX:
151     {
152       ret = ToString( value.Get<Matrix>() );
153       break;
154     }
155     case Property::RECTANGLE:
156     {
157       ret = ToString( value.Get< Rect<int> >() );
158       break;
159     }
160     case Property::ROTATION:
161     {
162       break;
163     }
164     case Property::STRING:
165     {
166       ret = value.Get<std::string>();
167       break;
168     }
169     case Property::ARRAY:
170     {
171       ret = std::string("Array Size=") + ToString( value.Get<Property::Array>().Size() );
172       break;
173     }
174     case Property::MAP:
175     {
176       ret = std::string("Map Size=") + ToString( value.Get<Property::Map>().Count() );
177       break;
178     }
179     case Property::TYPE_COUNT:
180     {
181       ret = "";
182       break;
183     }
184   }
185
186   return ret;
187 }
188 #endif // DEBUG_ENABLED
189
190 /*
191  * Recursively collects all stylesin a node (An array of style names).
192  *
193  * stylesCollection The set of styles from the json file (a json object of named styles)
194  * style The style array to begin the collection from
195  * styleList The style list to add nodes to apply
196  */
197 void CollectAllStyles( const TreeNode& stylesCollection, const TreeNode& style, TreeNodeList& styleList )
198 {
199   // style is an array of style names
200   if( TreeNode::ARRAY == style.GetType() )
201   {
202     for(TreeNode::ConstIterator iter = style.CBegin(); iter != style.CEnd(); ++iter)
203     {
204       if( OptionalString styleName = IsString( (*iter).second ) )
205       {
206         if( OptionalChild node = IsChild( stylesCollection, *styleName) )
207         {
208           styleList.push_back( &(*node) );
209
210           if( OptionalChild subStyle = IsChild( *node, KEYNAME_STYLES ) )
211           {
212             CollectAllStyles( stylesCollection, *subStyle, styleList );
213           }
214         }
215       }
216     }
217   }
218 }
219
220
221 } // namespace anon
222
223 /*
224  * Sets the handle properties found in the tree node
225  */
226 void Builder::SetProperties( const TreeNode& node, Handle& handle, const Replacement& constant )
227 {
228   if( handle )
229   {
230     for( TreeNode::ConstIterator iter = node.CBegin(); iter != node.CEnd(); ++iter )
231     {
232       const TreeNode::KeyNodePair& keyChild = *iter;
233
234       std::string key( keyChild.first );
235
236       // ignore special fields; type,actors,signals,styles
237       if(key == KEYNAME_TYPE || key == KEYNAME_ACTORS || key == KEYNAME_SIGNALS || key == KEYNAME_STYLES)
238       {
239         continue;
240       }
241
242       // special field 'image' usually contains an json object description
243       // although sometimes refers to a framebuffer
244       if( 0 == keyChild.second.Size() )
245       {
246         if(key == "image")
247         {
248           ImageActor imageActor = ImageActor::DownCast(handle);
249           if(imageActor)
250           {
251             if( OptionalString s = constant.IsString( keyChild.second ) )
252             {
253               FrameBufferImage fb = GetFrameBufferImage(*s, constant);
254               if(fb)
255               {
256                 imageActor.SetImage( fb );
257               }
258             }
259           }
260         }
261       }
262
263       // special field 'effect' references the shader effect instances
264       if(key == "effect")
265       {
266         RenderableActor actor = RenderableActor::DownCast(handle);
267         if( actor )
268         {
269           OptionalString str = constant.IsString( keyChild.second );
270           if( str )
271           {
272             ShaderEffect effect = GetShaderEffect( *str, constant );
273             actor.SetShaderEffect(effect);
274           }
275         }
276         else
277         {
278           DALI_SCRIPT_WARNING("Could not find or set shader effect\n");
279         }
280
281         continue;
282       }
283
284       Handle propertyObject( handle );
285
286       Dali::Property::Index index = propertyObject.GetPropertyIndex( key );
287
288       if( Property::INVALID_INDEX == index )
289       {
290         RenderableActor actor = RenderableActor::DownCast(handle);
291         if( actor )
292         {
293           if( ShaderEffect effect = actor.GetShaderEffect() )
294           {
295             index = effect.GetPropertyIndex( key );
296             if(index != Property::INVALID_INDEX)
297             {
298               propertyObject = effect;
299             }
300           }
301         }
302       }
303
304       if( Property::INVALID_INDEX != index )
305       {
306         Property::Type type = propertyObject.GetPropertyType(index);
307
308         Property::Value value;
309         if( !SetPropertyFromNode( keyChild.second, type, value, constant ) )
310         {
311           // verbose as this might not be a problem
312           // eg parent-origin can be a string which is picked up later
313           DALI_SCRIPT_VERBOSE("Could not convert property:%s\n", key.c_str());
314         }
315         else
316         {
317           DALI_SCRIPT_VERBOSE("SetProperty '%s' Index=:%d Value Type=%d Value '%s'\n", key.c_str(), index, value.GetType(), PropertyValueToString(value).c_str() );
318
319           propertyObject.SetProperty( index, value );
320         }
321       }
322       else
323       {
324         DALI_SCRIPT_VERBOSE("SetProperty INVALID '%s' Index=:%d\n", key.c_str(), index);
325       }
326
327     } // for property nodes
328   }
329   else
330   {
331     DALI_SCRIPT_WARNING("Style applied to empty handle\n");
332   }
333 }
334
335 // Set properties from node on handle.
336 void Builder::ApplyProperties( const TreeNode& root, const TreeNode& node,
337                                Dali::Handle& handle, const Replacement& constant )
338 {
339   if( Actor actor = Actor::DownCast(handle) )
340   {
341     SetProperties( node, actor, constant );
342
343     if( actor )
344     {
345       SetupActor( node, actor, constant );
346
347       // add signals
348       SetupSignalAction( mSlotDelegate.GetConnectionTracker(), root, node, actor, this );
349       SetupPropertyNotification( mSlotDelegate.GetConnectionTracker(), root, node, actor, this );
350    }
351   }
352   else
353   {
354     SetProperties( node, handle, constant );
355   }
356 }
357
358 // Appling by style helper
359 // use FindChildByName() to apply properties referenced in KEYNAME_ACTORS in the node
360 void Builder::ApplyStylesByActor(  const TreeNode& root, const TreeNode& node,
361                                    Dali::Handle& handle, const Replacement& constant )
362 {
363   if( Dali::Actor actor = Dali::Actor::DownCast( handle ) )
364   {
365     if( const TreeNode* actors = node.GetChild( KEYNAME_ACTORS ) )
366     {
367       // in a style the actor subtree properties referenced by actor name
368       for( TreeConstIter iter = actors->CBegin(); iter != actors->CEnd(); ++iter )
369       {
370         Dali::Actor foundActor;
371
372         if( (*iter).first )
373         {
374           foundActor = actor.FindChildByName( (*iter).first );
375         }
376
377         if( !foundActor )
378         {
379           // debug log cannot find searched for actor
380 #if defined(DEBUG_ENABLED)
381           DALI_SCRIPT_VERBOSE("Cannot find actor in style application '%s'\n", (*iter).first);
382 #endif
383         }
384         else
385         {
386 #if defined(DEBUG_ENABLED)
387           DALI_SCRIPT_VERBOSE("Styles applied to actor '%s'\n", (*iter).first);
388 #endif
389           ApplyProperties( root, (*iter).second, foundActor, constant );
390         }
391       }
392     }
393   }
394 }
395
396
397 void Builder::ApplyAllStyleProperties( const TreeNode& root, const TreeNode& node,
398                                        Dali::Handle& handle, const Replacement& constant )
399 {
400   OptionalChild styles = IsChild(root, KEYNAME_STYLES);
401   OptionalChild style  = IsChild(node, KEYNAME_STYLES);
402
403   if( styles && style )
404   {
405     TreeNodeList additionalStyles;
406
407     CollectAllStyles( *styles, *style, additionalStyles );
408
409 #if defined(DEBUG_ENABLED)
410     for(TreeNode::ConstIterator iter = (*style).CBegin(); iter != (*style).CEnd(); ++iter)
411     {
412       if( OptionalString styleName = IsString( (*iter).second ) )
413       {
414         DALI_SCRIPT_VERBOSE("Style Applied '%s'\n", (*styleName).c_str());
415       }
416     }
417 #endif
418
419     // a style may have other styles, which has other styles etc so we apply in reverse by convention.
420     for(TreeNodeList::reverse_iterator iter = additionalStyles.rbegin(); iter != additionalStyles.rend(); ++iter)
421     {
422       ApplyProperties( root, *(*iter), handle, constant );
423
424       ApplyStylesByActor( root, *(*iter), handle, constant );
425     }
426   }
427
428   // applying given node last
429   ApplyProperties( root, node, handle, constant );
430
431   ApplyStylesByActor( root, node, handle, constant );
432
433 }
434
435
436 /*
437  * Create a dali type from a node.
438  * If parent given and an actor type was created then add it to the parent and
439  * recursively add nodes children.
440  */
441 BaseHandle Builder::DoCreate( const TreeNode& root, const TreeNode& node,
442                               Actor parent, const Replacement& replacements )
443 {
444   BaseHandle baseHandle;
445   TypeInfo typeInfo;
446   const TreeNode* templateNode = NULL;
447
448   if( OptionalString typeName = IsString(node, KEYNAME_TYPE) )
449   {
450     typeInfo = TypeRegistry::Get().GetTypeInfo( *typeName );
451
452     if( !typeInfo )
453     {
454       // a template name is also allowed inplace of the type name
455       OptionalChild templates = IsChild( root, KEYNAME_TEMPLATES);
456
457       if( templates )
458       {
459         if( OptionalChild isTemplate = IsChild( *templates, *typeName ) )
460         {
461           templateNode = &(*isTemplate);
462
463           if( OptionalString templateTypeName = IsString(*templateNode, KEYNAME_TYPE) )
464           {
465             typeInfo = TypeRegistry::Get().GetTypeInfo( *templateTypeName );
466           }
467         }
468       }
469     }
470   }
471
472   if(!typeInfo)
473   {
474     DALI_SCRIPT_WARNING("Cannot create Dali type from node '%s'\n", node.GetName());
475   }
476   else
477   {
478     baseHandle       = typeInfo.CreateInstance();
479     Handle handle    = Handle::DownCast(baseHandle);
480     Actor actor      = Actor::DownCast(handle);
481
482     if(handle)
483     {
484
485       DALI_SCRIPT_VERBOSE("Create:%s\n", typeInfo.GetName().c_str());
486
487 #if defined(DEBUG_ENABLED)
488       if(handle)
489       {
490         DALI_SCRIPT_VERBOSE("  Is Handle Object=%d\n", (long*)handle.GetObjectPtr());
491         DALI_SCRIPT_VERBOSE("  Is Handle Property Count=%d\n", handle.GetPropertyCount());
492       }
493
494       if(actor)
495       {
496         DALI_SCRIPT_VERBOSE("  Is Actor id=%d\n", actor.GetId());
497       }
498
499       Toolkit::Control control  = Toolkit::Control::DownCast(handle);
500       if(control)
501       {
502         DALI_SCRIPT_VERBOSE("  Is Control id=%d\n", actor.GetId());
503       }
504 #endif // DEBUG_ENABLED
505
506       if( templateNode )
507       {
508         ApplyProperties( root, *templateNode, handle, replacements );
509
510         if( OptionalChild actors = IsChild( *templateNode, KEYNAME_ACTORS ) )
511         {
512           for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
513           {
514             DoCreate( root, (*iter).second, actor, replacements );
515           }
516         }
517       }
518
519       if( actor )
520       {
521         // add children of all the styles
522         if( OptionalChild actors = IsChild( node, KEYNAME_ACTORS ) )
523         {
524           for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
525           {
526             DoCreate( root, (*iter).second, actor, replacements );
527           }
528         }
529
530         // apply style on top as they need the children to exist
531         ApplyAllStyleProperties( root, node, actor, replacements );
532
533         // then add to parent
534         if( parent )
535         {
536           parent.Add( actor );
537         }
538       }
539       else
540       {
541         ApplyProperties( root, node, handle, replacements );
542       }
543     }
544     else
545     {
546       DALI_SCRIPT_WARNING("Cannot create handle from type '%s'\n", typeInfo.GetName().c_str());
547     }
548   }
549
550   return baseHandle;
551 }
552
553 void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replacement& constant )
554 {
555   const Stage& stage = Stage::GetCurrent();
556   Layer root  = stage.GetRootLayer();
557
558   if( OptionalString s = constant.IsString( IsChild(node, "source-actor") ) )
559   {
560     Actor actor = root.FindChildByName(*s);
561     if(actor)
562     {
563       task.SetSourceActor( actor );
564     }
565     else
566     {
567       DALI_SCRIPT_WARNING("Cannot find source actor on stage for render task called '%s'\n", (*s).c_str() );
568     }
569   }
570
571   if( OptionalString s = constant.IsString( IsChild(node, "camera-actor") ) )
572   {
573     CameraActor actor = CameraActor::DownCast( root.FindChildByName(*s) );
574     if(actor)
575     {
576       task.SetCameraActor( actor );
577     }
578     else
579     {
580       DALI_SCRIPT_WARNING("Cannot find camera actor on stage for render task called '%s'\n", (*s).c_str() );
581     }
582   }
583
584   if( OptionalString s = constant.IsString( IsChild(node, "target-frame-buffer") ) )
585   {
586     FrameBufferImage fb = GetFrameBufferImage( *s, constant );
587     if(fb)
588     {
589       task.SetTargetFrameBuffer( fb );
590     }
591     else
592     {
593       DALI_SCRIPT_WARNING("Cannot find target frame buffer '%s'\n", (*s).c_str() );
594     }
595   }
596
597   if( OptionalString s = constant.IsString( IsChild(node, "screen-to-frame-buffer-function") ) )
598   {
599     if("DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION" == *s)
600     {
601       task.SetScreenToFrameBufferFunction( RenderTask::DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION );
602     }
603     else if("FULLSCREEN_FRAMEBUFFER_FUNCTION" == *s)
604     {
605       task.SetScreenToFrameBufferFunction( RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION );
606     }
607     else
608     {
609       DALI_SCRIPT_WARNING("todo");
610     }
611   }
612
613   // other setup is via the property system
614   SetProperties( node, task, constant ); // @ todo, remove 'source-actor', 'camera-actor'?
615
616 }
617
618 void Builder::CreateRenderTask( const std::string &name )
619 {
620   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
621
622   Replacement constant(mReplacementMap);
623
624   const Stage& stage = Stage::GetCurrent();
625
626   OptionalChild tasks = IsChild(*mParser.GetRoot(), "render-tasks");
627
628   if(tasks)
629   {
630     //
631     // Create the tasks from the current task as generally we want
632     // to setup task zero and onwards. Although this does overwrite
633     // the properties of the current task.
634     //
635     if( OptionalChild renderTask = IsChild(*tasks, name ) )
636     {
637       RenderTaskList list = stage.GetRenderTaskList();
638       unsigned int start = list.GetTaskCount();
639
640       RenderTask task;
641       if(0 == start)
642       {
643         // zero should have already been created by the stage so really
644         // this case should never happen
645         task = list.CreateTask();
646         start++;
647       }
648
649       TreeNode::ConstIterator iter = (*renderTask).CBegin();
650       task = list.GetTask( start - 1 );
651
652       SetupTask( task, (*iter).second, constant  );
653
654       ++iter;
655
656       for(; iter != (*renderTask).CEnd(); ++iter )
657       {
658         task = list.CreateTask();
659         SetupTask( task, (*iter).second, constant );
660       }
661     }
662   }
663 }
664
665 ShaderEffect Builder::GetShaderEffect( const std::string &name)
666 {
667   Replacement constant( mReplacementMap );
668   return GetShaderEffect( name, constant );
669 }
670
671 ShaderEffect Builder::GetShaderEffect( const std::string &name, const Replacement& constant )
672 {
673   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
674
675   ShaderEffect ret;
676
677   ShaderEffectLut::const_iterator iter( mShaderEffectLut.find( name ) );
678   if( iter != mShaderEffectLut.end() )
679   {
680     ret = iter->second;
681   }
682   else
683   {
684     if( OptionalChild effects = IsChild( *mParser.GetRoot(), "shader-effects") )
685     {
686       if( OptionalChild effect = IsChild( *effects, name ) )
687       {
688         Dali::Property::Value propertyMap(Property::MAP);
689         if( SetPropertyFromNode( *effect, Property::MAP, propertyMap, constant ) )
690         {
691           ret = Dali::Scripting::NewShaderEffect( propertyMap );
692           mShaderEffectLut[ name ] = ret;
693         }
694       }
695     }
696   }
697
698   return ret;
699 }
700
701 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name )
702 {
703   Replacement constant( mReplacementMap );
704   return GetFrameBufferImage(name, constant);
705 }
706
707 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name, const Replacement& constant )
708 {
709   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
710
711   FrameBufferImage ret;
712
713   ImageLut::const_iterator iter( mFrameBufferImageLut.find( name ) );
714   if( iter != mFrameBufferImageLut.end() )
715   {
716     ret = iter->second;
717   }
718   else
719   {
720     if( OptionalChild images = IsChild( *mParser.GetRoot(), "frame-buffer-images") )
721     {
722       if( OptionalChild image = IsChild( *images, name ) )
723       {
724         Dali::Property::Value property(Property::MAP);
725         if( SetPropertyFromNode( *image, Property::MAP, property, constant ) )
726         {
727           Property::Map* map = property.GetMap();
728           (*map)[ KEYNAME_TYPE ] = Property::Value(std::string("FrameBufferImage") );
729           ret = FrameBufferImage::DownCast( Dali::Scripting::NewImage( property ) );
730           mFrameBufferImageLut[ name ] = ret;
731         }
732       }
733     }
734   }
735
736   return ret;
737 }
738
739 Path Builder::GetPath( const std::string& name )
740 {
741   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
742
743   Path ret;
744
745   PathLut::const_iterator iter( mPathLut.find( name ) );
746   if( iter != mPathLut.end() )
747   {
748     ret = iter->second;
749   }
750   else
751   {
752     if( OptionalChild paths = IsChild( *mParser.GetRoot(), "paths") )
753     {
754       if( OptionalChild path = IsChild( *paths, name ) )
755       {
756         //points property
757         if( OptionalChild pointsProperty = IsChild( *path, "points") )
758         {
759           Dali::Property::Value points(Property::ARRAY);
760           if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
761           {
762             ret = Path::New();
763             ret.SetProperty( Path::Property::POINTS, points);
764
765             //control-points property
766             if( OptionalChild pointsProperty = IsChild( *path, "control-points") )
767             {
768               Dali::Property::Value points(Property::ARRAY);
769               if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
770               {
771                 ret.SetProperty( Path::Property::CONTROL_POINTS, points);
772               }
773             }
774             else
775             {
776               //Curvature
777               float curvature(0.25f);
778               if( OptionalFloat pointsProperty = IsFloat( *path, "curvature") )
779               {
780                 curvature = *pointsProperty;
781               }
782               ret.GenerateControlPoints(curvature);
783             }
784
785             //Add the new path to the hash table for paths
786             mPathLut[ name ] = ret;
787           }
788         }
789         else
790         {
791           //Interpolation points not specified
792           DALI_SCRIPT_WARNING("Interpolation points not specified for path '%s'\n", name.c_str() );
793         }
794       }
795
796     }
797   }
798
799   return ret;
800 }
801
802 PathConstrainer Builder::GetPathConstrainer( const std::string& name )
803 {
804   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
805
806   //Search the pathConstrainer in the LUT
807   size_t count( mPathConstrainerLut.size() );
808   for( size_t i(0); i!=count; ++i )
809   {
810     if( mPathConstrainerLut[i].name == name )
811     {
812       //PathConstrainer has already been created
813       return mPathConstrainerLut[i].pathConstrainer;
814     }
815   }
816
817   //Create a new PathConstrainer
818   PathConstrainer ret;
819   if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
820   {
821     if( OptionalChild pathConstrainer = IsChild( *constrainers, name ) )
822     {
823       OptionalString constrainerType(IsString(IsChild(*pathConstrainer, "type")));
824       if(!constrainerType)
825       {
826         DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str() );
827       }
828       else if( *constrainerType == "PathConstrainer")
829       {
830         //points property
831         if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "points") )
832         {
833           Dali::Property::Value points(Property::ARRAY);
834           if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
835           {
836             ret = PathConstrainer::New();
837             ret.SetProperty( PathConstrainer::Property::POINTS, points);
838
839             //control-points property
840             if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "control-points") )
841             {
842               Dali::Property::Value points(Property::ARRAY);
843               if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
844               {
845                 ret.SetProperty( PathConstrainer::Property::CONTROL_POINTS, points);
846               }
847
848               //Forward vector
849               OptionalVector3 forward( IsVector3( IsChild(*pathConstrainer, "forward" ) ) );
850               if( forward )
851               {
852                 ret.SetProperty( PathConstrainer::Property::FORWARD, *forward);
853               }
854
855               //Add the new constrainer to the vector of PathConstrainer
856               PathConstrainerEntry entry = {name,ret};
857               mPathConstrainerLut.push_back( entry );
858             }
859             else
860             {
861               //Control points not specified
862               DALI_SCRIPT_WARNING("Control points not specified for pathConstrainer '%s'\n", name.c_str() );
863             }
864           }
865         }
866         else
867         {
868           //Interpolation points not specified
869           DALI_SCRIPT_WARNING("Interpolation points not specified for pathConstrainer '%s'\n", name.c_str() );
870         }
871       }
872       else
873       {
874         DALI_SCRIPT_WARNING("Constrainer '%s' is not a PathConstrainer\n", name.c_str() );
875       }
876     }
877   }
878
879   return ret;
880 }
881
882 bool Builder::IsPathConstrainer( const std::string& name )
883 {
884   size_t count( mPathConstrainerLut.size() );
885   for( size_t i(0); i!=count; ++i )
886   {
887     if( mPathConstrainerLut[i].name == name )
888     {
889       return true;
890     }
891   }
892
893   if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
894   {
895     if( OptionalChild constrainer = IsChild( *constrainers, name ) )
896     {
897       OptionalString constrainerType(IsString(IsChild(*constrainer, "type")));
898       if(!constrainerType)
899       {
900         return false;
901       }
902       else
903       {
904          return *constrainerType == "PathConstrainer";
905       }
906     }
907   }
908   return false;
909 }
910
911 Dali::LinearConstrainer Builder::GetLinearConstrainer( const std::string& name )
912 {
913   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
914
915   //Search the LinearConstrainer in the LUT
916   size_t count( mLinearConstrainerLut.size() );
917   for( size_t i(0); i!=count; ++i )
918   {
919     if( mLinearConstrainerLut[i].name == name )
920     {
921       //LinearConstrainer has already been created
922       return mLinearConstrainerLut[i].linearConstrainer;
923     }
924   }
925
926   //Create a new LinearConstrainer
927   LinearConstrainer ret;
928   if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
929   {
930     if( OptionalChild linearConstrainer = IsChild( *constrainers, name ) )
931     {
932       OptionalString constrainerType(IsString(IsChild(*linearConstrainer, "type")));
933       if(!constrainerType)
934       {
935         DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str() );
936       }
937       else if( *constrainerType == "LinearConstrainer")
938       {
939         //points property
940         if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "value") )
941         {
942           Dali::Property::Value points(Property::ARRAY);
943           if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
944           {
945             ret = Dali::LinearConstrainer::New();
946             ret.SetProperty( LinearConstrainer::Property::VALUE, points);
947
948             //control-points property
949             if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "progress") )
950             {
951               Dali::Property::Value points(Property::ARRAY);
952               if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
953               {
954                 ret.SetProperty( LinearConstrainer::Property::PROGRESS, points);
955               }
956             }
957             //Add the new constrainer to vector of LinearConstrainer
958             LinearConstrainerEntry entry = {name,ret};
959             mLinearConstrainerLut.push_back( entry );
960           }
961         }
962         else
963         {
964           //Interpolation points not specified
965           DALI_SCRIPT_WARNING("Values not specified for LinearConstrainer '%s'\n", name.c_str() );
966         }
967       }
968       else
969       {
970         DALI_SCRIPT_WARNING("Constrainer '%s' is not a LinearConstrainer\n", name.c_str() );
971       }
972     }
973   }
974
975   return ret;
976 }
977
978 bool Builder::IsLinearConstrainer( const std::string& name )
979 {
980   //Search the LinearConstrainer in the LUT
981   size_t count( mLinearConstrainerLut.size() );
982   for( size_t i(0); i!=count; ++i )
983   {
984     if( mLinearConstrainerLut[i].name == name )
985     {
986       return true;
987     }
988   }
989
990   if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
991   {
992     if( OptionalChild constrainer = IsChild( *constrainers, name ) )
993     {
994       OptionalString constrainerType(IsString(IsChild(*constrainer, "type")));
995       if(!constrainerType)
996       {
997         return false;
998       }
999       else
1000       {
1001          return *constrainerType == "LinearConstrainer";
1002       }
1003     }
1004   }
1005   return false;
1006 }
1007
1008 Toolkit::Builder::BuilderSignalType& Builder::QuitSignal()
1009 {
1010   return mQuitSignal;
1011 }
1012
1013 void Builder::EmitQuitSignal()
1014 {
1015   mQuitSignal.Emit();
1016 }
1017
1018 void Builder::AddActors( Actor toActor )
1019 {
1020   // 'stage' is the default/by convention section to add from
1021   AddActors( "stage", toActor );
1022 }
1023
1024 void Builder::AddActors( const std::string &sectionName, Actor toActor )
1025 {
1026   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1027
1028   Property::Map overrideMap;
1029   Replacement replacements(overrideMap, mReplacementMap);
1030
1031   OptionalChild add = IsChild(*mParser.GetRoot(), sectionName);
1032
1033   if( add )
1034   {
1035     for( TreeNode::ConstIterator iter = (*add).CBegin(); iter != (*add).CEnd(); ++iter )
1036     {
1037       // empty actor adds directly to the stage
1038       BaseHandle baseHandle = DoCreate( *mParser.GetRoot(), (*iter).second, Actor(), replacements );
1039       Actor actor = Actor::DownCast(baseHandle);
1040       if(actor)
1041       {
1042         toActor.Add( actor );
1043       }
1044     }
1045
1046     // if were adding the 'stage' section then also check for a render task called stage
1047     // to add automatically
1048     if( "stage" == sectionName )
1049     {
1050       if( OptionalChild renderTasks = IsChild(*mParser.GetRoot(), "render-tasks") )
1051       {
1052         if( OptionalChild tasks = IsChild(*renderTasks, "stage") )
1053         {
1054           CreateRenderTask( "stage" );
1055         }
1056       }
1057     }
1058   }
1059 }
1060
1061 Animation Builder::CreateAnimation( const std::string& animationName, const Replacement& replacement, Dali::Actor sourceActor )
1062 {
1063   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1064
1065   Animation anim;
1066
1067   if( OptionalChild animations = IsChild(*mParser.GetRoot(), "animations") )
1068   {
1069     if( OptionalChild animation = IsChild(*animations, animationName) )
1070     {
1071       anim = Dali::Toolkit::Internal::CreateAnimation( *animation, replacement, sourceActor, this );
1072     }
1073     else
1074     {
1075       DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed\n", animationName.c_str() );
1076     }
1077   }
1078   else
1079   {
1080     DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed (no animation section)\n", animationName.c_str() );
1081   }
1082
1083   return anim;
1084 }
1085
1086 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor )
1087 {
1088   Replacement replacement(map, mReplacementMap);
1089   return CreateAnimation( animationName, replacement, sourceActor);
1090 }
1091
1092 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map )
1093 {
1094   Replacement replacement(map, mReplacementMap);
1095   return CreateAnimation( animationName, replacement, Stage::GetCurrent().GetRootLayer() );
1096 }
1097
1098 Animation Builder::CreateAnimation( const std::string& animationName, Dali::Actor sourceActor )
1099 {
1100   Replacement replacement( mReplacementMap );
1101
1102   return CreateAnimation( animationName, replacement, sourceActor );
1103 }
1104
1105 Animation Builder::CreateAnimation( const std::string& animationName )
1106 {
1107   Replacement replacement( mReplacementMap );
1108
1109   return CreateAnimation( animationName, replacement, Dali::Stage::GetCurrent().GetRootLayer() );
1110 }
1111
1112 void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::UIFormat format )
1113 {
1114   // parser to get constants and includes only
1115   Dali::Toolkit::JsonParser parser = Dali::Toolkit::JsonParser::New();
1116
1117   if( !parser.Parse( data ) )
1118   {
1119     DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
1120                       parser.GetErrorLineNumber(),
1121                       parser.GetErrorColumn(),
1122                       parser.GetErrorDescription().c_str() );
1123
1124     DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
1125
1126   }
1127   else
1128   {
1129     // load constant map (allows the user to override the constants in the json after loading)
1130     LoadConstants( *parser.GetRoot(), mReplacementMap );
1131
1132     // merge includes
1133     if( OptionalChild includes = IsChild(*parser.GetRoot(), KEYNAME_INCLUDES) )
1134     {
1135       Replacement replacer( mReplacementMap );
1136
1137       for(TreeNode::ConstIterator iter = (*includes).CBegin(); iter != (*includes).CEnd(); ++iter)
1138       {
1139         OptionalString filename = replacer.IsString( (*iter).second );
1140
1141         if( filename )
1142         {
1143 #if defined(DEBUG_ENABLED)
1144           DALI_SCRIPT_VERBOSE("Loading Include '%s'\n", (*filename).c_str());
1145 #endif
1146           LoadFromString( GetFileContents(*filename) );
1147         }
1148       }
1149     }
1150
1151     if( !mParser.Parse( data ) )
1152     {
1153       DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
1154                         mParser.GetErrorLineNumber(),
1155                         mParser.GetErrorColumn(),
1156                         mParser.GetErrorDescription().c_str() );
1157
1158       DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
1159     }
1160   }
1161
1162   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Cannot parse JSON");
1163
1164 }
1165
1166 void Builder::AddConstants( const Property::Map& map )
1167 {
1168   mReplacementMap.Merge( map );
1169 }
1170
1171 void Builder::AddConstant( const std::string& key, const Property::Value& value )
1172 {
1173   mReplacementMap[key] = value;
1174 }
1175
1176 const Property::Map& Builder::GetConstants() const
1177 {
1178   return mReplacementMap;
1179 }
1180
1181 const Property::Value& Builder::GetConstant( const std::string& key ) const
1182 {
1183   Property::Value* match = mReplacementMap.Find( key );
1184   if( match )
1185   {
1186     return (*match);
1187   }
1188   else
1189   {
1190     static Property::Value invalid;
1191     return invalid;
1192   }
1193 }
1194
1195 void Builder::LoadConstants( const TreeNode& root, Property::Map& intoMap )
1196 {
1197   Replacement replacer(intoMap);
1198
1199   if( OptionalChild constants = IsChild(root, "constants") )
1200   {
1201     for(TreeNode::ConstIterator iter = (*constants).CBegin();
1202         iter != (*constants).CEnd(); ++iter)
1203     {
1204       Dali::Property::Value property;
1205       if( (*iter).second.GetName() )
1206       {
1207 #if defined(DEBUG_ENABLED)
1208         DALI_SCRIPT_VERBOSE("Constant set from json '%s'\n", (*iter).second.GetName());
1209 #endif
1210         if( SetPropertyFromNode( (*iter).second, property, replacer ) )
1211         {
1212           intoMap[ (*iter).second.GetName() ] = property;
1213         }
1214         else
1215         {
1216           DALI_SCRIPT_WARNING("Cannot convert property for constant %s\n",
1217                               (*iter).second.GetName() == NULL ? "no name?" : (*iter).second.GetName());
1218         }
1219       }
1220     }
1221   }
1222
1223 #if defined(DEBUG_ENABLED)
1224   Property::Value* iter = intoMap.Find( "CONFIG_SCRIPT_LOG_LEVEL" );
1225   if( iter && iter->GetType() == Property::STRING )
1226   {
1227     std::string logLevel( iter->Get< std::string >() );
1228     if( logLevel == "NoLogging" )
1229     {
1230       gFilterScript->SetLogLevel( Integration::Log::NoLogging );
1231     }
1232     else if( logLevel == "Concise" )
1233     {
1234       gFilterScript->SetLogLevel( Integration::Log::Concise );
1235     }
1236     else if( logLevel == "General" )
1237     {
1238       gFilterScript->SetLogLevel( Integration::Log::General );
1239     }
1240     else if( logLevel == "Verbose" )
1241     {
1242       gFilterScript->SetLogLevel( Integration::Log::Verbose );
1243     }
1244   }
1245 #endif
1246
1247 }
1248
1249 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle )
1250 {
1251   Replacement replacer( mReplacementMap );
1252   return ApplyStyle( styleName, handle, replacer );
1253 }
1254
1255 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle, const Replacement& replacement )
1256 {
1257   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1258
1259   OptionalChild styles = IsChild( *mParser.GetRoot(), KEYNAME_STYLES );
1260   OptionalChild style  = IsChild( *styles, styleName );
1261
1262   if( styles && style )
1263   {
1264     ApplyAllStyleProperties( *mParser.GetRoot(), *style, handle, replacement );
1265     return true;
1266   }
1267   else
1268   {
1269     return false;
1270   }
1271 }
1272
1273 BaseHandle Builder::Create( const std::string& templateName, const Property::Map& map )
1274 {
1275   Replacement replacement( map, mReplacementMap );
1276   return Create( templateName, replacement );
1277 }
1278
1279 BaseHandle Builder::Create( const std::string& templateName, const Replacement& constant )
1280 {
1281   DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1282
1283   BaseHandle baseHandle;
1284
1285   OptionalChild templates = IsChild(*mParser.GetRoot(), KEYNAME_TEMPLATES);
1286
1287   if( !templates )
1288   {
1289     DALI_SCRIPT_WARNING("No template section found to CreateFromTemplate\n");
1290   }
1291   else
1292   {
1293     OptionalChild childTemplate = IsChild(*templates, templateName);
1294     if(!childTemplate)
1295     {
1296       DALI_SCRIPT_WARNING("Template '%s' does not exist in template section\n", templateName.c_str());
1297     }
1298     else
1299     {
1300       OptionalString type = constant.IsString( IsChild(*childTemplate, KEYNAME_TYPE) );
1301
1302       if(!type)
1303       {
1304         DALI_SCRIPT_WARNING("Cannot create template '%s' as template section is missing 'type'\n", templateName.c_str());
1305       }
1306       else
1307       {
1308         baseHandle = DoCreate( *mParser.GetRoot(), *childTemplate, Actor(), constant );
1309       }
1310     }
1311   }
1312
1313   return baseHandle;
1314 }
1315
1316 BaseHandle Builder::CreateFromJson( const std::string& json )
1317 {
1318   BaseHandle ret;
1319
1320   // merge in new template, hoping no one else has one named '@temp@'
1321   std::string newTemplate =
1322     std::string("{\"templates\":{\"@temp@\":") +                      \
1323     json +                                                            \
1324     std::string("}}");
1325
1326   if( mParser.Parse(newTemplate) )
1327   {
1328     Replacement replacement( mReplacementMap );
1329     ret = Create( "@temp@", replacement );
1330   }
1331
1332   return ret;
1333 }
1334
1335 bool Builder::ApplyFromJson(  Handle& handle, const std::string& json )
1336 {
1337   bool ret = false;
1338
1339   // merge new style, hoping no one else has one named '@temp@'
1340   std::string newStyle =
1341     std::string("{\"styles\":{\"@temp@\":") +                           \
1342     json +                                                              \
1343     std::string("}}");
1344
1345   if( mParser.Parse(newStyle) )
1346   {
1347     Replacement replacement( mReplacementMap );
1348     ret = ApplyStyle( "@temp@", handle, replacement );
1349   }
1350
1351   return ret;
1352 }
1353
1354
1355 BaseHandle Builder::Create( const std::string& templateName )
1356 {
1357   Replacement replacement( mReplacementMap );
1358   return Create( templateName, replacement );
1359 }
1360
1361 Builder::Builder()
1362 : mSlotDelegate( this )
1363 {
1364   mParser = Dali::Toolkit::JsonParser::New();
1365 }
1366
1367 Builder::~Builder()
1368 {
1369 }
1370
1371 } // namespace Internal
1372
1373 } // namespace Toolkit
1374
1375 } // namespace Dali