2 * Copyright (c) 2016 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>
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/devel-api/scripting/scripting.h>
33 #include <dali/public-api/signals/functor-delegate.h>
34 #include <dali/integration-api/debug.h>
37 #include <dali-toolkit/public-api/controls/control.h>
38 #include <dali-toolkit/devel-api/builder/json-parser.h>
40 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
41 #include <dali-toolkit/internal/builder/builder-filesystem.h>
42 #include <dali-toolkit/internal/builder/builder-declarations.h>
43 #include <dali-toolkit/internal/builder/builder-set-property.h>
44 #include <dali-toolkit/internal/builder/replacement.h>
45 #include <dali-toolkit/internal/builder/tree-node-manipulator.h>
47 #include <dali-toolkit/internal/builder/builder-impl-debug.h>
59 extern Animation CreateAnimation(const TreeNode& child, const Replacement& replacements, const Dali::Actor searchRoot, Builder* const builder );
60 extern Actor SetupSignalAction(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, Dali::Toolkit::Internal::Builder* const builder);
61 extern Actor SetupPropertyNotification(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, Dali::Toolkit::Internal::Builder* const builder);
62 extern Actor SetupActor( const TreeNode& node, Actor& actor, const Replacement& constant );
64 #if defined(DEBUG_ENABLED)
65 Integration::Log::Filter* gFilterScript = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_SCRIPT");
71 #define TOKEN_STRING(x) #x
73 const std::string KEYNAME_STYLES = "styles";
74 const std::string KEYNAME_TYPE = "type";
75 const std::string KEYNAME_ACTORS = "actors";
76 const std::string KEYNAME_SIGNALS = "signals";
77 const std::string KEYNAME_NAME = "name";
78 const std::string KEYNAME_TEMPLATES = "templates";
79 const std::string KEYNAME_INCLUDES = "includes";
80 const std::string KEYNAME_MAPPINGS = "mappings";
82 const std::string PROPERTIES = "properties";
83 const std::string ANIMATABLE_PROPERTIES = "animatableProperties";
85 typedef std::vector<const TreeNode*> TreeNodeList;
88 bool GetMappingKey( const std::string& str, std::string& key )
91 std::string test( str );
94 if( test.at(0) == '<' )
96 if( test.at(test.length()-1) == '>' )
98 key = test.substr( 1, test.length()-2 );
107 * Recursively collects all stylesin a node (An array of style names).
109 * stylesCollection The set of styles from the json file (a json object of named styles)
110 * style The style array to begin the collection from
111 * styleList The style list to add nodes to apply
113 void CollectAllStyles( const TreeNode& stylesCollection, const TreeNode& style, TreeNodeList& styleList )
115 // style is an array of style names
116 if( TreeNode::ARRAY == style.GetType() )
118 for(TreeNode::ConstIterator iter = style.CBegin(); iter != style.CEnd(); ++iter)
120 if( OptionalString styleName = IsString( (*iter).second ) )
122 if( OptionalChild node = IsChild( stylesCollection, *styleName) )
124 styleList.push_back( &(*node) );
126 if( OptionalChild subStyle = IsChild( *node, KEYNAME_STYLES ) )
128 CollectAllStyles( stylesCollection, *subStyle, styleList );
140 * Sets the handle properties found in the tree node
142 void Builder::SetProperties( const TreeNode& node, Handle& handle, const Replacement& constant )
147 for( TreeNode::ConstIterator iter = node.CBegin(); iter != node.CEnd(); ++iter )
149 const TreeNode::KeyNodePair& keyChild = *iter;
151 std::string key( keyChild.first );
153 // ignore special fields; type,actors,signals,styles
154 if(key == KEYNAME_TYPE || key == KEYNAME_ACTORS || key == KEYNAME_SIGNALS || key == KEYNAME_STYLES || key == KEYNAME_MAPPINGS )
159 Handle propertyObject( handle );
161 Dali::Property::Index index = propertyObject.GetPropertyIndex( key );
163 if( Property::INVALID_INDEX != index )
165 Property::Type type = propertyObject.GetPropertyType(index);
166 Property::Value value;
169 // if node.value is a mapping, get the property value from the "mappings" table
170 if( keyChild.second.GetType() == TreeNode::STRING )
172 std::string mappingKey;
173 if( GetMappingKey(keyChild.second.GetString(), mappingKey) )
175 OptionalChild mappingRoot = IsChild( mParser.GetRoot(), KEYNAME_MAPPINGS );
176 mapped = GetPropertyMap( *mappingRoot, mappingKey.c_str(), type, value );
181 mapped = DeterminePropertyFromNode( keyChild.second, type, value, constant );
184 // verbose as this might not be a problem
185 // eg parentOrigin can be a string which is picked up later
186 DALI_SCRIPT_VERBOSE("Could not convert property:%s\n", key.c_str());
191 DALI_SCRIPT_VERBOSE("SetProperty '%s' Index=:%d Value Type=%d Value '%s'\n", key.c_str(), index, value.GetType(), PropertyValueToString(value).c_str() );
193 propertyObject.SetProperty( index, value );
198 DALI_SCRIPT_VERBOSE("SetProperty INVALID '%s' Index=:%d\n", key.c_str(), index);
201 // Add custom properties
202 SetCustomProperties(node, handle, constant, PROPERTIES, Property::READ_WRITE);
203 SetCustomProperties(node, handle, constant, ANIMATABLE_PROPERTIES, Property::ANIMATABLE);
205 } // for property nodes
209 DALI_SCRIPT_WARNING("Style applied to empty handle\n");
213 void Builder::SetCustomProperties( const TreeNode& node, Handle& handle, const Replacement& constant,
214 const std::string& childName, Property::AccessMode accessMode )
216 // Add custom properties
217 if( OptionalChild customPropertiesChild = IsChild(node, childName) )
219 const TreeNode& customPropertiesNode = *customPropertiesChild;
220 const TreeConstIter endIter = customPropertiesNode.CEnd();
221 for( TreeConstIter iter = customPropertiesNode.CBegin(); endIter != iter; ++iter )
223 const TreeNode::KeyNodePair& keyChild = *iter;
224 std::string key( keyChild.first );
226 Property::Value value;
227 DeterminePropertyFromNode( keyChild.second, value, constant );
228 // Register/Set property.
229 handle.RegisterProperty( key, value, accessMode );
234 // Set properties from node on handle.
235 void Builder::ApplyProperties( const TreeNode& root, const TreeNode& node,
236 Dali::Handle& handle, const Replacement& constant )
238 if( Actor actor = Actor::DownCast(handle) )
240 SetProperties( node, actor, constant );
244 SetupActor( node, actor, constant );
247 SetupSignalAction( mSlotDelegate.GetConnectionTracker(), root, node, actor, this );
248 SetupPropertyNotification( mSlotDelegate.GetConnectionTracker(), root, node, actor, this );
253 SetProperties( node, handle, constant );
257 // Appling by style helper
258 // use FindChildByName() to apply properties referenced in KEYNAME_ACTORS in the node
259 void Builder::ApplyStylesByActor( const TreeNode& root, const TreeNode& node,
260 Dali::Handle& handle, const Replacement& constant )
262 if( Dali::Actor actor = Dali::Actor::DownCast( handle ) )
264 if( const TreeNode* actors = node.GetChild( KEYNAME_ACTORS ) )
266 // in a style the actor subtree properties referenced by actor name
267 for( TreeConstIter iter = actors->CBegin(); iter != actors->CEnd(); ++iter )
269 Dali::Actor foundActor;
273 foundActor = actor.FindChildByName( (*iter).first );
278 // debug log cannot find searched for actor
279 #if defined(DEBUG_ENABLED)
280 DALI_SCRIPT_VERBOSE("Cannot find actor in style application '%s'\n", (*iter).first);
285 #if defined(DEBUG_ENABLED)
286 DALI_SCRIPT_VERBOSE("Styles applied to actor '%s'\n", (*iter).first);
288 ApplyProperties( root, (*iter).second, foundActor, constant );
296 void Builder::ApplyAllStyleProperties( const TreeNode& root, const TreeNode& node,
297 Dali::Handle& handle, const Replacement& constant )
299 OptionalChild styles = IsChild(root, KEYNAME_STYLES);
300 OptionalChild style = IsChild(node, KEYNAME_STYLES);
302 if( styles && style )
304 TreeNodeList additionalStyles;
306 CollectAllStyles( *styles, *style, additionalStyles );
308 #if defined(DEBUG_ENABLED)
309 for(TreeNode::ConstIterator iter = (*style).CBegin(); iter != (*style).CEnd(); ++iter)
311 if( OptionalString styleName = IsString( (*iter).second ) )
313 DALI_SCRIPT_VERBOSE("Style Applied '%s'\n", (*styleName).c_str());
318 // a style may have other styles, which has other styles etc so we apply in reverse by convention.
319 for(TreeNodeList::reverse_iterator iter = additionalStyles.rbegin(); iter != additionalStyles.rend(); ++iter)
321 ApplyProperties( root, *(*iter), handle, constant );
323 ApplyStylesByActor( root, *(*iter), handle, constant );
327 // applying given node last
328 ApplyProperties( root, node, handle, constant );
330 ApplyStylesByActor( root, node, handle, constant );
336 * Create a dali type from a node.
337 * If parent given and an actor type was created then add it to the parent and
338 * recursively add nodes children.
340 BaseHandle Builder::DoCreate( const TreeNode& root, const TreeNode& node,
341 Actor parent, const Replacement& replacements )
343 BaseHandle baseHandle;
345 const TreeNode* templateNode = NULL;
347 if( OptionalString typeName = IsString(node, KEYNAME_TYPE) )
349 typeInfo = TypeRegistry::Get().GetTypeInfo( *typeName );
353 // a template name is also allowed inplace of the type name
354 OptionalChild templates = IsChild( root, KEYNAME_TEMPLATES);
358 if( OptionalChild isTemplate = IsChild( *templates, *typeName ) )
360 templateNode = &(*isTemplate);
362 if( OptionalString templateTypeName = IsString(*templateNode, KEYNAME_TYPE) )
364 typeInfo = TypeRegistry::Get().GetTypeInfo( *templateTypeName );
373 DALI_SCRIPT_WARNING("Cannot create Dali type from node '%s'\n", node.GetName());
377 baseHandle = typeInfo.CreateInstance();
378 Handle handle = Handle::DownCast(baseHandle);
379 Actor actor = Actor::DownCast(handle);
384 DALI_SCRIPT_VERBOSE("Create:%s\n", typeInfo.GetName().c_str());
386 #if defined(DEBUG_ENABLED)
389 DALI_SCRIPT_VERBOSE(" Is Handle Object=%d\n", (long*)handle.GetObjectPtr());
390 DALI_SCRIPT_VERBOSE(" Is Handle Property Count=%d\n", handle.GetPropertyCount());
395 DALI_SCRIPT_VERBOSE(" Is Actor id=%d\n", actor.GetId());
398 Toolkit::Control control = Toolkit::Control::DownCast(handle);
401 DALI_SCRIPT_VERBOSE(" Is Control id=%d\n", actor.GetId());
403 #endif // DEBUG_ENABLED
407 ApplyProperties( root, *templateNode, handle, replacements );
409 if( OptionalChild actors = IsChild( *templateNode, KEYNAME_ACTORS ) )
411 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
413 DoCreate( root, (*iter).second, actor, replacements );
420 // add children of all the styles
421 if( OptionalChild actors = IsChild( node, KEYNAME_ACTORS ) )
423 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
425 DoCreate( root, (*iter).second, actor, replacements );
429 // apply style on top as they need the children to exist
430 ApplyAllStyleProperties( root, node, actor, replacements );
432 // then add to parent
440 ApplyProperties( root, node, handle, replacements );
445 DALI_SCRIPT_WARNING("Cannot create handle from type '%s'\n", typeInfo.GetName().c_str());
452 void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replacement& constant )
454 const Stage& stage = Stage::GetCurrent();
455 Layer root = stage.GetRootLayer();
457 if( OptionalString s = constant.IsString( IsChild(node, "sourceActor") ) )
459 Actor actor = root.FindChildByName(*s);
462 task.SetSourceActor( actor );
466 DALI_SCRIPT_WARNING("Cannot find source actor on stage for render task called '%s'\n", (*s).c_str() );
470 if( OptionalString s = constant.IsString( IsChild(node, "cameraActor") ) )
472 CameraActor actor = CameraActor::DownCast( root.FindChildByName(*s) );
475 task.SetCameraActor( actor );
479 DALI_SCRIPT_WARNING("Cannot find camera actor on stage for render task called '%s'\n", (*s).c_str() );
483 if( OptionalString s = constant.IsString( IsChild(node, "targetFrameBuffer") ) )
485 FrameBufferImage fb = GetFrameBufferImage( *s, constant );
488 task.SetTargetFrameBuffer( fb );
492 DALI_SCRIPT_WARNING("Cannot find target frame buffer '%s'\n", (*s).c_str() );
496 if( OptionalString s = constant.IsString( IsChild(node, "screenToFrameBufferFunction") ) )
498 if("DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION" == *s)
500 task.SetScreenToFrameBufferFunction( RenderTask::DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION );
502 else if("FULLSCREEN_FRAMEBUFFER_FUNCTION" == *s)
504 task.SetScreenToFrameBufferFunction( RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION );
508 DALI_SCRIPT_WARNING("todo");
512 // other setup is via the property system
513 SetProperties( node, task, constant );
516 void Builder::CreateRenderTask( const std::string &name )
518 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
520 Replacement constant(mReplacementMap);
522 const Stage& stage = Stage::GetCurrent();
524 OptionalChild tasks = IsChild(*mParser.GetRoot(), "renderTasks");
529 // Create the tasks from the current task as generally we want
530 // to setup task zero and onwards. Although this does overwrite
531 // the properties of the current task.
533 if( OptionalChild renderTask = IsChild(*tasks, name ) )
535 RenderTaskList list = stage.GetRenderTaskList();
536 unsigned int start = list.GetTaskCount();
541 // zero should have already been created by the stage so really
542 // this case should never happen
543 task = list.CreateTask();
547 TreeNode::ConstIterator iter = (*renderTask).CBegin();
548 task = list.GetTask( start - 1 );
550 SetupTask( task, (*iter).second, constant );
554 for(; iter != (*renderTask).CEnd(); ++iter )
556 task = list.CreateTask();
557 SetupTask( task, (*iter).second, constant );
563 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name )
565 Replacement constant( mReplacementMap );
566 return GetFrameBufferImage(name, constant);
569 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name, const Replacement& constant )
571 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
573 FrameBufferImage ret;
575 ImageLut::const_iterator iter( mFrameBufferImageLut.find( name ) );
576 if( iter != mFrameBufferImageLut.end() )
582 if( OptionalChild images = IsChild( *mParser.GetRoot(), "frameBufferImages") )
584 if( OptionalChild image = IsChild( *images, name ) )
586 Dali::Property::Value property(Property::MAP);
587 if( DeterminePropertyFromNode( *image, Property::MAP, property, constant ) )
589 Property::Map* map = property.GetMap();
593 (*map)[ KEYNAME_TYPE ] = Property::Value(std::string("FrameBufferImage") );
594 ret = FrameBufferImage::DownCast( Dali::Scripting::NewImage( property ) );
595 mFrameBufferImageLut[ name ] = ret;
605 Path Builder::GetPath( const std::string& name )
607 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
611 PathLut::const_iterator iter( mPathLut.find( name ) );
612 if( iter != mPathLut.end() )
618 if( OptionalChild paths = IsChild( *mParser.GetRoot(), "paths") )
620 if( OptionalChild path = IsChild( *paths, name ) )
623 if( OptionalChild pointsProperty = IsChild( *path, "points") )
625 Dali::Property::Value points(Property::ARRAY);
626 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
629 ret.SetProperty( Path::Property::POINTS, points);
631 //controlPoints property
632 if( OptionalChild pointsProperty = IsChild( *path, "controlPoints") )
634 Dali::Property::Value points(Property::ARRAY);
635 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
637 ret.SetProperty( Path::Property::CONTROL_POINTS, points);
643 float curvature(0.25f);
644 if( OptionalFloat pointsProperty = IsFloat( *path, "curvature") )
646 curvature = *pointsProperty;
648 ret.GenerateControlPoints(curvature);
651 //Add the new path to the hash table for paths
652 mPathLut[ name ] = ret;
657 //Interpolation points not specified
658 DALI_SCRIPT_WARNING("Interpolation points not specified for path '%s'\n", name.c_str() );
668 PathConstrainer Builder::GetPathConstrainer( const std::string& name )
670 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
672 //Search the pathConstrainer in the LUT
673 size_t count( mPathConstrainerLut.size() );
674 for( size_t i(0); i!=count; ++i )
676 if( mPathConstrainerLut[i].name == name )
678 //PathConstrainer has already been created
679 return mPathConstrainerLut[i].pathConstrainer;
683 //Create a new PathConstrainer
685 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
687 if( OptionalChild pathConstrainer = IsChild( *constrainers, name ) )
689 OptionalString constrainerType(IsString(IsChild(*pathConstrainer, "type")));
692 DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str() );
694 else if( *constrainerType == "PathConstrainer")
697 if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "points") )
699 Dali::Property::Value points(Property::ARRAY);
700 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
702 ret = PathConstrainer::New();
703 ret.SetProperty( PathConstrainer::Property::POINTS, points);
705 //controlPoints property
706 if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "controlPoints") )
708 Dali::Property::Value points(Property::ARRAY);
709 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
711 ret.SetProperty( PathConstrainer::Property::CONTROL_POINTS, points);
715 OptionalVector3 forward( IsVector3( IsChild(*pathConstrainer, "forward" ) ) );
718 ret.SetProperty( PathConstrainer::Property::FORWARD, *forward);
721 //Add the new constrainer to the vector of PathConstrainer
722 PathConstrainerEntry entry = {name,ret};
723 mPathConstrainerLut.push_back( entry );
727 //Control points not specified
728 DALI_SCRIPT_WARNING("Control points not specified for pathConstrainer '%s'\n", name.c_str() );
734 //Interpolation points not specified
735 DALI_SCRIPT_WARNING("Interpolation points not specified for pathConstrainer '%s'\n", name.c_str() );
740 DALI_SCRIPT_WARNING("Constrainer '%s' is not a PathConstrainer\n", name.c_str() );
748 bool Builder::IsPathConstrainer( const std::string& name )
750 size_t count( mPathConstrainerLut.size() );
751 for( size_t i(0); i!=count; ++i )
753 if( mPathConstrainerLut[i].name == name )
759 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
761 if( OptionalChild constrainer = IsChild( *constrainers, name ) )
763 OptionalString constrainerType(IsString(IsChild(*constrainer, "type")));
770 return *constrainerType == "PathConstrainer";
777 Dali::LinearConstrainer Builder::GetLinearConstrainer( const std::string& name )
779 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
781 //Search the LinearConstrainer in the LUT
782 size_t count( mLinearConstrainerLut.size() );
783 for( size_t i(0); i!=count; ++i )
785 if( mLinearConstrainerLut[i].name == name )
787 //LinearConstrainer has already been created
788 return mLinearConstrainerLut[i].linearConstrainer;
792 //Create a new LinearConstrainer
793 LinearConstrainer ret;
794 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
796 if( OptionalChild linearConstrainer = IsChild( *constrainers, name ) )
798 OptionalString constrainerType(IsString(IsChild(*linearConstrainer, "type")));
801 DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str() );
803 else if( *constrainerType == "LinearConstrainer")
806 if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "value") )
808 Dali::Property::Value points(Property::ARRAY);
809 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
811 ret = Dali::LinearConstrainer::New();
812 ret.SetProperty( LinearConstrainer::Property::VALUE, points);
814 //controlPoints property
815 if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "progress") )
817 Dali::Property::Value points(Property::ARRAY);
818 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
820 ret.SetProperty( LinearConstrainer::Property::PROGRESS, points);
823 //Add the new constrainer to vector of LinearConstrainer
824 LinearConstrainerEntry entry = {name,ret};
825 mLinearConstrainerLut.push_back( entry );
830 //Interpolation points not specified
831 DALI_SCRIPT_WARNING("Values not specified for LinearConstrainer '%s'\n", name.c_str() );
836 DALI_SCRIPT_WARNING("Constrainer '%s' is not a LinearConstrainer\n", name.c_str() );
844 bool Builder::IsLinearConstrainer( const std::string& name )
846 // Search the LinearConstrainer in the LUT
847 size_t count( mLinearConstrainerLut.size() );
848 for( size_t i(0); i!=count; ++i )
850 if( mLinearConstrainerLut[i].name == name )
856 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
858 if( OptionalChild constrainer = IsChild( *constrainers, name ) )
860 OptionalString constrainerType(IsString(IsChild(*constrainer, "type")));
867 return *constrainerType == "LinearConstrainer";
874 Toolkit::Builder::BuilderSignalType& Builder::QuitSignal()
879 void Builder::EmitQuitSignal()
884 void Builder::AddActors( Actor toActor )
886 // 'stage' is the default/by convention section to add from
887 AddActors( "stage", toActor );
890 void Builder::AddActors( const std::string §ionName, Actor toActor )
892 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
894 Property::Map overrideMap;
895 Replacement replacements(overrideMap, mReplacementMap);
897 OptionalChild add = IsChild(*mParser.GetRoot(), sectionName);
901 for( TreeNode::ConstIterator iter = (*add).CBegin(); iter != (*add).CEnd(); ++iter )
903 // empty actor adds directly to the stage
904 BaseHandle baseHandle = DoCreate( *mParser.GetRoot(), (*iter).second, Actor(), replacements );
905 Actor actor = Actor::DownCast(baseHandle);
908 toActor.Add( actor );
912 // if were adding the 'stage' section then also check for a render task called stage
913 // to add automatically
914 if( "stage" == sectionName )
916 if( OptionalChild renderTasks = IsChild(*mParser.GetRoot(), "renderTasks") )
918 if( OptionalChild tasks = IsChild(*renderTasks, "stage") )
920 CreateRenderTask( "stage" );
927 Animation Builder::CreateAnimation( const std::string& animationName, const Replacement& replacement, Dali::Actor sourceActor )
929 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
933 if( OptionalChild animations = IsChild(*mParser.GetRoot(), "animations") )
935 if( OptionalChild animation = IsChild(*animations, animationName) )
937 anim = Dali::Toolkit::Internal::CreateAnimation( *animation, replacement, sourceActor, this );
941 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed\n", animationName.c_str() );
946 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed (no animation section)\n", animationName.c_str() );
952 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor )
954 Replacement replacement(map, mReplacementMap);
955 return CreateAnimation( animationName, replacement, sourceActor);
958 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map )
960 Replacement replacement(map, mReplacementMap);
961 return CreateAnimation( animationName, replacement, Stage::GetCurrent().GetRootLayer() );
964 Animation Builder::CreateAnimation( const std::string& animationName, Dali::Actor sourceActor )
966 Replacement replacement( mReplacementMap );
968 return CreateAnimation( animationName, replacement, sourceActor );
971 Animation Builder::CreateAnimation( const std::string& animationName )
973 Replacement replacement( mReplacementMap );
975 return CreateAnimation( animationName, replacement, Dali::Stage::GetCurrent().GetRootLayer() );
978 bool Builder::ConvertChildValue( const TreeNode& mappingRoot, KeyStack& keyStack, Property::Value& child )
982 switch( child.GetType() )
984 case Property::STRING:
987 if( child.Get( value ) )
990 if( GetMappingKey( value, key ) )
992 // Check key for cycles:
994 for( KeyStack::iterator iter = keyStack.begin() ; iter != keyStack.end(); ++iter )
996 if( key.compare(*iter) == 0 )
998 // key is already in stack; stop.
999 DALI_LOG_WARNING("Detected cycle in stylesheet mapping table:%s\n", key.c_str());
1000 child = Property::Value("");
1008 // The following call will overwrite the child with the value
1009 // from the mapping.
1010 RecursePropertyMap( mappingRoot, keyStack, key.c_str(), Property::NONE, child );
1020 Property::Map* map = child.GetMap();
1021 for( Property::Map::SizeType i=0; i < map->Count(); ++i )
1023 Property::Value& child = map->GetValue(i);
1024 ConvertChildValue(mappingRoot, keyStack, child);
1029 case Property::ARRAY:
1031 Property::Array* array = child.GetArray();
1032 for( Property::Array::SizeType i=0; i < array->Count(); ++i )
1034 Property::Value& child = array->GetElementAt(i);
1035 ConvertChildValue(mappingRoot, keyStack, child);
1041 // Ignore other types.
1048 bool Builder::RecursePropertyMap( const TreeNode& mappingRoot, KeyStack& keyStack, const char* theKey, Property::Type propertyType, Property::Value& value )
1050 Replacement replacer( mReplacementMap );
1051 bool result = false;
1053 keyStack.push_back( theKey );
1055 for( TreeNode::ConstIterator iter = mappingRoot.CBegin(); iter != mappingRoot.CEnd(); ++iter )
1057 std::string aKey( (*iter).first );
1058 if( aKey.compare( theKey ) == 0 )
1060 if( propertyType == Property::NONE )
1062 DeterminePropertyFromNode( (*iter).second, value, replacer );
1067 result = DeterminePropertyFromNode( (*iter).second, propertyType, value, replacer );
1072 ConvertChildValue(mappingRoot, keyStack, value);
1077 keyStack.pop_back();
1083 bool Builder::GetPropertyMap( const TreeNode& mappingRoot, const char* theKey, Property::Type propertyType, Property::Value& value )
1086 return RecursePropertyMap( mappingRoot, keyStack, theKey, propertyType, value );
1090 void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::UIFormat format )
1092 // parser to get constants and includes only
1093 Dali::Toolkit::JsonParser parser = Dali::Toolkit::JsonParser::New();
1095 if( !parser.Parse( data ) )
1097 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
1098 parser.GetErrorLineNumber(),
1099 parser.GetErrorColumn(),
1100 parser.GetErrorDescription().c_str() );
1102 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
1106 // load constant map (allows the user to override the constants in the json after loading)
1107 LoadConstants( *parser.GetRoot(), mReplacementMap );
1110 if( OptionalChild includes = IsChild(*parser.GetRoot(), KEYNAME_INCLUDES) )
1112 Replacement replacer( mReplacementMap );
1114 for(TreeNode::ConstIterator iter = (*includes).CBegin(); iter != (*includes).CEnd(); ++iter)
1116 OptionalString filename = replacer.IsString( (*iter).second );
1120 #if defined(DEBUG_ENABLED)
1121 DALI_SCRIPT_VERBOSE("Loading Include '%s'\n", (*filename).c_str());
1123 LoadFromString( GetFileContents(*filename) );
1128 if( !mParser.Parse( data ) )
1130 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
1131 mParser.GetErrorLineNumber(),
1132 mParser.GetErrorColumn(),
1133 mParser.GetErrorDescription().c_str() );
1135 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
1139 DUMP_PARSE_TREE(parser); // This macro only writes out if DEBUG is enabled and the "DUMP_TREE" constant is defined in the stylesheet.
1140 DUMP_TEST_MAPPINGS(parser);
1142 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Cannot parse JSON");
1145 void Builder::AddConstants( const Property::Map& map )
1147 mReplacementMap.Merge( map );
1150 void Builder::AddConstant( const std::string& key, const Property::Value& value )
1152 mReplacementMap[key] = value;
1155 const Property::Map& Builder::GetConstants() const
1157 return mReplacementMap;
1160 const Property::Value& Builder::GetConstant( const std::string& key ) const
1162 Property::Value* match = mReplacementMap.Find( key );
1169 static Property::Value invalid;
1174 void Builder::LoadConstants( const TreeNode& root, Property::Map& intoMap )
1176 Replacement replacer(intoMap);
1178 if( OptionalChild constants = IsChild(root, "constants") )
1180 for(TreeNode::ConstIterator iter = (*constants).CBegin();
1181 iter != (*constants).CEnd(); ++iter)
1183 Dali::Property::Value property;
1184 if( (*iter).second.GetName() )
1186 #if defined(DEBUG_ENABLED)
1187 DALI_SCRIPT_VERBOSE("Constant set from json '%s'\n", (*iter).second.GetName());
1189 DeterminePropertyFromNode( (*iter).second, property, replacer );
1190 intoMap[ (*iter).second.GetName() ] = property;
1195 #if defined(DEBUG_ENABLED)
1196 Property::Value* iter = intoMap.Find( "CONFIG_SCRIPT_LOG_LEVEL" );
1197 if( iter && iter->GetType() == Property::STRING )
1199 std::string logLevel( iter->Get< std::string >() );
1200 if( logLevel == "NoLogging" )
1202 gFilterScript->SetLogLevel( Integration::Log::NoLogging );
1204 else if( logLevel == "Concise" )
1206 gFilterScript->SetLogLevel( Integration::Log::Concise );
1208 else if( logLevel == "General" )
1210 gFilterScript->SetLogLevel( Integration::Log::General );
1212 else if( logLevel == "Verbose" )
1214 gFilterScript->SetLogLevel( Integration::Log::Verbose );
1221 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle )
1223 Replacement replacer( mReplacementMap );
1224 return ApplyStyle( styleName, handle, replacer );
1227 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle, const Replacement& replacement )
1229 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1231 OptionalChild styles = IsChild( *mParser.GetRoot(), KEYNAME_STYLES );
1232 OptionalChild style = IsChild( *styles, styleName );
1234 if( styles && style )
1236 ApplyAllStyleProperties( *mParser.GetRoot(), *style, handle, replacement );
1245 BaseHandle Builder::Create( const std::string& templateName, const Property::Map& map )
1247 Replacement replacement( map, mReplacementMap );
1248 return Create( templateName, replacement );
1251 BaseHandle Builder::Create( const std::string& templateName, const Replacement& constant )
1253 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1255 BaseHandle baseHandle;
1257 OptionalChild templates = IsChild(*mParser.GetRoot(), KEYNAME_TEMPLATES);
1261 DALI_SCRIPT_WARNING("No template section found to CreateFromTemplate\n");
1265 OptionalChild childTemplate = IsChild(*templates, templateName);
1268 DALI_SCRIPT_WARNING("Template '%s' does not exist in template section\n", templateName.c_str());
1272 OptionalString type = constant.IsString( IsChild(*childTemplate, KEYNAME_TYPE) );
1276 DALI_SCRIPT_WARNING("Cannot create template '%s' as template section is missing 'type'\n", templateName.c_str());
1280 baseHandle = DoCreate( *mParser.GetRoot(), *childTemplate, Actor(), constant );
1288 BaseHandle Builder::CreateFromJson( const std::string& json )
1292 // merge in new template, hoping no one else has one named '@temp@'
1293 std::string newTemplate =
1294 std::string("{\"templates\":{\"@temp@\":") + \
1298 if( mParser.Parse(newTemplate) )
1300 Replacement replacement( mReplacementMap );
1301 ret = Create( "@temp@", replacement );
1307 bool Builder::ApplyFromJson( Handle& handle, const std::string& json )
1311 // merge new style, hoping no one else has one named '@temp@'
1312 std::string newStyle =
1313 std::string("{\"styles\":{\"@temp@\":") + \
1317 if( mParser.Parse(newStyle) )
1319 Replacement replacement( mReplacementMap );
1320 ret = ApplyStyle( "@temp@", handle, replacement );
1327 BaseHandle Builder::Create( const std::string& templateName )
1329 Replacement replacement( mReplacementMap );
1330 return Create( templateName, replacement );
1334 : mSlotDelegate( this )
1336 mParser = Dali::Toolkit::JsonParser::New();
1338 Property::Map defaultDirs;
1339 defaultDirs[ TOKEN_STRING(DALI_IMAGE_DIR) ] = DALI_IMAGE_DIR;
1340 defaultDirs[ TOKEN_STRING(DALI_SOUND_DIR) ] = DALI_SOUND_DIR;
1341 defaultDirs[ TOKEN_STRING(DALI_STYLE_DIR) ] = DALI_STYLE_DIR;
1342 defaultDirs[ TOKEN_STRING(DALI_STYLE_IMAGE_DIR) ] = DALI_STYLE_IMAGE_DIR;
1344 AddConstants( defaultDirs );
1351 } // namespace Internal
1353 } // namespace Toolkit