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-toolkit/internal/builder/builder-impl.h>
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>
36 #include <dali-toolkit/public-api/controls/control.h>
37 #include <dali-toolkit/devel-api/builder/json-parser.h>
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>
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 );
63 #if defined(DEBUG_ENABLED)
64 Integration::Log::Filter* gFilterScript = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_SCRIPT");
70 #define TOKEN_STRING(x) #x
72 const std::string KEYNAME_STYLES = "styles";
73 const std::string KEYNAME_TYPE = "type";
74 const std::string KEYNAME_ACTORS = "actors";
75 const std::string KEYNAME_SIGNALS = "signals";
76 const std::string KEYNAME_NAME = "name";
77 const std::string KEYNAME_TEMPLATES = "templates";
78 const std::string KEYNAME_INCLUDES = "includes";
80 typedef std::vector<const TreeNode*> TreeNodeList;
83 std::string ToString(const T& value)
91 std::string ToString(const Rect<int>& value)
94 ss << value.x << "," << value.y << "," << value.width << "," << value.height;
98 #if defined(DEBUG_ENABLED)
100 std::string PropertyValueToString( const Property::Value& value )
104 switch( value.GetType() )
111 case Property::BOOLEAN:
113 ret = value.Get<bool>() ? "True" : "False";
116 case Property::FLOAT:
119 ret = ToString( value.Get<float>() );
122 case Property::INTEGER:
124 ret = ToString( value.Get<int>() );
127 case Property::UNSIGNED_INTEGER:
129 ret = ToString( value.Get<unsigned int>() );
132 case Property::VECTOR2:
134 ret = ToString( value.Get<Vector2>() );
137 case Property::VECTOR3:
139 ret = ToString( value.Get<Vector3>() );
142 case Property::VECTOR4:
144 ret = ToString( value.Get<Vector4>() );
147 case Property::MATRIX3:
149 ret = ToString( value.Get<Matrix3>() );
152 case Property::MATRIX:
154 ret = ToString( value.Get<Matrix>() );
157 case Property::RECTANGLE:
159 ret = ToString( value.Get< Rect<int> >() );
162 case Property::ROTATION:
166 case Property::STRING:
168 ret = value.Get<std::string>();
171 case Property::ARRAY:
173 ret = std::string("Array Size=") + ToString( value.Get<Property::Array>().Size() );
178 ret = std::string("Map Size=") + ToString( value.Get<Property::Map>().Count() );
185 #endif // DEBUG_ENABLED
188 * Recursively collects all stylesin a node (An array of style names).
190 * stylesCollection The set of styles from the json file (a json object of named styles)
191 * style The style array to begin the collection from
192 * styleList The style list to add nodes to apply
194 void CollectAllStyles( const TreeNode& stylesCollection, const TreeNode& style, TreeNodeList& styleList )
196 // style is an array of style names
197 if( TreeNode::ARRAY == style.GetType() )
199 for(TreeNode::ConstIterator iter = style.CBegin(); iter != style.CEnd(); ++iter)
201 if( OptionalString styleName = IsString( (*iter).second ) )
203 if( OptionalChild node = IsChild( stylesCollection, *styleName) )
205 styleList.push_back( &(*node) );
207 if( OptionalChild subStyle = IsChild( *node, KEYNAME_STYLES ) )
209 CollectAllStyles( stylesCollection, *subStyle, styleList );
221 * Sets the handle properties found in the tree node
223 void Builder::SetProperties( const TreeNode& node, Handle& handle, const Replacement& constant )
227 for( TreeNode::ConstIterator iter = node.CBegin(); iter != node.CEnd(); ++iter )
229 const TreeNode::KeyNodePair& keyChild = *iter;
231 std::string key( keyChild.first );
233 // ignore special fields; type,actors,signals,styles
234 if(key == KEYNAME_TYPE || key == KEYNAME_ACTORS || key == KEYNAME_SIGNALS || key == KEYNAME_STYLES)
239 // special field 'image' usually contains an json object description
240 // although sometimes refers to a framebuffer
241 if( 0 == keyChild.second.Size() )
245 ImageActor imageActor = ImageActor::DownCast(handle);
248 if( OptionalString s = constant.IsString( keyChild.second ) )
250 FrameBufferImage fb = GetFrameBufferImage(*s, constant);
253 imageActor.SetImage( fb );
260 // special field 'effect' references the shader effect instances
263 ImageActor actor = ImageActor::DownCast(handle);
266 OptionalString str = constant.IsString( keyChild.second );
269 ShaderEffect effect = GetShaderEffect( *str, constant );
270 actor.SetShaderEffect(effect);
275 DALI_SCRIPT_WARNING("Could not find or set shader effect\n");
281 Handle propertyObject( handle );
283 Dali::Property::Index index = propertyObject.GetPropertyIndex( key );
285 if( Property::INVALID_INDEX == index )
287 ImageActor actor = ImageActor::DownCast(handle);
290 if( ShaderEffect effect = actor.GetShaderEffect() )
292 index = effect.GetPropertyIndex( key );
293 if(index != Property::INVALID_INDEX)
295 propertyObject = effect;
301 if( Property::INVALID_INDEX != index )
303 Property::Type type = propertyObject.GetPropertyType(index);
305 Property::Value value;
306 if( !SetPropertyFromNode( keyChild.second, type, value, constant ) )
308 // verbose as this might not be a problem
309 // eg parent-origin can be a string which is picked up later
310 DALI_SCRIPT_VERBOSE("Could not convert property:%s\n", key.c_str());
314 DALI_SCRIPT_VERBOSE("SetProperty '%s' Index=:%d Value Type=%d Value '%s'\n", key.c_str(), index, value.GetType(), PropertyValueToString(value).c_str() );
316 propertyObject.SetProperty( index, value );
321 DALI_SCRIPT_VERBOSE("SetProperty INVALID '%s' Index=:%d\n", key.c_str(), index);
324 } // for property nodes
328 DALI_SCRIPT_WARNING("Style applied to empty handle\n");
332 // Set properties from node on handle.
333 void Builder::ApplyProperties( const TreeNode& root, const TreeNode& node,
334 Dali::Handle& handle, const Replacement& constant )
336 if( Actor actor = Actor::DownCast(handle) )
338 SetProperties( node, actor, constant );
342 SetupActor( node, actor, constant );
345 SetupSignalAction( mSlotDelegate.GetConnectionTracker(), root, node, actor, this );
346 SetupPropertyNotification( mSlotDelegate.GetConnectionTracker(), root, node, actor, this );
351 SetProperties( node, handle, constant );
355 // Appling by style helper
356 // use FindChildByName() to apply properties referenced in KEYNAME_ACTORS in the node
357 void Builder::ApplyStylesByActor( const TreeNode& root, const TreeNode& node,
358 Dali::Handle& handle, const Replacement& constant )
360 if( Dali::Actor actor = Dali::Actor::DownCast( handle ) )
362 if( const TreeNode* actors = node.GetChild( KEYNAME_ACTORS ) )
364 // in a style the actor subtree properties referenced by actor name
365 for( TreeConstIter iter = actors->CBegin(); iter != actors->CEnd(); ++iter )
367 Dali::Actor foundActor;
371 foundActor = actor.FindChildByName( (*iter).first );
376 // debug log cannot find searched for actor
377 #if defined(DEBUG_ENABLED)
378 DALI_SCRIPT_VERBOSE("Cannot find actor in style application '%s'\n", (*iter).first);
383 #if defined(DEBUG_ENABLED)
384 DALI_SCRIPT_VERBOSE("Styles applied to actor '%s'\n", (*iter).first);
386 ApplyProperties( root, (*iter).second, foundActor, constant );
394 void Builder::ApplyAllStyleProperties( const TreeNode& root, const TreeNode& node,
395 Dali::Handle& handle, const Replacement& constant )
397 OptionalChild styles = IsChild(root, KEYNAME_STYLES);
398 OptionalChild style = IsChild(node, KEYNAME_STYLES);
400 if( styles && style )
402 TreeNodeList additionalStyles;
404 CollectAllStyles( *styles, *style, additionalStyles );
406 #if defined(DEBUG_ENABLED)
407 for(TreeNode::ConstIterator iter = (*style).CBegin(); iter != (*style).CEnd(); ++iter)
409 if( OptionalString styleName = IsString( (*iter).second ) )
411 DALI_SCRIPT_VERBOSE("Style Applied '%s'\n", (*styleName).c_str());
416 // a style may have other styles, which has other styles etc so we apply in reverse by convention.
417 for(TreeNodeList::reverse_iterator iter = additionalStyles.rbegin(); iter != additionalStyles.rend(); ++iter)
419 ApplyProperties( root, *(*iter), handle, constant );
421 ApplyStylesByActor( root, *(*iter), handle, constant );
425 // applying given node last
426 ApplyProperties( root, node, handle, constant );
428 ApplyStylesByActor( root, node, handle, constant );
434 * Create a dali type from a node.
435 * If parent given and an actor type was created then add it to the parent and
436 * recursively add nodes children.
438 BaseHandle Builder::DoCreate( const TreeNode& root, const TreeNode& node,
439 Actor parent, const Replacement& replacements )
441 BaseHandle baseHandle;
443 const TreeNode* templateNode = NULL;
445 if( OptionalString typeName = IsString(node, KEYNAME_TYPE) )
447 typeInfo = TypeRegistry::Get().GetTypeInfo( *typeName );
451 // a template name is also allowed inplace of the type name
452 OptionalChild templates = IsChild( root, KEYNAME_TEMPLATES);
456 if( OptionalChild isTemplate = IsChild( *templates, *typeName ) )
458 templateNode = &(*isTemplate);
460 if( OptionalString templateTypeName = IsString(*templateNode, KEYNAME_TYPE) )
462 typeInfo = TypeRegistry::Get().GetTypeInfo( *templateTypeName );
471 DALI_SCRIPT_WARNING("Cannot create Dali type from node '%s'\n", node.GetName());
475 baseHandle = typeInfo.CreateInstance();
476 Handle handle = Handle::DownCast(baseHandle);
477 Actor actor = Actor::DownCast(handle);
482 DALI_SCRIPT_VERBOSE("Create:%s\n", typeInfo.GetName().c_str());
484 #if defined(DEBUG_ENABLED)
487 DALI_SCRIPT_VERBOSE(" Is Handle Object=%d\n", (long*)handle.GetObjectPtr());
488 DALI_SCRIPT_VERBOSE(" Is Handle Property Count=%d\n", handle.GetPropertyCount());
493 DALI_SCRIPT_VERBOSE(" Is Actor id=%d\n", actor.GetId());
496 Toolkit::Control control = Toolkit::Control::DownCast(handle);
499 DALI_SCRIPT_VERBOSE(" Is Control id=%d\n", actor.GetId());
501 #endif // DEBUG_ENABLED
505 ApplyProperties( root, *templateNode, handle, replacements );
507 if( OptionalChild actors = IsChild( *templateNode, KEYNAME_ACTORS ) )
509 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
511 DoCreate( root, (*iter).second, actor, replacements );
518 // add children of all the styles
519 if( OptionalChild actors = IsChild( node, KEYNAME_ACTORS ) )
521 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
523 DoCreate( root, (*iter).second, actor, replacements );
527 // apply style on top as they need the children to exist
528 ApplyAllStyleProperties( root, node, actor, replacements );
530 // then add to parent
538 ApplyProperties( root, node, handle, replacements );
543 DALI_SCRIPT_WARNING("Cannot create handle from type '%s'\n", typeInfo.GetName().c_str());
550 void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replacement& constant )
552 const Stage& stage = Stage::GetCurrent();
553 Layer root = stage.GetRootLayer();
555 if( OptionalString s = constant.IsString( IsChild(node, "source-actor") ) )
557 Actor actor = root.FindChildByName(*s);
560 task.SetSourceActor( actor );
564 DALI_SCRIPT_WARNING("Cannot find source actor on stage for render task called '%s'\n", (*s).c_str() );
568 if( OptionalString s = constant.IsString( IsChild(node, "camera-actor") ) )
570 CameraActor actor = CameraActor::DownCast( root.FindChildByName(*s) );
573 task.SetCameraActor( actor );
577 DALI_SCRIPT_WARNING("Cannot find camera actor on stage for render task called '%s'\n", (*s).c_str() );
581 if( OptionalString s = constant.IsString( IsChild(node, "target-frame-buffer") ) )
583 FrameBufferImage fb = GetFrameBufferImage( *s, constant );
586 task.SetTargetFrameBuffer( fb );
590 DALI_SCRIPT_WARNING("Cannot find target frame buffer '%s'\n", (*s).c_str() );
594 if( OptionalString s = constant.IsString( IsChild(node, "screen-to-frame-buffer-function") ) )
596 if("DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION" == *s)
598 task.SetScreenToFrameBufferFunction( RenderTask::DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION );
600 else if("FULLSCREEN_FRAMEBUFFER_FUNCTION" == *s)
602 task.SetScreenToFrameBufferFunction( RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION );
606 DALI_SCRIPT_WARNING("todo");
610 // other setup is via the property system
611 SetProperties( node, task, constant ); // @ todo, remove 'source-actor', 'camera-actor'?
615 void Builder::CreateRenderTask( const std::string &name )
617 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
619 Replacement constant(mReplacementMap);
621 const Stage& stage = Stage::GetCurrent();
623 OptionalChild tasks = IsChild(*mParser.GetRoot(), "render-tasks");
628 // Create the tasks from the current task as generally we want
629 // to setup task zero and onwards. Although this does overwrite
630 // the properties of the current task.
632 if( OptionalChild renderTask = IsChild(*tasks, name ) )
634 RenderTaskList list = stage.GetRenderTaskList();
635 unsigned int start = list.GetTaskCount();
640 // zero should have already been created by the stage so really
641 // this case should never happen
642 task = list.CreateTask();
646 TreeNode::ConstIterator iter = (*renderTask).CBegin();
647 task = list.GetTask( start - 1 );
649 SetupTask( task, (*iter).second, constant );
653 for(; iter != (*renderTask).CEnd(); ++iter )
655 task = list.CreateTask();
656 SetupTask( task, (*iter).second, constant );
662 ShaderEffect Builder::GetShaderEffect( const std::string &name)
664 Replacement constant( mReplacementMap );
665 return GetShaderEffect( name, constant );
668 ShaderEffect Builder::GetShaderEffect( const std::string &name, const Replacement& constant )
670 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
674 ShaderEffectLut::const_iterator iter( mShaderEffectLut.find( name ) );
675 if( iter != mShaderEffectLut.end() )
681 if( OptionalChild effects = IsChild( *mParser.GetRoot(), "shader-effects") )
683 if( OptionalChild effect = IsChild( *effects, name ) )
685 Dali::Property::Value propertyMap(Property::MAP);
686 if( SetPropertyFromNode( *effect, Property::MAP, propertyMap, constant ) )
688 ret = Dali::Scripting::NewShaderEffect( propertyMap );
689 mShaderEffectLut[ name ] = ret;
698 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name )
700 Replacement constant( mReplacementMap );
701 return GetFrameBufferImage(name, constant);
704 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name, const Replacement& constant )
706 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
708 FrameBufferImage ret;
710 ImageLut::const_iterator iter( mFrameBufferImageLut.find( name ) );
711 if( iter != mFrameBufferImageLut.end() )
717 if( OptionalChild images = IsChild( *mParser.GetRoot(), "frame-buffer-images") )
719 if( OptionalChild image = IsChild( *images, name ) )
721 Dali::Property::Value property(Property::MAP);
722 if( SetPropertyFromNode( *image, Property::MAP, property, constant ) )
724 Property::Map* map = property.GetMap();
725 (*map)[ KEYNAME_TYPE ] = Property::Value(std::string("FrameBufferImage") );
726 ret = FrameBufferImage::DownCast( Dali::Scripting::NewImage( property ) );
727 mFrameBufferImageLut[ name ] = ret;
736 Path Builder::GetPath( const std::string& name )
738 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
742 PathLut::const_iterator iter( mPathLut.find( name ) );
743 if( iter != mPathLut.end() )
749 if( OptionalChild paths = IsChild( *mParser.GetRoot(), "paths") )
751 if( OptionalChild path = IsChild( *paths, name ) )
754 if( OptionalChild pointsProperty = IsChild( *path, "points") )
756 Dali::Property::Value points(Property::ARRAY);
757 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
760 ret.SetProperty( Path::Property::POINTS, points);
762 //control-points property
763 if( OptionalChild pointsProperty = IsChild( *path, "control-points") )
765 Dali::Property::Value points(Property::ARRAY);
766 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
768 ret.SetProperty( Path::Property::CONTROL_POINTS, points);
774 float curvature(0.25f);
775 if( OptionalFloat pointsProperty = IsFloat( *path, "curvature") )
777 curvature = *pointsProperty;
779 ret.GenerateControlPoints(curvature);
782 //Add the new path to the hash table for paths
783 mPathLut[ name ] = ret;
788 //Interpolation points not specified
789 DALI_SCRIPT_WARNING("Interpolation points not specified for path '%s'\n", name.c_str() );
799 PathConstrainer Builder::GetPathConstrainer( const std::string& name )
801 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
803 //Search the pathConstrainer in the LUT
804 size_t count( mPathConstrainerLut.size() );
805 for( size_t i(0); i!=count; ++i )
807 if( mPathConstrainerLut[i].name == name )
809 //PathConstrainer has already been created
810 return mPathConstrainerLut[i].pathConstrainer;
814 //Create a new PathConstrainer
816 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
818 if( OptionalChild pathConstrainer = IsChild( *constrainers, name ) )
820 OptionalString constrainerType(IsString(IsChild(*pathConstrainer, "type")));
823 DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str() );
825 else if( *constrainerType == "PathConstrainer")
828 if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "points") )
830 Dali::Property::Value points(Property::ARRAY);
831 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
833 ret = PathConstrainer::New();
834 ret.SetProperty( PathConstrainer::Property::POINTS, points);
836 //control-points property
837 if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "control-points") )
839 Dali::Property::Value points(Property::ARRAY);
840 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
842 ret.SetProperty( PathConstrainer::Property::CONTROL_POINTS, points);
846 OptionalVector3 forward( IsVector3( IsChild(*pathConstrainer, "forward" ) ) );
849 ret.SetProperty( PathConstrainer::Property::FORWARD, *forward);
852 //Add the new constrainer to the vector of PathConstrainer
853 PathConstrainerEntry entry = {name,ret};
854 mPathConstrainerLut.push_back( entry );
858 //Control points not specified
859 DALI_SCRIPT_WARNING("Control points not specified for pathConstrainer '%s'\n", name.c_str() );
865 //Interpolation points not specified
866 DALI_SCRIPT_WARNING("Interpolation points not specified for pathConstrainer '%s'\n", name.c_str() );
871 DALI_SCRIPT_WARNING("Constrainer '%s' is not a PathConstrainer\n", name.c_str() );
879 bool Builder::IsPathConstrainer( const std::string& name )
881 size_t count( mPathConstrainerLut.size() );
882 for( size_t i(0); i!=count; ++i )
884 if( mPathConstrainerLut[i].name == name )
890 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
892 if( OptionalChild constrainer = IsChild( *constrainers, name ) )
894 OptionalString constrainerType(IsString(IsChild(*constrainer, "type")));
901 return *constrainerType == "PathConstrainer";
908 Dali::LinearConstrainer Builder::GetLinearConstrainer( const std::string& name )
910 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
912 //Search the LinearConstrainer in the LUT
913 size_t count( mLinearConstrainerLut.size() );
914 for( size_t i(0); i!=count; ++i )
916 if( mLinearConstrainerLut[i].name == name )
918 //LinearConstrainer has already been created
919 return mLinearConstrainerLut[i].linearConstrainer;
923 //Create a new LinearConstrainer
924 LinearConstrainer ret;
925 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
927 if( OptionalChild linearConstrainer = IsChild( *constrainers, name ) )
929 OptionalString constrainerType(IsString(IsChild(*linearConstrainer, "type")));
932 DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str() );
934 else if( *constrainerType == "LinearConstrainer")
937 if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "value") )
939 Dali::Property::Value points(Property::ARRAY);
940 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
942 ret = Dali::LinearConstrainer::New();
943 ret.SetProperty( LinearConstrainer::Property::VALUE, points);
945 //control-points property
946 if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "progress") )
948 Dali::Property::Value points(Property::ARRAY);
949 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
951 ret.SetProperty( LinearConstrainer::Property::PROGRESS, points);
954 //Add the new constrainer to vector of LinearConstrainer
955 LinearConstrainerEntry entry = {name,ret};
956 mLinearConstrainerLut.push_back( entry );
961 //Interpolation points not specified
962 DALI_SCRIPT_WARNING("Values not specified for LinearConstrainer '%s'\n", name.c_str() );
967 DALI_SCRIPT_WARNING("Constrainer '%s' is not a LinearConstrainer\n", name.c_str() );
975 bool Builder::IsLinearConstrainer( const std::string& name )
977 //Search the LinearConstrainer in the LUT
978 size_t count( mLinearConstrainerLut.size() );
979 for( size_t i(0); i!=count; ++i )
981 if( mLinearConstrainerLut[i].name == name )
987 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
989 if( OptionalChild constrainer = IsChild( *constrainers, name ) )
991 OptionalString constrainerType(IsString(IsChild(*constrainer, "type")));
998 return *constrainerType == "LinearConstrainer";
1005 Toolkit::Builder::BuilderSignalType& Builder::QuitSignal()
1010 void Builder::EmitQuitSignal()
1015 void Builder::AddActors( Actor toActor )
1017 // 'stage' is the default/by convention section to add from
1018 AddActors( "stage", toActor );
1021 void Builder::AddActors( const std::string §ionName, Actor toActor )
1023 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1025 Property::Map overrideMap;
1026 Replacement replacements(overrideMap, mReplacementMap);
1028 OptionalChild add = IsChild(*mParser.GetRoot(), sectionName);
1032 for( TreeNode::ConstIterator iter = (*add).CBegin(); iter != (*add).CEnd(); ++iter )
1034 // empty actor adds directly to the stage
1035 BaseHandle baseHandle = DoCreate( *mParser.GetRoot(), (*iter).second, Actor(), replacements );
1036 Actor actor = Actor::DownCast(baseHandle);
1039 toActor.Add( actor );
1043 // if were adding the 'stage' section then also check for a render task called stage
1044 // to add automatically
1045 if( "stage" == sectionName )
1047 if( OptionalChild renderTasks = IsChild(*mParser.GetRoot(), "render-tasks") )
1049 if( OptionalChild tasks = IsChild(*renderTasks, "stage") )
1051 CreateRenderTask( "stage" );
1058 Animation Builder::CreateAnimation( const std::string& animationName, const Replacement& replacement, Dali::Actor sourceActor )
1060 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1064 if( OptionalChild animations = IsChild(*mParser.GetRoot(), "animations") )
1066 if( OptionalChild animation = IsChild(*animations, animationName) )
1068 anim = Dali::Toolkit::Internal::CreateAnimation( *animation, replacement, sourceActor, this );
1072 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed\n", animationName.c_str() );
1077 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed (no animation section)\n", animationName.c_str() );
1083 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor )
1085 Replacement replacement(map, mReplacementMap);
1086 return CreateAnimation( animationName, replacement, sourceActor);
1089 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map )
1091 Replacement replacement(map, mReplacementMap);
1092 return CreateAnimation( animationName, replacement, Stage::GetCurrent().GetRootLayer() );
1095 Animation Builder::CreateAnimation( const std::string& animationName, Dali::Actor sourceActor )
1097 Replacement replacement( mReplacementMap );
1099 return CreateAnimation( animationName, replacement, sourceActor );
1102 Animation Builder::CreateAnimation( const std::string& animationName )
1104 Replacement replacement( mReplacementMap );
1106 return CreateAnimation( animationName, replacement, Dali::Stage::GetCurrent().GetRootLayer() );
1109 void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::UIFormat format )
1111 // parser to get constants and includes only
1112 Dali::Toolkit::JsonParser parser = Dali::Toolkit::JsonParser::New();
1114 if( !parser.Parse( data ) )
1116 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
1117 parser.GetErrorLineNumber(),
1118 parser.GetErrorColumn(),
1119 parser.GetErrorDescription().c_str() );
1121 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
1126 // load constant map (allows the user to override the constants in the json after loading)
1127 LoadConstants( *parser.GetRoot(), mReplacementMap );
1130 if( OptionalChild includes = IsChild(*parser.GetRoot(), KEYNAME_INCLUDES) )
1132 Replacement replacer( mReplacementMap );
1134 for(TreeNode::ConstIterator iter = (*includes).CBegin(); iter != (*includes).CEnd(); ++iter)
1136 OptionalString filename = replacer.IsString( (*iter).second );
1140 #if defined(DEBUG_ENABLED)
1141 DALI_SCRIPT_VERBOSE("Loading Include '%s'\n", (*filename).c_str());
1143 LoadFromString( GetFileContents(*filename) );
1148 if( !mParser.Parse( data ) )
1150 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
1151 mParser.GetErrorLineNumber(),
1152 mParser.GetErrorColumn(),
1153 mParser.GetErrorDescription().c_str() );
1155 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
1159 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Cannot parse JSON");
1163 void Builder::AddConstants( const Property::Map& map )
1165 mReplacementMap.Merge( map );
1168 void Builder::AddConstant( const std::string& key, const Property::Value& value )
1170 mReplacementMap[key] = value;
1173 const Property::Map& Builder::GetConstants() const
1175 return mReplacementMap;
1178 const Property::Value& Builder::GetConstant( const std::string& key ) const
1180 Property::Value* match = mReplacementMap.Find( key );
1187 static Property::Value invalid;
1192 void Builder::LoadConstants( const TreeNode& root, Property::Map& intoMap )
1194 Replacement replacer(intoMap);
1196 if( OptionalChild constants = IsChild(root, "constants") )
1198 for(TreeNode::ConstIterator iter = (*constants).CBegin();
1199 iter != (*constants).CEnd(); ++iter)
1201 Dali::Property::Value property;
1202 if( (*iter).second.GetName() )
1204 #if defined(DEBUG_ENABLED)
1205 DALI_SCRIPT_VERBOSE("Constant set from json '%s'\n", (*iter).second.GetName());
1207 if( SetPropertyFromNode( (*iter).second, property, replacer ) )
1209 intoMap[ (*iter).second.GetName() ] = property;
1213 DALI_SCRIPT_WARNING("Cannot convert property for constant %s\n",
1214 (*iter).second.GetName() == NULL ? "no name?" : (*iter).second.GetName());
1220 #if defined(DEBUG_ENABLED)
1221 Property::Value* iter = intoMap.Find( "CONFIG_SCRIPT_LOG_LEVEL" );
1222 if( iter && iter->GetType() == Property::STRING )
1224 std::string logLevel( iter->Get< std::string >() );
1225 if( logLevel == "NoLogging" )
1227 gFilterScript->SetLogLevel( Integration::Log::NoLogging );
1229 else if( logLevel == "Concise" )
1231 gFilterScript->SetLogLevel( Integration::Log::Concise );
1233 else if( logLevel == "General" )
1235 gFilterScript->SetLogLevel( Integration::Log::General );
1237 else if( logLevel == "Verbose" )
1239 gFilterScript->SetLogLevel( Integration::Log::Verbose );
1246 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle )
1248 Replacement replacer( mReplacementMap );
1249 return ApplyStyle( styleName, handle, replacer );
1252 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle, const Replacement& replacement )
1254 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1256 OptionalChild styles = IsChild( *mParser.GetRoot(), KEYNAME_STYLES );
1257 OptionalChild style = IsChild( *styles, styleName );
1259 if( styles && style )
1261 ApplyAllStyleProperties( *mParser.GetRoot(), *style, handle, replacement );
1270 BaseHandle Builder::Create( const std::string& templateName, const Property::Map& map )
1272 Replacement replacement( map, mReplacementMap );
1273 return Create( templateName, replacement );
1276 BaseHandle Builder::Create( const std::string& templateName, const Replacement& constant )
1278 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1280 BaseHandle baseHandle;
1282 OptionalChild templates = IsChild(*mParser.GetRoot(), KEYNAME_TEMPLATES);
1286 DALI_SCRIPT_WARNING("No template section found to CreateFromTemplate\n");
1290 OptionalChild childTemplate = IsChild(*templates, templateName);
1293 DALI_SCRIPT_WARNING("Template '%s' does not exist in template section\n", templateName.c_str());
1297 OptionalString type = constant.IsString( IsChild(*childTemplate, KEYNAME_TYPE) );
1301 DALI_SCRIPT_WARNING("Cannot create template '%s' as template section is missing 'type'\n", templateName.c_str());
1305 baseHandle = DoCreate( *mParser.GetRoot(), *childTemplate, Actor(), constant );
1313 BaseHandle Builder::CreateFromJson( const std::string& json )
1317 // merge in new template, hoping no one else has one named '@temp@'
1318 std::string newTemplate =
1319 std::string("{\"templates\":{\"@temp@\":") + \
1323 if( mParser.Parse(newTemplate) )
1325 Replacement replacement( mReplacementMap );
1326 ret = Create( "@temp@", replacement );
1332 bool Builder::ApplyFromJson( Handle& handle, const std::string& json )
1336 // merge new style, hoping no one else has one named '@temp@'
1337 std::string newStyle =
1338 std::string("{\"styles\":{\"@temp@\":") + \
1342 if( mParser.Parse(newStyle) )
1344 Replacement replacement( mReplacementMap );
1345 ret = ApplyStyle( "@temp@", handle, replacement );
1352 BaseHandle Builder::Create( const std::string& templateName )
1354 Replacement replacement( mReplacementMap );
1355 return Create( templateName, replacement );
1359 : mSlotDelegate( this )
1361 mParser = Dali::Toolkit::JsonParser::New();
1363 Property::Map defaultDirs;
1364 defaultDirs[ TOKEN_STRING(DALI_IMAGE_DIR) ] = DALI_IMAGE_DIR;
1365 defaultDirs[ TOKEN_STRING(DALI_SOUND_DIR) ] = DALI_SOUND_DIR;
1366 defaultDirs[ TOKEN_STRING(DALI_STYLE_DIR) ] = DALI_STYLE_DIR;
1368 AddConstants( defaultDirs );
1375 } // namespace Internal
1377 } // namespace Toolkit