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);
61 extern Actor SetupActor( const TreeNode& node, Actor& actor, const Replacement& constant );
63 #if defined(DEBUG_ENABLED)
64 Integration::Log::Filter* gFilterScript = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_SCRIPT");
70 #define TOKEN_STRING(x) #x
72 const std::string KEYNAME_STYLES = "styles";
73 const std::string KEYNAME_TYPE = "type";
74 const std::string KEYNAME_ACTORS = "actors";
75 const std::string KEYNAME_SIGNALS = "signals";
76 const std::string KEYNAME_NAME = "name";
77 const std::string KEYNAME_TEMPLATES = "templates";
78 const std::string KEYNAME_INCLUDES = "includes";
79 const std::string KEYNAME_MAPPINGS = "mappings";
81 const std::string PROPERTIES = "properties";
82 const std::string ANIMATABLE_PROPERTIES = "animatableProperties";
84 typedef std::vector<const TreeNode*> TreeNodeList;
87 bool GetMappingKey( const std::string& str, std::string& key )
90 std::string test( str );
93 if( test.at(0) == '<' )
95 if( test.at(test.length()-1) == '>' )
97 key = test.substr( 1, test.length()-2 );
106 * Recursively collects all stylesin a node (An array of style names).
108 * stylesCollection The set of styles from the json file (a json object of named styles)
109 * style The style array to begin the collection from
110 * styleList The style list to add nodes to apply
112 void CollectAllStyles( const TreeNode& stylesCollection, const TreeNode& style, TreeNodeList& styleList )
114 // style is an array of style names
115 if( TreeNode::ARRAY == style.GetType() )
117 for(TreeNode::ConstIterator iter = style.CBegin(); iter != style.CEnd(); ++iter)
119 if( OptionalString styleName = IsString( (*iter).second ) )
121 if( OptionalChild node = IsChild( stylesCollection, *styleName) )
123 styleList.push_back( &(*node) );
125 if( OptionalChild subStyle = IsChild( *node, KEYNAME_STYLES ) )
127 CollectAllStyles( stylesCollection, *subStyle, styleList );
139 * Sets the handle properties found in the tree node
141 void Builder::SetProperties( const TreeNode& node, Handle& handle, const Replacement& constant )
146 for( TreeNode::ConstIterator iter = node.CBegin(); iter != node.CEnd(); ++iter )
148 const TreeNode::KeyNodePair& keyChild = *iter;
150 std::string key( keyChild.first );
152 // ignore special fields; type,actors,signals,styles
153 if(key == KEYNAME_TYPE || key == KEYNAME_ACTORS || key == KEYNAME_SIGNALS || key == KEYNAME_STYLES || key == KEYNAME_MAPPINGS )
158 Handle propertyObject( handle );
160 Dali::Property::Index index = propertyObject.GetPropertyIndex( key );
162 if( Property::INVALID_INDEX != index )
164 Property::Type type = propertyObject.GetPropertyType(index);
165 Property::Value value;
168 // if node.value is a mapping, get the property value from the "mappings" table
169 if( keyChild.second.GetType() == TreeNode::STRING )
171 std::string mappingKey;
172 if( GetMappingKey(keyChild.second.GetString(), mappingKey) )
174 OptionalChild mappingRoot = IsChild( mParser.GetRoot(), KEYNAME_MAPPINGS );
175 mapped = GetPropertyMap( *mappingRoot, mappingKey.c_str(), type, value );
180 mapped = DeterminePropertyFromNode( keyChild.second, type, value, constant );
183 // verbose as this might not be a problem
184 // eg parentOrigin can be a string which is picked up later
185 DALI_SCRIPT_VERBOSE("Could not convert property:%s\n", key.c_str());
190 DALI_SCRIPT_VERBOSE("SetProperty '%s' Index=:%d Value Type=%d Value '%s'\n", key.c_str(), index, value.GetType(), PropertyValueToString(value).c_str() );
192 propertyObject.SetProperty( index, value );
197 DALI_SCRIPT_VERBOSE("SetProperty INVALID '%s' Index=:%d\n", key.c_str(), index);
200 // Add custom properties
201 SetCustomProperties(node, handle, constant, PROPERTIES, Property::READ_WRITE);
202 SetCustomProperties(node, handle, constant, ANIMATABLE_PROPERTIES, Property::ANIMATABLE);
204 } // for property nodes
208 DALI_SCRIPT_WARNING("Style applied to empty handle\n");
212 void Builder::SetCustomProperties( const TreeNode& node, Handle& handle, const Replacement& constant,
213 const std::string& childName, Property::AccessMode accessMode )
215 // Add custom properties
216 if( OptionalChild customPropertiesChild = IsChild(node, childName) )
218 const TreeNode& customPropertiesNode = *customPropertiesChild;
219 const TreeConstIter endIter = customPropertiesNode.CEnd();
220 for( TreeConstIter iter = customPropertiesNode.CBegin(); endIter != iter; ++iter )
222 const TreeNode::KeyNodePair& keyChild = *iter;
223 std::string key( keyChild.first );
225 Property::Value value;
226 DeterminePropertyFromNode( keyChild.second, value, constant );
227 // Register/Set property.
228 handle.RegisterProperty( key, value, accessMode );
233 // Set properties from node on handle.
234 void Builder::ApplyProperties( const TreeNode& root, const TreeNode& node,
235 Dali::Handle& handle, const Replacement& constant )
237 if( Actor actor = Actor::DownCast(handle) )
239 SetProperties( node, actor, constant );
243 SetupActor( node, actor, constant );
246 SetupSignalAction( mSlotDelegate.GetConnectionTracker(), root, node, actor, this );
247 SetupPropertyNotification( mSlotDelegate.GetConnectionTracker(), root, node, actor, this );
252 SetProperties( node, handle, constant );
256 // Appling by style helper
257 // use FindChildByName() to apply properties referenced in KEYNAME_ACTORS in the node
258 void Builder::ApplyStylesByActor( const TreeNode& root, const TreeNode& node,
259 Dali::Handle& handle, const Replacement& constant )
261 if( Dali::Actor actor = Dali::Actor::DownCast( handle ) )
263 if( const TreeNode* actors = node.GetChild( KEYNAME_ACTORS ) )
265 // in a style the actor subtree properties referenced by actor name
266 for( TreeConstIter iter = actors->CBegin(); iter != actors->CEnd(); ++iter )
268 Dali::Actor foundActor;
272 foundActor = actor.FindChildByName( (*iter).first );
277 // debug log cannot find searched for actor
278 #if defined(DEBUG_ENABLED)
279 DALI_SCRIPT_VERBOSE("Cannot find actor in style application '%s'\n", (*iter).first);
284 #if defined(DEBUG_ENABLED)
285 DALI_SCRIPT_VERBOSE("Styles applied to actor '%s'\n", (*iter).first);
287 ApplyProperties( root, (*iter).second, foundActor, constant );
295 void Builder::ApplyAllStyleProperties( const TreeNode& root, const TreeNode& node,
296 Dali::Handle& handle, const Replacement& constant )
298 OptionalChild styles = IsChild(root, KEYNAME_STYLES);
299 OptionalChild style = IsChild(node, KEYNAME_STYLES);
301 if( styles && style )
303 TreeNodeList additionalStyles;
305 CollectAllStyles( *styles, *style, additionalStyles );
307 #if defined(DEBUG_ENABLED)
308 for(TreeNode::ConstIterator iter = (*style).CBegin(); iter != (*style).CEnd(); ++iter)
310 if( OptionalString styleName = IsString( (*iter).second ) )
312 DALI_SCRIPT_VERBOSE("Style Applied '%s'\n", (*styleName).c_str());
317 // a style may have other styles, which has other styles etc so we apply in reverse by convention.
318 for(TreeNodeList::reverse_iterator iter = additionalStyles.rbegin(); iter != additionalStyles.rend(); ++iter)
320 ApplyProperties( root, *(*iter), handle, constant );
322 ApplyStylesByActor( root, *(*iter), handle, constant );
326 // applying given node last
327 ApplyProperties( root, node, handle, constant );
329 ApplyStylesByActor( root, node, handle, constant );
335 * Create a dali type from a node.
336 * If parent given and an actor type was created then add it to the parent and
337 * recursively add nodes children.
339 BaseHandle Builder::DoCreate( const TreeNode& root, const TreeNode& node,
340 Actor parent, const Replacement& replacements )
342 BaseHandle baseHandle;
344 const TreeNode* templateNode = NULL;
346 if( OptionalString typeName = IsString(node, KEYNAME_TYPE) )
348 typeInfo = TypeRegistry::Get().GetTypeInfo( *typeName );
352 // a template name is also allowed inplace of the type name
353 OptionalChild templates = IsChild( root, KEYNAME_TEMPLATES);
357 if( OptionalChild isTemplate = IsChild( *templates, *typeName ) )
359 templateNode = &(*isTemplate);
361 if( OptionalString templateTypeName = IsString(*templateNode, KEYNAME_TYPE) )
363 typeInfo = TypeRegistry::Get().GetTypeInfo( *templateTypeName );
372 DALI_SCRIPT_WARNING("Cannot create Dali type from node '%s'\n", node.GetName());
376 baseHandle = typeInfo.CreateInstance();
377 Handle handle = Handle::DownCast(baseHandle);
378 Actor actor = Actor::DownCast(handle);
383 DALI_SCRIPT_VERBOSE("Create:%s\n", typeInfo.GetName().c_str());
385 #if defined(DEBUG_ENABLED)
388 DALI_SCRIPT_VERBOSE(" Is Handle Object=%d\n", (long*)handle.GetObjectPtr());
389 DALI_SCRIPT_VERBOSE(" Is Handle Property Count=%d\n", handle.GetPropertyCount());
394 DALI_SCRIPT_VERBOSE(" Is Actor id=%d\n", actor.GetId());
397 Toolkit::Control control = Toolkit::Control::DownCast(handle);
400 DALI_SCRIPT_VERBOSE(" Is Control id=%d\n", actor.GetId());
402 #endif // DEBUG_ENABLED
406 ApplyProperties( root, *templateNode, handle, replacements );
408 if( OptionalChild actors = IsChild( *templateNode, KEYNAME_ACTORS ) )
410 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
412 DoCreate( root, (*iter).second, actor, replacements );
419 // add children of all the styles
420 if( OptionalChild actors = IsChild( node, KEYNAME_ACTORS ) )
422 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
424 DoCreate( root, (*iter).second, actor, replacements );
428 // apply style on top as they need the children to exist
429 ApplyAllStyleProperties( root, node, actor, replacements );
431 // then add to parent
439 ApplyProperties( root, node, handle, replacements );
444 DALI_SCRIPT_WARNING("Cannot create handle from type '%s'\n", typeInfo.GetName().c_str());
451 void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replacement& constant )
453 const Stage& stage = Stage::GetCurrent();
454 Layer root = stage.GetRootLayer();
456 if( OptionalString s = constant.IsString( IsChild(node, "sourceActor") ) )
458 Actor actor = root.FindChildByName(*s);
461 task.SetSourceActor( actor );
465 DALI_SCRIPT_WARNING("Cannot find source actor on stage for render task called '%s'\n", (*s).c_str() );
469 if( OptionalString s = constant.IsString( IsChild(node, "cameraActor") ) )
471 CameraActor actor = CameraActor::DownCast( root.FindChildByName(*s) );
474 task.SetCameraActor( actor );
478 DALI_SCRIPT_WARNING("Cannot find camera actor on stage for render task called '%s'\n", (*s).c_str() );
482 if( OptionalString s = constant.IsString( IsChild(node, "targetFrameBuffer") ) )
484 FrameBufferImage fb = GetFrameBufferImage( *s, constant );
487 task.SetTargetFrameBuffer( fb );
491 DALI_SCRIPT_WARNING("Cannot find target frame buffer '%s'\n", (*s).c_str() );
495 if( OptionalString s = constant.IsString( IsChild(node, "screenToFrameBufferFunction") ) )
497 if("DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION" == *s)
499 task.SetScreenToFrameBufferFunction( RenderTask::DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION );
501 else if("FULLSCREEN_FRAMEBUFFER_FUNCTION" == *s)
503 task.SetScreenToFrameBufferFunction( RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION );
507 DALI_SCRIPT_WARNING("todo");
511 // other setup is via the property system
512 SetProperties( node, task, constant );
515 void Builder::CreateRenderTask( const std::string &name )
517 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
519 Replacement constant(mReplacementMap);
521 const Stage& stage = Stage::GetCurrent();
523 OptionalChild tasks = IsChild(*mParser.GetRoot(), "renderTasks");
528 // Create the tasks from the current task as generally we want
529 // to setup task zero and onwards. Although this does overwrite
530 // the properties of the current task.
532 if( OptionalChild renderTask = IsChild(*tasks, name ) )
534 RenderTaskList list = stage.GetRenderTaskList();
535 unsigned int start = list.GetTaskCount();
540 // zero should have already been created by the stage so really
541 // this case should never happen
542 task = list.CreateTask();
546 TreeNode::ConstIterator iter = (*renderTask).CBegin();
547 task = list.GetTask( start - 1 );
549 SetupTask( task, (*iter).second, constant );
553 for(; iter != (*renderTask).CEnd(); ++iter )
555 task = list.CreateTask();
556 SetupTask( task, (*iter).second, constant );
562 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name )
564 Replacement constant( mReplacementMap );
565 return GetFrameBufferImage(name, constant);
568 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name, const Replacement& constant )
570 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
572 FrameBufferImage ret;
574 ImageLut::const_iterator iter( mFrameBufferImageLut.find( name ) );
575 if( iter != mFrameBufferImageLut.end() )
581 if( OptionalChild images = IsChild( *mParser.GetRoot(), "frameBufferImages") )
583 if( OptionalChild image = IsChild( *images, name ) )
585 Dali::Property::Value property(Property::MAP);
586 if( DeterminePropertyFromNode( *image, Property::MAP, property, constant ) )
588 Property::Map* map = property.GetMap();
592 (*map)[ KEYNAME_TYPE ] = Property::Value(std::string("FrameBufferImage") );
593 ret = FrameBufferImage::DownCast( Dali::Scripting::NewImage( property ) );
594 mFrameBufferImageLut[ name ] = ret;
604 Path Builder::GetPath( const std::string& name )
606 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
610 PathLut::const_iterator iter( mPathLut.find( name ) );
611 if( iter != mPathLut.end() )
617 if( OptionalChild paths = IsChild( *mParser.GetRoot(), "paths") )
619 if( OptionalChild path = IsChild( *paths, name ) )
622 if( OptionalChild pointsProperty = IsChild( *path, "points") )
624 Dali::Property::Value points(Property::ARRAY);
625 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
628 ret.SetProperty( Path::Property::POINTS, points);
630 //controlPoints property
631 if( OptionalChild pointsProperty = IsChild( *path, "controlPoints") )
633 Dali::Property::Value points(Property::ARRAY);
634 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
636 ret.SetProperty( Path::Property::CONTROL_POINTS, points);
642 float curvature(0.25f);
643 if( OptionalFloat pointsProperty = IsFloat( *path, "curvature") )
645 curvature = *pointsProperty;
647 ret.GenerateControlPoints(curvature);
650 //Add the new path to the hash table for paths
651 mPathLut[ name ] = ret;
656 //Interpolation points not specified
657 DALI_SCRIPT_WARNING("Interpolation points not specified for path '%s'\n", name.c_str() );
667 PathConstrainer Builder::GetPathConstrainer( const std::string& name )
669 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
671 //Search the pathConstrainer in the LUT
672 size_t count( mPathConstrainerLut.size() );
673 for( size_t i(0); i!=count; ++i )
675 if( mPathConstrainerLut[i].name == name )
677 //PathConstrainer has already been created
678 return mPathConstrainerLut[i].pathConstrainer;
682 //Create a new PathConstrainer
684 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
686 if( OptionalChild pathConstrainer = IsChild( *constrainers, name ) )
688 OptionalString constrainerType(IsString(IsChild(*pathConstrainer, "type")));
691 DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str() );
693 else if( *constrainerType == "PathConstrainer")
696 if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "points") )
698 Dali::Property::Value points(Property::ARRAY);
699 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
701 ret = PathConstrainer::New();
702 ret.SetProperty( PathConstrainer::Property::POINTS, points);
704 //controlPoints property
705 if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "controlPoints") )
707 Dali::Property::Value points(Property::ARRAY);
708 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
710 ret.SetProperty( PathConstrainer::Property::CONTROL_POINTS, points);
714 OptionalVector3 forward( IsVector3( IsChild(*pathConstrainer, "forward" ) ) );
717 ret.SetProperty( PathConstrainer::Property::FORWARD, *forward);
720 //Add the new constrainer to the vector of PathConstrainer
721 PathConstrainerEntry entry = {name,ret};
722 mPathConstrainerLut.push_back( entry );
726 //Control points not specified
727 DALI_SCRIPT_WARNING("Control points not specified for pathConstrainer '%s'\n", name.c_str() );
733 //Interpolation points not specified
734 DALI_SCRIPT_WARNING("Interpolation points not specified for pathConstrainer '%s'\n", name.c_str() );
739 DALI_SCRIPT_WARNING("Constrainer '%s' is not a PathConstrainer\n", name.c_str() );
747 bool Builder::IsPathConstrainer( const std::string& name )
749 size_t count( mPathConstrainerLut.size() );
750 for( size_t i(0); i!=count; ++i )
752 if( mPathConstrainerLut[i].name == name )
758 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
760 if( OptionalChild constrainer = IsChild( *constrainers, name ) )
762 OptionalString constrainerType(IsString(IsChild(*constrainer, "type")));
769 return *constrainerType == "PathConstrainer";
776 Dali::LinearConstrainer Builder::GetLinearConstrainer( const std::string& name )
778 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
780 //Search the LinearConstrainer in the LUT
781 size_t count( mLinearConstrainerLut.size() );
782 for( size_t i(0); i!=count; ++i )
784 if( mLinearConstrainerLut[i].name == name )
786 //LinearConstrainer has already been created
787 return mLinearConstrainerLut[i].linearConstrainer;
791 //Create a new LinearConstrainer
792 LinearConstrainer ret;
793 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
795 if( OptionalChild linearConstrainer = IsChild( *constrainers, name ) )
797 OptionalString constrainerType(IsString(IsChild(*linearConstrainer, "type")));
800 DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str() );
802 else if( *constrainerType == "LinearConstrainer")
805 if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "value") )
807 Dali::Property::Value points(Property::ARRAY);
808 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
810 ret = Dali::LinearConstrainer::New();
811 ret.SetProperty( LinearConstrainer::Property::VALUE, points);
813 //controlPoints property
814 if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "progress") )
816 Dali::Property::Value points(Property::ARRAY);
817 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
819 ret.SetProperty( LinearConstrainer::Property::PROGRESS, points);
822 //Add the new constrainer to vector of LinearConstrainer
823 LinearConstrainerEntry entry = {name,ret};
824 mLinearConstrainerLut.push_back( entry );
829 //Interpolation points not specified
830 DALI_SCRIPT_WARNING("Values not specified for LinearConstrainer '%s'\n", name.c_str() );
835 DALI_SCRIPT_WARNING("Constrainer '%s' is not a LinearConstrainer\n", name.c_str() );
843 bool Builder::IsLinearConstrainer( const std::string& name )
845 // Search the LinearConstrainer in the LUT
846 size_t count( mLinearConstrainerLut.size() );
847 for( size_t i(0); i!=count; ++i )
849 if( mLinearConstrainerLut[i].name == name )
855 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
857 if( OptionalChild constrainer = IsChild( *constrainers, name ) )
859 OptionalString constrainerType(IsString(IsChild(*constrainer, "type")));
866 return *constrainerType == "LinearConstrainer";
873 Toolkit::Builder::BuilderSignalType& Builder::QuitSignal()
878 void Builder::EmitQuitSignal()
883 void Builder::AddActors( Actor toActor )
885 // 'stage' is the default/by convention section to add from
886 AddActors( "stage", toActor );
889 void Builder::AddActors( const std::string §ionName, Actor toActor )
891 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
893 Property::Map overrideMap;
894 Replacement replacements(overrideMap, mReplacementMap);
896 OptionalChild add = IsChild(*mParser.GetRoot(), sectionName);
900 for( TreeNode::ConstIterator iter = (*add).CBegin(); iter != (*add).CEnd(); ++iter )
902 // empty actor adds directly to the stage
903 BaseHandle baseHandle = DoCreate( *mParser.GetRoot(), (*iter).second, Actor(), replacements );
904 Actor actor = Actor::DownCast(baseHandle);
907 toActor.Add( actor );
911 // if were adding the 'stage' section then also check for a render task called stage
912 // to add automatically
913 if( "stage" == sectionName )
915 if( OptionalChild renderTasks = IsChild(*mParser.GetRoot(), "renderTasks") )
917 if( OptionalChild tasks = IsChild(*renderTasks, "stage") )
919 CreateRenderTask( "stage" );
926 Animation Builder::CreateAnimation( const std::string& animationName, const Replacement& replacement, Dali::Actor sourceActor )
928 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
932 if( OptionalChild animations = IsChild(*mParser.GetRoot(), "animations") )
934 if( OptionalChild animation = IsChild(*animations, animationName) )
936 anim = Dali::Toolkit::Internal::CreateAnimation( *animation, replacement, sourceActor, this );
940 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed\n", animationName.c_str() );
945 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed (no animation section)\n", animationName.c_str() );
951 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor )
953 Replacement replacement(map, mReplacementMap);
954 return CreateAnimation( animationName, replacement, sourceActor);
957 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map )
959 Replacement replacement(map, mReplacementMap);
960 return CreateAnimation( animationName, replacement, Stage::GetCurrent().GetRootLayer() );
963 Animation Builder::CreateAnimation( const std::string& animationName, Dali::Actor sourceActor )
965 Replacement replacement( mReplacementMap );
967 return CreateAnimation( animationName, replacement, sourceActor );
970 Animation Builder::CreateAnimation( const std::string& animationName )
972 Replacement replacement( mReplacementMap );
974 return CreateAnimation( animationName, replacement, Dali::Stage::GetCurrent().GetRootLayer() );
977 bool Builder::ConvertChildValue( const TreeNode& mappingRoot, KeyStack& keyStack, Property::Value& child )
981 switch( child.GetType() )
983 case Property::STRING:
986 if( child.Get( value ) )
989 if( GetMappingKey( value, key ) )
991 // Check key for cycles:
993 for( KeyStack::iterator iter = keyStack.begin() ; iter != keyStack.end(); ++iter )
995 if( key.compare(*iter) == 0 )
997 // key is already in stack; stop.
998 DALI_LOG_WARNING("Detected cycle in stylesheet mapping table:%s\n", key.c_str());
999 child = Property::Value("");
1007 // The following call will overwrite the child with the value
1008 // from the mapping.
1009 RecursePropertyMap( mappingRoot, keyStack, key.c_str(), Property::NONE, child );
1019 Property::Map* map = child.GetMap();
1020 for( Property::Map::SizeType i=0; i < map->Count(); ++i )
1022 Property::Value& child = map->GetValue(i);
1023 ConvertChildValue(mappingRoot, keyStack, child);
1028 case Property::ARRAY:
1030 Property::Array* array = child.GetArray();
1031 for( Property::Array::SizeType i=0; i < array->Count(); ++i )
1033 Property::Value& child = array->GetElementAt(i);
1034 ConvertChildValue(mappingRoot, keyStack, child);
1040 // Ignore other types.
1047 bool Builder::RecursePropertyMap( const TreeNode& mappingRoot, KeyStack& keyStack, const char* theKey, Property::Type propertyType, Property::Value& value )
1049 Replacement replacer( mReplacementMap );
1050 bool result = false;
1052 keyStack.push_back( theKey );
1054 for( TreeNode::ConstIterator iter = mappingRoot.CBegin(); iter != mappingRoot.CEnd(); ++iter )
1056 std::string aKey( (*iter).first );
1057 if( aKey.compare( theKey ) == 0 )
1059 if( propertyType == Property::NONE )
1061 DeterminePropertyFromNode( (*iter).second, value, replacer );
1066 result = DeterminePropertyFromNode( (*iter).second, propertyType, value, replacer );
1071 ConvertChildValue(mappingRoot, keyStack, value);
1076 keyStack.pop_back();
1082 bool Builder::GetPropertyMap( const TreeNode& mappingRoot, const char* theKey, Property::Type propertyType, Property::Value& value )
1085 return RecursePropertyMap( mappingRoot, keyStack, theKey, propertyType, value );
1089 void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::UIFormat format )
1091 // parser to get constants and includes only
1092 Dali::Toolkit::JsonParser parser = Dali::Toolkit::JsonParser::New();
1094 if( !parser.Parse( data ) )
1096 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
1097 parser.GetErrorLineNumber(),
1098 parser.GetErrorColumn(),
1099 parser.GetErrorDescription().c_str() );
1101 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
1105 // load constant map (allows the user to override the constants in the json after loading)
1106 LoadConstants( *parser.GetRoot(), mReplacementMap );
1109 if( OptionalChild includes = IsChild(*parser.GetRoot(), KEYNAME_INCLUDES) )
1111 Replacement replacer( mReplacementMap );
1113 for(TreeNode::ConstIterator iter = (*includes).CBegin(); iter != (*includes).CEnd(); ++iter)
1115 OptionalString filename = replacer.IsString( (*iter).second );
1119 #if defined(DEBUG_ENABLED)
1120 DALI_SCRIPT_VERBOSE("Loading Include '%s'\n", (*filename).c_str());
1122 LoadFromString( GetFileContents(*filename) );
1127 if( !mParser.Parse( data ) )
1129 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
1130 mParser.GetErrorLineNumber(),
1131 mParser.GetErrorColumn(),
1132 mParser.GetErrorDescription().c_str() );
1134 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
1138 DUMP_PARSE_TREE(parser); // This macro only writes out if DEBUG is enabled and the "DUMP_TREE" constant is defined in the stylesheet.
1139 DUMP_TEST_MAPPINGS(parser);
1141 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Cannot parse JSON");
1144 void Builder::AddConstants( const Property::Map& map )
1146 mReplacementMap.Merge( map );
1149 void Builder::AddConstant( const std::string& key, const Property::Value& value )
1151 mReplacementMap[key] = value;
1154 const Property::Map& Builder::GetConstants() const
1156 return mReplacementMap;
1159 const Property::Value& Builder::GetConstant( const std::string& key ) const
1161 Property::Value* match = mReplacementMap.Find( key );
1168 static Property::Value invalid;
1173 void Builder::LoadConstants( const TreeNode& root, Property::Map& intoMap )
1175 Replacement replacer(intoMap);
1177 if( OptionalChild constants = IsChild(root, "constants") )
1179 for(TreeNode::ConstIterator iter = (*constants).CBegin();
1180 iter != (*constants).CEnd(); ++iter)
1182 Dali::Property::Value property;
1183 if( (*iter).second.GetName() )
1185 #if defined(DEBUG_ENABLED)
1186 DALI_SCRIPT_VERBOSE("Constant set from json '%s'\n", (*iter).second.GetName());
1188 DeterminePropertyFromNode( (*iter).second, property, replacer );
1189 intoMap[ (*iter).second.GetName() ] = property;
1194 #if defined(DEBUG_ENABLED)
1195 Property::Value* iter = intoMap.Find( "CONFIG_SCRIPT_LOG_LEVEL" );
1196 if( iter && iter->GetType() == Property::STRING )
1198 std::string logLevel( iter->Get< std::string >() );
1199 if( logLevel == "NoLogging" )
1201 gFilterScript->SetLogLevel( Integration::Log::NoLogging );
1203 else if( logLevel == "Concise" )
1205 gFilterScript->SetLogLevel( Integration::Log::Concise );
1207 else if( logLevel == "General" )
1209 gFilterScript->SetLogLevel( Integration::Log::General );
1211 else if( logLevel == "Verbose" )
1213 gFilterScript->SetLogLevel( Integration::Log::Verbose );
1220 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle )
1222 Replacement replacer( mReplacementMap );
1223 return ApplyStyle( styleName, handle, replacer );
1226 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle, const Replacement& replacement )
1228 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1230 OptionalChild styles = IsChild( *mParser.GetRoot(), KEYNAME_STYLES );
1231 OptionalChild style = IsChild( *styles, styleName );
1233 if( styles && style )
1235 ApplyAllStyleProperties( *mParser.GetRoot(), *style, handle, replacement );
1244 BaseHandle Builder::Create( const std::string& templateName, const Property::Map& map )
1246 Replacement replacement( map, mReplacementMap );
1247 return Create( templateName, replacement );
1250 BaseHandle Builder::Create( const std::string& templateName, const Replacement& constant )
1252 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1254 BaseHandle baseHandle;
1256 OptionalChild templates = IsChild(*mParser.GetRoot(), KEYNAME_TEMPLATES);
1260 DALI_SCRIPT_WARNING("No template section found to CreateFromTemplate\n");
1264 OptionalChild childTemplate = IsChild(*templates, templateName);
1267 DALI_SCRIPT_WARNING("Template '%s' does not exist in template section\n", templateName.c_str());
1271 OptionalString type = constant.IsString( IsChild(*childTemplate, KEYNAME_TYPE) );
1275 DALI_SCRIPT_WARNING("Cannot create template '%s' as template section is missing 'type'\n", templateName.c_str());
1279 baseHandle = DoCreate( *mParser.GetRoot(), *childTemplate, Actor(), constant );
1287 BaseHandle Builder::CreateFromJson( const std::string& json )
1291 // merge in new template, hoping no one else has one named '@temp@'
1292 std::string newTemplate =
1293 std::string("{\"templates\":{\"@temp@\":") + \
1297 if( mParser.Parse(newTemplate) )
1299 Replacement replacement( mReplacementMap );
1300 ret = Create( "@temp@", replacement );
1306 bool Builder::ApplyFromJson( Handle& handle, const std::string& json )
1310 // merge new style, hoping no one else has one named '@temp@'
1311 std::string newStyle =
1312 std::string("{\"styles\":{\"@temp@\":") + \
1316 if( mParser.Parse(newStyle) )
1318 Replacement replacement( mReplacementMap );
1319 ret = ApplyStyle( "@temp@", handle, replacement );
1326 BaseHandle Builder::Create( const std::string& templateName )
1328 Replacement replacement( mReplacementMap );
1329 return Create( templateName, replacement );
1333 : mSlotDelegate( this )
1335 mParser = Dali::Toolkit::JsonParser::New();
1337 Property::Map defaultDirs;
1338 defaultDirs[ TOKEN_STRING(DALI_IMAGE_DIR) ] = DALI_IMAGE_DIR;
1339 defaultDirs[ TOKEN_STRING(DALI_SOUND_DIR) ] = DALI_SOUND_DIR;
1340 defaultDirs[ TOKEN_STRING(DALI_STYLE_DIR) ] = DALI_STYLE_DIR;
1341 defaultDirs[ TOKEN_STRING(DALI_STYLE_IMAGE_DIR) ] = DALI_STYLE_IMAGE_DIR;
1343 AddConstants( defaultDirs );
1350 } // namespace Internal
1352 } // namespace Toolkit