LUIFormattedLabel.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from panda3d.core import LVecBase2i
  2. from LUIObject import LUIObject
  3. from LUILabel import LUILabel
  4. from LUIInitialState import LUIInitialState
  5. __all__ = ["LUIFormattedLabel"]
  6. class LUIFormattedLabel(LUIObject):
  7. """ Small helper class to build a text consisting of different formatted
  8. parts of text. Uses LUILabels internally """
  9. def __init__(self, **kwargs):
  10. """ Creates a new formatted label. """
  11. LUIObject.__init__(self)
  12. LUIInitialState.init(self, kwargs)
  13. self._cursor = LVecBase2i(0)
  14. self._last_size = 14
  15. def clear(self):
  16. """ Removes all text from this label and resets it to the initial state.
  17. This will also detach the sub-labels from this label. """
  18. self._cursor.set(0, 0)
  19. self.remove_all_children()
  20. def newline(self, font_size=None):
  21. """ Moves the cursor to the next line. The font size controls how much
  22. the cursor will move. By default, the font size of the last added text
  23. is used, or if no text was added yet, a size of 14."""
  24. self._cursor.x = 0
  25. if font_size is None:
  26. font_size = self._last_size
  27. self._cursor.y += font_size + 2
  28. def add(self, *args, **kwargs):
  29. """ Appends a new text. The arguments are equal to the arguments of
  30. LUILabel. The arguments shouldn't contain information about the
  31. placement like top_left, or center_vertical, since the labels are
  32. placed at explicit positions. """
  33. self._last_size = kwargs.get("font_size", 14)
  34. label = LUILabel(parent=self, left=self._cursor.x, top=self._cursor.y,
  35. *args, **kwargs)
  36. # This is a bit of a hack, we should use a horizontal layout, but we
  37. # don't for performance reasons.
  38. self._cursor.x += label.text_handle.width