From: Thomas Geymayer Date: Tue, 6 Nov 2012 17:48:00 +0000 (+0100) Subject: Clean up Canvas element creation X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=34719da0005f8c3d13cce2538820a45f0a5b7d3e;p=simgear.git Clean up Canvas element creation --- diff --git a/simgear/canvas/canvas_fwd.hxx b/simgear/canvas/canvas_fwd.hxx index 2588460a..4c64c1d0 100644 --- a/simgear/canvas/canvas_fwd.hxx +++ b/simgear/canvas/canvas_fwd.hxx @@ -29,6 +29,7 @@ #include #include +#include #include namespace simgear @@ -40,6 +41,15 @@ namespace canvas typedef boost::shared_ptr CanvasPtr; typedef boost::weak_ptr CanvasWeakPtr; + class Element; + typedef boost::shared_ptr ElementPtr; + typedef boost::weak_ptr ElementWeakPtr; + + typedef std::map Style; + typedef boost::function ElementFactory; + typedef osg::ref_ptr FontPtr; class Placement; diff --git a/simgear/canvas/elements/CanvasElement.cxx b/simgear/canvas/elements/CanvasElement.cxx index e81cc206..bc150fc2 100644 --- a/simgear/canvas/elements/CanvasElement.cxx +++ b/simgear/canvas/elements/CanvasElement.cxx @@ -240,7 +240,7 @@ namespace canvas //---------------------------------------------------------------------------- Element::Element( const CanvasWeakPtr& canvas, - SGPropertyNode_ptr node, + const SGPropertyNode_ptr& node, const Style& parent_style ): _canvas( canvas ), _transform_dirty( false ), diff --git a/simgear/canvas/elements/CanvasElement.hxx b/simgear/canvas/elements/CanvasElement.hxx index a079cd6b..d28baaec 100644 --- a/simgear/canvas/elements/CanvasElement.hxx +++ b/simgear/canvas/elements/CanvasElement.hxx @@ -43,7 +43,6 @@ namespace canvas public SGPropertyChangeListener { public: - typedef std::map Style; typedef boost::function StyleSetter; typedef std::map StyleSetters; @@ -116,7 +115,7 @@ namespace canvas std::vector _bounding_box; Element( const CanvasWeakPtr& canvas, - SGPropertyNode_ptr node, + const SGPropertyNode_ptr& node, const Style& parent_style ); template diff --git a/simgear/canvas/elements/CanvasGroup.cxx b/simgear/canvas/elements/CanvasGroup.cxx index ff4be60f..89c62a62 100644 --- a/simgear/canvas/elements/CanvasGroup.cxx +++ b/simgear/canvas/elements/CanvasGroup.cxx @@ -22,20 +22,43 @@ #include "CanvasPath.hxx" #include "CanvasText.hxx" +#include #include +#include +#include namespace simgear { namespace canvas { + /** + * Create an ElementFactory for elements of type T + */ + template + ElementFactory createElementFactory() + { + return boost::bind + ( + &boost::make_shared, + boost::lambda::_1, + boost::lambda::_2, + boost::lambda::_3 + ); + } //---------------------------------------------------------------------------- Group::Group( const CanvasWeakPtr& canvas, - SGPropertyNode_ptr node, + const SGPropertyNode_ptr& node, const Style& parent_style ): Element(canvas, node, parent_style) { - + _child_factories["group"] = createElementFactory(); + _child_factories["image"] = createElementFactory(); + _child_factories["map" ] = createElementFactory(); + _child_factories["path" ] = createElementFactory(); + _child_factories["text" ] = createElementFactory(); } //---------------------------------------------------------------------------- @@ -71,26 +94,16 @@ namespace canvas if( child->getParent() != _node ) return; - boost::shared_ptr element; - - // TODO create map of child factories and use also to check for element - // 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 ) + ChildFactories::iterator child_factory = + _child_factories.find( child->getNameString() ); + if( child_factory != _child_factories.end() ) { + ElementPtr element = child_factory->second(_canvas, child, _style); + // Add to osg scene graph... _transform->addChild( element->getMatrixTransform() ); _children.push_back( ChildList::value_type(child, element) ); + return; } @@ -120,11 +133,7 @@ namespace canvas if( node->getParent() != _node ) return; - if( node->getNameString() == "text" - || node->getNameString() == "group" - || node->getNameString() == "map" - || node->getNameString() == "path" - || node->getNameString() == "image" ) + if( _child_factories.find(node->getNameString()) != _child_factories.end() ) { ChildFinder pred(node); ChildList::iterator child = diff --git a/simgear/canvas/elements/CanvasGroup.hxx b/simgear/canvas/elements/CanvasGroup.hxx index d57cea71..5be3bed4 100644 --- a/simgear/canvas/elements/CanvasGroup.hxx +++ b/simgear/canvas/elements/CanvasGroup.hxx @@ -21,17 +21,13 @@ #include "CanvasElement.hxx" -#include #include -#include namespace simgear { namespace canvas { - typedef boost::shared_ptr ElementPtr; - class Group: public Element { @@ -42,7 +38,7 @@ namespace canvas > ChildList; Group( const CanvasWeakPtr& canvas, - SGPropertyNode_ptr node, + const SGPropertyNode_ptr& node, const Style& parent_style = Style() ); virtual ~Group(); @@ -50,7 +46,10 @@ namespace canvas protected: - ChildList _children; + typedef std::map ChildFactories; + + ChildFactories _child_factories; + ChildList _children; virtual bool handleLocalMouseEvent(const canvas::MouseEvent& event); diff --git a/simgear/canvas/elements/CanvasImage.cxx b/simgear/canvas/elements/CanvasImage.cxx index 2d07d8e8..e97a85b3 100644 --- a/simgear/canvas/elements/CanvasImage.cxx +++ b/simgear/canvas/elements/CanvasImage.cxx @@ -73,7 +73,7 @@ namespace canvas //---------------------------------------------------------------------------- Image::Image( const CanvasWeakPtr& canvas, - SGPropertyNode_ptr node, + const SGPropertyNode_ptr& node, const Style& parent_style ): Element(canvas, node, parent_style), _texture(new osg::Texture2D), diff --git a/simgear/canvas/elements/CanvasImage.hxx b/simgear/canvas/elements/CanvasImage.hxx index 700c475a..d7affec5 100644 --- a/simgear/canvas/elements/CanvasImage.hxx +++ b/simgear/canvas/elements/CanvasImage.hxx @@ -42,7 +42,7 @@ namespace canvas * [x,y] Position of rectangle */ Image( const CanvasWeakPtr& canvas, - SGPropertyNode_ptr node, + const SGPropertyNode_ptr& node, const Style& parent_style ); virtual ~Image(); diff --git a/simgear/canvas/elements/CanvasMap.cxx b/simgear/canvas/elements/CanvasMap.cxx index 1145e1d0..a2ec2cba 100644 --- a/simgear/canvas/elements/CanvasMap.cxx +++ b/simgear/canvas/elements/CanvasMap.cxx @@ -46,7 +46,7 @@ namespace canvas //---------------------------------------------------------------------------- Map::Map( const CanvasWeakPtr& canvas, - SGPropertyNode_ptr node, + const SGPropertyNode_ptr& node, const Style& parent_style ): Group(canvas, node, parent_style), // TODO make projection configurable diff --git a/simgear/canvas/elements/CanvasMap.hxx b/simgear/canvas/elements/CanvasMap.hxx index 518c0b2a..bf278a6e 100644 --- a/simgear/canvas/elements/CanvasMap.hxx +++ b/simgear/canvas/elements/CanvasMap.hxx @@ -36,7 +36,7 @@ namespace canvas { public: Map( const CanvasWeakPtr& canvas, - SGPropertyNode_ptr node, + const SGPropertyNode_ptr& node, const Style& parent_style ); virtual ~Map(); diff --git a/simgear/canvas/elements/CanvasPath.cxx b/simgear/canvas/elements/CanvasPath.cxx index bdbd17c6..7b5af927 100644 --- a/simgear/canvas/elements/CanvasPath.cxx +++ b/simgear/canvas/elements/CanvasPath.cxx @@ -354,7 +354,7 @@ namespace canvas //---------------------------------------------------------------------------- Path::Path( const CanvasWeakPtr& canvas, - SGPropertyNode_ptr node, + const SGPropertyNode_ptr& node, const Style& parent_style ): Element(canvas, node, parent_style), _path( new PathDrawable(this) ) diff --git a/simgear/canvas/elements/CanvasPath.hxx b/simgear/canvas/elements/CanvasPath.hxx index e61897f7..3ef722a4 100644 --- a/simgear/canvas/elements/CanvasPath.hxx +++ b/simgear/canvas/elements/CanvasPath.hxx @@ -30,7 +30,7 @@ namespace canvas { public: Path( const CanvasWeakPtr& canvas, - SGPropertyNode_ptr node, + const SGPropertyNode_ptr& node, const Style& parent_style ); virtual ~Path(); diff --git a/simgear/canvas/elements/CanvasText.cxx b/simgear/canvas/elements/CanvasText.cxx index 2ddf39d4..d0680c36 100644 --- a/simgear/canvas/elements/CanvasText.cxx +++ b/simgear/canvas/elements/CanvasText.cxx @@ -176,7 +176,7 @@ namespace canvas //---------------------------------------------------------------------------- Text::Text( const CanvasWeakPtr& canvas, - SGPropertyNode_ptr node, + const SGPropertyNode_ptr& node, const Style& parent_style ): Element(canvas, node, parent_style), _text( new Text::TextOSG(this) ) diff --git a/simgear/canvas/elements/CanvasText.hxx b/simgear/canvas/elements/CanvasText.hxx index 22596c57..0937e591 100644 --- a/simgear/canvas/elements/CanvasText.hxx +++ b/simgear/canvas/elements/CanvasText.hxx @@ -34,7 +34,7 @@ namespace canvas { public: Text( const CanvasWeakPtr& canvas, - SGPropertyNode_ptr node, + const SGPropertyNode_ptr& node, const Style& parent_style ); ~Text();