Clean up Canvas element creation
This commit is contained in:
parent
df8a3d9c60
commit
34719da000
@ -29,6 +29,7 @@
|
|||||||
#include <boost/shared_ptr.hpp>
|
#include <boost/shared_ptr.hpp>
|
||||||
#include <boost/weak_ptr.hpp>
|
#include <boost/weak_ptr.hpp>
|
||||||
|
|
||||||
|
#include <map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace simgear
|
namespace simgear
|
||||||
@ -40,6 +41,15 @@ namespace canvas
|
|||||||
typedef boost::shared_ptr<Canvas> CanvasPtr;
|
typedef boost::shared_ptr<Canvas> CanvasPtr;
|
||||||
typedef boost::weak_ptr<Canvas> CanvasWeakPtr;
|
typedef boost::weak_ptr<Canvas> CanvasWeakPtr;
|
||||||
|
|
||||||
|
class Element;
|
||||||
|
typedef boost::shared_ptr<Element> ElementPtr;
|
||||||
|
typedef boost::weak_ptr<Element> ElementWeakPtr;
|
||||||
|
|
||||||
|
typedef std::map<std::string, const SGPropertyNode*> Style;
|
||||||
|
typedef boost::function<ElementPtr( const CanvasWeakPtr&,
|
||||||
|
const SGPropertyNode_ptr&,
|
||||||
|
const Style& )> ElementFactory;
|
||||||
|
|
||||||
typedef osg::ref_ptr<osgText::Font> FontPtr;
|
typedef osg::ref_ptr<osgText::Font> FontPtr;
|
||||||
|
|
||||||
class Placement;
|
class Placement;
|
||||||
|
@ -240,7 +240,7 @@ namespace canvas
|
|||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
Element::Element( const CanvasWeakPtr& canvas,
|
Element::Element( const CanvasWeakPtr& canvas,
|
||||||
SGPropertyNode_ptr node,
|
const SGPropertyNode_ptr& node,
|
||||||
const Style& parent_style ):
|
const Style& parent_style ):
|
||||||
_canvas( canvas ),
|
_canvas( canvas ),
|
||||||
_transform_dirty( false ),
|
_transform_dirty( false ),
|
||||||
|
@ -43,7 +43,6 @@ namespace canvas
|
|||||||
public SGPropertyChangeListener
|
public SGPropertyChangeListener
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
typedef std::map<std::string, const SGPropertyNode*> Style;
|
|
||||||
typedef boost::function<void(const SGPropertyNode*)> StyleSetter;
|
typedef boost::function<void(const SGPropertyNode*)> StyleSetter;
|
||||||
typedef std::map<std::string, StyleSetter> StyleSetters;
|
typedef std::map<std::string, StyleSetter> StyleSetters;
|
||||||
|
|
||||||
@ -116,7 +115,7 @@ namespace canvas
|
|||||||
std::vector<SGPropertyNode_ptr> _bounding_box;
|
std::vector<SGPropertyNode_ptr> _bounding_box;
|
||||||
|
|
||||||
Element( const CanvasWeakPtr& canvas,
|
Element( const CanvasWeakPtr& canvas,
|
||||||
SGPropertyNode_ptr node,
|
const SGPropertyNode_ptr& node,
|
||||||
const Style& parent_style );
|
const Style& parent_style );
|
||||||
|
|
||||||
template<typename T, class C1, class C2>
|
template<typename T, class C1, class C2>
|
||||||
|
@ -22,20 +22,43 @@
|
|||||||
#include "CanvasPath.hxx"
|
#include "CanvasPath.hxx"
|
||||||
#include "CanvasText.hxx"
|
#include "CanvasText.hxx"
|
||||||
|
|
||||||
|
#include <boost/bind.hpp>
|
||||||
#include <boost/foreach.hpp>
|
#include <boost/foreach.hpp>
|
||||||
|
#include <boost/make_shared.hpp>
|
||||||
|
#include <boost/lambda/core.hpp>
|
||||||
|
|
||||||
namespace simgear
|
namespace simgear
|
||||||
{
|
{
|
||||||
namespace canvas
|
namespace canvas
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Create an ElementFactory for elements of type T
|
||||||
|
*/
|
||||||
|
template<typename T>
|
||||||
|
ElementFactory createElementFactory()
|
||||||
|
{
|
||||||
|
return boost::bind
|
||||||
|
(
|
||||||
|
&boost::make_shared<T, const CanvasWeakPtr&,
|
||||||
|
const SGPropertyNode_ptr&,
|
||||||
|
const Style&>,
|
||||||
|
boost::lambda::_1,
|
||||||
|
boost::lambda::_2,
|
||||||
|
boost::lambda::_3
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
Group::Group( const CanvasWeakPtr& canvas,
|
Group::Group( const CanvasWeakPtr& canvas,
|
||||||
SGPropertyNode_ptr node,
|
const SGPropertyNode_ptr& node,
|
||||||
const Style& parent_style ):
|
const Style& parent_style ):
|
||||||
Element(canvas, node, parent_style)
|
Element(canvas, node, parent_style)
|
||||||
{
|
{
|
||||||
|
_child_factories["group"] = createElementFactory<Group>();
|
||||||
|
_child_factories["image"] = createElementFactory<Image>();
|
||||||
|
_child_factories["map" ] = createElementFactory<Map >();
|
||||||
|
_child_factories["path" ] = createElementFactory<Path >();
|
||||||
|
_child_factories["text" ] = createElementFactory<Text >();
|
||||||
}
|
}
|
||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
@ -71,26 +94,16 @@ namespace canvas
|
|||||||
if( child->getParent() != _node )
|
if( child->getParent() != _node )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
boost::shared_ptr<Element> element;
|
ChildFactories::iterator child_factory =
|
||||||
|
_child_factories.find( child->getNameString() );
|
||||||
// TODO create map of child factories and use also to check for element
|
if( child_factory != _child_factories.end() )
|
||||||
// on deletion in ::childRemoved
|
|
||||||
if( child->getNameString() == "text" )
|
|
||||||
element.reset( new Text(_canvas, child, _style) );
|
|
||||||
else if( child->getNameString() == "group" )
|
|
||||||
element.reset( new Group(_canvas, child, _style) );
|
|
||||||
else if( child->getNameString() == "map" )
|
|
||||||
element.reset( new Map(_canvas, child, _style) );
|
|
||||||
else if( child->getNameString() == "path" )
|
|
||||||
element.reset( new Path(_canvas, child, _style) );
|
|
||||||
else if( child->getNameString() == "image" )
|
|
||||||
element.reset( new Image(_canvas, child, _style) );
|
|
||||||
|
|
||||||
if( element )
|
|
||||||
{
|
{
|
||||||
|
ElementPtr element = child_factory->second(_canvas, child, _style);
|
||||||
|
|
||||||
// Add to osg scene graph...
|
// Add to osg scene graph...
|
||||||
_transform->addChild( element->getMatrixTransform() );
|
_transform->addChild( element->getMatrixTransform() );
|
||||||
_children.push_back( ChildList::value_type(child, element) );
|
_children.push_back( ChildList::value_type(child, element) );
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,11 +133,7 @@ namespace canvas
|
|||||||
if( node->getParent() != _node )
|
if( node->getParent() != _node )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if( node->getNameString() == "text"
|
if( _child_factories.find(node->getNameString()) != _child_factories.end() )
|
||||||
|| node->getNameString() == "group"
|
|
||||||
|| node->getNameString() == "map"
|
|
||||||
|| node->getNameString() == "path"
|
|
||||||
|| node->getNameString() == "image" )
|
|
||||||
{
|
{
|
||||||
ChildFinder pred(node);
|
ChildFinder pred(node);
|
||||||
ChildList::iterator child =
|
ChildList::iterator child =
|
||||||
|
@ -21,17 +21,13 @@
|
|||||||
|
|
||||||
#include "CanvasElement.hxx"
|
#include "CanvasElement.hxx"
|
||||||
|
|
||||||
#include <boost/shared_ptr.hpp>
|
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <map>
|
|
||||||
|
|
||||||
namespace simgear
|
namespace simgear
|
||||||
{
|
{
|
||||||
namespace canvas
|
namespace canvas
|
||||||
{
|
{
|
||||||
|
|
||||||
typedef boost::shared_ptr<Element> ElementPtr;
|
|
||||||
|
|
||||||
class Group:
|
class Group:
|
||||||
public Element
|
public Element
|
||||||
{
|
{
|
||||||
@ -42,7 +38,7 @@ namespace canvas
|
|||||||
> ChildList;
|
> ChildList;
|
||||||
|
|
||||||
Group( const CanvasWeakPtr& canvas,
|
Group( const CanvasWeakPtr& canvas,
|
||||||
SGPropertyNode_ptr node,
|
const SGPropertyNode_ptr& node,
|
||||||
const Style& parent_style = Style() );
|
const Style& parent_style = Style() );
|
||||||
virtual ~Group();
|
virtual ~Group();
|
||||||
|
|
||||||
@ -50,7 +46,10 @@ namespace canvas
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
ChildList _children;
|
typedef std::map<std::string, ElementFactory> ChildFactories;
|
||||||
|
|
||||||
|
ChildFactories _child_factories;
|
||||||
|
ChildList _children;
|
||||||
|
|
||||||
virtual bool handleLocalMouseEvent(const canvas::MouseEvent& event);
|
virtual bool handleLocalMouseEvent(const canvas::MouseEvent& event);
|
||||||
|
|
||||||
|
@ -73,7 +73,7 @@ namespace canvas
|
|||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
Image::Image( const CanvasWeakPtr& canvas,
|
Image::Image( const CanvasWeakPtr& canvas,
|
||||||
SGPropertyNode_ptr node,
|
const SGPropertyNode_ptr& node,
|
||||||
const Style& parent_style ):
|
const Style& parent_style ):
|
||||||
Element(canvas, node, parent_style),
|
Element(canvas, node, parent_style),
|
||||||
_texture(new osg::Texture2D),
|
_texture(new osg::Texture2D),
|
||||||
|
@ -42,7 +42,7 @@ namespace canvas
|
|||||||
* [x,y] Position of rectangle
|
* [x,y] Position of rectangle
|
||||||
*/
|
*/
|
||||||
Image( const CanvasWeakPtr& canvas,
|
Image( const CanvasWeakPtr& canvas,
|
||||||
SGPropertyNode_ptr node,
|
const SGPropertyNode_ptr& node,
|
||||||
const Style& parent_style );
|
const Style& parent_style );
|
||||||
virtual ~Image();
|
virtual ~Image();
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ namespace canvas
|
|||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
Map::Map( const CanvasWeakPtr& canvas,
|
Map::Map( const CanvasWeakPtr& canvas,
|
||||||
SGPropertyNode_ptr node,
|
const SGPropertyNode_ptr& node,
|
||||||
const Style& parent_style ):
|
const Style& parent_style ):
|
||||||
Group(canvas, node, parent_style),
|
Group(canvas, node, parent_style),
|
||||||
// TODO make projection configurable
|
// TODO make projection configurable
|
||||||
|
@ -36,7 +36,7 @@ namespace canvas
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Map( const CanvasWeakPtr& canvas,
|
Map( const CanvasWeakPtr& canvas,
|
||||||
SGPropertyNode_ptr node,
|
const SGPropertyNode_ptr& node,
|
||||||
const Style& parent_style );
|
const Style& parent_style );
|
||||||
virtual ~Map();
|
virtual ~Map();
|
||||||
|
|
||||||
|
@ -354,7 +354,7 @@ namespace canvas
|
|||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
Path::Path( const CanvasWeakPtr& canvas,
|
Path::Path( const CanvasWeakPtr& canvas,
|
||||||
SGPropertyNode_ptr node,
|
const SGPropertyNode_ptr& node,
|
||||||
const Style& parent_style ):
|
const Style& parent_style ):
|
||||||
Element(canvas, node, parent_style),
|
Element(canvas, node, parent_style),
|
||||||
_path( new PathDrawable(this) )
|
_path( new PathDrawable(this) )
|
||||||
|
@ -30,7 +30,7 @@ namespace canvas
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Path( const CanvasWeakPtr& canvas,
|
Path( const CanvasWeakPtr& canvas,
|
||||||
SGPropertyNode_ptr node,
|
const SGPropertyNode_ptr& node,
|
||||||
const Style& parent_style );
|
const Style& parent_style );
|
||||||
virtual ~Path();
|
virtual ~Path();
|
||||||
|
|
||||||
|
@ -176,7 +176,7 @@ namespace canvas
|
|||||||
|
|
||||||
//----------------------------------------------------------------------------
|
//----------------------------------------------------------------------------
|
||||||
Text::Text( const CanvasWeakPtr& canvas,
|
Text::Text( const CanvasWeakPtr& canvas,
|
||||||
SGPropertyNode_ptr node,
|
const SGPropertyNode_ptr& node,
|
||||||
const Style& parent_style ):
|
const Style& parent_style ):
|
||||||
Element(canvas, node, parent_style),
|
Element(canvas, node, parent_style),
|
||||||
_text( new Text::TextOSG(this) )
|
_text( new Text::TextOSG(this) )
|
||||||
|
@ -34,7 +34,7 @@ namespace canvas
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Text( const CanvasWeakPtr& canvas,
|
Text( const CanvasWeakPtr& canvas,
|
||||||
SGPropertyNode_ptr node,
|
const SGPropertyNode_ptr& node,
|
||||||
const Style& parent_style );
|
const Style& parent_style );
|
||||||
~Text();
|
~Text();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user