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 RenderableActor actor = RenderableActor::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 RenderableActor actor = RenderableActor::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 // TEMP: Assume all script created actors are not using size negotiation for now
538 actor.SetRelayoutEnabled( false );
540 // add children of all the styles
541 if( OptionalChild actors = IsChild( node, KEYNAME_ACTORS ) )
543 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
545 DoCreate( root, (*iter).second, actor, replacements );
549 // apply style on top as they need the children to exist
550 ApplyAllStyleProperties( root, node, actor, replacements );
552 // then add to parent
560 ApplyProperties( root, node, handle, replacements );
565 DALI_SCRIPT_WARNING("Cannot create handle from type '%s'\n", typeInfo.GetName().c_str());
572 void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replacement& constant )
574 const Stage& stage = Stage::GetCurrent();
575 Layer root = stage.GetRootLayer();
577 if( OptionalString s = constant.IsString( IsChild(node, "source-actor") ) )
579 Actor actor = root.FindChildByName(*s);
582 task.SetSourceActor( actor );
586 DALI_SCRIPT_WARNING("Cannot find source actor on stage for render task called '%s'\n", (*s).c_str() );
590 if( OptionalString s = constant.IsString( IsChild(node, "camera-actor") ) )
592 CameraActor actor = CameraActor::DownCast( root.FindChildByName(*s) );
595 task.SetCameraActor( actor );
599 DALI_SCRIPT_WARNING("Cannot find camera actor on stage for render task called '%s'\n", (*s).c_str() );
603 if( OptionalString s = constant.IsString( IsChild(node, "target-frame-buffer") ) )
605 FrameBufferImage fb = GetFrameBufferImage( *s, constant );
608 task.SetTargetFrameBuffer( fb );
612 DALI_SCRIPT_WARNING("Cannot find target frame buffer '%s'\n", (*s).c_str() );
616 if( OptionalString s = constant.IsString( IsChild(node, "screen-to-frame-buffer-function") ) )
618 if("DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION" == *s)
620 task.SetScreenToFrameBufferFunction( RenderTask::DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION );
622 else if("FULLSCREEN_FRAMEBUFFER_FUNCTION" == *s)
624 task.SetScreenToFrameBufferFunction( RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION );
628 DALI_SCRIPT_WARNING("todo");
632 // other setup is via the property system
633 SetProperties( node, task, constant ); // @ todo, remove 'source-actor', 'camera-actor'?
637 void Builder::CreateRenderTask( const std::string &name )
639 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
641 Replacement constant(mReplacementMap);
643 const Stage& stage = Stage::GetCurrent();
645 OptionalChild tasks = IsChild(*mParser.GetRoot(), "render-tasks");
650 // Create the tasks from the current task as generally we want
651 // to setup task zero and onwards. Although this does overwrite
652 // the properties of the current task.
654 if( OptionalChild renderTask = IsChild(*tasks, name ) )
656 RenderTaskList list = stage.GetRenderTaskList();
657 unsigned int start = list.GetTaskCount();
662 // zero should have already been created by the stage so really
663 // this case should never happen
664 task = list.CreateTask();
668 TreeNode::ConstIterator iter = (*renderTask).CBegin();
669 task = list.GetTask( start - 1 );
671 SetupTask( task, (*iter).second, constant );
675 for(; iter != (*renderTask).CEnd(); ++iter )
677 task = list.CreateTask();
678 SetupTask( task, (*iter).second, constant );
684 ShaderEffect Builder::GetShaderEffect( const std::string &name)
686 Replacement constant( mReplacementMap );
687 return GetShaderEffect( name, constant );
690 ShaderEffect Builder::GetShaderEffect( const std::string &name, const Replacement& constant )
692 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
696 ShaderEffectLut::const_iterator iter( mShaderEffectLut.find( name ) );
697 if( iter != mShaderEffectLut.end() )
703 if( OptionalChild effects = IsChild( *mParser.GetRoot(), "shader-effects") )
705 if( OptionalChild effect = IsChild( *effects, name ) )
707 Dali::Property::Value propertyMap(Property::MAP);
708 if( SetPropertyFromNode( *effect, Property::MAP, propertyMap, constant ) )
710 ret = Dali::Scripting::NewShaderEffect( propertyMap );
711 mShaderEffectLut[ name ] = ret;
720 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name )
722 Replacement constant( mReplacementMap );
723 return GetFrameBufferImage(name, constant);
726 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name, const Replacement& constant )
728 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
730 FrameBufferImage ret;
732 ImageLut::const_iterator iter( mFrameBufferImageLut.find( name ) );
733 if( iter != mFrameBufferImageLut.end() )
739 if( OptionalChild images = IsChild( *mParser.GetRoot(), "frame-buffer-images") )
741 if( OptionalChild image = IsChild( *images, name ) )
743 Dali::Property::Value propertyMap(Property::MAP);
744 if( SetPropertyFromNode( *image, Property::MAP, propertyMap, constant ) )
746 propertyMap.SetValue(KEYNAME_TYPE, Property::Value(std::string("FrameBufferImage")));
747 ret = FrameBufferImage::DownCast( Dali::Scripting::NewImage( propertyMap ) );
748 mFrameBufferImageLut[ name ] = ret;
757 Path Builder::GetPath( const std::string& name )
759 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
763 PathLut::const_iterator iter( mPathLut.find( name ) );
764 if( iter != mPathLut.end() )
770 if( OptionalChild paths = IsChild( *mParser.GetRoot(), "paths") )
772 if( OptionalChild path = IsChild( *paths, name ) )
775 if( OptionalChild pointsProperty = IsChild( *path, "points") )
777 Dali::Property::Value points(Property::ARRAY);
778 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
781 ret.SetProperty( Path::Property::POINTS, points);
783 //control-points property
784 if( OptionalChild pointsProperty = IsChild( *path, "control-points") )
786 Dali::Property::Value points(Property::ARRAY);
787 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
789 ret.SetProperty( Path::Property::CONTROL_POINTS, points);
795 float curvature(0.25f);
796 if( OptionalFloat pointsProperty = IsFloat( *path, "curvature") )
798 curvature = *pointsProperty;
800 ret.GenerateControlPoints(curvature);
803 //Add the new path to the hash table for paths
804 mPathLut[ name ] = ret;
809 //Interpolation points not specified
810 DALI_SCRIPT_WARNING("Interpolation points not specified for path '%s'\n", name.c_str() );
820 Toolkit::Builder::BuilderSignalType& Builder::QuitSignal()
825 void Builder::EmitQuitSignal()
830 void Builder::AddActors( Actor toActor )
832 // 'stage' is the default/by convention section to add from
833 AddActors( "stage", toActor );
836 void Builder::AddActors( const std::string §ionName, Actor toActor )
838 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
840 Property::Map overrideMap;
841 Replacement replacements(overrideMap, mReplacementMap);
843 OptionalChild add = IsChild(*mParser.GetRoot(), sectionName);
847 for( TreeNode::ConstIterator iter = (*add).CBegin(); iter != (*add).CEnd(); ++iter )
849 // empty actor adds directly to the stage
850 BaseHandle baseHandle = DoCreate( *mParser.GetRoot(), (*iter).second, Actor(), replacements );
851 Actor actor = Actor::DownCast(baseHandle);
854 toActor.Add( actor );
858 // if were adding the 'stage' section then also check for a render task called stage
859 // to add automatically
860 if( "stage" == sectionName )
862 if( OptionalChild renderTasks = IsChild(*mParser.GetRoot(), "render-tasks") )
864 if( OptionalChild tasks = IsChild(*renderTasks, "stage") )
866 CreateRenderTask( "stage" );
873 Animation Builder::CreateAnimation( const std::string& animationName, const Replacement& replacement, Dali::Actor sourceActor )
875 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
879 if( OptionalChild animations = IsChild(*mParser.GetRoot(), "animations") )
881 if( OptionalChild animation = IsChild(*animations, animationName) )
883 anim = Dali::Toolkit::Internal::CreateAnimation( *animation, replacement, sourceActor, this );
887 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed\n", animationName.c_str() );
892 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed (no animation section)\n", animationName.c_str() );
898 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor )
900 Replacement replacement(map, mReplacementMap);
901 return CreateAnimation( animationName, replacement, sourceActor);
904 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map )
906 Replacement replacement(map, mReplacementMap);
907 return CreateAnimation( animationName, replacement, Stage::GetCurrent().GetRootLayer() );
910 Animation Builder::CreateAnimation( const std::string& animationName, Dali::Actor sourceActor )
912 Replacement replacement( mReplacementMap );
914 return CreateAnimation( animationName, replacement, sourceActor );
917 Animation Builder::CreateAnimation( const std::string& animationName )
919 Replacement replacement( mReplacementMap );
921 return CreateAnimation( animationName, replacement, Dali::Stage::GetCurrent().GetRootLayer() );
924 void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::UIFormat format )
926 // parser to get constants and includes only
927 Dali::Toolkit::JsonParser parser = Dali::Toolkit::JsonParser::New();
929 if( !parser.Parse( data ) )
931 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
932 parser.GetErrorLineNumber(),
933 parser.GetErrorColumn(),
934 parser.GetErrorDescription().c_str() );
936 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
941 // load constant map (allows the user to override the constants in the json after loading)
942 LoadConstants( *parser.GetRoot(), mReplacementMap );
945 if( OptionalChild includes = IsChild(*parser.GetRoot(), KEYNAME_INCLUDES) )
947 Replacement replacer( mReplacementMap );
949 for(TreeNode::ConstIterator iter = (*includes).CBegin(); iter != (*includes).CEnd(); ++iter)
951 OptionalString filename = replacer.IsString( (*iter).second );
955 #if defined(DEBUG_ENABLED)
956 DALI_SCRIPT_VERBOSE("Loading Include '%s'\n", (*filename).c_str());
958 LoadFromString( GetFileContents(*filename) );
963 if( !mParser.Parse( data ) )
965 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
966 mParser.GetErrorLineNumber(),
967 mParser.GetErrorColumn(),
968 mParser.GetErrorDescription().c_str() );
970 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
974 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Cannot parse JSON");
978 void Builder::AddConstants( const Property::Map& map )
980 mReplacementMap.Merge( map );
983 void Builder::AddConstant( const std::string& key, const Property::Value& value )
985 mReplacementMap[key] = value;
988 const Property::Map& Builder::GetConstants() const
990 return mReplacementMap;
993 const Property::Value& Builder::GetConstant( const std::string& key ) const
995 Property::Value* match = mReplacementMap.Find( key );
1002 static Property::Value invalid;
1007 void Builder::LoadConstants( const TreeNode& root, Property::Map& intoMap )
1009 Replacement replacer(intoMap);
1011 if( OptionalChild constants = IsChild(root, "constants") )
1013 for(TreeNode::ConstIterator iter = (*constants).CBegin();
1014 iter != (*constants).CEnd(); ++iter)
1016 Dali::Property::Value property;
1017 if( (*iter).second.GetName() )
1019 #if defined(DEBUG_ENABLED)
1020 DALI_SCRIPT_VERBOSE("Constant set from json '%s'\n", (*iter).second.GetName());
1022 if( SetPropertyFromNode( (*iter).second, property, replacer ) )
1024 intoMap[ (*iter).second.GetName() ] = property;
1028 DALI_SCRIPT_WARNING("Cannot convert property for constant %s\n",
1029 (*iter).second.GetName() == NULL ? "no name?" : (*iter).second.GetName());
1035 #if defined(DEBUG_ENABLED)
1036 Property::Value* iter = intoMap.Find( "CONFIG_SCRIPT_LOG_LEVEL" );
1037 if( iter && iter->GetType() == Property::STRING )
1039 std::string logLevel( iter->Get< std::string >() );
1040 if( logLevel == "NoLogging" )
1042 gFilterScript->SetLogLevel( Integration::Log::NoLogging );
1044 else if( logLevel == "Concise" )
1046 gFilterScript->SetLogLevel( Integration::Log::Concise );
1048 else if( logLevel == "General" )
1050 gFilterScript->SetLogLevel( Integration::Log::General );
1052 else if( logLevel == "Verbose" )
1054 gFilterScript->SetLogLevel( Integration::Log::Verbose );
1061 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle )
1063 Replacement replacer( mReplacementMap );
1064 return ApplyStyle( styleName, handle, replacer );
1067 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle, const Replacement& replacement )
1069 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1071 OptionalChild styles = IsChild( *mParser.GetRoot(), KEYNAME_STYLES );
1072 OptionalChild style = IsChild( *styles, styleName );
1074 if( styles && style )
1076 ApplyAllStyleProperties( *mParser.GetRoot(), *style, handle, replacement );
1085 BaseHandle Builder::Create( const std::string& templateName, const Property::Map& map )
1087 Replacement replacement( map, mReplacementMap );
1088 return Create( templateName, replacement );
1091 BaseHandle Builder::Create( const std::string& templateName, const Replacement& constant )
1093 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1095 BaseHandle baseHandle;
1097 OptionalChild templates = IsChild(*mParser.GetRoot(), KEYNAME_TEMPLATES);
1101 DALI_SCRIPT_WARNING("No template section found to CreateFromTemplate\n");
1105 OptionalChild childTemplate = IsChild(*templates, templateName);
1108 DALI_SCRIPT_WARNING("Template '%s' does not exist in template section\n", templateName.c_str());
1112 OptionalString type = constant.IsString( IsChild(*childTemplate, KEYNAME_TYPE) );
1116 DALI_SCRIPT_WARNING("Cannot create template '%s' as template section is missing 'type'\n", templateName.c_str());
1120 baseHandle = DoCreate( *mParser.GetRoot(), *childTemplate, Actor(), constant );
1128 BaseHandle Builder::CreateFromJson( const std::string& json )
1132 // merge in new template, hoping no one else has one named '@temp@'
1133 std::string newTemplate =
1134 std::string("{\"templates\":{\"@temp@\":") + \
1138 if( mParser.Parse(newTemplate) )
1140 Replacement replacement( mReplacementMap );
1141 ret = Create( "@temp@", replacement );
1147 bool Builder::ApplyFromJson( Handle& handle, const std::string& json )
1151 // merge new style, hoping no one else has one named '@temp@'
1152 std::string newStyle =
1153 std::string("{\"styles\":{\"@temp@\":") + \
1157 if( mParser.Parse(newStyle) )
1159 Replacement replacement( mReplacementMap );
1160 ret = ApplyStyle( "@temp@", handle, replacement );
1167 BaseHandle Builder::Create( const std::string& templateName )
1169 Replacement replacement( mReplacementMap );
1170 return Create( templateName, replacement );
1174 : mSlotDelegate( this )
1176 mParser = Dali::Toolkit::JsonParser::New();
1183 } // namespace Internal
1185 } // namespace Toolkit