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 typedef std::vector<const TreeNode*> TreeNodeList;
85 bool GetMappingKey( const std::string& str, std::string& key )
88 std::string test( str );
91 if( test.at(0) == '<' )
93 if( test.at(test.length()-1) == '>' )
95 key = test.substr( 1, test.length()-2 );
104 * Recursively collects all stylesin a node (An array of style names).
106 * stylesCollection The set of styles from the json file (a json object of named styles)
107 * style The style array to begin the collection from
108 * styleList The style list to add nodes to apply
110 void CollectAllStyles( const TreeNode& stylesCollection, const TreeNode& style, TreeNodeList& styleList )
112 // style is an array of style names
113 if( TreeNode::ARRAY == style.GetType() )
115 for(TreeNode::ConstIterator iter = style.CBegin(); iter != style.CEnd(); ++iter)
117 if( OptionalString styleName = IsString( (*iter).second ) )
119 if( OptionalChild node = IsChild( stylesCollection, *styleName) )
121 styleList.push_back( &(*node) );
123 if( OptionalChild subStyle = IsChild( *node, KEYNAME_STYLES ) )
125 CollectAllStyles( stylesCollection, *subStyle, styleList );
137 * Sets the handle properties found in the tree node
139 void Builder::SetProperties( const TreeNode& node, Handle& handle, const Replacement& constant )
144 for( TreeNode::ConstIterator iter = node.CBegin(); iter != node.CEnd(); ++iter )
146 const TreeNode::KeyNodePair& keyChild = *iter;
148 std::string key( keyChild.first );
150 // ignore special fields; type,actors,signals,styles
151 if(key == KEYNAME_TYPE || key == KEYNAME_ACTORS || key == KEYNAME_SIGNALS || key == KEYNAME_STYLES || key == KEYNAME_MAPPINGS )
156 // special field 'image' usually contains an json object description
157 // although sometimes refers to a framebuffer
160 if( 0 == keyChild.second.Size() )
162 ImageActor imageActor = ImageActor::DownCast(handle);
165 if( OptionalString s = constant.IsString( keyChild.second ) )
167 FrameBufferImage fb = GetFrameBufferImage(*s, constant);
170 imageActor.SetImage( fb );
177 // special field 'effect' references the shader effect instances
178 if( key == "effect" )
180 ImageActor actor = ImageActor::DownCast(handle);
183 OptionalString str = constant.IsString( keyChild.second );
186 ShaderEffect effect = GetShaderEffect( *str, constant );
187 actor.SetShaderEffect(effect);
192 DALI_SCRIPT_WARNING("Could not find or set shader effect\n");
198 Handle propertyObject( handle );
200 Dali::Property::Index index = propertyObject.GetPropertyIndex( key );
202 if( Property::INVALID_INDEX == index )
204 ImageActor actor = ImageActor::DownCast(handle);
207 if( ShaderEffect effect = actor.GetShaderEffect() )
209 index = effect.GetPropertyIndex( key );
210 if(index != Property::INVALID_INDEX)
212 propertyObject = effect;
218 if( Property::INVALID_INDEX != index )
220 Property::Type type = propertyObject.GetPropertyType(index);
221 Property::Value value;
224 // if node.value is a mapping, get the property value from the "mappings" table
225 if( keyChild.second.GetType() == TreeNode::STRING )
227 std::string mappingKey;
228 if( GetMappingKey(keyChild.second.GetString(), mappingKey) )
230 OptionalChild mappingRoot = IsChild( mParser.GetRoot(), KEYNAME_MAPPINGS );
231 mapped = GetPropertyMap( *mappingRoot, mappingKey.c_str(), type, value );
236 mapped = DeterminePropertyFromNode( keyChild.second, type, value, constant );
239 // verbose as this might not be a problem
240 // eg parentOrigin can be a string which is picked up later
241 DALI_SCRIPT_VERBOSE("Could not convert property:%s\n", key.c_str());
246 DALI_SCRIPT_VERBOSE("SetProperty '%s' Index=:%d Value Type=%d Value '%s'\n", key.c_str(), index, value.GetType(), PropertyValueToString(value).c_str() );
248 propertyObject.SetProperty( index, value );
253 DALI_SCRIPT_VERBOSE("SetProperty INVALID '%s' Index=:%d\n", key.c_str(), index);
256 } // for property nodes
260 DALI_SCRIPT_WARNING("Style applied to empty handle\n");
264 // Set properties from node on handle.
265 void Builder::ApplyProperties( const TreeNode& root, const TreeNode& node,
266 Dali::Handle& handle, const Replacement& constant )
268 if( Actor actor = Actor::DownCast(handle) )
270 SetProperties( node, actor, constant );
274 SetupActor( node, actor, constant );
277 SetupSignalAction( mSlotDelegate.GetConnectionTracker(), root, node, actor, this );
278 SetupPropertyNotification( mSlotDelegate.GetConnectionTracker(), root, node, actor, this );
283 SetProperties( node, handle, constant );
287 // Appling by style helper
288 // use FindChildByName() to apply properties referenced in KEYNAME_ACTORS in the node
289 void Builder::ApplyStylesByActor( const TreeNode& root, const TreeNode& node,
290 Dali::Handle& handle, const Replacement& constant )
292 if( Dali::Actor actor = Dali::Actor::DownCast( handle ) )
294 if( const TreeNode* actors = node.GetChild( KEYNAME_ACTORS ) )
296 // in a style the actor subtree properties referenced by actor name
297 for( TreeConstIter iter = actors->CBegin(); iter != actors->CEnd(); ++iter )
299 Dali::Actor foundActor;
303 foundActor = actor.FindChildByName( (*iter).first );
308 // debug log cannot find searched for actor
309 #if defined(DEBUG_ENABLED)
310 DALI_SCRIPT_VERBOSE("Cannot find actor in style application '%s'\n", (*iter).first);
315 #if defined(DEBUG_ENABLED)
316 DALI_SCRIPT_VERBOSE("Styles applied to actor '%s'\n", (*iter).first);
318 ApplyProperties( root, (*iter).second, foundActor, constant );
326 void Builder::ApplyAllStyleProperties( const TreeNode& root, const TreeNode& node,
327 Dali::Handle& handle, const Replacement& constant )
329 OptionalChild styles = IsChild(root, KEYNAME_STYLES);
330 OptionalChild style = IsChild(node, KEYNAME_STYLES);
332 if( styles && style )
334 TreeNodeList additionalStyles;
336 CollectAllStyles( *styles, *style, additionalStyles );
338 #if defined(DEBUG_ENABLED)
339 for(TreeNode::ConstIterator iter = (*style).CBegin(); iter != (*style).CEnd(); ++iter)
341 if( OptionalString styleName = IsString( (*iter).second ) )
343 DALI_SCRIPT_VERBOSE("Style Applied '%s'\n", (*styleName).c_str());
348 // a style may have other styles, which has other styles etc so we apply in reverse by convention.
349 for(TreeNodeList::reverse_iterator iter = additionalStyles.rbegin(); iter != additionalStyles.rend(); ++iter)
351 ApplyProperties( root, *(*iter), handle, constant );
353 ApplyStylesByActor( root, *(*iter), handle, constant );
357 // applying given node last
358 ApplyProperties( root, node, handle, constant );
360 ApplyStylesByActor( root, node, handle, constant );
366 * Create a dali type from a node.
367 * If parent given and an actor type was created then add it to the parent and
368 * recursively add nodes children.
370 BaseHandle Builder::DoCreate( const TreeNode& root, const TreeNode& node,
371 Actor parent, const Replacement& replacements )
373 BaseHandle baseHandle;
375 const TreeNode* templateNode = NULL;
377 if( OptionalString typeName = IsString(node, KEYNAME_TYPE) )
379 typeInfo = TypeRegistry::Get().GetTypeInfo( *typeName );
383 // a template name is also allowed inplace of the type name
384 OptionalChild templates = IsChild( root, KEYNAME_TEMPLATES);
388 if( OptionalChild isTemplate = IsChild( *templates, *typeName ) )
390 templateNode = &(*isTemplate);
392 if( OptionalString templateTypeName = IsString(*templateNode, KEYNAME_TYPE) )
394 typeInfo = TypeRegistry::Get().GetTypeInfo( *templateTypeName );
403 DALI_SCRIPT_WARNING("Cannot create Dali type from node '%s'\n", node.GetName());
407 baseHandle = typeInfo.CreateInstance();
408 Handle handle = Handle::DownCast(baseHandle);
409 Actor actor = Actor::DownCast(handle);
414 DALI_SCRIPT_VERBOSE("Create:%s\n", typeInfo.GetName().c_str());
416 #if defined(DEBUG_ENABLED)
419 DALI_SCRIPT_VERBOSE(" Is Handle Object=%d\n", (long*)handle.GetObjectPtr());
420 DALI_SCRIPT_VERBOSE(" Is Handle Property Count=%d\n", handle.GetPropertyCount());
425 DALI_SCRIPT_VERBOSE(" Is Actor id=%d\n", actor.GetId());
428 Toolkit::Control control = Toolkit::Control::DownCast(handle);
431 DALI_SCRIPT_VERBOSE(" Is Control id=%d\n", actor.GetId());
433 #endif // DEBUG_ENABLED
437 ApplyProperties( root, *templateNode, handle, replacements );
439 if( OptionalChild actors = IsChild( *templateNode, KEYNAME_ACTORS ) )
441 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
443 DoCreate( root, (*iter).second, actor, replacements );
450 // add children of all the styles
451 if( OptionalChild actors = IsChild( node, KEYNAME_ACTORS ) )
453 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
455 DoCreate( root, (*iter).second, actor, replacements );
459 // apply style on top as they need the children to exist
460 ApplyAllStyleProperties( root, node, actor, replacements );
462 // then add to parent
470 ApplyProperties( root, node, handle, replacements );
475 DALI_SCRIPT_WARNING("Cannot create handle from type '%s'\n", typeInfo.GetName().c_str());
482 void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replacement& constant )
484 const Stage& stage = Stage::GetCurrent();
485 Layer root = stage.GetRootLayer();
487 if( OptionalString s = constant.IsString( IsChild(node, "sourceActor") ) )
489 Actor actor = root.FindChildByName(*s);
492 task.SetSourceActor( actor );
496 DALI_SCRIPT_WARNING("Cannot find source actor on stage for render task called '%s'\n", (*s).c_str() );
500 if( OptionalString s = constant.IsString( IsChild(node, "cameraActor") ) )
502 CameraActor actor = CameraActor::DownCast( root.FindChildByName(*s) );
505 task.SetCameraActor( actor );
509 DALI_SCRIPT_WARNING("Cannot find camera actor on stage for render task called '%s'\n", (*s).c_str() );
513 if( OptionalString s = constant.IsString( IsChild(node, "targetFrameBuffer") ) )
515 FrameBufferImage fb = GetFrameBufferImage( *s, constant );
518 task.SetTargetFrameBuffer( fb );
522 DALI_SCRIPT_WARNING("Cannot find target frame buffer '%s'\n", (*s).c_str() );
526 if( OptionalString s = constant.IsString( IsChild(node, "screenToFrameBufferFunction") ) )
528 if("DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION" == *s)
530 task.SetScreenToFrameBufferFunction( RenderTask::DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION );
532 else if("FULLSCREEN_FRAMEBUFFER_FUNCTION" == *s)
534 task.SetScreenToFrameBufferFunction( RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION );
538 DALI_SCRIPT_WARNING("todo");
542 // other setup is via the property system
543 SetProperties( node, task, constant );
546 void Builder::CreateRenderTask( const std::string &name )
548 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
550 Replacement constant(mReplacementMap);
552 const Stage& stage = Stage::GetCurrent();
554 OptionalChild tasks = IsChild(*mParser.GetRoot(), "renderTasks");
559 // Create the tasks from the current task as generally we want
560 // to setup task zero and onwards. Although this does overwrite
561 // the properties of the current task.
563 if( OptionalChild renderTask = IsChild(*tasks, name ) )
565 RenderTaskList list = stage.GetRenderTaskList();
566 unsigned int start = list.GetTaskCount();
571 // zero should have already been created by the stage so really
572 // this case should never happen
573 task = list.CreateTask();
577 TreeNode::ConstIterator iter = (*renderTask).CBegin();
578 task = list.GetTask( start - 1 );
580 SetupTask( task, (*iter).second, constant );
584 for(; iter != (*renderTask).CEnd(); ++iter )
586 task = list.CreateTask();
587 SetupTask( task, (*iter).second, constant );
593 ShaderEffect Builder::GetShaderEffect( const std::string &name)
595 Replacement constant( mReplacementMap );
596 return GetShaderEffect( name, constant );
599 ShaderEffect Builder::GetShaderEffect( const std::string &name, const Replacement& constant )
601 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
605 ShaderEffectLut::const_iterator iter( mShaderEffectLut.find( name ) );
606 if( iter != mShaderEffectLut.end() )
612 if( OptionalChild effects = IsChild( *mParser.GetRoot(), "shaderEffects") )
614 if( OptionalChild effect = IsChild( *effects, name ) )
616 Dali::Property::Value propertyMap(Property::MAP);
617 if( DeterminePropertyFromNode( *effect, Property::MAP, propertyMap, constant ) )
619 ret = Dali::Scripting::NewShaderEffect( propertyMap );
620 mShaderEffectLut[ name ] = ret;
629 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name )
631 Replacement constant( mReplacementMap );
632 return GetFrameBufferImage(name, constant);
635 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name, const Replacement& constant )
637 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
639 FrameBufferImage ret;
641 ImageLut::const_iterator iter( mFrameBufferImageLut.find( name ) );
642 if( iter != mFrameBufferImageLut.end() )
648 if( OptionalChild images = IsChild( *mParser.GetRoot(), "frameBufferImages") )
650 if( OptionalChild image = IsChild( *images, name ) )
652 Dali::Property::Value property(Property::MAP);
653 if( DeterminePropertyFromNode( *image, Property::MAP, property, constant ) )
655 Property::Map* map = property.GetMap();
659 (*map)[ KEYNAME_TYPE ] = Property::Value(std::string("FrameBufferImage") );
660 ret = FrameBufferImage::DownCast( Dali::Scripting::NewImage( property ) );
661 mFrameBufferImageLut[ name ] = ret;
671 Path Builder::GetPath( const std::string& name )
673 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
677 PathLut::const_iterator iter( mPathLut.find( name ) );
678 if( iter != mPathLut.end() )
684 if( OptionalChild paths = IsChild( *mParser.GetRoot(), "paths") )
686 if( OptionalChild path = IsChild( *paths, name ) )
689 if( OptionalChild pointsProperty = IsChild( *path, "points") )
691 Dali::Property::Value points(Property::ARRAY);
692 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
695 ret.SetProperty( Path::Property::POINTS, points);
697 //controlPoints property
698 if( OptionalChild pointsProperty = IsChild( *path, "controlPoints") )
700 Dali::Property::Value points(Property::ARRAY);
701 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
703 ret.SetProperty( Path::Property::CONTROL_POINTS, points);
709 float curvature(0.25f);
710 if( OptionalFloat pointsProperty = IsFloat( *path, "curvature") )
712 curvature = *pointsProperty;
714 ret.GenerateControlPoints(curvature);
717 //Add the new path to the hash table for paths
718 mPathLut[ name ] = ret;
723 //Interpolation points not specified
724 DALI_SCRIPT_WARNING("Interpolation points not specified for path '%s'\n", name.c_str() );
734 PathConstrainer Builder::GetPathConstrainer( const std::string& name )
736 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
738 //Search the pathConstrainer in the LUT
739 size_t count( mPathConstrainerLut.size() );
740 for( size_t i(0); i!=count; ++i )
742 if( mPathConstrainerLut[i].name == name )
744 //PathConstrainer has already been created
745 return mPathConstrainerLut[i].pathConstrainer;
749 //Create a new PathConstrainer
751 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
753 if( OptionalChild pathConstrainer = IsChild( *constrainers, name ) )
755 OptionalString constrainerType(IsString(IsChild(*pathConstrainer, "type")));
758 DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str() );
760 else if( *constrainerType == "PathConstrainer")
763 if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "points") )
765 Dali::Property::Value points(Property::ARRAY);
766 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
768 ret = PathConstrainer::New();
769 ret.SetProperty( PathConstrainer::Property::POINTS, points);
771 //controlPoints property
772 if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "controlPoints") )
774 Dali::Property::Value points(Property::ARRAY);
775 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
777 ret.SetProperty( PathConstrainer::Property::CONTROL_POINTS, points);
781 OptionalVector3 forward( IsVector3( IsChild(*pathConstrainer, "forward" ) ) );
784 ret.SetProperty( PathConstrainer::Property::FORWARD, *forward);
787 //Add the new constrainer to the vector of PathConstrainer
788 PathConstrainerEntry entry = {name,ret};
789 mPathConstrainerLut.push_back( entry );
793 //Control points not specified
794 DALI_SCRIPT_WARNING("Control points not specified for pathConstrainer '%s'\n", name.c_str() );
800 //Interpolation points not specified
801 DALI_SCRIPT_WARNING("Interpolation points not specified for pathConstrainer '%s'\n", name.c_str() );
806 DALI_SCRIPT_WARNING("Constrainer '%s' is not a PathConstrainer\n", name.c_str() );
814 bool Builder::IsPathConstrainer( const std::string& name )
816 size_t count( mPathConstrainerLut.size() );
817 for( size_t i(0); i!=count; ++i )
819 if( mPathConstrainerLut[i].name == name )
825 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
827 if( OptionalChild constrainer = IsChild( *constrainers, name ) )
829 OptionalString constrainerType(IsString(IsChild(*constrainer, "type")));
836 return *constrainerType == "PathConstrainer";
843 Dali::LinearConstrainer Builder::GetLinearConstrainer( const std::string& name )
845 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
847 //Search the LinearConstrainer in the LUT
848 size_t count( mLinearConstrainerLut.size() );
849 for( size_t i(0); i!=count; ++i )
851 if( mLinearConstrainerLut[i].name == name )
853 //LinearConstrainer has already been created
854 return mLinearConstrainerLut[i].linearConstrainer;
858 //Create a new LinearConstrainer
859 LinearConstrainer ret;
860 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
862 if( OptionalChild linearConstrainer = IsChild( *constrainers, name ) )
864 OptionalString constrainerType(IsString(IsChild(*linearConstrainer, "type")));
867 DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str() );
869 else if( *constrainerType == "LinearConstrainer")
872 if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "value") )
874 Dali::Property::Value points(Property::ARRAY);
875 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
877 ret = Dali::LinearConstrainer::New();
878 ret.SetProperty( LinearConstrainer::Property::VALUE, points);
880 //controlPoints property
881 if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "progress") )
883 Dali::Property::Value points(Property::ARRAY);
884 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
886 ret.SetProperty( LinearConstrainer::Property::PROGRESS, points);
889 //Add the new constrainer to vector of LinearConstrainer
890 LinearConstrainerEntry entry = {name,ret};
891 mLinearConstrainerLut.push_back( entry );
896 //Interpolation points not specified
897 DALI_SCRIPT_WARNING("Values not specified for LinearConstrainer '%s'\n", name.c_str() );
902 DALI_SCRIPT_WARNING("Constrainer '%s' is not a LinearConstrainer\n", name.c_str() );
910 bool Builder::IsLinearConstrainer( const std::string& name )
912 // Search the LinearConstrainer in the LUT
913 size_t count( mLinearConstrainerLut.size() );
914 for( size_t i(0); i!=count; ++i )
916 if( mLinearConstrainerLut[i].name == name )
922 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
924 if( OptionalChild constrainer = IsChild( *constrainers, name ) )
926 OptionalString constrainerType(IsString(IsChild(*constrainer, "type")));
933 return *constrainerType == "LinearConstrainer";
940 Toolkit::Builder::BuilderSignalType& Builder::QuitSignal()
945 void Builder::EmitQuitSignal()
950 void Builder::AddActors( Actor toActor )
952 // 'stage' is the default/by convention section to add from
953 AddActors( "stage", toActor );
956 void Builder::AddActors( const std::string §ionName, Actor toActor )
958 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
960 Property::Map overrideMap;
961 Replacement replacements(overrideMap, mReplacementMap);
963 OptionalChild add = IsChild(*mParser.GetRoot(), sectionName);
967 for( TreeNode::ConstIterator iter = (*add).CBegin(); iter != (*add).CEnd(); ++iter )
969 // empty actor adds directly to the stage
970 BaseHandle baseHandle = DoCreate( *mParser.GetRoot(), (*iter).second, Actor(), replacements );
971 Actor actor = Actor::DownCast(baseHandle);
974 toActor.Add( actor );
978 // if were adding the 'stage' section then also check for a render task called stage
979 // to add automatically
980 if( "stage" == sectionName )
982 if( OptionalChild renderTasks = IsChild(*mParser.GetRoot(), "renderTasks") )
984 if( OptionalChild tasks = IsChild(*renderTasks, "stage") )
986 CreateRenderTask( "stage" );
993 Animation Builder::CreateAnimation( const std::string& animationName, const Replacement& replacement, Dali::Actor sourceActor )
995 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
999 if( OptionalChild animations = IsChild(*mParser.GetRoot(), "animations") )
1001 if( OptionalChild animation = IsChild(*animations, animationName) )
1003 anim = Dali::Toolkit::Internal::CreateAnimation( *animation, replacement, sourceActor, this );
1007 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed\n", animationName.c_str() );
1012 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed (no animation section)\n", animationName.c_str() );
1018 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor )
1020 Replacement replacement(map, mReplacementMap);
1021 return CreateAnimation( animationName, replacement, sourceActor);
1024 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map )
1026 Replacement replacement(map, mReplacementMap);
1027 return CreateAnimation( animationName, replacement, Stage::GetCurrent().GetRootLayer() );
1030 Animation Builder::CreateAnimation( const std::string& animationName, Dali::Actor sourceActor )
1032 Replacement replacement( mReplacementMap );
1034 return CreateAnimation( animationName, replacement, sourceActor );
1037 Animation Builder::CreateAnimation( const std::string& animationName )
1039 Replacement replacement( mReplacementMap );
1041 return CreateAnimation( animationName, replacement, Dali::Stage::GetCurrent().GetRootLayer() );
1044 bool Builder::ConvertChildValue( const TreeNode& mappingRoot, KeyStack& keyStack, Property::Value& child )
1046 bool result = false;
1048 switch( child.GetType() )
1050 case Property::STRING:
1053 if( child.Get( value ) )
1056 if( GetMappingKey( value, key ) )
1058 // Check key for cycles:
1060 for( KeyStack::iterator iter = keyStack.begin() ; iter != keyStack.end(); ++iter )
1062 if( key.compare(*iter) == 0 )
1064 // key is already in stack; stop.
1065 DALI_LOG_WARNING("Detected cycle in stylesheet mapping table:%s\n", key.c_str());
1066 child = Property::Value("");
1074 // The following call will overwrite the child with the value
1075 // from the mapping.
1076 RecursePropertyMap( mappingRoot, keyStack, key.c_str(), Property::NONE, child );
1086 Property::Map* map = child.GetMap();
1087 for( Property::Map::SizeType i=0; i < map->Count(); ++i )
1089 Property::Value& child = map->GetValue(i);
1090 ConvertChildValue(mappingRoot, keyStack, child);
1095 case Property::ARRAY:
1097 Property::Array* array = child.GetArray();
1098 for( Property::Array::SizeType i=0; i < array->Count(); ++i )
1100 Property::Value& child = array->GetElementAt(i);
1101 ConvertChildValue(mappingRoot, keyStack, child);
1107 // Ignore other types.
1114 bool Builder::RecursePropertyMap( const TreeNode& mappingRoot, KeyStack& keyStack, const char* theKey, Property::Type propertyType, Property::Value& value )
1116 Replacement replacer( mReplacementMap );
1117 bool result = false;
1119 keyStack.push_back( theKey );
1121 for( TreeNode::ConstIterator iter = mappingRoot.CBegin(); iter != mappingRoot.CEnd(); ++iter )
1123 std::string aKey( (*iter).first );
1124 if( aKey.compare( theKey ) == 0 )
1126 if( propertyType == Property::NONE )
1128 DeterminePropertyFromNode( (*iter).second, value, replacer );
1133 result = DeterminePropertyFromNode( (*iter).second, propertyType, value, replacer );
1138 ConvertChildValue(mappingRoot, keyStack, value);
1143 keyStack.pop_back();
1149 bool Builder::GetPropertyMap( const TreeNode& mappingRoot, const char* theKey, Property::Type propertyType, Property::Value& value )
1152 return RecursePropertyMap( mappingRoot, keyStack, theKey, propertyType, value );
1156 void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::UIFormat format )
1158 // parser to get constants and includes only
1159 Dali::Toolkit::JsonParser parser = Dali::Toolkit::JsonParser::New();
1161 if( !parser.Parse( data ) )
1163 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
1164 parser.GetErrorLineNumber(),
1165 parser.GetErrorColumn(),
1166 parser.GetErrorDescription().c_str() );
1168 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
1172 // load constant map (allows the user to override the constants in the json after loading)
1173 LoadConstants( *parser.GetRoot(), mReplacementMap );
1176 if( OptionalChild includes = IsChild(*parser.GetRoot(), KEYNAME_INCLUDES) )
1178 Replacement replacer( mReplacementMap );
1180 for(TreeNode::ConstIterator iter = (*includes).CBegin(); iter != (*includes).CEnd(); ++iter)
1182 OptionalString filename = replacer.IsString( (*iter).second );
1186 #if defined(DEBUG_ENABLED)
1187 DALI_SCRIPT_VERBOSE("Loading Include '%s'\n", (*filename).c_str());
1189 LoadFromString( GetFileContents(*filename) );
1194 if( !mParser.Parse( data ) )
1196 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
1197 mParser.GetErrorLineNumber(),
1198 mParser.GetErrorColumn(),
1199 mParser.GetErrorDescription().c_str() );
1201 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
1205 DUMP_PARSE_TREE(parser); // This macro only writes out if DEBUG is enabled and the "DUMP_TREE" constant is defined in the stylesheet.
1206 DUMP_TEST_MAPPINGS(parser);
1208 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Cannot parse JSON");
1211 void Builder::AddConstants( const Property::Map& map )
1213 mReplacementMap.Merge( map );
1216 void Builder::AddConstant( const std::string& key, const Property::Value& value )
1218 mReplacementMap[key] = value;
1221 const Property::Map& Builder::GetConstants() const
1223 return mReplacementMap;
1226 const Property::Value& Builder::GetConstant( const std::string& key ) const
1228 Property::Value* match = mReplacementMap.Find( key );
1235 static Property::Value invalid;
1240 void Builder::LoadConstants( const TreeNode& root, Property::Map& intoMap )
1242 Replacement replacer(intoMap);
1244 if( OptionalChild constants = IsChild(root, "constants") )
1246 for(TreeNode::ConstIterator iter = (*constants).CBegin();
1247 iter != (*constants).CEnd(); ++iter)
1249 Dali::Property::Value property;
1250 if( (*iter).second.GetName() )
1252 #if defined(DEBUG_ENABLED)
1253 DALI_SCRIPT_VERBOSE("Constant set from json '%s'\n", (*iter).second.GetName());
1255 DeterminePropertyFromNode( (*iter).second, property, replacer );
1256 intoMap[ (*iter).second.GetName() ] = property;
1261 #if defined(DEBUG_ENABLED)
1262 Property::Value* iter = intoMap.Find( "CONFIG_SCRIPT_LOG_LEVEL" );
1263 if( iter && iter->GetType() == Property::STRING )
1265 std::string logLevel( iter->Get< std::string >() );
1266 if( logLevel == "NoLogging" )
1268 gFilterScript->SetLogLevel( Integration::Log::NoLogging );
1270 else if( logLevel == "Concise" )
1272 gFilterScript->SetLogLevel( Integration::Log::Concise );
1274 else if( logLevel == "General" )
1276 gFilterScript->SetLogLevel( Integration::Log::General );
1278 else if( logLevel == "Verbose" )
1280 gFilterScript->SetLogLevel( Integration::Log::Verbose );
1287 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle )
1289 Replacement replacer( mReplacementMap );
1290 return ApplyStyle( styleName, handle, replacer );
1293 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle, const Replacement& replacement )
1295 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1297 OptionalChild styles = IsChild( *mParser.GetRoot(), KEYNAME_STYLES );
1298 OptionalChild style = IsChild( *styles, styleName );
1300 if( styles && style )
1302 ApplyAllStyleProperties( *mParser.GetRoot(), *style, handle, replacement );
1311 BaseHandle Builder::Create( const std::string& templateName, const Property::Map& map )
1313 Replacement replacement( map, mReplacementMap );
1314 return Create( templateName, replacement );
1317 BaseHandle Builder::Create( const std::string& templateName, const Replacement& constant )
1319 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1321 BaseHandle baseHandle;
1323 OptionalChild templates = IsChild(*mParser.GetRoot(), KEYNAME_TEMPLATES);
1327 DALI_SCRIPT_WARNING("No template section found to CreateFromTemplate\n");
1331 OptionalChild childTemplate = IsChild(*templates, templateName);
1334 DALI_SCRIPT_WARNING("Template '%s' does not exist in template section\n", templateName.c_str());
1338 OptionalString type = constant.IsString( IsChild(*childTemplate, KEYNAME_TYPE) );
1342 DALI_SCRIPT_WARNING("Cannot create template '%s' as template section is missing 'type'\n", templateName.c_str());
1346 baseHandle = DoCreate( *mParser.GetRoot(), *childTemplate, Actor(), constant );
1354 BaseHandle Builder::CreateFromJson( const std::string& json )
1358 // merge in new template, hoping no one else has one named '@temp@'
1359 std::string newTemplate =
1360 std::string("{\"templates\":{\"@temp@\":") + \
1364 if( mParser.Parse(newTemplate) )
1366 Replacement replacement( mReplacementMap );
1367 ret = Create( "@temp@", replacement );
1373 bool Builder::ApplyFromJson( Handle& handle, const std::string& json )
1377 // merge new style, hoping no one else has one named '@temp@'
1378 std::string newStyle =
1379 std::string("{\"styles\":{\"@temp@\":") + \
1383 if( mParser.Parse(newStyle) )
1385 Replacement replacement( mReplacementMap );
1386 ret = ApplyStyle( "@temp@", handle, replacement );
1393 BaseHandle Builder::Create( const std::string& templateName )
1395 Replacement replacement( mReplacementMap );
1396 return Create( templateName, replacement );
1400 : mSlotDelegate( this )
1402 mParser = Dali::Toolkit::JsonParser::New();
1404 Property::Map defaultDirs;
1405 defaultDirs[ TOKEN_STRING(DALI_IMAGE_DIR) ] = DALI_IMAGE_DIR;
1406 defaultDirs[ TOKEN_STRING(DALI_SOUND_DIR) ] = DALI_SOUND_DIR;
1407 defaultDirs[ TOKEN_STRING(DALI_STYLE_DIR) ] = DALI_STYLE_DIR;
1408 defaultDirs[ TOKEN_STRING(DALI_STYLE_IMAGE_DIR) ] = DALI_STYLE_IMAGE_DIR;
1410 AddConstants( defaultDirs );
1417 } // namespace Internal
1419 } // namespace Toolkit