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>
25 #include <dali/public-api/render-tasks/render-task-list.h>
26 #include <dali/public-api/object/type-info.h>
27 #include <dali/public-api/object/type-registry.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/public-api/scripting/scripting.h>
32 #include <dali/integration-api/debug.h>
35 #include <dali-toolkit/public-api/controls/control.h>
36 #include <dali-toolkit/public-api/builder/json-parser.h>
38 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
39 #include <dali-toolkit/internal/builder/builder-filesystem.h>
40 #include <dali-toolkit/internal/builder/builder-declarations.h>
41 #include <dali-toolkit/internal/builder/replacement.h>
53 extern Animation CreateAnimation(const TreeNode& child, const Replacement& replacements, const Dali::Actor searchRoot, Builder* const builder );
54 extern bool SetPropertyFromNode( const TreeNode& node, Property::Value& value );
55 extern bool SetPropertyFromNode( const TreeNode& node, Property::Value& value, const Replacement& replacements );
56 extern bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value );
57 extern bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value, const Replacement& replacements );
58 extern Actor SetupSignalAction(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, boost::function<void (void)> quitAction, Dali::Toolkit::Internal::Builder* const builder);
59 extern Actor SetupPropertyNotification(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, boost::function<void (void)> quitAction, Dali::Toolkit::Internal::Builder* const builder);
60 extern Actor SetupActor( const TreeNode& node, Actor& actor, const Replacement& constant );
62 #if defined(DEBUG_ENABLED)
63 Integration::Log::Filter* gFilterScript = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_SCRIPT");
69 const std::string KEYNAME_STYLES = "styles";
70 const std::string KEYNAME_TYPE = "type";
71 const std::string KEYNAME_ACTORS = "actors";
72 const std::string KEYNAME_SIGNALS = "signals";
73 const std::string KEYNAME_NAME = "name";
74 const std::string KEYNAME_TEMPLATES = "templates";
75 const std::string KEYNAME_INCLUDES = "includes";
77 typedef std::vector<const TreeNode*> TreeNodeList;
80 std::string ToString(const T& value)
88 std::string ToString(const Rect<int>& value)
91 ss << value.x << "," << value.y << "," << value.width << "," << value.height;
95 #if defined(DEBUG_ENABLED)
97 std::string PropertyValueToString( const Property::Value& value )
101 switch( value.GetType() )
108 case Property::BOOLEAN:
110 ret = value.Get<bool>() ? "True" : "False";
113 case Property::FLOAT:
116 ret = ToString( value.Get<float>() );
119 case Property::INTEGER:
121 ret = ToString( value.Get<int>() );
124 case Property::UNSIGNED_INTEGER:
126 ret = ToString( value.Get<unsigned int>() );
129 case Property::VECTOR2:
131 ret = ToString( value.Get<Vector2>() );
134 case Property::VECTOR3:
136 ret = ToString( value.Get<Vector3>() );
139 case Property::VECTOR4:
141 ret = ToString( value.Get<Vector4>() );
144 case Property::MATRIX3:
146 ret = ToString( value.Get<Matrix3>() );
149 case Property::MATRIX:
151 ret = ToString( value.Get<Matrix>() );
154 case Property::RECTANGLE:
156 ret = ToString( value.Get< Rect<int> >() );
159 case Property::ROTATION:
163 case Property::STRING:
165 ret = value.Get<std::string>();
168 case Property::ARRAY:
170 ret = std::string("Array Size=") + ToString( value.Get<Property::Array>().size() );
175 ret = std::string("Map Size=") + ToString( value.Get<Property::Map>().Count() );
178 case Property::TYPE_COUNT:
187 #endif // DEBUG_ENABLED
190 * Recursively collects all stylesin a node (An array of style names).
192 * stylesCollection The set of styles from the json file (a json object of named styles)
193 * style The style array to begin the collection from
194 * styleList The style list to add nodes to apply
196 void CollectAllStyles( const TreeNode& stylesCollection, const TreeNode& style, TreeNodeList& styleList )
198 // style is an array of style names
199 if( TreeNode::ARRAY == style.GetType() )
201 for(TreeNode::ConstIterator iter = style.CBegin(); iter != style.CEnd(); ++iter)
203 if( OptionalString styleName = IsString( (*iter).second ) )
205 if( OptionalChild node = IsChild( stylesCollection, *styleName) )
207 styleList.push_back( &(*node) );
209 if( OptionalChild subStyle = IsChild( *node, KEYNAME_STYLES ) )
211 CollectAllStyles( stylesCollection, *subStyle, styleList );
222 QuitAction( Builder& builder )
223 : mBuilder( builder )
227 void operator()(void)
229 mBuilder.EmitQuitSignal();
239 * Sets the handle properties found in the tree node
241 void Builder::SetProperties( const TreeNode& node, Handle& handle, const Replacement& constant )
245 for( TreeNode::ConstIterator iter = node.CBegin(); iter != node.CEnd(); ++iter )
247 const TreeNode::KeyNodePair& keyChild = *iter;
249 std::string key( keyChild.first );
251 // ignore special fields; type,actors,signals,styles
252 if(key == KEYNAME_TYPE || key == KEYNAME_ACTORS || key == KEYNAME_SIGNALS || key == KEYNAME_STYLES)
257 // special field 'image' usually contains an json object description
258 // although sometimes refers to a framebuffer
259 if( 0 == keyChild.second.Size() )
263 ImageActor imageActor = ImageActor::DownCast(handle);
266 if( OptionalString s = constant.IsString( keyChild.second ) )
268 FrameBufferImage fb = GetFrameBufferImage(*s, constant);
271 imageActor.SetImage( fb );
278 // special field 'effect' references the shader effect instances
281 ImageActor actor = ImageActor::DownCast(handle);
284 OptionalString str = constant.IsString( keyChild.second );
287 ShaderEffect effect = GetShaderEffect( *str, constant );
288 actor.SetShaderEffect(effect);
293 DALI_SCRIPT_WARNING("Could not find or set shader effect\n");
299 Handle propertyObject( handle );
301 Dali::Property::Index index = propertyObject.GetPropertyIndex( key );
303 if( Property::INVALID_INDEX == index )
305 ImageActor actor = ImageActor::DownCast(handle);
308 if( ShaderEffect effect = actor.GetShaderEffect() )
310 index = effect.GetPropertyIndex( key );
311 if(index != Property::INVALID_INDEX)
313 propertyObject = effect;
319 if( Property::INVALID_INDEX != index )
321 Property::Type type = propertyObject.GetPropertyType(index);
323 Property::Value value;
324 if( !SetPropertyFromNode( keyChild.second, type, value, constant ) )
326 // verbose as this might not be a problem
327 // eg parent-origin can be a string which is picked up later
328 DALI_SCRIPT_VERBOSE("Could not convert property:%s\n", key.c_str());
332 DALI_SCRIPT_VERBOSE("SetProperty '%s' Index=:%d Value Type=%d Value '%s'\n", key.c_str(), index, value.GetType(), PropertyValueToString(value).c_str() );
334 propertyObject.SetProperty( index, value );
339 DALI_SCRIPT_VERBOSE("SetProperty INVALID '%s' Index=:%d\n", key.c_str(), index);
342 } // for property nodes
346 DALI_SCRIPT_WARNING("Style applied to empty handle\n");
350 // Set properties from node on handle.
351 void Builder::ApplyProperties( const TreeNode& root, const TreeNode& node,
352 Dali::Handle& handle, const Replacement& constant )
354 if( Actor actor = Actor::DownCast(handle) )
356 SetProperties( node, actor, constant );
360 SetupActor( node, actor, constant );
363 QuitAction quitAction( *this );
364 SetupSignalAction( mSlotDelegate.GetConnectionTracker(), root, node, actor, quitAction, this );
365 SetupPropertyNotification( mSlotDelegate.GetConnectionTracker(), root, node, actor, quitAction, this );
370 SetProperties( node, handle, constant );
374 // Appling by style helper
375 // use FindChildByName() to apply properties referenced in KEYNAME_ACTORS in the node
376 void Builder::ApplyStylesByActor( const TreeNode& root, const TreeNode& node,
377 Dali::Handle& handle, const Replacement& constant )
379 if( Dali::Actor actor = Dali::Actor::DownCast( handle ) )
381 if( const TreeNode* actors = node.GetChild( KEYNAME_ACTORS ) )
383 // in a style the actor subtree properties referenced by actor name
384 for( TreeConstIter iter = actors->CBegin(); iter != actors->CEnd(); ++iter )
386 Dali::Actor foundActor;
390 foundActor = actor.FindChildByName( (*iter).first );
395 // debug log cannot find searched for actor
396 #if defined(DEBUG_ENABLED)
397 DALI_SCRIPT_VERBOSE("Cannot find actor in style application '%s'\n", (*iter).first);
402 #if defined(DEBUG_ENABLED)
403 DALI_SCRIPT_VERBOSE("Styles applied to actor '%s'\n", (*iter).first);
405 ApplyProperties( root, (*iter).second, foundActor, constant );
413 void Builder::ApplyAllStyleProperties( const TreeNode& root, const TreeNode& node,
414 Dali::Handle& handle, const Replacement& constant )
416 OptionalChild styles = IsChild(root, KEYNAME_STYLES);
417 OptionalChild style = IsChild(node, KEYNAME_STYLES);
419 if( styles && style )
421 TreeNodeList additionalStyles;
423 CollectAllStyles( *styles, *style, additionalStyles );
425 #if defined(DEBUG_ENABLED)
426 for(TreeNode::ConstIterator iter = (*style).CBegin(); iter != (*style).CEnd(); ++iter)
428 if( OptionalString styleName = IsString( (*iter).second ) )
430 DALI_SCRIPT_VERBOSE("Style Applied '%s'\n", (*styleName).c_str());
435 // a style may have other styles, which has other styles etc so we apply in reverse by convention.
436 for(TreeNodeList::reverse_iterator iter = additionalStyles.rbegin(); iter != additionalStyles.rend(); ++iter)
438 ApplyProperties( root, *(*iter), handle, constant );
440 ApplyStylesByActor( root, *(*iter), handle, constant );
444 // applying given node last
445 ApplyProperties( root, node, handle, constant );
447 ApplyStylesByActor( root, node, handle, constant );
453 * Create a dali type from a node.
454 * If parent given and an actor type was created then add it to the parent and
455 * recursively add nodes children.
457 BaseHandle Builder::DoCreate( const TreeNode& root, const TreeNode& node,
458 Actor parent, const Replacement& replacements )
460 BaseHandle baseHandle;
462 const TreeNode* templateNode = NULL;
464 if( OptionalString typeName = IsString(node, KEYNAME_TYPE) )
466 typeInfo = TypeRegistry::Get().GetTypeInfo( *typeName );
470 // a template name is also allowed inplace of the type name
471 OptionalChild templates = IsChild( root, KEYNAME_TEMPLATES);
475 if( OptionalChild isTemplate = IsChild( *templates, *typeName ) )
477 templateNode = &(*isTemplate);
479 if( OptionalString templateTypeName = IsString(*templateNode, KEYNAME_TYPE) )
481 typeInfo = TypeRegistry::Get().GetTypeInfo( *templateTypeName );
490 DALI_SCRIPT_WARNING("Cannot create Dali type from node '%s'\n", node.GetName());
494 baseHandle = typeInfo.CreateInstance();
495 Handle handle = Handle::DownCast(baseHandle);
496 Actor actor = Actor::DownCast(handle);
501 DALI_SCRIPT_VERBOSE("Create:%s\n", typeInfo.GetName().c_str());
503 #if defined(DEBUG_ENABLED)
506 DALI_SCRIPT_VERBOSE(" Is Handle Object=%d\n", (long*)handle.GetObjectPtr());
507 DALI_SCRIPT_VERBOSE(" Is Handle Property Count=%d\n", handle.GetPropertyCount());
512 DALI_SCRIPT_VERBOSE(" Is Actor id=%d\n", actor.GetId());
515 Toolkit::Control control = Toolkit::Control::DownCast(handle);
518 DALI_SCRIPT_VERBOSE(" Is Control id=%d\n", actor.GetId());
520 #endif // DEBUG_ENABLED
524 ApplyProperties( root, *templateNode, handle, replacements );
526 if( OptionalChild actors = IsChild( *templateNode, KEYNAME_ACTORS ) )
528 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
530 DoCreate( root, (*iter).second, actor, replacements );
537 // add children of all the styles
538 if( OptionalChild actors = IsChild( node, KEYNAME_ACTORS ) )
540 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
542 DoCreate( root, (*iter).second, actor, replacements );
546 // apply style on top as they need the children to exist
547 ApplyAllStyleProperties( root, node, actor, replacements );
549 // then add to parent
557 ApplyProperties( root, node, handle, replacements );
562 DALI_SCRIPT_WARNING("Cannot create handle from type '%s'\n", typeInfo.GetName().c_str());
569 void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replacement& constant )
571 const Stage& stage = Stage::GetCurrent();
572 Layer root = stage.GetRootLayer();
574 if( OptionalString s = constant.IsString( IsChild(node, "source-actor") ) )
576 Actor actor = root.FindChildByName(*s);
579 task.SetSourceActor( actor );
583 DALI_SCRIPT_WARNING("Cannot find source actor on stage for render task called '%s'\n", (*s).c_str() );
587 if( OptionalString s = constant.IsString( IsChild(node, "camera-actor") ) )
589 CameraActor actor = CameraActor::DownCast( root.FindChildByName(*s) );
592 task.SetCameraActor( actor );
596 DALI_SCRIPT_WARNING("Cannot find camera actor on stage for render task called '%s'\n", (*s).c_str() );
600 if( OptionalString s = constant.IsString( IsChild(node, "target-frame-buffer") ) )
602 FrameBufferImage fb = GetFrameBufferImage( *s, constant );
605 task.SetTargetFrameBuffer( fb );
609 DALI_SCRIPT_WARNING("Cannot find target frame buffer '%s'\n", (*s).c_str() );
613 if( OptionalString s = constant.IsString( IsChild(node, "screen-to-frame-buffer-function") ) )
615 if("DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION" == *s)
617 task.SetScreenToFrameBufferFunction( RenderTask::DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION );
619 else if("FULLSCREEN_FRAMEBUFFER_FUNCTION" == *s)
621 task.SetScreenToFrameBufferFunction( RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION );
625 DALI_SCRIPT_WARNING("todo");
629 // other setup is via the property system
630 SetProperties( node, task, constant ); // @ todo, remove 'source-actor', 'camera-actor'?
634 void Builder::CreateRenderTask( const std::string &name )
636 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
638 Replacement constant(mReplacementMap);
640 const Stage& stage = Stage::GetCurrent();
642 OptionalChild tasks = IsChild(*mParser.GetRoot(), "render-tasks");
647 // Create the tasks from the current task as generally we want
648 // to setup task zero and onwards. Although this does overwrite
649 // the properties of the current task.
651 if( OptionalChild renderTask = IsChild(*tasks, name ) )
653 RenderTaskList list = stage.GetRenderTaskList();
654 unsigned int start = list.GetTaskCount();
659 // zero should have already been created by the stage so really
660 // this case should never happen
661 task = list.CreateTask();
665 TreeNode::ConstIterator iter = (*renderTask).CBegin();
666 task = list.GetTask( start - 1 );
668 SetupTask( task, (*iter).second, constant );
672 for(; iter != (*renderTask).CEnd(); ++iter )
674 task = list.CreateTask();
675 SetupTask( task, (*iter).second, constant );
681 ShaderEffect Builder::GetShaderEffect( const std::string &name)
683 Replacement constant( mReplacementMap );
684 return GetShaderEffect( name, constant );
687 ShaderEffect Builder::GetShaderEffect( const std::string &name, const Replacement& constant )
689 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
693 ShaderEffectLut::const_iterator iter( mShaderEffectLut.find( name ) );
694 if( iter != mShaderEffectLut.end() )
700 if( OptionalChild effects = IsChild( *mParser.GetRoot(), "shader-effects") )
702 if( OptionalChild effect = IsChild( *effects, name ) )
704 Dali::Property::Value propertyMap(Property::MAP);
705 if( SetPropertyFromNode( *effect, Property::MAP, propertyMap, constant ) )
707 ret = Dali::Scripting::NewShaderEffect( propertyMap );
708 mShaderEffectLut[ name ] = ret;
717 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name )
719 Replacement constant( mReplacementMap );
720 return GetFrameBufferImage(name, constant);
723 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name, const Replacement& constant )
725 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
727 FrameBufferImage ret;
729 ImageLut::const_iterator iter( mFrameBufferImageLut.find( name ) );
730 if( iter != mFrameBufferImageLut.end() )
736 if( OptionalChild images = IsChild( *mParser.GetRoot(), "frame-buffer-images") )
738 if( OptionalChild image = IsChild( *images, name ) )
740 Dali::Property::Value propertyMap(Property::MAP);
741 if( SetPropertyFromNode( *image, Property::MAP, propertyMap, constant ) )
743 propertyMap.SetValue(KEYNAME_TYPE, Property::Value(std::string("FrameBufferImage")));
744 ret = FrameBufferImage::DownCast( Dali::Scripting::NewImage( propertyMap ) );
745 mFrameBufferImageLut[ name ] = ret;
754 Path Builder::GetPath( const std::string& name )
756 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
760 PathLut::const_iterator iter( mPathLut.find( name ) );
761 if( iter != mPathLut.end() )
767 if( OptionalChild paths = IsChild( *mParser.GetRoot(), "paths") )
769 if( OptionalChild path = IsChild( *paths, name ) )
772 if( OptionalChild pointsProperty = IsChild( *path, "points") )
774 Dali::Property::Value points(Property::ARRAY);
775 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
778 ret.SetProperty( Path::Property::POINTS, points);
780 //control-points property
781 if( OptionalChild pointsProperty = IsChild( *path, "control-points") )
783 Dali::Property::Value points(Property::ARRAY);
784 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
786 ret.SetProperty( Path::Property::CONTROL_POINTS, points);
792 float curvature(0.25f);
793 if( OptionalFloat pointsProperty = IsFloat( *path, "curvature") )
795 curvature = *pointsProperty;
797 ret.GenerateControlPoints(curvature);
800 //Add the new path to the hash table for paths
801 mPathLut[ name ] = ret;
806 //Interpolation points not specified
807 DALI_SCRIPT_WARNING("Interpolation points not specified for path '%s'\n", name.c_str() );
817 Toolkit::Builder::BuilderSignalType& Builder::QuitSignal()
822 void Builder::EmitQuitSignal()
827 void Builder::AddActors( Actor toActor )
829 // 'stage' is the default/by convention section to add from
830 AddActors( "stage", toActor );
833 void Builder::AddActors( const std::string §ionName, Actor toActor )
835 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
837 Property::Map overrideMap;
838 Replacement replacements(overrideMap, mReplacementMap);
840 OptionalChild add = IsChild(*mParser.GetRoot(), sectionName);
844 for( TreeNode::ConstIterator iter = (*add).CBegin(); iter != (*add).CEnd(); ++iter )
846 // empty actor adds directly to the stage
847 BaseHandle baseHandle = DoCreate( *mParser.GetRoot(), (*iter).second, Actor(), replacements );
848 Actor actor = Actor::DownCast(baseHandle);
851 toActor.Add( actor );
855 // if were adding the 'stage' section then also check for a render task called stage
856 // to add automatically
857 if( "stage" == sectionName )
859 if( OptionalChild renderTasks = IsChild(*mParser.GetRoot(), "render-tasks") )
861 if( OptionalChild tasks = IsChild(*renderTasks, "stage") )
863 CreateRenderTask( "stage" );
870 Animation Builder::CreateAnimation( const std::string& animationName, const Replacement& replacement, Dali::Actor sourceActor )
872 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
876 if( OptionalChild animations = IsChild(*mParser.GetRoot(), "animations") )
878 if( OptionalChild animation = IsChild(*animations, animationName) )
880 anim = Dali::Toolkit::Internal::CreateAnimation( *animation, replacement, sourceActor, this );
884 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed\n", animationName.c_str() );
889 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed (no animation section)\n", animationName.c_str() );
895 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor )
897 Replacement replacement(map, mReplacementMap);
898 return CreateAnimation( animationName, replacement, sourceActor);
901 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map )
903 Replacement replacement(map, mReplacementMap);
904 return CreateAnimation( animationName, replacement, Stage::GetCurrent().GetRootLayer() );
907 Animation Builder::CreateAnimation( const std::string& animationName, Dali::Actor sourceActor )
909 Replacement replacement( mReplacementMap );
911 return CreateAnimation( animationName, replacement, sourceActor );
914 Animation Builder::CreateAnimation( const std::string& animationName )
916 Replacement replacement( mReplacementMap );
918 return CreateAnimation( animationName, replacement, Dali::Stage::GetCurrent().GetRootLayer() );
921 void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::UIFormat format )
923 // parser to get constants and includes only
924 Dali::Toolkit::JsonParser parser = Dali::Toolkit::JsonParser::New();
926 if( !parser.Parse( data ) )
928 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
929 parser.GetErrorLineNumber(),
930 parser.GetErrorColumn(),
931 parser.GetErrorDescription().c_str() );
933 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
938 // load constant map (allows the user to override the constants in the json after loading)
939 LoadConstants( *parser.GetRoot(), mReplacementMap );
942 if( OptionalChild includes = IsChild(*parser.GetRoot(), KEYNAME_INCLUDES) )
944 Replacement replacer( mReplacementMap );
946 for(TreeNode::ConstIterator iter = (*includes).CBegin(); iter != (*includes).CEnd(); ++iter)
948 OptionalString filename = replacer.IsString( (*iter).second );
952 #if defined(DEBUG_ENABLED)
953 DALI_SCRIPT_VERBOSE("Loading Include '%s'\n", (*filename).c_str());
955 LoadFromString( GetFileContents(*filename) );
960 if( !mParser.Parse( data ) )
962 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
963 mParser.GetErrorLineNumber(),
964 mParser.GetErrorColumn(),
965 mParser.GetErrorDescription().c_str() );
967 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
971 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Cannot parse JSON");
975 void Builder::AddConstants( const Property::Map& map )
977 mReplacementMap.Merge( map );
980 void Builder::AddConstant( const std::string& key, const Property::Value& value )
982 mReplacementMap[key] = value;
985 const Property::Map& Builder::GetConstants() const
987 return mReplacementMap;
990 const Property::Value& Builder::GetConstant( const std::string& key ) const
992 Property::Value* match = mReplacementMap.Find( key );
999 static Property::Value invalid;
1004 void Builder::LoadConstants( const TreeNode& root, Property::Map& intoMap )
1006 Replacement replacer(intoMap);
1008 if( OptionalChild constants = IsChild(root, "constants") )
1010 for(TreeNode::ConstIterator iter = (*constants).CBegin();
1011 iter != (*constants).CEnd(); ++iter)
1013 Dali::Property::Value property;
1014 if( (*iter).second.GetName() )
1016 #if defined(DEBUG_ENABLED)
1017 DALI_SCRIPT_VERBOSE("Constant set from json '%s'\n", (*iter).second.GetName());
1019 if( SetPropertyFromNode( (*iter).second, property, replacer ) )
1021 intoMap[ (*iter).second.GetName() ] = property;
1025 DALI_SCRIPT_WARNING("Cannot convert property for constant %s\n",
1026 (*iter).second.GetName() == NULL ? "no name?" : (*iter).second.GetName());
1032 #if defined(DEBUG_ENABLED)
1033 Property::Value* iter = intoMap.Find( "CONFIG_SCRIPT_LOG_LEVEL" );
1034 if( iter && iter->GetType() == Property::STRING )
1036 std::string logLevel( iter->Get< std::string >() );
1037 if( logLevel == "NoLogging" )
1039 gFilterScript->SetLogLevel( Integration::Log::NoLogging );
1041 else if( logLevel == "Concise" )
1043 gFilterScript->SetLogLevel( Integration::Log::Concise );
1045 else if( logLevel == "General" )
1047 gFilterScript->SetLogLevel( Integration::Log::General );
1049 else if( logLevel == "Verbose" )
1051 gFilterScript->SetLogLevel( Integration::Log::Verbose );
1058 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle )
1060 Replacement replacer( mReplacementMap );
1061 return ApplyStyle( styleName, handle, replacer );
1064 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle, const Replacement& replacement )
1066 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1068 OptionalChild styles = IsChild( *mParser.GetRoot(), KEYNAME_STYLES );
1069 OptionalChild style = IsChild( *styles, styleName );
1071 if( styles && style )
1073 ApplyAllStyleProperties( *mParser.GetRoot(), *style, handle, replacement );
1082 BaseHandle Builder::Create( const std::string& templateName, const Property::Map& map )
1084 Replacement replacement( map, mReplacementMap );
1085 return Create( templateName, replacement );
1088 BaseHandle Builder::Create( const std::string& templateName, const Replacement& constant )
1090 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1092 BaseHandle baseHandle;
1094 OptionalChild templates = IsChild(*mParser.GetRoot(), KEYNAME_TEMPLATES);
1098 DALI_SCRIPT_WARNING("No template section found to CreateFromTemplate\n");
1102 OptionalChild childTemplate = IsChild(*templates, templateName);
1105 DALI_SCRIPT_WARNING("Template '%s' does not exist in template section\n", templateName.c_str());
1109 OptionalString type = constant.IsString( IsChild(*childTemplate, KEYNAME_TYPE) );
1113 DALI_SCRIPT_WARNING("Cannot create template '%s' as template section is missing 'type'\n", templateName.c_str());
1117 baseHandle = DoCreate( *mParser.GetRoot(), *childTemplate, Actor(), constant );
1125 BaseHandle Builder::CreateFromJson( const std::string& json )
1129 // merge in new template, hoping no one else has one named '@temp@'
1130 std::string newTemplate =
1131 std::string("{\"templates\":{\"@temp@\":") + \
1135 if( mParser.Parse(newTemplate) )
1137 Replacement replacement( mReplacementMap );
1138 ret = Create( "@temp@", replacement );
1144 bool Builder::ApplyFromJson( Handle& handle, const std::string& json )
1148 // merge new style, hoping no one else has one named '@temp@'
1149 std::string newStyle =
1150 std::string("{\"styles\":{\"@temp@\":") + \
1154 if( mParser.Parse(newStyle) )
1156 Replacement replacement( mReplacementMap );
1157 ret = ApplyStyle( "@temp@", handle, replacement );
1164 BaseHandle Builder::Create( const std::string& templateName )
1166 Replacement replacement( mReplacementMap );
1167 return Create( templateName, replacement );
1171 : mSlotDelegate( this )
1173 mParser = Dali::Toolkit::JsonParser::New();
1180 } // namespace Internal
1182 } // namespace Toolkit