DirectScrolledFrame.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. """Contains the DirectScrolledFrame class.
  2. See the :ref:`directscrolledframe` page in the programming manual for a more
  3. in-depth explanation and an example of how to use this class.
  4. """
  5. __all__ = ['DirectScrolledFrame']
  6. from panda3d.core import NodePath, PGScrollFrame
  7. from . import DirectGuiGlobals as DGG
  8. from .DirectFrame import DirectFrame
  9. from .DirectScrollBar import DirectScrollBar
  10. class DirectScrolledFrame(DirectFrame):
  11. """
  12. DirectScrolledFrame -- a special frame that uses DirectScrollBar to
  13. implement a small window (the frameSize) into a potentially much
  14. larger virtual canvas (the canvasSize, scrolledFrame.getCanvas()).
  15. Unless specified otherwise, scroll bars are automatically created
  16. and managed as needed, based on the relative sizes od the
  17. frameSize and the canvasSize. You can also set manageScrollBars =
  18. 0 and explicitly position and hide or show the scroll bars
  19. yourself.
  20. """
  21. def __init__(self, parent = None, **kw):
  22. optiondefs = (
  23. # Define type of DirectGuiWidget
  24. ('pgFunc', PGScrollFrame, None),
  25. ('frameSize', (-0.5, 0.5, -0.5, 0.5), None),
  26. ('canvasSize', (-1, 1, -1, 1), self.setCanvasSize),
  27. ('manageScrollBars', 1, self.setManageScrollBars),
  28. ('autoHideScrollBars', 1, self.setAutoHideScrollBars),
  29. ('scrollBarWidth', 0.08, self.setScrollBarWidth),
  30. ('borderWidth', (0.01, 0.01), self.setBorderWidth),
  31. )
  32. # Merge keyword options with default options
  33. self.defineoptions(kw, optiondefs)
  34. # Initialize superclasses
  35. DirectFrame.__init__(self, parent)
  36. # The scrollBarWidth parameter is just used at scroll bar
  37. # construction time, and supplies a default frame. It does
  38. # not override an explicit frame specified on the scroll bar.
  39. # If you want to change the frame width after construction,
  40. # you must specify their frameSize tuples explicitly.
  41. w = self['scrollBarWidth']
  42. self.verticalScroll = self.createcomponent(
  43. "verticalScroll", (), None,
  44. DirectScrollBar, (self,),
  45. borderWidth = self['borderWidth'],
  46. frameSize = (-w / 2.0, w / 2.0, -1, 1),
  47. orientation = DGG.VERTICAL)
  48. self.horizontalScroll = self.createcomponent(
  49. "horizontalScroll", (), None,
  50. DirectScrollBar, (self,),
  51. borderWidth = self['borderWidth'],
  52. frameSize = (-1, 1, -w / 2.0, w / 2.0),
  53. orientation = DGG.HORIZONTAL)
  54. self.guiItem.setVerticalSlider(self.verticalScroll.guiItem)
  55. self.guiItem.setHorizontalSlider(self.horizontalScroll.guiItem)
  56. self.canvas = NodePath(self.guiItem.getCanvasNode())
  57. # Call option initialization functions
  58. self.initialiseoptions(DirectScrolledFrame)
  59. def setScrollBarWidth(self):
  60. if self.fInit:
  61. return
  62. w = self['scrollBarWidth']
  63. self.verticalScroll["frameSize"] = (-w / 2.0, w / 2.0, self.verticalScroll["frameSize"][2], self.verticalScroll["frameSize"][3])
  64. self.horizontalScroll["frameSize"] = (self.horizontalScroll["frameSize"][0], self.horizontalScroll["frameSize"][1], -w / 2.0, w / 2.0)
  65. def setCanvasSize(self):
  66. f = self['canvasSize']
  67. self.guiItem.setVirtualFrame(f[0], f[1], f[2], f[3])
  68. def getCanvas(self):
  69. """Returns the NodePath of the virtual canvas. Nodes parented to this
  70. canvas will show inside the scrolled area.
  71. """
  72. return self.canvas
  73. def setManageScrollBars(self):
  74. self.guiItem.setManagePieces(self['manageScrollBars'])
  75. def setAutoHideScrollBars(self):
  76. self.guiItem.setAutoHide(self['autoHideScrollBars'])
  77. def commandFunc(self):
  78. if self['command']:
  79. self['command'](*self['extraArgs'])
  80. def destroy(self):
  81. # Destroy children of the canvas
  82. for child in self.canvas.getChildren():
  83. childGui = self.guiDict.get(child.getName())
  84. if childGui:
  85. childGui.destroy()
  86. else:
  87. parts = child.getName().split('-')
  88. simpleChildGui = self.guiDict.get(parts[-1])
  89. if simpleChildGui:
  90. simpleChildGui.destroy()
  91. if self.verticalScroll:
  92. self.verticalScroll.destroy()
  93. if self.horizontalScroll:
  94. self.horizontalScroll.destroy()
  95. self.verticalScroll = None
  96. self.horizontalScroll = None
  97. DirectFrame.destroy(self)