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/camera-actor.h>
31 #include <dali/devel-api/scripting/scripting.h>
32 #include <dali/public-api/signals/functor-delegate.h>
33 #include <dali/integration-api/debug.h>
36 #include <dali-toolkit/public-api/controls/control.h>
37 #include <dali-toolkit/devel-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/builder-set-property.h>
43 #include <dali-toolkit/internal/builder/replacement.h>
44 #include <dali-toolkit/internal/builder/tree-node-manipulator.h>
46 #include <dali-toolkit/internal/builder/builder-impl-debug.h>
58 extern Animation CreateAnimation(const TreeNode& child, const Replacement& replacements, const Dali::Actor searchRoot, Builder* const builder );
59 extern Actor SetupSignalAction(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, Dali::Toolkit::Internal::Builder* const builder);
60 extern Actor SetupPropertyNotification(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, Dali::Toolkit::Internal::Builder* const builder);
62 #if defined(DEBUG_ENABLED)
63 Integration::Log::Filter* gFilterScript = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_SCRIPT");
69 #define TOKEN_STRING(x) #x
71 const std::string KEYNAME_STYLES = "styles";
72 const std::string KEYNAME_TYPE = "type";
73 const std::string KEYNAME_ACTORS = "actors";
74 const std::string KEYNAME_SIGNALS = "signals";
75 const std::string KEYNAME_NAME = "name";
76 const std::string KEYNAME_TEMPLATES = "templates";
77 const std::string KEYNAME_INCLUDES = "includes";
78 const std::string KEYNAME_MAPPINGS = "mappings";
80 const std::string PROPERTIES = "properties";
81 const std::string ANIMATABLE_PROPERTIES = "animatableProperties";
83 typedef std::vector<const TreeNode*> TreeNodeList;
86 bool GetMappingKey( const std::string& str, std::string& key )
89 std::string test( str );
92 if( test.at(0) == '<' )
94 if( test.at(test.length()-1) == '>' )
96 key = test.substr( 1, test.length()-2 );
105 * Recursively collects all stylesin a node (An array of style names).
107 * stylesCollection The set of styles from the json file (a json object of named styles)
108 * style The style array to begin the collection from
109 * styleList The style list to add nodes to apply
111 void CollectAllStyles( const TreeNode& stylesCollection, const TreeNode& style, TreeNodeList& styleList )
113 // style is an array of style names
114 if( TreeNode::ARRAY == style.GetType() )
116 for(TreeNode::ConstIterator iter = style.CBegin(); iter != style.CEnd(); ++iter)
118 if( OptionalString styleName = IsString( (*iter).second ) )
120 if( OptionalChild node = IsChild( stylesCollection, *styleName) )
122 styleList.push_back( &(*node) );
124 if( OptionalChild subStyle = IsChild( *node, KEYNAME_STYLES ) )
126 CollectAllStyles( stylesCollection, *subStyle, styleList );
138 * Sets the handle properties found in the tree node
140 void Builder::SetProperties( const TreeNode& node, Handle& handle, const Replacement& constant )
145 for( TreeNode::ConstIterator iter = node.CBegin(); iter != node.CEnd(); ++iter )
147 const TreeNode::KeyNodePair& keyChild = *iter;
149 std::string key( keyChild.first );
151 // ignore special fields; type,actors,signals,styles
152 if(key == KEYNAME_TYPE || key == KEYNAME_ACTORS || key == KEYNAME_SIGNALS || key == KEYNAME_STYLES || key == KEYNAME_MAPPINGS )
157 Handle propertyObject( handle );
159 Dali::Property::Index index = propertyObject.GetPropertyIndex( key );
161 if( Property::INVALID_INDEX != index )
163 Property::Type type = propertyObject.GetPropertyType(index);
164 Property::Value value;
167 // if node.value is a mapping, get the property value from the "mappings" table
168 if( keyChild.second.GetType() == TreeNode::STRING )
170 std::string mappingKey;
171 if( GetMappingKey(keyChild.second.GetString(), mappingKey) )
173 OptionalChild mappingRoot = IsChild( mParser.GetRoot(), KEYNAME_MAPPINGS );
174 mapped = GetPropertyMap( *mappingRoot, mappingKey.c_str(), type, value );
179 mapped = DeterminePropertyFromNode( keyChild.second, type, value, constant );
182 // Just determine the property from the node and if it's valid, let the property object handle it
183 DeterminePropertyFromNode( keyChild.second, value, constant );
184 mapped = ( value.GetType() != Property::NONE );
189 DALI_SCRIPT_VERBOSE("SetProperty '%s' Index=:%d Value Type=%d Value '%s'\n", key.c_str(), index, value.GetType(), PropertyValueToString(value).c_str() );
191 propertyObject.SetProperty( index, value );
196 DALI_SCRIPT_VERBOSE("SetProperty INVALID '%s' Index=:%d\n", key.c_str(), index);
199 // Add custom properties
200 SetCustomProperties(node, handle, constant, PROPERTIES, Property::READ_WRITE);
201 SetCustomProperties(node, handle, constant, ANIMATABLE_PROPERTIES, Property::ANIMATABLE);
203 } // for property nodes
207 DALI_SCRIPT_WARNING("Style applied to empty handle\n");
211 void Builder::SetCustomProperties( const TreeNode& node, Handle& handle, const Replacement& constant,
212 const std::string& childName, Property::AccessMode accessMode )
214 // Add custom properties
215 if( OptionalChild customPropertiesChild = IsChild(node, childName) )
217 const TreeNode& customPropertiesNode = *customPropertiesChild;
218 const TreeConstIter endIter = customPropertiesNode.CEnd();
219 for( TreeConstIter iter = customPropertiesNode.CBegin(); endIter != iter; ++iter )
221 const TreeNode::KeyNodePair& keyChild = *iter;
222 std::string key( keyChild.first );
224 Property::Value value;
225 DeterminePropertyFromNode( keyChild.second, value, constant );
226 // Register/Set property.
227 handle.RegisterProperty( key, value, accessMode );
232 // Set properties from node on handle.
233 void Builder::ApplyProperties( const TreeNode& root, const TreeNode& node,
234 Dali::Handle& handle, const Replacement& constant )
236 if( Actor actor = Actor::DownCast(handle) )
238 SetProperties( node, actor, constant );
243 SetupSignalAction( mSlotDelegate.GetConnectionTracker(), root, node, actor, this );
244 SetupPropertyNotification( mSlotDelegate.GetConnectionTracker(), root, node, actor, this );
249 SetProperties( node, handle, constant );
253 // Appling by style helper
254 // use FindChildByName() to apply properties referenced in KEYNAME_ACTORS in the node
255 void Builder::ApplyStylesByActor( const TreeNode& root, const TreeNode& node,
256 Dali::Handle& handle, const Replacement& constant )
258 if( Dali::Actor actor = Dali::Actor::DownCast( handle ) )
260 if( const TreeNode* actors = node.GetChild( KEYNAME_ACTORS ) )
262 // in a style the actor subtree properties referenced by actor name
263 for( TreeConstIter iter = actors->CBegin(); iter != actors->CEnd(); ++iter )
265 Dali::Actor foundActor;
269 foundActor = actor.FindChildByName( (*iter).first );
274 // debug log cannot find searched for actor
275 #if defined(DEBUG_ENABLED)
276 DALI_SCRIPT_VERBOSE("Cannot find actor in style application '%s'\n", (*iter).first);
281 #if defined(DEBUG_ENABLED)
282 DALI_SCRIPT_VERBOSE("Styles applied to actor '%s'\n", (*iter).first);
284 ApplyProperties( root, (*iter).second, foundActor, constant );
292 void Builder::ApplyAllStyleProperties( const TreeNode& root, const TreeNode& node,
293 Dali::Handle& handle, const Replacement& constant )
295 OptionalChild styles = IsChild(root, KEYNAME_STYLES);
296 OptionalChild style = IsChild(node, KEYNAME_STYLES);
298 if( styles && style )
300 TreeNodeList additionalStyles;
302 CollectAllStyles( *styles, *style, additionalStyles );
304 #if defined(DEBUG_ENABLED)
305 for(TreeNode::ConstIterator iter = (*style).CBegin(); iter != (*style).CEnd(); ++iter)
307 if( OptionalString styleName = IsString( (*iter).second ) )
309 DALI_SCRIPT_VERBOSE("Style Applied '%s'\n", (*styleName).c_str());
314 // a style may have other styles, which has other styles etc so we apply in reverse by convention.
315 for(TreeNodeList::reverse_iterator iter = additionalStyles.rbegin(); iter != additionalStyles.rend(); ++iter)
317 ApplyProperties( root, *(*iter), handle, constant );
319 ApplyStylesByActor( root, *(*iter), handle, constant );
323 // applying given node last
324 ApplyProperties( root, node, handle, constant );
326 ApplyStylesByActor( root, node, handle, constant );
332 * Create a dali type from a node.
333 * If parent given and an actor type was created then add it to the parent and
334 * recursively add nodes children.
336 BaseHandle Builder::DoCreate( const TreeNode& root, const TreeNode& node,
337 Actor parent, const Replacement& replacements )
339 BaseHandle baseHandle;
341 const TreeNode* templateNode = NULL;
343 if( OptionalString typeName = IsString(node, KEYNAME_TYPE) )
345 typeInfo = TypeRegistry::Get().GetTypeInfo( *typeName );
349 // a template name is also allowed inplace of the type name
350 OptionalChild templates = IsChild( root, KEYNAME_TEMPLATES);
354 if( OptionalChild isTemplate = IsChild( *templates, *typeName ) )
356 templateNode = &(*isTemplate);
358 if( OptionalString templateTypeName = IsString(*templateNode, KEYNAME_TYPE) )
360 typeInfo = TypeRegistry::Get().GetTypeInfo( *templateTypeName );
369 DALI_SCRIPT_WARNING("Cannot create Dali type from node '%s'\n", node.GetName());
373 baseHandle = typeInfo.CreateInstance();
374 Handle handle = Handle::DownCast(baseHandle);
375 Actor actor = Actor::DownCast(handle);
380 DALI_SCRIPT_VERBOSE("Create:%s\n", typeInfo.GetName().c_str());
382 #if defined(DEBUG_ENABLED)
385 DALI_SCRIPT_VERBOSE(" Is Handle Object=%d\n", (long*)handle.GetObjectPtr());
386 DALI_SCRIPT_VERBOSE(" Is Handle Property Count=%d\n", handle.GetPropertyCount());
391 DALI_SCRIPT_VERBOSE(" Is Actor id=%d\n", actor.GetId());
394 Toolkit::Control control = Toolkit::Control::DownCast(handle);
397 DALI_SCRIPT_VERBOSE(" Is Control id=%d\n", actor.GetId());
399 #endif // DEBUG_ENABLED
403 ApplyProperties( root, *templateNode, handle, replacements );
405 if( OptionalChild actors = IsChild( *templateNode, KEYNAME_ACTORS ) )
407 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
409 DoCreate( root, (*iter).second, actor, replacements );
416 // add children of all the styles
417 if( OptionalChild actors = IsChild( node, KEYNAME_ACTORS ) )
419 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
421 DoCreate( root, (*iter).second, actor, replacements );
425 // apply style on top as they need the children to exist
426 ApplyAllStyleProperties( root, node, actor, replacements );
428 // then add to parent
436 ApplyProperties( root, node, handle, replacements );
441 DALI_SCRIPT_WARNING("Cannot create handle from type '%s'\n", typeInfo.GetName().c_str());
448 void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replacement& constant )
450 const Stage& stage = Stage::GetCurrent();
451 Layer root = stage.GetRootLayer();
453 if( OptionalString s = constant.IsString( IsChild(node, "sourceActor") ) )
455 Actor actor = root.FindChildByName(*s);
458 task.SetSourceActor( actor );
462 DALI_SCRIPT_WARNING("Cannot find source actor on stage for render task called '%s'\n", (*s).c_str() );
466 if( OptionalString s = constant.IsString( IsChild(node, "cameraActor") ) )
468 CameraActor actor = CameraActor::DownCast( root.FindChildByName(*s) );
471 task.SetCameraActor( actor );
475 DALI_SCRIPT_WARNING("Cannot find camera actor on stage for render task called '%s'\n", (*s).c_str() );
479 if( OptionalString s = constant.IsString( IsChild(node, "targetFrameBuffer") ) )
481 FrameBufferImage fb = GetFrameBufferImage( *s, constant );
484 task.SetTargetFrameBuffer( fb );
488 DALI_SCRIPT_WARNING("Cannot find target frame buffer '%s'\n", (*s).c_str() );
492 if( OptionalString s = constant.IsString( IsChild(node, "screenToFrameBufferFunction") ) )
494 if("DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION" == *s)
496 task.SetScreenToFrameBufferFunction( RenderTask::DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION );
498 else if("FULLSCREEN_FRAMEBUFFER_FUNCTION" == *s)
500 task.SetScreenToFrameBufferFunction( RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION );
504 DALI_SCRIPT_WARNING("todo");
508 // other setup is via the property system
509 SetProperties( node, task, constant );
512 void Builder::CreateRenderTask( const std::string &name )
514 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
516 Replacement constant(mReplacementMap);
518 const Stage& stage = Stage::GetCurrent();
520 OptionalChild tasks = IsChild(*mParser.GetRoot(), "renderTasks");
525 // Create the tasks from the current task as generally we want
526 // to setup task zero and onwards. Although this does overwrite
527 // the properties of the current task.
529 if( OptionalChild renderTask = IsChild(*tasks, name ) )
531 RenderTaskList list = stage.GetRenderTaskList();
532 unsigned int start = list.GetTaskCount();
537 // zero should have already been created by the stage so really
538 // this case should never happen
539 task = list.CreateTask();
543 TreeNode::ConstIterator iter = (*renderTask).CBegin();
544 task = list.GetTask( start - 1 );
546 SetupTask( task, (*iter).second, constant );
550 for(; iter != (*renderTask).CEnd(); ++iter )
552 task = list.CreateTask();
553 SetupTask( task, (*iter).second, constant );
559 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name )
561 Replacement constant( mReplacementMap );
562 return GetFrameBufferImage(name, constant);
565 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name, const Replacement& constant )
567 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
569 FrameBufferImage ret;
571 ImageLut::const_iterator iter( mFrameBufferImageLut.find( name ) );
572 if( iter != mFrameBufferImageLut.end() )
578 if( OptionalChild images = IsChild( *mParser.GetRoot(), "frameBufferImages") )
580 if( OptionalChild image = IsChild( *images, name ) )
582 Dali::Property::Value property(Property::MAP);
583 if( DeterminePropertyFromNode( *image, Property::MAP, property, constant ) )
585 Property::Map* map = property.GetMap();
589 (*map)[ KEYNAME_TYPE ] = Property::Value(std::string("FrameBufferImage") );
590 ret = FrameBufferImage::DownCast( Dali::Scripting::NewImage( property ) );
591 mFrameBufferImageLut[ name ] = ret;
601 Path Builder::GetPath( const std::string& name )
603 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
607 PathLut::const_iterator iter( mPathLut.find( name ) );
608 if( iter != mPathLut.end() )
614 if( OptionalChild paths = IsChild( *mParser.GetRoot(), "paths") )
616 if( OptionalChild path = IsChild( *paths, name ) )
619 if( OptionalChild pointsProperty = IsChild( *path, "points") )
621 Dali::Property::Value points(Property::ARRAY);
622 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
625 ret.SetProperty( Path::Property::POINTS, points);
627 //controlPoints property
628 if( OptionalChild pointsProperty = IsChild( *path, "controlPoints") )
630 Dali::Property::Value points(Property::ARRAY);
631 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
633 ret.SetProperty( Path::Property::CONTROL_POINTS, points);
639 float curvature(0.25f);
640 if( OptionalFloat pointsProperty = IsFloat( *path, "curvature") )
642 curvature = *pointsProperty;
644 ret.GenerateControlPoints(curvature);
647 //Add the new path to the hash table for paths
648 mPathLut[ name ] = ret;
653 //Interpolation points not specified
654 DALI_SCRIPT_WARNING("Interpolation points not specified for path '%s'\n", name.c_str() );
664 PathConstrainer Builder::GetPathConstrainer( const std::string& name )
666 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
668 //Search the pathConstrainer in the LUT
669 size_t count( mPathConstrainerLut.size() );
670 for( size_t i(0); i!=count; ++i )
672 if( mPathConstrainerLut[i].name == name )
674 //PathConstrainer has already been created
675 return mPathConstrainerLut[i].pathConstrainer;
679 //Create a new PathConstrainer
681 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
683 if( OptionalChild pathConstrainer = IsChild( *constrainers, name ) )
685 OptionalString constrainerType(IsString(IsChild(*pathConstrainer, "type")));
688 DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str() );
690 else if( *constrainerType == "PathConstrainer")
693 if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "points") )
695 Dali::Property::Value points(Property::ARRAY);
696 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
698 ret = PathConstrainer::New();
699 ret.SetProperty( PathConstrainer::Property::POINTS, points);
701 //controlPoints property
702 if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "controlPoints") )
704 Dali::Property::Value points(Property::ARRAY);
705 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
707 ret.SetProperty( PathConstrainer::Property::CONTROL_POINTS, points);
711 OptionalVector3 forward( IsVector3( IsChild(*pathConstrainer, "forward" ) ) );
714 ret.SetProperty( PathConstrainer::Property::FORWARD, *forward);
717 //Add the new constrainer to the vector of PathConstrainer
718 PathConstrainerEntry entry = {name,ret};
719 mPathConstrainerLut.push_back( entry );
723 //Control points not specified
724 DALI_SCRIPT_WARNING("Control points not specified for pathConstrainer '%s'\n", name.c_str() );
730 //Interpolation points not specified
731 DALI_SCRIPT_WARNING("Interpolation points not specified for pathConstrainer '%s'\n", name.c_str() );
736 DALI_SCRIPT_WARNING("Constrainer '%s' is not a PathConstrainer\n", name.c_str() );
744 bool Builder::IsPathConstrainer( const std::string& name )
746 size_t count( mPathConstrainerLut.size() );
747 for( size_t i(0); i!=count; ++i )
749 if( mPathConstrainerLut[i].name == name )
755 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
757 if( OptionalChild constrainer = IsChild( *constrainers, name ) )
759 OptionalString constrainerType(IsString(IsChild(*constrainer, "type")));
766 return *constrainerType == "PathConstrainer";
773 Dali::LinearConstrainer Builder::GetLinearConstrainer( const std::string& name )
775 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
777 //Search the LinearConstrainer in the LUT
778 size_t count( mLinearConstrainerLut.size() );
779 for( size_t i(0); i!=count; ++i )
781 if( mLinearConstrainerLut[i].name == name )
783 //LinearConstrainer has already been created
784 return mLinearConstrainerLut[i].linearConstrainer;
788 //Create a new LinearConstrainer
789 LinearConstrainer ret;
790 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
792 if( OptionalChild linearConstrainer = IsChild( *constrainers, name ) )
794 OptionalString constrainerType(IsString(IsChild(*linearConstrainer, "type")));
797 DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str() );
799 else if( *constrainerType == "LinearConstrainer")
802 if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "value") )
804 Dali::Property::Value points(Property::ARRAY);
805 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
807 ret = Dali::LinearConstrainer::New();
808 ret.SetProperty( LinearConstrainer::Property::VALUE, points);
810 //controlPoints property
811 if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "progress") )
813 Dali::Property::Value points(Property::ARRAY);
814 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
816 ret.SetProperty( LinearConstrainer::Property::PROGRESS, points);
819 //Add the new constrainer to vector of LinearConstrainer
820 LinearConstrainerEntry entry = {name,ret};
821 mLinearConstrainerLut.push_back( entry );
826 //Interpolation points not specified
827 DALI_SCRIPT_WARNING("Values not specified for LinearConstrainer '%s'\n", name.c_str() );
832 DALI_SCRIPT_WARNING("Constrainer '%s' is not a LinearConstrainer\n", name.c_str() );
840 bool Builder::IsLinearConstrainer( const std::string& name )
842 // Search the LinearConstrainer in the LUT
843 size_t count( mLinearConstrainerLut.size() );
844 for( size_t i(0); i!=count; ++i )
846 if( mLinearConstrainerLut[i].name == name )
852 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
854 if( OptionalChild constrainer = IsChild( *constrainers, name ) )
856 OptionalString constrainerType(IsString(IsChild(*constrainer, "type")));
863 return *constrainerType == "LinearConstrainer";
870 Toolkit::Builder::BuilderSignalType& Builder::QuitSignal()
875 void Builder::EmitQuitSignal()
880 void Builder::AddActors( Actor toActor )
882 // 'stage' is the default/by convention section to add from
883 AddActors( "stage", toActor );
886 void Builder::AddActors( const std::string §ionName, Actor toActor )
888 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
890 Property::Map overrideMap;
891 Replacement replacements(overrideMap, mReplacementMap);
893 OptionalChild add = IsChild(*mParser.GetRoot(), sectionName);
897 for( TreeNode::ConstIterator iter = (*add).CBegin(); iter != (*add).CEnd(); ++iter )
899 // empty actor adds directly to the stage
900 BaseHandle baseHandle = DoCreate( *mParser.GetRoot(), (*iter).second, Actor(), replacements );
901 Actor actor = Actor::DownCast(baseHandle);
904 toActor.Add( actor );
908 // if were adding the 'stage' section then also check for a render task called stage
909 // to add automatically
910 if( "stage" == sectionName )
912 if( OptionalChild renderTasks = IsChild(*mParser.GetRoot(), "renderTasks") )
914 if( OptionalChild tasks = IsChild(*renderTasks, "stage") )
916 CreateRenderTask( "stage" );
923 Animation Builder::CreateAnimation( const std::string& animationName, const Replacement& replacement, Dali::Actor sourceActor )
925 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
929 if( OptionalChild animations = IsChild(*mParser.GetRoot(), "animations") )
931 if( OptionalChild animation = IsChild(*animations, animationName) )
933 anim = Dali::Toolkit::Internal::CreateAnimation( *animation, replacement, sourceActor, this );
937 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed\n", animationName.c_str() );
942 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed (no animation section)\n", animationName.c_str() );
948 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor )
950 Replacement replacement(map, mReplacementMap);
951 return CreateAnimation( animationName, replacement, sourceActor);
954 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map )
956 Replacement replacement(map, mReplacementMap);
957 return CreateAnimation( animationName, replacement, Stage::GetCurrent().GetRootLayer() );
960 Animation Builder::CreateAnimation( const std::string& animationName, Dali::Actor sourceActor )
962 Replacement replacement( mReplacementMap );
964 return CreateAnimation( animationName, replacement, sourceActor );
967 Animation Builder::CreateAnimation( const std::string& animationName )
969 Replacement replacement( mReplacementMap );
971 return CreateAnimation( animationName, replacement, Dali::Stage::GetCurrent().GetRootLayer() );
974 bool Builder::ConvertChildValue( const TreeNode& mappingRoot, KeyStack& keyStack, Property::Value& child )
978 switch( child.GetType() )
980 case Property::STRING:
983 if( child.Get( value ) )
986 if( GetMappingKey( value, key ) )
988 // Check key for cycles:
990 for( KeyStack::iterator iter = keyStack.begin() ; iter != keyStack.end(); ++iter )
992 if( key.compare(*iter) == 0 )
994 // key is already in stack; stop.
995 DALI_LOG_WARNING("Detected cycle in stylesheet mapping table:%s\n", key.c_str());
996 child = Property::Value("");
1004 // The following call will overwrite the child with the value
1005 // from the mapping.
1006 RecursePropertyMap( mappingRoot, keyStack, key.c_str(), Property::NONE, child );
1016 Property::Map* map = child.GetMap();
1019 for( Property::Map::SizeType i=0; i < map->Count(); ++i )
1021 Property::Value& child = map->GetValue(i);
1022 ConvertChildValue(mappingRoot, keyStack, child);
1028 case Property::ARRAY:
1030 Property::Array* array = child.GetArray();
1033 for( Property::Array::SizeType i=0; i < array->Count(); ++i )
1035 Property::Value& child = array->GetElementAt(i);
1036 ConvertChildValue(mappingRoot, keyStack, child);
1043 // Ignore other types.
1050 bool Builder::RecursePropertyMap( const TreeNode& mappingRoot, KeyStack& keyStack, const char* theKey, Property::Type propertyType, Property::Value& value )
1052 Replacement replacer( mReplacementMap );
1053 bool result = false;
1055 keyStack.push_back( theKey );
1057 for( TreeNode::ConstIterator iter = mappingRoot.CBegin(); iter != mappingRoot.CEnd(); ++iter )
1059 std::string aKey( (*iter).first );
1060 if( aKey.compare( theKey ) == 0 )
1062 if( propertyType == Property::NONE )
1064 DeterminePropertyFromNode( (*iter).second, value, replacer );
1069 result = DeterminePropertyFromNode( (*iter).second, propertyType, value, replacer );
1074 ConvertChildValue(mappingRoot, keyStack, value);
1079 keyStack.pop_back();
1085 bool Builder::GetPropertyMap( const TreeNode& mappingRoot, const char* theKey, Property::Type propertyType, Property::Value& value )
1088 return RecursePropertyMap( mappingRoot, keyStack, theKey, propertyType, value );
1092 void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::UIFormat format )
1094 // parser to get constants and includes only
1095 Dali::Toolkit::JsonParser parser = Dali::Toolkit::JsonParser::New();
1097 if( !parser.Parse( data ) )
1099 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
1100 parser.GetErrorLineNumber(),
1101 parser.GetErrorColumn(),
1102 parser.GetErrorDescription().c_str() );
1104 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
1108 // load constant map (allows the user to override the constants in the json after loading)
1109 LoadConstants( *parser.GetRoot(), mReplacementMap );
1112 if( OptionalChild includes = IsChild(*parser.GetRoot(), KEYNAME_INCLUDES) )
1114 Replacement replacer( mReplacementMap );
1116 for(TreeNode::ConstIterator iter = (*includes).CBegin(); iter != (*includes).CEnd(); ++iter)
1118 OptionalString filename = replacer.IsString( (*iter).second );
1122 #if defined(DEBUG_ENABLED)
1123 DALI_SCRIPT_VERBOSE("Loading Include '%s'\n", (*filename).c_str());
1125 LoadFromString( GetFileContents(*filename) );
1130 if( !mParser.Parse( data ) )
1132 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
1133 mParser.GetErrorLineNumber(),
1134 mParser.GetErrorColumn(),
1135 mParser.GetErrorDescription().c_str() );
1137 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
1141 DUMP_PARSE_TREE(parser); // This macro only writes out if DEBUG is enabled and the "DUMP_TREE" constant is defined in the stylesheet.
1142 DUMP_TEST_MAPPINGS(parser);
1144 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Cannot parse JSON");
1147 void Builder::AddConstants( const Property::Map& map )
1149 mReplacementMap.Merge( map );
1152 void Builder::AddConstant( const std::string& key, const Property::Value& value )
1154 mReplacementMap[key] = value;
1157 const Property::Map& Builder::GetConstants() const
1159 return mReplacementMap;
1162 const Property::Value& Builder::GetConstant( const std::string& key ) const
1164 Property::Value* match = mReplacementMap.Find( key );
1171 static Property::Value invalid;
1176 void Builder::LoadConstants( const TreeNode& root, Property::Map& intoMap )
1178 Replacement replacer(intoMap);
1180 if( OptionalChild constants = IsChild(root, "constants") )
1182 for(TreeNode::ConstIterator iter = (*constants).CBegin();
1183 iter != (*constants).CEnd(); ++iter)
1185 Dali::Property::Value property;
1186 if( (*iter).second.GetName() )
1188 #if defined(DEBUG_ENABLED)
1189 DALI_SCRIPT_VERBOSE("Constant set from json '%s'\n", (*iter).second.GetName());
1191 DeterminePropertyFromNode( (*iter).second, property, replacer );
1192 intoMap[ (*iter).second.GetName() ] = property;
1197 #if defined(DEBUG_ENABLED)
1198 Property::Value* iter = intoMap.Find( "CONFIG_SCRIPT_LOG_LEVEL" );
1199 if( iter && iter->GetType() == Property::STRING )
1201 std::string logLevel( iter->Get< std::string >() );
1202 if( logLevel == "NoLogging" )
1204 gFilterScript->SetLogLevel( Integration::Log::NoLogging );
1206 else if( logLevel == "Concise" )
1208 gFilterScript->SetLogLevel( Integration::Log::Concise );
1210 else if( logLevel == "General" )
1212 gFilterScript->SetLogLevel( Integration::Log::General );
1214 else if( logLevel == "Verbose" )
1216 gFilterScript->SetLogLevel( Integration::Log::Verbose );
1223 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle )
1225 Replacement replacer( mReplacementMap );
1226 return ApplyStyle( styleName, handle, replacer );
1229 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle, const Replacement& replacement )
1231 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1233 OptionalChild styles = IsChild( *mParser.GetRoot(), KEYNAME_STYLES );
1234 OptionalChild style = IsChild( *styles, styleName );
1236 if( styles && style )
1238 ApplyAllStyleProperties( *mParser.GetRoot(), *style, handle, replacement );
1247 BaseHandle Builder::Create( const std::string& templateName, const Property::Map& map )
1249 Replacement replacement( map, mReplacementMap );
1250 return Create( templateName, replacement );
1253 BaseHandle Builder::Create( const std::string& templateName, const Replacement& constant )
1255 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1257 BaseHandle baseHandle;
1259 OptionalChild templates = IsChild(*mParser.GetRoot(), KEYNAME_TEMPLATES);
1263 DALI_SCRIPT_WARNING("No template section found to CreateFromTemplate\n");
1267 OptionalChild childTemplate = IsChild(*templates, templateName);
1270 DALI_SCRIPT_WARNING("Template '%s' does not exist in template section\n", templateName.c_str());
1274 OptionalString type = constant.IsString( IsChild(*childTemplate, KEYNAME_TYPE) );
1278 DALI_SCRIPT_WARNING("Cannot create template '%s' as template section is missing 'type'\n", templateName.c_str());
1282 baseHandle = DoCreate( *mParser.GetRoot(), *childTemplate, Actor(), constant );
1290 BaseHandle Builder::CreateFromJson( const std::string& json )
1294 // merge in new template, hoping no one else has one named '@temp@'
1295 std::string newTemplate =
1296 std::string("{\"templates\":{\"@temp@\":") + \
1300 if( mParser.Parse(newTemplate) )
1302 Replacement replacement( mReplacementMap );
1303 ret = Create( "@temp@", replacement );
1309 bool Builder::ApplyFromJson( Handle& handle, const std::string& json )
1313 // merge new style, hoping no one else has one named '@temp@'
1314 std::string newStyle =
1315 std::string("{\"styles\":{\"@temp@\":") + \
1319 if( mParser.Parse(newStyle) )
1321 Replacement replacement( mReplacementMap );
1322 ret = ApplyStyle( "@temp@", handle, replacement );
1329 BaseHandle Builder::Create( const std::string& templateName )
1331 Replacement replacement( mReplacementMap );
1332 return Create( templateName, replacement );
1336 : mSlotDelegate( this )
1338 mParser = Dali::Toolkit::JsonParser::New();
1340 Property::Map defaultDirs;
1341 defaultDirs[ TOKEN_STRING(DALI_IMAGE_DIR) ] = DALI_IMAGE_DIR;
1342 defaultDirs[ TOKEN_STRING(DALI_SOUND_DIR) ] = DALI_SOUND_DIR;
1343 defaultDirs[ TOKEN_STRING(DALI_STYLE_DIR) ] = DALI_STYLE_DIR;
1344 defaultDirs[ TOKEN_STRING(DALI_STYLE_IMAGE_DIR) ] = DALI_STYLE_IMAGE_DIR;
1346 AddConstants( defaultDirs );
1353 } // namespace Internal
1355 } // namespace Toolkit