LUIButton.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from LUIObject import LUIObject
  2. from LUILayouts import LUIHorizontalStretchedLayout
  3. from LUILabel import LUILabel
  4. from LUIInitialState import LUIInitialState
  5. __all__ = ["LUIButton"]
  6. class LUIButton(LUIObject):
  7. """ Simple button, containing three sprites and a label. """
  8. def __init__(self, text=u"Button", template="ButtonDefault", **kwargs):
  9. """ Constructs a new button. The template controls which sprites to use.
  10. If the template is "ButtonDefault" for example, the sprites
  11. "ButtonDefault_Left", "ButtonDefault" and "ButtonDefault_Right" will
  12. be used. The sprites used when the button is pressed should be named
  13. "ButtonDefaultFocus_Left" and so on then.
  14. If an explicit width is set on the button, the button will stick to
  15. that width, otherwise it will automatically resize to fit the label """
  16. LUIObject.__init__(self, x=0, y=0, solid=True)
  17. self._template = template
  18. self._layout = LUIHorizontalStretchedLayout(
  19. parent=self, prefix=self._template, width="100%")
  20. self._label = LUILabel(parent=self, text=text)
  21. self._label.z_offset = 1
  22. self._label.center_vertical = True
  23. self._label.margin = 0, 20, 0, 20
  24. self.margin.left = -1
  25. LUIInitialState.init(self, kwargs)
  26. @property
  27. def text(self):
  28. """ Returns the current label text of the button """
  29. return self._label.text
  30. @text.setter
  31. def text(self, text):
  32. """ Sets the label text of the button """
  33. self._label.text = text
  34. def on_mousedown(self, event):
  35. """ Internal on_mousedown handler. Do not override """
  36. self._layout.prefix = self._template + "Focus"
  37. self._label.margin.top = 1
  38. def on_mouseup(self, event):
  39. """ Internal on_mouseup handler. Do not override """
  40. self._layout.prefix = self._template
  41. self._label.margin.top = 0