]> Skullheadx's Git Forge - PygameGUIEngine.git/commitdiff
Container for label/button/text/rect
authorSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Fri, 14 Apr 2023 16:48:38 +0000 (12:48 -0400)
committerSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Fri, 14 Apr 2023 16:48:38 +0000 (12:48 -0400)
container.py
main.py
rectangle.py
text.py

index 51c347b6423da05bcf0ed0d199245accd39536b5..0d172b5f24fe60d06fc6564813d59f0a855dfaa7 100644 (file)
@@ -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 b36cf6bd97acca3ce653508a9db39208b697058c..28a6225d031e26ce14054ff77d5f7db7627b4ffa 100644 (file)
--- 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
 
index b68bf573f440648e7e5b77644c92d27b5121aa13..0901375e413b08c89b7e68f2acdb7123e1119acb 100644 (file)
@@ -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 da6adbc4c9566aa74515caeb1008f6133c9c9ad8..6ab8c639a041e2fd3461f18c640d248772ed8410 100644 (file)
--- 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)