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=88c51b6e845ba3ca74ce938104c41913fd44ac13;hp=de3fbe14e119bea994dbe15c677d935bc5d8dcde;hb=53eb583fbee4c4a305d6e04b36b16bf173790219;hpb=6cb03e371972cf2707a35ab860c73f1a87470747 diff --git a/dali-toolkit/internal/builder/builder-impl.cpp b/dali-toolkit/internal/builder/builder-impl.cpp index de3fbe1..88c51b6 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,29 +22,32 @@ #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 - namespace Dali { @@ -56,9 +59,12 @@ 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); + #if defined(DEBUG_ENABLED) Integration::Log::Filter* gFilterScript = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_SCRIPT"); #endif @@ -68,14 +74,20 @@ 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 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"; @@ -121,7 +133,12 @@ void CollectAllStyles( const TreeNode& stylesCollection, const TreeNode& style, { 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 ); } @@ -134,379 +151,270 @@ void CollectAllStyles( const TreeNode& stylesCollection, const TreeNode& style, } // 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; + Replacement replacement(map, mReplacementMap); + return CreateAnimation( animationName, replacement, sourceActor); +} - if( (*iter).first ) - { - foundActor = actor.FindChildByName( (*iter).first ); - } +Animation Builder::CreateAnimation( const std::string& animationName, const Property::Map& map ) +{ + Replacement replacement(map, mReplacementMap); + return CreateAnimation( animationName, replacement, Stage::GetCurrent().GetRootLayer() ); +} - 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 ); - } - } - } - } +Animation Builder::CreateAnimation( const std::string& animationName, Dali::Actor sourceActor ) +{ + Replacement replacement( mReplacementMap ); + + return CreateAnimation( animationName, replacement, sourceActor ); +} + +Animation Builder::CreateAnimation( const std::string& animationName ) +{ + Replacement replacement( mReplacementMap ); + + return CreateAnimation( animationName, replacement, Dali::Stage::GetCurrent().GetRootLayer() ); } +BaseHandle Builder::Create( const std::string& templateName ) +{ + Replacement replacement( mReplacementMap ); + return Create( templateName, replacement ); +} -void Builder::ApplyAllStyleProperties( const TreeNode& root, const TreeNode& node, - Dali::Handle& handle, const Replacement& constant ) +BaseHandle Builder::Create( const std::string& templateName, const Property::Map& map ) +{ + Replacement replacement( map, mReplacementMap ); + return Create( templateName, replacement ); +} + +BaseHandle Builder::CreateFromJson( const std::string& json ) { - OptionalChild styles = IsChild(root, KEYNAME_STYLES); - OptionalChild style = IsChild(node, KEYNAME_STYLES); + BaseHandle ret; - if( styles && style ) + // 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) ) { - TreeNodeList additionalStyles; + Replacement replacement( mReplacementMap ); + ret = Create( "@temp@", replacement ); + } - CollectAllStyles( *styles, *style, additionalStyles ); + return ret; +} -#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 +bool Builder::ApplyFromJson( Handle& handle, const std::string& json ) +{ + bool ret = false; - // 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 ); + // merge new style, hoping no one else has one named '@temp@' + std::string newStyle = + std::string("{\"styles\":{\"@temp@\":") + \ + json + \ + std::string("}}"); - ApplyStylesByActor( root, *(*iter), handle, constant ); - } + if( mParser.Parse(newStyle) ) + { + Replacement replacement( mReplacementMap ); + ret = ApplyStyle( "@temp@", handle, replacement ); } - // applying given node last - ApplyProperties( root, node, handle, constant ); - - ApplyStylesByActor( root, node, handle, constant ); + return ret; +} +bool Builder::ApplyStyle( const std::string& styleName, Handle& handle ) +{ + Replacement replacer( mReplacementMap ); + return ApplyStyle( styleName, handle, replacer ); } +bool Builder::LookupStyleName( const std::string& styleName ) +{ + DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded"); -/* - * 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 ) -{ - BaseHandle baseHandle; - TypeInfo typeInfo; - const TreeNode* templateNode = NULL; + OptionalChild styles = IsChild( *mParser.GetRoot(), KEYNAME_STYLES ); + OptionalChild style = IsChildIgnoreCase( *styles, styleName ); - if( OptionalString typeName = IsString(node, KEYNAME_TYPE) ) + if( styles && style ) { - typeInfo = TypeRegistry::Get().GetTypeInfo( *typeName ); - - if( !typeInfo ) - { - // a template name is also allowed inplace of the type name - OptionalChild templates = IsChild( root, KEYNAME_TEMPLATES); - - if( templates ) - { - if( OptionalChild isTemplate = IsChild( *templates, *typeName ) ) - { - templateNode = &(*isTemplate); - - if( OptionalString templateTypeName = IsString(*templateNode, KEYNAME_TYPE) ) - { - typeInfo = TypeRegistry::Get().GetTypeInfo( *templateTypeName ); - } - } - } - } + 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); - - if(handle) - { - - DALI_SCRIPT_VERBOSE("Create:%s\n", typeInfo.GetName().c_str()); - -#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()); - } - - if(actor) - { - DALI_SCRIPT_VERBOSE(" Is Actor id=%d\n", actor.GetId()); - } - - Toolkit::Control control = Toolkit::Control::DownCast(handle); - if(control) - { - DALI_SCRIPT_VERBOSE(" Is Control id=%d\n", actor.GetId()); - } -#endif // DEBUG_ENABLED - - if( templateNode ) - { - ApplyProperties( root, *templateNode, handle, replacements ); - - if( OptionalChild actors = IsChild( *templateNode, KEYNAME_ACTORS ) ) - { - for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter ) - { - DoCreate( root, (*iter).second, actor, replacements ); - } - } - } - - 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 *style; } +} - return baseHandle; +void Builder::AddActors( Actor toActor ) +{ + // 'stage' is the default/by convention section to add from + AddActors( "stage", toActor ); } -void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replacement& constant ) +void Builder::AddActors( const std::string §ionName, Actor toActor ) { - const Stage& stage = Stage::GetCurrent(); - Layer root = stage.GetRootLayer(); + DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded"); - 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() ); - } - } + Property::Map overrideMap; + Replacement replacements(overrideMap, mReplacementMap); - 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() ); - } - } + OptionalChild add = IsChild(*mParser.GetRoot(), sectionName); - if( OptionalString s = constant.IsString( IsChild(node, "targetFrameBuffer") ) ) + if( add ) { - FrameBufferImage fb = GetFrameBufferImage( *s, constant ); - if(fb) - { - task.SetTargetFrameBuffer( fb ); - } - else + for( TreeNode::ConstIterator iter = (*add).CBegin(); iter != (*add).CEnd(); ++iter ) { - DALI_SCRIPT_WARNING("Cannot find target frame buffer '%s'\n", (*s).c_str() ); + // empty actor adds directly to the stage + BaseHandle baseHandle = DoCreate( *mParser.GetRoot(), (*iter).second, Actor(), replacements ); + Actor actor = Actor::DownCast(baseHandle); + if(actor) + { + toActor.Add( actor ); + } } - } - 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 + // if were adding the 'stage' section then also check for a render task called stage + // to add automatically + if( "stage" == sectionName ) { - DALI_SCRIPT_WARNING("todo"); + if( OptionalChild renderTasks = IsChild(*mParser.GetRoot(), "renderTasks") ) + { + if( OptionalChild tasks = IsChild(*renderTasks, "stage") ) + { + CreateRenderTask( "stage" ); + } + } } } - - // other setup is via the property system - SetProperties( node, task, constant ); } void Builder::CreateRenderTask( const std::string &name ) @@ -556,48 +464,6 @@ void Builder::CreateRenderTask( const std::string &name ) } } -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; - } - } - } - } - } - - return ret; -} - Path Builder::GetPath( const std::string& name ) { DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded"); @@ -741,6 +607,7 @@ PathConstrainer Builder::GetPathConstrainer( const std::string& name ) return ret; } + bool Builder::IsPathConstrainer( const std::string& name ) { size_t count( mPathConstrainerLut.size() ); @@ -877,52 +744,133 @@ 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 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") ) - { - if( OptionalChild tasks = IsChild(*renderTasks, "stage") ) + // If config is string, find constant and replace it to original value. + if( (*iter).second.GetType() == TreeNode::STRING ) { - CreateRenderTask( "stage" ); - } - } - } - } -} + std::string stringConfigValue; + if( property.Get( stringConfigValue ) ) + { + std::size_t pos = 0; -Animation Builder::CreateAnimation( const std::string& animationName, const Replacement& replacement, Dali::Actor sourceActor ) -{ - DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded"); + 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; + } + } + } +} + +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) + { + Dali::Property::Value property; + if( (*iter).second.GetName() ) + { +#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< std::string >() ); + 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 ) +{ + DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded"); Animation anim; @@ -945,413 +893,755 @@ Animation Builder::CreateAnimation( const std::string& animationName, const Repl 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 ) + DALI_SCRIPT_VERBOSE(" Is Handle Object=%d\n", (long*)handle.GetObjectPtr()); + DALI_SCRIPT_VERBOSE(" Is Handle Property Count=%d\n", handle.GetPropertyCount()); + } + + if(actor) + { + DALI_SCRIPT_VERBOSE(" Is Actor id=%d\n", actor.GetProperty< int >( Actor::Property::ID )); + } + + Toolkit::Control control = Toolkit::Control::DownCast(handle); + if(control) + { + DALI_SCRIPT_VERBOSE(" Is Control id=%d\n", actor.GetProperty< int >( Actor::Property::ID )); + } +#endif // DEBUG_ENABLED + + if( templateNode ) + { + ApplyProperties( root, *templateNode, handle, replacements ); + + if( OptionalChild actors = IsChild( *templateNode, KEYNAME_ACTORS ) ) { - Property::Value& child = map->GetValue(i); - ConvertChildValue(mappingRoot, keyStack, child); + for( TreeConstIter iter = (*actors).CBegin(); iter != (*actors).CEnd(); ++iter ) + { + DoCreate( root, (*iter).second, actor, replacements ); + } } } - break; - } - case Property::ARRAY: - { - Property::Array* array = child.GetArray(); - if( array ) + if( actor ) { - for( Property::Array::SizeType i=0; i < array->Count(); ++i ) + // add children of all the styles + if( OptionalChild actors = IsChild( node, KEYNAME_ACTORS ) ) { - Property::Value& child = array->GetElementAt(i); - ConvertChildValue(mappingRoot, keyStack, child); + 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 ); } } - break; + else + { + ApplyProperties( root, node, handle, replacements ); + } + } + else + { + DALI_SCRIPT_WARNING("Cannot create handle from type '%s'\n", typeInfo.GetName().c_str()); } - - default: - // Ignore other types. - break; } - return result; + return baseHandle; } -bool Builder::RecursePropertyMap( const TreeNode& mappingRoot, KeyStack& keyStack, const char* theKey, Property::Type propertyType, Property::Value& value ) +void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replacement& constant ) { - Replacement replacer( mReplacementMap ); - bool result = false; - - keyStack.push_back( theKey ); + const Stage& stage = Stage::GetCurrent(); + Layer root = stage.GetRootLayer(); - for( TreeNode::ConstIterator iter = mappingRoot.CBegin(); iter != mappingRoot.CEnd(); ++iter ) + if( OptionalString s = constant.IsString( IsChild(node, "sourceActor") ) ) { - std::string aKey( (*iter).first ); - if( aKey.compare( theKey ) == 0 ) + Actor actor = root.FindChildByName(*s); + if(actor) { - if( propertyType == Property::NONE ) - { - DeterminePropertyFromNode( (*iter).second, value, replacer ); - result = true; - } - else - { - result = DeterminePropertyFromNode( (*iter).second, propertyType, value, replacer ); - } + task.SetSourceActor( actor ); + } + else + { + DALI_SCRIPT_WARNING("Cannot find source actor on stage for render task called '%s'\n", (*s).c_str() ); + } + } - if( result ) - { - ConvertChildValue(mappingRoot, keyStack, value); - } - break; + 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() ); } } - keyStack.pop_back(); - return result; -} + 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 ); +} -bool Builder::GetPropertyMap( const TreeNode& mappingRoot, const char* theKey, Property::Type propertyType, Property::Value& value ) +bool Builder::ApplyStyle( const std::string& styleName, Handle& handle, const Replacement& replacement ) { - KeyStack keyStack; - return RecursePropertyMap( mappingRoot, keyStack, theKey, propertyType, value ); -} + DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Builder script not loaded"); + OptionalChild styles = IsChild( *mParser.GetRoot(), KEYNAME_STYLES ); -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(); + std::string styleNameLower(styleName); + OptionalChild style = IsChildIgnoreCase( *styles, styleNameLower ); - if( !parser.Parse( data ) ) + if( styles && style ) { - DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n", - parser.GetErrorLineNumber(), - parser.GetErrorColumn(), - parser.GetErrorDescription().c_str() ); - - DALI_ASSERT_ALWAYS(!"Cannot parse JSON"); + ApplyAllStyleProperties( *mParser.GetRoot(), *style, handle, replacement ); + return true; } else { - // load constant map (allows the user to override the constants in the json after loading) - LoadConstants( *parser.GetRoot(), mReplacementMap ); + return false; + } +} - // merge includes - if( OptionalChild includes = IsChild(*parser.GetRoot(), KEYNAME_INCLUDES) ) - { - Replacement replacer( mReplacementMap ); +void Builder::ApplyAllStyleProperties( const TreeNode& root, const TreeNode& node, + Dali::Handle& handle, const Replacement& constant ) +{ + const char* styleName = node.GetName(); - for(TreeNode::ConstIterator iter = (*includes).CBegin(); iter != (*includes).CEnd(); ++iter) + StylePtr style = Style::New(); + + StylePtr* matchedStyle = NULL; + if( styleName ) + { + matchedStyle = mStyles.Find( styleName ); + if( ! matchedStyle ) + { + OptionalChild styleNodes = IsChild(root, KEYNAME_STYLES); + OptionalChild inheritFromNode = IsChild(node, KEYNAME_INHERIT); + if( !inheritFromNode ) { - OptionalString filename = replacer.IsString( (*iter).second ); + inheritFromNode = IsChild( node, KEYNAME_STYLES ); + } - if( filename ) + if( styleNodes ) + { + if( inheritFromNode ) { + TreeNodeList additionalStyleNodes; + + CollectAllStyles( *styleNodes, *inheritFromNode, additionalStyleNodes ); + #if defined(DEBUG_ENABLED) - DALI_SCRIPT_VERBOSE("Loading Include '%s'\n", (*filename).c_str()); + 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 - LoadFromString( GetFileContents(*filename) ); - } - } - } - if( !mParser.Parse( data ) ) - { - DALI_LOG_WARNING( "JSON Parse Error:%d:%d:'%s'\n", - mParser.GetErrorLineNumber(), - mParser.GetErrorColumn(), - mParser.GetErrorDescription().c_str() ); + // 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 ); + } + } - DALI_ASSERT_ALWAYS(!"Cannot parse JSON"); + RecordStyle( style, node, handle, constant ); + mStyles.Add( styleName, style ); // shallow copy + matchedStyle = &style; + } } } - 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); - - DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Cannot parse JSON"); -} - -void Builder::AddConstants( const Property::Map& map ) -{ - mReplacementMap.Merge( map ); -} - -void Builder::AddConstant( const std::string& key, const Property::Value& value ) -{ - mReplacementMap[key] = value; -} - -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 ) + if( matchedStyle ) { - return (*match); + StylePtr style( *matchedStyle ); + Dictionary instancedProperties; + style->ApplyVisualsAndPropertiesRecursively( handle, instancedProperties ); } - else + else // If there were no styles, instead set properties { - static Property::Value invalid; - return invalid; + SetProperties( node, handle, constant ); } + ApplySignals( root, node, handle ); + ApplyStylesByActor( root, node, handle, constant ); } -void Builder::LoadConstants( const TreeNode& root, Property::Map& intoMap ) +void Builder::RecordStyle( StylePtr style, + const TreeNode& node, + Dali::Handle& handle, + const Replacement& replacements ) { - Replacement replacer(intoMap); + // With repeated calls, accumulate inherited states, visuals and properties + // but override any with same name - if( OptionalChild constants = IsChild(root, "constants") ) + for( TreeNode::ConstIterator iter = node.CBegin(); iter != node.CEnd(); ++iter ) { - for(TreeNode::ConstIterator iter = (*constants).CBegin(); - iter != (*constants).CEnd(); ++iter) + const TreeNode::KeyNodePair& keyValue = *iter; + std::string key( keyValue.first ); + if( key == KEYNAME_STATES ) { - Dali::Property::Value property; - if( (*iter).second.GetName() ) + const TreeNode& states = keyValue.second; + if( states.GetType() != TreeNode::OBJECT ) { -#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; + DALI_LOG_WARNING( "RecordStyle() Node \"%s\" is not a JSON object\n", key.c_str() ); + continue; } - } - } -#if defined(DEBUG_ENABLED) - Property::Value* iter = intoMap.Find( "CONFIG_SCRIPT_LOG_LEVEL" ); - if( iter && iter->GetType() == Property::STRING ) - { - std::string logLevel( iter->Get< std::string >() ); - if( logLevel == "NoLogging" ) + 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 ) { - gFilterScript->SetLogLevel( Integration::Log::NoLogging ); + 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 ); - - std::string styleNameLower(styleName); - OptionalChild style = IsChildIgnoreCase( *styles, styleNameLower ); - - 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; + } - AddConstants( defaultDirs ); + 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; + } + + 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 } // namespace Toolkit