From: Thomas Geymayer Date: Thu, 8 Nov 2012 23:04:50 +0000 (+0100) Subject: Canvas cleanup and restructuring X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=45ac5cb2fa738afa3a50b0b03ece5358f3a31e74;p=simgear.git Canvas cleanup and restructuring - Add some methods for easier using the Canvas from C++ - Add some documentation to Nasal --- diff --git a/simgear/canvas/Canvas.cxx b/simgear/canvas/Canvas.cxx index c998f710..350fdf95 100644 --- a/simgear/canvas/Canvas.cxx +++ b/simgear/canvas/Canvas.cxx @@ -135,6 +135,15 @@ namespace canvas _dependent_canvases.erase(canvas); } + //---------------------------------------------------------------------------- + GroupPtr Canvas::createGroup(const std::string& name) + { + return boost::dynamic_pointer_cast + ( + _root_group->createChild("group", name) + ); + } + //---------------------------------------------------------------------------- void Canvas::enableRendering(bool force) { diff --git a/simgear/canvas/Canvas.hxx b/simgear/canvas/Canvas.hxx index 7f0ffb88..11c832c9 100644 --- a/simgear/canvas/Canvas.hxx +++ b/simgear/canvas/Canvas.hxx @@ -91,6 +91,8 @@ namespace canvas */ void removeDependentCanvas(const CanvasWeakPtr& canvas); + GroupPtr createGroup(const std::string& name = ""); + /** * Enable rendering for the next frame * diff --git a/simgear/canvas/CanvasMgr.cxx b/simgear/canvas/CanvasMgr.cxx index ff39bd8e..de1d5400 100644 --- a/simgear/canvas/CanvasMgr.cxx +++ b/simgear/canvas/CanvasMgr.cxx @@ -44,13 +44,15 @@ namespace canvas } //---------------------------------------------------------------------------- - CanvasPtr CanvasMgr::getCanvas(size_t index) const + CanvasPtr CanvasMgr::createCanvas(const std::string& name) { - if( index >= _elements.size() - || !_elements[index] ) - return CanvasPtr(); + return boost::static_pointer_cast( createElement(name) ); + } - return boost::static_pointer_cast(_elements[index]); + //---------------------------------------------------------------------------- + CanvasPtr CanvasMgr::getCanvas(size_t index) const + { + return boost::static_pointer_cast( getElement(index) ); } //---------------------------------------------------------------------------- diff --git a/simgear/canvas/CanvasMgr.hxx b/simgear/canvas/CanvasMgr.hxx index 5e51d2df..cc67b4fd 100644 --- a/simgear/canvas/CanvasMgr.hxx +++ b/simgear/canvas/CanvasMgr.hxx @@ -41,6 +41,13 @@ namespace canvas CanvasMgr( SGPropertyNode_ptr node, SystemAdapterPtr system_adapter ); + /** + * Create a new canvas + * + * @param name Name of the new canvas + */ + CanvasPtr createCanvas(const std::string& name = ""); + /** * Get ::Canvas by index * diff --git a/simgear/canvas/canvas_fwd.hxx b/simgear/canvas/canvas_fwd.hxx index 802a8827..0eb70b87 100644 --- a/simgear/canvas/canvas_fwd.hxx +++ b/simgear/canvas/canvas_fwd.hxx @@ -37,13 +37,23 @@ namespace simgear namespace canvas { - class Canvas; - typedef boost::shared_ptr CanvasPtr; - typedef boost::weak_ptr CanvasWeakPtr; +#define SG_FWD_DECL(name)\ + class name;\ + typedef boost::shared_ptr name##Ptr;\ + typedef boost::weak_ptr name##WeakPtr; - class Element; - typedef boost::shared_ptr ElementPtr; - typedef boost::weak_ptr ElementWeakPtr; + SG_FWD_DECL(Canvas) + SG_FWD_DECL(Element) + SG_FWD_DECL(Group) + SG_FWD_DECL(Image) + SG_FWD_DECL(Map) + SG_FWD_DECL(Path) + SG_FWD_DECL(Text) + + SG_FWD_DECL(Placement) + SG_FWD_DECL(SystemAdapter) + +#undef SG_FWD_DECL typedef std::map Style; typedef ElementPtr (*ElementFactory)( const CanvasWeakPtr&, @@ -52,15 +62,10 @@ namespace canvas typedef osg::ref_ptr FontPtr; - class Placement; - typedef boost::shared_ptr PlacementPtr; typedef std::vector Placements; typedef boost::function PlacementFactory; - class SystemAdapter; - typedef boost::shared_ptr SystemAdapterPtr; - } // namespace canvas } // namespace simgear diff --git a/simgear/canvas/elements/CanvasGroup.cxx b/simgear/canvas/elements/CanvasGroup.cxx index 375c171f..aa8c45c5 100644 --- a/simgear/canvas/elements/CanvasGroup.cxx +++ b/simgear/canvas/elements/CanvasGroup.cxx @@ -22,7 +22,9 @@ #include "CanvasPath.hxx" #include "CanvasText.hxx" +#include #include +#include namespace simgear { @@ -58,6 +60,27 @@ namespace canvas } + //---------------------------------------------------------------------------- + ElementPtr Group::createChild( const std::string& type, + const std::string& name ) + { + SGPropertyNode* node = _node->addChild(type, 0, false); + if( !name.empty() ) + node->setStringValue("name", name); + + return getChild(node); + } + + //---------------------------------------------------------------------------- + ElementPtr Group::getChild(const SGPropertyNode* node) + { + ChildList::iterator child = findChild(node); + if( child == _children.end() ) + return ElementPtr(); + + return child->second; + } + //---------------------------------------------------------------------------- void Group::update(double dt) { @@ -101,23 +124,6 @@ namespace canvas _style[ child->getNameString() ] = child; } - //---------------------------------------------------------------------------- - struct ChildFinder - { - public: - ChildFinder(SGPropertyNode *node): - _node(node) - {} - - bool operator()(const Group::ChildList::value_type& el) const - { - return el.first == _node; - } - - private: - SGPropertyNode *_node; - }; - //---------------------------------------------------------------------------- void Group::childRemoved(SGPropertyNode* node) { @@ -126,10 +132,7 @@ namespace canvas if( _child_factories.find(node->getNameString()) != _child_factories.end() ) { - ChildFinder pred(node); - ChildList::iterator child = - std::find_if(_children.begin(), _children.end(), pred); - + ChildList::iterator child = findChild(node); if( child == _children.end() ) SG_LOG ( @@ -162,10 +165,7 @@ namespace canvas //---------------------------------------------------------------------------- void Group::handleZIndexChanged(SGPropertyNode* node, int z_index) { - ChildFinder pred(node); - ChildList::iterator child = - std::find_if(_children.begin(), _children.end(), pred); - + ChildList::iterator child = findChild(node); if( child == _children.end() ) return; @@ -215,5 +215,16 @@ namespace canvas ); } + //---------------------------------------------------------------------------- + Group::ChildList::iterator Group::findChild(const SGPropertyNode* node) + { + return std::find_if + ( + _children.begin(), + _children.end(), + boost::bind(&Group::ChildList::value_type::first, _1) == node + ); + } + } // namespace canvas } // namespace simgear diff --git a/simgear/canvas/elements/CanvasGroup.hxx b/simgear/canvas/elements/CanvasGroup.hxx index 5be3bed4..0cac8a1c 100644 --- a/simgear/canvas/elements/CanvasGroup.hxx +++ b/simgear/canvas/elements/CanvasGroup.hxx @@ -42,6 +42,10 @@ namespace canvas const Style& parent_style = Style() ); virtual ~Group(); + ElementPtr createChild( const std::string& type, + const std::string& name = "" ); + ElementPtr getChild(const SGPropertyNode* node); + virtual void update(double dt); protected: @@ -58,6 +62,8 @@ namespace canvas virtual void childChanged(SGPropertyNode * child); void handleZIndexChanged(SGPropertyNode* node, int z_index); + + ChildList::iterator findChild(const SGPropertyNode* node); }; } // namespace canvas diff --git a/simgear/nasal/nasal.h b/simgear/nasal/nasal.h index 3c5140e0..bf48cdc2 100644 --- a/simgear/nasal/nasal.h +++ b/simgear/nasal/nasal.h @@ -175,6 +175,11 @@ naRef naHash_cget(naRef hash, char* key); void naHash_set(naRef hash, naRef key, naRef val); void naHash_cset(naRef hash, char* key, naRef val); void naHash_delete(naRef hash, naRef key); +/** + * Store the keys in ::hash into the vector at ::dst + * + * @see ::naNewVector + */ void naHash_keys(naRef dst, naRef hash); // Ghost utilities: @@ -185,7 +190,16 @@ typedef struct naGhostType { void(*set_member)(naContext c, void*, naRef key, naRef val); } naGhostType; +/** + * Create a ghost for an object without any attributes. If ::t contains pointers + * to get_member or set_member function they will be ignored. + */ naRef naNewGhost(naContext c, naGhostType* t, void* ghost); +/** + * Create a ghost for an object. This version uses the get_member and set_member + * function pointers in ::t upon trying to get or set a member respectively from + * Nasal. + */ naRef naNewGhost2(naContext c, naGhostType* t, void* ghost); naGhostType* naGhost_type(naRef ghost); void* naGhost_ptr(naRef ghost); diff --git a/simgear/props/PropertyBasedMgr.cxx b/simgear/props/PropertyBasedMgr.cxx index 397aa2b6..b3d3ed74 100644 --- a/simgear/props/PropertyBasedMgr.cxx +++ b/simgear/props/PropertyBasedMgr.cxx @@ -45,6 +45,49 @@ namespace simgear _elements[i]->update(delta_time_sec); } + //---------------------------------------------------------------------------- + PropertyBasedElementPtr + PropertyBasedMgr::createElement(const std::string& name) + { + SGPropertyNode* node = _props->addChild(_name_elements, 0, false); + if( !name.empty() ) + node->setStringValue("name", name); + + return getElement( node->getIndex() ); + } + + //---------------------------------------------------------------------------- + PropertyBasedElementPtr PropertyBasedMgr::getElement(size_t index) const + { + if( index >= _elements.size() ) + return PropertyBasedElementPtr(); + + return _elements[index]; + } + + //---------------------------------------------------------------------------- + const SGPropertyNode* PropertyBasedMgr::getPropertyRoot() const + { + return _props; + } + + //---------------------------------------------------------------------------- + PropertyBasedMgr::PropertyBasedMgr( SGPropertyNode_ptr props, + const std::string& name_elements, + ElementFactory element_factory ): + _props( props ), + _name_elements( name_elements ), + _element_factory( element_factory ) + { + + } + + //---------------------------------------------------------------------------- + PropertyBasedMgr::~PropertyBasedMgr() + { + + } + //---------------------------------------------------------------------------- void PropertyBasedMgr::childAdded( SGPropertyNode * parent, SGPropertyNode * child ) @@ -103,27 +146,4 @@ namespace simgear _elements[index].reset(); } - //---------------------------------------------------------------------------- - const SGPropertyNode* PropertyBasedMgr::getPropertyRoot() const - { - return _props; - } - - //---------------------------------------------------------------------------- - PropertyBasedMgr::PropertyBasedMgr( SGPropertyNode_ptr props, - const std::string& name_elements, - ElementFactory element_factory ): - _props( props ), - _name_elements( name_elements ), - _element_factory( element_factory ) - { - - } - - //---------------------------------------------------------------------------- - PropertyBasedMgr::~PropertyBasedMgr() - { - - } - } // namespace simgear diff --git a/simgear/props/PropertyBasedMgr.hxx b/simgear/props/PropertyBasedMgr.hxx index 6246822a..0abe9b83 100644 --- a/simgear/props/PropertyBasedMgr.hxx +++ b/simgear/props/PropertyBasedMgr.hxx @@ -39,12 +39,19 @@ namespace simgear virtual void update (double delta_time_sec); - virtual void childAdded( SGPropertyNode * parent, - SGPropertyNode * child ); - virtual void childRemoved( SGPropertyNode * parent, - SGPropertyNode * child ); + /** + * Create a new PropertyBasedElement + * + * @param name Name of the new element + */ + PropertyBasedElementPtr createElement(const std::string& name = ""); - virtual void elementCreated(PropertyBasedElementPtr element) {} + /** + * Get an existing PropertyBasedElement by its index + * + * @param index Index of element node in property tree + */ + PropertyBasedElementPtr getElement(size_t index) const; virtual const SGPropertyNode* getPropertyRoot() const; @@ -75,6 +82,13 @@ namespace simgear ElementFactory element_factory ); virtual ~PropertyBasedMgr() = 0; + virtual void childAdded( SGPropertyNode * parent, + SGPropertyNode * child ); + virtual void childRemoved( SGPropertyNode * parent, + SGPropertyNode * child ); + + virtual void elementCreated(PropertyBasedElementPtr element) {} + }; } // namespace simgear