DirectSlider.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. """Undocumented Module"""
  2. __all__ = ['DirectSlider']
  3. from panda3d.core import *
  4. import DirectGuiGlobals as DGG
  5. from DirectFrame import *
  6. from DirectButton import *
  7. """
  8. import DirectSlider
  9. d = DirectSlider(borderWidth=(0, 0))
  10. """
  11. class DirectSlider(DirectFrame):
  12. """
  13. DirectSlider -- a widget which represents a slider that the
  14. user can pull left and right to represent a continuous value.
  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. # Function to be called repeatedly as slider is moved
  28. ('command', None, None),
  29. ('extraArgs', [], None),
  30. )
  31. if kw.get('orientation') == DGG.VERTICAL:
  32. # These are the default options for a vertical layout.
  33. optiondefs += (
  34. ('frameSize', (-0.08, 0.08, -1, 1), None),
  35. ('frameVisibleScale', (0.25, 1), None),
  36. )
  37. else:
  38. # These are the default options for a horizontal layout.
  39. optiondefs += (
  40. ('frameSize', (-1, 1, -0.08, 0.08), None),
  41. ('frameVisibleScale', (1, 0.25), 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("thumb", (), None,
  48. DirectButton, (self,),
  49. borderWidth = self['borderWidth'])
  50. if self.thumb['frameSize'] == None and \
  51. self.thumb.bounds == [0.0, 0.0, 0.0, 0.0]:
  52. # Compute a default frameSize for the thumb.
  53. f = self['frameSize']
  54. if self['orientation'] == DGG.HORIZONTAL:
  55. self.thumb['frameSize'] = (f[0]*0.05, f[1]*0.05, f[2], f[3])
  56. else:
  57. self.thumb['frameSize'] = (f[0], f[1], f[2]*0.05, f[3]*0.05)
  58. self.guiItem.setThumbButton(self.thumb.guiItem)
  59. # Bind command function
  60. self.bind(DGG.ADJUST, self.commandFunc)
  61. # Call option initialization functions
  62. self.initialiseoptions(DirectSlider)
  63. def setRange(self):
  64. # Try to preserve the value across a setRange call.
  65. v = self['value']
  66. r = self['range']
  67. self.guiItem.setRange(r[0], r[1])
  68. self['value'] = v
  69. def __setValue(self):
  70. # This is the internal function that is called when
  71. # self['value'] is directly assigned.
  72. self.guiItem.setValue(self['value'])
  73. def setValue(self, value):
  74. # This is the public function that is meant to be called by a
  75. # user that doesn't like to use (or doesn't understand) the
  76. # preferred interface of self['value'].
  77. self['value'] = value
  78. def getValue(self):
  79. return self.guiItem.getValue()
  80. def getRatio(self):
  81. return self.guiItem.getRatio()
  82. def setScrollSize(self):
  83. self.guiItem.setScrollSize(self['scrollSize'])
  84. def setPageSize(self):
  85. self.guiItem.setPageSize(self['pageSize'])
  86. def setOrientation(self):
  87. if self['orientation'] == DGG.HORIZONTAL:
  88. self.guiItem.setAxis(Vec3(1, 0, 0))
  89. elif self['orientation'] == DGG.VERTICAL:
  90. self.guiItem.setAxis(Vec3(0, 0, 1))
  91. else:
  92. raise ValueError('Invalid value for orientation: %s' % (self['orientation']))
  93. def destroy(self):
  94. if (hasattr(self, 'thumb')):
  95. self.thumb.destroy() # ow!
  96. del self.thumb
  97. DirectFrame.destroy(self)
  98. def commandFunc(self):
  99. # Store the updated value in self['value']
  100. self._optionInfo['value'][DGG._OPT_VALUE] = self.guiItem.getValue()
  101. if self['command']:
  102. apply(self['command'], self['extraArgs'])