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 #define TOKEN_STRING(x) #x
72 const std::string KEYNAME_STYLES = "styles";
73 const std::string KEYNAME_TYPE = "type";
74 const std::string KEYNAME_ACTORS = "actors";
75 const std::string KEYNAME_SIGNALS = "signals";
76 const std::string KEYNAME_NAME = "name";
77 const std::string KEYNAME_TEMPLATES = "templates";
78 const std::string KEYNAME_INCLUDES = "includes";
80 typedef std::vector<const TreeNode*> TreeNodeList;
83 std::string ToString(const T& value)
91 std::string ToString(const Rect<int>& value)
94 ss << value.x << "," << value.y << "," << value.width << "," << value.height;
98 #if defined(DEBUG_ENABLED)
100 std::string PropertyValueToString( const Property::Value& value )
104 switch( value.GetType() )
111 case Property::BOOLEAN:
113 ret = value.Get<bool>() ? "True" : "False";
116 case Property::FLOAT:
119 ret = ToString( value.Get<float>() );
122 case Property::INTEGER:
124 ret = ToString( value.Get<int>() );
127 case Property::VECTOR2:
129 ret = ToString( value.Get<Vector2>() );
132 case Property::VECTOR3:
134 ret = ToString( value.Get<Vector3>() );
137 case Property::VECTOR4:
139 ret = ToString( value.Get<Vector4>() );
142 case Property::MATRIX3:
144 ret = ToString( value.Get<Matrix3>() );
147 case Property::MATRIX:
149 ret = ToString( value.Get<Matrix>() );
152 case Property::RECTANGLE:
154 ret = ToString( value.Get< Rect<int> >() );
157 case Property::ROTATION:
161 case Property::STRING:
163 ret = value.Get<std::string>();
166 case Property::ARRAY:
168 ret = std::string("Array Size=") + ToString( value.Get<Property::Array>().Size() );
173 ret = std::string("Map Size=") + ToString( value.Get<Property::Map>().Count() );
180 #endif // DEBUG_ENABLED
183 * Recursively collects all stylesin a node (An array of style names).
185 * stylesCollection The set of styles from the json file (a json object of named styles)
186 * style The style array to begin the collection from
187 * styleList The style list to add nodes to apply
189 void CollectAllStyles( const TreeNode& stylesCollection, const TreeNode& style, TreeNodeList& styleList )
191 // style is an array of style names
192 if( TreeNode::ARRAY == style.GetType() )
194 for(TreeNode::ConstIterator iter = style.CBegin(); iter != style.CEnd(); ++iter)
196 if( OptionalString styleName = IsString( (*iter).second ) )
198 if( OptionalChild node = IsChild( stylesCollection, *styleName) )
200 styleList.push_back( &(*node) );
202 if( OptionalChild subStyle = IsChild( *node, KEYNAME_STYLES ) )
204 CollectAllStyles( stylesCollection, *subStyle, styleList );
216 * Sets the handle properties found in the tree node
218 void Builder::SetProperties( const TreeNode& node, Handle& handle, const Replacement& constant )
222 for( TreeNode::ConstIterator iter = node.CBegin(); iter != node.CEnd(); ++iter )
224 const TreeNode::KeyNodePair& keyChild = *iter;
226 std::string key( keyChild.first );
228 // ignore special fields; type,actors,signals,styles
229 if(key == KEYNAME_TYPE || key == KEYNAME_ACTORS || key == KEYNAME_SIGNALS || key == KEYNAME_STYLES)
234 // special field 'image' usually contains an json object description
235 // although sometimes refers to a framebuffer
236 if( 0 == keyChild.second.Size() )
240 ImageActor imageActor = ImageActor::DownCast(handle);
243 if( OptionalString s = constant.IsString( keyChild.second ) )
245 FrameBufferImage fb = GetFrameBufferImage(*s, constant);
248 imageActor.SetImage( fb );
255 // special field 'effect' references the shader effect instances
258 ImageActor actor = ImageActor::DownCast(handle);
261 OptionalString str = constant.IsString( keyChild.second );
264 ShaderEffect effect = GetShaderEffect( *str, constant );
265 actor.SetShaderEffect(effect);
270 DALI_SCRIPT_WARNING("Could not find or set shader effect\n");
276 Handle propertyObject( handle );
278 Dali::Property::Index index = propertyObject.GetPropertyIndex( key );
280 if( Property::INVALID_INDEX == index )
282 ImageActor actor = ImageActor::DownCast(handle);
285 if( ShaderEffect effect = actor.GetShaderEffect() )
287 index = effect.GetPropertyIndex( key );
288 if(index != Property::INVALID_INDEX)
290 propertyObject = effect;
296 if( Property::INVALID_INDEX != index )
298 Property::Type type = propertyObject.GetPropertyType(index);
300 Property::Value value;
301 if( !SetPropertyFromNode( keyChild.second, type, value, constant ) )
303 // verbose as this might not be a problem
304 // eg parentOrigin can be a string which is picked up later
305 DALI_SCRIPT_VERBOSE("Could not convert property:%s\n", key.c_str());
309 DALI_SCRIPT_VERBOSE("SetProperty '%s' Index=:%d Value Type=%d Value '%s'\n", key.c_str(), index, value.GetType(), PropertyValueToString(value).c_str() );
311 propertyObject.SetProperty( index, value );
316 DALI_SCRIPT_VERBOSE("SetProperty INVALID '%s' Index=:%d\n", key.c_str(), index);
319 } // for property nodes
323 DALI_SCRIPT_WARNING("Style applied to empty handle\n");
327 // Set properties from node on handle.
328 void Builder::ApplyProperties( const TreeNode& root, const TreeNode& node,
329 Dali::Handle& handle, const Replacement& constant )
331 if( Actor actor = Actor::DownCast(handle) )
333 SetProperties( node, actor, constant );
337 SetupActor( node, actor, constant );
340 SetupSignalAction( mSlotDelegate.GetConnectionTracker(), root, node, actor, this );
341 SetupPropertyNotification( mSlotDelegate.GetConnectionTracker(), root, node, actor, this );
346 SetProperties( node, handle, constant );
350 // Appling by style helper
351 // use FindChildByName() to apply properties referenced in KEYNAME_ACTORS in the node
352 void Builder::ApplyStylesByActor( const TreeNode& root, const TreeNode& node,
353 Dali::Handle& handle, const Replacement& constant )
355 if( Dali::Actor actor = Dali::Actor::DownCast( handle ) )
357 if( const TreeNode* actors = node.GetChild( KEYNAME_ACTORS ) )
359 // in a style the actor subtree properties referenced by actor name
360 for( TreeConstIter iter = actors->CBegin(); iter != actors->CEnd(); ++iter )
362 Dali::Actor foundActor;
366 foundActor = actor.FindChildByName( (*iter).first );
371 // debug log cannot find searched for actor
372 #if defined(DEBUG_ENABLED)
373 DALI_SCRIPT_VERBOSE("Cannot find actor in style application '%s'\n", (*iter).first);
378 #if defined(DEBUG_ENABLED)
379 DALI_SCRIPT_VERBOSE("Styles applied to actor '%s'\n", (*iter).first);
381 ApplyProperties( root, (*iter).second, foundActor, constant );
389 void Builder::ApplyAllStyleProperties( const TreeNode& root, const TreeNode& node,
390 Dali::Handle& handle, const Replacement& constant )
392 OptionalChild styles = IsChild(root, KEYNAME_STYLES);
393 OptionalChild style = IsChild(node, KEYNAME_STYLES);
395 if( styles && style )
397 TreeNodeList additionalStyles;
399 CollectAllStyles( *styles, *style, additionalStyles );
401 #if defined(DEBUG_ENABLED)
402 for(TreeNode::ConstIterator iter = (*style).CBegin(); iter != (*style).CEnd(); ++iter)
404 if( OptionalString styleName = IsString( (*iter).second ) )
406 DALI_SCRIPT_VERBOSE("Style Applied '%s'\n", (*styleName).c_str());
411 // a style may have other styles, which has other styles etc so we apply in reverse by convention.
412 for(TreeNodeList::reverse_iterator iter = additionalStyles.rbegin(); iter != additionalStyles.rend(); ++iter)
414 ApplyProperties( root, *(*iter), handle, constant );
416 ApplyStylesByActor( root, *(*iter), handle, constant );
420 // applying given node last
421 ApplyProperties( root, node, handle, constant );
423 ApplyStylesByActor( root, node, handle, constant );
429 * Create a dali type from a node.
430 * If parent given and an actor type was created then add it to the parent and
431 * recursively add nodes children.
433 BaseHandle Builder::DoCreate( const TreeNode& root, const TreeNode& node,
434 Actor parent, const Replacement& replacements )
436 BaseHandle baseHandle;
438 const TreeNode* templateNode = NULL;
440 if( OptionalString typeName = IsString(node, KEYNAME_TYPE) )
442 typeInfo = TypeRegistry::Get().GetTypeInfo( *typeName );
446 // a template name is also allowed inplace of the type name
447 OptionalChild templates = IsChild( root, KEYNAME_TEMPLATES);
451 if( OptionalChild isTemplate = IsChild( *templates, *typeName ) )
453 templateNode = &(*isTemplate);
455 if( OptionalString templateTypeName = IsString(*templateNode, KEYNAME_TYPE) )
457 typeInfo = TypeRegistry::Get().GetTypeInfo( *templateTypeName );
466 DALI_SCRIPT_WARNING("Cannot create Dali type from node '%s'\n", node.GetName());
470 baseHandle = typeInfo.CreateInstance();
471 Handle handle = Handle::DownCast(baseHandle);
472 Actor actor = Actor::DownCast(handle);
477 DALI_SCRIPT_VERBOSE("Create:%s\n", typeInfo.GetName().c_str());
479 #if defined(DEBUG_ENABLED)
482 DALI_SCRIPT_VERBOSE(" Is Handle Object=%d\n", (long*)handle.GetObjectPtr());
483 DALI_SCRIPT_VERBOSE(" Is Handle Property Count=%d\n", handle.GetPropertyCount());
488 DALI_SCRIPT_VERBOSE(" Is Actor id=%d\n", actor.GetId());
491 Toolkit::Control control = Toolkit::Control::DownCast(handle);
494 DALI_SCRIPT_VERBOSE(" Is Control id=%d\n", actor.GetId());
496 #endif // DEBUG_ENABLED
500 ApplyProperties( root, *templateNode, handle, replacements );
502 if( OptionalChild actors = IsChild( *templateNode, KEYNAME_ACTORS ) )
504 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
506 DoCreate( root, (*iter).second, actor, replacements );
513 // add children of all the styles
514 if( OptionalChild actors = IsChild( node, KEYNAME_ACTORS ) )
516 for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter )
518 DoCreate( root, (*iter).second, actor, replacements );
522 // apply style on top as they need the children to exist
523 ApplyAllStyleProperties( root, node, actor, replacements );
525 // then add to parent
533 ApplyProperties( root, node, handle, replacements );
538 DALI_SCRIPT_WARNING("Cannot create handle from type '%s'\n", typeInfo.GetName().c_str());
545 void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replacement& constant )
547 const Stage& stage = Stage::GetCurrent();
548 Layer root = stage.GetRootLayer();
550 if( OptionalString s = constant.IsString( IsChild(node, "sourceActor") ) )
552 Actor actor = root.FindChildByName(*s);
555 task.SetSourceActor( actor );
559 DALI_SCRIPT_WARNING("Cannot find source actor on stage for render task called '%s'\n", (*s).c_str() );
563 if( OptionalString s = constant.IsString( IsChild(node, "cameraActor") ) )
565 CameraActor actor = CameraActor::DownCast( root.FindChildByName(*s) );
568 task.SetCameraActor( actor );
572 DALI_SCRIPT_WARNING("Cannot find camera actor on stage for render task called '%s'\n", (*s).c_str() );
576 if( OptionalString s = constant.IsString( IsChild(node, "targetFrameBuffer") ) )
578 FrameBufferImage fb = GetFrameBufferImage( *s, constant );
581 task.SetTargetFrameBuffer( fb );
585 DALI_SCRIPT_WARNING("Cannot find target frame buffer '%s'\n", (*s).c_str() );
589 if( OptionalString s = constant.IsString( IsChild(node, "screenToFrameBufferFunction") ) )
591 if("DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION" == *s)
593 task.SetScreenToFrameBufferFunction( RenderTask::DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION );
595 else if("FULLSCREEN_FRAMEBUFFER_FUNCTION" == *s)
597 task.SetScreenToFrameBufferFunction( RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION );
601 DALI_SCRIPT_WARNING("todo");
605 // other setup is via the property system
606 SetProperties( node, task, constant );
609 void Builder::CreateRenderTask( const std::string &name )
611 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
613 Replacement constant(mReplacementMap);
615 const Stage& stage = Stage::GetCurrent();
617 OptionalChild tasks = IsChild(*mParser.GetRoot(), "renderTasks");
622 // Create the tasks from the current task as generally we want
623 // to setup task zero and onwards. Although this does overwrite
624 // the properties of the current task.
626 if( OptionalChild renderTask = IsChild(*tasks, name ) )
628 RenderTaskList list = stage.GetRenderTaskList();
629 unsigned int start = list.GetTaskCount();
634 // zero should have already been created by the stage so really
635 // this case should never happen
636 task = list.CreateTask();
640 TreeNode::ConstIterator iter = (*renderTask).CBegin();
641 task = list.GetTask( start - 1 );
643 SetupTask( task, (*iter).second, constant );
647 for(; iter != (*renderTask).CEnd(); ++iter )
649 task = list.CreateTask();
650 SetupTask( task, (*iter).second, constant );
656 ShaderEffect Builder::GetShaderEffect( const std::string &name)
658 Replacement constant( mReplacementMap );
659 return GetShaderEffect( name, constant );
662 ShaderEffect Builder::GetShaderEffect( const std::string &name, const Replacement& constant )
664 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
668 ShaderEffectLut::const_iterator iter( mShaderEffectLut.find( name ) );
669 if( iter != mShaderEffectLut.end() )
675 if( OptionalChild effects = IsChild( *mParser.GetRoot(), "shaderEffects") )
677 if( OptionalChild effect = IsChild( *effects, name ) )
679 Dali::Property::Value propertyMap(Property::MAP);
680 if( SetPropertyFromNode( *effect, Property::MAP, propertyMap, constant ) )
682 ret = Dali::Scripting::NewShaderEffect( propertyMap );
683 mShaderEffectLut[ name ] = ret;
692 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name )
694 Replacement constant( mReplacementMap );
695 return GetFrameBufferImage(name, constant);
698 FrameBufferImage Builder::GetFrameBufferImage( const std::string &name, const Replacement& constant )
700 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
702 FrameBufferImage ret;
704 ImageLut::const_iterator iter( mFrameBufferImageLut.find( name ) );
705 if( iter != mFrameBufferImageLut.end() )
711 if( OptionalChild images = IsChild( *mParser.GetRoot(), "frameBufferImages") )
713 if( OptionalChild image = IsChild( *images, name ) )
715 Dali::Property::Value property(Property::MAP);
716 if( SetPropertyFromNode( *image, Property::MAP, property, constant ) )
718 Property::Map* map = property.GetMap();
722 (*map)[ KEYNAME_TYPE ] = Property::Value(std::string("FrameBufferImage") );
723 ret = FrameBufferImage::DownCast( Dali::Scripting::NewImage( property ) );
724 mFrameBufferImageLut[ name ] = ret;
734 Path Builder::GetPath( const std::string& name )
736 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
740 PathLut::const_iterator iter( mPathLut.find( name ) );
741 if( iter != mPathLut.end() )
747 if( OptionalChild paths = IsChild( *mParser.GetRoot(), "paths") )
749 if( OptionalChild path = IsChild( *paths, name ) )
752 if( OptionalChild pointsProperty = IsChild( *path, "points") )
754 Dali::Property::Value points(Property::ARRAY);
755 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
758 ret.SetProperty( Path::Property::POINTS, points);
760 //controlPoints property
761 if( OptionalChild pointsProperty = IsChild( *path, "controlPoints") )
763 Dali::Property::Value points(Property::ARRAY);
764 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
766 ret.SetProperty( Path::Property::CONTROL_POINTS, points);
772 float curvature(0.25f);
773 if( OptionalFloat pointsProperty = IsFloat( *path, "curvature") )
775 curvature = *pointsProperty;
777 ret.GenerateControlPoints(curvature);
780 //Add the new path to the hash table for paths
781 mPathLut[ name ] = ret;
786 //Interpolation points not specified
787 DALI_SCRIPT_WARNING("Interpolation points not specified for path '%s'\n", name.c_str() );
797 PathConstrainer Builder::GetPathConstrainer( const std::string& name )
799 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
801 //Search the pathConstrainer in the LUT
802 size_t count( mPathConstrainerLut.size() );
803 for( size_t i(0); i!=count; ++i )
805 if( mPathConstrainerLut[i].name == name )
807 //PathConstrainer has already been created
808 return mPathConstrainerLut[i].pathConstrainer;
812 //Create a new PathConstrainer
814 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
816 if( OptionalChild pathConstrainer = IsChild( *constrainers, name ) )
818 OptionalString constrainerType(IsString(IsChild(*pathConstrainer, "type")));
821 DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str() );
823 else if( *constrainerType == "PathConstrainer")
826 if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "points") )
828 Dali::Property::Value points(Property::ARRAY);
829 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
831 ret = PathConstrainer::New();
832 ret.SetProperty( PathConstrainer::Property::POINTS, points);
834 //controlPoints property
835 if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "controlPoints") )
837 Dali::Property::Value points(Property::ARRAY);
838 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
840 ret.SetProperty( PathConstrainer::Property::CONTROL_POINTS, points);
844 OptionalVector3 forward( IsVector3( IsChild(*pathConstrainer, "forward" ) ) );
847 ret.SetProperty( PathConstrainer::Property::FORWARD, *forward);
850 //Add the new constrainer to the vector of PathConstrainer
851 PathConstrainerEntry entry = {name,ret};
852 mPathConstrainerLut.push_back( entry );
856 //Control points not specified
857 DALI_SCRIPT_WARNING("Control points not specified for pathConstrainer '%s'\n", name.c_str() );
863 //Interpolation points not specified
864 DALI_SCRIPT_WARNING("Interpolation points not specified for pathConstrainer '%s'\n", name.c_str() );
869 DALI_SCRIPT_WARNING("Constrainer '%s' is not a PathConstrainer\n", name.c_str() );
877 bool Builder::IsPathConstrainer( const std::string& name )
879 size_t count( mPathConstrainerLut.size() );
880 for( size_t i(0); i!=count; ++i )
882 if( mPathConstrainerLut[i].name == name )
888 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
890 if( OptionalChild constrainer = IsChild( *constrainers, name ) )
892 OptionalString constrainerType(IsString(IsChild(*constrainer, "type")));
899 return *constrainerType == "PathConstrainer";
906 Dali::LinearConstrainer Builder::GetLinearConstrainer( const std::string& name )
908 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
910 //Search the LinearConstrainer in the LUT
911 size_t count( mLinearConstrainerLut.size() );
912 for( size_t i(0); i!=count; ++i )
914 if( mLinearConstrainerLut[i].name == name )
916 //LinearConstrainer has already been created
917 return mLinearConstrainerLut[i].linearConstrainer;
921 //Create a new LinearConstrainer
922 LinearConstrainer ret;
923 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
925 if( OptionalChild linearConstrainer = IsChild( *constrainers, name ) )
927 OptionalString constrainerType(IsString(IsChild(*linearConstrainer, "type")));
930 DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str() );
932 else if( *constrainerType == "LinearConstrainer")
935 if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "value") )
937 Dali::Property::Value points(Property::ARRAY);
938 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
940 ret = Dali::LinearConstrainer::New();
941 ret.SetProperty( LinearConstrainer::Property::VALUE, points);
943 //controlPoints property
944 if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "progress") )
946 Dali::Property::Value points(Property::ARRAY);
947 if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) )
949 ret.SetProperty( LinearConstrainer::Property::PROGRESS, points);
952 //Add the new constrainer to vector of LinearConstrainer
953 LinearConstrainerEntry entry = {name,ret};
954 mLinearConstrainerLut.push_back( entry );
959 //Interpolation points not specified
960 DALI_SCRIPT_WARNING("Values not specified for LinearConstrainer '%s'\n", name.c_str() );
965 DALI_SCRIPT_WARNING("Constrainer '%s' is not a LinearConstrainer\n", name.c_str() );
973 bool Builder::IsLinearConstrainer( const std::string& name )
975 //Search the LinearConstrainer in the LUT
976 size_t count( mLinearConstrainerLut.size() );
977 for( size_t i(0); i!=count; ++i )
979 if( mLinearConstrainerLut[i].name == name )
985 if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") )
987 if( OptionalChild constrainer = IsChild( *constrainers, name ) )
989 OptionalString constrainerType(IsString(IsChild(*constrainer, "type")));
996 return *constrainerType == "LinearConstrainer";
1003 Toolkit::Builder::BuilderSignalType& Builder::QuitSignal()
1008 void Builder::EmitQuitSignal()
1013 void Builder::AddActors( Actor toActor )
1015 // 'stage' is the default/by convention section to add from
1016 AddActors( "stage", toActor );
1019 void Builder::AddActors( const std::string §ionName, Actor toActor )
1021 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1023 Property::Map overrideMap;
1024 Replacement replacements(overrideMap, mReplacementMap);
1026 OptionalChild add = IsChild(*mParser.GetRoot(), sectionName);
1030 for( TreeNode::ConstIterator iter = (*add).CBegin(); iter != (*add).CEnd(); ++iter )
1032 // empty actor adds directly to the stage
1033 BaseHandle baseHandle = DoCreate( *mParser.GetRoot(), (*iter).second, Actor(), replacements );
1034 Actor actor = Actor::DownCast(baseHandle);
1037 toActor.Add( actor );
1041 // if were adding the 'stage' section then also check for a render task called stage
1042 // to add automatically
1043 if( "stage" == sectionName )
1045 if( OptionalChild renderTasks = IsChild(*mParser.GetRoot(), "renderTasks") )
1047 if( OptionalChild tasks = IsChild(*renderTasks, "stage") )
1049 CreateRenderTask( "stage" );
1056 Animation Builder::CreateAnimation( const std::string& animationName, const Replacement& replacement, Dali::Actor sourceActor )
1058 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1062 if( OptionalChild animations = IsChild(*mParser.GetRoot(), "animations") )
1064 if( OptionalChild animation = IsChild(*animations, animationName) )
1066 anim = Dali::Toolkit::Internal::CreateAnimation( *animation, replacement, sourceActor, this );
1070 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed\n", animationName.c_str() );
1075 DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed (no animation section)\n", animationName.c_str() );
1081 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor )
1083 Replacement replacement(map, mReplacementMap);
1084 return CreateAnimation( animationName, replacement, sourceActor);
1087 Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map )
1089 Replacement replacement(map, mReplacementMap);
1090 return CreateAnimation( animationName, replacement, Stage::GetCurrent().GetRootLayer() );
1093 Animation Builder::CreateAnimation( const std::string& animationName, Dali::Actor sourceActor )
1095 Replacement replacement( mReplacementMap );
1097 return CreateAnimation( animationName, replacement, sourceActor );
1100 Animation Builder::CreateAnimation( const std::string& animationName )
1102 Replacement replacement( mReplacementMap );
1104 return CreateAnimation( animationName, replacement, Dali::Stage::GetCurrent().GetRootLayer() );
1107 void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::UIFormat format )
1109 // parser to get constants and includes only
1110 Dali::Toolkit::JsonParser parser = Dali::Toolkit::JsonParser::New();
1112 if( !parser.Parse( data ) )
1114 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
1115 parser.GetErrorLineNumber(),
1116 parser.GetErrorColumn(),
1117 parser.GetErrorDescription().c_str() );
1119 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
1123 // load constant map (allows the user to override the constants in the json after loading)
1124 LoadConstants( *parser.GetRoot(), mReplacementMap );
1127 if( OptionalChild includes = IsChild(*parser.GetRoot(), KEYNAME_INCLUDES) )
1129 Replacement replacer( mReplacementMap );
1131 for(TreeNode::ConstIterator iter = (*includes).CBegin(); iter != (*includes).CEnd(); ++iter)
1133 OptionalString filename = replacer.IsString( (*iter).second );
1137 #if defined(DEBUG_ENABLED)
1138 DALI_SCRIPT_VERBOSE("Loading Include '%s'\n", (*filename).c_str());
1140 LoadFromString( GetFileContents(*filename) );
1145 if( !mParser.Parse( data ) )
1147 DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n",
1148 mParser.GetErrorLineNumber(),
1149 mParser.GetErrorColumn(),
1150 mParser.GetErrorDescription().c_str() );
1152 DALI_ASSERT_ALWAYS(!"Cannot parse JSON");
1156 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Cannot parse JSON");
1159 void Builder::AddConstants( const Property::Map& map )
1161 mReplacementMap.Merge( map );
1164 void Builder::AddConstant( const std::string& key, const Property::Value& value )
1166 mReplacementMap[key] = value;
1169 const Property::Map& Builder::GetConstants() const
1171 return mReplacementMap;
1174 const Property::Value& Builder::GetConstant( const std::string& key ) const
1176 Property::Value* match = mReplacementMap.Find( key );
1183 static Property::Value invalid;
1188 void Builder::LoadConstants( const TreeNode& root, Property::Map& intoMap )
1190 Replacement replacer(intoMap);
1192 if( OptionalChild constants = IsChild(root, "constants") )
1194 for(TreeNode::ConstIterator iter = (*constants).CBegin();
1195 iter != (*constants).CEnd(); ++iter)
1197 Dali::Property::Value property;
1198 if( (*iter).second.GetName() )
1200 #if defined(DEBUG_ENABLED)
1201 DALI_SCRIPT_VERBOSE("Constant set from json '%s'\n", (*iter).second.GetName());
1203 if( SetPropertyFromNode( (*iter).second, property, replacer ) )
1205 intoMap[ (*iter).second.GetName() ] = property;
1209 DALI_SCRIPT_WARNING("Cannot convert property for constant %s\n",
1210 (*iter).second.GetName() == NULL ? "no name?" : (*iter).second.GetName());
1216 #if defined(DEBUG_ENABLED)
1217 Property::Value* iter = intoMap.Find( "CONFIG_SCRIPT_LOG_LEVEL" );
1218 if( iter && iter->GetType() == Property::STRING )
1220 std::string logLevel( iter->Get< std::string >() );
1221 if( logLevel == "NoLogging" )
1223 gFilterScript->SetLogLevel( Integration::Log::NoLogging );
1225 else if( logLevel == "Concise" )
1227 gFilterScript->SetLogLevel( Integration::Log::Concise );
1229 else if( logLevel == "General" )
1231 gFilterScript->SetLogLevel( Integration::Log::General );
1233 else if( logLevel == "Verbose" )
1235 gFilterScript->SetLogLevel( Integration::Log::Verbose );
1242 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle )
1244 Replacement replacer( mReplacementMap );
1245 return ApplyStyle( styleName, handle, replacer );
1248 bool Builder::ApplyStyle( const std::string& styleName, Handle& handle, const Replacement& replacement )
1250 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1252 OptionalChild styles = IsChild( *mParser.GetRoot(), KEYNAME_STYLES );
1253 OptionalChild style = IsChild( *styles, styleName );
1255 if( styles && style )
1257 ApplyAllStyleProperties( *mParser.GetRoot(), *style, handle, replacement );
1266 BaseHandle Builder::Create( const std::string& templateName, const Property::Map& map )
1268 Replacement replacement( map, mReplacementMap );
1269 return Create( templateName, replacement );
1272 BaseHandle Builder::Create( const std::string& templateName, const Replacement& constant )
1274 DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded");
1276 BaseHandle baseHandle;
1278 OptionalChild templates = IsChild(*mParser.GetRoot(), KEYNAME_TEMPLATES);
1282 DALI_SCRIPT_WARNING("No template section found to CreateFromTemplate\n");
1286 OptionalChild childTemplate = IsChild(*templates, templateName);
1289 DALI_SCRIPT_WARNING("Template '%s' does not exist in template section\n", templateName.c_str());
1293 OptionalString type = constant.IsString( IsChild(*childTemplate, KEYNAME_TYPE) );
1297 DALI_SCRIPT_WARNING("Cannot create template '%s' as template section is missing 'type'\n", templateName.c_str());
1301 baseHandle = DoCreate( *mParser.GetRoot(), *childTemplate, Actor(), constant );
1309 BaseHandle Builder::CreateFromJson( const std::string& json )
1313 // merge in new template, hoping no one else has one named '@temp@'
1314 std::string newTemplate =
1315 std::string("{\"templates\":{\"@temp@\":") + \
1319 if( mParser.Parse(newTemplate) )
1321 Replacement replacement( mReplacementMap );
1322 ret = Create( "@temp@", replacement );
1328 bool Builder::ApplyFromJson( Handle& handle, const std::string& json )
1332 // merge new style, hoping no one else has one named '@temp@'
1333 std::string newStyle =
1334 std::string("{\"styles\":{\"@temp@\":") + \
1338 if( mParser.Parse(newStyle) )
1340 Replacement replacement( mReplacementMap );
1341 ret = ApplyStyle( "@temp@", handle, replacement );
1348 BaseHandle Builder::Create( const std::string& templateName )
1350 Replacement replacement( mReplacementMap );
1351 return Create( templateName, replacement );
1355 : mSlotDelegate( this )
1357 mParser = Dali::Toolkit::JsonParser::New();
1359 Property::Map defaultDirs;
1360 defaultDirs[ TOKEN_STRING(DALI_IMAGE_DIR) ] = DALI_IMAGE_DIR;
1361 defaultDirs[ TOKEN_STRING(DALI_SOUND_DIR) ] = DALI_SOUND_DIR;
1362 defaultDirs[ TOKEN_STRING(DALI_STYLE_DIR) ] = DALI_STYLE_DIR;
1363 defaultDirs[ TOKEN_STRING(DALI_STYLE_IMAGE_DIR) ] = DALI_STYLE_IMAGE_DIR;
1365 AddConstants( defaultDirs );
1372 } // namespace Internal
1374 } // namespace Toolkit