DirectScrollBar.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. """Undocumented Module"""
  2. __all__ = ['DirectScrollBar']
  3. from panda3d.core import *
  4. import DirectGuiGlobals as DGG
  5. from DirectFrame import *
  6. from DirectButton import *
  7. """
  8. import DirectScrollBar
  9. d = DirectScrollBar(borderWidth=(0, 0))
  10. """
  11. class DirectScrollBar(DirectFrame):
  12. """
  13. DirectScrollBar -- a widget which represents a scroll bar the user can
  14. use for paging through a large document or panel.
  15. """
  16. def __init__(self, parent = None, **kw):
  17. optiondefs = (
  18. # Define type of DirectGuiWidget
  19. ('pgFunc', PGSliderBar, None),
  20. ('state', DGG.NORMAL, None),
  21. ('frameColor', (0.6, 0.6, 0.6, 1), None),
  22. ('range', (0, 1), self.setRange),
  23. ('value', 0, self.__setValue),
  24. ('scrollSize', 0.01, self.setScrollSize),
  25. ('pageSize', 0.1, self.setPageSize),
  26. ('orientation', DGG.HORIZONTAL, self.setOrientation),
  27. ('manageButtons', 1, self.setManageButtons),
  28. ('resizeThumb', 1, self.setResizeThumb),
  29. # Function to be called repeatedly as the bar is scrolled
  30. ('command', None, None),
  31. ('extraArgs', [], None),
  32. )
  33. if kw.get('orientation') in (DGG.VERTICAL, DGG.VERTICAL_INVERTED):
  34. # These are the default options for a vertical layout.
  35. optiondefs += (
  36. ('frameSize', (-0.04, 0.04, -0.5, 0.5), None),
  37. )
  38. else:
  39. # These are the default options for a horizontal layout.
  40. optiondefs += (
  41. ('frameSize', (-0.5, 0.5, -0.04, 0.04), None),
  42. )
  43. # Merge keyword options with default options
  44. self.defineoptions(kw, optiondefs)
  45. # Initialize superclasses
  46. DirectFrame.__init__(self, parent)
  47. self.thumb = self.createcomponent(
  48. "thumb", (), None,
  49. DirectButton, (self,),
  50. borderWidth = self['borderWidth'])
  51. self.incButton = self.createcomponent(
  52. "incButton", (), None,
  53. DirectButton, (self,),
  54. borderWidth = self['borderWidth'])
  55. self.decButton = self.createcomponent(
  56. "decButton", (), None,
  57. DirectButton, (self,),
  58. borderWidth = self['borderWidth'])
  59. if self.decButton['frameSize'] == None and \
  60. self.decButton.bounds == [0.0, 0.0, 0.0, 0.0]:
  61. f = self['frameSize']
  62. if self['orientation'] == DGG.HORIZONTAL:
  63. self.decButton['frameSize'] = (f[0]*0.05, f[1]*0.05, f[2], f[3])
  64. else:
  65. self.decButton['frameSize'] = (f[0], f[1], f[2]*0.05, f[3]*0.05)
  66. if self.incButton['frameSize'] == None and \
  67. self.incButton.bounds == [0.0, 0.0, 0.0, 0.0]:
  68. f = self['frameSize']
  69. if self['orientation'] == DGG.HORIZONTAL:
  70. self.incButton['frameSize'] = (f[0]*0.05, f[1]*0.05, f[2], f[3])
  71. else:
  72. self.incButton['frameSize'] = (f[0], f[1], f[2]*0.05, f[3]*0.05)
  73. self.guiItem.setThumbButton(self.thumb.guiItem)
  74. self.guiItem.setLeftButton(self.decButton.guiItem)
  75. self.guiItem.setRightButton(self.incButton.guiItem)
  76. # Bind command function
  77. self.bind(DGG.ADJUST, self.commandFunc)
  78. # Call option initialization functions
  79. self.initialiseoptions(DirectScrollBar)
  80. def setRange(self):
  81. # Try to preserve the value across a setRange call.
  82. v = self['value']
  83. r = self['range']
  84. self.guiItem.setRange(r[0], r[1])
  85. self['value'] = v
  86. def __setValue(self):
  87. # This is the internal function that is called when
  88. # self['value'] is directly assigned.
  89. self.guiItem.setValue(self['value'])
  90. def setValue(self, value):
  91. # This is the public function that is meant to be called by a
  92. # user that doesn't like to use (or doesn't understand) the
  93. # preferred interface of self['value'].
  94. self['value'] = value
  95. def getValue(self):
  96. return self.guiItem.getValue()
  97. def getRatio(self):
  98. return self.guiItem.getRatio()
  99. def setScrollSize(self):
  100. self.guiItem.setScrollSize(self['scrollSize'])
  101. def setPageSize(self):
  102. self.guiItem.setPageSize(self['pageSize'])
  103. def scrollStep(self, stepCount):
  104. """Scrolls the indicated number of steps forward. If
  105. stepCount is negative, scrolls backward."""
  106. self['value'] = self.guiItem.getValue() + self.guiItem.getScrollSize() * stepCount
  107. def scrollPage(self, pageCount):
  108. """Scrolls the indicated number of pages forward. If
  109. pageCount is negative, scrolls backward."""
  110. self['value'] = self.guiItem.getValue() + self.guiItem.getPageSize() * pageCount
  111. def setOrientation(self):
  112. if self['orientation'] == DGG.HORIZONTAL:
  113. self.guiItem.setAxis(Vec3(1, 0, 0))
  114. elif self['orientation'] == DGG.VERTICAL:
  115. self.guiItem.setAxis(Vec3(0, 0, -1))
  116. elif self['orientation'] == DGG.VERTICAL_INVERTED:
  117. self.guiItem.setAxis(Vec3(0, 0, 1))
  118. else:
  119. raise ValueError('Invalid value for orientation: %s' % (self['orientation']))
  120. def setManageButtons(self):
  121. self.guiItem.setManagePieces(self['manageButtons'])
  122. def setResizeThumb(self):
  123. self.guiItem.setResizeThumb(self['resizeThumb'])
  124. def destroy(self):
  125. self.thumb.destroy()
  126. del self.thumb
  127. self.incButton.destroy()
  128. del self.incButton
  129. self.decButton.destroy()
  130. del self.decButton
  131. DirectFrame.destroy(self)
  132. def commandFunc(self):
  133. # Store the updated value in self['value']
  134. self._optionInfo['value'][DGG._OPT_VALUE] = self.guiItem.getValue()
  135. if self['command']:
  136. apply(self['command'], self['extraArgs'])