Canvas cleanup and restructuring
- Add some methods for easier using the Canvas from C++ - Add some documentation to Nasal
This commit is contained in:
@@ -135,6 +135,15 @@ namespace canvas
|
||||
_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)
|
||||
{
|
||||
|
||||
@@ -91,6 +91,8 @@ namespace canvas
|
||||
*/
|
||||
void removeDependentCanvas(const CanvasWeakPtr& canvas);
|
||||
|
||||
GroupPtr createGroup(const std::string& name = "");
|
||||
|
||||
/**
|
||||
* Enable rendering for the next frame
|
||||
*
|
||||
|
||||
@@ -43,14 +43,16 @@ namespace canvas
|
||||
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
CanvasPtr CanvasMgr::createCanvas(const std::string& name)
|
||||
{
|
||||
return boost::static_pointer_cast<Canvas>( createElement(name) );
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
CanvasPtr CanvasMgr::getCanvas(size_t index) const
|
||||
{
|
||||
if( index >= _elements.size()
|
||||
|| !_elements[index] )
|
||||
return CanvasPtr();
|
||||
|
||||
return boost::static_pointer_cast<Canvas>(_elements[index]);
|
||||
return boost::static_pointer_cast<Canvas>( getElement(index) );
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
|
||||
@@ -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
|
||||
*
|
||||
|
||||
@@ -37,13 +37,23 @@ namespace simgear
|
||||
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&,
|
||||
@@ -52,15 +62,10 @@ namespace canvas
|
||||
|
||||
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
|
||||
|
||||
|
||||
@@ -22,7 +22,9 @@
|
||||
#include "CanvasPath.hxx"
|
||||
#include "CanvasText.hxx"
|
||||
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/foreach.hpp>
|
||||
#include <boost/lambda/core.hpp>
|
||||
|
||||
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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user