DirectWaitBar.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. """Undocumented Module"""
  2. __all__ = ['DirectWaitBar']
  3. from panda3d.core import *
  4. from . import DirectGuiGlobals as DGG
  5. from .DirectFrame import *
  6. import sys
  7. if sys.version_info >= (3, 0):
  8. stringType = str
  9. else:
  10. stringType = basestring
  11. """
  12. import DirectWaitBar
  13. d = DirectWaitBar(borderWidth=(0, 0))
  14. """
  15. class DirectWaitBar(DirectFrame):
  16. """ DirectWaitBar - A DirectWidget that shows progress completed
  17. towards a task. """
  18. def __init__(self, parent = None, **kw):
  19. # Inherits from DirectFrame
  20. # A Direct Frame can have:
  21. # - A background texture (pass in path to image, or Texture Card)
  22. # - A midground geometry item (pass in geometry)
  23. # - A foreground text Node (pass in text string or Onscreen Text)
  24. optiondefs = (
  25. # Define type of DirectGuiWidget
  26. ('pgFunc', PGWaitBar, None),
  27. ('frameSize', (-1, 1, -0.08, 0.08), None),
  28. ('borderWidth', (0, 0), None),
  29. ('range', 100, self.setRange),
  30. ('value', 0, self.setValue),
  31. ('barBorderWidth', (0, 0), self.setBarBorderWidth),
  32. ('barColor', (1, 0, 0, 1), self.setBarColor),
  33. ('barTexture', None, self.setBarTexture),
  34. ('barRelief', DGG.FLAT, self.setBarRelief),
  35. ('sortOrder', DGG.NO_FADE_SORT_INDEX, None),
  36. )
  37. if 'text' in kw:
  38. textoptiondefs = (
  39. ('text_pos', (0, -0.025), None),
  40. ('text_scale', 0.1, None)
  41. )
  42. else:
  43. textoptiondefs = ()
  44. # Merge keyword options with default options
  45. self.defineoptions(kw, optiondefs + textoptiondefs)
  46. # Initialize superclasses
  47. DirectFrame.__init__(self, parent)
  48. self.barStyle = PGFrameStyle()
  49. # Call option initialization functions
  50. self.initialiseoptions(DirectWaitBar)
  51. self.updateBarStyle()
  52. def destroy(self):
  53. self.barStyle = None
  54. DirectFrame.destroy(self)
  55. def setRange(self):
  56. """Updates the bar range which you can set using bar['range'].
  57. This is the value at which the WaitBar indicates 100%."""
  58. self.guiItem.setRange(self['range'])
  59. def setValue(self):
  60. """Updates the bar value which you can set using bar['value'].
  61. The value should range between 0 and bar['range']."""
  62. self.guiItem.setValue(self['value'])
  63. def getPercent(self):
  64. """Returns the percentage complete."""
  65. return self.guiItem.getPercent()
  66. def updateBarStyle(self):
  67. if not self.fInit:
  68. self.guiItem.setBarStyle(self.barStyle)
  69. def setBarRelief(self):
  70. """Updates the bar relief, which you can set using bar['barRelief']."""
  71. self.barStyle.setType(self['barRelief'])
  72. self.updateBarStyle()
  73. def setBarBorderWidth(self):
  74. """Updates the bar's border width, which you can set using bar['barBorderWidth']."""
  75. self.barStyle.setWidth(*self['barBorderWidth'])
  76. self.updateBarStyle()
  77. def setBarColor(self):
  78. """Updates the bar color, which you can set using bar['barColor']."""
  79. color = self['barColor']
  80. self.barStyle.setColor(color[0], color[1], color[2], color[3])
  81. self.updateBarStyle()
  82. def setBarTexture(self):
  83. """Updates the bar texture, which you can set using bar['barTexture']."""
  84. # this must be a single texture (or a string).
  85. texture = self['barTexture']
  86. if isinstance(texture, stringType):
  87. texture = loader.loadTexture(texture)
  88. if texture:
  89. self.barStyle.setTexture(texture)
  90. else:
  91. self.barStyle.clearTexture()
  92. self.updateBarStyle()
  93. def update(self, value):
  94. """Updates the bar with the given value and renders a frame."""
  95. self['value'] = value
  96. # Render a frame out-of-sync with the igLoop to update the
  97. # window right now. This allows the wait bar to be updated
  98. # even though we are not normally rendering frames.
  99. base.graphicsEngine.renderFrame()
  100. def finish(self, N = 10):
  101. """Fill the bar in N frames. This call is blocking."""
  102. remaining = self['range'] - self['value']
  103. if remaining:
  104. step = max(1, int(remaining / N))
  105. count = self['value']
  106. while count != self['range']:
  107. count += step
  108. if count > self['range']:
  109. count = self['range']
  110. self.update(count)