Selaa lähdekoodia

add NodePath::get_top()

David Rose 22 vuotta sitten
vanhempi
sitoutus
4df761d285
3 muutettua tiedostoa jossa 37 lisäystä ja 14 poistoa
  1. 26 7
      panda/src/pgraph/nodePath.I
  2. 9 7
      panda/src/pgraph/nodePath.cxx
  3. 2 0
      panda/src/pgraph/nodePath.h

+ 26 - 7
panda/src/pgraph/nodePath.I

@@ -240,6 +240,21 @@ get_error_type() const {
   return _error_type;
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: NodePath::get_top_node
+//       Access: Published
+//  Description: Returns the top node of the path, or NULL if the path
+//               is empty.  This requires iterating through the path.
+////////////////////////////////////////////////////////////////////
+INLINE PandaNode *NodePath::
+get_top_node() const {
+  if (is_empty()) {
+    return (PandaNode *)NULL;
+  }
+
+  return get_top().node();
+}
+
 ////////////////////////////////////////////////////////////////////
 //     Function: NodePath::node
 //       Access: Published
@@ -282,17 +297,21 @@ get_key() const {
 //  Description: Returns true if the node represented by this NodePath
 //               is parented within the same graph as that of the
 //               other NodePath.  This is essentially the same thing
-//               as asking whether the top node of both NodePaths is
-//               the same (e.g., both "render").
+//               as asking whether get_top() of both NodePaths is the
+//               same (e.g., both "render").
 ////////////////////////////////////////////////////////////////////
 INLINE bool NodePath::
 is_same_graph(const NodePath &other) const {
   // Actually, it's possible for the top nodes to be the same, but the
-  // NodePaths still to be considered in different graphs.  This will
-  // happen if one of the top nodes is considered a different
-  // instance--for instance, render.instance_to(NodePath()) returns a
-  // different instance of render that appears to have the same top
-  // node.  But this is a very rare thing to do.
+  // NodePaths still to be considered in different graphs.  But even
+  // in this case, get_top() will be different for each one.  (They'll
+  // be different singleton NodePaths that happen to reference the
+  // same node).
+
+  // This will happen if one of the top nodes is considered a
+  // different instance--for instance, render.instance_to(NodePath())
+  // returns a different instance of render that appears to have the
+  // same top node.  But this is a very rare thing to do.
   int a_count, b_count;
   return (find_common_ancestor(*this, other, a_count, b_count) != (NodePathComponent *)NULL);
 }

+ 9 - 7
panda/src/pgraph/nodePath.cxx

@@ -96,15 +96,15 @@ get_node(int index) const {
 }
 
 ////////////////////////////////////////////////////////////////////
-//     Function: NodePath::get_top_node
+//     Function: NodePath::get_top
 //       Access: Published
-//  Description: Returns the top node of the path, or NULL if the path
-//               is empty.  This requires iterating through the path.
+//  Description: Returns a singleton NodePath that represents the top
+//               of the path, or empty NodePath if this path is empty.
 ////////////////////////////////////////////////////////////////////
-PandaNode *NodePath::
-get_top_node() const {
+NodePath NodePath::
+get_top() const {
   if (is_empty()) {
-    return (PandaNode *)NULL;
+    return *this;
   }
 
   NodePathComponent *comp = _head;
@@ -113,7 +113,9 @@ get_top_node() const {
     nassertr(comp != (NodePathComponent *)NULL, NULL);
   }
 
-  return comp->get_node();
+  NodePath top;
+  top._head = comp;
+  return top;
 }
 
 

+ 2 - 0
panda/src/pgraph/nodePath.h

@@ -181,6 +181,8 @@ PUBLISHED:
   INLINE ErrorType get_error_type() const;
 
   PandaNode *get_top_node() const;
+  NodePath get_top() const;
+
   INLINE PandaNode *node() const;
 
   INLINE int get_key() const;