From: Thomas Geymayer Date: Fri, 7 Jun 2013 14:39:03 +0000 (+0200) Subject: Canvas: More helper functions and cleanup. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=d24d3ce487cad4af7f07b392bde2a936eb4b96a8;p=simgear.git Canvas: More helper functions and cleanup. --- diff --git a/simgear/canvas/Canvas.cxx b/simgear/canvas/Canvas.cxx index a124642c..37877d5f 100644 --- a/simgear/canvas/Canvas.cxx +++ b/simgear/canvas/Canvas.cxx @@ -173,29 +173,19 @@ namespace canvas //---------------------------------------------------------------------------- GroupPtr Canvas::createGroup(const std::string& name) { - return boost::dynamic_pointer_cast - ( - _root_group->createChild("group", name) - ); + return _root_group->createChild(name); } //---------------------------------------------------------------------------- GroupPtr Canvas::getGroup(const std::string& name) { - return boost::dynamic_pointer_cast - ( - _root_group->getChild(name) - ); + return _root_group->getChild(name); } //---------------------------------------------------------------------------- GroupPtr Canvas::getOrCreateGroup(const std::string& name) { - GroupPtr group = getGroup(name); - if( group ) - return group; - - return createGroup(name); + return _root_group->getOrCreateChild(name); } //---------------------------------------------------------------------------- diff --git a/simgear/canvas/elements/CanvasGroup.cxx b/simgear/canvas/elements/CanvasGroup.cxx index 3d29c77e..4b6591f9 100644 --- a/simgear/canvas/elements/CanvasGroup.cxx +++ b/simgear/canvas/elements/CanvasGroup.cxx @@ -46,8 +46,18 @@ namespace canvas return el; } + /** + * Add canvas Element type to factory map + */ + template + void add(ElementFactories& factories) + { + factories[T::TYPE_NAME] = &createElement; + } + //---------------------------------------------------------------------------- ElementFactories Group::_child_factories; + const std::string Group::TYPE_NAME = "group"; //---------------------------------------------------------------------------- Group::Group( const CanvasWeakPtr& canvas, @@ -58,11 +68,11 @@ namespace canvas { if( !isInit() ) { - _child_factories["group"] = &createElement; - _child_factories["image"] = &createElement; - _child_factories["map" ] = &createElement; - _child_factories["path" ] = &createElement; - _child_factories["text" ] = &createElement; + add(_child_factories); + add(_child_factories); + add(_child_factories); + add(_child_factories); + add(_child_factories); } } @@ -107,6 +117,31 @@ namespace canvas return ElementPtr(); } + //---------------------------------------------------------------------------- + ElementPtr Group::getOrCreateChild( const std::string& type, + const std::string& id ) + { + ElementPtr child = getChild(id); + if( child ) + { + if( child->getProps()->getNameString() == type ) + return child; + + SG_LOG + ( + SG_GENERAL, + SG_WARN, + "Group::getOrCreateChild: type missmatch! " + "('" << type << "' != '" << child->getProps()->getName() << "', " + "id = '" << id << "')" + ); + + return ElementPtr(); + } + + return createChild(type, id); + } + //---------------------------------------------------------------------------- ElementPtr Group::getElementById(const std::string& id) { diff --git a/simgear/canvas/elements/CanvasGroup.hxx b/simgear/canvas/elements/CanvasGroup.hxx index 8a40eaab..43fa3681 100644 --- a/simgear/canvas/elements/CanvasGroup.hxx +++ b/simgear/canvas/elements/CanvasGroup.hxx @@ -34,6 +34,8 @@ namespace canvas public Element { public: + static const std::string TYPE_NAME; + typedef std::list< std::pair< const SGPropertyNode*, ElementPtr > @@ -49,6 +51,33 @@ namespace canvas const std::string& id = "" ); ElementPtr getChild(const SGPropertyNode* node); ElementPtr getChild(const std::string& id); + ElementPtr getOrCreateChild( const std::string& type, + const std::string& id ); + + template + boost::shared_ptr createChild(const std::string& id = "") + { + return boost::dynamic_pointer_cast( createChild(T::TYPE_NAME, id) ); + } + + template + boost::shared_ptr getChild(const SGPropertyNode* node) + { + return boost::dynamic_pointer_cast( getChild(node) ); + } + + template + boost::shared_ptr getChild(const std::string& id) + { + return boost::dynamic_pointer_cast( getChild(id) ); + } + + template + boost::shared_ptr getOrCreateChild(const std::string& id) + { + return + boost::dynamic_pointer_cast( getOrCreateChild(T::TYPE_NAME, id) ); + } /** * Get first child with given id (breadth-first search) diff --git a/simgear/canvas/elements/CanvasImage.cxx b/simgear/canvas/elements/CanvasImage.cxx index bd3e7403..077d0042 100644 --- a/simgear/canvas/elements/CanvasImage.cxx +++ b/simgear/canvas/elements/CanvasImage.cxx @@ -86,6 +86,9 @@ namespace canvas return true; } + //---------------------------------------------------------------------------- + const std::string Image::TYPE_NAME = "image"; + //---------------------------------------------------------------------------- Image::Image( const CanvasWeakPtr& canvas, const SGPropertyNode_ptr& node, diff --git a/simgear/canvas/elements/CanvasImage.hxx b/simgear/canvas/elements/CanvasImage.hxx index 741d74d8..491ac19d 100644 --- a/simgear/canvas/elements/CanvasImage.hxx +++ b/simgear/canvas/elements/CanvasImage.hxx @@ -34,6 +34,8 @@ namespace canvas public Element { public: + static const std::string TYPE_NAME; + /** * @param node Property node containing settings for this image: * rect/[left/right/top/bottom] Dimensions of source diff --git a/simgear/canvas/elements/CanvasMap.cxx b/simgear/canvas/elements/CanvasMap.cxx index 184ac819..f0484ce4 100644 --- a/simgear/canvas/elements/CanvasMap.cxx +++ b/simgear/canvas/elements/CanvasMap.cxx @@ -42,7 +42,9 @@ namespace simgear namespace canvas { + //---------------------------------------------------------------------------- const std::string GEO = "-geo"; + const std::string Map::TYPE_NAME = "map"; //---------------------------------------------------------------------------- Map::Map( const CanvasWeakPtr& canvas, diff --git a/simgear/canvas/elements/CanvasMap.hxx b/simgear/canvas/elements/CanvasMap.hxx index dda9c482..57e12f2c 100644 --- a/simgear/canvas/elements/CanvasMap.hxx +++ b/simgear/canvas/elements/CanvasMap.hxx @@ -35,6 +35,8 @@ namespace canvas public Group { public: + static const std::string TYPE_NAME; + Map( const CanvasWeakPtr& canvas, const SGPropertyNode_ptr& node, const Style& parent_style, diff --git a/simgear/canvas/elements/CanvasPath.cxx b/simgear/canvas/elements/CanvasPath.cxx index e2281905..1580285e 100644 --- a/simgear/canvas/elements/CanvasPath.cxx +++ b/simgear/canvas/elements/CanvasPath.cxx @@ -469,6 +469,9 @@ namespace canvas }; }; + //---------------------------------------------------------------------------- + const std::string Path::TYPE_NAME = "path"; + //---------------------------------------------------------------------------- Path::Path( const CanvasWeakPtr& canvas, const SGPropertyNode_ptr& node, diff --git a/simgear/canvas/elements/CanvasPath.hxx b/simgear/canvas/elements/CanvasPath.hxx index de7c6524..f00e0a60 100644 --- a/simgear/canvas/elements/CanvasPath.hxx +++ b/simgear/canvas/elements/CanvasPath.hxx @@ -30,6 +30,8 @@ namespace canvas public Element { public: + static const std::string TYPE_NAME; + Path( const CanvasWeakPtr& canvas, const SGPropertyNode_ptr& node, const Style& parent_style, diff --git a/simgear/canvas/elements/CanvasText.cxx b/simgear/canvas/elements/CanvasText.cxx index 469b19aa..38f11611 100644 --- a/simgear/canvas/elements/CanvasText.cxx +++ b/simgear/canvas/elements/CanvasText.cxx @@ -244,6 +244,9 @@ namespace canvas return osgText::Text::computePositions(contextID); } + //---------------------------------------------------------------------------- + const std::string Text::TYPE_NAME = "text"; + //---------------------------------------------------------------------------- Text::Text( const CanvasWeakPtr& canvas, const SGPropertyNode_ptr& node, diff --git a/simgear/canvas/elements/CanvasText.hxx b/simgear/canvas/elements/CanvasText.hxx index 7e90c453..1866e688 100644 --- a/simgear/canvas/elements/CanvasText.hxx +++ b/simgear/canvas/elements/CanvasText.hxx @@ -33,6 +33,8 @@ namespace canvas public Element { public: + static const std::string TYPE_NAME; + Text( const CanvasWeakPtr& canvas, const SGPropertyNode_ptr& node, const Style& parent_style,