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/object/property-array.h>
29 #include <dali/public-api/actors/layer.h>
30 #include <dali/public-api/actors/image-actor.h>
31 #include <dali/public-api/actors/camera-actor.h>
32 #include <dali/public-api/scripting/scripting.h>
33 #include <dali/integration-api/debug.h>
36 #include <dali-toolkit/public-api/controls/control.h>
37 #include <dali-toolkit/public-api/builder/json-parser.h>
39 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
40 #include <dali-toolkit/internal/builder/builder-filesystem.h>
41 #include <dali-toolkit/internal/builder/builder-declarations.h>
42 #include <dali-toolkit/internal/builder/replacement.h>
54 extern Animation CreateAnimation(const TreeNode& child, const Replacement& replacements, const Dali::Actor searchRoot, Builder* const builder );
55 extern bool SetPropertyFromNode( const TreeNode& node, Property::Value& value );
56 extern bool SetPropertyFromNode( const TreeNode& node, Property::Value& value, const Replacement& replacements );
57 extern bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value );
58 extern bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value, const Replacement& replacements );
59 extern Actor SetupSignalAction(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, boost::function<void (void)> quitAction, Dali::Toolkit::Internal::Builder* const builder);
60 extern Actor SetupPropertyNotification(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, boost::function<void (void)> quitAction, Dali::Toolkit::Internal::Builder* const builder);
61 extern Actor SetupActor( const TreeNode& node, Actor& actor, const Replacement& constant );
63 #if defined(DEBUG_ENABLED)
64 Integration::Log::Filter* gFilterScript = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_SCRIPT");
70 const std::string KEYNAME_STYLES = "styles";
71 const std::string KEYNAME_TYPE = "type";
72 const std::string KEYNAME_ACTORS = "actors";
73 const std::string KEYNAME_SIGNALS = "signals";
74 const std::string KEYNAME_NAME = "name";
75 const std::string KEYNAME_TEMPLATES = "templates";
76 const std::string KEYNAME_INCLUDES = "includes";
78 typedef std::vector<const TreeNode*> TreeNodeList;
81 std::string ToString(const T& value)
89 std::string ToString(const Rect<int>& value)
92 ss << value.x << "," << value.y << "," << value.width << "," << value.height;
96 #if defined(DEBUG_ENABLED)
98 std::string PropertyValueToString( const Property::Value& value )
102 switch( value.GetType() )
109 case Property::BOOLEAN:
111 ret = value.Get<bool>() ? "True" : "False";
114 case Property::FLOAT:
117 ret = ToString( value.Get<float>() );
120 case Property::INTEGER:
122 ret = ToString( value.Get<int>() );
125 case Property::UNSIGNED_INTEGER:
127 ret = ToString( value.Get<unsigned int>() );
130 case Property::VECTOR2:
132 ret = ToString( value.Get<Vector2>() );
135 case Property::VECTOR3:
137 ret = ToString( value.Get<Vector3>() );
140 case Property::VECTOR4:
142 ret = ToString( value.Get<Vector4>() );
145 case Property::MATRIX3:
147 ret = ToString( value.Get<Matrix3>() );
150 case Property::MATRIX:
152 ret = ToString( value.Get<Matrix>() );
155 case Property::RECTANGLE:
157 ret = ToString( value.Get< Rect<int> >() );
160 case Property::ROTATION:
164 case Property::STRING:
166 ret = value.Get<std::string>();
169 case Property::ARRAY:
171 ret = std::string("Array Size=") + ToString( value.Get<Property::Array>().Size() );
176 ret = std::string("Map Size=") + ToString( value.Get<Property::Map>().Count() );
179 case Property::TYPE_COUNT:
188 #endif // DEBUG_ENABLED
191 * Recursively collects all stylesin a node (An array of style names).
193 * stylesCollection The set of styles from the json file (a json object of named styles)
194 * style The style array to begin the collection from
195 * styleList The style list to add nodes to apply
197 void CollectAllStyles( const TreeNode& stylesCollection, const TreeNode& style, TreeNodeList& styleList )
199 // style is an array of style names
200 if( TreeNode::ARRAY == style.GetType() )
202 for(TreeNode::ConstIterator iter = style.CBegin(); iter != style.CEnd(); ++iter)
204 if( OptionalString styleName = IsString( (*iter).second ) )
206 if( OptionalChild node = IsChild( stylesCollection, *styleName) )
208 styleList.push_back( &(*node) );
210 if( OptionalChild subStyle = IsChild( *node, KEYNAME_STYLES ) )
212 CollectAllStyles( stylesCollection, *subStyle, styleList );
223 QuitAction( Builder& builder )
224 : mBuilder( builder )
228 void operator()(void)
230 mBuilder.EmitQuitSignal();
240 * Sets the handle properties found in the tree node
242 void Builder::SetProperties( const TreeNode& node, Handle& handle, const Replacement& constant )
246 for( TreeNode::ConstIterator iter = node.CBegin(); iter != node.CEnd(); ++iter )
248 const TreeNode::KeyNodePair& keyChild = *iter;
250 std::string key( keyChild.first );
252 // ignore special fields; type,actors,signals,styles
253 if(key == KEYNAME_TYPE || key == KEYNAME_ACTORS || key == KEYNAME_SIGNALS || key == KEYNAME_STYLES)
258 // special field 'image' usually contains an json object description
259 // although sometimes refers to a framebuffer
260 if( 0 == keyChild.second.Size() )
264 ImageActor imageActor = ImageActor::DownCast(handle);
267 if( OptionalString s = constant.IsString( keyChild.second ) )
269 FrameBufferImage fb = GetFrameBufferImage(*s, constant);
272 imageActor.SetImage( fb );
279 // special field 'effect' references the shader effect instances
282 RenderableActor actor = RenderableActor::DownCast(handle);
285 OptionalString str = constant.IsString( keyChild.second );
288 ShaderEffect effect = GetShaderEffect( *str, constant );
289 actor.SetShaderEffect(effect);
294 DALI_SCRIPT_WARNING("Could not find or set shader effect\n");
300 Handle propertyObject( handle );
302 Dali::Property::Index index = propertyObject.GetPropertyIndex( key );
304 if( Property::INVALID_INDEX == index )
306 RenderableActor actor = RenderableActor::DownCast(handle);
309 if( ShaderEffect effect = actor.GetShaderEffect() )
311 index = effect.GetPropertyIndex( key );
312 if(index != Property::INVALID_INDEX)
314 propertyObject = effect;
320 if( Property::INVALID_INDEX != index )
322 Property::Type type = propertyObject.GetPropertyType(index);
324 Property::Value value;
325 if( !SetPropertyFromNode( keyChild.second, type, value, constant ) )
327 // verbose as this might not be a problem
328 // eg parent-origin can be a string which is picked up later
329 DALI_SCRIPT_VERBOSE("Could not convert property:%s\n", key.c_str());
333 DALI_SCRIPT_VERBOSE("SetProperty '%s' Index=:%d Value Type=%d Value '%s'\n", key.c_str(), index, value.GetType(), PropertyValueToString(value).c_str() );
335 propertyObject.SetProperty( index, value );
340 DALI_SCRIPT_VERBOSE("SetProperty INVALID '%s' Index=:%d\n", key.c_str(), index);
343 } // for property nodes
347 DALI_SCRIPT_WARNING("Style applied to empty handle\n");
351 // Set properties from node on handle.
352 void Builder::ApplyProperties( const TreeNode& root, const TreeNode& node,
353 Dali::Handle& handle, const Replacement& constant )
355 if( Actor actor = Actor::DownCast(handle) )
357 SetProperties( node, actor, constant );
361 SetupActor( node, actor, constant );
364 QuitAction quitAction( *this );
365 SetupSignalAction( mSlotDelegate.GetConnectionTracker(), root, node, actor, quitAction, this );
366 SetupPropertyNotification( mSlotDelegate.GetConnectionTracker(), root, node, actor, quitAction, this );
371 SetProperties( node, handle, constant );
375 // Appling by style helper
376 // use FindChildByName() to apply properties referenced in KEYNAME_ACTORS in the node
377 void Builder::ApplyStylesByActor( const TreeNode& root, const TreeNode& node,
378 Dali::Handle& handle, const Replacement& constant )
380 if( Dali::Actor actor = Dali::Actor::DownCast( handle ) )
382 if( const TreeNode* actors = node.GetChild( KEYNAME_ACTORS ) )
384 // in a style the actor subtree properties referenced by actor name
385 for( TreeConstIter iter = actors->CBegin(); iter != actors->CEnd(); ++iter )
387 Dali::Actor foundActor;
391 foundActor = actor.FindChildByName( (*iter).first );
396 // debug log cannot find searched for actor
397 #if defined(DEBUG_ENABLED)
398 DALI_SCRIPT_VERBOSE("Cannot find actor in style application '%s'\n", (*iter).first);
403 #if defined(DEBUG_ENABLED)
404 DALI_SCRIPT_VERBOSE("Styles applied to actor '%s'\n", (*iter).first);
406 ApplyProperties( root, (*iter).second, foundActor, constant );
414 void Builder::ApplyAllStyleProperties( const TreeNode& root, const TreeNode& node,
415 Dali::Handle& handle, const Replacement& constant )
417 OptionalChild styles = IsChild(root, KEYNAME_STYLES);
418 OptionalChild style = IsChild(node, KEYNAME_STYLES);
420 if( styles && style )
422 TreeNodeList additionalStyles;
424 CollectAllStyles( *styles, *style, additionalStyles );
426 #if defined(DEBUG_ENABLED)
427 for(TreeNode::ConstIterator iter = (*style).CBegin(); iter != (*style).CEnd(); ++iter)
429 if( OptionalString styleName = IsString( (*iter).second ) )
431 DALI_SCRIPT_VERBOSE("Style Applied '%s'\n", (*styleName).c_str());
436 // a style may have other styles, which has other styles etc so we apply in reverse by convention.
437 for(TreeNodeList::reverse_iterator iter = additionalStyles.rbegin(); iter != additionalStyles.rend(); ++iter)
439 ApplyProperties( root, *(*iter), handle, constant );
441 ApplyStylesByActor( root, *(*iter), handle, constant );
445 // applying given node last
446 ApplyProperties( root, node, handle, constant );
448 ApplyStylesByActor( root, node, handle, constant );
454 * Create a dali type from a node.
455 * If parent given and an actor type was created then add it to the parent and
456 * recursively add nodes children.
458 BaseHandle Builder::DoCreate( const TreeNode& root, const TreeNode& node,
459 Actor parent, const Replacement& replacements )
461 BaseHandle baseHandle;
463 const TreeNode* templateNode = NULL;
465 if( OptionalString typeName = IsString(node, KEYNAME_TYPE) )
467 typeInfo = TypeRegistry::Get().GetTypeInfo( *typeName );
471 // a template name is also allowed inplace of the type name
472 OptionalChild templates = IsChild( root, KEYNAME_TEMPLATES);
476 if( OptionalChild isTemplate = IsChild( *templates, *typeName ) )
478 templateNode = &(*isTemplate);
480 if( OptionalString templateTypeName = IsString(*templateNode, KEYNAME_TYPE) )
482 typeInfo = TypeRegistry::Get().GetTypeInfo( *templateTypeName );
491 DALI_SCRIPT_WARNING("Cannot create Dali type from node '%s'\n", node.GetName());
495 baseHandle = typeInfo.CreateInstance();
496 Handle handle = Handle::DownCast(baseHandle);
497 Actor actor = Actor::DownCast(handle);
502 DALI_SCRIPT_VERBOSE("Create:%s\n", typeInfo.GetName().c_str());
504 #if defined(DEBUG_ENABLED)
507 DALI_SCRIPT_VERBOSE(" Is Handle Object=%d\n", (long*)handle.GetObjectPtr());
508 DALI_SCRIPT_VERBOSE(" Is Handle Property Count=%d\n", handle.GetPropertyCount());
513 DALI_SCRIPT_VERBOSE(" Is Actor id=%d\n", actor.GetId());
516 Toolkit::Control control = Toolkit::Control::DownCast(handle);
519 DALI_SCRIPT_VERBOSE(" Is Control id=%d\n", actor.GetId());
521 #endif // DEBUG_ENABLED
525 ApplyProperties( root, *templateNode, handle, replacements );
527 if( OptionalChild actors = IsChild( *templateNode, KEYNAME_ACTORS ) )
529 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
531 DoCreate( root, (*iter).second, actor, replacements );
538 // add children of all the styles
539 if( OptionalChild actors = IsChild( node, KEYNAME_ACTORS ) )
541 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
543 DoCreate( root, (*iter).second, actor, replacements );
547 // apply style on top as they need the children to exist
548 ApplyAllStyleProperties( root, node, actor, replacements );
550 // then add to parent
558 ApplyProperties( root, node, handle, replacements );
563 DALI_SCRIPT_WARNING("Cannot create handle from type '%s'\n", typeInfo.GetName().c_str());
570 void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replacement& constant )
572 const Stage& stage = Stage::GetCurrent();
573 Layer root = stage.GetRootLayer();
575 if( OptionalString s = constant.IsString( IsChild(node, "source-actor") ) )
577 Actor actor = root.FindChildByName(*s);
580 task.SetSourceActor( actor );
584 DALI_SCRIPT_WARNING("Cannot find source actor on stage for render task called '%s'\n", (*s).c_str() );
588 if( OptionalString s = constant.IsString( IsChild(node, "camera-actor") ) )
590 CameraActor actor = CameraActor::DownCast( root.FindChildByName(*s) );
593 task.SetCameraActor( actor );
597 DALI_SCRIPT_WARNING("Cannot find camera actor on stage for render task called '%s'\n", (*s).c_str() );
601 if( OptionalString s = constant.IsString( IsChild(node, "target-frame-buffer") ) )
603 FrameBufferImage fb = GetFrameBufferImage( *s, constant );
606 task.SetTargetFrameBuffer( fb );
610 DALI_SCRIPT_WARNING("Cannot find target frame buffer '%s'\n", (*s).c_str() );
614 if( OptionalString s = constant.IsString( IsChild(node, "screen-to-frame-buffer-function") ) )
616 if("DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION" == *s)
618 task.SetScreenToFrameBufferFunction( RenderTask::DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION );
620 else if("FULLSCREEN_FRAMEBUFFER_FUNCTION" == *s)
622 task.SetScreenToFrameBufferFunction( RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION );
626 DALI_SCRIPT_WARNING("todo");
630 // other setup is via the property system
631 SetProperties( node, task, constant ); // @ todo, remove 'source-actor', 'camera-actor'?
635 void Builder::CreateRenderTask( const std::string &name )
637 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
639 Replacement constant(mReplacementMap);
641 const Stage& stage = Stage::GetCurrent();
643 OptionalChild tasks = IsChild(*mParser.GetRoot(), "render-tasks");
648 // Create the tasks from the current task as generally we want
649 // to setup task zero and onwards. Although this does overwrite
650 // the properties of the current task.
652 if( OptionalChild renderTask = IsChild(*tasks, name ) )
654 RenderTaskList list = stage.GetRenderTaskList();
655 unsigned int start = list.GetTaskCount();
660 // zero should have already been created by the stage so really
661 // this case should never happen
662 task = list.CreateTask();
666 TreeNode::ConstIterator iter = (*renderTask).CBegin();
667 task = list.GetTask( start - 1 );
669 SetupTask( task, (*iter).second, constant );
673 for(; iter != (*renderTask).CEnd(); ++iter )
675 task = list.CreateTask();
676 SetupTask( task, (*iter).second, constant );
682 ShaderEffect Builder::GetShaderEffect( const std::string &name)
684 Replacement constant( mReplacementMap );
685 return GetShaderEffect( name, constant );
688 ShaderEffect Builder::GetShaderEffect( const std::string &name, const Replacement& constant )
690 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
694 ShaderEffectLut::const_iterator iter( mShaderEffectLut.find( name ) );
695 if( iter != mShaderEffectLut.end() )
701 if( OptionalChild effects = IsChild( *mParser.GetRoot(), "shader-effects") )
703 if( OptionalChild effect = IsChild( *effects, name ) )
705 Dali::Property::Value propertyMap(Property::MAP);
706 if( SetPropertyFromNode( *effect, Property::MAP, propertyMap, constant ) )
708 ret = Dali::Scripting::NewShaderEffect( propertyMap );
709 mShaderEffectLut[ name ] = ret;
718 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name )
720 Replacement constant( mReplacementMap );
721 return GetFrameBufferImage(name, constant);
724 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name, const Replacement& constant )
726 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
728 FrameBufferImage ret;
730 ImageLut::const_iterator iter( mFrameBufferImageLut.find( name ) );
731 if( iter != mFrameBufferImageLut.end() )
737 if( OptionalChild images = IsChild( *mParser.GetRoot(), "frame-buffer-images") )
739 if( OptionalChild image = IsChild( *images, name ) )
741 Dali::Property::Value propertyMap(Property::MAP);
742 if( SetPropertyFromNode( *image, Property::MAP, propertyMap, constant ) )
744 propertyMap.SetValue(KEYNAME_TYPE, Property::Value(std::string("FrameBufferImage")));
745 ret = FrameBufferImage::DownCast( Dali::Scripting::NewImage( propertyMap ) );
746 mFrameBufferImageLut[ name ] = ret;
755 Path Builder::GetPath( const std::string& name )
757 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
761 PathLut::const_iterator iter( mPathLut.find( name ) );
762 if( iter != mPathLut.end() )
768 if( OptionalChild paths = IsChild( *mParser.GetRoot(), "paths") )
770 if( OptionalChild path = IsChild( *paths, name ) )
773 if( OptionalChild pointsProperty = IsChild( *path, "points") )
775 Dali::Property::Value points(Property::ARRAY);
776 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
779 ret.SetProperty( Path::Property::POINTS, points);
781 //control-points property
782 if( OptionalChild pointsProperty = IsChild( *path, "control-points") )
784 Dali::Property::Value points(Property::ARRAY);
785 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
787 ret.SetProperty( Path::Property::CONTROL_POINTS, points);
793 float curvature(0.25f);
794 if( OptionalFloat pointsProperty = IsFloat( *path, "curvature") )
796 curvature = *pointsProperty;
798 ret.GenerateControlPoints(curvature);
801 //Add the new path to the hash table for paths
802 mPathLut[ name ] = ret;
807 //Interpolation points not specified
808 DALI_SCRIPT_WARNING("Interpolation points not specified for path '%s'\n", name.c_str() );
818 Toolkit::Builder::BuilderSignalType& Builder::QuitSignal()
823 void Builder::EmitQuitSignal()
828 void Builder::AddActors( Actor toActor )
830 // 'stage' is the default/by convention section to add from
831 AddActors( "stage", toActor );
834 void Builder::AddActors( const std::string §ionName, Actor toActor )
836 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
838 Property::Map overrideMap;
839 Replacement replacements(overrideMap, mReplacementMap);
841 OptionalChild add = IsChild(*mParser.GetRoot(), sectionName);
845 for( TreeNode::ConstIterator iter = (*add).CBegin(); iter != (*add).CEnd(); ++iter )
847 // empty actor adds directly to the stage
848 BaseHandle baseHandle = DoCreate( *mParser.GetRoot(), (*iter).second, Actor(), replacements );
849 Actor actor = Actor::DownCast(baseHandle);
852 toActor.Add( actor );
856 // if were adding the 'stage' section then also check for a render task called stage
857 // to add automatically
858 if( "stage" == sectionName )
860 if( OptionalChild renderTasks = IsChild(*mParser.GetRoot(), "render-tasks") )
862 if( OptionalChild tasks = IsChild(*renderTasks, "stage") )
864 CreateRenderTask( "stage" );
871 Animation Builder::CreateAnimation( const std::string& animationName, const Replacement& replacement, Dali::Actor sourceActor )
873 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
877 if( OptionalChild animations = IsChild(*mParser.GetRoot(), "animations") )
879 if( OptionalChild animation = IsChild(*animations, animationName) )
881 anim = Dali::Toolkit::Internal::CreateAnimation( *animation, replacement, sourceActor, this );
885 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed\n", animationName.c_str() );
890 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed (no animation section)\n", animationName.c_str() );
896 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor )
898 Replacement replacement(map, mReplacementMap);
899 return CreateAnimation( animationName, replacement, sourceActor);
902 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map )
904 Replacement replacement(map, mReplacementMap);
905 return CreateAnimation( animationName, replacement, Stage::GetCurrent().GetRootLayer() );
908 Animation Builder::CreateAnimation( const std::string& animationName, Dali::Actor sourceActor )
910 Replacement replacement( mReplacementMap );
912 return CreateAnimation( animationName, replacement, sourceActor );
915 Animation Builder::CreateAnimation( const std::string& animationName )
917 Replacement replacement( mReplacementMap );
919 return CreateAnimation( animationName, replacement, Dali::Stage::GetCurrent().GetRootLayer() );
922 void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::UIFormat format )
924 // parser to get constants and includes only
925 Dali::Toolkit::JsonParser parser = Dali::Toolkit::JsonParser::New();
927 if( !parser.Parse( data ) )
929 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
930 parser.GetErrorLineNumber(),
931 parser.GetErrorColumn(),
932 parser.GetErrorDescription().c_str() );
934 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
939 // load constant map (allows the user to override the constants in the json after loading)
940 LoadConstants( *parser.GetRoot(), mReplacementMap );
943 if( OptionalChild includes = IsChild(*parser.GetRoot(), KEYNAME_INCLUDES) )
945 Replacement replacer( mReplacementMap );
947 for(TreeNode::ConstIterator iter = (*includes).CBegin(); iter != (*includes).CEnd(); ++iter)
949 OptionalString filename = replacer.IsString( (*iter).second );
953 #if defined(DEBUG_ENABLED)
954 DALI_SCRIPT_VERBOSE("Loading Include '%s'\n", (*filename).c_str());
956 LoadFromString( GetFileContents(*filename) );
961 if( !mParser.Parse( data ) )
963 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
964 mParser.GetErrorLineNumber(),
965 mParser.GetErrorColumn(),
966 mParser.GetErrorDescription().c_str() );
968 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
972 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Cannot parse JSON");
976 void Builder::AddConstants( const Property::Map& map )
978 mReplacementMap.Merge( map );
981 void Builder::AddConstant( const std::string& key, const Property::Value& value )
983 mReplacementMap[key] = value;
986 const Property::Map& Builder::GetConstants() const
988 return mReplacementMap;
991 const Property::Value& Builder::GetConstant( const std::string& key ) const
993 Property::Value* match = mReplacementMap.Find( key );
1000 static Property::Value invalid;
1005 void Builder::LoadConstants( const TreeNode& root, Property::Map& intoMap )
1007 Replacement replacer(intoMap);
1009 if( OptionalChild constants = IsChild(root, "constants") )
1011 for(TreeNode::ConstIterator iter = (*constants).CBegin();
1012 iter != (*constants).CEnd(); ++iter)
1014 Dali::Property::Value property;
1015 if( (*iter).second.GetName() )
1017 #if defined(DEBUG_ENABLED)
1018 DALI_SCRIPT_VERBOSE("Constant set from json '%s'\n", (*iter).second.GetName());
1020 if( SetPropertyFromNode( (*iter).second, property, replacer ) )
1022 intoMap[ (*iter).second.GetName() ] = property;
1026 DALI_SCRIPT_WARNING("Cannot convert property for constant %s\n",
1027 (*iter).second.GetName() == NULL ? "no name?" : (*iter).second.GetName());
1033 #if defined(DEBUG_ENABLED)
1034 Property::Value* iter = intoMap.Find( "CONFIG_SCRIPT_LOG_LEVEL" );
1035 if( iter && iter->GetType() == Property::STRING )
1037 std::string logLevel( iter->Get< std::string >() );
1038 if( logLevel == "NoLogging" )
1040 gFilterScript->SetLogLevel( Integration::Log::NoLogging );
1042 else if( logLevel == "Concise" )
1044 gFilterScript->SetLogLevel( Integration::Log::Concise );
1046 else if( logLevel == "General" )
1048 gFilterScript->SetLogLevel( Integration::Log::General );
1050 else if( logLevel == "Verbose" )
1052 gFilterScript->SetLogLevel( Integration::Log::Verbose );
1059 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle )
1061 Replacement replacer( mReplacementMap );
1062 return ApplyStyle( styleName, handle, replacer );
1065 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle, const Replacement& replacement )
1067 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1069 OptionalChild styles = IsChild( *mParser.GetRoot(), KEYNAME_STYLES );
1070 OptionalChild style = IsChild( *styles, styleName );
1072 if( styles && style )
1074 ApplyAllStyleProperties( *mParser.GetRoot(), *style, handle, replacement );
1083 BaseHandle Builder::Create( const std::string& templateName, const Property::Map& map )
1085 Replacement replacement( map, mReplacementMap );
1086 return Create( templateName, replacement );
1089 BaseHandle Builder::Create( const std::string& templateName, const Replacement& constant )
1091 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1093 BaseHandle baseHandle;
1095 OptionalChild templates = IsChild(*mParser.GetRoot(), KEYNAME_TEMPLATES);
1099 DALI_SCRIPT_WARNING("No template section found to CreateFromTemplate\n");
1103 OptionalChild childTemplate = IsChild(*templates, templateName);
1106 DALI_SCRIPT_WARNING("Template '%s' does not exist in template section\n", templateName.c_str());
1110 OptionalString type = constant.IsString( IsChild(*childTemplate, KEYNAME_TYPE) );
1114 DALI_SCRIPT_WARNING("Cannot create template '%s' as template section is missing 'type'\n", templateName.c_str());
1118 baseHandle = DoCreate( *mParser.GetRoot(), *childTemplate, Actor(), constant );
1126 BaseHandle Builder::CreateFromJson( const std::string& json )
1130 // merge in new template, hoping no one else has one named '@temp@'
1131 std::string newTemplate =
1132 std::string("{\"templates\":{\"@temp@\":") + \
1136 if( mParser.Parse(newTemplate) )
1138 Replacement replacement( mReplacementMap );
1139 ret = Create( "@temp@", replacement );
1145 bool Builder::ApplyFromJson( Handle& handle, const std::string& json )
1149 // merge new style, hoping no one else has one named '@temp@'
1150 std::string newStyle =
1151 std::string("{\"styles\":{\"@temp@\":") + \
1155 if( mParser.Parse(newStyle) )
1157 Replacement replacement( mReplacementMap );
1158 ret = ApplyStyle( "@temp@", handle, replacement );
1165 BaseHandle Builder::Create( const std::string& templateName )
1167 Replacement replacement( mReplacementMap );
1168 return Create( templateName, replacement );
1172 : mSlotDelegate( this )
1174 mParser = Dali::Toolkit::JsonParser::New();
1181 } // namespace Internal
1183 } // namespace Toolkit