From: Skullheadx Date: Fri, 14 Apr 2023 20:23:37 +0000 (-0400) Subject: Grid Container X-Git-Url: http://git.skullheadx.com/nixos/static/batman.png?a=commitdiff_plain;h=0a7f082dd16ec78756fc1154b39204320fc48b40;p=PygameGUIEngine.git Grid Container note that it looks weird when having a large difference in size between rows as the elements will not line up. Another issue may lie with adding children to grid box container? --- diff --git a/container.py b/container.py index 2bea335..a3cca9e 100644 --- a/container.py +++ b/container.py @@ -51,8 +51,8 @@ class VBoxContainer: 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_position_x += child.padding + child_position_y += child.padding child.set_position(child_position_x, child_position_y) child_y += child.get_height() + self.separation_distance @@ -148,11 +148,11 @@ class HBoxContainer: for i, child in enumerate(self.children): child_position_x = child_x child_position_y = self.y + ( - self.height - 2 * self.padding - child.get_height()) // 2 if self.centered else self.x + self.height - 2 * self.padding - child.get_height()) // 2 if self.centered else self.x if hasattr(child, 'padding'): - child_position_x += self.padding - child_position_y += self.padding + child_position_x += child.padding + child_position_y += child.padding child.set_position(child_position_x, child_position_y) child_x += child.get_width() + self.separation_distance @@ -208,19 +208,25 @@ class GridContainer: if children is None: children = [[None for _ in range(columns)] for _ in range(rows)] else: - if len(children) != rows: raise ValueError('Children must be a 2D array of size rows x columns') for row in children: if len(row) != columns: raise ValueError('Children must be a 2D array of size rows x columns') + + self.x = x + self.y = y + self.width = 0 + self.height = 0 self.rows = rows self.columns = columns - grid = VBoxContainer(x, y, background_color, border_color, border_width, border_radius, padding, - separation_distance, True) + + self.grid = VBoxContainer(x, y, background_color, border_color, border_width, border_radius, padding, + separation_distance, True) for row in children: - grid.add_child(HBoxContainer(x, y, background_color, border_color, border_width, border_radius, padding, - separation_distance, True, row)) + self.grid.add_child( + HBoxContainer(x, y, background_color, border_color=background_color, border_width=0, border_radius=0, + padding=0, separation_distance=separation_distance, centered=True, children=row)) self.children = children def get_children(self): @@ -244,27 +250,13 @@ class GridContainer: 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_x = self.x - for i, child in enumerate(self.children): - child_position_x = child_x - child_position_y = self.y + ( - self.height - 2 * self.padding - child.get_height()) // 2 if self.centered else self.x - - if hasattr(child, 'padding'): - child_position_x += self.padding - child_position_y += self.padding - - child.set_position(child_position_x, child_position_y) - child_x += child.get_width() + self.separation_distance + self.grid.set_position(x, y) def update(self): - for child in self.children: - if hasattr(child, 'update'): - child.update() + for row in self.children: + for child in row: + if hasattr(child, 'update'): + child.update() def get_width(self): return self.width @@ -275,31 +267,28 @@ class GridContainer: 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) + self.grid.move(dx, dy) def set_size(self): if self.get_child_count() == 0: self.width = 0 self.height = 0 else: - self.width = sum([child.get_width() for child in self.children]) + self.padding * 2 + ( - self.get_child_count() - 1) * self.separation_distance - self.height = max([child.get_height() for child in self.children]) + self.padding * 2 - self.rect.set_size(self.width, self.height) + self.width = sum([child.get_width() for child in self.children]) + self.grid.padding * 2 + ( + self.get_child_count() - 1) * self.grid.separation_distance + self.height = sum([child.get_height() for child in self.children]) + self.grid.padding * 2 + ( + self.get_child_count() - 1) * self.grid.separation_distance + self.grid.set_size() def add_child(self, child, row, col): self.children[row][col] = child - self.set_size() - self.update_children_position() + self.grid.set_size() + self.grid.update_children_position() def remove_child(self, child): self.children.remove(child) - self.set_size() - self.update_children_position() + self.grid.set_size() + self.grid.update_children_position() def draw(self, screen): - self.rect.draw(screen) - for child in self.children: - child.draw(screen) + self.grid.draw(screen) diff --git a/main.py b/main.py index dbd321b..79ae235 100644 --- a/main.py +++ b/main.py @@ -3,10 +3,9 @@ from color import Color from rectangle import Rectangle from text import Text, Label from button import Button -from container import VBoxContainer, HBoxContainer +from container import VBoxContainer, HBoxContainer, GridContainer from image import Image - pygame.init() screen = pygame.display.set_mode((640, 480)) @@ -15,19 +14,30 @@ clock = pygame.time.Clock() delta = 0 # Testing Text -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")) -e = Button(100, 300, "Quit", "Imprint Shadow", 40, Color.BLUE, Color.BLACK, Color.BLACK, 1, 15, 20, - lambda: print("Goodbye World")) -f = HBoxContainer(400, 25, Color.WHITE, Color.BLACK, 1, 15, 20, 10, True, [d, e]) -f.add_child(a) +# 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")) +# e = Button(100, 300, "Quit", "Imprint Shadow", 40, Color.BLUE, Color.BLACK, Color.BLACK, 1, 15, 20, +# lambda: print("Goodbye World")) +# f = HBoxContainer(400, 25, Color.WHITE, Color.BLACK, 1, 15, 20, 10, True, [d, e]) +# f.add_child(a) +# +# g = Image(0, 0, "image.PNG", Color.BLACK, 5) +# g.resize(200, 200) +# f.add_child(c) -g = Image(0, 0, "image.PNG", Color.BLACK, 5) -g.resize(200, 200) -f.add_child(c) +p = Rectangle(0, 0, 100, 100, Color.WHITE, Color.BLACK, 1, 0) +q = Text(0, 0, "Hello World", "Arial", 20, color=Color.RED) +r = Button(0, 0, "Play", "Imprint Shadow", 40, Color.BLUE, Color.BLACK, Color.BLACK, 1, 15, 20, + lambda: print("Hello World")) +# s = Rectangle(0, 0, 100, 100, Color.BLUE, Color.BLACK, 1, 0) +s = Image(0, 0, "image.PNG", Color.BLACK, 5) +# s.resize(300, 200) +s.resize(100, 100) +t = GridContainer(100, 100, 2, 2,Color.LIGHT_GRAY,Color.BLACK, 1,15,10,20, children=[[p, q], [r, s]]) +# t = VBoxContainer(100, 100, Color.LIGHT_GRAY, Color.BLACK, 1, 15, 10, 10, True, [p, q, r, s]) is_running = True while is_running: @@ -37,24 +47,28 @@ while is_running: screen.fill((255, 255, 255)) - a.draw(screen) - b.move(delta * 10, delta * 10) - b.set_text(str(round(clock.get_fps())) + " FPS") - b.draw(screen) - c.draw(screen) - - d.update() - d.draw(screen) - - e.update() - e.draw(screen) - - f.update() - f.move(delta * -40, 0) - f.draw(screen) + # a.draw(screen) + # b.move(delta * 10, delta * 10) + # b.set_text(str(round(clock.get_fps())) + " FPS") + # b.draw(screen) + # c.draw(screen) + # + # d.update() + # d.draw(screen) + # + # e.update() + # e.draw(screen) + # + # f.update() + # f.move(delta * -40, 0) + # f.draw(screen) + # + # g.move(0, delta * 25) + # g.draw(screen) - g.move(0, delta * 25) - g.draw(screen) + t.update() + t.move(delta * 20, 0) + t.draw(screen) pygame.display.update() delta = clock.tick(60) / 1000 # Seconds since last frame