Browse Source

Added functions to show/hide by nodepath tag

Gyedo Jeon 18 years ago
parent
commit
b49718e725
2 changed files with 58 additions and 16 deletions
  1. 7 6
      direct/src/tkwidgets/SceneGraphExplorer.py
  2. 51 10
      direct/src/tkwidgets/Tree.py

+ 7 - 6
direct/src/tkwidgets/SceneGraphExplorer.py

@@ -11,6 +11,7 @@ import Pmw
 # changing these strings requires changing DirectSession.py SGE_ strs too!
 DEFAULT_MENU_ITEMS = [
     'Update Explorer',
+    'Expand All',
     'Separator',
     'Select', 'Deselect',
     'Separator',
@@ -91,6 +92,11 @@ class SceneGraphExplorer(Pmw.MegaWidget, DirectObject):
         # Check keywords and initialise options based on input values.
         self.initialiseoptions(SceneGraphExplorer)
 
+    # [gjeon] to set childrenTag and fModeChildrenTag of tree node
+    def setChildrenTag(self, tag, fModeChildrenTag):
+        self._node.setChildrenTag(tag, fModeChildrenTag)
+        self._node.update()
+
     # [gjeon] to set fSortChildren of tree node
     def setFSortChildren(self, fSortChildren):
         self._node.setFSortChildren(fSortChildren)
@@ -126,12 +132,7 @@ class SceneGraphExplorer(Pmw.MegaWidget, DirectObject):
         # Remove hooks
         self.ignore('DIRECT_activeParent')
         self.ignore('SGE_Update Explorer')
-    
-    def sortChildren(self, searchKey):
-        sceneGraphItem = self._node.find(searchKey)
-        if sceneGraphItem:
-            sceneGraphItem.updateSelf(fSortChildren = 1)
-    
+   
     def updateSelection(self, searchKey):
         # [gjeon] update SGE selection with directSession
         sceneGraphItem = self._node.find(searchKey)

+ 51 - 10
direct/src/tkwidgets/Tree.py

@@ -51,12 +51,18 @@ class TreeNode:
         self.menuVar = IntVar()
         self.menuVar.set(0)
         self._popupMenu = None
-        self.fSortChildren = 0 # [gjeon] flag for sorting children or not
+        self.fSortChildren = False # [gjeon] flag for sorting children or not
+        self.fModeChildrenTag = 0 # [gjeon] 0: hide by given tag, 1: show by given tag
+        self.childrenTag = None
 
     # [gjeon] to set fSortChildren
     def setFSortChildren(self, fSortChildren):
         self.fSortChildren = fSortChildren
 
+    def setChildrenTag(self, tag, fModeChildrenTag):
+        self.childrenTag = tag
+        self.fModeChildrenTag = fModeChildrenTag
+    
     def destroy(self):
         if self._popupMenu:
             self._popupMenu.destroy()
@@ -142,10 +148,20 @@ class TreeNode:
 
     def popupMenuCommand(self):
         command = self.menuList[self.menuVar.get()]
-        self.item.MenuCommand(command)
-        if self.parent and (command != 'Update Explorer'):
-            # Update parent to try to keep explorer up to date
-            self.parent.update(self.fSortChilren)
+        
+        if (command == 'Expand All'):
+            self.update(fExpandMode = 1)
+        ## elif (command == 'Collapse All'):
+            ## self.update(fExpandMode = 2)
+        ## elif (command == 'Expand'):
+            ## self.expand()
+        ## elif (command == 'Collapse'):
+            ## self.collapse()
+        else:
+            self.item.MenuCommand(command)
+            if self.parent and (command != 'Update Explorer'):
+                # Update parent to try to keep explorer up to date
+                self.parent.update()
 
     def expand(self, event=None):
         if not self.item.IsExpandable():
@@ -197,24 +213,30 @@ class TreeNode:
         else:
             return self
 
-    def update(self, fUseCachedChildren = 1):
+    def update(self, fUseCachedChildren = 1, fExpandMode = 0):
         if self.parent:
-            self.parent.update(fUseCachedChildren)
+            self.parent.update(fUseCachedChildren, fExpandMode = fExpandMode)
         else:
             oldcursor = self.canvas['cursor']
             self.canvas['cursor'] = "watch"
             self.canvas.update()
             self.canvas.delete(ALL)     # XXX could be more subtle
-            self.draw(7, 2, fUseCachedChildren)
+            self.draw(7, 2, fUseCachedChildren, fExpandMode = fExpandMode)
             x0, y0, x1, y1 = self.canvas.bbox(ALL)
             self.canvas.configure(scrollregion=(0, 0, x1, y1))
             self.canvas['cursor'] = oldcursor
 
-    def draw(self, x, y, fUseCachedChildren = 1):
+    def draw(self, x, y, fUseCachedChildren = 1, fExpandMode = 0):
         # XXX This hard-codes too many geometry constants!
         self.x, self.y = x, y
         self.drawicon()
         self.drawtext()
+        
+        if fExpandMode == 1: # [gjeon] expand all
+            self.state = 'expanded'
+        elif fExpandMode == 2: # [gjeon] collapse all
+            self.state = 'collapsed'
+        
         if self.state != 'expanded':
             return y+17
         # draw children
@@ -242,9 +264,20 @@ class TreeNode:
                 child = self.children[key]
             else:
                 child = TreeNode(self.canvas, self, item, self.menuList)
+
+            # [gjeon] to set flag recursively
             child.setFSortChildren(self.fSortChildren)
+            child.setChildrenTag(self.childrenTag, self.fModeChildrenTag)
+
             self.children[key] = child
             self.kidKeys.append(key)
+
+            # [gjeon] to filter by given tag
+            if self.childrenTag:
+                #if not item.nodePath.hasTag('GameArea'):
+                if self.fModeChildrenTag != item.nodePath.hasTag(self.childrenTag):
+                    self.kidKeys.remove(key)
+
         # Remove unused children
         for key in self.children.keys():
             if key not in self.kidKeys:
@@ -256,7 +289,7 @@ class TreeNode:
             child = self.children[key]
             cylast = cy
             self.canvas.create_line(x+9, cy+7, cx, cy+7, fill="gray50")
-            cy = child.draw(cx, cy, fUseCachedChildren)
+            cy = child.draw(cx, cy, fUseCachedChildren, fExpandMode = fExpandMode)
             if child.item.IsExpandable():
                 if child.state == 'expanded':
                     iconname = "minusnode"
@@ -365,11 +398,16 @@ class TreeNode:
         # Search for a node who's key matches the given key
         # Is it this node
         if searchKey == self.item.GetKey():
+            # [gjeon] to filter by given tag
+            if self.childrenTag:
+                if self.fModeChildrenTag != self.item.nodePath.hasTag(self.childrenTag):
+                    return None
             return self
         # Nope, check the children
         sublist = self.item._GetSubList()
         for item in sublist:
             key = item.GetKey()
+
             # Use existing child or create new TreeNode if none exists
             if self.children.has_key(key):
                 child = self.children[key]
@@ -378,6 +416,9 @@ class TreeNode:
                 # Update local list of children and keys
                 self.children[key] = child
                 self.kidKeys.append(key)
+            # [gjeon] to set flag recursively
+            child.setChildrenTag(self.childrenTag, self.fModeChildrenTag)
+
             # See if node is child (or one of child's descendants)
             retVal = child.find(searchKey)
             if retVal: