]> git.mxchange.org Git - simgear.git/commitdiff
Canvas cleanup and restructuring
authorThomas Geymayer <tomgey@gmail.com>
Thu, 8 Nov 2012 23:04:50 +0000 (00:04 +0100)
committerThomas Geymayer <tomgey@gmail.com>
Thu, 8 Nov 2012 23:05:11 +0000 (00:05 +0100)
 - Add some methods for easier using the Canvas from C++
 - Add some documentation to Nasal

simgear/canvas/Canvas.cxx
simgear/canvas/Canvas.hxx
simgear/canvas/CanvasMgr.cxx
simgear/canvas/CanvasMgr.hxx
simgear/canvas/canvas_fwd.hxx
simgear/canvas/elements/CanvasGroup.cxx
simgear/canvas/elements/CanvasGroup.hxx
simgear/nasal/nasal.h
simgear/props/PropertyBasedMgr.cxx
simgear/props/PropertyBasedMgr.hxx

index c998f71016677174f262734ed1c5bc19aa307df6..350fdf9563edec6521d2e888c122a80aa91720b6 100644 (file)
@@ -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)
   {
index 7f0ffb888f6a9ee5463d64278b81958c8b26b02e..11c832c943bbcc501f735ea9c50d4eb43174091e 100644 (file)
@@ -91,6 +91,8 @@ namespace canvas
        */
       void removeDependentCanvas(const CanvasWeakPtr& canvas);
 
+      GroupPtr createGroup(const std::string& name = "");
+
       /**
        * Enable rendering for the next frame
        *
index ff39bd8e841dd628e23e9dbe2d7e45708631dab1..de1d5400474d290fb191ae1c3e95e42c00473a48 100644 (file)
@@ -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<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) );
   }
 
   //----------------------------------------------------------------------------
index 5e51d2dfaee86509100eea2c0043ab95f09ac87f..cc67b4fd288a6be80f15d9639330d54cd93fa32f 100644 (file)
@@ -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
        *
index 802a882761f127cd5176f8a8133624d9117f46ec..0eb70b87dea1b10554ea2970e1ab2c9e9a1d00b3 100644 (file)
@@ -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
 
index 375c171fd6545ae01f69b4357f289bde41a8c0ad..aa8c45c5da27efd781695a4d7b779b368b10a9ec 100644 (file)
@@ -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
index 5be3bed4ab86efcc200476c55859b228940100f3..0cac8a1c9b9344e803485b62dcc2324fb8d9e0e6 100644 (file)
@@ -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
index 3c5140e0d20896022286ef0e87cadb64862a1b7a..bf48cdc209463e34bbbb997de0f8164d4efc9659 100644 (file)
@@ -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);
index 397aa2b6223a94ad81d78c38f4dd7eb382ed70e4..b3d3ed747581450cac288e7b6f96760c7858a16c 100644 (file)
@@ -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
index 6246822a2a386ab3f12e0da8a4466d0676173b3d..0abe9b83f557e9569f565a82b8735dfbff2c13c1 100644 (file)
@@ -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