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=2cf23db21b4066078a97109f60929b54a47194c9;hp=aaf3f22c5b19d75a7ce386b28de2c8836f648464;hb=9598e692217c5fb541d862a3957b3efd5fd5171d;hpb=2071317d301e9d5af326a3a2efcd2996263fbead diff --git a/dali-toolkit/internal/builder/builder-impl.cpp b/dali-toolkit/internal/builder/builder-impl.cpp index aaf3f22..2cf23db 100644 --- a/dali-toolkit/internal/builder/builder-impl.cpp +++ b/dali-toolkit/internal/builder/builder-impl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd. + * Copyright (c) 2016 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. @@ -20,8 +20,8 @@ // EXTERNAL INCLUDES #include -#include #include + #include #include #include @@ -30,16 +30,21 @@ #include #include #include +#include #include // INTERNAL INCLUDES #include -#include +#include #include #include #include +#include #include +#include + +#include namespace Dali { @@ -52,12 +57,8 @@ namespace Internal class Replacement; extern Animation CreateAnimation(const TreeNode& child, const Replacement& replacements, const Dali::Actor searchRoot, Builder* const builder ); -extern bool SetPropertyFromNode( const TreeNode& node, Property::Value& value ); -extern bool SetPropertyFromNode( const TreeNode& node, Property::Value& value, const Replacement& replacements ); -extern bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value ); -extern bool SetPropertyFromNode( const TreeNode& node, Property::Type type, Property::Value& value, const Replacement& replacements ); -extern Actor SetupSignalAction(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, boost::function quitAction, Dali::Toolkit::Internal::Builder* const builder); -extern Actor SetupPropertyNotification(ConnectionTracker* tracker, const TreeNode &root, const TreeNode &child, Actor actor, boost::function quitAction, Dali::Toolkit::Internal::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 Actor SetupActor( const TreeNode& node, Actor& actor, const Replacement& constant ); #if defined(DEBUG_ENABLED) @@ -67,6 +68,8 @@ Integration::Log::Filter* gFilterScript = Integration::Log::Filter::New(Debug:: namespace { +#define TOKEN_STRING(x) #x + const std::string KEYNAME_STYLES = "styles"; const std::string KEYNAME_TYPE = "type"; const std::string KEYNAME_ACTORS = "actors"; @@ -74,118 +77,28 @@ 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"; typedef std::vector TreeNodeList; -template -std::string ToString(const T& value) -{ - std::stringstream ss; - ss << value; - return ss.str(); -} -template <> -std::string ToString(const Rect& value) +bool GetMappingKey( const std::string& str, std::string& key ) { - std::stringstream ss; - ss << value.x << "," << value.y << "," << value.width << "," << value.height; - return ss.str(); -} - -#if defined(DEBUG_ENABLED) - -std::string PropertyValueToString( const Property::Value& value ) -{ - std::string ret; - - switch( value.GetType() ) + bool result = false; + std::string test( str ); + if( ! test.empty() ) { - case Property::NONE: - { - ret = "NONE"; - break; - } ///< No type - case Property::BOOLEAN: - { - ret = value.Get() ? "True" : "False"; - break; - } - case Property::FLOAT: - { - - ret = ToString( value.Get() ); - break; - } - case Property::INTEGER: - { - ret = ToString( value.Get() ); - break; - } - case Property::UNSIGNED_INTEGER: - { - ret = ToString( value.Get() ); - break; - } - case Property::VECTOR2: - { - ret = ToString( value.Get() ); - break; - } - case Property::VECTOR3: - { - ret = ToString( value.Get() ); - break; - } - case Property::VECTOR4: - { - ret = ToString( value.Get() ); - break; - } - case Property::MATRIX3: - { - ret = ToString( value.Get() ); - break; - } - case Property::MATRIX: - { - ret = ToString( value.Get() ); - break; - } - case Property::RECTANGLE: + if( test.at(0) == '<' ) { - ret = ToString( value.Get< Rect >() ); - break; - } - case Property::ROTATION: - { - break; - } - case Property::STRING: - { - ret = value.Get(); - break; - } - case Property::ARRAY: - { - ret = std::string("Array Size=") + ToString( value.Get().Size() ); - break; - } - case Property::MAP: - { - ret = std::string("Map Size=") + ToString( value.Get().Count() ); - break; - } - case Property::TYPE_COUNT: - { - ret = ""; - break; + if( test.at(test.length()-1) == '>' ) + { + key = test.substr( 1, test.length()-2 ); + result = true; + } } } - - return ret; + return result; } -#endif // DEBUG_ENABLED /* * Recursively collects all stylesin a node (An array of style names). @@ -217,22 +130,6 @@ void CollectAllStyles( const TreeNode& stylesCollection, const TreeNode& style, } } -struct QuitAction -{ -public: - QuitAction( Builder& builder ) - : mBuilder( builder ) - { - } - - void operator()(void) - { - mBuilder.EmitQuitSignal(); - } - -private: - Builder& mBuilder; -}; } // namespace anon @@ -243,6 +140,7 @@ void Builder::SetProperties( const TreeNode& node, Handle& handle, const Replace { if( handle ) { + for( TreeNode::ConstIterator iter = node.CBegin(); iter != node.CEnd(); ++iter ) { const TreeNode::KeyNodePair& keyChild = *iter; @@ -250,16 +148,16 @@ void Builder::SetProperties( const TreeNode& node, Handle& handle, const Replace std::string key( keyChild.first ); // ignore special fields; type,actors,signals,styles - if(key == KEYNAME_TYPE || key == KEYNAME_ACTORS || key == KEYNAME_SIGNALS || key == KEYNAME_STYLES) + if(key == KEYNAME_TYPE || key == KEYNAME_ACTORS || key == KEYNAME_SIGNALS || key == KEYNAME_STYLES || key == KEYNAME_MAPPINGS ) { continue; } // special field 'image' usually contains an json object description // although sometimes refers to a framebuffer - if( 0 == keyChild.second.Size() ) + if( key == "image" ) { - if(key == "image") + if( 0 == keyChild.second.Size() ) { ImageActor imageActor = ImageActor::DownCast(handle); if(imageActor) @@ -277,7 +175,7 @@ void Builder::SetProperties( const TreeNode& node, Handle& handle, const Replace } // special field 'effect' references the shader effect instances - if(key == "effect") + if( key == "effect" ) { ImageActor actor = ImageActor::DownCast(handle); if( actor ) @@ -320,15 +218,30 @@ void Builder::SetProperties( const TreeNode& node, Handle& handle, const Replace if( Property::INVALID_INDEX != index ) { Property::Type type = propertyObject.GetPropertyType(index); - Property::Value value; - if( !SetPropertyFromNode( keyChild.second, type, value, constant ) ) + bool mapped = false; + + // if node.value is a mapping, get the property value from the "mappings" table + if( keyChild.second.GetType() == TreeNode::STRING ) { - // verbose as this might not be a problem - // eg parent-origin can be a string which is picked up later - DALI_SCRIPT_VERBOSE("Could not convert property:%s\n", key.c_str()); + std::string mappingKey; + if( GetMappingKey(keyChild.second.GetString(), mappingKey) ) + { + OptionalChild mappingRoot = IsChild( mParser.GetRoot(), KEYNAME_MAPPINGS ); + mapped = GetPropertyMap( *mappingRoot, mappingKey.c_str(), type, value ); + } } - else + if( ! mapped ) + { + mapped = DeterminePropertyFromNode( keyChild.second, type, value, constant ); + if( ! mapped ) + { + // verbose as this might not be a problem + // eg parentOrigin can be a string which is picked up later + DALI_SCRIPT_VERBOSE("Could not convert property:%s\n", key.c_str()); + } + } + if( mapped ) { DALI_SCRIPT_VERBOSE("SetProperty '%s' Index=:%d Value Type=%d Value '%s'\n", key.c_str(), index, value.GetType(), PropertyValueToString(value).c_str() ); @@ -361,9 +274,8 @@ void Builder::ApplyProperties( const TreeNode& root, const TreeNode& node, SetupActor( node, actor, constant ); // add signals - QuitAction quitAction( *this ); - SetupSignalAction( mSlotDelegate.GetConnectionTracker(), root, node, actor, quitAction, this ); - SetupPropertyNotification( mSlotDelegate.GetConnectionTracker(), root, node, actor, quitAction, this ); + SetupSignalAction( mSlotDelegate.GetConnectionTracker(), root, node, actor, this ); + SetupPropertyNotification( mSlotDelegate.GetConnectionTracker(), root, node, actor, this ); } } else @@ -572,7 +484,7 @@ void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replaceme const Stage& stage = Stage::GetCurrent(); Layer root = stage.GetRootLayer(); - if( OptionalString s = constant.IsString( IsChild(node, "source-actor") ) ) + if( OptionalString s = constant.IsString( IsChild(node, "sourceActor") ) ) { Actor actor = root.FindChildByName(*s); if(actor) @@ -585,7 +497,7 @@ void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replaceme } } - if( OptionalString s = constant.IsString( IsChild(node, "camera-actor") ) ) + if( OptionalString s = constant.IsString( IsChild(node, "cameraActor") ) ) { CameraActor actor = CameraActor::DownCast( root.FindChildByName(*s) ); if(actor) @@ -598,7 +510,7 @@ void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replaceme } } - if( OptionalString s = constant.IsString( IsChild(node, "target-frame-buffer") ) ) + if( OptionalString s = constant.IsString( IsChild(node, "targetFrameBuffer") ) ) { FrameBufferImage fb = GetFrameBufferImage( *s, constant ); if(fb) @@ -611,7 +523,7 @@ void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replaceme } } - if( OptionalString s = constant.IsString( IsChild(node, "screen-to-frame-buffer-function") ) ) + if( OptionalString s = constant.IsString( IsChild(node, "screenToFrameBufferFunction") ) ) { if("DEFAULT_SCREEN_TO_FRAMEBUFFER_FUNCTION" == *s) { @@ -628,8 +540,7 @@ void Builder::SetupTask( RenderTask& task, const TreeNode& node, const Replaceme } // other setup is via the property system - SetProperties( node, task, constant ); // @ todo, remove 'source-actor', 'camera-actor'? - + SetProperties( node, task, constant ); } void Builder::CreateRenderTask( const std::string &name ) @@ -640,7 +551,7 @@ void Builder::CreateRenderTask( const std::string &name ) const Stage& stage = Stage::GetCurrent(); - OptionalChild tasks = IsChild(*mParser.GetRoot(), "render-tasks"); + OptionalChild tasks = IsChild(*mParser.GetRoot(), "renderTasks"); if(tasks) { @@ -698,12 +609,12 @@ ShaderEffect Builder::GetShaderEffect( const std::string &name, const Replacemen } else { - if( OptionalChild effects = IsChild( *mParser.GetRoot(), "shader-effects") ) + if( OptionalChild effects = IsChild( *mParser.GetRoot(), "shaderEffects") ) { if( OptionalChild effect = IsChild( *effects, name ) ) { Dali::Property::Value propertyMap(Property::MAP); - if( SetPropertyFromNode( *effect, Property::MAP, propertyMap, constant ) ) + if( DeterminePropertyFromNode( *effect, Property::MAP, propertyMap, constant ) ) { ret = Dali::Scripting::NewShaderEffect( propertyMap ); mShaderEffectLut[ name ] = ret; @@ -734,16 +645,21 @@ FrameBufferImage Builder::GetFrameBufferImage( const std::string &name, const Re } else { - if( OptionalChild images = IsChild( *mParser.GetRoot(), "frame-buffer-images") ) + if( OptionalChild images = IsChild( *mParser.GetRoot(), "frameBufferImages") ) { if( OptionalChild image = IsChild( *images, name ) ) { - Dali::Property::Value propertyMap(Property::MAP); - if( SetPropertyFromNode( *image, Property::MAP, propertyMap, constant ) ) + Dali::Property::Value property(Property::MAP); + if( DeterminePropertyFromNode( *image, Property::MAP, property, constant ) ) { - propertyMap.SetValue(KEYNAME_TYPE, Property::Value(std::string("FrameBufferImage"))); - ret = FrameBufferImage::DownCast( Dali::Scripting::NewImage( propertyMap ) ); - mFrameBufferImageLut[ name ] = ret; + 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; + } } } } @@ -773,16 +689,16 @@ Path Builder::GetPath( const std::string& name ) if( OptionalChild pointsProperty = IsChild( *path, "points") ) { Dali::Property::Value points(Property::ARRAY); - if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) ) + if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) ) { ret = Path::New(); ret.SetProperty( Path::Property::POINTS, points); - //control-points property - if( OptionalChild pointsProperty = IsChild( *path, "control-points") ) + //controlPoints property + if( OptionalChild pointsProperty = IsChild( *path, "controlPoints") ) { Dali::Property::Value points(Property::ARRAY); - if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) ) + if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) ) { ret.SetProperty( Path::Property::CONTROL_POINTS, points); } @@ -847,16 +763,16 @@ PathConstrainer Builder::GetPathConstrainer( const std::string& name ) if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "points") ) { Dali::Property::Value points(Property::ARRAY); - if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) ) + if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) ) { ret = PathConstrainer::New(); ret.SetProperty( PathConstrainer::Property::POINTS, points); - //control-points property - if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "control-points") ) + //controlPoints property + if( OptionalChild pointsProperty = IsChild( *pathConstrainer, "controlPoints") ) { Dali::Property::Value points(Property::ARRAY); - if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) ) + if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) ) { ret.SetProperty( PathConstrainer::Property::CONTROL_POINTS, points); } @@ -956,16 +872,16 @@ Dali::LinearConstrainer Builder::GetLinearConstrainer( const std::string& name ) if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "value") ) { Dali::Property::Value points(Property::ARRAY); - if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) ) + if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) ) { ret = Dali::LinearConstrainer::New(); ret.SetProperty( LinearConstrainer::Property::VALUE, points); - //control-points property + //controlPoints property if( OptionalChild pointsProperty = IsChild( *linearConstrainer, "progress") ) { Dali::Property::Value points(Property::ARRAY); - if( SetPropertyFromNode( *pointsProperty, Property::ARRAY, points ) ) + if( DeterminePropertyFromNode( *pointsProperty, Property::ARRAY, points ) ) { ret.SetProperty( LinearConstrainer::Property::PROGRESS, points); } @@ -993,7 +909,7 @@ Dali::LinearConstrainer Builder::GetLinearConstrainer( const std::string& name ) bool Builder::IsLinearConstrainer( const std::string& name ) { - //Search the LinearConstrainer in the LUT + // Search the LinearConstrainer in the LUT size_t count( mLinearConstrainerLut.size() ); for( size_t i(0); i!=count; ++i ) { @@ -1063,7 +979,7 @@ void Builder::AddActors( const std::string §ionName, Actor toActor ) // to add automatically if( "stage" == sectionName ) { - if( OptionalChild renderTasks = IsChild(*mParser.GetRoot(), "render-tasks") ) + if( OptionalChild renderTasks = IsChild(*mParser.GetRoot(), "renderTasks") ) { if( OptionalChild tasks = IsChild(*renderTasks, "stage") ) { @@ -1125,6 +1041,118 @@ Animation Builder::CreateAnimation( const std::string& animationName ) return CreateAnimation( animationName, replacement, Dali::Stage::GetCurrent().GetRootLayer() ); } +bool Builder::ConvertChildValue( const TreeNode& mappingRoot, KeyStack& keyStack, Property::Value& child ) +{ + bool result = false; + + 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; + } + } + + 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(); + 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(); + 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; +} + +bool Builder::RecursePropertyMap( const TreeNode& mappingRoot, KeyStack& keyStack, const char* theKey, Property::Type propertyType, Property::Value& value ) +{ + Replacement replacer( mReplacementMap ); + bool result = false; + + 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 ) + { + 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 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 ); +} + + void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::UIFormat format ) { // parser to get constants and includes only @@ -1138,7 +1166,6 @@ void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::U parser.GetErrorDescription().c_str() ); DALI_ASSERT_ALWAYS(!"Cannot parse JSON"); - } else { @@ -1175,8 +1202,10 @@ void Builder::LoadFromString( std::string const& data, Dali::Toolkit::Builder::U } } - DALI_ASSERT_ALWAYS(mParser.GetRoot() && "Cannot parse JSON"); + 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 ) @@ -1223,15 +1252,8 @@ void Builder::LoadConstants( const TreeNode& root, Property::Map& intoMap ) #if defined(DEBUG_ENABLED) DALI_SCRIPT_VERBOSE("Constant set from json '%s'\n", (*iter).second.GetName()); #endif - if( SetPropertyFromNode( (*iter).second, property, replacer ) ) - { - intoMap[ (*iter).second.GetName() ] = property; - } - else - { - DALI_SCRIPT_WARNING("Cannot convert property for constant %s\n", - (*iter).second.GetName() == NULL ? "no name?" : (*iter).second.GetName()); - } + DeterminePropertyFromNode( (*iter).second, property, replacer ); + intoMap[ (*iter).second.GetName() ] = property; } } } @@ -1378,6 +1400,14 @@ Builder::Builder() : mSlotDelegate( this ) { mParser = Dali::Toolkit::JsonParser::New(); + + 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; + + AddConstants( defaultDirs ); } Builder::~Builder()