Browse Source

get template elements by name

David Rose 21 years ago
parent
commit
b9b7ddec00

+ 4 - 0
pandatool/src/xfile/xFileDataNodeTemplate.cxx

@@ -247,5 +247,9 @@ get_element(int n) const {
 ////////////////////////////////////////////////////////////////////
 const XFileDataObject *XFileDataNodeTemplate::
 get_element(const string &name) const {
+  int child_index = _template->find_child_index(name);
+  if (child_index >= 0) {
+    return get_element(child_index);
+  }
   return NULL;
 }

+ 20 - 3
pandatool/src/xfile/xFileNode.cxx

@@ -57,12 +57,29 @@ find_child(const string &name) const {
   ChildrenByName::const_iterator ni;
   ni = _children_by_name.find(name);
   if (ni != _children_by_name.end()) {
-    return (*ni).second;
+    return get_child((*ni).second);
   }
 
   return NULL;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: XFileNode::find_child_index
+//       Access: Public
+//  Description: Returns the index number of the child with the
+//               indicated name, if any, or -1 if none.
+////////////////////////////////////////////////////////////////////
+int XFileNode::
+find_child_index(const string &name) const {
+  ChildrenByName::const_iterator ni;
+  ni = _children_by_name.find(name);
+  if (ni != _children_by_name.end()) {
+    return (*ni).second;
+  }
+
+  return -1;
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: XFileNode::find_descendent
 //       Access: Public
@@ -117,13 +134,13 @@ get_guid() const {
 ////////////////////////////////////////////////////////////////////
 void XFileNode::
 add_child(XFileNode *node) {
-  _children.push_back(node);
   if (node->has_name()) {
-    _children_by_name[node->get_name()] = node;
+    _children_by_name[node->get_name()] = (int)_children.size();
   }
   if (node->has_guid()) {
     _x_file->_nodes_by_guid[node->get_guid()] = node;
   }
+  _children.push_back(node);
 }
 
 ////////////////////////////////////////////////////////////////////

+ 2 - 1
pandatool/src/xfile/xFileNode.h

@@ -50,6 +50,7 @@ public:
   INLINE int get_num_children() const;
   INLINE XFileNode *get_child(int n) const;
   XFileNode *find_child(const string &name) const;
+  int find_child_index(const string &name) const;
   XFileNode *find_descendent(const string &name) const;
 
   virtual bool has_guid() const;
@@ -73,7 +74,7 @@ protected:
   typedef pvector< PT(XFileNode) > Children;
   Children _children;
 
-  typedef pmap<string, XFileNode *> ChildrenByName;
+  typedef pmap<string, int> ChildrenByName;
   ChildrenByName _children_by_name;
 
 public: