CanvasGroup: allow derived classes to provide more/other child factories

This commit is contained in:
Thomas Geymayer 2013-06-11 22:09:57 +02:00
parent 830fb3b752
commit 14f04878d1
3 changed files with 42 additions and 21 deletions

View File

@ -137,6 +137,26 @@ namespace canvas
*/
virtual osg::BoundingBox getTransformedBounds(const osg::Matrix& m) const;
/**
* Create an canvas Element
*
* @tparam Derived Type of element (needs to derive from Element)
*/
template<typename Derived>
static
typename boost::enable_if<
boost::is_base_of<Element, Derived>,
ElementPtr
>::type create( const CanvasWeakPtr& canvas,
const SGPropertyNode_ptr& node,
const Style& style,
Element* parent )
{
ElementPtr el( new Derived(canvas, node, style, parent) );
el->setSelf(el);
return el;
}
protected:
enum Attributes

View File

@ -32,27 +32,13 @@ namespace simgear
{
namespace canvas
{
/**
* Create an canvas Element of type T
*/
template<typename T>
ElementPtr createElement( const CanvasWeakPtr& canvas,
const SGPropertyNode_ptr& node,
const Style& style,
Element* parent )
{
ElementPtr el( new T(canvas, node, style, parent) );
el->setSelf(el);
return el;
}
/**
* Add canvas Element type to factory map
*/
template<typename T>
template<typename ElementType>
void add(ElementFactories& factories)
{
factories[T::TYPE_NAME] = &createElement<T>;
factories[ElementType::TYPE_NAME] = &Element::create<ElementType>;
}
//----------------------------------------------------------------------------
@ -231,17 +217,26 @@ namespace canvas
return bb;
}
//----------------------------------------------------------------------------
ElementFactory Group::getChildFactory(const std::string& type) const
{
ElementFactories::iterator child_factory = _child_factories.find(type);
if( child_factory != _child_factories.end() )
return child_factory->second;
return ElementFactory();
}
//----------------------------------------------------------------------------
void Group::childAdded(SGPropertyNode* child)
{
if( child->getParent() != _node )
return;
ElementFactories::iterator child_factory =
_child_factories.find( child->getNameString() );
if( child_factory != _child_factories.end() )
ElementFactory child_factory = getChildFactory( child->getNameString() );
if( child_factory )
{
ElementPtr element = child_factory->second(_canvas, child, _style, this);
ElementPtr element = child_factory(_canvas, child, _style, this);
// Add to osg scene graph...
_transform->addChild( element->getMatrixTransform() );
@ -266,7 +261,7 @@ namespace canvas
if( node->getParent() != _node )
return;
if( _child_factories.find(node->getNameString()) != _child_factories.end() )
if( getChildFactory(node->getNameString()) )
{
ElementPtr child = getChild(node);
if( !child )

View File

@ -100,6 +100,12 @@ namespace canvas
static ElementFactories _child_factories;
/**
* Overload in derived classes to allow for more/other types of elements
* to be managed.
*/
virtual ElementFactory getChildFactory(const std::string& type) const;
virtual void childAdded(SGPropertyNode * child);
virtual void childRemoved(SGPropertyNode * child);
virtual void childChanged(SGPropertyNode * child);