From: Thomas Geymayer Date: Sat, 20 Jul 2013 13:45:02 +0000 (+0200) Subject: Canvas: Ensure all element types are initialized before first usage. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=b2368216618c5742fd29040b24a83e0b10bef06e;p=simgear.git Canvas: Ensure all element types are initialized before first usage. 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. --- diff --git a/simgear/canvas/elements/CanvasElement.cxx b/simgear/canvas/elements/CanvasElement.cxx index 319c07d2..c4612659 100644 --- a/simgear/canvas/elements/CanvasElement.cxx +++ b/simgear/canvas/elements/CanvasElement.cxx @@ -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() ) - { - 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() ) + return; + + addStyle("clip", "", &Element::setClip, false); + } + //---------------------------------------------------------------------------- bool Element::isStyleEmpty(const SGPropertyNode* child) const { diff --git a/simgear/canvas/elements/CanvasElement.hxx b/simgear/canvas/elements/CanvasElement.hxx index 07e87691..3487f868 100644 --- a/simgear/canvas/elements/CanvasElement.hxx +++ b/simgear/canvas/elements/CanvasElement.hxx @@ -197,6 +197,8 @@ namespace canvas typedef std::map 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 - 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 + static boost::function bindOther( void (Other::*setter)(T), OtherRef Derived::*instance_ref ) { @@ -446,6 +458,7 @@ namespace canvas } template + static boost::function bindOther( const boost::function& setter, OtherRef Derived::*instance_ref ) @@ -463,6 +476,7 @@ namespace canvas } template + static StyleSetterFuncUnchecked bindStyleSetter( const std::string& name, const boost::function& setter ) diff --git a/simgear/canvas/elements/CanvasGroup.cxx b/simgear/canvas/elements/CanvasGroup.cxx index 989d0b38..51523f42 100644 --- a/simgear/canvas/elements/CanvasGroup.cxx +++ b/simgear/canvas/elements/CanvasGroup.cxx @@ -38,6 +38,7 @@ namespace canvas template void add(ElementFactories& factories) { + ElementType::staticInit(); factories[ElementType::TYPE_NAME] = &Element::create; } @@ -52,6 +53,19 @@ namespace canvas "canvas::Group::" << member_name << ": Group has expired." ); } + //---------------------------------------------------------------------------- + void Group::staticInit() + { + if( isInit() ) + return; + + add(_child_factories); + add(_child_factories); + add(_child_factories); + add(_child_factories); + add(_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() ) - { - add(_child_factories); - add(_child_factories); - add(_child_factories); - add(_child_factories); - add(_child_factories); - } + staticInit(); } //---------------------------------------------------------------------------- diff --git a/simgear/canvas/elements/CanvasGroup.hxx b/simgear/canvas/elements/CanvasGroup.hxx index 5ee417ea..74adeafe 100644 --- a/simgear/canvas/elements/CanvasGroup.hxx +++ b/simgear/canvas/elements/CanvasGroup.hxx @@ -35,6 +35,7 @@ namespace canvas { public: static const std::string TYPE_NAME; + static void staticInit(); typedef std::list< std::pair< const SGPropertyNode*, ElementPtr diff --git a/simgear/canvas/elements/CanvasImage.cxx b/simgear/canvas/elements/CanvasImage.cxx index 9f1f5758..fee83c41 100644 --- a/simgear/canvas/elements/CanvasImage.cxx +++ b/simgear/canvas/elements/CanvasImage.cxx @@ -89,6 +89,18 @@ namespace canvas //---------------------------------------------------------------------------- const std::string Image::TYPE_NAME = "image"; + //---------------------------------------------------------------------------- + void Image::staticInit() + { + if( isInit() ) + 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() ) - { - 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(); } diff --git a/simgear/canvas/elements/CanvasImage.hxx b/simgear/canvas/elements/CanvasImage.hxx index 491ac19d..02a21536 100644 --- a/simgear/canvas/elements/CanvasImage.hxx +++ b/simgear/canvas/elements/CanvasImage.hxx @@ -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: diff --git a/simgear/canvas/elements/CanvasMap.cxx b/simgear/canvas/elements/CanvasMap.cxx index f0484ce4..73ba6b0d 100644 --- a/simgear/canvas/elements/CanvasMap.cxx +++ b/simgear/canvas/elements/CanvasMap.cxx @@ -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() ) + 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(); } //---------------------------------------------------------------------------- diff --git a/simgear/canvas/elements/CanvasMap.hxx b/simgear/canvas/elements/CanvasMap.hxx index 57e12f2c..6bd2e54d 100644 --- a/simgear/canvas/elements/CanvasMap.hxx +++ b/simgear/canvas/elements/CanvasMap.hxx @@ -36,6 +36,7 @@ namespace canvas { public: static const std::string TYPE_NAME; + static void staticInit(); Map( const CanvasWeakPtr& canvas, const SGPropertyNode_ptr& node, diff --git a/simgear/canvas/elements/CanvasPath.cxx b/simgear/canvas/elements/CanvasPath.cxx index d801c68a..6d4d0bf5 100644 --- a/simgear/canvas/elements/CanvasPath.cxx +++ b/simgear/canvas/elements/CanvasPath.cxx @@ -472,6 +472,22 @@ namespace canvas //---------------------------------------------------------------------------- const std::string Path::TYPE_NAME = "path"; + //---------------------------------------------------------------------------- + void Path::staticInit() + { + if( isInit() ) + 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() ) - { - 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(); } diff --git a/simgear/canvas/elements/CanvasPath.hxx b/simgear/canvas/elements/CanvasPath.hxx index f00e0a60..9cd3aef5 100644 --- a/simgear/canvas/elements/CanvasPath.hxx +++ b/simgear/canvas/elements/CanvasPath.hxx @@ -31,6 +31,7 @@ namespace canvas { public: static const std::string TYPE_NAME; + static void staticInit(); Path( const CanvasWeakPtr& canvas, const SGPropertyNode_ptr& node, diff --git a/simgear/canvas/elements/CanvasText.cxx b/simgear/canvas/elements/CanvasText.cxx index 615eb052..ef6f4393 100644 --- a/simgear/canvas/elements/CanvasText.cxx +++ b/simgear/canvas/elements/CanvasText.cxx @@ -261,6 +261,39 @@ namespace canvas //---------------------------------------------------------------------------- const std::string Text::TYPE_NAME = "text"; + //---------------------------------------------------------------------------- + void Text::staticInit() + { + if( isInit() ) + return; + + osg::ref_ptr 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("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() ) - { - osg::ref_ptr 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("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(); } diff --git a/simgear/canvas/elements/CanvasText.hxx b/simgear/canvas/elements/CanvasText.hxx index 1866e688..aba96319 100644 --- a/simgear/canvas/elements/CanvasText.hxx +++ b/simgear/canvas/elements/CanvasText.hxx @@ -34,6 +34,7 @@ namespace canvas { public: static const std::string TYPE_NAME; + static void staticInit(); Text( const CanvasWeakPtr& canvas, const SGPropertyNode_ptr& node,