DWBPackageInstaller.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. __all__ = ["DWBPackageInstaller"]
  2. from direct.p3d.PackageInstaller import PackageInstaller
  3. from direct.gui.DirectWaitBar import DirectWaitBar
  4. from direct.gui import DirectGuiGlobals as DGG
  5. class DWBPackageInstaller(DirectWaitBar, PackageInstaller):
  6. """ This class presents a PackageInstaller that also inherits from
  7. DirectWaitBar, so it updates its own GUI as it downloads.
  8. Specify perPackage = True to make the progress bar reset for each
  9. package, or False (the default) to show one continuous progress
  10. bar for all packages.
  11. Specify updateText = True (the default) to update the text label
  12. with the name of the package or False to leave it up to you to set
  13. it.
  14. You can specify a callback function with finished = func; this
  15. function will be called, with one boolean parameter, when the
  16. download has completed. The parameter will be true on success, or
  17. false on failure.
  18. """
  19. def __init__(self, appRunner, parent = None, **kw):
  20. PackageInstaller.__init__(self, appRunner)
  21. optiondefs = (
  22. ('borderWidth', (0.01, 0.01), None),
  23. ('relief', DGG.SUNKEN, self.setRelief),
  24. ('range', 1, self.setRange),
  25. ('barBorderWidth', (0.01, 0.01), self.setBarBorderWidth),
  26. ('barColor', (0.424, 0.647, 0.878, 1), self.setBarColor),
  27. ('barRelief', DGG.RAISED, self.setBarRelief),
  28. ('text', 'Starting', self.setText),
  29. ('text_pos', (0, -0.025), None),
  30. ('text_scale', 0.1, None),
  31. ('perPackage', False, None),
  32. ('updateText', True, None),
  33. ('finished', None, None),
  34. )
  35. self.defineoptions(kw, optiondefs)
  36. DirectWaitBar.__init__(self, parent, **kw)
  37. self.initialiseoptions(DWBPackageInstaller)
  38. self.updateBarStyle()
  39. # Hidden by default until the download begins.
  40. self.hide()
  41. def cleanup(self):
  42. PackageInstaller.cleanup(self)
  43. DirectWaitBar.destroy(self)
  44. def destroy(self):
  45. PackageInstaller.cleanup(self)
  46. DirectWaitBar.destroy(self)
  47. def packageStarted(self, package):
  48. """ This callback is made for each package between
  49. downloadStarted() and downloadFinished() to indicate the start
  50. of a new package. """
  51. if self['updateText']:
  52. self['text'] = 'Installing %s' % (package.getFormattedName())
  53. self.show()
  54. def packageProgress(self, package, progress):
  55. """ This callback is made repeatedly between packageStarted()
  56. and packageFinished() to update the current progress on the
  57. indicated package only. The progress value ranges from 0
  58. (beginning) to 1 (complete). """
  59. if self['perPackage']:
  60. self['value'] = progress * self['range']
  61. def downloadProgress(self, overallProgress):
  62. """ This callback is made repeatedly between downloadStarted()
  63. and downloadFinished() to update the current progress through
  64. all packages. The progress value ranges from 0 (beginning) to
  65. 1 (complete). """
  66. if not self['perPackage']:
  67. self['value'] = overallProgress * self['range']
  68. def downloadFinished(self, success):
  69. """ This callback is made when all of the packages have been
  70. downloaded and installed (or there has been some failure). If
  71. all packages where successfully installed, success is True.
  72. If there were no packages that required downloading, this
  73. callback will be made immediately, *without* a corresponding
  74. call to downloadStarted(). """
  75. self.hide()
  76. if self['finished']:
  77. self['finished'](success)