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>
24 #include <dali/public-api/render-tasks/render-task-list.h>
25 #include <dali/public-api/object/type-info.h>
26 #include <dali/public-api/object/type-registry.h>
27 #include <dali/public-api/object/property-array.h>
28 #include <dali/public-api/actors/layer.h>
29 #include <dali/public-api/actors/image-actor.h>
30 #include <dali/public-api/actors/camera-actor.h>
31 #include <dali/devel-api/scripting/scripting.h>
32 #include <dali/public-api/signals/functor-delegate.h>
33 #include <dali/integration-api/debug.h>
36 #include <dali-toolkit/public-api/controls/control.h>
37 #include <dali-toolkit/devel-api/builder/json-parser.h>
39 #include <dali-toolkit/internal/builder/builder-get-is.inl.h>
40 #include <dali-toolkit/internal/builder/builder-filesystem.h>
41 #include <dali-toolkit/internal/builder/builder-declarations.h>
42 #include <dali-toolkit/internal/builder/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, Dali::Toolkit::Internal::Builder* const builder);
60 extern Actor SetupPropertyNotification(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, Dali::Toolkit::Internal::Builder* const builder);
61 extern Actor SetupActor( const TreeNode& node, Actor& actor, const Replacement& constant );
63 #if defined(DEBUG_ENABLED)
64 Integration::Log::Filter* gFilterScript = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_SCRIPT");
70 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 );
224 * Sets the handle properties found in the tree node
226 void Builder::SetProperties( const TreeNode& node, Handle& handle, const Replacement& constant )
230 for( TreeNode::ConstIterator iter = node.CBegin(); iter != node.CEnd(); ++iter )
232 const TreeNode::KeyNodePair& keyChild = *iter;
234 std::string key( keyChild.first );
236 // ignore special fields; type,actors,signals,styles
237 if(key == KEYNAME_TYPE || key == KEYNAME_ACTORS || key == KEYNAME_SIGNALS || key == KEYNAME_STYLES)
242 // special field 'image' usually contains an json object description
243 // although sometimes refers to a framebuffer
244 if( 0 == keyChild.second.Size() )
248 ImageActor imageActor = ImageActor::DownCast(handle);
251 if( OptionalString s = constant.IsString( keyChild.second ) )
253 FrameBufferImage fb = GetFrameBufferImage(*s, constant);
256 imageActor.SetImage( fb );
263 // special field 'effect' references the shader effect instances
266 ImageActor actor = ImageActor::DownCast(handle);
269 OptionalString str = constant.IsString( keyChild.second );
272 ShaderEffect effect = GetShaderEffect( *str, constant );
273 actor.SetShaderEffect(effect);
278 DALI_SCRIPT_WARNING("Could not find or set shader effect\n");
284 Handle propertyObject( handle );
286 Dali::Property::Index index = propertyObject.GetPropertyIndex( key );
288 if( Property::INVALID_INDEX == index )
290 ImageActor actor = ImageActor::DownCast(handle);
293 if( ShaderEffect effect = actor.GetShaderEffect() )
295 index = effect.GetPropertyIndex( key );
296 if(index != Property::INVALID_INDEX)
298 propertyObject = effect;
304 if( Property::INVALID_INDEX != index )
306 Property::Type type = propertyObject.GetPropertyType(index);
308 Property::Value value;
309 if( !SetPropertyFromNode( keyChild.second, type, value, constant ) )
311 // verbose as this might not be a problem
312 // eg parent-origin can be a string which is picked up later
313 DALI_SCRIPT_VERBOSE("Could not convert property:%s\n", key.c_str());
317 DALI_SCRIPT_VERBOSE("SetProperty '%s' Index=:%d Value Type=%d Value '%s'\n", key.c_str(), index, value.GetType(), PropertyValueToString(value).c_str() );
319 propertyObject.SetProperty( index, value );
324 DALI_SCRIPT_VERBOSE("SetProperty INVALID '%s' Index=:%d\n", key.c_str(), index);
327 } // for property nodes
331 DALI_SCRIPT_WARNING("Style applied to empty handle\n");
335 // Set properties from node on handle.
336 void Builder::ApplyProperties( const TreeNode& root, const TreeNode& node,
337 Dali::Handle& handle, const Replacement& constant )
339 if( Actor actor = Actor::DownCast(handle) )
341 SetProperties( node, actor, constant );
345 SetupActor( node, actor, constant );
348 SetupSignalAction( mSlotDelegate.GetConnectionTracker(), root, node, actor, this );
349 SetupPropertyNotification( mSlotDelegate.GetConnectionTracker(), root, node, actor, this );
354 SetProperties( node, handle, constant );
358 // Appling by style helper
359 // use FindChildByName() to apply properties referenced in KEYNAME_ACTORS in the node
360 void Builder::ApplyStylesByActor( const TreeNode& root, const TreeNode& node,
361 Dali::Handle& handle, const Replacement& constant )
363 if( Dali::Actor actor = Dali::Actor::DownCast( handle ) )
365 if( const TreeNode* actors = node.GetChild( KEYNAME_ACTORS ) )
367 // in a style the actor subtree properties referenced by actor name
368 for( TreeConstIter iter = actors->CBegin(); iter != actors->CEnd(); ++iter )
370 Dali::Actor foundActor;
374 foundActor = actor.FindChildByName( (*iter).first );
379 // debug log cannot find searched for actor
380 #if defined(DEBUG_ENABLED)
381 DALI_SCRIPT_VERBOSE("Cannot find actor in style application '%s'\n", (*iter).first);
386 #if defined(DEBUG_ENABLED)
387 DALI_SCRIPT_VERBOSE("Styles applied to actor '%s'\n", (*iter).first);
389 ApplyProperties( root, (*iter).second, foundActor, constant );
397 void Builder::ApplyAllStyleProperties( const TreeNode& root, const TreeNode& node,
398 Dali::Handle& handle, const Replacement& constant )
400 OptionalChild styles = IsChild(root, KEYNAME_STYLES);
401 OptionalChild style = IsChild(node, KEYNAME_STYLES);
403 if( styles && style )
405 TreeNodeList additionalStyles;
407 CollectAllStyles( *styles, *style, additionalStyles );
409 #if defined(DEBUG_ENABLED)
410 for(TreeNode::ConstIterator iter = (*style).CBegin(); iter != (*style).CEnd(); ++iter)
412 if( OptionalString styleName = IsString( (*iter).second ) )
414 DALI_SCRIPT_VERBOSE("Style Applied '%s'\n", (*styleName).c_str());
419 // a style may have other styles, which has other styles etc so we apply in reverse by convention.
420 for(TreeNodeList::reverse_iterator iter = additionalStyles.rbegin(); iter != additionalStyles.rend(); ++iter)
422 ApplyProperties( root, *(*iter), handle, constant );
424 ApplyStylesByActor( root, *(*iter), handle, constant );
428 // applying given node last
429 ApplyProperties( root, node, handle, constant );
431 ApplyStylesByActor( root, node, handle, constant );
437 * Create a dali type from a node.
438 * If parent given and an actor type was created then add it to the parent and
439 * recursively add nodes children.
441 BaseHandle Builder::DoCreate( const TreeNode& root, const TreeNode& node,
442 Actor parent, const Replacement& replacements )
444 BaseHandle baseHandle;
446 const TreeNode* templateNode = NULL;
448 if( OptionalString typeName = IsString(node, KEYNAME_TYPE) )
450 typeInfo = TypeRegistry::Get().GetTypeInfo( *typeName );
454 // a template name is also allowed inplace of the type name
455 OptionalChild templates = IsChild( root, KEYNAME_TEMPLATES);
459 if( OptionalChild isTemplate = IsChild( *templates, *typeName ) )
461 templateNode = &(*isTemplate);
463 if( OptionalString templateTypeName = IsString(*templateNode, KEYNAME_TYPE) )
465 typeInfo = TypeRegistry::Get().GetTypeInfo( *templateTypeName );
474 DALI_SCRIPT_WARNING("Cannot create Dali type from node '%s'\n", node.GetName());
478 baseHandle = typeInfo.CreateInstance();
479 Handle handle = Handle::DownCast(baseHandle);
480 Actor actor = Actor::DownCast(handle);
485 DALI_SCRIPT_VERBOSE("Create:%s\n", typeInfo.GetName().c_str());
487 #if defined(DEBUG_ENABLED)
490 DALI_SCRIPT_VERBOSE(" Is Handle Object=%d\n", (long*)handle.GetObjectPtr());
491 DALI_SCRIPT_VERBOSE(" Is Handle Property Count=%d\n", handle.GetPropertyCount());
496 DALI_SCRIPT_VERBOSE(" Is Actor id=%d\n", actor.GetId());
499 Toolkit::Control control = Toolkit::Control::DownCast(handle);
502 DALI_SCRIPT_VERBOSE(" Is Control id=%d\n", actor.GetId());
504 #endif // DEBUG_ENABLED
508 ApplyProperties( root, *templateNode, handle, replacements );
510 if( OptionalChild actors = IsChild( *templateNode, KEYNAME_ACTORS ) )
512 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
514 DoCreate( root, (*iter).second, actor, replacements );
521 // add children of all the styles
522 if( OptionalChild actors = IsChild( node, KEYNAME_ACTORS ) )
524 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
526 DoCreate( root, (*iter).second, actor, replacements );
530 // apply style on top as they need the children to exist
531 ApplyAllStyleProperties( root, node, actor, replacements );
533 // then add to parent
541 ApplyProperties( root, node, handle, replacements );
546 DALI_SCRIPT_WARNING("Cannot create handle from type '%s'\n", typeInfo.GetName().c_str());
553 void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replacement& constant )
555 const Stage& stage = Stage::GetCurrent();
556 Layer root = stage.GetRootLayer();
558 if( OptionalString s = constant.IsString( IsChild(node, "source-actor") ) )
560 Actor actor = root.FindChildByName(*s);
563 task.SetSourceActor( actor );
567 DALI_SCRIPT_WARNING("Cannot find source actor on stage for render task called '%s'\n", (*s).c_str() );
571 if( OptionalString s = constant.IsString( IsChild(node, "camera-actor") ) )
573 CameraActor actor = CameraActor::DownCast( root.FindChildByName(*s) );
576 task.SetCameraActor( actor );
580 DALI_SCRIPT_WARNING("Cannot find camera actor on stage for render task called '%s'\n", (*s).c_str() );
584 if( OptionalString s = constant.IsString( IsChild(node, "target-frame-buffer") ) )
586 FrameBufferImage fb = GetFrameBufferImage( *s, constant );
589 task.SetTargetFrameBuffer( fb );
593 DALI_SCRIPT_WARNING("Cannot find target frame buffer '%s'\n", (*s).c_str() );
597 if( OptionalString s = constant.IsString( IsChild(node, "screen-to-frame-buffer-function") ) )
599 if("DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION" == *s)
601 task.SetScreenToFrameBufferFunction( RenderTask::DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION );
603 else if("FULLSCREEN_FRAMEBUFFER_FUNCTION" == *s)
605 task.SetScreenToFrameBufferFunction( RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION );
609 DALI_SCRIPT_WARNING("todo");
613 // other setup is via the property system
614 SetProperties( node, task, constant ); // @ todo, remove 'source-actor', 'camera-actor'?
618 void Builder::CreateRenderTask( const std::string &name )
620 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
622 Replacement constant(mReplacementMap);
624 const Stage& stage = Stage::GetCurrent();
626 OptionalChild tasks = IsChild(*mParser.GetRoot(), "render-tasks");
631 // Create the tasks from the current task as generally we want
632 // to setup task zero and onwards. Although this does overwrite
633 // the properties of the current task.
635 if( OptionalChild renderTask = IsChild(*tasks, name ) )
637 RenderTaskList list = stage.GetRenderTaskList();
638 unsigned int start = list.GetTaskCount();
643 // zero should have already been created by the stage so really
644 // this case should never happen
645 task = list.CreateTask();
649 TreeNode::ConstIterator iter = (*renderTask).CBegin();
650 task = list.GetTask( start - 1 );
652 SetupTask( task, (*iter).second, constant );
656 for(; iter != (*renderTask).CEnd(); ++iter )
658 task = list.CreateTask();
659 SetupTask( task, (*iter).second, constant );
665 ShaderEffect Builder::GetShaderEffect( const std::string &name)
667 Replacement constant( mReplacementMap );
668 return GetShaderEffect( name, constant );
671 ShaderEffect Builder::GetShaderEffect( const std::string &name, const Replacement& constant )
673 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
677 ShaderEffectLut::const_iterator iter( mShaderEffectLut.find( name ) );
678 if( iter != mShaderEffectLut.end() )
684 if( OptionalChild effects = IsChild( *mParser.GetRoot(), "shader-effects") )
686 if( OptionalChild effect = IsChild( *effects, name ) )
688 Dali::Property::Value propertyMap(Property::MAP);
689 if( SetPropertyFromNode( *effect, Property::MAP, propertyMap, constant ) )
691 ret = Dali::Scripting::NewShaderEffect( propertyMap );
692 mShaderEffectLut[ name ] = ret;
701 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name )
703 Replacement constant( mReplacementMap );
704 return GetFrameBufferImage(name, constant);
707 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name, const Replacement& constant )
709 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
711 FrameBufferImage ret;
713 ImageLut::const_iterator iter( mFrameBufferImageLut.find( name ) );
714 if( iter != mFrameBufferImageLut.end() )
720 if( OptionalChild images = IsChild( *mParser.GetRoot(), "frame-buffer-images") )
722 if( OptionalChild image = IsChild( *images, name ) )
724 Dali::Property::Value propertyMap(Property::MAP);
725 if( SetPropertyFromNode( *image, Property::MAP, propertyMap, constant ) )
727 propertyMap.SetValue(KEYNAME_TYPE, Property::Value(std::string("FrameBufferImage")));
728 ret = FrameBufferImage::DownCast( Dali::Scripting::NewImage( propertyMap ) );
729 mFrameBufferImageLut[ name ] = ret;
738 Path Builder::GetPath( const std::string& name )
740 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
744 PathLut::const_iterator iter( mPathLut.find( name ) );
745 if( iter != mPathLut.end() )
751 if( OptionalChild paths = IsChild( *mParser.GetRoot(), "paths") )
753 if( OptionalChild path = IsChild( *paths, name ) )
756 if( OptionalChild pointsProperty = IsChild( *path, "points") )
758 Dali::Property::Value points(Property::ARRAY);
759 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
762 ret.SetProperty( Path::Property::POINTS, points);
764 //control-points property
765 if( OptionalChild pointsProperty = IsChild( *path, "control-points") )
767 Dali::Property::Value points(Property::ARRAY);
768 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
770 ret.SetProperty( Path::Property::CONTROL_POINTS, points);
776 float curvature(0.25f);
777 if( OptionalFloat pointsProperty = IsFloat( *path, "curvature") )
779 curvature = *pointsProperty;
781 ret.GenerateControlPoints(curvature);
784 //Add the new path to the hash table for paths
785 mPathLut[ name ] = ret;
790 //Interpolation points not specified
791 DALI_SCRIPT_WARNING("Interpolation points not specified for path '%s'\n", name.c_str() );
801 PathConstrainer Builder::GetPathConstrainer( const std::string& name )
803 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
805 //Search the pathConstrainer in the LUT
806 size_t count( mPathConstrainerLut.size() );
807 for( size_t i(0); i!=count; ++i )
809 if( mPathConstrainerLut[i].name == name )
811 //PathConstrainer has already been created
812 return mPathConstrainerLut[i].pathConstrainer;
816 //Create a new PathConstrainer
818 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
820 if( OptionalChild pathConstrainer = IsChild( *constrainers, name ) )
822 OptionalString constrainerType(IsString(IsChild(*pathConstrainer, "type")));
825 DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str() );
827 else if( *constrainerType == "PathConstrainer")
830 if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "points") )
832 Dali::Property::Value points(Property::ARRAY);
833 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
835 ret = PathConstrainer::New();
836 ret.SetProperty( PathConstrainer::Property::POINTS, points);
838 //control-points property
839 if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "control-points") )
841 Dali::Property::Value points(Property::ARRAY);
842 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
844 ret.SetProperty( PathConstrainer::Property::CONTROL_POINTS, points);
848 OptionalVector3 forward( IsVector3( IsChild(*pathConstrainer, "forward" ) ) );
851 ret.SetProperty( PathConstrainer::Property::FORWARD, *forward);
854 //Add the new constrainer to the vector of PathConstrainer
855 PathConstrainerEntry entry = {name,ret};
856 mPathConstrainerLut.push_back( entry );
860 //Control points not specified
861 DALI_SCRIPT_WARNING("Control points not specified for pathConstrainer '%s'\n", name.c_str() );
867 //Interpolation points not specified
868 DALI_SCRIPT_WARNING("Interpolation points not specified for pathConstrainer '%s'\n", name.c_str() );
873 DALI_SCRIPT_WARNING("Constrainer '%s' is not a PathConstrainer\n", name.c_str() );
881 bool Builder::IsPathConstrainer( const std::string& name )
883 size_t count( mPathConstrainerLut.size() );
884 for( size_t i(0); i!=count; ++i )
886 if( mPathConstrainerLut[i].name == name )
892 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
894 if( OptionalChild constrainer = IsChild( *constrainers, name ) )
896 OptionalString constrainerType(IsString(IsChild(*constrainer, "type")));
903 return *constrainerType == "PathConstrainer";
910 Dali::LinearConstrainer Builder::GetLinearConstrainer( const std::string& name )
912 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
914 //Search the LinearConstrainer in the LUT
915 size_t count( mLinearConstrainerLut.size() );
916 for( size_t i(0); i!=count; ++i )
918 if( mLinearConstrainerLut[i].name == name )
920 //LinearConstrainer has already been created
921 return mLinearConstrainerLut[i].linearConstrainer;
925 //Create a new LinearConstrainer
926 LinearConstrainer ret;
927 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
929 if( OptionalChild linearConstrainer = IsChild( *constrainers, name ) )
931 OptionalString constrainerType(IsString(IsChild(*linearConstrainer, "type")));
934 DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str() );
936 else if( *constrainerType == "LinearConstrainer")
939 if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "value") )
941 Dali::Property::Value points(Property::ARRAY);
942 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
944 ret = Dali::LinearConstrainer::New();
945 ret.SetProperty( LinearConstrainer::Property::VALUE, points);
947 //control-points property
948 if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "progress") )
950 Dali::Property::Value points(Property::ARRAY);
951 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
953 ret.SetProperty( LinearConstrainer::Property::PROGRESS, points);
956 //Add the new constrainer to vector of LinearConstrainer
957 LinearConstrainerEntry entry = {name,ret};
958 mLinearConstrainerLut.push_back( entry );
963 //Interpolation points not specified
964 DALI_SCRIPT_WARNING("Values not specified for LinearConstrainer '%s'\n", name.c_str() );
969 DALI_SCRIPT_WARNING("Constrainer '%s' is not a LinearConstrainer\n", name.c_str() );
977 bool Builder::IsLinearConstrainer( const std::string& name )
979 //Search the LinearConstrainer in the LUT
980 size_t count( mLinearConstrainerLut.size() );
981 for( size_t i(0); i!=count; ++i )
983 if( mLinearConstrainerLut[i].name == name )
989 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
991 if( OptionalChild constrainer = IsChild( *constrainers, name ) )
993 OptionalString constrainerType(IsString(IsChild(*constrainer, "type")));
1000 return *constrainerType == "LinearConstrainer";
1007 Toolkit::Builder::BuilderSignalType& Builder::QuitSignal()
1012 void Builder::EmitQuitSignal()
1017 void Builder::AddActors( Actor toActor )
1019 // 'stage' is the default/by convention section to add from
1020 AddActors( "stage", toActor );
1023 void Builder::AddActors( const std::string §ionName, Actor toActor )
1025 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1027 Property::Map overrideMap;
1028 Replacement replacements(overrideMap, mReplacementMap);
1030 OptionalChild add = IsChild(*mParser.GetRoot(), sectionName);
1034 for( TreeNode::ConstIterator iter = (*add).CBegin(); iter != (*add).CEnd(); ++iter )
1036 // empty actor adds directly to the stage
1037 BaseHandle baseHandle = DoCreate( *mParser.GetRoot(), (*iter).second, Actor(), replacements );
1038 Actor actor = Actor::DownCast(baseHandle);
1041 toActor.Add( actor );
1045 // if were adding the 'stage' section then also check for a render task called stage
1046 // to add automatically
1047 if( "stage" == sectionName )
1049 if( OptionalChild renderTasks = IsChild(*mParser.GetRoot(), "render-tasks") )
1051 if( OptionalChild tasks = IsChild(*renderTasks, "stage") )
1053 CreateRenderTask( "stage" );
1060 Animation Builder::CreateAnimation( const std::string& animationName, const Replacement& replacement, Dali::Actor sourceActor )
1062 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1066 if( OptionalChild animations = IsChild(*mParser.GetRoot(), "animations") )
1068 if( OptionalChild animation = IsChild(*animations, animationName) )
1070 anim = Dali::Toolkit::Internal::CreateAnimation( *animation, replacement, sourceActor, this );
1074 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed\n", animationName.c_str() );
1079 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed (no animation section)\n", animationName.c_str() );
1085 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor )
1087 Replacement replacement(map, mReplacementMap);
1088 return CreateAnimation( animationName, replacement, sourceActor);
1091 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map )
1093 Replacement replacement(map, mReplacementMap);
1094 return CreateAnimation( animationName, replacement, Stage::GetCurrent().GetRootLayer() );
1097 Animation Builder::CreateAnimation( const std::string& animationName, Dali::Actor sourceActor )
1099 Replacement replacement( mReplacementMap );
1101 return CreateAnimation( animationName, replacement, sourceActor );
1104 Animation Builder::CreateAnimation( const std::string& animationName )
1106 Replacement replacement( mReplacementMap );
1108 return CreateAnimation( animationName, replacement, Dali::Stage::GetCurrent().GetRootLayer() );
1111 void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::UIFormat format )
1113 // parser to get constants and includes only
1114 Dali::Toolkit::JsonParser parser = Dali::Toolkit::JsonParser::New();
1116 if( !parser.Parse( data ) )
1118 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
1119 parser.GetErrorLineNumber(),
1120 parser.GetErrorColumn(),
1121 parser.GetErrorDescription().c_str() );
1123 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
1128 // load constant map (allows the user to override the constants in the json after loading)
1129 LoadConstants( *parser.GetRoot(), mReplacementMap );
1132 if( OptionalChild includes = IsChild(*parser.GetRoot(), KEYNAME_INCLUDES) )
1134 Replacement replacer( mReplacementMap );
1136 for(TreeNode::ConstIterator iter = (*includes).CBegin(); iter != (*includes).CEnd(); ++iter)
1138 OptionalString filename = replacer.IsString( (*iter).second );
1142 #if defined(DEBUG_ENABLED)
1143 DALI_SCRIPT_VERBOSE("Loading Include '%s'\n", (*filename).c_str());
1145 LoadFromString( GetFileContents(*filename) );
1150 if( !mParser.Parse( data ) )
1152 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
1153 mParser.GetErrorLineNumber(),
1154 mParser.GetErrorColumn(),
1155 mParser.GetErrorDescription().c_str() );
1157 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
1161 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Cannot parse JSON");
1165 void Builder::AddConstants( const Property::Map& map )
1167 mReplacementMap.Merge( map );
1170 void Builder::AddConstant( const std::string& key, const Property::Value& value )
1172 mReplacementMap[key] = value;
1175 const Property::Map& Builder::GetConstants() const
1177 return mReplacementMap;
1180 const Property::Value& Builder::GetConstant( const std::string& key ) const
1182 Property::Value* match = mReplacementMap.Find( key );
1189 static Property::Value invalid;
1194 void Builder::LoadConstants( const TreeNode& root, Property::Map& intoMap )
1196 Replacement replacer(intoMap);
1198 if( OptionalChild constants = IsChild(root, "constants") )
1200 for(TreeNode::ConstIterator iter = (*constants).CBegin();
1201 iter != (*constants).CEnd(); ++iter)
1203 Dali::Property::Value property;
1204 if( (*iter).second.GetName() )
1206 #if defined(DEBUG_ENABLED)
1207 DALI_SCRIPT_VERBOSE("Constant set from json '%s'\n", (*iter).second.GetName());
1209 if( SetPropertyFromNode( (*iter).second, property, replacer ) )
1211 intoMap[ (*iter).second.GetName() ] = property;
1215 DALI_SCRIPT_WARNING("Cannot convert property for constant %s\n",
1216 (*iter).second.GetName() == NULL ? "no name?" : (*iter).second.GetName());
1222 #if defined(DEBUG_ENABLED)
1223 Property::Value* iter = intoMap.Find( "CONFIG_SCRIPT_LOG_LEVEL" );
1224 if( iter && iter->GetType() == Property::STRING )
1226 std::string logLevel( iter->Get< std::string >() );
1227 if( logLevel == "NoLogging" )
1229 gFilterScript->SetLogLevel( Integration::Log::NoLogging );
1231 else if( logLevel == "Concise" )
1233 gFilterScript->SetLogLevel( Integration::Log::Concise );
1235 else if( logLevel == "General" )
1237 gFilterScript->SetLogLevel( Integration::Log::General );
1239 else if( logLevel == "Verbose" )
1241 gFilterScript->SetLogLevel( Integration::Log::Verbose );
1248 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle )
1250 Replacement replacer( mReplacementMap );
1251 return ApplyStyle( styleName, handle, replacer );
1254 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle, const Replacement& replacement )
1256 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1258 OptionalChild styles = IsChild( *mParser.GetRoot(), KEYNAME_STYLES );
1259 OptionalChild style = IsChild( *styles, styleName );
1261 if( styles && style )
1263 ApplyAllStyleProperties( *mParser.GetRoot(), *style, handle, replacement );
1272 BaseHandle Builder::Create( const std::string& templateName, const Property::Map& map )
1274 Replacement replacement( map, mReplacementMap );
1275 return Create( templateName, replacement );
1278 BaseHandle Builder::Create( const std::string& templateName, const Replacement& constant )
1280 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1282 BaseHandle baseHandle;
1284 OptionalChild templates = IsChild(*mParser.GetRoot(), KEYNAME_TEMPLATES);
1288 DALI_SCRIPT_WARNING("No template section found to CreateFromTemplate\n");
1292 OptionalChild childTemplate = IsChild(*templates, templateName);
1295 DALI_SCRIPT_WARNING("Template '%s' does not exist in template section\n", templateName.c_str());
1299 OptionalString type = constant.IsString( IsChild(*childTemplate, KEYNAME_TYPE) );
1303 DALI_SCRIPT_WARNING("Cannot create template '%s' as template section is missing 'type'\n", templateName.c_str());
1307 baseHandle = DoCreate( *mParser.GetRoot(), *childTemplate, Actor(), constant );
1315 BaseHandle Builder::CreateFromJson( const std::string& json )
1319 // merge in new template, hoping no one else has one named '@temp@'
1320 std::string newTemplate =
1321 std::string("{\"templates\":{\"@temp@\":") + \
1325 if( mParser.Parse(newTemplate) )
1327 Replacement replacement( mReplacementMap );
1328 ret = Create( "@temp@", replacement );
1334 bool Builder::ApplyFromJson( Handle& handle, const std::string& json )
1338 // merge new style, hoping no one else has one named '@temp@'
1339 std::string newStyle =
1340 std::string("{\"styles\":{\"@temp@\":") + \
1344 if( mParser.Parse(newStyle) )
1346 Replacement replacement( mReplacementMap );
1347 ret = ApplyStyle( "@temp@", handle, replacement );
1354 BaseHandle Builder::Create( const std::string& templateName )
1356 Replacement replacement( mReplacementMap );
1357 return Create( templateName, replacement );
1361 : mSlotDelegate( this )
1363 mParser = Dali::Toolkit::JsonParser::New();
1370 } // namespace Internal
1372 } // namespace Toolkit