X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fbuilder%2Fbuilder-impl.cpp;h=9ddab28b4468662497e6eab89df70bd5bb47772a;hp=820343710166eb160ace8a987434b4f23b5dbf63;hb=f2039d47f9bed8104575da80a2ecf0bb6e37ff8d;hpb=27833bb592e5b8a271f12754dee5f1836becab51 diff --git a/dali-toolkit/internal/builder/builder-impl.cpp b/dali-toolkit/internal/builder/builder-impl.cpp index 8203437..9ddab28 100644 --- a/dali-toolkit/internal/builder/builder-impl.cpp +++ b/dali-toolkit/internal/builder/builder-impl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * Copyright (c) 2021 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,78 +22,85 @@ #include #include -#include +#include +#include +#include +#include +#include +#include +#include #include #include -#include -#include -#include -#include +#include +#include #include -#include // INTERNAL INCLUDES -#include +#include #include +#include -#include -#include #include +#include +#include +#include #include #include #include -#include - namespace Dali { - namespace Toolkit { - namespace Internal { class Replacement; -extern Animation CreateAnimation(const TreeNode& child, const Replacement& replacements, const Dali::Actor searchRoot, Builder* const builder ); -extern Actor SetupSignalAction(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, Dali::Toolkit::Internal::Builder* const builder); -extern Actor SetupPropertyNotification(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, Dali::Toolkit::Internal::Builder* const builder); +extern Animation CreateAnimation(const TreeNode& child, const Replacement& replacements, const Dali::Actor searchRoot, Builder* const builder); + +extern Actor SetupSignalAction(ConnectionTracker* tracker, const TreeNode& root, const TreeNode& child, Actor actor, Dali::Toolkit::Internal::Builder* const builder); + +extern Actor SetupPropertyNotification(ConnectionTracker* tracker, const TreeNode& root, const TreeNode& child, Actor actor, Dali::Toolkit::Internal::Builder* const builder); #if defined(DEBUG_ENABLED) -Integration::Log::Filter* gFilterScript = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_SCRIPT"); +Integration::Log::Filter* gFilterScript = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_SCRIPT"); #endif namespace { - #define TOKEN_STRING(x) #x -const std::string KEYNAME_STYLES = "styles"; -const std::string KEYNAME_TYPE = "type"; -const std::string KEYNAME_ACTORS = "actors"; -const std::string KEYNAME_SIGNALS = "signals"; -const std::string KEYNAME_NAME = "name"; -const std::string KEYNAME_TEMPLATES = "templates"; -const std::string KEYNAME_INCLUDES = "includes"; -const std::string KEYNAME_MAPPINGS = "mappings"; - -const std::string PROPERTIES = "properties"; +const std::string KEYNAME_ACTORS = "actors"; +const std::string KEYNAME_ENTRY_TRANSITION = "entryTransition"; +const std::string KEYNAME_EXIT_TRANSITION = "exitTransition"; +const std::string KEYNAME_INCLUDES = "includes"; +const std::string KEYNAME_INHERIT = "inherit"; +const std::string KEYNAME_MAPPINGS = "mappings"; +const std::string KEYNAME_NAME = "name"; +const std::string KEYNAME_SIGNALS = "signals"; +const std::string KEYNAME_STATES = "states"; +const std::string KEYNAME_STYLES = "styles"; +const std::string KEYNAME_TEMPLATES = "templates"; +const std::string KEYNAME_TRANSITIONS = "transitions"; +const std::string KEYNAME_TYPE = "type"; +const std::string KEYNAME_VISUALS = "visuals"; + +const std::string PROPERTIES = "properties"; const std::string ANIMATABLE_PROPERTIES = "animatableProperties"; typedef std::vector TreeNodeList; - -bool GetMappingKey( const std::string& str, std::string& key ) +bool GetMappingKey(const std::string& str, std::string& key) { - bool result = false; - std::string test( str ); - if( ! test.empty() ) + bool result = false; + std::string test(str); + if(!test.empty()) { - if( test.at(0) == '<' ) + if(test.at(0) == '<') { - if( test.at(test.length()-1) == '>' ) + if(test.at(test.length() - 1) == '>') { - key = test.substr( 1, test.length()-2 ); + key = test.substr(1, test.length() - 2); result = true; } } @@ -108,22 +115,27 @@ bool GetMappingKey( const std::string& str, std::string& key ) * style The style array to begin the collection from * styleList The style list to add nodes to apply */ -void CollectAllStyles( const TreeNode& stylesCollection, const TreeNode& style, TreeNodeList& styleList ) +void CollectAllStyles(const TreeNode& stylesCollection, const TreeNode& style, TreeNodeList& styleList) { // style is an array of style names - if( TreeNode::ARRAY == style.GetType() ) + if(TreeNode::ARRAY == style.GetType()) { for(TreeNode::ConstIterator iter = style.CBegin(); iter != style.CEnd(); ++iter) { - if( OptionalString styleName = IsString( (*iter).second ) ) + if(OptionalString styleName = IsString((*iter).second)) { - if( OptionalChild node = IsChild( stylesCollection, *styleName) ) + if(OptionalChild node = IsChildIgnoreCase(stylesCollection, *styleName)) { - styleList.push_back( &(*node) ); + styleList.push_back(&(*node)); - if( OptionalChild subStyle = IsChild( *node, KEYNAME_STYLES ) ) + OptionalChild subStyle = IsChild(*node, KEYNAME_INHERIT); + if(!subStyle) + { + subStyle = IsChild(*node, KEYNAME_STYLES); + } + if(subStyle) { - CollectAllStyles( stylesCollection, *subStyle, styleList ); + CollectAllStyles(stylesCollection, *subStyle, styleList); } } } @@ -131,387 +143,276 @@ void CollectAllStyles( const TreeNode& stylesCollection, const TreeNode& style, } } +} // namespace -} // namespace anon - -/* - * Sets the handle properties found in the tree node - */ -void Builder::SetProperties( const TreeNode& node, Handle& handle, const Replacement& constant ) +Builder::Builder() +: mSlotDelegate(this) { - if( handle ) - { + mParser = Dali::Toolkit::JsonParser::New(); - for( TreeNode::ConstIterator iter = node.CBegin(); iter != node.CEnd(); ++iter ) - { - const TreeNode::KeyNodePair& keyChild = *iter; + Property::Map defaultConstants; + defaultConstants[TOKEN_STRING(DALI_IMAGE_DIR)] = AssetManager::GetDaliImagePath(); + defaultConstants[TOKEN_STRING(DALI_SOUND_DIR)] = AssetManager::GetDaliSoundPath(); + defaultConstants[TOKEN_STRING(DALI_STYLE_DIR)] = AssetManager::GetDaliStylePath(); + defaultConstants[TOKEN_STRING(DALI_STYLE_IMAGE_DIR)] = AssetManager::GetDaliStyleImagePath(); + defaultConstants[TOKEN_STRING(DALI_SHADER_VERSION_PREFIX)] = Shader::GetShaderVersionPrefix(); + defaultConstants[TOKEN_STRING(DALI_VERTEX_SHADER_PREFIX)] = Shader::GetVertexShaderPrefix(); + defaultConstants[TOKEN_STRING(DALI_FRAGMENT_SHADER_PREFIX)] = Shader::GetFragmentShaderPrefix(); - std::string key( keyChild.first ); + AddConstants(defaultConstants); +} - // ignore special fields; type,actors,signals,styles - if(key == KEYNAME_TYPE || key == KEYNAME_ACTORS || key == KEYNAME_SIGNALS || key == KEYNAME_STYLES || key == KEYNAME_MAPPINGS ) - { - continue; - } +void Builder::LoadFromString(std::string const& data, Dali::Toolkit::Builder::UIFormat format) +{ + // parser to get constants and includes only + Dali::Toolkit::JsonParser parser = Dali::Toolkit::JsonParser::New(); - Handle propertyObject( handle ); + if(!parser.Parse(data)) + { + DALI_LOG_WARNING("JSON Parse Error:%d:%d:'%s'\n", + parser.GetErrorLineNumber(), + parser.GetErrorColumn(), + parser.GetErrorDescription().c_str()); - Dali::Property::Index index = propertyObject.GetPropertyIndex( key ); + DALI_ASSERT_ALWAYS(!"Cannot parse JSON"); + } + else + { + // load constant map (allows the user to override the constants in the json after loading) + LoadConstants(*parser.GetRoot(), mReplacementMap); + // load configuration map + LoadConfiguration(*parser.GetRoot(), mConfigurationMap); + // merge includes + if(OptionalChild includes = IsChild(*parser.GetRoot(), KEYNAME_INCLUDES)) + { + Replacement replacer(mReplacementMap); - if( Property::INVALID_INDEX != index ) + for(TreeNode::ConstIterator iter = (*includes).CBegin(); iter != (*includes).CEnd(); ++iter) { - Property::Type type = propertyObject.GetPropertyType(index); - Property::Value value; - bool mapped = false; + OptionalString filename = replacer.IsString((*iter).second); - // if node.value is a mapping, get the property value from the "mappings" table - if( keyChild.second.GetType() == TreeNode::STRING ) - { - std::string mappingKey; - if( GetMappingKey(keyChild.second.GetString(), mappingKey) ) - { - OptionalChild mappingRoot = IsChild( mParser.GetRoot(), KEYNAME_MAPPINGS ); - mapped = GetPropertyMap( *mappingRoot, mappingKey.c_str(), type, value ); - } - } - if( ! mapped ) - { - mapped = DeterminePropertyFromNode( keyChild.second, type, value, constant ); - if( ! mapped ) - { - // Just determine the property from the node and if it's valid, let the property object handle it - DeterminePropertyFromNode( keyChild.second, value, constant ); - mapped = ( value.GetType() != Property::NONE ); - } - } - if( mapped ) + if(filename) { - DALI_SCRIPT_VERBOSE("SetProperty '%s' Index=:%d Value Type=%d Value '%s'\n", key.c_str(), index, value.GetType(), PropertyValueToString(value).c_str() ); - - propertyObject.SetProperty( index, value ); +#if defined(DEBUG_ENABLED) + DALI_SCRIPT_VERBOSE("Loading Include '%s'\n", (*filename).c_str()); +#endif + LoadFromString(GetFileContents(*filename)); } } - else - { - DALI_SCRIPT_VERBOSE("SetProperty INVALID '%s' Index=:%d\n", key.c_str(), index); - } + } - // Add custom properties - SetCustomProperties(node, handle, constant, PROPERTIES, Property::READ_WRITE); - SetCustomProperties(node, handle, constant, ANIMATABLE_PROPERTIES, Property::ANIMATABLE); + if(mParser.Parse(data)) + { + // Drop the styles and get them to be rebuilt against the new parse tree as required. + mStyles.Clear(); + } + else + { + DALI_LOG_WARNING("JSON Parse Error:%d:%d:'%s'\n", + mParser.GetErrorLineNumber(), + mParser.GetErrorColumn(), + mParser.GetErrorDescription().c_str()); - } // for property nodes - } - else - { - DALI_SCRIPT_WARNING("Style applied to empty handle\n"); + DALI_ASSERT_ALWAYS(!"Cannot parse JSON"); + } } + + DUMP_PARSE_TREE(mParser); // This macro only writes out if DEBUG is enabled and the "DUMP_TREE" constant is defined in the stylesheet. + DUMP_TEST_MAPPINGS(mParser); + + DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Cannot parse JSON"); } -void Builder::SetCustomProperties( const TreeNode& node, Handle& handle, const Replacement& constant, - const std::string& childName, Property::AccessMode accessMode ) +void Builder::AddConstants(const Property::Map& map) { - // Add custom properties - if( OptionalChild customPropertiesChild = IsChild(node, childName) ) - { - const TreeNode& customPropertiesNode = *customPropertiesChild; - const TreeConstIter endIter = customPropertiesNode.CEnd(); - for( TreeConstIter iter = customPropertiesNode.CBegin(); endIter != iter; ++iter ) - { - const TreeNode::KeyNodePair& keyChild = *iter; - std::string key( keyChild.first ); + mReplacementMap.Merge(map); +} - Property::Value value; - DeterminePropertyFromNode( keyChild.second, value, constant ); - // Register/Set property. - handle.RegisterProperty( key, value, accessMode ); - } - } +void Builder::AddConstant(const std::string& key, const Property::Value& value) +{ + mReplacementMap[key] = value; } -// Set properties from node on handle. -void Builder::ApplyProperties( const TreeNode& root, const TreeNode& node, - Dali::Handle& handle, const Replacement& constant ) +const Property::Map& Builder::GetConfigurations() const { - if( Actor actor = Actor::DownCast(handle) ) - { - SetProperties( node, actor, constant ); + return mConfigurationMap; +} - if( actor ) - { - // add signals - SetupSignalAction( mSlotDelegate.GetConnectionTracker(), root, node, actor, this ); - SetupPropertyNotification( mSlotDelegate.GetConnectionTracker(), root, node, actor, this ); - } +const Property::Map& Builder::GetConstants() const +{ + return mReplacementMap; +} + +const Property::Value& Builder::GetConstant(const std::string& key) const +{ + Property::Value* match = mReplacementMap.Find(key); + if(match) + { + return (*match); } else { - SetProperties( node, handle, constant ); + static Property::Value invalid; + return invalid; } } -// Appling by style helper -// use FindChildByName() to apply properties referenced in KEYNAME_ACTORS in the node -void Builder::ApplyStylesByActor( const TreeNode& root, const TreeNode& node, - Dali::Handle& handle, const Replacement& constant ) +Animation Builder::CreateAnimation(const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor) { - if( Dali::Actor actor = Dali::Actor::DownCast( handle ) ) - { - if( const TreeNode* actors = node.GetChild( KEYNAME_ACTORS ) ) - { - // in a style the actor subtree properties referenced by actor name - for( TreeConstIter iter = actors->CBegin(); iter != actors->CEnd(); ++iter ) - { - Dali::Actor foundActor; - - if( (*iter).first ) - { - foundActor = actor.FindChildByName( (*iter).first ); - } - - if( !foundActor ) - { - // debug log cannot find searched for actor -#if defined(DEBUG_ENABLED) - DALI_SCRIPT_VERBOSE("Cannot find actor in style application '%s'\n", (*iter).first); -#endif - } - else - { -#if defined(DEBUG_ENABLED) - DALI_SCRIPT_VERBOSE("Styles applied to actor '%s'\n", (*iter).first); -#endif - ApplyProperties( root, (*iter).second, foundActor, constant ); - } - } - } - } + Replacement replacement(map, mReplacementMap); + return CreateAnimation(animationName, replacement, sourceActor); } +Animation Builder::CreateAnimation(const std::string& animationName, const Property::Map& map) +{ + Replacement replacement(map, mReplacementMap); + return CreateAnimation(animationName, replacement, Stage::GetCurrent().GetRootLayer()); +} -void Builder::ApplyAllStyleProperties( const TreeNode& root, const TreeNode& node, - Dali::Handle& handle, const Replacement& constant ) +Animation Builder::CreateAnimation(const std::string& animationName, Dali::Actor sourceActor) { - OptionalChild styles = IsChild(root, KEYNAME_STYLES); - OptionalChild style = IsChild(node, KEYNAME_STYLES); + Replacement replacement(mReplacementMap); - if( styles && style ) - { - TreeNodeList additionalStyles; + return CreateAnimation(animationName, replacement, sourceActor); +} - CollectAllStyles( *styles, *style, additionalStyles ); +Animation Builder::CreateAnimation(const std::string& animationName) +{ + Replacement replacement(mReplacementMap); -#if defined(DEBUG_ENABLED) - for(TreeNode::ConstIterator iter = (*style).CBegin(); iter != (*style).CEnd(); ++iter) - { - if( OptionalString styleName = IsString( (*iter).second ) ) - { - DALI_SCRIPT_VERBOSE("Style Applied '%s'\n", (*styleName).c_str()); - } - } -#endif + return CreateAnimation(animationName, replacement, Dali::Stage::GetCurrent().GetRootLayer()); +} - // a style may have other styles, which has other styles etc so we apply in reverse by convention. - for(TreeNodeList::reverse_iterator iter = additionalStyles.rbegin(); iter != additionalStyles.rend(); ++iter) - { - ApplyProperties( root, *(*iter), handle, constant ); +BaseHandle Builder::Create(const std::string& templateName) +{ + Replacement replacement(mReplacementMap); + return Create(templateName, replacement); +} - ApplyStylesByActor( root, *(*iter), handle, constant ); - } - } +BaseHandle Builder::Create(const std::string& templateName, const Property::Map& map) +{ + Replacement replacement(map, mReplacementMap); + return Create(templateName, replacement); +} - // applying given node last - ApplyProperties( root, node, handle, constant ); +BaseHandle Builder::CreateFromJson(const std::string& json) +{ + BaseHandle ret; - ApplyStylesByActor( root, node, handle, constant ); + // merge in new template, hoping no one else has one named '@temp@' + std::string newTemplate = + std::string("{\"templates\":{\"@temp@\":") + + json + + std::string("}}"); -} + if(mParser.Parse(newTemplate)) + { + Replacement replacement(mReplacementMap); + ret = Create("@temp@", replacement); + } + return ret; +} -/* - * Create a dali type from a node. - * If parent given and an actor type was created then add it to the parent and - * recursively add nodes children. - */ -BaseHandle Builder::DoCreate( const TreeNode& root, const TreeNode& node, - Actor parent, const Replacement& replacements ) +bool Builder::ApplyFromJson(Handle& handle, const std::string& json) { - BaseHandle baseHandle; - TypeInfo typeInfo; - const TreeNode* templateNode = NULL; + bool ret = false; + + // merge new style, hoping no one else has one named '@temp@' + std::string newStyle = + std::string("{\"styles\":{\"@temp@\":") + + json + + std::string("}}"); - if( OptionalString typeName = IsString(node, KEYNAME_TYPE) ) + if(mParser.Parse(newStyle)) { - typeInfo = TypeRegistry::Get().GetTypeInfo( *typeName ); + Replacement replacement(mReplacementMap); + ret = ApplyStyle("@temp@", handle, replacement); + } - if( !typeInfo ) - { - // a template name is also allowed inplace of the type name - OptionalChild templates = IsChild( root, KEYNAME_TEMPLATES); + return ret; +} - if( templates ) - { - if( OptionalChild isTemplate = IsChild( *templates, *typeName ) ) - { - templateNode = &(*isTemplate); +bool Builder::ApplyStyle(const std::string& styleName, Handle& handle) +{ + Replacement replacer(mReplacementMap); + return ApplyStyle(styleName, handle, replacer); +} - if( OptionalString templateTypeName = IsString(*templateNode, KEYNAME_TYPE) ) - { - typeInfo = TypeRegistry::Get().GetTypeInfo( *templateTypeName ); - } - } - } - } +bool Builder::LookupStyleName(const std::string& styleName) +{ + DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded"); + + OptionalChild styles = IsChild(*mParser.GetRoot(), KEYNAME_STYLES); + OptionalChild style = IsChildIgnoreCase(*styles, styleName); + + if(styles && style) + { + return true; } + return false; +} - if(!typeInfo) +const StylePtr Builder::GetStyle(const std::string& styleName) +{ + const StylePtr* style = mStyles.FindConst(styleName); + + if(style == NULL) { - DALI_SCRIPT_WARNING("Cannot create Dali type from node '%s'\n", node.GetName()); + return StylePtr(NULL); } else { - baseHandle = typeInfo.CreateInstance(); - Handle handle = Handle::DownCast(baseHandle); - Actor actor = Actor::DownCast(handle); + return *style; + } +} - if(handle) - { +void Builder::AddActors(Actor toActor) +{ + // 'stage' is the default/by convention section to add from + AddActors("stage", toActor); +} - DALI_SCRIPT_VERBOSE("Create:%s\n", typeInfo.GetName().c_str()); +void Builder::AddActors(const std::string& sectionName, Actor toActor) +{ + DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded"); -#if defined(DEBUG_ENABLED) - if(handle) - { - DALI_SCRIPT_VERBOSE(" Is Handle Object=%d\n", (long*)handle.GetObjectPtr()); - DALI_SCRIPT_VERBOSE(" Is Handle Property Count=%d\n", handle.GetPropertyCount()); - } + Property::Map overrideMap; + Replacement replacements(overrideMap, mReplacementMap); - if(actor) - { - DALI_SCRIPT_VERBOSE(" Is Actor id=%d\n", actor.GetId()); - } + OptionalChild add = IsChild(*mParser.GetRoot(), sectionName); - Toolkit::Control control = Toolkit::Control::DownCast(handle); - if(control) + if(add) + { + for(TreeNode::ConstIterator iter = (*add).CBegin(); iter != (*add).CEnd(); ++iter) + { + // empty actor adds directly to the stage + BaseHandle baseHandle = DoCreate(*mParser.GetRoot(), (*iter).second, Actor(), replacements); + Actor actor = Actor::DownCast(baseHandle); + if(actor) { - DALI_SCRIPT_VERBOSE(" Is Control id=%d\n", actor.GetId()); + toActor.Add(actor); } -#endif // DEBUG_ENABLED + } - if( templateNode ) + // if were adding the 'stage' section then also check for a render task called stage + // to add automatically + if("stage" == sectionName) + { + if(OptionalChild renderTasks = IsChild(*mParser.GetRoot(), "renderTasks")) { - ApplyProperties( root, *templateNode, handle, replacements ); - - if( OptionalChild actors = IsChild( *templateNode, KEYNAME_ACTORS ) ) + if(OptionalChild tasks = IsChild(*renderTasks, "stage")) { - for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter ) - { - DoCreate( root, (*iter).second, actor, replacements ); - } + CreateRenderTask("stage"); } } + } + } +} - if( actor ) - { - // add children of all the styles - if( OptionalChild actors = IsChild( node, KEYNAME_ACTORS ) ) - { - for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter ) - { - DoCreate( root, (*iter).second, actor, replacements ); - } - } - - // apply style on top as they need the children to exist - ApplyAllStyleProperties( root, node, actor, replacements ); - - // then add to parent - if( parent ) - { - parent.Add( actor ); - } - } - else - { - ApplyProperties( root, node, handle, replacements ); - } - } - else - { - DALI_SCRIPT_WARNING("Cannot create handle from type '%s'\n", typeInfo.GetName().c_str()); - } - } - - return baseHandle; -} - -void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replacement& constant ) -{ - const Stage& stage = Stage::GetCurrent(); - Layer root = stage.GetRootLayer(); - - if( OptionalString s = constant.IsString( IsChild(node, "sourceActor") ) ) - { - Actor actor = root.FindChildByName(*s); - if(actor) - { - task.SetSourceActor( actor ); - } - else - { - DALI_SCRIPT_WARNING("Cannot find source actor on stage for render task called '%s'\n", (*s).c_str() ); - } - } - - if( OptionalString s = constant.IsString( IsChild(node, "cameraActor") ) ) - { - CameraActor actor = CameraActor::DownCast( root.FindChildByName(*s) ); - if(actor) - { - task.SetCameraActor( actor ); - } - else - { - DALI_SCRIPT_WARNING("Cannot find camera actor on stage for render task called '%s'\n", (*s).c_str() ); - } - } - - if( OptionalString s = constant.IsString( IsChild(node, "targetFrameBuffer") ) ) - { - FrameBufferImage fb = GetFrameBufferImage( *s, constant ); - if(fb) - { - task.SetTargetFrameBuffer( fb ); - } - else - { - DALI_SCRIPT_WARNING("Cannot find target frame buffer '%s'\n", (*s).c_str() ); - } - } - - if( OptionalString s = constant.IsString( IsChild(node, "screenToFrameBufferFunction") ) ) - { - if("DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION" == *s) - { - task.SetScreenToFrameBufferFunction( RenderTask::DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION ); - } - else if("FULLSCREEN_FRAMEBUFFER_FUNCTION" == *s) - { - task.SetScreenToFrameBufferFunction( RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION ); - } - else - { - DALI_SCRIPT_WARNING("todo"); - } - } - - // other setup is via the property system - SetProperties( node, task, constant ); -} - -void Builder::CreateRenderTask( const std::string &name ) -{ - DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded"); +void Builder::CreateRenderTask(const std::string& name) +{ + DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded"); Replacement constant(mReplacementMap); @@ -526,10 +427,10 @@ void Builder::CreateRenderTask( const std::string &name ) // to setup task zero and onwards. Although this does overwrite // the properties of the current task. // - if( OptionalChild renderTask = IsChild(*tasks, name ) ) + if(OptionalChild renderTask = IsChild(*tasks, name)) { - RenderTaskList list = stage.GetRenderTaskList(); - unsigned int start = list.GetTaskCount(); + RenderTaskList list = stage.GetRenderTaskList(); + unsigned int start = list.GetTaskCount(); RenderTask task; if(0 == start) @@ -541,103 +442,61 @@ void Builder::CreateRenderTask( const std::string &name ) } TreeNode::ConstIterator iter = (*renderTask).CBegin(); - task = list.GetTask( start - 1 ); + task = list.GetTask(start - 1); - SetupTask( task, (*iter).second, constant ); + SetupTask(task, (*iter).second, constant); ++iter; - for(; iter != (*renderTask).CEnd(); ++iter ) + for(; iter != (*renderTask).CEnd(); ++iter) { task = list.CreateTask(); - SetupTask( task, (*iter).second, constant ); - } - } - } -} - -FrameBufferImage Builder::GetFrameBufferImage( const std::string &name ) -{ - Replacement constant( mReplacementMap ); - return GetFrameBufferImage(name, constant); -} - -FrameBufferImage Builder::GetFrameBufferImage( const std::string &name, const Replacement& constant ) -{ - DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded"); - - FrameBufferImage ret; - - ImageLut::const_iterator iter( mFrameBufferImageLut.find( name ) ); - if( iter != mFrameBufferImageLut.end() ) - { - ret = iter->second; - } - else - { - if( OptionalChild images = IsChild( *mParser.GetRoot(), "frameBufferImages") ) - { - if( OptionalChild image = IsChild( *images, name ) ) - { - Dali::Property::Value property(Property::MAP); - if( DeterminePropertyFromNode( *image, Property::MAP, property, constant ) ) - { - Property::Map* map = property.GetMap(); - - if( map ) - { - (*map)[ KEYNAME_TYPE ] = Property::Value(std::string("FrameBufferImage") ); - ret = FrameBufferImage::DownCast( Dali::Scripting::NewImage( property ) ); - mFrameBufferImageLut[ name ] = ret; - } - } + SetupTask(task, (*iter).second, constant); } } } - - return ret; } -Path Builder::GetPath( const std::string& name ) +Path Builder::GetPath(const std::string& name) { DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded"); Path ret; - PathLut::const_iterator iter( mPathLut.find( name ) ); - if( iter != mPathLut.end() ) + PathLut::const_iterator iter(mPathLut.find(name)); + if(iter != mPathLut.end()) { ret = iter->second; } else { - if( OptionalChild paths = IsChild( *mParser.GetRoot(), "paths") ) + if(OptionalChild paths = IsChild(*mParser.GetRoot(), "paths")) { - if( OptionalChild path = IsChild( *paths, name ) ) + if(OptionalChild path = IsChild(*paths, name)) { //points property - if( OptionalChild pointsProperty = IsChild( *path, "points") ) + if(OptionalChild pointsProperty = IsChild(*path, "points")) { Dali::Property::Value points(Property::ARRAY); - if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) ) + if(DeterminePropertyFromNode(*pointsProperty, Property::ARRAY, points)) { ret = Path::New(); - ret.SetProperty( Path::Property::POINTS, points); + ret.SetProperty(Path::Property::POINTS, points); //controlPoints property - if( OptionalChild pointsProperty = IsChild( *path, "controlPoints") ) + if(OptionalChild pointsProperty = IsChild(*path, "controlPoints")) { Dali::Property::Value points(Property::ARRAY); - if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) ) + if(DeterminePropertyFromNode(*pointsProperty, Property::ARRAY, points)) { - ret.SetProperty( Path::Property::CONTROL_POINTS, points); + ret.SetProperty(Path::Property::CONTROL_POINTS, points); } } else { //Curvature float curvature(0.25f); - if( OptionalFloat pointsProperty = IsFloat( *path, "curvature") ) + if(OptionalFloat pointsProperty = IsFloat(*path, "curvature")) { curvature = *pointsProperty; } @@ -645,31 +504,30 @@ Path Builder::GetPath( const std::string& name ) } //Add the new path to the hash table for paths - mPathLut[ name ] = ret; + mPathLut[name] = ret; } } else { //Interpolation points not specified - DALI_SCRIPT_WARNING("Interpolation points not specified for path '%s'\n", name.c_str() ); + DALI_SCRIPT_WARNING("Interpolation points not specified for path '%s'\n", name.c_str()); } } - } } return ret; } -PathConstrainer Builder::GetPathConstrainer( const std::string& name ) +PathConstrainer Builder::GetPathConstrainer(const std::string& name) { DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded"); //Search the pathConstrainer in the LUT - size_t count( mPathConstrainerLut.size() ); - for( size_t i(0); i!=count; ++i ) + size_t count(mPathConstrainerLut.size()); + for(size_t i(0); i != count; ++i) { - if( mPathConstrainerLut[i].name == name ) + if(mPathConstrainerLut[i].name == name) { //PathConstrainer has already been created return mPathConstrainerLut[i].pathConstrainer; @@ -678,62 +536,62 @@ PathConstrainer Builder::GetPathConstrainer( const std::string& name ) //Create a new PathConstrainer PathConstrainer ret; - if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") ) + if(OptionalChild constrainers = IsChild(*mParser.GetRoot(), "constrainers")) { - if( OptionalChild pathConstrainer = IsChild( *constrainers, name ) ) + if(OptionalChild pathConstrainer = IsChild(*constrainers, name)) { OptionalString constrainerType(IsString(IsChild(*pathConstrainer, "type"))); if(!constrainerType) { - DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str() ); + DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str()); } - else if( *constrainerType == "PathConstrainer") + else if(*constrainerType == "PathConstrainer") { //points property - if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "points") ) + if(OptionalChild pointsProperty = IsChild(*pathConstrainer, "points")) { Dali::Property::Value points(Property::ARRAY); - if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) ) + if(DeterminePropertyFromNode(*pointsProperty, Property::ARRAY, points)) { ret = PathConstrainer::New(); - ret.SetProperty( PathConstrainer::Property::POINTS, points); + ret.SetProperty(PathConstrainer::Property::POINTS, points); //controlPoints property - if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "controlPoints") ) + if(OptionalChild pointsProperty = IsChild(*pathConstrainer, "controlPoints")) { Dali::Property::Value points(Property::ARRAY); - if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) ) + if(DeterminePropertyFromNode(*pointsProperty, Property::ARRAY, points)) { - ret.SetProperty( PathConstrainer::Property::CONTROL_POINTS, points); + ret.SetProperty(PathConstrainer::Property::CONTROL_POINTS, points); } //Forward vector - OptionalVector3 forward( IsVector3( IsChild(*pathConstrainer, "forward" ) ) ); - if( forward ) + OptionalVector3 forward(IsVector3(IsChild(*pathConstrainer, "forward"))); + if(forward) { - ret.SetProperty( PathConstrainer::Property::FORWARD, *forward); + ret.SetProperty(PathConstrainer::Property::FORWARD, *forward); } //Add the new constrainer to the vector of PathConstrainer - PathConstrainerEntry entry = {name,ret}; - mPathConstrainerLut.push_back( entry ); + PathConstrainerEntry entry = {name, ret}; + mPathConstrainerLut.push_back(entry); } else { //Control points not specified - DALI_SCRIPT_WARNING("Control points not specified for pathConstrainer '%s'\n", name.c_str() ); + DALI_SCRIPT_WARNING("Control points not specified for pathConstrainer '%s'\n", name.c_str()); } } } else { //Interpolation points not specified - DALI_SCRIPT_WARNING("Interpolation points not specified for pathConstrainer '%s'\n", name.c_str() ); + DALI_SCRIPT_WARNING("Interpolation points not specified for pathConstrainer '%s'\n", name.c_str()); } } else { - DALI_SCRIPT_WARNING("Constrainer '%s' is not a PathConstrainer\n", name.c_str() ); + DALI_SCRIPT_WARNING("Constrainer '%s' is not a PathConstrainer\n", name.c_str()); } } } @@ -741,20 +599,20 @@ PathConstrainer Builder::GetPathConstrainer( const std::string& name ) return ret; } -bool Builder::IsPathConstrainer( const std::string& name ) +bool Builder::IsPathConstrainer(const std::string& name) { - size_t count( mPathConstrainerLut.size() ); - for( size_t i(0); i!=count; ++i ) + size_t count(mPathConstrainerLut.size()); + for(size_t i(0); i != count; ++i) { - if( mPathConstrainerLut[i].name == name ) + if(mPathConstrainerLut[i].name == name) { return true; } } - if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") ) + if(OptionalChild constrainers = IsChild(*mParser.GetRoot(), "constrainers")) { - if( OptionalChild constrainer = IsChild( *constrainers, name ) ) + if(OptionalChild constrainer = IsChild(*constrainers, name)) { OptionalString constrainerType(IsString(IsChild(*constrainer, "type"))); if(!constrainerType) @@ -763,22 +621,22 @@ bool Builder::IsPathConstrainer( const std::string& name ) } else { - return *constrainerType == "PathConstrainer"; + return *constrainerType == "PathConstrainer"; } } } return false; } -Dali::LinearConstrainer Builder::GetLinearConstrainer( const std::string& name ) +Dali::LinearConstrainer Builder::GetLinearConstrainer(const std::string& name) { DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded"); //Search the LinearConstrainer in the LUT - size_t count( mLinearConstrainerLut.size() ); - for( size_t i(0); i!=count; ++i ) + size_t count(mLinearConstrainerLut.size()); + for(size_t i(0); i != count; ++i) { - if( mLinearConstrainerLut[i].name == name ) + if(mLinearConstrainerLut[i].name == name) { //LinearConstrainer has already been created return mLinearConstrainerLut[i].linearConstrainer; @@ -787,49 +645,49 @@ Dali::LinearConstrainer Builder::GetLinearConstrainer( const std::string& name ) //Create a new LinearConstrainer LinearConstrainer ret; - if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") ) + if(OptionalChild constrainers = IsChild(*mParser.GetRoot(), "constrainers")) { - if( OptionalChild linearConstrainer = IsChild( *constrainers, name ) ) + if(OptionalChild linearConstrainer = IsChild(*constrainers, name)) { OptionalString constrainerType(IsString(IsChild(*linearConstrainer, "type"))); if(!constrainerType) { - DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str() ); + DALI_SCRIPT_WARNING("Constrainer type not specified for constrainer '%s'\n", name.c_str()); } - else if( *constrainerType == "LinearConstrainer") + else if(*constrainerType == "LinearConstrainer") { //points property - if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "value") ) + if(OptionalChild pointsProperty = IsChild(*linearConstrainer, "value")) { Dali::Property::Value points(Property::ARRAY); - if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) ) + if(DeterminePropertyFromNode(*pointsProperty, Property::ARRAY, points)) { ret = Dali::LinearConstrainer::New(); - ret.SetProperty( LinearConstrainer::Property::VALUE, points); + ret.SetProperty(LinearConstrainer::Property::VALUE, points); //controlPoints property - if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "progress") ) + if(OptionalChild pointsProperty = IsChild(*linearConstrainer, "progress")) { Dali::Property::Value points(Property::ARRAY); - if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) ) + if(DeterminePropertyFromNode(*pointsProperty, Property::ARRAY, points)) { - ret.SetProperty( LinearConstrainer::Property::PROGRESS, points); + ret.SetProperty(LinearConstrainer::Property::PROGRESS, points); } } //Add the new constrainer to vector of LinearConstrainer - LinearConstrainerEntry entry = {name,ret}; - mLinearConstrainerLut.push_back( entry ); + LinearConstrainerEntry entry = {name, ret}; + mLinearConstrainerLut.push_back(entry); } } else { //Interpolation points not specified - DALI_SCRIPT_WARNING("Values not specified for LinearConstrainer '%s'\n", name.c_str() ); + DALI_SCRIPT_WARNING("Values not specified for LinearConstrainer '%s'\n", name.c_str()); } } else { - DALI_SCRIPT_WARNING("Constrainer '%s' is not a LinearConstrainer\n", name.c_str() ); + DALI_SCRIPT_WARNING("Constrainer '%s' is not a LinearConstrainer\n", name.c_str()); } } } @@ -837,21 +695,21 @@ Dali::LinearConstrainer Builder::GetLinearConstrainer( const std::string& name ) return ret; } -bool Builder::IsLinearConstrainer( const std::string& name ) +bool Builder::IsLinearConstrainer(const std::string& name) { // Search the LinearConstrainer in the LUT - size_t count( mLinearConstrainerLut.size() ); - for( size_t i(0); i!=count; ++i ) + size_t count(mLinearConstrainerLut.size()); + for(size_t i(0); i != count; ++i) { - if( mLinearConstrainerLut[i].name == name ) + if(mLinearConstrainerLut[i].name == name) { return true; } } - if( OptionalChild constrainers = IsChild( *mParser.GetRoot(), "constrainers") ) + if(OptionalChild constrainers = IsChild(*mParser.GetRoot(), "constrainers")) { - if( OptionalChild constrainer = IsChild( *constrainers, name ) ) + if(OptionalChild constrainer = IsChild(*constrainers, name)) { OptionalString constrainerType(IsString(IsChild(*constrainer, "type"))); if(!constrainerType) @@ -860,7 +718,7 @@ bool Builder::IsLinearConstrainer( const std::string& name ) } else { - return *constrainerType == "LinearConstrainer"; + return *constrainerType == "LinearConstrainer"; } } } @@ -877,477 +735,895 @@ void Builder::EmitQuitSignal() mQuitSignal.Emit(); } -void Builder::AddActors( Actor toActor ) +Builder::~Builder() { - // 'stage' is the default/by convention section to add from - AddActors( "stage", toActor ); } -void Builder::AddActors( const std::string §ionName, Actor toActor ) +void Builder::LoadConfiguration(const TreeNode& root, Property::Map& intoMap) { - DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded"); - - Property::Map overrideMap; - Replacement replacements(overrideMap, mReplacementMap); - - OptionalChild add = IsChild(*mParser.GetRoot(), sectionName); + Replacement replacer(intoMap); - if( add ) + if(OptionalChild constants = IsChild(root, "config")) { - for( TreeNode::ConstIterator iter = (*add).CBegin(); iter != (*add).CEnd(); ++iter ) + for(TreeNode::ConstIterator iter = (*constants).CBegin(); + iter != (*constants).CEnd(); + ++iter) { - // empty actor adds directly to the stage - BaseHandle baseHandle = DoCreate( *mParser.GetRoot(), (*iter).second, Actor(), replacements ); - Actor actor = Actor::DownCast(baseHandle); - if(actor) + Dali::Property::Value property; + if((*iter).second.GetName()) { - toActor.Add( actor ); + DeterminePropertyFromNode((*iter).second, property, replacer); + + // If config is string, find constant and replace it to original value. + if((*iter).second.GetType() == TreeNode::STRING) + { + std::string stringConfigValue; + if(property.Get(stringConfigValue)) + { + std::size_t pos = 0; + + while(pos < stringConfigValue.size()) + { + // If we can't find "{","}" pair in stringConfigValue, will out loop. + std::size_t leftPos = stringConfigValue.find("{", pos); + if(leftPos != std::string::npos) + { + std::size_t rightPos = stringConfigValue.find("}", pos + 1); + + if(rightPos != std::string::npos) + { + // If we find "{","}" pair but can't find matched constant + // try to find other "{","}" pair after current left position. + pos = leftPos + 1; + + for(uint32_t i = 0; i < mReplacementMap.Count(); i++) + { + Property::Key constant = mReplacementMap.GetKeyAt(i); + + // Compare string which is between "{" and "}" with constant string + // If they are same, change string in stringConfigValue to mapped constant value. + if(0 == stringConfigValue.compare(leftPos + 1, rightPos - leftPos - 1, constant.stringKey)) + { + std::string replaceString; + mReplacementMap.GetValue(i).Get(replaceString); + + stringConfigValue.replace(leftPos, rightPos - leftPos + 1, replaceString); + pos = leftPos + replaceString.size(); + break; + } + } + } + else + { + // If we cannot find constant in const value, will out loop. + pos = stringConfigValue.size(); + } + } + else + { + // If we cannot find constant in const value, will out loop. + pos = stringConfigValue.size(); + } + } + property = Property::Value(stringConfigValue); + } + } + intoMap[(*iter).second.GetName()] = property; } } + } +} - // if were adding the 'stage' section then also check for a render task called stage - // to add automatically - if( "stage" == sectionName ) +void Builder::LoadConstants(const TreeNode& root, Property::Map& intoMap) +{ + Replacement replacer(intoMap); + + if(OptionalChild constants = IsChild(root, "constants")) + { + for(TreeNode::ConstIterator iter = (*constants).CBegin(); + iter != (*constants).CEnd(); + ++iter) { - if( OptionalChild renderTasks = IsChild(*mParser.GetRoot(), "renderTasks") ) + Dali::Property::Value property; + if((*iter).second.GetName()) { - if( OptionalChild tasks = IsChild(*renderTasks, "stage") ) - { - CreateRenderTask( "stage" ); - } +#if defined(DEBUG_ENABLED) + DALI_SCRIPT_VERBOSE("Constant set from json '%s'\n", (*iter).second.GetName()); +#endif + DeterminePropertyFromNode((*iter).second, property, replacer); + intoMap[(*iter).second.GetName()] = property; } } } + +#if defined(DEBUG_ENABLED) + Property::Value* iter = intoMap.Find("CONFIG_SCRIPT_LOG_LEVEL"); + if(iter && iter->GetType() == Property::STRING) + { + std::string logLevel(iter->Get()); + if(logLevel == "NoLogging") + { + gFilterScript->SetLogLevel(Integration::Log::NoLogging); + } + else if(logLevel == "Concise") + { + gFilterScript->SetLogLevel(Integration::Log::Concise); + } + else if(logLevel == "General") + { + gFilterScript->SetLogLevel(Integration::Log::General); + } + else if(logLevel == "Verbose") + { + gFilterScript->SetLogLevel(Integration::Log::Verbose); + } + } +#endif } -Animation Builder::CreateAnimation( const std::string& animationName, const Replacement& replacement, Dali::Actor sourceActor ) +Animation Builder::CreateAnimation(const std::string& animationName, const Replacement& replacement, Dali::Actor sourceActor) { DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded"); Animation anim; - if( OptionalChild animations = IsChild(*mParser.GetRoot(), "animations") ) + if(OptionalChild animations = IsChild(*mParser.GetRoot(), "animations")) { - if( OptionalChild animation = IsChild(*animations, animationName) ) + if(OptionalChild animation = IsChild(*animations, animationName)) { - anim = Dali::Toolkit::Internal::CreateAnimation( *animation, replacement, sourceActor, this ); + anim = Dali::Toolkit::Internal::CreateAnimation(*animation, replacement, sourceActor, this); } else { - DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed\n", animationName.c_str() ); + DALI_SCRIPT_WARNING("Request for Animation called '%s' failed\n", animationName.c_str()); } } else { - DALI_SCRIPT_WARNING( "Request for Animation called '%s' failed (no animation section)\n", animationName.c_str() ); + DALI_SCRIPT_WARNING("Request for Animation called '%s' failed (no animation section)\n", animationName.c_str()); } return anim; } -Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor ) +BaseHandle Builder::Create(const std::string& templateName, const Replacement& constant) { - Replacement replacement(map, mReplacementMap); - return CreateAnimation( animationName, replacement, sourceActor); -} + DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded"); -Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map ) -{ - Replacement replacement(map, mReplacementMap); - return CreateAnimation( animationName, replacement, Stage::GetCurrent().GetRootLayer() ); -} + BaseHandle baseHandle; -Animation Builder::CreateAnimation( const std::string& animationName, Dali::Actor sourceActor ) -{ - Replacement replacement( mReplacementMap ); + OptionalChild templates = IsChild(*mParser.GetRoot(), KEYNAME_TEMPLATES); - return CreateAnimation( animationName, replacement, sourceActor ); -} + if(!templates) + { + DALI_SCRIPT_WARNING("No template section found to CreateFromTemplate\n"); + } + else + { + OptionalChild childTemplate = IsChild(*templates, templateName); + if(!childTemplate) + { + DALI_SCRIPT_WARNING("Template '%s' does not exist in template section\n", templateName.c_str()); + } + else + { + OptionalString type = constant.IsString(IsChild(*childTemplate, KEYNAME_TYPE)); -Animation Builder::CreateAnimation( const std::string& animationName ) -{ - Replacement replacement( mReplacementMap ); + if(!type) + { + DALI_SCRIPT_WARNING("Cannot create template '%s' as template section is missing 'type'\n", templateName.c_str()); + } + else + { + baseHandle = DoCreate(*mParser.GetRoot(), *childTemplate, Actor(), constant); + } + } + } - return CreateAnimation( animationName, replacement, Dali::Stage::GetCurrent().GetRootLayer() ); + return baseHandle; } -bool Builder::ConvertChildValue( const TreeNode& mappingRoot, KeyStack& keyStack, Property::Value& child ) +/* + * Create a dali type from a node. + * If parent given and an actor type was created then add it to the parent and + * recursively add nodes children. + */ +BaseHandle Builder::DoCreate(const TreeNode& root, const TreeNode& node, Actor parent, const Replacement& replacements) { - bool result = false; + BaseHandle baseHandle; + TypeInfo typeInfo; + const TreeNode* templateNode = NULL; - switch( child.GetType() ) + if(OptionalString typeName = IsString(node, KEYNAME_TYPE)) { - case Property::STRING: + typeInfo = TypeRegistry::Get().GetTypeInfo(*typeName); + + if(!typeInfo) { - std::string value; - if( child.Get( value ) ) + // a template name is also allowed inplace of the type name + OptionalChild templates = IsChild(root, KEYNAME_TEMPLATES); + + if(templates) { - std::string key; - if( GetMappingKey( value, key ) ) + if(OptionalChild isTemplate = IsChild(*templates, *typeName)) { - // Check key for cycles: - result=true; - for( KeyStack::iterator iter = keyStack.begin() ; iter != keyStack.end(); ++iter ) - { - if( key.compare(*iter) == 0 ) - { - // key is already in stack; stop. - DALI_LOG_WARNING("Detected cycle in stylesheet mapping table:%s\n", key.c_str()); - child = Property::Value(""); - result=false; - break; - } - } + templateNode = &(*isTemplate); - if( result ) + if(OptionalString templateTypeName = IsString(*templateNode, KEYNAME_TYPE)) { - // The following call will overwrite the child with the value - // from the mapping. - RecursePropertyMap( mappingRoot, keyStack, key.c_str(), Property::NONE, child ); - result = true; + typeInfo = TypeRegistry::Get().GetTypeInfo(*templateTypeName); } } } - break; } + } - case Property::MAP: + if(!typeInfo) + { + DALI_SCRIPT_WARNING("Cannot create Dali type from node '%s'\n", node.GetName()); + } + else + { + baseHandle = typeInfo.CreateInstance(); + Handle handle = Handle::DownCast(baseHandle); + Actor actor = Actor::DownCast(handle); + + if(handle) { - Property::Map* map = child.GetMap(); - if( map ) + DALI_SCRIPT_VERBOSE("Create:%s\n", typeInfo.GetName().c_str()); + +#if defined(DEBUG_ENABLED) + if(handle) { - for( Property::Map::SizeType i=0; i < map->Count(); ++i ) - { - Property::Value& child = map->GetValue(i); - ConvertChildValue(mappingRoot, keyStack, child); - } + DALI_SCRIPT_VERBOSE(" Is Handle Object=%d\n", (long*)handle.GetObjectPtr()); + DALI_SCRIPT_VERBOSE(" Is Handle Property Count=%d\n", handle.GetPropertyCount()); } - break; - } - case Property::ARRAY: - { - Property::Array* array = child.GetArray(); - if( array ) + if(actor) { - for( Property::Array::SizeType i=0; i < array->Count(); ++i ) - { - Property::Value& child = array->GetElementAt(i); - ConvertChildValue(mappingRoot, keyStack, child); - } + DALI_SCRIPT_VERBOSE(" Is Actor id=%d\n", actor.GetProperty(Actor::Property::ID)); } - break; - } - default: - // Ignore other types. - break; - } + Toolkit::Control control = Toolkit::Control::DownCast(handle); + if(control) + { + DALI_SCRIPT_VERBOSE(" Is Control id=%d\n", actor.GetProperty(Actor::Property::ID)); + } +#endif // DEBUG_ENABLED - return result; -} + if(templateNode) + { + ApplyProperties(root, *templateNode, handle, replacements); -bool Builder::RecursePropertyMap( const TreeNode& mappingRoot, KeyStack& keyStack, const char* theKey, Property::Type propertyType, Property::Value& value ) -{ - Replacement replacer( mReplacementMap ); - bool result = false; + if(OptionalChild actors = IsChild(*templateNode, KEYNAME_ACTORS)) + { + for(TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter) + { + DoCreate(root, (*iter).second, actor, replacements); + } + } + } - keyStack.push_back( theKey ); - - for( TreeNode::ConstIterator iter = mappingRoot.CBegin(); iter != mappingRoot.CEnd(); ++iter ) - { - std::string aKey( (*iter).first ); - if( aKey.compare( theKey ) == 0 ) - { - if( propertyType == Property::NONE ) + if(actor) { - DeterminePropertyFromNode( (*iter).second, value, replacer ); - result = true; + // add children of all the styles + if(OptionalChild actors = IsChild(node, KEYNAME_ACTORS)) + { + for(TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter) + { + DoCreate(root, (*iter).second, actor, replacements); + } + } + + // apply style on top as they need the children to exist + ApplyAllStyleProperties(root, node, actor, replacements); + + // then add to parent + if(parent) + { + parent.Add(actor); + } } else { - result = DeterminePropertyFromNode( (*iter).second, propertyType, value, replacer ); + ApplyProperties(root, node, handle, replacements); } - - if( result ) - { - ConvertChildValue(mappingRoot, keyStack, value); - } - break; + } + else + { + DALI_SCRIPT_WARNING("Cannot create handle from type '%s'\n", typeInfo.GetName().c_str()); } } - keyStack.pop_back(); - - return result; -} - -bool Builder::GetPropertyMap( const TreeNode& mappingRoot, const char* theKey, Property::Type propertyType, Property::Value& value ) -{ - KeyStack keyStack; - return RecursePropertyMap( mappingRoot, keyStack, theKey, propertyType, value ); + return baseHandle; } - -void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::UIFormat format ) +void Builder::SetupTask(RenderTask& task, const TreeNode& node, const Replacement& constant) { - // parser to get constants and includes only - Dali::Toolkit::JsonParser parser = Dali::Toolkit::JsonParser::New(); + const Stage& stage = Stage::GetCurrent(); + Layer root = stage.GetRootLayer(); - if( !parser.Parse( data ) ) + if(OptionalString s = constant.IsString(IsChild(node, "sourceActor"))) { - DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n", - parser.GetErrorLineNumber(), - parser.GetErrorColumn(), - parser.GetErrorDescription().c_str() ); - - DALI_ASSERT_ALWAYS(!"Cannot parse JSON"); + Actor actor = root.FindChildByName(*s); + if(actor) + { + task.SetSourceActor(actor); + } + else + { + DALI_SCRIPT_WARNING("Cannot find source actor on stage for render task called '%s'\n", (*s).c_str()); + } } - else - { - // load constant map (allows the user to override the constants in the json after loading) - LoadConstants( *parser.GetRoot(), mReplacementMap ); - // merge includes - if( OptionalChild includes = IsChild(*parser.GetRoot(), KEYNAME_INCLUDES) ) + if(OptionalString s = constant.IsString(IsChild(node, "cameraActor"))) + { + CameraActor actor = CameraActor::DownCast(root.FindChildByName(*s)); + if(actor) { - Replacement replacer( mReplacementMap ); - - for(TreeNode::ConstIterator iter = (*includes).CBegin(); iter != (*includes).CEnd(); ++iter) - { - OptionalString filename = replacer.IsString( (*iter).second ); - - if( filename ) - { -#if defined(DEBUG_ENABLED) - DALI_SCRIPT_VERBOSE("Loading Include '%s'\n", (*filename).c_str()); -#endif - LoadFromString( GetFileContents(*filename) ); - } - } + task.SetCameraActor(actor); } - - if( !mParser.Parse( data ) ) + else { - DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n", - mParser.GetErrorLineNumber(), - mParser.GetErrorColumn(), - mParser.GetErrorDescription().c_str() ); - - DALI_ASSERT_ALWAYS(!"Cannot parse JSON"); + DALI_SCRIPT_WARNING("Cannot find camera actor on stage for render task called '%s'\n", (*s).c_str()); } } - DUMP_PARSE_TREE(parser); // This macro only writes out if DEBUG is enabled and the "DUMP_TREE" constant is defined in the stylesheet. - DUMP_TEST_MAPPINGS(parser); + if(OptionalString s = constant.IsString(IsChild(node, "screenToFrameBufferFunction"))) + { + if("DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION" == *s) + { + task.SetScreenToFrameBufferFunction(RenderTask::DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION); + } + else if("FULLSCREEN_FRAMEBUFFER_FUNCTION" == *s) + { + task.SetScreenToFrameBufferFunction(RenderTask::FULLSCREEN_FRAMEBUFFER_FUNCTION); + } + else + { + DALI_SCRIPT_WARNING("todo"); + } + } - DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Cannot parse JSON"); + // other setup is via the property system + SetProperties(node, task, constant); } -void Builder::AddConstants( const Property::Map& map ) +bool Builder::ApplyStyle(const std::string& styleName, Handle& handle, const Replacement& replacement) { - mReplacementMap.Merge( map ); -} + DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded"); -void Builder::AddConstant( const std::string& key, const Property::Value& value ) -{ - mReplacementMap[key] = value; -} + OptionalChild styles = IsChild(*mParser.GetRoot(), KEYNAME_STYLES); -const Property::Map& Builder::GetConstants() const -{ - return mReplacementMap; -} + std::string styleNameLower(styleName); + OptionalChild style = IsChildIgnoreCase(*styles, styleNameLower); -const Property::Value& Builder::GetConstant( const std::string& key ) const -{ - Property::Value* match = mReplacementMap.Find( key ); - if( match ) + if(styles && style) { - return (*match); + ApplyAllStyleProperties(*mParser.GetRoot(), *style, handle, replacement); + return true; } else { - static Property::Value invalid; - return invalid; + return false; } } -void Builder::LoadConstants( const TreeNode& root, Property::Map& intoMap ) +void Builder::ApplyAllStyleProperties(const TreeNode& root, const TreeNode& node, Dali::Handle& handle, const Replacement& constant) { - Replacement replacer(intoMap); + const char* styleName = node.GetName(); + + StylePtr style = Style::New(); - if( OptionalChild constants = IsChild(root, "constants") ) + StylePtr* matchedStyle = NULL; + if(styleName) { - for(TreeNode::ConstIterator iter = (*constants).CBegin(); - iter != (*constants).CEnd(); ++iter) + matchedStyle = mStyles.Find(styleName); + if(!matchedStyle) { - Dali::Property::Value property; - if( (*iter).second.GetName() ) + OptionalChild styleNodes = IsChild(root, KEYNAME_STYLES); + OptionalChild inheritFromNode = IsChild(node, KEYNAME_INHERIT); + if(!inheritFromNode) { + inheritFromNode = IsChild(node, KEYNAME_STYLES); + } + + if(styleNodes) + { + if(inheritFromNode) + { + TreeNodeList additionalStyleNodes; + + CollectAllStyles(*styleNodes, *inheritFromNode, additionalStyleNodes); + #if defined(DEBUG_ENABLED) - DALI_SCRIPT_VERBOSE("Constant set from json '%s'\n", (*iter).second.GetName()); + for(TreeNode::ConstIterator iter = (*inheritFromNode).CBegin(); iter != (*inheritFromNode).CEnd(); ++iter) + { + if(OptionalString styleName = IsString((*iter).second)) + { + DALI_SCRIPT_VERBOSE("Style Applied '%s'\n", (*styleName).c_str()); + } + } #endif - DeterminePropertyFromNode( (*iter).second, property, replacer ); - intoMap[ (*iter).second.GetName() ] = property; + + // a style may have other styles, which has other styles etc so we apply in reverse by convention. + for(TreeNodeList::reverse_iterator iter = additionalStyleNodes.rbegin(); iter != additionalStyleNodes.rend(); ++iter) + { + RecordStyle(style, *(*iter), handle, constant); + ApplySignals(root, *(*iter), handle); + ApplyStylesByActor(root, *(*iter), handle, constant); + } + } + + RecordStyle(style, node, handle, constant); + mStyles.Add(styleName, style); // shallow copy + matchedStyle = &style; } } } -#if defined(DEBUG_ENABLED) - Property::Value* iter = intoMap.Find( "CONFIG_SCRIPT_LOG_LEVEL" ); - if( iter && iter->GetType() == Property::STRING ) + if(matchedStyle) + { + StylePtr style(*matchedStyle); + Dictionary instancedProperties; + style->ApplyVisualsAndPropertiesRecursively(handle, instancedProperties); + } + else // If there were no styles, instead set properties + { + SetProperties(node, handle, constant); + } + ApplySignals(root, node, handle); + ApplyStylesByActor(root, node, handle, constant); +} + +void Builder::RecordStyle(StylePtr style, + const TreeNode& node, + Dali::Handle& handle, + const Replacement& replacements) +{ + // With repeated calls, accumulate inherited states, visuals and properties + // but override any with same name + + for(TreeNode::ConstIterator iter = node.CBegin(); iter != node.CEnd(); ++iter) { - std::string logLevel( iter->Get< std::string >() ); - if( logLevel == "NoLogging" ) + const TreeNode::KeyNodePair& keyValue = *iter; + std::string key(keyValue.first); + if(key == KEYNAME_STATES) { - gFilterScript->SetLogLevel( Integration::Log::NoLogging ); + const TreeNode& states = keyValue.second; + if(states.GetType() != TreeNode::OBJECT) + { + DALI_LOG_WARNING("RecordStyle() Node \"%s\" is not a JSON object\n", key.c_str()); + continue; + } + + for(TreeNode::ConstIterator iter = states.CBegin(); iter != states.CEnd(); ++iter) + { + const TreeNode& stateNode = (*iter).second; + const char* stateName = stateNode.GetName(); + if(stateNode.GetType() != TreeNode::OBJECT) + { + DALI_LOG_WARNING("RecordStyle() Node \"%s\" is not a JSON object\n", stateName); + continue; + } + + StylePtr* stylePtr = style->subStates.Find(stateName); + if(stylePtr) + { + StylePtr style(*stylePtr); + RecordStyle(style, stateNode, handle, replacements); + } + else + { + StylePtr subState = Style::New(); + RecordStyle(subState, stateNode, handle, replacements); + style->subStates.Add(stateName, subState); + } + } + } + else if(key == KEYNAME_VISUALS) + { + for(TreeNode::ConstIterator iter = keyValue.second.CBegin(); iter != keyValue.second.CEnd(); ++iter) + { + // Each key in this table should be a property name matching a visual. + const TreeNode::KeyNodePair& visual = *iter; + Dali::Property::Value property(Property::MAP); + if(DeterminePropertyFromNode(visual.second, Property::MAP, property, replacements)) + { + Property::Map* mapPtr = style->visuals.Find(visual.first); + if(mapPtr) + { + // Override existing visuals + mapPtr->Clear(); + mapPtr->Merge(*property.GetMap()); + } + else + { + Property::Map* map = property.GetMap(); + if(map) + { + style->visuals.Add(visual.first, *map); + } + } + } + } } - else if( logLevel == "Concise" ) + else if(key == KEYNAME_ENTRY_TRANSITION) { - gFilterScript->SetLogLevel( Integration::Log::Concise ); + RecordTransitionData(keyValue, style->entryTransition, replacements); } - else if( logLevel == "General" ) + else if(key == KEYNAME_EXIT_TRANSITION) { - gFilterScript->SetLogLevel( Integration::Log::General ); + RecordTransitionData(keyValue, style->exitTransition, replacements); } - else if( logLevel == "Verbose" ) + else if(key == KEYNAME_TRANSITIONS) { - gFilterScript->SetLogLevel( Integration::Log::Verbose ); + RecordTransitions(keyValue, style->transitions, replacements); + } + else if(key == KEYNAME_TYPE || + key == KEYNAME_ACTORS || + key == KEYNAME_SIGNALS || + key == KEYNAME_STYLES || + key == KEYNAME_MAPPINGS || + key == KEYNAME_INHERIT) + { + continue; + } + else // It's a property + { + Property::Index index; + Property::Value value; + if(MapToTargetProperty(handle, key, keyValue.second, replacements, index, value)) + { + Property::Value* existingValuePtr = style->properties.Find(index); + if(existingValuePtr != NULL) + { + *existingValuePtr = value; // Overwrite existing property. + } + else + { + style->properties.Add(index, value); + } + } } } -#endif - } -bool Builder::ApplyStyle( const std::string& styleName, Handle& handle ) +void Builder::RecordTransitions( + const TreeNode::KeyNodePair& keyValue, + Property::Array& value, + const Replacement& replacements) { - Replacement replacer( mReplacementMap ); - return ApplyStyle( styleName, handle, replacer ); + //@todo add new transitions to style.transitions + // override existing transitions. A transition matches on target & property name + const TreeNode& node = keyValue.second; + if(node.GetType() == TreeNode::ARRAY) + { + Dali::Property::Value property(Property::ARRAY); + if(DeterminePropertyFromNode(node, Property::ARRAY, property, replacements)) + { + value = *property.GetArray(); + } + } + else if(node.GetType() == TreeNode::OBJECT) + { + Dali::Property::Value property(Property::MAP); + if(DeterminePropertyFromNode(node, Property::MAP, property, replacements)) + { + Property::Array propertyArray; + propertyArray.Add(property); + value = propertyArray; + } + } + else + { + DALI_LOG_WARNING("RecordStyle() Node \"%s\" is not a JSON array or object\n", keyValue.first); + } } -bool Builder::ApplyStyle( const std::string& styleName, Handle& handle, const Replacement& replacement ) +void Builder::RecordTransitionData( + const TreeNode::KeyNodePair& keyValue, + Toolkit::TransitionData& transitionData, + const Replacement& replacements) { - DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded"); - - OptionalChild styles = IsChild( *mParser.GetRoot(), KEYNAME_STYLES ); - OptionalChild style = IsChild( *styles, styleName ); - - if( styles && style ) + const TreeNode& node = keyValue.second; + if(node.GetType() == TreeNode::ARRAY) { - ApplyAllStyleProperties( *mParser.GetRoot(), *style, handle, replacement ); - return true; + Dali::Property::Value property(Property::ARRAY); + if(DeterminePropertyFromNode(keyValue.second, Property::ARRAY, property, replacements)) + { + transitionData = Toolkit::TransitionData::New(*property.GetArray()); + } } - else + else if(node.GetType() == TreeNode::OBJECT) { - return false; + Dali::Property::Value property(Property::MAP); + if(DeterminePropertyFromNode(keyValue.second, Property::MAP, property, replacements)) + { + transitionData = Toolkit::TransitionData::New(*property.GetMap()); + } } } -BaseHandle Builder::Create( const std::string& templateName, const Property::Map& map ) +// Set properties from node on handle. +void Builder::ApplyProperties(const TreeNode& root, const TreeNode& node, Dali::Handle& handle, const Replacement& constant) { - Replacement replacement( map, mReplacementMap ); - return Create( templateName, replacement ); + SetProperties(node, handle, constant); + ApplySignals(root, node, handle); } -BaseHandle Builder::Create( const std::string& templateName, const Replacement& constant ) +void Builder::ApplySignals(const TreeNode& root, const TreeNode& node, Dali::Handle& handle) { - DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded"); - - BaseHandle baseHandle; - - OptionalChild templates = IsChild(*mParser.GetRoot(), KEYNAME_TEMPLATES); - - if( !templates ) + Actor actor = Actor::DownCast(handle); + if(actor) { - DALI_SCRIPT_WARNING("No template section found to CreateFromTemplate\n"); + // add signals + SetupSignalAction(mSlotDelegate.GetConnectionTracker(), root, node, actor, this); + SetupPropertyNotification(mSlotDelegate.GetConnectionTracker(), root, node, actor, this); } - else +} + +// Appling by style helper +// use FindChildByName() to apply properties referenced in KEYNAME_ACTORS in the node +void Builder::ApplyStylesByActor(const TreeNode& root, const TreeNode& node, Dali::Handle& handle, const Replacement& constant) +{ + if(Dali::Actor actor = Dali::Actor::DownCast(handle)) { - OptionalChild childTemplate = IsChild(*templates, templateName); - if(!childTemplate) + if(const TreeNode* actors = node.GetChild(KEYNAME_ACTORS)) { - DALI_SCRIPT_WARNING("Template '%s' does not exist in template section\n", templateName.c_str()); + // in a style the actor subtree properties referenced by actor name + for(TreeConstIter iter = actors->CBegin(); iter != actors->CEnd(); ++iter) + { + Dali::Actor foundActor; + + if((*iter).first) + { + foundActor = actor.FindChildByName((*iter).first); + } + + if(!foundActor) + { + DALI_SCRIPT_VERBOSE("Cannot find actor in style application '%s'\n", (*iter).first); + } + else + { + DALI_SCRIPT_VERBOSE("Styles applied to actor '%s'\n", (*iter).first); + ApplyProperties(root, (*iter).second, foundActor, constant); + } + } } - else + } +} + +/* + * Sets the handle properties found in the tree node + */ +void Builder::SetProperties(const TreeNode& node, Handle& handle, const Replacement& constant) +{ + if(handle) + { + for(TreeNode::ConstIterator iter = node.CBegin(); iter != node.CEnd(); ++iter) { - OptionalString type = constant.IsString( IsChild(*childTemplate, KEYNAME_TYPE) ); + const TreeNode::KeyNodePair& keyChild = *iter; - if(!type) + std::string key(keyChild.first); + + // ignore special fields; + if(key == KEYNAME_TYPE || + key == KEYNAME_ACTORS || + key == KEYNAME_SIGNALS || + key == KEYNAME_STYLES || + key == KEYNAME_MAPPINGS || + key == KEYNAME_INHERIT || + key == KEYNAME_STATES || + key == KEYNAME_VISUALS || + key == KEYNAME_ENTRY_TRANSITION || + key == KEYNAME_EXIT_TRANSITION || + key == KEYNAME_TRANSITIONS) { - DALI_SCRIPT_WARNING("Cannot create template '%s' as template section is missing 'type'\n", templateName.c_str()); + continue; } - else + + Property::Index index; + Property::Value value; + + bool mapped = MapToTargetProperty(handle, key, keyChild.second, constant, index, value); + if(mapped) { - baseHandle = DoCreate( *mParser.GetRoot(), *childTemplate, Actor(), constant ); + DALI_SCRIPT_VERBOSE("SetProperty '%s' Index=:%d Value Type=%d Value '%s'\n", key.c_str(), index, value.GetType(), PropertyValueToString(value).c_str()); + + handle.SetProperty(index, value); } - } - } - return baseHandle; + // Add custom properties + SetCustomProperties(node, handle, constant, PROPERTIES, Property::READ_WRITE); + SetCustomProperties(node, handle, constant, ANIMATABLE_PROPERTIES, Property::ANIMATABLE); + + } // for property nodes + } + else + { + DALI_SCRIPT_WARNING("Style applied to empty handle\n"); + } } -BaseHandle Builder::CreateFromJson( const std::string& json ) +bool Builder::MapToTargetProperty( + Handle& propertyObject, + const std::string& key, + const TreeNode& node, + const Replacement& constant, + Property::Index& index, + Property::Value& value) { - BaseHandle ret; + bool mapped = false; - // merge in new template, hoping no one else has one named '@temp@' - std::string newTemplate = - std::string("{\"templates\":{\"@temp@\":") + \ - json + \ - std::string("}}"); + index = propertyObject.GetPropertyIndex(key); + if(Property::INVALID_INDEX != index) + { + Property::Type type = propertyObject.GetPropertyType(index); - if( mParser.Parse(newTemplate) ) + // if node.value is a mapping, get the property value from the "mappings" table + if(node.GetType() == TreeNode::STRING) + { + std::string mappingKey; + if(GetMappingKey(node.GetString(), mappingKey)) + { + OptionalChild mappingRoot = IsChild(mParser.GetRoot(), KEYNAME_MAPPINGS); + mapped = GetPropertyMap(*mappingRoot, mappingKey.c_str(), type, value); + } + } + if(!mapped) + { + mapped = DeterminePropertyFromNode(node, type, value, constant); + if(!mapped) + { + // Just determine the property from the node and if it's valid, let the property object handle it + DeterminePropertyFromNode(node, value, constant); + mapped = (value.GetType() != Property::NONE); + } + } + } + else { - Replacement replacement( mReplacementMap ); - ret = Create( "@temp@", replacement ); + DALI_LOG_ERROR("Key '%s' not found.\n", key.c_str()); } + return mapped; +} - return ret; +bool Builder::GetPropertyMap(const TreeNode& mappingRoot, const char* theKey, Property::Type propertyType, Property::Value& value) +{ + KeyStack keyStack; + return RecursePropertyMap(mappingRoot, keyStack, theKey, propertyType, value); } -bool Builder::ApplyFromJson( Handle& handle, const std::string& json ) +bool Builder::RecursePropertyMap(const TreeNode& mappingRoot, KeyStack& keyStack, const char* theKey, Property::Type propertyType, Property::Value& value) { - bool ret = false; + Replacement replacer(mReplacementMap); + bool result = false; - // merge new style, hoping no one else has one named '@temp@' - std::string newStyle = - std::string("{\"styles\":{\"@temp@\":") + \ - json + \ - std::string("}}"); + keyStack.push_back(theKey); - if( mParser.Parse(newStyle) ) + for(TreeNode::ConstIterator iter = mappingRoot.CBegin(); iter != mappingRoot.CEnd(); ++iter) { - Replacement replacement( mReplacementMap ); - ret = ApplyStyle( "@temp@", handle, replacement ); + std::string aKey((*iter).first); + if(aKey.compare(theKey) == 0) + { + if(propertyType == Property::NONE) + { + DeterminePropertyFromNode((*iter).second, value, replacer); + result = true; + } + else + { + result = DeterminePropertyFromNode((*iter).second, propertyType, value, replacer); + } + + if(result) + { + ConvertChildValue(mappingRoot, keyStack, value); + } + break; + } } + keyStack.pop_back(); - return ret; + return result; } - -BaseHandle Builder::Create( const std::string& templateName ) +bool Builder::ConvertChildValue(const TreeNode& mappingRoot, KeyStack& keyStack, Property::Value& child) { - Replacement replacement( mReplacementMap ); - return Create( templateName, replacement ); -} + bool result = false; -Builder::Builder() -: mSlotDelegate( this ) -{ - mParser = Dali::Toolkit::JsonParser::New(); + switch(child.GetType()) + { + case Property::STRING: + { + std::string value; + if(child.Get(value)) + { + std::string key; + if(GetMappingKey(value, key)) + { + // Check key for cycles: + result = true; + for(KeyStack::iterator iter = keyStack.begin(); iter != keyStack.end(); ++iter) + { + if(key.compare(*iter) == 0) + { + // key is already in stack; stop. + DALI_LOG_WARNING("Detected cycle in stylesheet mapping table:%s\n", key.c_str()); + child = Property::Value(""); + result = false; + break; + } + } - Property::Map defaultDirs; - defaultDirs[ TOKEN_STRING(DALI_IMAGE_DIR) ] = DALI_IMAGE_DIR; - defaultDirs[ TOKEN_STRING(DALI_SOUND_DIR) ] = DALI_SOUND_DIR; - defaultDirs[ TOKEN_STRING(DALI_STYLE_DIR) ] = DALI_STYLE_DIR; - defaultDirs[ TOKEN_STRING(DALI_STYLE_IMAGE_DIR) ] = DALI_STYLE_IMAGE_DIR; + if(result) + { + // The following call will overwrite the child with the value + // from the mapping. + RecursePropertyMap(mappingRoot, keyStack, key.c_str(), Property::NONE, child); + result = true; + } + } + } + break; + } + + case Property::MAP: + { + Property::Map* map = child.GetMap(); + if(map) + { + for(Property::Map::SizeType i = 0; i < map->Count(); ++i) + { + Property::Value& child = map->GetValue(i); + ConvertChildValue(mappingRoot, keyStack, child); + } + } + break; + } + + case Property::ARRAY: + { + Property::Array* array = child.GetArray(); + if(array) + { + for(Property::Array::SizeType i = 0; i < array->Count(); ++i) + { + Property::Value& child = array->GetElementAt(i); + ConvertChildValue(mappingRoot, keyStack, child); + } + } + break; + } + + default: + // Ignore other types. + break; + } - AddConstants( defaultDirs ); + return result; } -Builder::~Builder() +void Builder::SetCustomProperties(const TreeNode& node, Handle& handle, const Replacement& constant, const std::string& childName, Property::AccessMode accessMode) { + // Add custom properties + if(OptionalChild customPropertiesChild = IsChild(node, childName)) + { + const TreeNode& customPropertiesNode = *customPropertiesChild; + const TreeConstIter endIter = customPropertiesNode.CEnd(); + for(TreeConstIter iter = customPropertiesNode.CBegin(); endIter != iter; ++iter) + { + const TreeNode::KeyNodePair& keyChild = *iter; + std::string key(keyChild.first); + Property::Value value; + DeterminePropertyFromNode(keyChild.second, value, constant); + + // Register/Set property. + handle.RegisterProperty(key, value, accessMode); + } + } } } // namespace Internal