]> git.mxchange.org Git - simgear.git/commitdiff
Canvas: Ensure all element types are initialized before first usage.
authorThomas Geymayer <tomgey@gmail.com>
Sat, 20 Jul 2013 13:45:02 +0000 (15:45 +0200)
committerThomas Geymayer <tomgey@gmail.com>
Sat, 20 Jul 2013 13:45:02 +0000 (15:45 +0200)
If setting properties on a group a check is performed if
this property exists on any possible child element, and
only if this is the case the property is stored in the
groups style.

Previously elements have been only initialized during
their first usage, leading to ignored styles if they
have been set on a parent element before instantiating
an instance of the actual element type.

12 files changed:
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 319c07d27c6177e1bbbbbd055910220332949e70..c4612659dfb80fcf231acea6469784a5aa8dcd9a 100644 (file)
@@ -523,6 +523,8 @@ namespace canvas
     _style( parent_style ),
     _drawable( 0 )
   {
+    staticInit();
+
     SG_LOG
     (
       SG_GL,
@@ -530,11 +532,6 @@ namespace canvas
       "New canvas element " << node->getPath()
     );
 
-    if( !isInit<Element>() )
-    {
-      addStyle("clip", "", &Element::setClip, false);
-    }
-
     // Ensure elements are drawn in order they appear in the element tree
     _transform->getOrCreateStateSet()
               ->setRenderBinDetails
@@ -545,6 +542,15 @@ namespace canvas
               );
   }
 
+  //----------------------------------------------------------------------------
+  void Element::staticInit()
+  {
+    if( isInit<Element>() )
+      return;
+
+    addStyle("clip", "", &Element::setClip, false);
+  }
+
   //----------------------------------------------------------------------------
   bool Element::isStyleEmpty(const SGPropertyNode* child) const
   {
index 07e876911ae900019d8a94409d31daebc7fc6a73..3487f868135ec07dae2a5d242919c8b114e9065c 100644 (file)
@@ -197,6 +197,8 @@ namespace canvas
       typedef std::map<std::string, StyleInfo> StyleSetters;
       static StyleSetters       _style_setters;
 
+      static void staticInit();
+
       Element( const CanvasWeakPtr& canvas,
                const SGPropertyNode_ptr& node,
                const Style& parent_style,
@@ -210,7 +212,7 @@ namespace canvas
        * @tparam Derived    (Derived) class type
        */
       template<class Derived>
-      bool isInit() const
+      static bool isInit()
       {
         static bool is_init = false;
         if( is_init )
@@ -239,6 +241,7 @@ namespace canvas
         typename T2,
         class Derived
       >
+      static
       StyleSetter
       addStyle( const std::string& name,
                 const std::string& type,
@@ -282,6 +285,7 @@ namespace canvas
         typename T,
         class Derived
       >
+      static
       StyleSetter
       addStyle( const std::string& name,
                 const std::string& type,
@@ -295,6 +299,7 @@ namespace canvas
         typename T,
         class Derived
       >
+      static
       StyleSetter
       addStyle( const std::string& name,
                 const std::string& type,
@@ -315,6 +320,7 @@ namespace canvas
         typename T2,
         class Derived
       >
+      static
       StyleSetterFunc
       addStyle( const std::string& name,
                 const std::string& type,
@@ -333,6 +339,7 @@ namespace canvas
       template<
         class Derived
       >
+      static
       StyleSetter
       addStyle( const std::string& name,
                 const std::string& type,
@@ -354,6 +361,7 @@ namespace canvas
         class Other,
         class OtherRef
       >
+      static
       StyleSetter
       addStyle( const std::string& name,
                 const std::string& type,
@@ -377,6 +385,7 @@ namespace canvas
         class Other,
         class OtherRef
       >
+      static
       StyleSetter
       addStyle( const std::string& name,
                 const std::string& type,
@@ -400,6 +409,7 @@ namespace canvas
         class Other,
         class OtherRef
       >
+      static
       StyleSetter
       addStyle( const std::string& name,
                 const std::string& type,
@@ -421,6 +431,7 @@ namespace canvas
         class Other,
         class OtherRef
       >
+      static
       StyleSetter
       addStyle( const std::string& name,
                 const std::string& type,
@@ -439,6 +450,7 @@ namespace canvas
       }
 
       template<typename T, class Derived, class Other, class OtherRef>
+      static
       boost::function<void (Derived&, T)>
       bindOther( void (Other::*setter)(T), OtherRef Derived::*instance_ref )
       {
@@ -446,6 +458,7 @@ namespace canvas
       }
 
       template<typename T, class Derived, class Other, class OtherRef>
+      static
       boost::function<void (Derived&, T)>
       bindOther( const boost::function<void (Other&, T)>& setter,
                  OtherRef Derived::*instance_ref )
@@ -463,6 +476,7 @@ namespace canvas
       }
 
       template<typename T1, typename T2, class Derived>
+      static
       StyleSetterFuncUnchecked
       bindStyleSetter( const std::string& name,
                        const boost::function<void (Derived&, T2)>& setter )
index 989d0b386ae5877888d42c2a76bc9f69e0c66a83..51523f426f2391afe196401b201e3847fc3ea6e3 100644 (file)
@@ -38,6 +38,7 @@ namespace canvas
   template<typename ElementType>
   void add(ElementFactories& factories)
   {
+    ElementType::staticInit();
     factories[ElementType::TYPE_NAME] = &Element::create<ElementType>;
   }
 
@@ -52,6 +53,19 @@ namespace canvas
             "canvas::Group::" << member_name << ": Group has expired." );
   }
 
+  //----------------------------------------------------------------------------
+  void Group::staticInit()
+  {
+    if( isInit<Group>() )
+      return;
+
+    add<Group>(_child_factories);
+    add<Image>(_child_factories);
+    add<Map  >(_child_factories);
+    add<Path >(_child_factories);
+    add<Text >(_child_factories);
+  }
+
   //----------------------------------------------------------------------------
   Group::Group( const CanvasWeakPtr& canvas,
                 const SGPropertyNode_ptr& node,
@@ -59,14 +73,7 @@ namespace canvas
                 Element* parent ):
     Element(canvas, node, parent_style, parent)
   {
-    if( !isInit<Group>() )
-    {
-      add<Group>(_child_factories);
-      add<Image>(_child_factories);
-      add<Map  >(_child_factories);
-      add<Path >(_child_factories);
-      add<Text >(_child_factories);
-    }
+    staticInit();
   }
 
   //----------------------------------------------------------------------------
index 5ee417ea3b6e5b30ca11eb857514a5f854c19ca6..74adeafe6a7270511a1bff7b13f33b44162c257c 100644 (file)
@@ -35,6 +35,7 @@ namespace canvas
   {
     public:
       static const std::string TYPE_NAME;
+      static void staticInit();
 
       typedef std::list< std::pair< const SGPropertyNode*,
                                     ElementPtr
index 9f1f57586b502decd808de633880506a6ae6218c..fee83c417c3b6bd0fa47b9967d545cb8300f7223 100644 (file)
@@ -89,6 +89,18 @@ namespace canvas
   //----------------------------------------------------------------------------
   const std::string Image::TYPE_NAME = "image";
 
+  //----------------------------------------------------------------------------
+  void Image::staticInit()
+  {
+    if( isInit<Image>() )
+      return;
+
+    addStyle("fill", "color", &Image::setFill);
+    addStyle("slice", "", &Image::setSlice);
+    addStyle("slice-width", "", &Image::setSliceWidth);
+    addStyle("outset", "", &Image::setOutset);
+  }
+
   //----------------------------------------------------------------------------
   Image::Image( const CanvasWeakPtr& canvas,
                 const SGPropertyNode_ptr& node,
@@ -100,6 +112,8 @@ namespace canvas
     _src_rect(0,0),
     _region(0,0)
   {
+    staticInit();
+
     _geom = new osg::Geometry;
     _geom->setUseDisplayList(false);
 
@@ -128,16 +142,7 @@ namespace canvas
 
     setDrawable(_geom);
 
-    if( !isInit<Image>() )
-    {
-      addStyle("fill", "color", &Image::setFill);
-      addStyle("slice", "", &Image::setSlice);
-      addStyle("slice-width", "", &Image::setSliceWidth);
-      addStyle("outset", "", &Image::setOutset);
-    }
-
     setFill("#ffffff"); // TODO how should we handle default values?
-
     setupStyle();
   }
 
index 491ac19d37df3b7f063bdc030302e91ffc9f02b0..02a21536214bc9f79b5a1937b8d5737276abe078 100644 (file)
@@ -35,6 +35,7 @@ namespace canvas
   {
     public:
       static const std::string TYPE_NAME;
+      static void staticInit();
 
       /**
        * @param node    Property node containing settings for this image:
index f0484ce4295f77f712b09ae6998a1cc030d3ff0f..73ba6b0dfe0ac426b2b9482021a698d1b3f55efb 100644 (file)
@@ -46,6 +46,17 @@ namespace canvas
   const std::string GEO = "-geo";
   const std::string Map::TYPE_NAME = "map";
 
+  //----------------------------------------------------------------------------
+  void Map::staticInit()
+  {
+    Group::staticInit();
+
+    if( isInit<Map>() )
+      return;
+
+    // Do some initialization if needed...
+  }
+
   //----------------------------------------------------------------------------
   Map::Map( const CanvasWeakPtr& canvas,
             const SGPropertyNode_ptr& node,
@@ -56,7 +67,7 @@ namespace canvas
     _projection(new SansonFlamsteedProjection),
     _projection_dirty(true)
   {
-
+    staticInit();
   }
 
   //----------------------------------------------------------------------------
index 57e12f2cb374a6520afe3300c3905f6e2d1ffcc6..6bd2e54d9a5ef43373895e8d7cdd336598b44548 100644 (file)
@@ -36,6 +36,7 @@ namespace canvas
   {
     public:
       static const std::string TYPE_NAME;
+      static void staticInit();
 
       Map( const CanvasWeakPtr& canvas,
            const SGPropertyNode_ptr& node,
index d801c68ac95aff1fe9847aebe197a231581f8b8e..6d4d0bf56846d135c714e1c2aefa367d4fd9efcd 100644 (file)
@@ -472,6 +472,22 @@ namespace canvas
   //----------------------------------------------------------------------------
   const std::string Path::TYPE_NAME = "path";
 
+  //----------------------------------------------------------------------------
+  void Path::staticInit()
+  {
+    if( isInit<Path>() )
+      return;
+
+    PathDrawableRef Path::*path = &Path::_path;
+
+    addStyle("fill", "color", &PathDrawable::setFill, path);
+    addStyle("fill-rule", "", &PathDrawable::setFillRule, path);
+    addStyle("stroke", "color", &PathDrawable::setStroke, path);
+    addStyle("stroke-width", "numeric", &PathDrawable::setStrokeWidth, path);
+    addStyle("stroke-dasharray", "", &PathDrawable::setStrokeDashArray, path);
+    addStyle("stroke-linecap", "", &PathDrawable::setStrokeLinecap, path);
+  }
+
   //----------------------------------------------------------------------------
   Path::Path( const CanvasWeakPtr& canvas,
               const SGPropertyNode_ptr& node,
@@ -480,20 +496,9 @@ namespace canvas
     Element(canvas, node, parent_style, parent),
     _path( new PathDrawable(this) )
   {
-    setDrawable(_path);
-
-    if( !isInit<Path>() )
-    {
-      PathDrawableRef Path::*path = &Path::_path;
-
-      addStyle("fill", "color", &PathDrawable::setFill, path);
-      addStyle("fill-rule", "", &PathDrawable::setFillRule, path);
-      addStyle("stroke", "color", &PathDrawable::setStroke, path);
-      addStyle("stroke-width", "numeric", &PathDrawable::setStrokeWidth, path);
-      addStyle("stroke-dasharray", "", &PathDrawable::setStrokeDashArray, path);
-      addStyle("stroke-linecap", "", &PathDrawable::setStrokeLinecap, path);
-    }
+    staticInit();
 
+    setDrawable(_path);
     setupStyle();
   }
 
index f00e0a60ede400c06d3d942a78f95bcb54152240..9cd3aef589e48385d4e53c7c3bf8dd16ad5a5f14 100644 (file)
@@ -31,6 +31,7 @@ namespace canvas
   {
     public:
       static const std::string TYPE_NAME;
+      static void staticInit();
 
       Path( const CanvasWeakPtr& canvas,
             const SGPropertyNode_ptr& node,
index 615eb05218af82c655c172f7c230669aa2573450..ef6f439337fa2cbdd0ca109cbd11adca6f89697c 100644 (file)
@@ -261,6 +261,39 @@ namespace canvas
   //----------------------------------------------------------------------------
   const std::string Text::TYPE_NAME = "text";
 
+  //----------------------------------------------------------------------------
+  void Text::staticInit()
+  {
+    if( isInit<Text>() )
+      return;
+
+    osg::ref_ptr<TextOSG> Text::*text = &Text::_text;
+
+    addStyle("fill", "color", &TextOSG::setFill, text);
+    addStyle("background", "color", &TextOSG::setBackgroundColor, text);
+    addStyle("character-size",
+             "numeric",
+             static_cast<
+               void (TextOSG::*)(float)
+             > (&TextOSG::setCharacterSize),
+             text);
+    addStyle("character-aspect-ratio",
+             "numeric",
+             &TextOSG::setCharacterAspect, text);
+    addStyle("line-height", "numeric", &TextOSG::setLineHeight, text);
+    addStyle("font-resolution", "numeric", &TextOSG::setFontResolution, text);
+    addStyle("padding", "numeric", &TextOSG::setBoundingBoxMargin, text);
+    //  TEXT              = 1 default
+    //  BOUNDINGBOX       = 2
+    //  FILLEDBOUNDINGBOX = 4
+    //  ALIGNMENT         = 8
+    addStyle<int>("draw-mode", "", &TextOSG::setDrawMode, text);
+    addStyle("max-width", "numeric", &TextOSG::setMaximumWidth, text);
+    addStyle("font", "", &Text::setFont);
+    addStyle("alignment", "", &Text::setAlignment);
+    addStyle("text", "", &Text::setText, false);
+  }
+
   //----------------------------------------------------------------------------
   Text::Text( const CanvasWeakPtr& canvas,
               const SGPropertyNode_ptr& node,
@@ -269,40 +302,13 @@ namespace canvas
     Element(canvas, node, parent_style, parent),
     _text( new Text::TextOSG(this) )
   {
+    staticInit();
+
     setDrawable(_text);
     _text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS);
     _text->setAxisAlignment(osgText::Text::USER_DEFINED_ROTATION);
     _text->setRotation(osg::Quat(osg::PI, osg::X_AXIS));
 
-    if( !isInit<Text>() )
-    {
-      osg::ref_ptr<TextOSG> Text::*text = &Text::_text;
-
-      addStyle("fill", "color", &TextOSG::setFill, text);
-      addStyle("background", "color", &TextOSG::setBackgroundColor, text);
-      addStyle("character-size",
-               "numeric",
-               static_cast<
-                 void (TextOSG::*)(float)
-               > (&TextOSG::setCharacterSize),
-               text);
-      addStyle("character-aspect-ratio",
-               "numeric",
-               &TextOSG::setCharacterAspect, text);
-      addStyle("line-height", "numeric", &TextOSG::setLineHeight, text);
-      addStyle("font-resolution", "numeric", &TextOSG::setFontResolution, text);
-      addStyle("padding", "numeric", &TextOSG::setBoundingBoxMargin, text);
-      //  TEXT              = 1 default
-      //  BOUNDINGBOX       = 2
-      //  FILLEDBOUNDINGBOX = 4
-      //  ALIGNMENT         = 8
-      addStyle<int>("draw-mode", "", &TextOSG::setDrawMode, text);
-      addStyle("max-width", "numeric", &TextOSG::setMaximumWidth, text);
-      addStyle("font", "", &Text::setFont);
-      addStyle("alignment", "", &Text::setAlignment);
-      addStyle("text", "", &Text::setText, false);
-    }
-
     setupStyle();
   }
 
index 1866e6888f0ca7bce3f21de5684be0c15b1ee996..aba96319173d6220be31c8d42b79db502ef3ea7d 100644 (file)
@@ -34,6 +34,7 @@ namespace canvas
   {
     public:
       static const std::string TYPE_NAME;
+      static void staticInit();
 
       Text( const CanvasWeakPtr& canvas,
             const SGPropertyNode_ptr& node,