From: Skullheadx <94652084+Skullheadx@users.noreply.github.com> Date: Fri, 14 Apr 2023 16:48:38 +0000 (-0400) Subject: Container for label/button/text/rect X-Git-Url: http://git.skullheadx.com/nixos/tech/openbsd_html_css/post.html?a=commitdiff_plain;h=88e0a098c15992dc575428988faceebf5c387129;p=PygameGUIEngine.git Container for label/button/text/rect --- diff --git a/container.py b/container.py index 51c347b..0d172b5 100644 --- a/container.py +++ b/container.py @@ -1,39 +1,99 @@ -import pygame from color import Color from rectangle import Rectangle -from text import Text, Label -from button import Button class Container: - def __init__(self, x, y, width, height, background_color=Color.WHITE, border_color=Color.BLACK, border_width=0, - border_radius=0, padding=10): + def __init__(self, x, y, background_color=Color.WHITE, border_color=Color.BLACK, border_width=0, + border_radius=0, padding=10, separation_distance=20, centered=False, children=None): + if children is None: + children = [] self.x = x self.y = y - self.width = width - self.height = height + self.width = 0 + self.height = 0 self.background_color = background_color self.border_color = border_color self.border_width = border_width self.border_radius = border_radius self.padding = padding + self.separation_distance = separation_distance + self.centered = centered - self.children = [] + self.rect = Rectangle(self.x - self.padding, self.y - self.padding, self.width, self.height, + self.background_color, self.border_color, self.border_width, self.border_radius) + + self.children = children + self.set_size() + self.update_children_position() + + def get_children(self): + return self.children + + def get_child(self, index): + return self.children[index] + + def get_child_count(self): + return len(self.children) + + def get_child_index(self, child): + return self.children.index(child) + + def set_position(self, x, y): + self.x = x + self.y = y + self.rect.set_position(x - self.padding, y - self.padding) + self.update_children_position() + + def update_children_position(self): + child_y = self.y + for i, child in enumerate(self.children): + child_position_x = self.x + (self.width - 2 * self.padding - child.get_width()) // 2 if self.centered else self.x + child_position_y = child_y + if hasattr(child, 'padding'): + child_position_x += self.padding + child_position_y += self.padding + + child.set_position(child_position_x, child_position_y) + child_y += child.get_height() + self.separation_distance + + def update(self): + for child in self.children: + if hasattr(child, 'update'): + child.update() + + def get_width(self): + return self.width + + def get_height(self): + return self.height + + def move(self, dx, dy): + self.x += dx + self.y += dy + self.rect.move(dx, dy) + for child in self.children: + child.move(dx, dy) + + def set_size(self): + if self.get_child_count() == 0: + self.width = 0 + self.height = 0 + else: + self.width = max([child.get_width() for child in self.children]) + self.padding * 2 + self.height = sum([child.get_height() for child in self.children]) + self.padding * 2 + (self.get_child_count() - 1) * self.separation_distance + self.rect.set_size(self.width, self.height) def add_child(self, child): self.children.append(child) + self.set_size() + self.update_children_position() def remove_child(self, child): self.children.remove(child) + self.set_size() + self.update_children_position() def draw(self, screen): - # Draw background - pygame.draw.rect(screen, self.background_color, (self.x, self.y, self.width, self.height), 0, self.border_radius) - - # Draw border - pygame.draw.rect(screen, self.border_color, (self.x, self.y, self.width, self.height), self.border_width, - self.border_radius) - - # Draw children + self.rect.draw(screen) for child in self.children: - child.draw(screen) \ No newline at end of file + child.draw(screen) diff --git a/main.py b/main.py index b36cf6b..28a6225 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,7 @@ from color import Color from rectangle import Rectangle from text import Text, Label from button import Button +from container import Container pygame.init() @@ -15,8 +16,12 @@ delta = 0 a = Text(100, 100, "Hello World", "Arial", 20, color=Color.RED) b = Label(100, 200, "Word hunt", "Imprint Shadow", 20, Color.RED, Color.BLACK, Color.BLACK, 1, 15) c = Rectangle(100, 300, 100, 100, (255, 0, 0)) -d = Button(100, 200, "Play", "Imprint Shadow", 40, Color.BLUE, Color.BLACK, Color.BLACK, 1, 15, 20, lambda: print("Hello World")) - +d = Button(100, 200, "Play", "Imprint Shadow", 40, Color.BLUE, Color.BLACK, Color.BLACK, 1, 15, 20, + lambda: print("Hello World")) +e = Button(100, 300, "Quit", "Imprint Shadow", 40, Color.BLUE, Color.BLACK, Color.BLACK, 1, 15, 20, + lambda: print("Goodbye World")) +f = Container(400, 100, Color.WHITE, Color.BLACK, 1, 15, 20, 50, True, [d, e]) +f.add_child(a) is_running = True while is_running: for event in pygame.event.get(): @@ -34,6 +39,13 @@ while is_running: d.update() d.draw(screen) + e.update() + e.draw(screen) + + f.update() + f.move(delta * -10, 0) + f.draw(screen) + pygame.display.update() delta = clock.tick(60) / 1000 # Seconds since last frame diff --git a/rectangle.py b/rectangle.py index b68bf57..0901375 100644 --- a/rectangle.py +++ b/rectangle.py @@ -16,6 +16,12 @@ class Rectangle: self.border_width = border_width self.border_radius = border_radius + def get_width(self): + return self.width + + def get_height(self): + return self.height + def set_position(self, x, y): self.x = x self.y = y diff --git a/text.py b/text.py index da6adbc..6ab8c63 100644 --- a/text.py +++ b/text.py @@ -65,6 +65,12 @@ class Label(Text): self.rect = Rectangle(self.x - self.padding, self.y - self.padding, self.width, self.height, self.background_color, self.border_color, self.border_width, self.border_radius) + def get_width(self): + return self.text_surface.get_width() + self.padding * 2 + + def get_height(self): + return self.text_surface.get_height() + self.padding * 2 + def set_position(self, x, y): super().set_position(x, y) self.rect.set_position(self.x - self.padding, self.y - self.padding)