2 * Copyright (c) 2014 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>
23 #include <boost/function.hpp>
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/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/replacement.h>
54 extern Animation CreateAnimation(const TreeNode& child, const Replacement& replacements, const Dali::Actor searchRoot, Builder* const builder );
55 extern bool SetPropertyFromNode( const TreeNode& node, Property::Value& value );
56 extern bool SetPropertyFromNode( const TreeNode& node, Property::Value& value, const Replacement& replacements );
57 extern bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value );
58 extern bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value, const Replacement& replacements );
59 extern Actor SetupSignalAction(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, boost::function<void (void)> quitAction, Dali::Toolkit::Internal::Builder* const builder);
60 extern Actor SetupPropertyNotification(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, boost::function<void (void)> quitAction, 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 const std::string KEYNAME_STYLES = "styles";
71 const std::string KEYNAME_TYPE = "type";
72 const std::string KEYNAME_ACTORS = "actors";
73 const std::string KEYNAME_SIGNALS = "signals";
74 const std::string KEYNAME_NAME = "name";
75 const std::string KEYNAME_TEMPLATES = "templates";
76 const std::string KEYNAME_INCLUDES = "includes";
78 typedef std::vector<const TreeNode*> TreeNodeList;
81 std::string ToString(const T& value)
89 std::string ToString(const Rect<int>& value)
92 ss << value.x << "," << value.y << "," << value.width << "," << value.height;
96 #if defined(DEBUG_ENABLED)
98 std::string PropertyValueToString( const Property::Value& value )
102 switch( value.GetType() )
109 case Property::BOOLEAN:
111 ret = value.Get<bool>() ? "True" : "False";
114 case Property::FLOAT:
117 ret = ToString( value.Get<float>() );
120 case Property::INTEGER:
122 ret = ToString( value.Get<int>() );
125 case Property::UNSIGNED_INTEGER:
127 ret = ToString( value.Get<unsigned int>() );
130 case Property::VECTOR2:
132 ret = ToString( value.Get<Vector2>() );
135 case Property::VECTOR3:
137 ret = ToString( value.Get<Vector3>() );
140 case Property::VECTOR4:
142 ret = ToString( value.Get<Vector4>() );
145 case Property::MATRIX3:
147 ret = ToString( value.Get<Matrix3>() );
150 case Property::MATRIX:
152 ret = ToString( value.Get<Matrix>() );
155 case Property::RECTANGLE:
157 ret = ToString( value.Get< Rect<int> >() );
160 case Property::ROTATION:
164 case Property::STRING:
166 ret = value.Get<std::string>();
169 case Property::ARRAY:
171 ret = std::string("Array Size=") + ToString( value.Get<Property::Array>().Size() );
176 ret = std::string("Map Size=") + ToString( value.Get<Property::Map>().Count() );
179 case Property::TYPE_COUNT:
188 #endif // DEBUG_ENABLED
191 * Recursively collects all stylesin a node (An array of style names).
193 * stylesCollection The set of styles from the json file (a json object of named styles)
194 * style The style array to begin the collection from
195 * styleList The style list to add nodes to apply
197 void CollectAllStyles( const TreeNode& stylesCollection, const TreeNode& style, TreeNodeList& styleList )
199 // style is an array of style names
200 if( TreeNode::ARRAY == style.GetType() )
202 for(TreeNode::ConstIterator iter = style.CBegin(); iter != style.CEnd(); ++iter)
204 if( OptionalString styleName = IsString( (*iter).second ) )
206 if( OptionalChild node = IsChild( stylesCollection, *styleName) )
208 styleList.push_back( &(*node) );
210 if( OptionalChild subStyle = IsChild( *node, KEYNAME_STYLES ) )
212 CollectAllStyles( stylesCollection, *subStyle, styleList );
223 QuitAction( Builder& builder )
224 : mBuilder( builder )
228 void operator()(void)
230 mBuilder.EmitQuitSignal();
240 * Sets the handle properties found in the tree node
242 void Builder::SetProperties( const TreeNode& node, Handle& handle, const Replacement& constant )
246 for( TreeNode::ConstIterator iter = node.CBegin(); iter != node.CEnd(); ++iter )
248 const TreeNode::KeyNodePair& keyChild = *iter;
250 std::string key( keyChild.first );
252 // ignore special fields; type,actors,signals,styles
253 if(key == KEYNAME_TYPE || key == KEYNAME_ACTORS || key == KEYNAME_SIGNALS || key == KEYNAME_STYLES)
258 // special field 'image' usually contains an json object description
259 // although sometimes refers to a framebuffer
260 if( 0 == keyChild.second.Size() )
264 ImageActor imageActor = ImageActor::DownCast(handle);
267 if( OptionalString s = constant.IsString( keyChild.second ) )
269 FrameBufferImage fb = GetFrameBufferImage(*s, constant);
272 imageActor.SetImage( fb );
279 // special field 'effect' references the shader effect instances
282 RenderableActor actor = RenderableActor::DownCast(handle);
285 OptionalString str = constant.IsString( keyChild.second );
288 ShaderEffect effect = GetShaderEffect( *str, constant );
289 actor.SetShaderEffect(effect);
294 DALI_SCRIPT_WARNING("Could not find or set shader effect\n");
300 Handle propertyObject( handle );
302 Dali::Property::Index index = propertyObject.GetPropertyIndex( key );
304 if( Property::INVALID_INDEX == index )
306 RenderableActor actor = RenderableActor::DownCast(handle);
309 if( ShaderEffect effect = actor.GetShaderEffect() )
311 index = effect.GetPropertyIndex( key );
312 if(index != Property::INVALID_INDEX)
314 propertyObject = effect;
320 if( Property::INVALID_INDEX != index )
322 Property::Type type = propertyObject.GetPropertyType(index);
324 Property::Value value;
325 if( !SetPropertyFromNode( keyChild.second, type, value, constant ) )
327 // verbose as this might not be a problem
328 // eg parent-origin can be a string which is picked up later
329 DALI_SCRIPT_VERBOSE("Could not convert property:%s\n", key.c_str());
333 DALI_SCRIPT_VERBOSE("SetProperty '%s' Index=:%d Value Type=%d Value '%s'\n", key.c_str(), index, value.GetType(), PropertyValueToString(value).c_str() );
335 propertyObject.SetProperty( index, value );
340 DALI_SCRIPT_VERBOSE("SetProperty INVALID '%s' Index=:%d\n", key.c_str(), index);
343 } // for property nodes
347 DALI_SCRIPT_WARNING("Style applied to empty handle\n");
351 // Set properties from node on handle.
352 void Builder::ApplyProperties( const TreeNode& root, const TreeNode& node,
353 Dali::Handle& handle, const Replacement& constant )
355 if( Actor actor = Actor::DownCast(handle) )
357 SetProperties( node, actor, constant );
361 SetupActor( node, actor, constant );
364 QuitAction quitAction( *this );
365 SetupSignalAction( mSlotDelegate.GetConnectionTracker(), root, node, actor, quitAction, this );
366 SetupPropertyNotification( mSlotDelegate.GetConnectionTracker(), root, node, actor, quitAction, this );
371 SetProperties( node, handle, constant );
375 // Appling by style helper
376 // use FindChildByName() to apply properties referenced in KEYNAME_ACTORS in the node
377 void Builder::ApplyStylesByActor( const TreeNode& root, const TreeNode& node,
378 Dali::Handle& handle, const Replacement& constant )
380 if( Dali::Actor actor = Dali::Actor::DownCast( handle ) )
382 if( const TreeNode* actors = node.GetChild( KEYNAME_ACTORS ) )
384 // in a style the actor subtree properties referenced by actor name
385 for( TreeConstIter iter = actors->CBegin(); iter != actors->CEnd(); ++iter )
387 Dali::Actor foundActor;
391 foundActor = actor.FindChildByName( (*iter).first );
396 // debug log cannot find searched for actor
397 #if defined(DEBUG_ENABLED)
398 DALI_SCRIPT_VERBOSE("Cannot find actor in style application '%s'\n", (*iter).first);
403 #if defined(DEBUG_ENABLED)
404 DALI_SCRIPT_VERBOSE("Styles applied to actor '%s'\n", (*iter).first);
406 ApplyProperties( root, (*iter).second, foundActor, constant );
414 void Builder::ApplyAllStyleProperties( const TreeNode& root, const TreeNode& node,
415 Dali::Handle& handle, const Replacement& constant )
417 OptionalChild styles = IsChild(root, KEYNAME_STYLES);
418 OptionalChild style = IsChild(node, KEYNAME_STYLES);
420 if( styles && style )
422 TreeNodeList additionalStyles;
424 CollectAllStyles( *styles, *style, additionalStyles );
426 #if defined(DEBUG_ENABLED)
427 for(TreeNode::ConstIterator iter = (*style).CBegin(); iter != (*style).CEnd(); ++iter)
429 if( OptionalString styleName = IsString( (*iter).second ) )
431 DALI_SCRIPT_VERBOSE("Style Applied '%s'\n", (*styleName).c_str());
436 // a style may have other styles, which has other styles etc so we apply in reverse by convention.
437 for(TreeNodeList::reverse_iterator iter = additionalStyles.rbegin(); iter != additionalStyles.rend(); ++iter)
439 ApplyProperties( root, *(*iter), handle, constant );
441 ApplyStylesByActor( root, *(*iter), handle, constant );
445 // applying given node last
446 ApplyProperties( root, node, handle, constant );
448 ApplyStylesByActor( root, node, handle, constant );
454 * Create a dali type from a node.
455 * If parent given and an actor type was created then add it to the parent and
456 * recursively add nodes children.
458 BaseHandle Builder::DoCreate( const TreeNode& root, const TreeNode& node,
459 Actor parent, const Replacement& replacements )
461 BaseHandle baseHandle;
463 const TreeNode* templateNode = NULL;
465 if( OptionalString typeName = IsString(node, KEYNAME_TYPE) )
467 typeInfo = TypeRegistry::Get().GetTypeInfo( *typeName );
471 // a template name is also allowed inplace of the type name
472 OptionalChild templates = IsChild( root, KEYNAME_TEMPLATES);
476 if( OptionalChild isTemplate = IsChild( *templates, *typeName ) )
478 templateNode = &(*isTemplate);
480 if( OptionalString templateTypeName = IsString(*templateNode, KEYNAME_TYPE) )
482 typeInfo = TypeRegistry::Get().GetTypeInfo( *templateTypeName );
491 DALI_SCRIPT_WARNING("Cannot create Dali type from node '%s'\n", node.GetName());
495 baseHandle = typeInfo.CreateInstance();
496 Handle handle = Handle::DownCast(baseHandle);
497 Actor actor = Actor::DownCast(handle);
502 DALI_SCRIPT_VERBOSE("Create:%s\n", typeInfo.GetName().c_str());
504 #if defined(DEBUG_ENABLED)
507 DALI_SCRIPT_VERBOSE(" Is Handle Object=%d\n", (long*)handle.GetObjectPtr());
508 DALI_SCRIPT_VERBOSE(" Is Handle Property Count=%d\n", handle.GetPropertyCount());
513 DALI_SCRIPT_VERBOSE(" Is Actor id=%d\n", actor.GetId());
516 Toolkit::Control control = Toolkit::Control::DownCast(handle);
519 DALI_SCRIPT_VERBOSE(" Is Control id=%d\n", actor.GetId());
521 #endif // DEBUG_ENABLED
525 ApplyProperties( root, *templateNode, handle, replacements );
527 if( OptionalChild actors = IsChild( *templateNode, KEYNAME_ACTORS ) )
529 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
531 DoCreate( root, (*iter).second, actor, replacements );
538 // add children of all the styles
539 if( OptionalChild actors = IsChild( node, KEYNAME_ACTORS ) )
541 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
543 DoCreate( root, (*iter).second, actor, replacements );
547 // apply style on top as they need the children to exist
548 ApplyAllStyleProperties( root, node, actor, replacements );
550 // then add to parent
558 ApplyProperties( root, node, handle, replacements );
563 DALI_SCRIPT_WARNING("Cannot create handle from type '%s'\n", typeInfo.GetName().c_str());
570 void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replacement& constant )
572 const Stage& stage = Stage::GetCurrent();
573 Layer root = stage.GetRootLayer();
575 if( OptionalString s = constant.IsString( IsChild(node, "source-actor") ) )
577 Actor actor = root.FindChildByName(*s);
580 task.SetSourceActor( actor );
584 DALI_SCRIPT_WARNING("Cannot find source actor on stage for render task called '%s'\n", (*s).c_str() );
588 if( OptionalString s = constant.IsString( IsChild(node, "camera-actor") ) )
590 CameraActor actor = CameraActor::DownCast( root.FindChildByName(*s) );
593 task.SetCameraActor( actor );
597 DALI_SCRIPT_WARNING("Cannot find camera actor on stage for render task called '%s'\n", (*s).c_str() );
601 if( OptionalString s = constant.IsString( IsChild(node, "target-frame-buffer") ) )
603 FrameBufferImage fb = GetFrameBufferImage( *s, constant );
606 task.SetTargetFrameBuffer( fb );
610 DALI_SCRIPT_WARNING("Cannot find target frame buffer '%s'\n", (*s).c_str() );
614 if( OptionalString s = constant.IsString( IsChild(node, "screen-to-frame-buffer-function") ) )
616 if("DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION" == *s)
618 task.SetScreenToFrameBufferFunction( RenderTask::DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION );
620 else if("FULLSCREEN_FRAMEBUFFER_FUNCTION" == *s)
622 task.SetScreenToFrameBufferFunction( RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION );
626 DALI_SCRIPT_WARNING("todo");
630 // other setup is via the property system
631 SetProperties( node, task, constant ); // @ todo, remove 'source-actor', 'camera-actor'?
635 void Builder::CreateRenderTask( const std::string &name )
637 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
639 Replacement constant(mReplacementMap);
641 const Stage& stage = Stage::GetCurrent();
643 OptionalChild tasks = IsChild(*mParser.GetRoot(), "render-tasks");
648 // Create the tasks from the current task as generally we want
649 // to setup task zero and onwards. Although this does overwrite
650 // the properties of the current task.
652 if( OptionalChild renderTask = IsChild(*tasks, name ) )
654 RenderTaskList list = stage.GetRenderTaskList();
655 unsigned int start = list.GetTaskCount();
660 // zero should have already been created by the stage so really
661 // this case should never happen
662 task = list.CreateTask();
666 TreeNode::ConstIterator iter = (*renderTask).CBegin();
667 task = list.GetTask( start - 1 );
669 SetupTask( task, (*iter).second, constant );
673 for(; iter != (*renderTask).CEnd(); ++iter )
675 task = list.CreateTask();
676 SetupTask( task, (*iter).second, constant );
682 ShaderEffect Builder::GetShaderEffect( const std::string &name)
684 Replacement constant( mReplacementMap );
685 return GetShaderEffect( name, constant );
688 ShaderEffect Builder::GetShaderEffect( const std::string &name, const Replacement& constant )
690 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
694 ShaderEffectLut::const_iterator iter( mShaderEffectLut.find( name ) );
695 if( iter != mShaderEffectLut.end() )
701 if( OptionalChild effects = IsChild( *mParser.GetRoot(), "shader-effects") )
703 if( OptionalChild effect = IsChild( *effects, name ) )
705 Dali::Property::Value propertyMap(Property::MAP);
706 if( SetPropertyFromNode( *effect, Property::MAP, propertyMap, constant ) )
708 ret = Dali::Scripting::NewShaderEffect( propertyMap );
709 mShaderEffectLut[ name ] = ret;
718 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name )
720 Replacement constant( mReplacementMap );
721 return GetFrameBufferImage(name, constant);
724 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name, const Replacement& constant )
726 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
728 FrameBufferImage ret;
730 ImageLut::const_iterator iter( mFrameBufferImageLut.find( name ) );
731 if( iter != mFrameBufferImageLut.end() )
737 if( OptionalChild images = IsChild( *mParser.GetRoot(), "frame-buffer-images") )
739 if( OptionalChild image = IsChild( *images, name ) )
741 Dali::Property::Value propertyMap(Property::MAP);
742 if( SetPropertyFromNode( *image, Property::MAP, propertyMap, constant ) )
744 propertyMap.SetValue(KEYNAME_TYPE, Property::Value(std::string("FrameBufferImage")));
745 ret = FrameBufferImage::DownCast( Dali::Scripting::NewImage( propertyMap ) );
746 mFrameBufferImageLut[ name ] = ret;
755 Path Builder::GetPath( const std::string& name )
757 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
761 PathLut::const_iterator iter( mPathLut.find( name ) );
762 if( iter != mPathLut.end() )
768 if( OptionalChild paths = IsChild( *mParser.GetRoot(), "paths") )
770 if( OptionalChild path = IsChild( *paths, name ) )
773 if( OptionalChild pointsProperty = IsChild( *path, "points") )
775 Dali::Property::Value points(Property::ARRAY);
776 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
779 ret.SetProperty( Path::Property::POINTS, points);
781 //control-points property
782 if( OptionalChild pointsProperty = IsChild( *path, "control-points") )
784 Dali::Property::Value points(Property::ARRAY);
785 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
787 ret.SetProperty( Path::Property::CONTROL_POINTS, points);
793 float curvature(0.25f);
794 if( OptionalFloat pointsProperty = IsFloat( *path, "curvature") )
796 curvature = *pointsProperty;
798 ret.GenerateControlPoints(curvature);
801 //Add the new path to the hash table for paths
802 mPathLut[ name ] = ret;
807 //Interpolation points not specified
808 DALI_SCRIPT_WARNING("Interpolation points not specified for path '%s'\n", name.c_str() );
818 PathConstrainer Builder::GetPathConstrainer( const std::string& name )
820 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
822 //Search the pathConstrainer in the LUT
823 size_t count( mPathConstrainerLut.size() );
824 for( size_t i(0); i!=count; ++i )
826 if( mPathConstrainerLut[i].name == name )
828 //PathConstrainer has already been created
829 return mPathConstrainerLut[i].pathConstrainer;
833 //Create a new PathConstrainer
835 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
837 if( OptionalChild pathConstrainer = IsChild( *constrainers, name ) )
839 OptionalString constrainerType(IsString(IsChild(*pathConstrainer, "type")));
842 DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str() );
844 else if( *constrainerType == "PathConstrainer")
847 if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "points") )
849 Dali::Property::Value points(Property::ARRAY);
850 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
852 ret = PathConstrainer::New();
853 ret.SetProperty( PathConstrainer::Property::POINTS, points);
855 //control-points property
856 if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "control-points") )
858 Dali::Property::Value points(Property::ARRAY);
859 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
861 ret.SetProperty( PathConstrainer::Property::CONTROL_POINTS, points);
865 OptionalVector3 forward( IsVector3( IsChild(*pathConstrainer, "forward" ) ) );
868 ret.SetProperty( PathConstrainer::Property::FORWARD, *forward);
871 //Add the new constrainer to the vector of PathConstrainer
872 PathConstrainerEntry entry = {name,ret};
873 mPathConstrainerLut.push_back( entry );
877 //Control points not specified
878 DALI_SCRIPT_WARNING("Control points not specified for pathConstrainer '%s'\n", name.c_str() );
884 //Interpolation points not specified
885 DALI_SCRIPT_WARNING("Interpolation points not specified for pathConstrainer '%s'\n", name.c_str() );
890 DALI_SCRIPT_WARNING("Constrainer '%s' is not a PathConstrainer\n", name.c_str() );
898 bool Builder::IsPathConstrainer( const std::string& name )
900 size_t count( mPathConstrainerLut.size() );
901 for( size_t i(0); i!=count; ++i )
903 if( mPathConstrainerLut[i].name == name )
909 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
911 if( OptionalChild constrainer = IsChild( *constrainers, name ) )
913 OptionalString constrainerType(IsString(IsChild(*constrainer, "type")));
920 return *constrainerType == "PathConstrainer";
927 Dali::LinearConstrainer Builder::GetLinearConstrainer( const std::string& name )
929 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
931 //Search the LinearConstrainer in the LUT
932 size_t count( mLinearConstrainerLut.size() );
933 for( size_t i(0); i!=count; ++i )
935 if( mLinearConstrainerLut[i].name == name )
937 //LinearConstrainer has already been created
938 return mLinearConstrainerLut[i].linearConstrainer;
942 //Create a new LinearConstrainer
943 LinearConstrainer ret;
944 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
946 if( OptionalChild linearConstrainer = IsChild( *constrainers, name ) )
948 OptionalString constrainerType(IsString(IsChild(*linearConstrainer, "type")));
951 DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str() );
953 else if( *constrainerType == "LinearConstrainer")
956 if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "value") )
958 Dali::Property::Value points(Property::ARRAY);
959 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
961 ret = Dali::LinearConstrainer::New();
962 ret.SetProperty( LinearConstrainer::Property::VALUE, points);
964 //control-points property
965 if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "progress") )
967 Dali::Property::Value points(Property::ARRAY);
968 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
970 ret.SetProperty( LinearConstrainer::Property::PROGRESS, points);
973 //Add the new constrainer to vector of LinearConstrainer
974 LinearConstrainerEntry entry = {name,ret};
975 mLinearConstrainerLut.push_back( entry );
980 //Interpolation points not specified
981 DALI_SCRIPT_WARNING("Values not specified for LinearConstrainer '%s'\n", name.c_str() );
986 DALI_SCRIPT_WARNING("Constrainer '%s' is not a LinearConstrainer\n", name.c_str() );
994 bool Builder::IsLinearConstrainer( const std::string& name )
996 //Search the LinearConstrainer in the LUT
997 size_t count( mLinearConstrainerLut.size() );
998 for( size_t i(0); i!=count; ++i )
1000 if( mLinearConstrainerLut[i].name == name )
1006 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
1008 if( OptionalChild constrainer = IsChild( *constrainers, name ) )
1010 OptionalString constrainerType(IsString(IsChild(*constrainer, "type")));
1011 if(!constrainerType)
1017 return *constrainerType == "LinearConstrainer";
1024 Toolkit::Builder::BuilderSignalType& Builder::QuitSignal()
1029 void Builder::EmitQuitSignal()
1034 void Builder::AddActors( Actor toActor )
1036 // 'stage' is the default/by convention section to add from
1037 AddActors( "stage", toActor );
1040 void Builder::AddActors( const std::string §ionName, Actor toActor )
1042 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1044 Property::Map overrideMap;
1045 Replacement replacements(overrideMap, mReplacementMap);
1047 OptionalChild add = IsChild(*mParser.GetRoot(), sectionName);
1051 for( TreeNode::ConstIterator iter = (*add).CBegin(); iter != (*add).CEnd(); ++iter )
1053 // empty actor adds directly to the stage
1054 BaseHandle baseHandle = DoCreate( *mParser.GetRoot(), (*iter).second, Actor(), replacements );
1055 Actor actor = Actor::DownCast(baseHandle);
1058 toActor.Add( actor );
1062 // if were adding the 'stage' section then also check for a render task called stage
1063 // to add automatically
1064 if( "stage" == sectionName )
1066 if( OptionalChild renderTasks = IsChild(*mParser.GetRoot(), "render-tasks") )
1068 if( OptionalChild tasks = IsChild(*renderTasks, "stage") )
1070 CreateRenderTask( "stage" );
1077 Animation Builder::CreateAnimation( const std::string& animationName, const Replacement& replacement, Dali::Actor sourceActor )
1079 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1083 if( OptionalChild animations = IsChild(*mParser.GetRoot(), "animations") )
1085 if( OptionalChild animation = IsChild(*animations, animationName) )
1087 anim = Dali::Toolkit::Internal::CreateAnimation( *animation, replacement, sourceActor, this );
1091 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed\n", animationName.c_str() );
1096 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed (no animation section)\n", animationName.c_str() );
1102 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor )
1104 Replacement replacement(map, mReplacementMap);
1105 return CreateAnimation( animationName, replacement, sourceActor);
1108 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map )
1110 Replacement replacement(map, mReplacementMap);
1111 return CreateAnimation( animationName, replacement, Stage::GetCurrent().GetRootLayer() );
1114 Animation Builder::CreateAnimation( const std::string& animationName, Dali::Actor sourceActor )
1116 Replacement replacement( mReplacementMap );
1118 return CreateAnimation( animationName, replacement, sourceActor );
1121 Animation Builder::CreateAnimation( const std::string& animationName )
1123 Replacement replacement( mReplacementMap );
1125 return CreateAnimation( animationName, replacement, Dali::Stage::GetCurrent().GetRootLayer() );
1128 void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::UIFormat format )
1130 // parser to get constants and includes only
1131 Dali::Toolkit::JsonParser parser = Dali::Toolkit::JsonParser::New();
1133 if( !parser.Parse( data ) )
1135 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
1136 parser.GetErrorLineNumber(),
1137 parser.GetErrorColumn(),
1138 parser.GetErrorDescription().c_str() );
1140 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
1145 // load constant map (allows the user to override the constants in the json after loading)
1146 LoadConstants( *parser.GetRoot(), mReplacementMap );
1149 if( OptionalChild includes = IsChild(*parser.GetRoot(), KEYNAME_INCLUDES) )
1151 Replacement replacer( mReplacementMap );
1153 for(TreeNode::ConstIterator iter = (*includes).CBegin(); iter != (*includes).CEnd(); ++iter)
1155 OptionalString filename = replacer.IsString( (*iter).second );
1159 #if defined(DEBUG_ENABLED)
1160 DALI_SCRIPT_VERBOSE("Loading Include '%s'\n", (*filename).c_str());
1162 LoadFromString( GetFileContents(*filename) );
1167 if( !mParser.Parse( data ) )
1169 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
1170 mParser.GetErrorLineNumber(),
1171 mParser.GetErrorColumn(),
1172 mParser.GetErrorDescription().c_str() );
1174 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
1178 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Cannot parse JSON");
1182 void Builder::AddConstants( const Property::Map& map )
1184 mReplacementMap.Merge( map );
1187 void Builder::AddConstant( const std::string& key, const Property::Value& value )
1189 mReplacementMap[key] = value;
1192 const Property::Map& Builder::GetConstants() const
1194 return mReplacementMap;
1197 const Property::Value& Builder::GetConstant( const std::string& key ) const
1199 Property::Value* match = mReplacementMap.Find( key );
1206 static Property::Value invalid;
1211 void Builder::LoadConstants( const TreeNode& root, Property::Map& intoMap )
1213 Replacement replacer(intoMap);
1215 if( OptionalChild constants = IsChild(root, "constants") )
1217 for(TreeNode::ConstIterator iter = (*constants).CBegin();
1218 iter != (*constants).CEnd(); ++iter)
1220 Dali::Property::Value property;
1221 if( (*iter).second.GetName() )
1223 #if defined(DEBUG_ENABLED)
1224 DALI_SCRIPT_VERBOSE("Constant set from json '%s'\n", (*iter).second.GetName());
1226 if( SetPropertyFromNode( (*iter).second, property, replacer ) )
1228 intoMap[ (*iter).second.GetName() ] = property;
1232 DALI_SCRIPT_WARNING("Cannot convert property for constant %s\n",
1233 (*iter).second.GetName() == NULL ? "no name?" : (*iter).second.GetName());
1239 #if defined(DEBUG_ENABLED)
1240 Property::Value* iter = intoMap.Find( "CONFIG_SCRIPT_LOG_LEVEL" );
1241 if( iter && iter->GetType() == Property::STRING )
1243 std::string logLevel( iter->Get< std::string >() );
1244 if( logLevel == "NoLogging" )
1246 gFilterScript->SetLogLevel( Integration::Log::NoLogging );
1248 else if( logLevel == "Concise" )
1250 gFilterScript->SetLogLevel( Integration::Log::Concise );
1252 else if( logLevel == "General" )
1254 gFilterScript->SetLogLevel( Integration::Log::General );
1256 else if( logLevel == "Verbose" )
1258 gFilterScript->SetLogLevel( Integration::Log::Verbose );
1265 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle )
1267 Replacement replacer( mReplacementMap );
1268 return ApplyStyle( styleName, handle, replacer );
1271 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle, const Replacement& replacement )
1273 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1275 OptionalChild styles = IsChild( *mParser.GetRoot(), KEYNAME_STYLES );
1276 OptionalChild style = IsChild( *styles, styleName );
1278 if( styles && style )
1280 ApplyAllStyleProperties( *mParser.GetRoot(), *style, handle, replacement );
1289 BaseHandle Builder::Create( const std::string& templateName, const Property::Map& map )
1291 Replacement replacement( map, mReplacementMap );
1292 return Create( templateName, replacement );
1295 BaseHandle Builder::Create( const std::string& templateName, const Replacement& constant )
1297 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1299 BaseHandle baseHandle;
1301 OptionalChild templates = IsChild(*mParser.GetRoot(), KEYNAME_TEMPLATES);
1305 DALI_SCRIPT_WARNING("No template section found to CreateFromTemplate\n");
1309 OptionalChild childTemplate = IsChild(*templates, templateName);
1312 DALI_SCRIPT_WARNING("Template '%s' does not exist in template section\n", templateName.c_str());
1316 OptionalString type = constant.IsString( IsChild(*childTemplate, KEYNAME_TYPE) );
1320 DALI_SCRIPT_WARNING("Cannot create template '%s' as template section is missing 'type'\n", templateName.c_str());
1324 baseHandle = DoCreate( *mParser.GetRoot(), *childTemplate, Actor(), constant );
1332 BaseHandle Builder::CreateFromJson( const std::string& json )
1336 // merge in new template, hoping no one else has one named '@temp@'
1337 std::string newTemplate =
1338 std::string("{\"templates\":{\"@temp@\":") + \
1342 if( mParser.Parse(newTemplate) )
1344 Replacement replacement( mReplacementMap );
1345 ret = Create( "@temp@", replacement );
1351 bool Builder::ApplyFromJson( Handle& handle, const std::string& json )
1355 // merge new style, hoping no one else has one named '@temp@'
1356 std::string newStyle =
1357 std::string("{\"styles\":{\"@temp@\":") + \
1361 if( mParser.Parse(newStyle) )
1363 Replacement replacement( mReplacementMap );
1364 ret = ApplyStyle( "@temp@", handle, replacement );
1371 BaseHandle Builder::Create( const std::string& templateName )
1373 Replacement replacement( mReplacementMap );
1374 return Create( templateName, replacement );
1378 : mSlotDelegate( this )
1380 mParser = Dali::Toolkit::JsonParser::New();
1387 } // namespace Internal
1389 } // namespace Toolkit