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/actors/camera-actor.h>
26 #include <dali/public-api/actors/layer.h>
27 #include <dali/public-api/object/property-array.h>
28 #include <dali/public-api/object/type-info.h>
29 #include <dali/public-api/object/type-registry.h>
30 #include <dali/public-api/render-tasks/render-task-list.h>
31 #include <dali/public-api/signals/functor-delegate.h>
32 #include <dali/devel-api/scripting/scripting.h>
33 #include <dali/integration-api/debug.h>
34 #include <dali-toolkit/devel-api/controls/control-devel.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-declarations.h>
41 #include <dali-toolkit/internal/builder/builder-filesystem.h>
42 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
43 #include <dali-toolkit/internal/builder/builder-impl-debug.h>
44 #include <dali-toolkit/internal/builder/builder-set-property.h>
45 #include <dali-toolkit/internal/builder/replacement.h>
46 #include <dali-toolkit/internal/builder/tree-node-manipulator.h>
58 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);
62 extern Actor SetupPropertyNotification(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, Dali::Toolkit::Internal::Builder* const builder);
65 #if defined(DEBUG_ENABLED)
66 Integration::Log::Filter* gFilterScript = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_SCRIPT");
72 #define TOKEN_STRING(x) #x
74 const std::string KEYNAME_ACTORS = "actors";
75 const std::string KEYNAME_INCLUDES = "includes";
76 const std::string KEYNAME_MAPPINGS = "mappings";
77 const std::string KEYNAME_NAME = "name";
78 const std::string KEYNAME_SIGNALS = "signals";
79 const std::string KEYNAME_STYLES = "styles";
80 const std::string KEYNAME_TEMPLATES = "templates";
81 const std::string KEYNAME_TYPE = "type";
83 const std::string PROPERTIES = "properties";
84 const std::string ANIMATABLE_PROPERTIES = "animatableProperties";
86 typedef std::vector<const TreeNode*> TreeNodeList;
89 bool GetMappingKey( const std::string& str, std::string& key )
92 std::string test( str );
95 if( test.at(0) == '<' )
97 if( test.at(test.length()-1) == '>' )
99 key = test.substr( 1, test.length()-2 );
108 * Recursively collects all styles in a node (An array of style names).
110 * stylesCollection The set of styles from the json file (a json object of named styles)
111 * style The style array to begin the collection from
112 * styleList The style list to add nodes to apply
114 void CollectAllStyles( const TreeNode& stylesCollection, const TreeNode& style, TreeNodeList& styleList )
116 // style is an array of style names
117 if( TreeNode::ARRAY == style.GetType() )
119 for(TreeNode::ConstIterator iter = style.CBegin(); iter != style.CEnd(); ++iter)
121 if( OptionalString styleName = IsString( (*iter).second ) )
123 if( OptionalChild node = IsChildIgnoreCase( stylesCollection, *styleName) )
125 styleList.push_back( &(*node) );
127 if( OptionalChild subStyle = IsChild( *node, KEYNAME_STYLES ) )
129 CollectAllStyles( stylesCollection, *subStyle, styleList );
142 : mSlotDelegate( this )
144 mParser = Dali::Toolkit::JsonParser::New();
146 Property::Map defaultDirs;
147 defaultDirs[ TOKEN_STRING(DALI_IMAGE_DIR) ] = DALI_IMAGE_DIR;
148 defaultDirs[ TOKEN_STRING(DALI_SOUND_DIR) ] = DALI_SOUND_DIR;
149 defaultDirs[ TOKEN_STRING(DALI_STYLE_DIR) ] = DALI_STYLE_DIR;
150 defaultDirs[ TOKEN_STRING(DALI_STYLE_IMAGE_DIR) ] = DALI_STYLE_IMAGE_DIR;
152 AddConstants( defaultDirs );
155 void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::UIFormat format )
157 // parser to get constants and includes only
158 Dali::Toolkit::JsonParser parser = Dali::Toolkit::JsonParser::New();
160 if( !parser.Parse( data ) )
162 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
163 parser.GetErrorLineNumber(),
164 parser.GetErrorColumn(),
165 parser.GetErrorDescription().c_str() );
167 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
171 // load constant map (allows the user to override the constants in the json after loading)
172 LoadConstants( *parser.GetRoot(), mReplacementMap );
175 if( OptionalChild includes = IsChild(*parser.GetRoot(), KEYNAME_INCLUDES) )
177 Replacement replacer( mReplacementMap );
179 for(TreeNode::ConstIterator iter = (*includes).CBegin(); iter != (*includes).CEnd(); ++iter)
181 OptionalString filename = replacer.IsString( (*iter).second );
185 #if defined(DEBUG_ENABLED)
186 DALI_SCRIPT_VERBOSE("Loading Include '%s'\n", (*filename).c_str());
188 LoadFromString( GetFileContents(*filename) );
193 if( !mParser.Parse( data ) )
195 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
196 mParser.GetErrorLineNumber(),
197 mParser.GetErrorColumn(),
198 mParser.GetErrorDescription().c_str() );
200 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
204 DUMP_PARSE_TREE(parser); // This macro only writes out if DEBUG is enabled and the "DUMP_TREE" constant is defined in the stylesheet.
205 DUMP_TEST_MAPPINGS(parser);
207 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Cannot parse JSON");
210 void Builder::AddConstants( const Property::Map& map )
212 mReplacementMap.Merge( map );
215 void Builder::AddConstant( const std::string& key, const Property::Value& value )
217 mReplacementMap[key] = value;
220 const Property::Map& Builder::GetConstants() const
222 return mReplacementMap;
225 const Property::Value& Builder::GetConstant( const std::string& key ) const
227 Property::Value* match = mReplacementMap.Find( key );
234 static Property::Value invalid;
239 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor )
241 Replacement replacement(map, mReplacementMap);
242 return CreateAnimation( animationName, replacement, sourceActor);
245 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map )
247 Replacement replacement(map, mReplacementMap);
248 return CreateAnimation( animationName, replacement, Stage::GetCurrent().GetRootLayer() );
251 Animation Builder::CreateAnimation( const std::string& animationName, Dali::Actor sourceActor )
253 Replacement replacement( mReplacementMap );
255 return CreateAnimation( animationName, replacement, sourceActor );
258 Animation Builder::CreateAnimation( const std::string& animationName )
260 Replacement replacement( mReplacementMap );
262 return CreateAnimation( animationName, replacement, Dali::Stage::GetCurrent().GetRootLayer() );
265 BaseHandle Builder::Create( const std::string& templateName )
267 Replacement replacement( mReplacementMap );
268 return Create( templateName, replacement );
271 BaseHandle Builder::Create( const std::string& templateName, const Property::Map& map )
273 Replacement replacement( map, mReplacementMap );
274 return Create( templateName, replacement );
277 BaseHandle Builder::CreateFromJson( const std::string& json )
281 // merge in new template, hoping no one else has one named '@temp@'
282 std::string newTemplate =
283 std::string("{\"templates\":{\"@temp@\":") + \
287 if( mParser.Parse(newTemplate) )
289 Replacement replacement( mReplacementMap );
290 ret = Create( "@temp@", replacement );
296 bool Builder::ApplyFromJson( Handle& handle, const std::string& json )
300 // merge new style, hoping no one else has one named '@temp@'
301 std::string newStyle =
302 std::string("{\"styles\":{\"@temp@\":") + \
306 if( mParser.Parse(newStyle) )
308 Replacement replacement( mReplacementMap );
309 ret = ApplyStyle( "@temp@", handle, replacement );
315 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle )
317 Replacement replacer( mReplacementMap );
318 return ApplyStyle( styleName, handle, replacer );
321 void Builder::AddActors( Actor toActor )
323 // 'stage' is the default/by convention section to add from
324 AddActors( "stage", toActor );
327 void Builder::AddActors( const std::string §ionName, Actor toActor )
329 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
331 Property::Map overrideMap;
332 Replacement replacements(overrideMap, mReplacementMap);
334 OptionalChild add = IsChild(*mParser.GetRoot(), sectionName);
338 for( TreeNode::ConstIterator iter = (*add).CBegin(); iter != (*add).CEnd(); ++iter )
340 // empty actor adds directly to the stage
341 BaseHandle baseHandle = DoCreate( *mParser.GetRoot(), (*iter).second, Actor(), replacements );
342 Actor actor = Actor::DownCast(baseHandle);
345 toActor.Add( actor );
349 // if were adding the 'stage' section then also check for a render task called stage
350 // to add automatically
351 if( "stage" == sectionName )
353 if( OptionalChild renderTasks = IsChild(*mParser.GetRoot(), "renderTasks") )
355 if( OptionalChild tasks = IsChild(*renderTasks, "stage") )
357 CreateRenderTask( "stage" );
364 void Builder::CreateRenderTask( const std::string &name )
366 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
368 Replacement constant(mReplacementMap);
370 const Stage& stage = Stage::GetCurrent();
372 OptionalChild tasks = IsChild(*mParser.GetRoot(), "renderTasks");
377 // Create the tasks from the current task as generally we want
378 // to setup task zero and onwards. Although this does overwrite
379 // the properties of the current task.
381 if( OptionalChild renderTask = IsChild(*tasks, name ) )
383 RenderTaskList list = stage.GetRenderTaskList();
384 unsigned int start = list.GetTaskCount();
389 // zero should have already been created by the stage so really
390 // this case should never happen
391 task = list.CreateTask();
395 TreeNode::ConstIterator iter = (*renderTask).CBegin();
396 task = list.GetTask( start - 1 );
398 SetupTask( task, (*iter).second, constant );
402 for(; iter != (*renderTask).CEnd(); ++iter )
404 task = list.CreateTask();
405 SetupTask( task, (*iter).second, constant );
411 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name )
413 Replacement constant( mReplacementMap );
414 return GetFrameBufferImage(name, constant);
417 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name, const Replacement& constant )
419 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
421 FrameBufferImage ret;
423 ImageLut::const_iterator iter( mFrameBufferImageLut.find( name ) );
424 if( iter != mFrameBufferImageLut.end() )
430 if( OptionalChild images = IsChild( *mParser.GetRoot(), "frameBufferImages") )
432 if( OptionalChild image = IsChild( *images, name ) )
434 Dali::Property::Value property(Property::MAP);
435 if( DeterminePropertyFromNode( *image, Property::MAP, property, constant ) )
437 Property::Map* map = property.GetMap();
441 (*map)[ KEYNAME_TYPE ] = Property::Value(std::string("FrameBufferImage") );
442 ret = FrameBufferImage::DownCast( Dali::Scripting::NewImage( property ) );
443 mFrameBufferImageLut[ name ] = ret;
453 Path Builder::GetPath( const std::string& name )
455 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
459 PathLut::const_iterator iter( mPathLut.find( name ) );
460 if( iter != mPathLut.end() )
466 if( OptionalChild paths = IsChild( *mParser.GetRoot(), "paths") )
468 if( OptionalChild path = IsChild( *paths, name ) )
471 if( OptionalChild pointsProperty = IsChild( *path, "points") )
473 Dali::Property::Value points(Property::ARRAY);
474 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
477 ret.SetProperty( Path::Property::POINTS, points);
479 //controlPoints property
480 if( OptionalChild pointsProperty = IsChild( *path, "controlPoints") )
482 Dali::Property::Value points(Property::ARRAY);
483 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
485 ret.SetProperty( Path::Property::CONTROL_POINTS, points);
491 float curvature(0.25f);
492 if( OptionalFloat pointsProperty = IsFloat( *path, "curvature") )
494 curvature = *pointsProperty;
496 ret.GenerateControlPoints(curvature);
499 //Add the new path to the hash table for paths
500 mPathLut[ name ] = ret;
505 //Interpolation points not specified
506 DALI_SCRIPT_WARNING("Interpolation points not specified for path '%s'\n", name.c_str() );
516 PathConstrainer Builder::GetPathConstrainer( const std::string& name )
518 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
520 //Search the pathConstrainer in the LUT
521 size_t count( mPathConstrainerLut.size() );
522 for( size_t i(0); i!=count; ++i )
524 if( mPathConstrainerLut[i].name == name )
526 //PathConstrainer has already been created
527 return mPathConstrainerLut[i].pathConstrainer;
531 //Create a new PathConstrainer
533 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
535 if( OptionalChild pathConstrainer = IsChild( *constrainers, name ) )
537 OptionalString constrainerType(IsString(IsChild(*pathConstrainer, "type")));
540 DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str() );
542 else if( *constrainerType == "PathConstrainer")
545 if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "points") )
547 Dali::Property::Value points(Property::ARRAY);
548 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
550 ret = PathConstrainer::New();
551 ret.SetProperty( PathConstrainer::Property::POINTS, points);
553 //controlPoints property
554 if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "controlPoints") )
556 Dali::Property::Value points(Property::ARRAY);
557 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
559 ret.SetProperty( PathConstrainer::Property::CONTROL_POINTS, points);
563 OptionalVector3 forward( IsVector3( IsChild(*pathConstrainer, "forward" ) ) );
566 ret.SetProperty( PathConstrainer::Property::FORWARD, *forward);
569 //Add the new constrainer to the vector of PathConstrainer
570 PathConstrainerEntry entry = {name,ret};
571 mPathConstrainerLut.push_back( entry );
575 //Control points not specified
576 DALI_SCRIPT_WARNING("Control points not specified for pathConstrainer '%s'\n", name.c_str() );
582 //Interpolation points not specified
583 DALI_SCRIPT_WARNING("Interpolation points not specified for pathConstrainer '%s'\n", name.c_str() );
588 DALI_SCRIPT_WARNING("Constrainer '%s' is not a PathConstrainer\n", name.c_str() );
597 bool Builder::IsPathConstrainer( const std::string& name )
599 size_t count( mPathConstrainerLut.size() );
600 for( size_t i(0); i!=count; ++i )
602 if( mPathConstrainerLut[i].name == name )
608 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
610 if( OptionalChild constrainer = IsChild( *constrainers, name ) )
612 OptionalString constrainerType(IsString(IsChild(*constrainer, "type")));
619 return *constrainerType == "PathConstrainer";
626 Dali::LinearConstrainer Builder::GetLinearConstrainer( const std::string& name )
628 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
630 //Search the LinearConstrainer in the LUT
631 size_t count( mLinearConstrainerLut.size() );
632 for( size_t i(0); i!=count; ++i )
634 if( mLinearConstrainerLut[i].name == name )
636 //LinearConstrainer has already been created
637 return mLinearConstrainerLut[i].linearConstrainer;
641 //Create a new LinearConstrainer
642 LinearConstrainer ret;
643 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
645 if( OptionalChild linearConstrainer = IsChild( *constrainers, name ) )
647 OptionalString constrainerType(IsString(IsChild(*linearConstrainer, "type")));
650 DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str() );
652 else if( *constrainerType == "LinearConstrainer")
655 if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "value") )
657 Dali::Property::Value points(Property::ARRAY);
658 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
660 ret = Dali::LinearConstrainer::New();
661 ret.SetProperty( LinearConstrainer::Property::VALUE, points);
663 //controlPoints property
664 if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "progress") )
666 Dali::Property::Value points(Property::ARRAY);
667 if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
669 ret.SetProperty( LinearConstrainer::Property::PROGRESS, points);
672 //Add the new constrainer to vector of LinearConstrainer
673 LinearConstrainerEntry entry = {name,ret};
674 mLinearConstrainerLut.push_back( entry );
679 //Interpolation points not specified
680 DALI_SCRIPT_WARNING("Values not specified for LinearConstrainer '%s'\n", name.c_str() );
685 DALI_SCRIPT_WARNING("Constrainer '%s' is not a LinearConstrainer\n", name.c_str() );
693 bool Builder::IsLinearConstrainer( const std::string& name )
695 // Search the LinearConstrainer in the LUT
696 size_t count( mLinearConstrainerLut.size() );
697 for( size_t i(0); i!=count; ++i )
699 if( mLinearConstrainerLut[i].name == name )
705 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
707 if( OptionalChild constrainer = IsChild( *constrainers, name ) )
709 OptionalString constrainerType(IsString(IsChild(*constrainer, "type")));
716 return *constrainerType == "LinearConstrainer";
723 Toolkit::Builder::BuilderSignalType& Builder::QuitSignal()
728 void Builder::EmitQuitSignal()
737 void Builder::LoadConstants( const TreeNode& root, Property::Map& intoMap )
739 Replacement replacer(intoMap);
741 if( OptionalChild constants = IsChild(root, "constants") )
743 for(TreeNode::ConstIterator iter = (*constants).CBegin();
744 iter != (*constants).CEnd(); ++iter)
746 Dali::Property::Value property;
747 if( (*iter).second.GetName() )
749 #if defined(DEBUG_ENABLED)
750 DALI_SCRIPT_VERBOSE("Constant set from json '%s'\n", (*iter).second.GetName());
752 DeterminePropertyFromNode( (*iter).second, property, replacer );
753 intoMap[ (*iter).second.GetName() ] = property;
758 #if defined(DEBUG_ENABLED)
759 Property::Value* iter = intoMap.Find( "CONFIG_SCRIPT_LOG_LEVEL" );
760 if( iter && iter->GetType() == Property::STRING )
762 std::string logLevel( iter->Get< std::string >() );
763 if( logLevel == "NoLogging" )
765 gFilterScript->SetLogLevel( Integration::Log::NoLogging );
767 else if( logLevel == "Concise" )
769 gFilterScript->SetLogLevel( Integration::Log::Concise );
771 else if( logLevel == "General" )
773 gFilterScript->SetLogLevel( Integration::Log::General );
775 else if( logLevel == "Verbose" )
777 gFilterScript->SetLogLevel( Integration::Log::Verbose );
783 Animation Builder::CreateAnimation( const std::string& animationName, const Replacement& replacement, Dali::Actor sourceActor )
785 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
789 if( OptionalChild animations = IsChild(*mParser.GetRoot(), "animations") )
791 if( OptionalChild animation = IsChild(*animations, animationName) )
793 anim = Dali::Toolkit::Internal::CreateAnimation( *animation, replacement, sourceActor, this );
797 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed\n", animationName.c_str() );
802 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed (no animation section)\n", animationName.c_str() );
808 BaseHandle Builder::Create( const std::string& templateName, const Replacement& constant )
810 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
812 BaseHandle baseHandle;
814 OptionalChild templates = IsChild(*mParser.GetRoot(), KEYNAME_TEMPLATES);
818 DALI_SCRIPT_WARNING("No template section found to CreateFromTemplate\n");
822 OptionalChild childTemplate = IsChild(*templates, templateName);
825 DALI_SCRIPT_WARNING("Template '%s' does not exist in template section\n", templateName.c_str());
829 OptionalString type = constant.IsString( IsChild(*childTemplate, KEYNAME_TYPE) );
833 DALI_SCRIPT_WARNING("Cannot create template '%s' as template section is missing 'type'\n", templateName.c_str());
837 baseHandle = DoCreate( *mParser.GetRoot(), *childTemplate, Actor(), constant );
846 * Create a dali type from a node.
847 * If parent given and an actor type was created then add it to the parent and
848 * recursively add nodes children.
850 BaseHandle Builder::DoCreate( const TreeNode& root, const TreeNode& node,
851 Actor parent, const Replacement& replacements )
853 BaseHandle baseHandle;
855 const TreeNode* templateNode = NULL;
857 if( OptionalString typeName = IsString(node, KEYNAME_TYPE) )
859 typeInfo = TypeRegistry::Get().GetTypeInfo( *typeName );
863 // a template name is also allowed inplace of the type name
864 OptionalChild templates = IsChild( root, KEYNAME_TEMPLATES);
868 if( OptionalChild isTemplate = IsChild( *templates, *typeName ) )
870 templateNode = &(*isTemplate);
872 if( OptionalString templateTypeName = IsString(*templateNode, KEYNAME_TYPE) )
874 typeInfo = TypeRegistry::Get().GetTypeInfo( *templateTypeName );
883 DALI_SCRIPT_WARNING("Cannot create Dali type from node '%s'\n", node.GetName());
887 baseHandle = typeInfo.CreateInstance();
888 Handle handle = Handle::DownCast(baseHandle);
889 Actor actor = Actor::DownCast(handle);
894 DALI_SCRIPT_VERBOSE("Create:%s\n", typeInfo.GetName().c_str());
896 #if defined(DEBUG_ENABLED)
899 DALI_SCRIPT_VERBOSE(" Is Handle Object=%d\n", (long*)handle.GetObjectPtr());
900 DALI_SCRIPT_VERBOSE(" Is Handle Property Count=%d\n", handle.GetPropertyCount());
905 DALI_SCRIPT_VERBOSE(" Is Actor id=%d\n", actor.GetId());
908 Toolkit::Control control = Toolkit::Control::DownCast(handle);
911 DALI_SCRIPT_VERBOSE(" Is Control id=%d\n", actor.GetId());
913 #endif // DEBUG_ENABLED
917 ApplyProperties( root, *templateNode, handle, replacements );
919 if( OptionalChild actors = IsChild( *templateNode, KEYNAME_ACTORS ) )
921 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
923 DoCreate( root, (*iter).second, actor, replacements );
930 // add children of all the styles
931 if( OptionalChild actors = IsChild( node, KEYNAME_ACTORS ) )
933 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
935 DoCreate( root, (*iter).second, actor, replacements );
939 // apply style on top as they need the children to exist
940 ApplyAllStyleProperties( root, node, actor, replacements );
942 // then add to parent
950 ApplyProperties( root, node, handle, replacements );
955 DALI_SCRIPT_WARNING("Cannot create handle from type '%s'\n", typeInfo.GetName().c_str());
962 void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replacement& constant )
964 const Stage& stage = Stage::GetCurrent();
965 Layer root = stage.GetRootLayer();
967 if( OptionalString s = constant.IsString( IsChild(node, "sourceActor") ) )
969 Actor actor = root.FindChildByName(*s);
972 task.SetSourceActor( actor );
976 DALI_SCRIPT_WARNING("Cannot find source actor on stage for render task called '%s'\n", (*s).c_str() );
980 if( OptionalString s = constant.IsString( IsChild(node, "cameraActor") ) )
982 CameraActor actor = CameraActor::DownCast( root.FindChildByName(*s) );
985 task.SetCameraActor( actor );
989 DALI_SCRIPT_WARNING("Cannot find camera actor on stage for render task called '%s'\n", (*s).c_str() );
993 if( OptionalString s = constant.IsString( IsChild(node, "targetFrameBuffer") ) )
995 FrameBufferImage fb = GetFrameBufferImage( *s, constant );
998 task.SetTargetFrameBuffer( fb );
1002 DALI_SCRIPT_WARNING("Cannot find target frame buffer '%s'\n", (*s).c_str() );
1006 if( OptionalString s = constant.IsString( IsChild(node, "screenToFrameBufferFunction") ) )
1008 if("DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION" == *s)
1010 task.SetScreenToFrameBufferFunction( RenderTask::DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION );
1012 else if("FULLSCREEN_FRAMEBUFFER_FUNCTION" == *s)
1014 task.SetScreenToFrameBufferFunction( RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION );
1018 DALI_SCRIPT_WARNING("todo");
1022 // other setup is via the property system
1023 SetProperties( node, task, constant );
1027 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle, const Replacement& replacement )
1029 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1031 OptionalChild styles = IsChild( *mParser.GetRoot(), KEYNAME_STYLES );
1033 std::string styleNameLower(styleName);
1034 OptionalChild style = IsChildIgnoreCase( *styles, styleNameLower );
1036 if( styles && style )
1038 ApplyAllStyleProperties( *mParser.GetRoot(), *style, handle, replacement );
1047 void Builder::ApplyAllStyleProperties( const TreeNode& root, const TreeNode& node,
1048 Dali::Handle& handle, const Replacement& constant )
1050 OptionalChild styles = IsChild(root, KEYNAME_STYLES);
1051 OptionalChild style = IsChild(node, KEYNAME_STYLES);
1053 if( styles && style )
1055 TreeNodeList additionalStyles;
1057 CollectAllStyles( *styles, *style, additionalStyles );
1059 #if defined(DEBUG_ENABLED)
1060 for(TreeNode::ConstIterator iter = (*style).CBegin(); iter != (*style).CEnd(); ++iter)
1062 if( OptionalString styleName = IsString( (*iter).second ) )
1064 DALI_SCRIPT_VERBOSE("Style Applied '%s'\n", (*styleName).c_str());
1069 // a style may have other styles, which has other styles etc so we apply in reverse by convention.
1070 for(TreeNodeList::reverse_iterator iter = additionalStyles.rbegin(); iter != additionalStyles.rend(); ++iter)
1072 ApplyProperties( root, *(*iter), handle, constant );
1073 ApplyStylesByActor( root, *(*iter), handle, constant );
1077 // applying given node last
1078 ApplyProperties( root, node, handle, constant );
1079 ApplyStylesByActor( root, node, handle, constant );
1082 // Set properties from node on handle.
1083 void Builder::ApplyProperties( const TreeNode& root, const TreeNode& node,
1084 Dali::Handle& handle, const Replacement& constant )
1086 if( Actor actor = Actor::DownCast(handle) )
1088 SetProperties( node, actor, constant );
1093 SetupSignalAction( mSlotDelegate.GetConnectionTracker(), root, node, actor, this );
1094 SetupPropertyNotification( mSlotDelegate.GetConnectionTracker(), root, node, actor, this );
1099 SetProperties( node, handle, constant );
1103 // Appling by style helper
1104 // use FindChildByName() to apply properties referenced in KEYNAME_ACTORS in the node
1105 void Builder::ApplyStylesByActor( const TreeNode& root, const TreeNode& node,
1106 Dali::Handle& handle, const Replacement& constant )
1108 if( Dali::Actor actor = Dali::Actor::DownCast( handle ) )
1110 if( const TreeNode* actors = node.GetChild( KEYNAME_ACTORS ) )
1112 // in a style the actor subtree properties referenced by actor name
1113 for( TreeConstIter iter = actors->CBegin(); iter != actors->CEnd(); ++iter )
1115 Dali::Actor foundActor;
1119 foundActor = actor.FindChildByName( (*iter).first );
1124 // debug log cannot find searched for actor
1125 #if defined(DEBUG_ENABLED)
1126 DALI_SCRIPT_VERBOSE("Cannot find actor in style application '%s'\n", (*iter).first);
1131 #if defined(DEBUG_ENABLED)
1132 DALI_SCRIPT_VERBOSE("Styles applied to actor '%s'\n", (*iter).first);
1134 ApplyProperties( root, (*iter).second, foundActor, constant );
1142 * Sets the handle properties found in the tree node
1144 void Builder::SetProperties( const TreeNode& node, Handle& handle, const Replacement& constant )
1148 for( TreeNode::ConstIterator iter = node.CBegin(); iter != node.CEnd(); ++iter )
1150 const TreeNode::KeyNodePair& keyChild = *iter;
1152 std::string key( keyChild.first );
1154 // ignore special fields;
1155 if( key == KEYNAME_TYPE || key == KEYNAME_ACTORS || key == KEYNAME_SIGNALS || key == KEYNAME_STYLES || key == KEYNAME_MAPPINGS )
1160 Handle propertyObject( handle );
1162 Dali::Property::Index index = propertyObject.GetPropertyIndex( key );
1164 if( Property::INVALID_INDEX != index )
1166 Property::Type type = propertyObject.GetPropertyType(index);
1167 Property::Value value;
1168 bool mapped = false;
1170 // if node.value is a mapping, get the property value from the "mappings" table
1171 if( keyChild.second.GetType() == TreeNode::STRING )
1173 std::string mappingKey;
1174 if( GetMappingKey(keyChild.second.GetString(), mappingKey) )
1176 OptionalChild mappingRoot = IsChild( mParser.GetRoot(), KEYNAME_MAPPINGS );
1177 mapped = GetPropertyMap( *mappingRoot, mappingKey.c_str(), type, value );
1182 mapped = DeterminePropertyFromNode( keyChild.second, type, value, constant );
1185 // Just determine the property from the node and if it's valid, let the property object handle it
1186 DeterminePropertyFromNode( keyChild.second, value, constant );
1187 mapped = ( value.GetType() != Property::NONE );
1192 DALI_SCRIPT_VERBOSE("SetProperty '%s' Index=:%d Value Type=%d Value '%s'\n", key.c_str(), index, value.GetType(), PropertyValueToString(value).c_str() );
1194 propertyObject.SetProperty( index, value );
1199 DALI_LOG_ERROR("Key '%s' not found.\n", key.c_str());
1202 // Add custom properties
1203 SetCustomProperties(node, handle, constant, PROPERTIES, Property::READ_WRITE);
1204 SetCustomProperties(node, handle, constant, ANIMATABLE_PROPERTIES, Property::ANIMATABLE);
1206 } // for property nodes
1210 DALI_SCRIPT_WARNING("Style applied to empty handle\n");
1214 bool Builder::GetPropertyMap( const TreeNode& mappingRoot, const char* theKey, Property::Type propertyType, Property::Value& value )
1217 return RecursePropertyMap( mappingRoot, keyStack, theKey, propertyType, value );
1220 bool Builder::RecursePropertyMap( const TreeNode& mappingRoot, KeyStack& keyStack, const char* theKey, Property::Type propertyType, Property::Value& value )
1222 Replacement replacer( mReplacementMap );
1223 bool result = false;
1225 keyStack.push_back( theKey );
1227 for( TreeNode::ConstIterator iter = mappingRoot.CBegin(); iter != mappingRoot.CEnd(); ++iter )
1229 std::string aKey( (*iter).first );
1230 if( aKey.compare( theKey ) == 0 )
1232 if( propertyType == Property::NONE )
1234 DeterminePropertyFromNode( (*iter).second, value, replacer );
1239 result = DeterminePropertyFromNode( (*iter).second, propertyType, value, replacer );
1244 ConvertChildValue(mappingRoot, keyStack, value);
1249 keyStack.pop_back();
1254 bool Builder::ConvertChildValue( const TreeNode& mappingRoot, KeyStack& keyStack, Property::Value& child )
1256 bool result = false;
1258 switch( child.GetType() )
1260 case Property::STRING:
1263 if( child.Get( value ) )
1266 if( GetMappingKey( value, key ) )
1268 // Check key for cycles:
1270 for( KeyStack::iterator iter = keyStack.begin() ; iter != keyStack.end(); ++iter )
1272 if( key.compare(*iter) == 0 )
1274 // key is already in stack; stop.
1275 DALI_LOG_WARNING("Detected cycle in stylesheet mapping table:%s\n", key.c_str());
1276 child = Property::Value("");
1284 // The following call will overwrite the child with the value
1285 // from the mapping.
1286 RecursePropertyMap( mappingRoot, keyStack, key.c_str(), Property::NONE, child );
1296 Property::Map* map = child.GetMap();
1299 for( Property::Map::SizeType i=0; i < map->Count(); ++i )
1301 Property::Value& child = map->GetValue(i);
1302 ConvertChildValue(mappingRoot, keyStack, child);
1308 case Property::ARRAY:
1310 Property::Array* array = child.GetArray();
1313 for( Property::Array::SizeType i=0; i < array->Count(); ++i )
1315 Property::Value& child = array->GetElementAt(i);
1316 ConvertChildValue(mappingRoot, keyStack, child);
1323 // Ignore other types.
1330 void Builder::SetCustomProperties( const TreeNode& node, Handle& handle, const Replacement& constant,
1331 const std::string& childName, Property::AccessMode accessMode )
1333 // Add custom properties
1334 if( OptionalChild customPropertiesChild = IsChild(node, childName) )
1336 const TreeNode& customPropertiesNode = *customPropertiesChild;
1337 const TreeConstIter endIter = customPropertiesNode.CEnd();
1338 for( TreeConstIter iter = customPropertiesNode.CBegin(); endIter != iter; ++iter )
1340 const TreeNode::KeyNodePair& keyChild = *iter;
1341 std::string key( keyChild.first );
1343 Property::Value value;
1344 DeterminePropertyFromNode( keyChild.second, value, constant );
1346 // Register/Set property.
1347 handle.RegisterProperty( key, value, accessMode );
1352 } // namespace Internal
1354 } // namespace Toolkit