Canvas: More helper functions and cleanup.
This commit is contained in:
@@ -173,29 +173,19 @@ namespace canvas
|
|||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
GroupPtr Canvas::createGroup(const std::string& name)
|
GroupPtr Canvas::createGroup(const std::string& name)
|
||||||
{
|
{
|
||||||
return boost::dynamic_pointer_cast<Group>
|
return _root_group->createChild<Group>(name);
|
||||||
(
|
|
||||||
_root_group->createChild("group", name)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
GroupPtr Canvas::getGroup(const std::string& name)
|
GroupPtr Canvas::getGroup(const std::string& name)
|
||||||
{
|
{
|
||||||
return boost::dynamic_pointer_cast<Group>
|
return _root_group->getChild<Group>(name);
|
||||||
(
|
|
||||||
_root_group->getChild(name)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
GroupPtr Canvas::getOrCreateGroup(const std::string& name)
|
GroupPtr Canvas::getOrCreateGroup(const std::string& name)
|
||||||
{
|
{
|
||||||
GroupPtr group = getGroup(name);
|
return _root_group->getOrCreateChild<Group>(name);
|
||||||
if( group )
|
|
||||||
return group;
|
|
||||||
|
|
||||||
return createGroup(name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -46,8 +46,18 @@ namespace canvas
|
|||||||
return el;
|
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;
|
ElementFactories Group::_child_factories;
|
||||||
|
const std::string Group::TYPE_NAME = "group";
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
Group::Group( const CanvasWeakPtr& canvas,
|
Group::Group( const CanvasWeakPtr& canvas,
|
||||||
@@ -58,11 +68,11 @@ namespace canvas
|
|||||||
{
|
{
|
||||||
if( !isInit<Group>() )
|
if( !isInit<Group>() )
|
||||||
{
|
{
|
||||||
_child_factories["group"] = &createElement<Group>;
|
add<Group>(_child_factories);
|
||||||
_child_factories["image"] = &createElement<Image>;
|
add<Image>(_child_factories);
|
||||||
_child_factories["map" ] = &createElement<Map >;
|
add<Map >(_child_factories);
|
||||||
_child_factories["path" ] = &createElement<Path >;
|
add<Path >(_child_factories);
|
||||||
_child_factories["text" ] = &createElement<Text >;
|
add<Text >(_child_factories);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,6 +117,31 @@ namespace canvas
|
|||||||
return ElementPtr();
|
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)
|
ElementPtr Group::getElementById(const std::string& id)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ namespace canvas
|
|||||||
public Element
|
public Element
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
static const std::string TYPE_NAME;
|
||||||
|
|
||||||
typedef std::list< std::pair< const SGPropertyNode*,
|
typedef std::list< std::pair< const SGPropertyNode*,
|
||||||
ElementPtr
|
ElementPtr
|
||||||
>
|
>
|
||||||
@@ -49,6 +51,33 @@ namespace canvas
|
|||||||
const std::string& id = "" );
|
const std::string& id = "" );
|
||||||
ElementPtr getChild(const SGPropertyNode* node);
|
ElementPtr getChild(const SGPropertyNode* node);
|
||||||
ElementPtr getChild(const std::string& id);
|
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)
|
* Get first child with given id (breadth-first search)
|
||||||
|
|||||||
@@ -86,6 +86,9 @@ namespace canvas
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------
|
||||||
|
const std::string Image::TYPE_NAME = "image";
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
Image::Image( const CanvasWeakPtr& canvas,
|
Image::Image( const CanvasWeakPtr& canvas,
|
||||||
const SGPropertyNode_ptr& node,
|
const SGPropertyNode_ptr& node,
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ namespace canvas
|
|||||||
public Element
|
public Element
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
static const std::string TYPE_NAME;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param node Property node containing settings for this image:
|
* @param node Property node containing settings for this image:
|
||||||
* rect/[left/right/top/bottom] Dimensions of source
|
* rect/[left/right/top/bottom] Dimensions of source
|
||||||
|
|||||||
@@ -42,7 +42,9 @@ namespace simgear
|
|||||||
namespace canvas
|
namespace canvas
|
||||||
{
|
{
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------
|
||||||
const std::string GEO = "-geo";
|
const std::string GEO = "-geo";
|
||||||
|
const std::string Map::TYPE_NAME = "map";
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
Map::Map( const CanvasWeakPtr& canvas,
|
Map::Map( const CanvasWeakPtr& canvas,
|
||||||
|
|||||||
@@ -35,6 +35,8 @@ namespace canvas
|
|||||||
public Group
|
public Group
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
static const std::string TYPE_NAME;
|
||||||
|
|
||||||
Map( const CanvasWeakPtr& canvas,
|
Map( const CanvasWeakPtr& canvas,
|
||||||
const SGPropertyNode_ptr& node,
|
const SGPropertyNode_ptr& node,
|
||||||
const Style& parent_style,
|
const Style& parent_style,
|
||||||
|
|||||||
@@ -469,6 +469,9 @@ namespace canvas
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------
|
||||||
|
const std::string Path::TYPE_NAME = "path";
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
Path::Path( const CanvasWeakPtr& canvas,
|
Path::Path( const CanvasWeakPtr& canvas,
|
||||||
const SGPropertyNode_ptr& node,
|
const SGPropertyNode_ptr& node,
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ namespace canvas
|
|||||||
public Element
|
public Element
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
static const std::string TYPE_NAME;
|
||||||
|
|
||||||
Path( const CanvasWeakPtr& canvas,
|
Path( const CanvasWeakPtr& canvas,
|
||||||
const SGPropertyNode_ptr& node,
|
const SGPropertyNode_ptr& node,
|
||||||
const Style& parent_style,
|
const Style& parent_style,
|
||||||
|
|||||||
@@ -244,6 +244,9 @@ namespace canvas
|
|||||||
return osgText::Text::computePositions(contextID);
|
return osgText::Text::computePositions(contextID);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------
|
||||||
|
const std::string Text::TYPE_NAME = "text";
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
Text::Text( const CanvasWeakPtr& canvas,
|
Text::Text( const CanvasWeakPtr& canvas,
|
||||||
const SGPropertyNode_ptr& node,
|
const SGPropertyNode_ptr& node,
|
||||||
|
|||||||
@@ -33,6 +33,8 @@ namespace canvas
|
|||||||
public Element
|
public Element
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
static const std::string TYPE_NAME;
|
||||||
|
|
||||||
Text( const CanvasWeakPtr& canvas,
|
Text( const CanvasWeakPtr& canvas,
|
||||||
const SGPropertyNode_ptr& node,
|
const SGPropertyNode_ptr& node,
|
||||||
const Style& parent_style,
|
const Style& parent_style,
|
||||||
|
|||||||
Reference in New Issue
Block a user