//----------------------------------------------------------------------------
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);
}
//----------------------------------------------------------------------------
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,
{
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);
}
}
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)
{
public Element
{
public:
+ static const std::string TYPE_NAME;
+
typedef std::list< std::pair< const SGPropertyNode*,
ElementPtr
>
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)
return true;
}
+ //----------------------------------------------------------------------------
+ const std::string Image::TYPE_NAME = "image";
+
//----------------------------------------------------------------------------
Image::Image( const CanvasWeakPtr& canvas,
const SGPropertyNode_ptr& node,
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
namespace canvas
{
+ //----------------------------------------------------------------------------
const std::string GEO = "-geo";
+ const std::string Map::TYPE_NAME = "map";
//----------------------------------------------------------------------------
Map::Map( const CanvasWeakPtr& canvas,
public Group
{
public:
+ static const std::string TYPE_NAME;
+
Map( const CanvasWeakPtr& canvas,
const SGPropertyNode_ptr& node,
const Style& parent_style,
};
};
+ //----------------------------------------------------------------------------
+ const std::string Path::TYPE_NAME = "path";
+
//----------------------------------------------------------------------------
Path::Path( const CanvasWeakPtr& canvas,
const SGPropertyNode_ptr& node,
public Element
{
public:
+ static const std::string TYPE_NAME;
+
Path( const CanvasWeakPtr& canvas,
const SGPropertyNode_ptr& node,
const Style& parent_style,
return osgText::Text::computePositions(contextID);
}
+ //----------------------------------------------------------------------------
+ const std::string Text::TYPE_NAME = "text";
+
//----------------------------------------------------------------------------
Text::Text( const CanvasWeakPtr& canvas,
const SGPropertyNode_ptr& node,
public Element
{
public:
+ static const std::string TYPE_NAME;
+
Text( const CanvasWeakPtr& canvas,
const SGPropertyNode_ptr& node,
const Style& parent_style,