DirectCheckBox.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from direct.gui.DirectGui import DGG, DirectButton
  2. from panda3d.core import PGButton
  3. class DirectCheckBox(DirectButton):
  4. """
  5. DirectCheckBox(parent) - Create a DirectGuiWidget which responds
  6. to mouse clicks by setting a state of True or False and executes
  7. a callback function if defined.
  8. Uses an image swap rather than a text change to indicate state.
  9. """
  10. def __init__(self, parent = None, **kw):
  11. optiondefs = (
  12. # Define type of DirectGuiWidget
  13. ('pgFunc', PGButton, None),
  14. ('numStates', 4, None),
  15. ('state', DGG.NORMAL, None),
  16. ('relief', DGG.RAISED, None),
  17. ('invertedFrames', (1,), None),
  18. # Command to be called on button click
  19. ('command', None, None),
  20. ('extraArgs', [], None),
  21. # Which mouse buttons can be used to click the button
  22. ('commandButtons', (DGG.LMB,), self.setCommandButtons),
  23. # Sounds to be used for button events
  24. ('rolloverSound', DGG.getDefaultRolloverSound(), self.setRolloverSound),
  25. ('clickSound', DGG.getDefaultClickSound(), self.setClickSound),
  26. # Can only be specified at time of widget contruction
  27. # Do the text/graphics appear to move when the button is clicked
  28. ('pressEffect', 1, DGG.INITOPT),
  29. ('uncheckedImage', None, None),
  30. ('checkedImage', None, None),
  31. ('isChecked', False, None),
  32. )
  33. # Merge keyword options with default options
  34. self.defineoptions(kw, optiondefs)
  35. DirectButton.__init__(self,parent)
  36. self.initialiseoptions(DirectCheckBox)
  37. def commandFunc(self, event):
  38. self['isChecked'] = not self['isChecked']
  39. if self['isChecked']:
  40. self['image'] = self['checkedImage']
  41. else:
  42. self['image'] = self['uncheckedImage']
  43. self.setImage()
  44. if self['command']:
  45. # Pass any extra args to command
  46. self['command'](*[self['isChecked']] + self['extraArgs'])