More helper methods for Canvas and PropertyBasedElement.

This commit is contained in:
Thomas Geymayer
2013-06-02 21:19:37 +02:00
parent 7a8d796ac1
commit 2a6c50c893
6 changed files with 42 additions and 0 deletions

View File

@@ -177,6 +177,15 @@ namespace canvas
);
}
//----------------------------------------------------------------------------
GroupPtr Canvas::getGroup(const std::string& name)
{
return boost::dynamic_pointer_cast<Group>
(
_root_group->getChild(name)
);
}
//----------------------------------------------------------------------------
GroupPtr Canvas::getRootGroup()
{

View File

@@ -103,6 +103,7 @@ namespace canvas
void removeChildCanvas(const CanvasWeakPtr& canvas);
GroupPtr createGroup(const std::string& name = "");
GroupPtr getGroup(const std::string& name);
GroupPtr getRootGroup();
/**

View File

@@ -93,6 +93,20 @@ namespace canvas
return child->second;
}
//----------------------------------------------------------------------------
ElementPtr Group::getChild(const std::string& id)
{
for( ChildList::iterator child = _children.begin();
child != _children.end();
++child )
{
if( child->second->get<std::string>("id") == id )
return child->second;
}
return ElementPtr();
}
//----------------------------------------------------------------------------
ElementPtr Group::getElementById(const std::string& id)
{

View File

@@ -48,6 +48,7 @@ namespace canvas
ElementPtr createChild( const std::string& type,
const std::string& id = "" );
ElementPtr getChild(const SGPropertyNode* node);
ElementPtr getChild(const std::string& id);
/**
* Get first child with given id (breadth-first search)

View File

@@ -55,6 +55,17 @@ namespace simgear
setValue(_node->getNode(name, true), val);
}
template<class T>
T get( const std::string& name,
typename boost::call_traits<T>::param_type def = T() )
{
SGPropertyNode const* child = _node->getNode(name);
if( !child )
return def;
return getValue<T>(child);
}
virtual void setSelf(const PropertyBasedElementPtr& self);
protected:

View File

@@ -1796,6 +1796,12 @@ inline const char * getValue<const char*>(const SGPropertyNode* node)
return node->getStringValue ();
}
template<>
inline std::string getValue<std::string>(const SGPropertyNode* node)
{
return node->getStringValue();
}
inline bool setValue(SGPropertyNode* node, bool value)
{
return node->setBoolValue(value);