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 ImageActor actor = ImageActor::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 ImageActor actor = ImageActor::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 // add children of all the styles
533 if( OptionalChild actors = IsChild( node, KEYNAME_ACTORS ) )
535 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
537 DoCreate( root, (*iter).second, actor, replacements );
541 // apply style on top as they need the children to exist
542 ApplyAllStyleProperties( root, node, actor, replacements );
544 // then add to parent
552 ApplyProperties( root, node, handle, replacements );
557 DALI_SCRIPT_WARNING("Cannot create handle from type '%s'\n", typeInfo.GetName().c_str());
564 void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replacement& constant )
566 const Stage& stage = Stage::GetCurrent();
567 Layer root = stage.GetRootLayer();
569 if( OptionalString s = constant.IsString( IsChild(node, "source-actor") ) )
571 Actor actor = root.FindChildByName(*s);
574 task.SetSourceActor( actor );
578 DALI_SCRIPT_WARNING("Cannot find source actor on stage for render task called '%s'\n", (*s).c_str() );
582 if( OptionalString s = constant.IsString( IsChild(node, "camera-actor") ) )
584 CameraActor actor = CameraActor::DownCast( root.FindChildByName(*s) );
587 task.SetCameraActor( actor );
591 DALI_SCRIPT_WARNING("Cannot find camera actor on stage for render task called '%s'\n", (*s).c_str() );
595 if( OptionalString s = constant.IsString( IsChild(node, "target-frame-buffer") ) )
597 FrameBufferImage fb = GetFrameBufferImage( *s, constant );
600 task.SetTargetFrameBuffer( fb );
604 DALI_SCRIPT_WARNING("Cannot find target frame buffer '%s'\n", (*s).c_str() );
608 if( OptionalString s = constant.IsString( IsChild(node, "screen-to-frame-buffer-function") ) )
610 if("DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION" == *s)
612 task.SetScreenToFrameBufferFunction( RenderTask::DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION );
614 else if("FULLSCREEN_FRAMEBUFFER_FUNCTION" == *s)
616 task.SetScreenToFrameBufferFunction( RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION );
620 DALI_SCRIPT_WARNING("todo");
624 // other setup is via the property system
625 SetProperties( node, task, constant ); // @ todo, remove 'source-actor', 'camera-actor'?
629 void Builder::CreateRenderTask( const std::string &name )
631 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
633 Replacement constant(mReplacementMap);
635 const Stage& stage = Stage::GetCurrent();
637 OptionalChild tasks = IsChild(*mParser.GetRoot(), "render-tasks");
642 // Create the tasks from the current task as generally we want
643 // to setup task zero and onwards. Although this does overwrite
644 // the properties of the current task.
646 if( OptionalChild renderTask = IsChild(*tasks, name ) )
648 RenderTaskList list = stage.GetRenderTaskList();
649 unsigned int start = list.GetTaskCount();
654 // zero should have already been created by the stage so really
655 // this case should never happen
656 task = list.CreateTask();
660 TreeNode::ConstIterator iter = (*renderTask).CBegin();
661 task = list.GetTask( start - 1 );
663 SetupTask( task, (*iter).second, constant );
667 for(; iter != (*renderTask).CEnd(); ++iter )
669 task = list.CreateTask();
670 SetupTask( task, (*iter).second, constant );
676 ShaderEffect Builder::GetShaderEffect( const std::string &name)
678 Replacement constant( mReplacementMap );
679 return GetShaderEffect( name, constant );
682 ShaderEffect Builder::GetShaderEffect( const std::string &name, const Replacement& constant )
684 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
688 ShaderEffectLut::const_iterator iter( mShaderEffectLut.find( name ) );
689 if( iter != mShaderEffectLut.end() )
695 if( OptionalChild effects = IsChild( *mParser.GetRoot(), "shader-effects") )
697 if( OptionalChild effect = IsChild( *effects, name ) )
699 Dali::Property::Value propertyMap(Property::MAP);
700 if( SetPropertyFromNode( *effect, Property::MAP, propertyMap, constant ) )
702 ret = Dali::Scripting::NewShaderEffect( propertyMap );
703 mShaderEffectLut[ name ] = ret;
712 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name )
714 Replacement constant( mReplacementMap );
715 return GetFrameBufferImage(name, constant);
718 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name, const Replacement& constant )
720 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
722 FrameBufferImage ret;
724 ImageLut::const_iterator iter( mFrameBufferImageLut.find( name ) );
725 if( iter != mFrameBufferImageLut.end() )
731 if( OptionalChild images = IsChild( *mParser.GetRoot(), "frame-buffer-images") )
733 if( OptionalChild image = IsChild( *images, name ) )
735 Dali::Property::Value propertyMap(Property::MAP);
736 if( SetPropertyFromNode( *image, Property::MAP, propertyMap, constant ) )
738 propertyMap.SetValue(KEYNAME_TYPE, Property::Value(std::string("FrameBufferImage")));
739 ret = FrameBufferImage::DownCast( Dali::Scripting::NewImage( propertyMap ) );
740 mFrameBufferImageLut[ name ] = ret;
749 Path Builder::GetPath( const std::string& name )
751 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
755 PathLut::const_iterator iter( mPathLut.find( name ) );
756 if( iter != mPathLut.end() )
762 if( OptionalChild paths = IsChild( *mParser.GetRoot(), "paths") )
764 if( OptionalChild path = IsChild( *paths, name ) )
767 if( OptionalChild pointsProperty = IsChild( *path, "points") )
769 Dali::Property::Value points(Property::ARRAY);
770 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
773 ret.SetProperty( Path::Property::Points, points);
775 //control-points property
776 if( OptionalChild pointsProperty = IsChild( *path, "control-points") )
778 Dali::Property::Value points(Property::ARRAY);
779 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
781 ret.SetProperty( Path::Property::ControlPoints, points);
787 float curvature(0.25f);
788 if( OptionalFloat pointsProperty = IsFloat( *path, "curvature") )
790 curvature = *pointsProperty;
792 ret.GenerateControlPoints(curvature);
795 //Add the new path to the hash table for paths
796 mPathLut[ name ] = ret;
801 //Interpolation points not specified
802 DALI_SCRIPT_WARNING("Interpolation points not specified for path '%s'\n", name.c_str() );
812 Toolkit::Builder::BuilderSignalType& Builder::QuitSignal()
817 void Builder::EmitQuitSignal()
822 void Builder::AddActors( Actor toActor )
824 // 'stage' is the default/by convention section to add from
825 AddActors( "stage", toActor );
828 void Builder::AddActors( const std::string §ionName, Actor toActor )
830 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
832 Property::Map overrideMap;
833 Replacement replacements(overrideMap, mReplacementMap);
835 OptionalChild add = IsChild(*mParser.GetRoot(), sectionName);
839 for( TreeNode::ConstIterator iter = (*add).CBegin(); iter != (*add).CEnd(); ++iter )
841 // empty actor adds directly to the stage
842 BaseHandle baseHandle = DoCreate( *mParser.GetRoot(), (*iter).second, Actor(), replacements );
843 Actor actor = Actor::DownCast(baseHandle);
846 toActor.Add( actor );
850 // if were adding the 'stage' section then also check for a render task called stage
851 // to add automatically
852 if( "stage" == sectionName )
854 if( OptionalChild renderTasks = IsChild(*mParser.GetRoot(), "render-tasks") )
856 if( OptionalChild tasks = IsChild(*renderTasks, "stage") )
858 CreateRenderTask( "stage" );
865 Animation Builder::CreateAnimation( const std::string& animationName, const Replacement& replacement, Dali::Actor sourceActor )
867 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
871 if( OptionalChild animations = IsChild(*mParser.GetRoot(), "animations") )
873 if( OptionalChild animation = IsChild(*animations, animationName) )
875 anim = Dali::Toolkit::Internal::CreateAnimation( *animation, replacement, sourceActor, this );
879 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed\n", animationName.c_str() );
884 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed (no animation section)\n", animationName.c_str() );
890 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor )
892 Replacement replacement(map, mReplacementMap);
893 return CreateAnimation( animationName, replacement, sourceActor);
896 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map )
898 Replacement replacement(map, mReplacementMap);
899 return CreateAnimation( animationName, replacement, Stage::GetCurrent().GetRootLayer() );
902 Animation Builder::CreateAnimation( const std::string& animationName, Dali::Actor sourceActor )
904 Replacement replacement( mReplacementMap );
906 return CreateAnimation( animationName, replacement, sourceActor );
909 Animation Builder::CreateAnimation( const std::string& animationName )
911 Replacement replacement( mReplacementMap );
913 return CreateAnimation( animationName, replacement, Dali::Stage::GetCurrent().GetRootLayer() );
916 void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::UIFormat format )
918 // parser to get constants and includes only
919 Dali::Toolkit::JsonParser parser = Dali::Toolkit::JsonParser::New();
921 if( !parser.Parse( data ) )
923 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
924 parser.GetErrorLineNumber(),
925 parser.GetErrorColumn(),
926 parser.GetErrorDescription().c_str() );
928 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
933 // load constant map (allows the user to override the constants in the json after loading)
934 LoadConstants( *parser.GetRoot(), mReplacementMap );
937 if( OptionalChild includes = IsChild(*parser.GetRoot(), KEYNAME_INCLUDES) )
939 Replacement replacer( mReplacementMap );
941 for(TreeNode::ConstIterator iter = (*includes).CBegin(); iter != (*includes).CEnd(); ++iter)
943 OptionalString filename = replacer.IsString( (*iter).second );
947 #if defined(DEBUG_ENABLED)
948 DALI_SCRIPT_VERBOSE("Loading Include '%s'\n", (*filename).c_str());
950 LoadFromString( GetFileContents(*filename) );
955 if( !mParser.Parse( data ) )
957 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
958 mParser.GetErrorLineNumber(),
959 mParser.GetErrorColumn(),
960 mParser.GetErrorDescription().c_str() );
962 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
966 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Cannot parse JSON");
970 void Builder::AddConstants( const Property::Map& map )
972 mReplacementMap.Merge( map );
975 void Builder::AddConstant( const std::string& key, const Property::Value& value )
977 mReplacementMap[key] = value;
980 const Property::Map& Builder::GetConstants() const
982 return mReplacementMap;
985 const Property::Value& Builder::GetConstant( const std::string& key ) const
987 Property::Value* match = mReplacementMap.Find( key );
994 static Property::Value invalid;
999 void Builder::LoadConstants( const TreeNode& root, Property::Map& intoMap )
1001 Replacement replacer(intoMap);
1003 if( OptionalChild constants = IsChild(root, "constants") )
1005 for(TreeNode::ConstIterator iter = (*constants).CBegin();
1006 iter != (*constants).CEnd(); ++iter)
1008 Dali::Property::Value property;
1009 if( (*iter).second.GetName() )
1011 #if defined(DEBUG_ENABLED)
1012 DALI_SCRIPT_VERBOSE("Constant set from json '%s'\n", (*iter).second.GetName());
1014 if( SetPropertyFromNode( (*iter).second, property, replacer ) )
1016 intoMap[ (*iter).second.GetName() ] = property;
1020 DALI_SCRIPT_WARNING("Cannot convert property for constant %s\n",
1021 (*iter).second.GetName() == NULL ? "no name?" : (*iter).second.GetName());
1027 #if defined(DEBUG_ENABLED)
1028 Property::Value* iter = intoMap.Find( "CONFIG_SCRIPT_LOG_LEVEL" );
1029 if( iter && iter->GetType() == Property::STRING )
1031 std::string logLevel( iter->Get< std::string >() );
1032 if( logLevel == "NoLogging" )
1034 gFilterScript->SetLogLevel( Integration::Log::NoLogging );
1036 else if( logLevel == "Concise" )
1038 gFilterScript->SetLogLevel( Integration::Log::Concise );
1040 else if( logLevel == "General" )
1042 gFilterScript->SetLogLevel( Integration::Log::General );
1044 else if( logLevel == "Verbose" )
1046 gFilterScript->SetLogLevel( Integration::Log::Verbose );
1053 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle )
1055 Replacement replacer( mReplacementMap );
1056 return ApplyStyle( styleName, handle, replacer );
1059 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle, const Replacement& replacement )
1061 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1063 OptionalChild styles = IsChild( *mParser.GetRoot(), KEYNAME_STYLES );
1064 OptionalChild style = IsChild( *styles, styleName );
1066 if( styles && style )
1068 ApplyAllStyleProperties( *mParser.GetRoot(), *style, handle, replacement );
1077 BaseHandle Builder::Create( const std::string& templateName, const Property::Map& map )
1079 Replacement replacement( map, mReplacementMap );
1080 return Create( templateName, replacement );
1083 BaseHandle Builder::Create( const std::string& templateName, const Replacement& constant )
1085 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1087 BaseHandle baseHandle;
1089 OptionalChild templates = IsChild(*mParser.GetRoot(), KEYNAME_TEMPLATES);
1093 DALI_SCRIPT_WARNING("No template section found to CreateFromTemplate\n");
1097 OptionalChild childTemplate = IsChild(*templates, templateName);
1100 DALI_SCRIPT_WARNING("Template '%s' does not exist in template section\n", templateName.c_str());
1104 OptionalString type = constant.IsString( IsChild(*childTemplate, KEYNAME_TYPE) );
1108 DALI_SCRIPT_WARNING("Cannot create template '%s' as template section is missing 'type'\n", templateName.c_str());
1112 baseHandle = DoCreate( *mParser.GetRoot(), *childTemplate, Actor(), constant );
1120 BaseHandle Builder::CreateFromJson( const std::string& json )
1124 // merge in new template, hoping no one else has one named '@temp@'
1125 std::string newTemplate =
1126 std::string("{\"templates\":{\"@temp@\":") + \
1130 if( mParser.Parse(newTemplate) )
1132 Replacement replacement( mReplacementMap );
1133 ret = Create( "@temp@", replacement );
1139 bool Builder::ApplyFromJson( Handle& handle, const std::string& json )
1143 // merge new style, hoping no one else has one named '@temp@'
1144 std::string newStyle =
1145 std::string("{\"styles\":{\"@temp@\":") + \
1149 if( mParser.Parse(newStyle) )
1151 Replacement replacement( mReplacementMap );
1152 ret = ApplyStyle( "@temp@", handle, replacement );
1159 BaseHandle Builder::Create( const std::string& templateName )
1161 Replacement replacement( mReplacementMap );
1162 return Create( templateName, replacement );
1166 : mSlotDelegate( this )
1168 mParser = Dali::Toolkit::JsonParser::New();
1175 } // namespace Internal
1177 } // namespace Toolkit