LUILayouts.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from __future__ import print_function, division
  2. from LUIObject import LUIObject
  3. from LUISprite import LUISprite
  4. from LUIHorizontalLayout import LUIHorizontalLayout
  5. from LUIInitialState import LUIInitialState
  6. __all__ = ["LUICornerLayout", "LUIHorizontalStretchedLayout"]
  7. class LUICornerLayout(LUIObject):
  8. """ This is a layout which is used to combine 9 sprites to a single sprite,
  9. e.g. used for box shadow or frames."""
  10. # List of all sprite identifiers required for the layout
  11. _MODES = ["TR", "Top", "TL", "Right", "Mid", "Left", "BR", "Bottom", "BL"]
  12. def __init__(self, image_prefix="", **kwargs):
  13. """ Creates a new layout, using the image_prefix as prefix. """
  14. LUIObject.__init__(self)
  15. self.set_size("100%", "100%")
  16. self._prefix = image_prefix
  17. self._parts = {}
  18. for i in self._MODES:
  19. self._parts[i] = LUISprite(self, "blank", "skin")
  20. self._update_layout()
  21. LUIInitialState.init(self, kwargs)
  22. def _update_layout(self):
  23. """ Updates the layouts components. """
  24. for i in self._MODES:
  25. self._parts[i].set_texture(self._prefix + i, "skin", resize=True)
  26. # Top and Left
  27. self._parts["Top"].width = "100%"
  28. self._parts["Top"].margin = (0, self._parts["TR"].width, 0, self._parts["TL"].width)
  29. self._parts["Left"].height = "100%"
  30. self._parts["Left"].margin = (self._parts["TL"].height, 0, self._parts["BL"].height, 0)
  31. # Mid
  32. self._parts["Mid"].set_size("100%", "100%")
  33. self._parts["Mid"].margin = (self._parts["Top"].height, self._parts["Right"].width,
  34. self._parts["Bottom"].height, self._parts["Left"].width)
  35. # Bottom and Right
  36. self._parts["Bottom"].width = "100%"
  37. self._parts["Bottom"].margin = (0, self._parts["BR"].width, 0, self._parts["BL"].width)
  38. self._parts["Bottom"].bottom = 0
  39. self._parts["Right"].height = "100%"
  40. self._parts["Right"].margin = (self._parts["TR"].height, 0, self._parts["BR"].width, 0)
  41. self._parts["Right"].right = 0
  42. # Corners
  43. self._parts["TL"].top_left = 0, 0
  44. self._parts["TR"].top_right = 0, 0
  45. self._parts["BL"].bottom_left = 0, 0
  46. self._parts["BR"].bottom_right = 0, 0
  47. def set_prefix(self, prefix):
  48. """ Changes the texture of the layout """
  49. self._prefix = prefix
  50. self._update_layout()
  51. def get_prefix(self):
  52. """ Returns the layouts texture prefix """
  53. return self._prefix
  54. prefix = property(get_prefix, set_prefix)
  55. class LUIHorizontalStretchedLayout(LUIObject):
  56. """ A layout which takes 3 sprites, a left sprite, a right sprite, and a
  57. middle sprite. While the left and right sprites remain untouched, the middle
  58. one will be stretched to fit the layout """
  59. def __init__(self, parent=None, prefix="ButtonDefault", **kwargs):
  60. LUIObject.__init__(self)
  61. self._layout = LUIHorizontalLayout(self, spacing=0)
  62. self._layout.width = "100%"
  63. self._sprite_left = LUISprite(self._layout.cell(), "blank", "skin")
  64. self._sprite_mid = LUISprite(self._layout.cell('*'), "blank", "skin")
  65. self._sprite_right = LUISprite(self._layout.cell(), "blank", "skin")
  66. if parent is not None:
  67. self.parent = parent
  68. self.prefix = prefix
  69. LUIInitialState.init(self, kwargs)
  70. def set_prefix(self, prefix):
  71. """ Sets the layout prefix, this controls which sprites will be used """
  72. self._sprite_left.set_texture(prefix + "_Left", "skin")
  73. self._sprite_mid.set_texture(prefix, "skin")
  74. self._sprite_right.set_texture(prefix + "_Right", "skin")
  75. self._sprite_mid.width = "100%"
  76. self._prefix = prefix
  77. def get_prefix(self):
  78. """ Returns the layout prefix """
  79. return self._prefix
  80. prefix = property(get_prefix, set_prefix)