]> git.mxchange.org Git - simgear.git/commitdiff
Add method canvas::Group::getElementById
authorThomas Geymayer <tomgey@gmail.com>
Thu, 29 Nov 2012 23:06:17 +0000 (00:06 +0100)
committerThomas Geymayer <tomgey@gmail.com>
Thu, 29 Nov 2012 23:06:17 +0000 (00:06 +0100)
 - canvas::Group: New method to get a (possibly
   nested) child by its id.
 - nasal::Ghost: Also support recursive parent
   hashes.

simgear/canvas/CMakeLists.txt
simgear/canvas/elements/CanvasGroup.cxx
simgear/canvas/elements/CanvasGroup.hxx
simgear/nasal/cppbind/Ghost.hxx
simgear/nasal/cppbind/cppbind_test.cxx

index c0e7fecd20c576171cdf1dc45ed95ee789c18092..96a23eaafa54dda05e9e6147faf0f9b90392889b 100644 (file)
@@ -16,7 +16,6 @@ set(HEADERS
 )
 
 set(SOURCES
-  Canvas.cxx
   Canvas.cxx
   CanvasEvent.cxx
   CanvasEventListener.cxx
index fdcc7ae244454b882a3bc1934f99e73f51629c5c..0740026828c721d9146eeba92e56ea6a3e793514 100644 (file)
@@ -66,11 +66,11 @@ namespace canvas
 
   //----------------------------------------------------------------------------
   ElementPtr Group::createChild( const std::string& type,
-                                 const std::string& name )
+                                 const std::string& id )
   {
     SGPropertyNode* node = _node->addChild(type, 0, false);
-    if( !name.empty() )
-      node->setStringValue("name", name);
+    if( !id.empty() )
+      node->setStringValue("id", id);
 
     return getChild(node);
   }
@@ -85,6 +85,32 @@ namespace canvas
     return child->second;
   }
 
+  //----------------------------------------------------------------------------
+  ElementPtr Group::getElementById(const std::string& id)
+  {
+    std::vector<GroupPtr> groups;
+
+    BOOST_FOREACH( ChildList::value_type child, _children )
+    {
+      const ElementPtr& el = child.second;
+      if( el->getProps()->getStringValue("id") == id )
+        return el;
+
+      GroupPtr group = boost::dynamic_pointer_cast<Group>(el);
+      if( group )
+        groups.push_back(group);
+    }
+
+    BOOST_FOREACH( GroupPtr group, groups )
+    {
+      ElementPtr el = group->getElementById(id);
+      if( el )
+        return el;
+    }
+
+    return ElementPtr();
+  }
+
   //----------------------------------------------------------------------------
   void Group::update(double dt)
   {
index 55b031e82ccea8eefd51e2da9d24d135e3698df3..38cb0cc90205f59ba9e23ad1c3e28264f74cd1c2 100644 (file)
@@ -44,9 +44,16 @@ namespace canvas
       virtual ~Group();
 
       ElementPtr createChild( const std::string& type,
-                              const std::string& name = "" );
+                              const std::string& id = "" );
       ElementPtr getChild(const SGPropertyNode* node);
 
+      /**
+       * Get first child with given id (breadth-first search)
+       *
+       * @param id  Id (value if property node 'id') of element
+       */
+      ElementPtr getElementById(const std::string& id);
+
       virtual void update(double dt);
 
       virtual bool traverse(EventVisitor& visitor);
index cfb6a960bc830d9ba45e33affe85a5b7e8798424..bc32f9ed14256388dbb734034451504e2506944d 100644 (file)
@@ -628,7 +628,6 @@ namespace nasal
           return Ghost::getPtr( naGhost_ptr(me) );
 
         // Now if it is derived from a ghost (hash with ghost in parent vector)
-        // TODO handle recursive parents
         else if( naIsHash(me) )
         {
           naRef na_parents = naHash_cget(me, const_cast<char*>("parents"));
@@ -644,8 +643,9 @@ namespace nasal
                                       parent != parents.end();
                                     ++parent )
           {
-            if( isBaseOf(naGhost_type(*parent)) )
-              return Ghost::getPtr( naGhost_ptr(*parent) );
+            pointer ptr = fromNasal(c, *parent);
+            if( ptr )
+              return ptr;
           }
         }
 
index 1ff01973b83f29c33a9c84ed381e85b95ce56a11..ef1b4674e788969242e73411df4a6fbcf1ba820a 100644 (file)
@@ -153,6 +153,13 @@ int main(int argc, char* argv[])
   obj.set("parents", parents);
   VERIFY( Ghost<BasePtr>::fromNasal(c, obj.get_naRef()) == d3 );
 
+  // Check recursive parents (aka parent-of-parent)
+  std::vector<naRef> parents2;
+  parents2.push_back(obj.get_naRef());
+  Hash derived_obj(c);
+  derived_obj.set("parents", parents2);
+  VERIFY( Ghost<BasePtr>::fromNasal(c, derived_obj.get_naRef()) == d3 );
+
   naRef args[] = {
     to_nasal(c, std::string("test-arg"))
   };