LUICheckbox.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from __future__ import division
  2. from LUIObject import LUIObject
  3. from LUISprite import LUISprite
  4. from LUILabel import LUILabel
  5. from LUIInitialState import LUIInitialState
  6. __all__ = ["LUICheckbox"]
  7. class LUICheckbox(LUIObject):
  8. """ This is a simple checkbox, including a Label. The checkbox can either
  9. be checked or unchecked. """
  10. def __init__(self, checked=False, label=u"Checkbox", **kwargs):
  11. """ Constructs a new checkbox with the given label and state. By default,
  12. the checkbox is not checked. """
  13. LUIObject.__init__(self, x=0, y=0, solid=True)
  14. self._checked = checked
  15. self._checkbox_sprite = LUISprite(self, "Checkbox_Default", "skin")
  16. self._label = LUILabel(parent=self, text=label, margin=(0, 0, 0, 25),
  17. center_vertical=True, alpha=0.4)
  18. self._hovered = False
  19. self._update_sprite()
  20. LUIInitialState.init(self, kwargs)
  21. @property
  22. def checked(self):
  23. """ Returns True if the checkbox is currently checked """
  24. return self._checked
  25. @checked.setter
  26. def checked(self, checked):
  27. """ Sets the checkbox state """
  28. self._checked = checked
  29. self._update_sprite()
  30. def toggle(self):
  31. """ Toggles the checkbox state """
  32. self.checked = not self.checked
  33. @property
  34. def label(self):
  35. """ Returns a handle to the label, so it can be modified """
  36. return self._label
  37. @property
  38. def sprite(self):
  39. """ Returns a handle to the internal checkbox sprite """
  40. return self._checkbox_sprite
  41. def on_click(self, event):
  42. """ Internal onclick handler. Do not override """
  43. self._checked = not self._checked
  44. self.trigger_event("changed")
  45. self._update_sprite()
  46. def on_mousedown(self, event):
  47. """ Internal mousedown handler. """
  48. self._checkbox_sprite.color = (0.9, 0.9, 0.9, 1.0)
  49. def on_mouseup(self, event):
  50. """ Internal on_mouseup handler. """
  51. self._checkbox_sprite.color = (1, 1, 1, 1)
  52. def on_mouseover(self, event):
  53. """ Internal mouseover handler """
  54. self._hovered = True
  55. self._update_sprite()
  56. def on_mouseout(self, event):
  57. """ Internal mouseout handler """
  58. self._hovered = False
  59. self._update_sprite()
  60. def _update_sprite(self):
  61. """ Internal method to update the sprites """
  62. img = "Checkbox_Checked" if self._checked else "Checkbox_Default"
  63. if self._hovered:
  64. img += "Hover"
  65. self._checkbox_sprite.set_texture(img, "skin")