]> git.mxchange.org Git - simgear.git/commitdiff
Clean up Canvas element creation
authorThomas Geymayer <tomgey@gmail.com>
Tue, 6 Nov 2012 17:48:00 +0000 (18:48 +0100)
committerThomas Geymayer <tomgey@gmail.com>
Tue, 6 Nov 2012 17:48:00 +0000 (18:48 +0100)
13 files changed:
simgear/canvas/canvas_fwd.hxx
simgear/canvas/elements/CanvasElement.cxx
simgear/canvas/elements/CanvasElement.hxx
simgear/canvas/elements/CanvasGroup.cxx
simgear/canvas/elements/CanvasGroup.hxx
simgear/canvas/elements/CanvasImage.cxx
simgear/canvas/elements/CanvasImage.hxx
simgear/canvas/elements/CanvasMap.cxx
simgear/canvas/elements/CanvasMap.hxx
simgear/canvas/elements/CanvasPath.cxx
simgear/canvas/elements/CanvasPath.hxx
simgear/canvas/elements/CanvasText.cxx
simgear/canvas/elements/CanvasText.hxx

index 2588460a313221899cfd73d1f888fbec16a92067..4c64c1d0087697d2bfab2f6996b4959ba3820de8 100644 (file)
@@ -29,6 +29,7 @@
 #include <boost/shared_ptr.hpp>
 #include <boost/weak_ptr.hpp>
 
+#include <map>
 #include <vector>
 
 namespace simgear
@@ -40,6 +41,15 @@ namespace canvas
   typedef boost::shared_ptr<Canvas> CanvasPtr;
   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;
 
   class Placement;
index e81cc2067c60963a7ad6dfe6ee28b0e50562be2f..bc150fc2f397992fe3984b6da9fda6fd499a05c4 100644 (file)
@@ -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 ),
index a079cd6babec86713c3e0c6cae1ef5becf3d2b95..d28baaecbd188df18f394c3de458c8baf3802595 100644 (file)
@@ -43,7 +43,6 @@ namespace canvas
     public SGPropertyChangeListener
   {
     public:
-      typedef std::map<std::string, const SGPropertyNode*> Style;
       typedef boost::function<void(const SGPropertyNode*)> StyleSetter;
       typedef std::map<std::string, StyleSetter> StyleSetters;
 
@@ -116,7 +115,7 @@ namespace canvas
       std::vector<SGPropertyNode_ptr>   _bounding_box;
 
       Element( const CanvasWeakPtr& canvas,
-               SGPropertyNode_ptr node,
+               const SGPropertyNode_ptr& node,
                const Style& parent_style );
 
       template<typename T, class C1, class C2>
index ff4be60ff0ac56530e4c4a5996636e0d114e36fd..89c62a628142bef555e4ab7bc3e3f3c7ea0507e8 100644 (file)
 #include "CanvasPath.hxx"
 #include "CanvasText.hxx"
 
+#include <boost/bind.hpp>
 #include <boost/foreach.hpp>
+#include <boost/make_shared.hpp>
+#include <boost/lambda/core.hpp>
 
 namespace simgear
 {
 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,
-                SGPropertyNode_ptr node,
+                const SGPropertyNode_ptr& node,
                 const Style& 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 )
       return;
 
-    boost::shared_ptr<Element> 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 =
index d57cea71d0d10c29ea6ab9abec598b5c40cbd6e7..5be3bed4ab86efcc200476c55859b228940100f3 100644 (file)
 
 #include "CanvasElement.hxx"
 
-#include <boost/shared_ptr.hpp>
 #include <list>
-#include <map>
 
 namespace simgear
 {
 namespace canvas
 {
 
-  typedef boost::shared_ptr<Element> 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<std::string, ElementFactory> ChildFactories;
+
+      ChildFactories    _child_factories;
+      ChildList         _children;
 
       virtual bool handleLocalMouseEvent(const canvas::MouseEvent& event);
 
index 2d07d8e8af0b1f6c1c245fb59afb2eae3cc3524e..e97a85b31c45e9c80e2e901fa42fb5f0eb433c06 100644 (file)
@@ -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),
index 700c475a9a47408bf108301564fdc35ace55e435..d7affec5c181393cc2b6fcdb06a3f2dd6a157e0f 100644 (file)
@@ -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();
 
index 1145e1d035d2954ecd70f344f2a0b8aa8080b100..a2ec2cba8a2cfacc683e579ade76b2361809fcca 100644 (file)
@@ -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
index 518c0b2a425f40643809a939e016b1e6d2519452..bf278a6e135e5ce24d7ddf55e3c14d9e99a60df3 100644 (file)
@@ -36,7 +36,7 @@ namespace canvas
   {
     public:
       Map( const CanvasWeakPtr& canvas,
-           SGPropertyNode_ptr node,
+           const SGPropertyNode_ptr& node,
            const Style& parent_style );
       virtual ~Map();
 
index bdbd17c65f29e65f419d00cfe355f1c10cfe9345..7b5af9272a4a267f22fb7a04d820d00b1082412e 100644 (file)
@@ -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) )
index e61897f7d3c0cc40874b0ca158c3a95aa3baef3a..3ef722a4ca02b31f15cd895dc9b3733dd299a552 100644 (file)
@@ -30,7 +30,7 @@ namespace canvas
   {
     public:
       Path( const CanvasWeakPtr& canvas,
-            SGPropertyNode_ptr node,
+            const SGPropertyNode_ptr& node,
             const Style& parent_style );
       virtual ~Path();
 
index 2ddf39d4a26f2610698a802f223172679436198f..d0680c365b35d33fc9336debad1f4e815d3942d5 100644 (file)
@@ -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) )
index 22596c57b80fb910550f6e5fd3ad868c7e321139..0937e5910d5640a95f24696acc5e259f3b6cb055 100644 (file)
@@ -34,7 +34,7 @@ namespace canvas
   {
     public:
       Text( const CanvasWeakPtr& canvas,
-            SGPropertyNode_ptr node,
+            const SGPropertyNode_ptr& node,
             const Style& parent_style );
       ~Text();