Canvas: More helper functions and cleanup.

This commit is contained in:
Thomas Geymayer
2013-06-07 16:39:03 +02:00
parent 60a81dfbd8
commit d24d3ce487
11 changed files with 91 additions and 18 deletions

View File

@@ -173,29 +173,19 @@ namespace canvas
//----------------------------------------------------------------------------
GroupPtr Canvas::createGroup(const std::string& name)
{
return boost::dynamic_pointer_cast<Group>
(
_root_group->createChild("group", name)
);
return _root_group->createChild<Group>(name);
}
//----------------------------------------------------------------------------
GroupPtr Canvas::getGroup(const std::string& name)
{
return boost::dynamic_pointer_cast<Group>
(
_root_group->getChild(name)
);
return _root_group->getChild<Group>(name);
}
//----------------------------------------------------------------------------
GroupPtr Canvas::getOrCreateGroup(const std::string& name)
{
GroupPtr group = getGroup(name);
if( group )
return group;
return createGroup(name);
return _root_group->getOrCreateChild<Group>(name);
}
//----------------------------------------------------------------------------

View File

@@ -46,8 +46,18 @@ namespace canvas
return el;
}
/**
* Add canvas Element type to factory map
*/
template<typename T>
void add(ElementFactories& factories)
{
factories[T::TYPE_NAME] = &createElement<T>;
}
//----------------------------------------------------------------------------
ElementFactories Group::_child_factories;
const std::string Group::TYPE_NAME = "group";
//----------------------------------------------------------------------------
Group::Group( const CanvasWeakPtr& canvas,
@@ -58,11 +68,11 @@ namespace canvas
{
if( !isInit<Group>() )
{
_child_factories["group"] = &createElement<Group>;
_child_factories["image"] = &createElement<Image>;
_child_factories["map" ] = &createElement<Map >;
_child_factories["path" ] = &createElement<Path >;
_child_factories["text" ] = &createElement<Text >;
add<Group>(_child_factories);
add<Image>(_child_factories);
add<Map >(_child_factories);
add<Path >(_child_factories);
add<Text >(_child_factories);
}
}
@@ -107,6 +117,31 @@ namespace canvas
return ElementPtr();
}
//----------------------------------------------------------------------------
ElementPtr Group::getOrCreateChild( const std::string& type,
const std::string& id )
{
ElementPtr child = getChild(id);
if( child )
{
if( child->getProps()->getNameString() == type )
return child;
SG_LOG
(
SG_GENERAL,
SG_WARN,
"Group::getOrCreateChild: type missmatch! "
"('" << type << "' != '" << child->getProps()->getName() << "', "
"id = '" << id << "')"
);
return ElementPtr();
}
return createChild(type, id);
}
//----------------------------------------------------------------------------
ElementPtr Group::getElementById(const std::string& id)
{

View File

@@ -34,6 +34,8 @@ namespace canvas
public Element
{
public:
static const std::string TYPE_NAME;
typedef std::list< std::pair< const SGPropertyNode*,
ElementPtr
>
@@ -49,6 +51,33 @@ namespace canvas
const std::string& id = "" );
ElementPtr getChild(const SGPropertyNode* node);
ElementPtr getChild(const std::string& id);
ElementPtr getOrCreateChild( const std::string& type,
const std::string& id );
template<class T>
boost::shared_ptr<T> createChild(const std::string& id = "")
{
return boost::dynamic_pointer_cast<T>( createChild(T::TYPE_NAME, id) );
}
template<class T>
boost::shared_ptr<T> getChild(const SGPropertyNode* node)
{
return boost::dynamic_pointer_cast<T>( getChild(node) );
}
template<class T>
boost::shared_ptr<T> getChild(const std::string& id)
{
return boost::dynamic_pointer_cast<T>( getChild(id) );
}
template<class T>
boost::shared_ptr<T> getOrCreateChild(const std::string& id)
{
return
boost::dynamic_pointer_cast<T>( getOrCreateChild(T::TYPE_NAME, id) );
}
/**
* Get first child with given id (breadth-first search)

View File

@@ -86,6 +86,9 @@ namespace canvas
return true;
}
//----------------------------------------------------------------------------
const std::string Image::TYPE_NAME = "image";
//----------------------------------------------------------------------------
Image::Image( const CanvasWeakPtr& canvas,
const SGPropertyNode_ptr& node,

View File

@@ -34,6 +34,8 @@ namespace canvas
public Element
{
public:
static const std::string TYPE_NAME;
/**
* @param node Property node containing settings for this image:
* rect/[left/right/top/bottom] Dimensions of source

View File

@@ -42,7 +42,9 @@ namespace simgear
namespace canvas
{
//----------------------------------------------------------------------------
const std::string GEO = "-geo";
const std::string Map::TYPE_NAME = "map";
//----------------------------------------------------------------------------
Map::Map( const CanvasWeakPtr& canvas,

View File

@@ -35,6 +35,8 @@ namespace canvas
public Group
{
public:
static const std::string TYPE_NAME;
Map( const CanvasWeakPtr& canvas,
const SGPropertyNode_ptr& node,
const Style& parent_style,

View File

@@ -469,6 +469,9 @@ namespace canvas
};
};
//----------------------------------------------------------------------------
const std::string Path::TYPE_NAME = "path";
//----------------------------------------------------------------------------
Path::Path( const CanvasWeakPtr& canvas,
const SGPropertyNode_ptr& node,

View File

@@ -30,6 +30,8 @@ namespace canvas
public Element
{
public:
static const std::string TYPE_NAME;
Path( const CanvasWeakPtr& canvas,
const SGPropertyNode_ptr& node,
const Style& parent_style,

View File

@@ -244,6 +244,9 @@ namespace canvas
return osgText::Text::computePositions(contextID);
}
//----------------------------------------------------------------------------
const std::string Text::TYPE_NAME = "text";
//----------------------------------------------------------------------------
Text::Text( const CanvasWeakPtr& canvas,
const SGPropertyNode_ptr& node,

View File

@@ -33,6 +33,8 @@ namespace canvas
public Element
{
public:
static const std::string TYPE_NAME;
Text( const CanvasWeakPtr& canvas,
const SGPropertyNode_ptr& node,
const Style& parent_style,