container.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # -*- coding: utf-8 -*-
  2. import pygame
  3. from .widget import Widget
  4. class Container(Widget):
  5. def __init__(self, x, y, width=10, height=10, bg_color=None):
  6. super().__init__(x=x, y=y, width=width, height=height, bg_color=bg_color)
  7. self._Canvas = pygame.Surface((self._Width, self._Height), pygame.SRCALPHA, 32)
  8. self._Canvas.convert_alpha()
  9. self._Childs = []
  10. def add_child(self, child: Widget):
  11. self._Childs.append(child)
  12. child.set_parent(self)
  13. def set_size(self, width, height):
  14. super().set_size(width, height)
  15. # Recreate the surface
  16. self._Canvas = pygame.Surface((self._Width, self._Height), pygame.SRCALPHA, 32)
  17. self._Canvas.convert_alpha()
  18. def Draw(self):
  19. if self._BG_Color is not None:
  20. pygame.draw.rect(self._Canvas, self._BG_Color, (0, 0, self._Width, self._Height))
  21. for c in self._Childs:
  22. c.Draw()
  23. if self._Parent is not None:
  24. self._Parent.get_canvas().blit(self._Canvas, self._Rect)
  25. def handle_event(self, evt):
  26. super().handle_event(evt)
  27. for c in self._Childs:
  28. c.handle_event(evt)