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>
23 #include <boost/function.hpp>
26 #include <dali/integration-api/debug.h>
30 #include <dali-toolkit/public-api/controls/control.h>
31 #include <dali-toolkit/public-api/builder/json-parser.h>
33 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
34 #include <dali-toolkit/internal/builder/builder-filesystem.h>
35 #include <dali-toolkit/internal/builder/builder-declarations.h>
36 #include <dali-toolkit/internal/builder/replacement.h>
48 extern Animation CreateAnimation(const TreeNode& child, const Replacement& replacements, const Dali::Actor searchRoot, Builder* const builder );
49 extern bool SetPropertyFromNode( const TreeNode& node, Property::Value& value );
50 extern bool SetPropertyFromNode( const TreeNode& node, Property::Value& value, const Replacement& replacements );
51 extern bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value );
52 extern bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value, const Replacement& replacements );
53 extern Actor SetupSignalAction(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, boost::function<void (void)> quitAction, Dali::Toolkit::Internal::Builder* const builder);
54 extern Actor SetupPropertyNotification(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, boost::function<void (void)> quitAction, Dali::Toolkit::Internal::Builder* const builder);
55 extern Actor SetupActor( const TreeNode& node, Actor& actor, const Replacement& constant );
57 #if defined(DEBUG_ENABLED)
58 Integration::Log::Filter* gFilterScript = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_SCRIPT");
64 const std::string KEYNAME_STYLES = "styles";
65 const std::string KEYNAME_TYPE = "type";
66 const std::string KEYNAME_ACTORS = "actors";
67 const std::string KEYNAME_SIGNALS = "signals";
68 const std::string KEYNAME_NAME = "name";
69 const std::string KEYNAME_TEMPLATES = "templates";
70 const std::string KEYNAME_INCLUDES = "includes";
72 typedef std::vector<const TreeNode*> TreeNodeList;
75 std::string ToString(const T& value)
83 std::string ToString(const Rect<int>& value)
86 ss << value.x << "," << value.y << "," << value.width << "," << value.height;
90 #if defined(DEBUG_ENABLED)
92 std::string PropertyValueToString( const Property::Value& value )
96 switch( value.GetType() )
103 case Property::BOOLEAN:
105 ret = value.Get<bool>() ? "True" : "False";
108 case Property::FLOAT:
111 ret = ToString( value.Get<float>() );
114 case Property::INTEGER:
116 ret = ToString( value.Get<int>() );
119 case Property::UNSIGNED_INTEGER:
121 ret = ToString( value.Get<unsigned int>() );
124 case Property::VECTOR2:
126 ret = ToString( value.Get<Vector2>() );
129 case Property::VECTOR3:
131 ret = ToString( value.Get<Vector3>() );
134 case Property::VECTOR4:
136 ret = ToString( value.Get<Vector4>() );
139 case Property::MATRIX3:
141 ret = ToString( value.Get<Matrix3>() );
144 case Property::MATRIX:
146 ret = ToString( value.Get<Matrix>() );
149 case Property::RECTANGLE:
151 ret = ToString( value.Get< Rect<int> >() );
154 case Property::ROTATION:
158 case Property::STRING:
160 ret = value.Get<std::string>();
163 case Property::ARRAY:
165 ret = std::string("Array Size=") + ToString( value.Get<Property::Array>().size() );
170 ret = std::string("Map Size=") + ToString( value.Get<Property::Map>().Count() );
173 case Property::TYPE_COUNT:
182 #endif // DEBUG_ENABLED
185 * Recursively collects all stylesin a node (An array of style names).
187 * stylesCollection The set of styles from the json file (a json object of named styles)
188 * style The style array to begin the collection from
189 * styleList The style list to add nodes to apply
191 void CollectAllStyles( const TreeNode& stylesCollection, const TreeNode& style, TreeNodeList& styleList )
193 // style is an array of style names
194 if( TreeNode::ARRAY == style.GetType() )
196 for(TreeNode::ConstIterator iter = style.CBegin(); iter != style.CEnd(); ++iter)
198 if( OptionalString styleName = IsString( (*iter).second ) )
200 if( OptionalChild node = IsChild( stylesCollection, *styleName) )
202 styleList.push_back( &(*node) );
204 if( OptionalChild subStyle = IsChild( *node, KEYNAME_STYLES ) )
206 CollectAllStyles( stylesCollection, *subStyle, styleList );
217 QuitAction( Builder& builder )
218 : mBuilder( builder )
222 void operator()(void)
224 mBuilder.EmitQuitSignal();
234 * Sets the handle properties found in the tree node
236 void Builder::SetProperties( const TreeNode& node, Handle& handle, const Replacement& constant )
240 for( TreeNode::ConstIterator iter = node.CBegin(); iter != node.CEnd(); ++iter )
242 const TreeNode::KeyNodePair& keyChild = *iter;
244 std::string key( keyChild.first );
246 // ignore special fields; type,actors,signals,styles
247 if(key == KEYNAME_TYPE || key == KEYNAME_ACTORS || key == KEYNAME_SIGNALS || key == KEYNAME_STYLES)
252 // special field 'image' usually contains an json object description
253 // although sometimes refers to a framebuffer
254 if( 0 == keyChild.second.Size() )
258 ImageActor imageActor = ImageActor::DownCast(handle);
261 if( OptionalString s = constant.IsString( keyChild.second ) )
263 FrameBufferImage fb = GetFrameBufferImage(*s, constant);
266 imageActor.SetImage( fb );
273 // special field 'effect' references the shader effect instances
276 RenderableActor actor = RenderableActor::DownCast(handle);
279 OptionalString str = constant.IsString( keyChild.second );
282 ShaderEffect effect = GetShaderEffect( *str, constant );
283 actor.SetShaderEffect(effect);
288 DALI_SCRIPT_WARNING("Could not find or set shader effect\n");
294 Handle propertyObject( handle );
296 Dali::Property::Index index = propertyObject.GetPropertyIndex( key );
298 if( Property::INVALID_INDEX == index )
300 RenderableActor actor = RenderableActor::DownCast(handle);
303 if( ShaderEffect effect = actor.GetShaderEffect() )
305 index = effect.GetPropertyIndex( key );
306 if(index != Property::INVALID_INDEX)
308 propertyObject = effect;
314 if( Property::INVALID_INDEX != index )
316 Property::Type type = propertyObject.GetPropertyType(index);
318 Property::Value value;
319 if( !SetPropertyFromNode( keyChild.second, type, value, constant ) )
321 // verbose as this might not be a problem
322 // eg parent-origin can be a string which is picked up later
323 DALI_SCRIPT_VERBOSE("Could not convert property:%s\n", key.c_str());
327 DALI_SCRIPT_VERBOSE("SetProperty '%s' Index=:%d Value Type=%d Value '%s'\n", key.c_str(), index, value.GetType(), PropertyValueToString(value).c_str() );
329 propertyObject.SetProperty( index, value );
334 DALI_SCRIPT_VERBOSE("SetProperty INVALID '%s' Index=:%d\n", key.c_str(), index);
337 } // for property nodes
341 DALI_SCRIPT_WARNING("Style applied to empty handle\n");
345 // Set properties from node on handle.
346 void Builder::ApplyProperties( const TreeNode& root, const TreeNode& node,
347 Dali::Handle& handle, const Replacement& constant )
349 if( Actor actor = Actor::DownCast(handle) )
351 SetProperties( node, actor, constant );
355 SetupActor( node, actor, constant );
358 QuitAction quitAction( *this );
359 SetupSignalAction( mSlotDelegate.GetConnectionTracker(), root, node, actor, quitAction, this );
360 SetupPropertyNotification( mSlotDelegate.GetConnectionTracker(), root, node, actor, quitAction, this );
365 SetProperties( node, handle, constant );
369 // Appling by style helper
370 // use FindChildByName() to apply properties referenced in KEYNAME_ACTORS in the node
371 void Builder::ApplyStylesByActor( const TreeNode& root, const TreeNode& node,
372 Dali::Handle& handle, const Replacement& constant )
374 if( Dali::Actor actor = Dali::Actor::DownCast( handle ) )
376 if( const TreeNode* actors = node.GetChild( KEYNAME_ACTORS ) )
378 // in a style the actor subtree properties referenced by actor name
379 for( TreeConstIter iter = actors->CBegin(); iter != actors->CEnd(); ++iter )
381 Dali::Actor foundActor;
385 foundActor = actor.FindChildByName( (*iter).first );
390 // debug log cannot find searched for actor
391 #if defined(DEBUG_ENABLED)
392 DALI_SCRIPT_VERBOSE("Cannot find actor in style application '%s'\n", (*iter).first);
397 #if defined(DEBUG_ENABLED)
398 DALI_SCRIPT_VERBOSE("Styles applied to actor '%s'\n", (*iter).first);
400 ApplyProperties( root, (*iter).second, foundActor, constant );
408 void Builder::ApplyAllStyleProperties( const TreeNode& root, const TreeNode& node,
409 Dali::Handle& handle, const Replacement& constant )
411 OptionalChild styles = IsChild(root, KEYNAME_STYLES);
412 OptionalChild style = IsChild(node, KEYNAME_STYLES);
414 if( styles && style )
416 TreeNodeList additionalStyles;
418 CollectAllStyles( *styles, *style, additionalStyles );
420 #if defined(DEBUG_ENABLED)
421 for(TreeNode::ConstIterator iter = (*style).CBegin(); iter != (*style).CEnd(); ++iter)
423 if( OptionalString styleName = IsString( (*iter).second ) )
425 DALI_SCRIPT_VERBOSE("Style Applied '%s'\n", (*styleName).c_str());
430 // a style may have other styles, which has other styles etc so we apply in reverse by convention.
431 for(TreeNodeList::reverse_iterator iter = additionalStyles.rbegin(); iter != additionalStyles.rend(); ++iter)
433 ApplyProperties( root, *(*iter), handle, constant );
435 ApplyStylesByActor( root, *(*iter), handle, constant );
439 // applying given node last
440 ApplyProperties( root, node, handle, constant );
442 ApplyStylesByActor( root, node, handle, constant );
448 * Create a dali type from a node.
449 * If parent given and an actor type was created then add it to the parent and
450 * recursively add nodes children.
452 BaseHandle Builder::DoCreate( const TreeNode& root, const TreeNode& node,
453 Actor parent, const Replacement& replacements )
455 BaseHandle baseHandle;
457 const TreeNode* templateNode = NULL;
459 if( OptionalString typeName = IsString(node, KEYNAME_TYPE) )
461 typeInfo = TypeRegistry::Get().GetTypeInfo( *typeName );
465 // a template name is also allowed inplace of the type name
466 OptionalChild templates = IsChild( root, KEYNAME_TEMPLATES);
470 if( OptionalChild isTemplate = IsChild( *templates, *typeName ) )
472 templateNode = &(*isTemplate);
474 if( OptionalString templateTypeName = IsString(*templateNode, KEYNAME_TYPE) )
476 typeInfo = TypeRegistry::Get().GetTypeInfo( *templateTypeName );
485 DALI_SCRIPT_WARNING("Cannot create Dali type from node '%s'\n", node.GetName());
489 baseHandle = typeInfo.CreateInstance();
490 Handle handle = Handle::DownCast(baseHandle);
491 Actor actor = Actor::DownCast(handle);
496 DALI_SCRIPT_VERBOSE("Create:%s\n", typeInfo.GetName().c_str());
498 #if defined(DEBUG_ENABLED)
501 DALI_SCRIPT_VERBOSE(" Is Handle Object=%d\n", (long*)handle.GetObjectPtr());
502 DALI_SCRIPT_VERBOSE(" Is Handle Property Count=%d\n", handle.GetPropertyCount());
507 DALI_SCRIPT_VERBOSE(" Is Actor id=%d\n", actor.GetId());
510 Toolkit::Control control = Toolkit::Control::DownCast(handle);
513 DALI_SCRIPT_VERBOSE(" Is Control id=%d\n", actor.GetId());
515 #endif // DEBUG_ENABLED
519 ApplyProperties( root, *templateNode, handle, replacements );
521 if( OptionalChild actors = IsChild( *templateNode, KEYNAME_ACTORS ) )
523 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
525 DoCreate( root, (*iter).second, actor, replacements );
532 // TEMP: Assume all script created actors are not using size negotiation for now
533 actor.SetRelayoutEnabled( false );
535 // add children of all the styles
536 if( OptionalChild actors = IsChild( node, KEYNAME_ACTORS ) )
538 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
540 DoCreate( root, (*iter).second, actor, replacements );
544 // apply style on top as they need the children to exist
545 ApplyAllStyleProperties( root, node, actor, replacements );
547 // then add to parent
555 ApplyProperties( root, node, handle, replacements );
560 DALI_SCRIPT_WARNING("Cannot create handle from type '%s'\n", typeInfo.GetName().c_str());
567 void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replacement& constant )
569 const Stage& stage = Stage::GetCurrent();
570 Layer root = stage.GetRootLayer();
572 if( OptionalString s = constant.IsString( IsChild(node, "source-actor") ) )
574 Actor actor = root.FindChildByName(*s);
577 task.SetSourceActor( actor );
581 DALI_SCRIPT_WARNING("Cannot find source actor on stage for render task called '%s'\n", (*s).c_str() );
585 if( OptionalString s = constant.IsString( IsChild(node, "camera-actor") ) )
587 CameraActor actor = CameraActor::DownCast( root.FindChildByName(*s) );
590 task.SetCameraActor( actor );
594 DALI_SCRIPT_WARNING("Cannot find camera actor on stage for render task called '%s'\n", (*s).c_str() );
598 if( OptionalString s = constant.IsString( IsChild(node, "target-frame-buffer") ) )
600 FrameBufferImage fb = GetFrameBufferImage( *s, constant );
603 task.SetTargetFrameBuffer( fb );
607 DALI_SCRIPT_WARNING("Cannot find target frame buffer '%s'\n", (*s).c_str() );
611 if( OptionalString s = constant.IsString( IsChild(node, "screen-to-frame-buffer-function") ) )
613 if("DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION" == *s)
615 task.SetScreenToFrameBufferFunction( RenderTask::DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION );
617 else if("FULLSCREEN_FRAMEBUFFER_FUNCTION" == *s)
619 task.SetScreenToFrameBufferFunction( RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION );
623 DALI_SCRIPT_WARNING("todo");
627 // other setup is via the property system
628 SetProperties( node, task, constant ); // @ todo, remove 'source-actor', 'camera-actor'?
632 void Builder::CreateRenderTask( const std::string &name )
634 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
636 Replacement constant(mReplacementMap);
638 const Stage& stage = Stage::GetCurrent();
640 OptionalChild tasks = IsChild(*mParser.GetRoot(), "render-tasks");
645 // Create the tasks from the current task as generally we want
646 // to setup task zero and onwards. Although this does overwrite
647 // the properties of the current task.
649 if( OptionalChild renderTask = IsChild(*tasks, name ) )
651 RenderTaskList list = stage.GetRenderTaskList();
652 unsigned int start = list.GetTaskCount();
657 // zero should have already been created by the stage so really
658 // this case should never happen
659 task = list.CreateTask();
663 TreeNode::ConstIterator iter = (*renderTask).CBegin();
664 task = list.GetTask( start - 1 );
666 SetupTask( task, (*iter).second, constant );
670 for(; iter != (*renderTask).CEnd(); ++iter )
672 task = list.CreateTask();
673 SetupTask( task, (*iter).second, constant );
679 ShaderEffect Builder::GetShaderEffect( const std::string &name)
681 Replacement constant( mReplacementMap );
682 return GetShaderEffect( name, constant );
685 ShaderEffect Builder::GetShaderEffect( const std::string &name, const Replacement& constant )
687 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
691 ShaderEffectLut::const_iterator iter( mShaderEffectLut.find( name ) );
692 if( iter != mShaderEffectLut.end() )
698 if( OptionalChild effects = IsChild( *mParser.GetRoot(), "shader-effects") )
700 if( OptionalChild effect = IsChild( *effects, name ) )
702 Dali::Property::Value propertyMap(Property::MAP);
703 if( SetPropertyFromNode( *effect, Property::MAP, propertyMap, constant ) )
705 ret = Dali::Scripting::NewShaderEffect( propertyMap );
706 mShaderEffectLut[ name ] = ret;
715 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name )
717 Replacement constant( mReplacementMap );
718 return GetFrameBufferImage(name, constant);
721 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name, const Replacement& constant )
723 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
725 FrameBufferImage ret;
727 ImageLut::const_iterator iter( mFrameBufferImageLut.find( name ) );
728 if( iter != mFrameBufferImageLut.end() )
734 if( OptionalChild images = IsChild( *mParser.GetRoot(), "frame-buffer-images") )
736 if( OptionalChild image = IsChild( *images, name ) )
738 Dali::Property::Value propertyMap(Property::MAP);
739 if( SetPropertyFromNode( *image, Property::MAP, propertyMap, constant ) )
741 propertyMap.SetValue(KEYNAME_TYPE, Property::Value(std::string("FrameBufferImage")));
742 ret = FrameBufferImage::DownCast( Dali::Scripting::NewImage( propertyMap ) );
743 mFrameBufferImageLut[ name ] = ret;
752 Path Builder::GetPath( const std::string& name )
754 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
758 PathLut::const_iterator iter( mPathLut.find( name ) );
759 if( iter != mPathLut.end() )
765 if( OptionalChild paths = IsChild( *mParser.GetRoot(), "paths") )
767 if( OptionalChild path = IsChild( *paths, name ) )
770 if( OptionalChild pointsProperty = IsChild( *path, "points") )
772 Dali::Property::Value points(Property::ARRAY);
773 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
776 ret.SetProperty( Path::Property::POINTS, points);
778 //control-points property
779 if( OptionalChild pointsProperty = IsChild( *path, "control-points") )
781 Dali::Property::Value points(Property::ARRAY);
782 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
784 ret.SetProperty( Path::Property::CONTROL_POINTS, points);
790 float curvature(0.25f);
791 if( OptionalFloat pointsProperty = IsFloat( *path, "curvature") )
793 curvature = *pointsProperty;
795 ret.GenerateControlPoints(curvature);
798 //Add the new path to the hash table for paths
799 mPathLut[ name ] = ret;
804 //Interpolation points not specified
805 DALI_SCRIPT_WARNING("Interpolation points not specified for path '%s'\n", name.c_str() );
815 Toolkit::Builder::BuilderSignalType& Builder::QuitSignal()
820 void Builder::EmitQuitSignal()
825 void Builder::AddActors( Actor toActor )
827 // 'stage' is the default/by convention section to add from
828 AddActors( "stage", toActor );
831 void Builder::AddActors( const std::string §ionName, Actor toActor )
833 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
835 Property::Map overrideMap;
836 Replacement replacements(overrideMap, mReplacementMap);
838 OptionalChild add = IsChild(*mParser.GetRoot(), sectionName);
842 for( TreeNode::ConstIterator iter = (*add).CBegin(); iter != (*add).CEnd(); ++iter )
844 // empty actor adds directly to the stage
845 BaseHandle baseHandle = DoCreate( *mParser.GetRoot(), (*iter).second, Actor(), replacements );
846 Actor actor = Actor::DownCast(baseHandle);
849 toActor.Add( actor );
853 // if were adding the 'stage' section then also check for a render task called stage
854 // to add automatically
855 if( "stage" == sectionName )
857 if( OptionalChild renderTasks = IsChild(*mParser.GetRoot(), "render-tasks") )
859 if( OptionalChild tasks = IsChild(*renderTasks, "stage") )
861 CreateRenderTask( "stage" );
868 Animation Builder::CreateAnimation( const std::string& animationName, const Replacement& replacement, Dali::Actor sourceActor )
870 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
874 if( OptionalChild animations = IsChild(*mParser.GetRoot(), "animations") )
876 if( OptionalChild animation = IsChild(*animations, animationName) )
878 anim = Dali::Toolkit::Internal::CreateAnimation( *animation, replacement, sourceActor, this );
882 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed\n", animationName.c_str() );
887 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed (no animation section)\n", animationName.c_str() );
893 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor )
895 Replacement replacement(map, mReplacementMap);
896 return CreateAnimation( animationName, replacement, sourceActor);
899 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map )
901 Replacement replacement(map, mReplacementMap);
902 return CreateAnimation( animationName, replacement, Stage::GetCurrent().GetRootLayer() );
905 Animation Builder::CreateAnimation( const std::string& animationName, Dali::Actor sourceActor )
907 Replacement replacement( mReplacementMap );
909 return CreateAnimation( animationName, replacement, sourceActor );
912 Animation Builder::CreateAnimation( const std::string& animationName )
914 Replacement replacement( mReplacementMap );
916 return CreateAnimation( animationName, replacement, Dali::Stage::GetCurrent().GetRootLayer() );
919 void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::UIFormat format )
921 // parser to get constants and includes only
922 Dali::Toolkit::JsonParser parser = Dali::Toolkit::JsonParser::New();
924 if( !parser.Parse( data ) )
926 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
927 parser.GetErrorLineNumber(),
928 parser.GetErrorColumn(),
929 parser.GetErrorDescription().c_str() );
931 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
936 // load constant map (allows the user to override the constants in the json after loading)
937 LoadConstants( *parser.GetRoot(), mReplacementMap );
940 if( OptionalChild includes = IsChild(*parser.GetRoot(), KEYNAME_INCLUDES) )
942 Replacement replacer( mReplacementMap );
944 for(TreeNode::ConstIterator iter = (*includes).CBegin(); iter != (*includes).CEnd(); ++iter)
946 OptionalString filename = replacer.IsString( (*iter).second );
950 #if defined(DEBUG_ENABLED)
951 DALI_SCRIPT_VERBOSE("Loading Include '%s'\n", (*filename).c_str());
953 LoadFromString( GetFileContents(*filename) );
958 if( !mParser.Parse( data ) )
960 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
961 mParser.GetErrorLineNumber(),
962 mParser.GetErrorColumn(),
963 mParser.GetErrorDescription().c_str() );
965 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
969 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Cannot parse JSON");
973 void Builder::AddConstants( const Property::Map& map )
975 mReplacementMap.Merge( map );
978 void Builder::AddConstant( const std::string& key, const Property::Value& value )
980 mReplacementMap[key] = value;
983 const Property::Map& Builder::GetConstants() const
985 return mReplacementMap;
988 const Property::Value& Builder::GetConstant( const std::string& key ) const
990 Property::Value* match = mReplacementMap.Find( key );
997 static Property::Value invalid;
1002 void Builder::LoadConstants( const TreeNode& root, Property::Map& intoMap )
1004 Replacement replacer(intoMap);
1006 if( OptionalChild constants = IsChild(root, "constants") )
1008 for(TreeNode::ConstIterator iter = (*constants).CBegin();
1009 iter != (*constants).CEnd(); ++iter)
1011 Dali::Property::Value property;
1012 if( (*iter).second.GetName() )
1014 #if defined(DEBUG_ENABLED)
1015 DALI_SCRIPT_VERBOSE("Constant set from json '%s'\n", (*iter).second.GetName());
1017 if( SetPropertyFromNode( (*iter).second, property, replacer ) )
1019 intoMap[ (*iter).second.GetName() ] = property;
1023 DALI_SCRIPT_WARNING("Cannot convert property for constant %s\n",
1024 (*iter).second.GetName() == NULL ? "no name?" : (*iter).second.GetName());
1030 #if defined(DEBUG_ENABLED)
1031 Property::Value* iter = intoMap.Find( "CONFIG_SCRIPT_LOG_LEVEL" );
1032 if( iter && iter->GetType() == Property::STRING )
1034 std::string logLevel( iter->Get< std::string >() );
1035 if( logLevel == "NoLogging" )
1037 gFilterScript->SetLogLevel( Integration::Log::NoLogging );
1039 else if( logLevel == "Concise" )
1041 gFilterScript->SetLogLevel( Integration::Log::Concise );
1043 else if( logLevel == "General" )
1045 gFilterScript->SetLogLevel( Integration::Log::General );
1047 else if( logLevel == "Verbose" )
1049 gFilterScript->SetLogLevel( Integration::Log::Verbose );
1056 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle )
1058 Replacement replacer( mReplacementMap );
1059 return ApplyStyle( styleName, handle, replacer );
1062 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle, const Replacement& replacement )
1064 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1066 OptionalChild styles = IsChild( *mParser.GetRoot(), KEYNAME_STYLES );
1067 OptionalChild style = IsChild( *styles, styleName );
1069 if( styles && style )
1071 ApplyAllStyleProperties( *mParser.GetRoot(), *style, handle, replacement );
1080 BaseHandle Builder::Create( const std::string& templateName, const Property::Map& map )
1082 Replacement replacement( map, mReplacementMap );
1083 return Create( templateName, replacement );
1086 BaseHandle Builder::Create( const std::string& templateName, const Replacement& constant )
1088 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1090 BaseHandle baseHandle;
1092 OptionalChild templates = IsChild(*mParser.GetRoot(), KEYNAME_TEMPLATES);
1096 DALI_SCRIPT_WARNING("No template section found to CreateFromTemplate\n");
1100 OptionalChild childTemplate = IsChild(*templates, templateName);
1103 DALI_SCRIPT_WARNING("Template '%s' does not exist in template section\n", templateName.c_str());
1107 OptionalString type = constant.IsString( IsChild(*childTemplate, KEYNAME_TYPE) );
1111 DALI_SCRIPT_WARNING("Cannot create template '%s' as template section is missing 'type'\n", templateName.c_str());
1115 baseHandle = DoCreate( *mParser.GetRoot(), *childTemplate, Actor(), constant );
1123 BaseHandle Builder::CreateFromJson( const std::string& json )
1127 // merge in new template, hoping no one else has one named '@temp@'
1128 std::string newTemplate =
1129 std::string("{\"templates\":{\"@temp@\":") + \
1133 if( mParser.Parse(newTemplate) )
1135 Replacement replacement( mReplacementMap );
1136 ret = Create( "@temp@", replacement );
1142 bool Builder::ApplyFromJson( Handle& handle, const std::string& json )
1146 // merge new style, hoping no one else has one named '@temp@'
1147 std::string newStyle =
1148 std::string("{\"styles\":{\"@temp@\":") + \
1152 if( mParser.Parse(newStyle) )
1154 Replacement replacement( mReplacementMap );
1155 ret = ApplyStyle( "@temp@", handle, replacement );
1162 BaseHandle Builder::Create( const std::string& templateName )
1164 Replacement replacement( mReplacementMap );
1165 return Create( templateName, replacement );
1169 : mSlotDelegate( this )
1171 mParser = Dali::Toolkit::JsonParser::New();
1178 } // namespace Internal
1180 } // namespace Toolkit