flow_container.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. from .container import Container
  4. from .widget import Widget
  5. class FlowContainer(Container):
  6. def __init__(self, x, y, width=10, height=10, bg_color=None, margin=2, top_margin=2):
  7. super().__init__(x=x, y=y, width=width, height=height, bg_color=bg_color)
  8. self._LeftChilds = []
  9. self._RightChilds = []
  10. self._Margin = margin
  11. self._TopMargin = top_margin
  12. def __set_child_pos(self):
  13. # Left childs
  14. x_pos = 0
  15. for c in self._LeftChilds:
  16. x_pos += self._Margin
  17. c.set_position(x_pos, self._TopMargin)
  18. x_pos += c.get_rect().width
  19. # Right childs
  20. x_pos = self._Rect.width
  21. for c in reversed(self._RightChilds):
  22. x_pos -= c.get_rect().width
  23. x_pos -= self._Margin
  24. c.set_position(x_pos, self._TopMargin)
  25. def set_margin(self, margin):
  26. self._Margin = margin
  27. def set_top_margin(self, margin):
  28. self._TopMargin = margin
  29. def add_left_child(self, child: Widget):
  30. self._LeftChilds.append(child)
  31. child.set_parent(self)
  32. def add_right_child(self, child: Widget):
  33. self._RightChilds.append(child)
  34. child.set_parent(self)
  35. def clear_left_childs(self):
  36. self._LeftChilds.clear()
  37. def clear_right_childs(self):
  38. self._RightChilds.clear()
  39. def Draw(self):
  40. self.__set_child_pos()
  41. if self._BG_Color is not None:
  42. pygame.draw.rect(self._Canvas, self._BG_Color, (0, 0, self._Width, self._Height))
  43. for c in self._Childs:
  44. c.Draw()
  45. for c in self._LeftChilds:
  46. c.Draw()
  47. for c in self._RightChilds:
  48. c.Draw()
  49. self._Parent.get_canvas().blit(self._Canvas, self._Rect)