LUIRadiobox.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from __future__ import division
  2. from LUIObject import LUIObject
  3. from LUISprite import LUISprite
  4. from LUIInitialState import LUIInitialState
  5. from LUILabel import LUILabel
  6. class LUIRadiobox(LUIObject):
  7. """ A radiobox which can be used in combination with a LUIRadioboxGroup """
  8. def __init__(self, parent=None, group=None, value=None, active=False, label=u"Radiobox", **kwargs):
  9. """ Constructs a new radiobox. group should be a handle to a LUIRadioboxGroup.
  10. value will be the value returned by group.value, in case the box was
  11. selected. By default, the radiobox is not active. """
  12. assert group is not None, "LUIRadiobox needs a LUIRadioboxGroup!"
  13. LUIObject.__init__(self, x=0, y=0, solid=True)
  14. self._sprite = LUISprite(self, "Radiobox_Default", "skin")
  15. self._label = LUILabel(parent=self, text=label, margin=(0, 0, 0, 23),
  16. center_vertical=True)
  17. self._value = value
  18. self._active = False
  19. self._hovered = False
  20. self._group = group
  21. self._group.register_box(self)
  22. if active:
  23. self.set_active()
  24. if parent:
  25. self.parent = parent
  26. LUIInitialState.init(self, kwargs)
  27. def on_click(self, event):
  28. """ Internal onclick handler. Do not override. """
  29. self.set_active()
  30. def on_mouseover(self, event):
  31. """ Internal mouseover handler """
  32. self._hovered = True
  33. self._update_sprite()
  34. def on_mouseout(self, event):
  35. """ Internal mouseout handler """
  36. self._hovered = False
  37. self._update_sprite()
  38. def set_active(self):
  39. """ Internal function to set the radiobox active """
  40. if self._group is not None:
  41. self._group.set_active_box(self)
  42. else:
  43. self._update_state(True)
  44. def get_value(self):
  45. """ Returns the value of the radiobox """
  46. return self._value
  47. def set_value(self, value):
  48. """ Sets the value of the radiobox """
  49. self._value = value
  50. value = property(get_value, set_value)
  51. def get_label(self):
  52. """ Returns a handle to the label, so it can be modified (e.g. change
  53. its text) """
  54. return self._label
  55. label = property(get_label)
  56. def _update_state(self, active):
  57. """ Internal method to update the state of the radiobox. Called by the
  58. LUIRadioboxGroup """
  59. self._active = active
  60. self.trigger_event("changed")
  61. self._update_sprite()
  62. def on_mousedown(self, event):
  63. """ Internal onmousedown handler. Do not override. """
  64. self._sprite.color = (0.86,0.86,0.86,1.0)
  65. def on_mouseup(self, event):
  66. """ Internal onmouseup handler. Do not override. """
  67. self._sprite.color = (1,1,1,1)
  68. def _update_sprite(self):
  69. """ Internal function to update the sprite of the radiobox """
  70. img = "Radiobox_Active" if self._active else "Radiobox_Default"
  71. if self._hovered:
  72. img += "Hover"
  73. self._sprite.set_texture(img, "skin")