_dependent_canvases.erase(canvas);
}
+ //----------------------------------------------------------------------------
+ GroupPtr Canvas::createGroup(const std::string& name)
+ {
+ return boost::dynamic_pointer_cast<Group>
+ (
+ _root_group->createChild("group", name)
+ );
+ }
+
//----------------------------------------------------------------------------
void Canvas::enableRendering(bool force)
{
*/
void removeDependentCanvas(const CanvasWeakPtr& canvas);
+ GroupPtr createGroup(const std::string& name = "");
+
/**
* Enable rendering for the next frame
*
}
//----------------------------------------------------------------------------
- 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<Canvas>( createElement(name) );
+ }
- return boost::static_pointer_cast<Canvas>(_elements[index]);
+ //----------------------------------------------------------------------------
+ CanvasPtr CanvasMgr::getCanvas(size_t index) const
+ {
+ return boost::static_pointer_cast<Canvas>( getElement(index) );
}
//----------------------------------------------------------------------------
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
*
namespace canvas
{
- class Canvas;
- typedef boost::shared_ptr<Canvas> CanvasPtr;
- typedef boost::weak_ptr<Canvas> CanvasWeakPtr;
+#define SG_FWD_DECL(name)\
+ class name;\
+ typedef boost::shared_ptr<name> name##Ptr;\
+ typedef boost::weak_ptr<name> name##WeakPtr;
- class Element;
- typedef boost::shared_ptr<Element> ElementPtr;
- typedef boost::weak_ptr<Element> 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<std::string, const SGPropertyNode*> Style;
typedef ElementPtr (*ElementFactory)( const CanvasWeakPtr&,
typedef osg::ref_ptr<osgText::Font> FontPtr;
- class Placement;
- typedef boost::shared_ptr<Placement> PlacementPtr;
typedef std::vector<PlacementPtr> Placements;
typedef boost::function<Placements( const SGPropertyNode*,
CanvasPtr )> PlacementFactory;
- class SystemAdapter;
- typedef boost::shared_ptr<SystemAdapter> SystemAdapterPtr;
-
} // namespace canvas
} // namespace simgear
#include "CanvasPath.hxx"
#include "CanvasText.hxx"
+#include <boost/bind.hpp>
#include <boost/foreach.hpp>
+#include <boost/lambda/core.hpp>
namespace simgear
{
}
+ //----------------------------------------------------------------------------
+ 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)
{
_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)
{
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
(
//----------------------------------------------------------------------------
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;
);
}
+ //----------------------------------------------------------------------------
+ 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
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:
virtual void childChanged(SGPropertyNode * child);
void handleZIndexChanged(SGPropertyNode* node, int z_index);
+
+ ChildList::iterator findChild(const SGPropertyNode* node);
};
} // namespace canvas
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:
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);
_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 )
_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
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;
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