]> git.mxchange.org Git - simgear.git/commitdiff
CanvasGroup: allow derived classes to provide more/other child factories
authorThomas Geymayer <tomgey@gmail.com>
Tue, 11 Jun 2013 20:09:57 +0000 (22:09 +0200)
committerThomas Geymayer <tomgey@gmail.com>
Tue, 11 Jun 2013 20:09:57 +0000 (22:09 +0200)
simgear/canvas/elements/CanvasElement.hxx
simgear/canvas/elements/CanvasGroup.cxx
simgear/canvas/elements/CanvasGroup.hxx

index 693aec88c893d1761c22d86701de5d019f912268..deb57f6775bfe839602863d8905e3979559ab7dd 100644 (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
index e0ad87e07673ea44235172fbbe4d6e24caafbeb2..ff0cdd5f70abc97e6910e7dc025ec1bfa530e0c2 100644 (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 )
index f3ba781e9ce6dac2b72086daec6306de3469946d..81c6e639ab192b62647c27f13bc5b4a086fd42df 100644 (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);