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>
27 #include <dali/integration-api/debug.h>
29 #include <dali-toolkit/public-api/controls/control.h>
30 #include <dali-toolkit/public-api/builder/json-parser.h>
32 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
33 #include <dali-toolkit/internal/builder/builder-filesystem.h>
34 #include <dali-toolkit/internal/builder/builder-declarations.h>
35 #include <dali-toolkit/internal/builder/replacement.h>
47 extern Animation CreateAnimation(const TreeNode& child, const Replacement& replacements, const Dali::Actor searchRoot, Builder* const builder );
48 extern bool SetPropertyFromNode( const TreeNode& node, Property::Value& value );
49 extern bool SetPropertyFromNode( const TreeNode& node, Property::Value& value, const Replacement& replacements );
50 extern bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value );
51 extern bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value, const Replacement& replacements );
52 extern Actor SetupSignalAction(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, boost::function<void (void)> quitAction, Dali::Toolkit::Internal::Builder* const builder);
53 extern Actor SetupPropertyNotification(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, boost::function<void (void)> quitAction, Dali::Toolkit::Internal::Builder* const builder);
54 extern Actor SetupActor( const TreeNode& node, Actor& actor, const Replacement& constant );
56 #if defined(DEBUG_ENABLED)
57 Integration::Log::Filter* gFilterScript = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_SCRIPT");
63 const std::string KEYNAME_STYLES = "styles";
64 const std::string KEYNAME_TYPE = "type";
65 const std::string KEYNAME_ACTORS = "actors";
66 const std::string KEYNAME_SIGNALS = "signals";
67 const std::string KEYNAME_NAME = "name";
68 const std::string KEYNAME_TEMPLATES = "templates";
69 const std::string KEYNAME_INCLUDES = "includes";
71 typedef std::vector<const TreeNode*> TreeNodeList;
74 std::string ToString(const T& value)
82 std::string ToString(const Rect<int>& value)
85 ss << value.x << "," << value.y << "," << value.width << "," << value.height;
89 #if defined(DEBUG_ENABLED)
91 std::string PropertyValueToString( const Property::Value& value )
95 switch( value.GetType() )
102 case Property::BOOLEAN:
104 ret = value.Get<bool>() ? "True" : "False";
107 case Property::FLOAT:
110 ret = ToString( value.Get<float>() );
113 case Property::INTEGER:
115 ret = ToString( value.Get<int>() );
118 case Property::UNSIGNED_INTEGER:
120 ret = ToString( value.Get<unsigned int>() );
123 case Property::VECTOR2:
125 ret = ToString( value.Get<Vector2>() );
128 case Property::VECTOR3:
130 ret = ToString( value.Get<Vector3>() );
133 case Property::VECTOR4:
135 ret = ToString( value.Get<Vector4>() );
138 case Property::MATRIX3:
140 ret = ToString( value.Get<Matrix3>() );
143 case Property::MATRIX:
145 ret = ToString( value.Get<Matrix>() );
148 case Property::RECTANGLE:
150 ret = ToString( value.Get< Rect<int> >() );
153 case Property::ROTATION:
157 case Property::STRING:
159 ret = value.Get<std::string>();
162 case Property::ARRAY:
164 ret = std::string("Array Size=") + ToString( value.Get<Property::Array>().size() );
169 ret = std::string("Map Size=") + ToString( value.Get<Property::Map>().Count() );
172 case Property::TYPE_COUNT:
181 #endif // DEBUG_ENABLED
184 * Recursively collects all stylesin a node (An array of style names).
186 * stylesCollection The set of styles from the json file (a json object of named styles)
187 * style The style array to begin the collection from
188 * styleList The style list to add nodes to apply
190 void CollectAllStyles( const TreeNode& stylesCollection, const TreeNode& style, TreeNodeList& styleList )
192 // style is an array of style names
193 if( TreeNode::ARRAY == style.GetType() )
195 for(TreeNode::ConstIterator iter = style.CBegin(); iter != style.CEnd(); ++iter)
197 if( OptionalString styleName = IsString( (*iter).second ) )
199 if( OptionalChild node = IsChild( stylesCollection, *styleName) )
201 styleList.push_back( &(*node) );
203 if( OptionalChild subStyle = IsChild( *node, KEYNAME_STYLES ) )
205 CollectAllStyles( stylesCollection, *subStyle, styleList );
216 QuitAction( Builder& builder )
217 : mBuilder( builder )
221 void operator()(void)
223 mBuilder.EmitQuitSignal();
233 * Sets the handle properties found in the tree node
235 void Builder::SetProperties( const TreeNode& node, Handle& handle, const Replacement& constant )
239 for( TreeNode::ConstIterator iter = node.CBegin(); iter != node.CEnd(); ++iter )
241 const TreeNode::KeyNodePair& keyChild = *iter;
243 std::string key( keyChild.first );
245 // ignore special fields; type,actors,signals,styles
246 if(key == KEYNAME_TYPE || key == KEYNAME_ACTORS || key == KEYNAME_SIGNALS || key == KEYNAME_STYLES)
251 // special field 'image' usually contains an json object description
252 // although sometimes refers to a framebuffer
253 if( 0 == keyChild.second.Size() )
257 ImageActor imageActor = ImageActor::DownCast(handle);
260 if( OptionalString s = constant.IsString( keyChild.second ) )
262 FrameBufferImage fb = GetFrameBufferImage(*s, constant);
265 imageActor.SetImage( fb );
272 // special field 'effect' references the shader effect instances
275 RenderableActor actor = RenderableActor::DownCast(handle);
278 OptionalString str = constant.IsString( keyChild.second );
281 ShaderEffect effect = GetShaderEffect( *str, constant );
282 actor.SetShaderEffect(effect);
287 DALI_SCRIPT_WARNING("Could not find or set shader effect\n");
293 Handle propertyObject( handle );
295 Property::Index index = propertyObject.GetPropertyIndex( key );
297 if( Property::INVALID_INDEX == index )
299 RenderableActor actor = RenderableActor::DownCast(handle);
302 if( ShaderEffect effect = actor.GetShaderEffect() )
304 index = effect.GetPropertyIndex( key );
305 if(index != Property::INVALID_INDEX)
307 propertyObject = effect;
313 if( Property::INVALID_INDEX != index )
315 Property::Type type = propertyObject.GetPropertyType(index);
317 Property::Value value;
318 if( !SetPropertyFromNode( keyChild.second, type, value, constant ) )
320 // verbose as this might not be a problem
321 // eg parent-origin can be a string which is picked up later
322 DALI_SCRIPT_VERBOSE("Could not convert property:%s\n", key.c_str());
326 DALI_SCRIPT_VERBOSE("SetProperty '%s' Index=:%d Value Type=%d Value '%s'\n", key.c_str(), index, value.GetType(), PropertyValueToString(value).c_str() );
328 propertyObject.SetProperty( index, value );
333 DALI_SCRIPT_VERBOSE("SetProperty INVALID '%s' Index=:%d\n", key.c_str(), index);
336 } // for property nodes
340 DALI_SCRIPT_WARNING("Style applied to empty handle\n");
344 // Set properties from node on handle.
345 void Builder::ApplyProperties( const TreeNode& root, const TreeNode& node,
346 Dali::Handle& handle, const Replacement& constant )
348 if( Actor actor = Actor::DownCast(handle) )
350 SetProperties( node, actor, constant );
354 SetupActor( node, actor, constant );
357 QuitAction quitAction( *this );
358 SetupSignalAction( mSlotDelegate.GetConnectionTracker(), root, node, actor, quitAction, this );
359 SetupPropertyNotification( mSlotDelegate.GetConnectionTracker(), root, node, actor, quitAction, this );
364 SetProperties( node, handle, constant );
368 // Appling by style helper
369 // use FindChildByName() to apply properties referenced in KEYNAME_ACTORS in the node
370 void Builder::ApplyStylesByActor( const TreeNode& root, const TreeNode& node,
371 Dali::Handle& handle, const Replacement& constant )
373 if( Dali::Actor actor = Dali::Actor::DownCast( handle ) )
375 if( const TreeNode* actors = node.GetChild( KEYNAME_ACTORS ) )
377 // in a style the actor subtree properties referenced by actor name
378 for( TreeConstIter iter = actors->CBegin(); iter != actors->CEnd(); ++iter )
380 Dali::Actor foundActor;
384 foundActor = actor.FindChildByName( (*iter).first );
389 // debug log cannot find searched for actor
390 #if defined(DEBUG_ENABLED)
391 DALI_SCRIPT_VERBOSE("Cannot find actor in style application '%s'\n", (*iter).first);
396 #if defined(DEBUG_ENABLED)
397 DALI_SCRIPT_VERBOSE("Styles applied to actor '%s'\n", (*iter).first);
399 ApplyProperties( root, (*iter).second, foundActor, constant );
407 void Builder::ApplyAllStyleProperties( const TreeNode& root, const TreeNode& node,
408 Dali::Handle& handle, const Replacement& constant )
410 OptionalChild styles = IsChild(root, KEYNAME_STYLES);
411 OptionalChild style = IsChild(node, KEYNAME_STYLES);
413 if( styles && style )
415 TreeNodeList additionalStyles;
417 CollectAllStyles( *styles, *style, additionalStyles );
419 #if defined(DEBUG_ENABLED)
420 for(TreeNode::ConstIterator iter = (*style).CBegin(); iter != (*style).CEnd(); ++iter)
422 if( OptionalString styleName = IsString( (*iter).second ) )
424 DALI_SCRIPT_VERBOSE("Style Applied '%s'\n", (*styleName).c_str());
429 // a style may have other styles, which has other styles etc so we apply in reverse by convention.
430 for(TreeNodeList::reverse_iterator iter = additionalStyles.rbegin(); iter != additionalStyles.rend(); ++iter)
432 ApplyProperties( root, *(*iter), handle, constant );
434 ApplyStylesByActor( root, *(*iter), handle, constant );
438 // applying given node last
439 ApplyProperties( root, node, handle, constant );
441 ApplyStylesByActor( root, node, handle, constant );
447 * Create a dali type from a node.
448 * If parent given and an actor type was created then add it to the parent and
449 * recursively add nodes children.
451 BaseHandle Builder::DoCreate( const TreeNode& root, const TreeNode& node,
452 Actor parent, const Replacement& replacements )
454 BaseHandle baseHandle;
456 const TreeNode* templateNode = NULL;
458 if( OptionalString typeName = IsString(node, KEYNAME_TYPE) )
460 typeInfo = TypeRegistry::Get().GetTypeInfo( *typeName );
464 // a template name is also allowed inplace of the type name
465 OptionalChild templates = IsChild( root, KEYNAME_TEMPLATES);
469 if( OptionalChild isTemplate = IsChild( *templates, *typeName ) )
471 templateNode = &(*isTemplate);
473 if( OptionalString templateTypeName = IsString(*templateNode, KEYNAME_TYPE) )
475 typeInfo = TypeRegistry::Get().GetTypeInfo( *templateTypeName );
484 DALI_SCRIPT_WARNING("Cannot create Dali type from node '%s'\n", node.GetName());
488 baseHandle = typeInfo.CreateInstance();
489 Handle handle = Handle::DownCast(baseHandle);
490 Actor actor = Actor::DownCast(handle);
495 DALI_SCRIPT_VERBOSE("Create:%s\n", typeInfo.GetName().c_str());
497 #if defined(DEBUG_ENABLED)
500 DALI_SCRIPT_VERBOSE(" Is Handle Object=%d\n", (long*)handle.GetObjectPtr());
501 DALI_SCRIPT_VERBOSE(" Is Handle Property Count=%d\n", handle.GetPropertyCount());
506 DALI_SCRIPT_VERBOSE(" Is Actor id=%d\n", actor.GetId());
509 Toolkit::Control control = Toolkit::Control::DownCast(handle);
512 DALI_SCRIPT_VERBOSE(" Is Control id=%d\n", actor.GetId());
514 #endif // DEBUG_ENABLED
518 ApplyProperties( root, *templateNode, handle, replacements );
520 if( OptionalChild actors = IsChild( *templateNode, KEYNAME_ACTORS ) )
522 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
524 DoCreate( root, (*iter).second, actor, replacements );
531 // add children of all the styles
532 if( OptionalChild actors = IsChild( node, KEYNAME_ACTORS ) )
534 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
536 DoCreate( root, (*iter).second, actor, replacements );
540 // apply style on top as they need the children to exist
541 ApplyAllStyleProperties( root, node, actor, replacements );
543 // then add to parent
551 ApplyProperties( root, node, handle, replacements );
556 DALI_SCRIPT_WARNING("Cannot create handle from type '%s'\n", typeInfo.GetName().c_str());
563 void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replacement& constant )
565 const Stage& stage = Stage::GetCurrent();
566 Layer root = stage.GetRootLayer();
568 if( OptionalString s = constant.IsString( IsChild(node, "source-actor") ) )
570 Actor actor = root.FindChildByName(*s);
573 task.SetSourceActor( actor );
577 DALI_SCRIPT_WARNING("Cannot find source actor on stage for render task called '%s'\n", (*s).c_str() );
581 if( OptionalString s = constant.IsString( IsChild(node, "camera-actor") ) )
583 CameraActor actor = CameraActor::DownCast( root.FindChildByName(*s) );
586 task.SetCameraActor( actor );
590 DALI_SCRIPT_WARNING("Cannot find camera actor on stage for render task called '%s'\n", (*s).c_str() );
594 if( OptionalString s = constant.IsString( IsChild(node, "target-frame-buffer") ) )
596 FrameBufferImage fb = GetFrameBufferImage( *s, constant );
599 task.SetTargetFrameBuffer( fb );
603 DALI_SCRIPT_WARNING("Cannot find target frame buffer '%s'\n", (*s).c_str() );
607 if( OptionalString s = constant.IsString( IsChild(node, "screen-to-frame-buffer-function") ) )
609 if("DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION" == *s)
611 task.SetScreenToFrameBufferFunction( RenderTask::DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION );
613 else if("FULLSCREEN_FRAMEBUFFER_FUNCTION" == *s)
615 task.SetScreenToFrameBufferFunction( RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION );
619 DALI_SCRIPT_WARNING("todo");
623 // other setup is via the property system
624 SetProperties( node, task, constant ); // @ todo, remove 'source-actor', 'camera-actor'?
628 void Builder::CreateRenderTask( const std::string &name )
630 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
632 Replacement constant(mReplacementMap);
634 const Stage& stage = Stage::GetCurrent();
636 OptionalChild tasks = IsChild(*mParser.GetRoot(), "render-tasks");
641 // Create the tasks from the current task as generally we want
642 // to setup task zero and onwards. Although this does overwrite
643 // the properties of the current task.
645 if( OptionalChild renderTask = IsChild(*tasks, name ) )
647 RenderTaskList list = stage.GetRenderTaskList();
648 unsigned int start = list.GetTaskCount();
653 // zero should have already been created by the stage so really
654 // this case should never happen
655 task = list.CreateTask();
659 TreeNode::ConstIterator iter = (*renderTask).CBegin();
660 task = list.GetTask( start - 1 );
662 SetupTask( task, (*iter).second, constant );
666 for(; iter != (*renderTask).CEnd(); ++iter )
668 task = list.CreateTask();
669 SetupTask( task, (*iter).second, constant );
675 ShaderEffect Builder::GetShaderEffect( const std::string &name)
677 Replacement constant( mReplacementMap );
678 return GetShaderEffect( name, constant );
681 ShaderEffect Builder::GetShaderEffect( const std::string &name, const Replacement& constant )
683 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
687 ShaderEffectLut::const_iterator iter( mShaderEffectLut.find( name ) );
688 if( iter != mShaderEffectLut.end() )
694 if( OptionalChild effects = IsChild( *mParser.GetRoot(), "shader-effects") )
696 if( OptionalChild effect = IsChild( *effects, name ) )
698 Dali::Property::Value propertyMap(Property::MAP);
699 if( SetPropertyFromNode( *effect, Property::MAP, propertyMap, constant ) )
701 ret = Dali::Scripting::NewShaderEffect( propertyMap );
702 mShaderEffectLut[ name ] = ret;
711 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name )
713 Replacement constant( mReplacementMap );
714 return GetFrameBufferImage(name, constant);
717 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name, const Replacement& constant )
719 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
721 FrameBufferImage ret;
723 ImageLut::const_iterator iter( mFrameBufferImageLut.find( name ) );
724 if( iter != mFrameBufferImageLut.end() )
730 if( OptionalChild images = IsChild( *mParser.GetRoot(), "frame-buffer-images") )
732 if( OptionalChild image = IsChild( *images, name ) )
734 Dali::Property::Value propertyMap(Property::MAP);
735 if( SetPropertyFromNode( *image, Property::MAP, propertyMap, constant ) )
737 propertyMap.SetValue(KEYNAME_TYPE, Property::Value(std::string("FrameBufferImage")));
738 ret = FrameBufferImage::DownCast( Dali::Scripting::NewImage( propertyMap ) );
739 mFrameBufferImageLut[ name ] = ret;
748 Path Builder::GetPath( const std::string& name )
750 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
754 PathLut::const_iterator iter( mPathLut.find( name ) );
755 if( iter != mPathLut.end() )
761 if( OptionalChild paths = IsChild( *mParser.GetRoot(), "paths") )
763 if( OptionalChild path = IsChild( *paths, name ) )
766 if( OptionalChild pointsProperty = IsChild( *path, "points") )
768 Dali::Property::Value points(Property::ARRAY);
769 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
772 ret.SetProperty( Path::POINTS, points);
774 //control-points property
775 if( OptionalChild pointsProperty = IsChild( *path, "control-points") )
777 Dali::Property::Value points(Property::ARRAY);
778 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
780 ret.SetProperty( Path::CONTROL_POINTS, points);
786 float curvature(0.25f);
787 if( OptionalFloat pointsProperty = IsFloat( *path, "curvature") )
789 curvature = *pointsProperty;
791 ret.GenerateControlPoints(curvature);
794 //Add the new path to the hash table for paths
795 mPathLut[ name ] = ret;
800 //Interpolation points not specified
801 DALI_SCRIPT_WARNING("Interpolation points not specified for path '%s'\n", name.c_str() );
811 Toolkit::Builder::BuilderSignalType& Builder::QuitSignal()
816 void Builder::EmitQuitSignal()
821 void Builder::AddActors( Actor toActor )
823 // 'stage' is the default/by convention section to add from
824 AddActors( "stage", toActor );
827 void Builder::AddActors( const std::string §ionName, Actor toActor )
829 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
831 Property::Map overrideMap;
832 Replacement replacements(overrideMap, mReplacementMap);
834 OptionalChild add = IsChild(*mParser.GetRoot(), sectionName);
838 for( TreeNode::ConstIterator iter = (*add).CBegin(); iter != (*add).CEnd(); ++iter )
840 // empty actor adds directly to the stage
841 BaseHandle baseHandle = DoCreate( *mParser.GetRoot(), (*iter).second, Actor(), replacements );
842 Actor actor = Actor::DownCast(baseHandle);
845 toActor.Add( actor );
849 // if were adding the 'stage' section then also check for a render task called stage
850 // to add automatically
851 if( "stage" == sectionName )
853 if( OptionalChild renderTasks = IsChild(*mParser.GetRoot(), "render-tasks") )
855 if( OptionalChild tasks = IsChild(*renderTasks, "stage") )
857 CreateRenderTask( "stage" );
864 Animation Builder::CreateAnimation( const std::string& animationName, const Replacement& replacement, Dali::Actor sourceActor )
866 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
870 if( OptionalChild animations = IsChild(*mParser.GetRoot(), "animations") )
872 if( OptionalChild animation = IsChild(*animations, animationName) )
874 anim = Dali::Toolkit::Internal::CreateAnimation( *animation, replacement, sourceActor, this );
878 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed\n", animationName.c_str() );
883 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed (no animation section)\n", animationName.c_str() );
889 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor )
891 Replacement replacement(map, mReplacementMap);
892 return CreateAnimation( animationName, replacement, sourceActor);
895 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map )
897 Replacement replacement(map, mReplacementMap);
898 return CreateAnimation( animationName, replacement, Stage::GetCurrent().GetRootLayer() );
901 Animation Builder::CreateAnimation( const std::string& animationName, Dali::Actor sourceActor )
903 Replacement replacement( mReplacementMap );
905 return CreateAnimation( animationName, replacement, sourceActor );
908 Animation Builder::CreateAnimation( const std::string& animationName )
910 Replacement replacement( mReplacementMap );
912 return CreateAnimation( animationName, replacement, Dali::Stage::GetCurrent().GetRootLayer() );
915 void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::UIFormat format )
917 // parser to get constants and includes only
918 Dali::Toolkit::JsonParser parser = Dali::Toolkit::JsonParser::New();
920 if( !parser.Parse( data ) )
922 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
923 parser.GetErrorLineNumber(),
924 parser.GetErrorColumn(),
925 parser.GetErrorDescription().c_str() );
927 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
932 // load constant map (allows the user to override the constants in the json after loading)
933 LoadConstants( *parser.GetRoot(), mReplacementMap );
936 if( OptionalChild includes = IsChild(*parser.GetRoot(), KEYNAME_INCLUDES) )
938 Replacement replacer( mReplacementMap );
940 for(TreeNode::ConstIterator iter = (*includes).CBegin(); iter != (*includes).CEnd(); ++iter)
942 OptionalString filename = replacer.IsString( (*iter).second );
946 #if defined(DEBUG_ENABLED)
947 DALI_SCRIPT_VERBOSE("Loading Include '%s'\n", (*filename).c_str());
949 LoadFromString( GetFileContents(*filename) );
954 if( !mParser.Parse( data ) )
956 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
957 mParser.GetErrorLineNumber(),
958 mParser.GetErrorColumn(),
959 mParser.GetErrorDescription().c_str() );
961 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
965 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Cannot parse JSON");
969 void Builder::AddConstants( const Property::Map& map )
971 mReplacementMap.Merge( map );
974 void Builder::AddConstant( const std::string& key, const Property::Value& value )
976 mReplacementMap[key] = value;
979 const Property::Map& Builder::GetConstants() const
981 return mReplacementMap;
984 const Property::Value& Builder::GetConstant( const std::string& key ) const
986 Property::Value* match = mReplacementMap.Find( key );
993 static Property::Value invalid;
998 void Builder::LoadConstants( const TreeNode& root, Property::Map& intoMap )
1000 Replacement replacer(intoMap);
1002 if( OptionalChild constants = IsChild(root, "constants") )
1004 for(TreeNode::ConstIterator iter = (*constants).CBegin();
1005 iter != (*constants).CEnd(); ++iter)
1007 Dali::Property::Value property;
1008 if( (*iter).second.GetName() )
1010 #if defined(DEBUG_ENABLED)
1011 DALI_SCRIPT_VERBOSE("Constant set from json '%s'\n", (*iter).second.GetName());
1013 if( SetPropertyFromNode( (*iter).second, property, replacer ) )
1015 intoMap[ (*iter).second.GetName() ] = property;
1019 DALI_SCRIPT_WARNING("Cannot convert property for constant %s\n",
1020 (*iter).second.GetName() == NULL ? "no name?" : (*iter).second.GetName());
1026 #if defined(DEBUG_ENABLED)
1027 Property::Value* iter = intoMap.Find( "CONFIG_SCRIPT_LOG_LEVEL" );
1028 if( iter && iter->GetType() == Property::STRING )
1030 std::string logLevel( iter->Get< std::string >() );
1031 if( logLevel == "NoLogging" )
1033 gFilterScript->SetLogLevel( Integration::Log::NoLogging );
1035 else if( logLevel == "Concise" )
1037 gFilterScript->SetLogLevel( Integration::Log::Concise );
1039 else if( logLevel == "General" )
1041 gFilterScript->SetLogLevel( Integration::Log::General );
1043 else if( logLevel == "Verbose" )
1045 gFilterScript->SetLogLevel( Integration::Log::Verbose );
1052 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle )
1054 Replacement replacer( mReplacementMap );
1055 return ApplyStyle( styleName, handle, replacer );
1058 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle, const Replacement& replacement )
1060 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1062 OptionalChild styles = IsChild( *mParser.GetRoot(), KEYNAME_STYLES );
1063 OptionalChild style = IsChild( *styles, styleName );
1065 if( styles && style )
1067 ApplyAllStyleProperties( *mParser.GetRoot(), *style, handle, replacement );
1076 BaseHandle Builder::Create( const std::string& templateName, const Property::Map& map )
1078 Replacement replacement( map, mReplacementMap );
1079 return Create( templateName, replacement );
1082 BaseHandle Builder::Create( const std::string& templateName, const Replacement& constant )
1084 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1086 BaseHandle baseHandle;
1088 OptionalChild templates = IsChild(*mParser.GetRoot(), KEYNAME_TEMPLATES);
1092 DALI_SCRIPT_WARNING("No template section found to CreateFromTemplate\n");
1096 OptionalChild childTemplate = IsChild(*templates, templateName);
1099 DALI_SCRIPT_WARNING("Template '%s' does not exist in template section\n", templateName.c_str());
1103 OptionalString type = constant.IsString( IsChild(*childTemplate, KEYNAME_TYPE) );
1107 DALI_SCRIPT_WARNING("Cannot create template '%s' as template section is missing 'type'\n", templateName.c_str());
1111 baseHandle = DoCreate( *mParser.GetRoot(), *childTemplate, Actor(), constant );
1119 BaseHandle Builder::CreateFromJson( const std::string& json )
1123 // merge in new template, hoping no one else has one named '@temp@'
1124 std::string newTemplate =
1125 std::string("{\"templates\":{\"@temp@\":") + \
1129 if( mParser.Parse(newTemplate) )
1131 Replacement replacement( mReplacementMap );
1132 ret = Create( "@temp@", replacement );
1138 bool Builder::ApplyFromJson( Handle& handle, const std::string& json )
1142 // merge new style, hoping no one else has one named '@temp@'
1143 std::string newStyle =
1144 std::string("{\"styles\":{\"@temp@\":") + \
1148 if( mParser.Parse(newStyle) )
1150 Replacement replacement( mReplacementMap );
1151 ret = ApplyStyle( "@temp@", handle, replacement );
1158 BaseHandle Builder::Create( const std::string& templateName )
1160 Replacement replacement( mReplacementMap );
1161 return Create( templateName, replacement );
1165 : mSlotDelegate( this )
1167 mParser = Dali::Toolkit::JsonParser::New();
1174 } // namespace Internal
1176 } // namespace Toolkit