From: Skullheadx Date: Fri, 14 Apr 2023 22:13:34 +0000 (-0400) Subject: darken and centering X-Git-Url: http://git.skullheadx.com/nixos/static/index.html?a=commitdiff_plain;h=ce3bb09635642e8976d6169f12ce107d4745090a;p=PygameGUIEngine.git darken and centering --- diff --git a/button.py b/button.py index d774d0c..875bbfc 100644 --- a/button.py +++ b/button.py @@ -7,7 +7,7 @@ class Button(Label): cooldown = 0.25 # seconds def __init__(self, x, y, text, font, size, text_color=Color.BLACK, background=Color.WHITE, border_color=Color.BLACK, - border_width=1, border_radius=0, padding=10, func=None): + border_width=1, border_radius=0, padding=10, darken=True, func=None): super().__init__(x, y, text, font, size, text_color, background, border_color, border_width, border_radius, padding) self.is_hover = False @@ -16,8 +16,12 @@ class Button(Label): self.func = func - self.hover_color = Color.darken(self.background_color, 20) - self.pressed_color = Color.darken(self.background_color, 40) + if darken: + self.hover_color = Color.darken(self.background_color, 20) + self.pressed_color = Color.darken(self.background_color, 40) + else: + self.hover_color = Color.lighten(self.background_color, 20) + self.pressed_color = Color.lighten(self.background_color, 40) self.last_click = 0 self.still_pressed = False diff --git a/container.py b/container.py index c4f066c..bec6a64 100644 --- a/container.py +++ b/container.py @@ -38,6 +38,13 @@ class VBoxContainer: def get_child_index(self, child): return self.children.index(child) + def center(self, x=None, y=None): + if x is None: + x = self.x + if y is None: + y = self.y + self.set_position(x - (self.width - 2 * self.padding) // 2, y - (self.height - 2 * self.padding) // 2) + def set_position(self, x, y): self.x = x self.y = y @@ -48,7 +55,7 @@ class VBoxContainer: 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 + self.get_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 += child.padding @@ -56,6 +63,7 @@ class VBoxContainer: child.set_position(child_position_x, child_position_y) child_y += child.get_height() + self.separation_distance + self.rect.set_size(self.get_width(), self.get_height()) def update(self): for child in self.children: @@ -64,10 +72,13 @@ class VBoxContainer: self.update_children_position() def get_width(self): - return self.width + if self.get_child_count() == 0: + return 0 + return max([child.get_width() for child in self.children]) + self.padding * 2 def get_height(self): - return self.height + return sum([child.get_height() for child in self.children]) + self.padding * 2 + ( + self.get_child_count() - 1) * self.separation_distance def move(self, dx, dy): self.x += dx @@ -138,6 +149,13 @@ class HBoxContainer: def get_child_index(self, child): return self.children.index(child) + def center(self, x=None, y=None): + if x is None: + x = self.x + if y is None: + y = self.y + self.set_position(x - (self.width - 2 * self.padding) // 2, y - (self.height - 2 * self.padding) // 2) + def set_position(self, x, y): self.x = x self.y = y @@ -149,7 +167,7 @@ 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.get_height() - 2 * self.padding - child.get_height()) // 2 if self.centered else self.x if hasattr(child, 'padding'): child_position_x += child.padding @@ -157,17 +175,22 @@ class HBoxContainer: child.set_position(child_position_x, child_position_y) child_x += child.get_width() + self.separation_distance + self.rect.set_size(self.get_width(), self.get_height()) def update(self): for child in self.children: if hasattr(child, 'update'): child.update() + self.update_children_position() def get_width(self): - return self.width + return sum([child.get_width() for child in self.children]) + self.padding * 2 + ( + self.get_child_count() - 1) * self.separation_distance def get_height(self): - return self.height + if self.get_child_count() == 0: + return 0 + return max([child.get_height() for child in self.children]) + self.padding * 2 def move(self, dx, dy): self.x += dx @@ -248,22 +271,29 @@ class GridContainer: return self.children.index(child) return -1 + def center(self, x=None, y=None): + if x is None: + x = self.x + if y is None: + y = self.y + self.set_position(x - (self.width - 2 * self.grid.padding) // 2, y - (self.height - 2 * self.grid.padding) // 2) + self.grid.center(x, y) + def set_position(self, x, y): self.x = x self.y = y self.grid.set_position(x, y) def update(self): - for row in self.children: - for child in row: - if hasattr(child, 'update'): - child.update() + self.grid.update() def get_width(self): - return self.width + return sum([child.get_width() for child in self.children]) + self.grid.padding * 2 + ( + self.get_child_count() - 1) * self.grid.separation_distance def get_height(self): - return self.height + return sum([child.get_height() for child in self.children]) + self.grid.padding * 2 + ( + self.get_child_count() - 1) * self.grid.separation_distance def move(self, dx, dy): self.x += dx diff --git a/image.py b/image.py index 55a4cb8..bf8d2bf 100644 --- a/image.py +++ b/image.py @@ -21,6 +21,13 @@ class Image: def get_height(self): return self.height + def center(self, x=None, y=None): + if x is None: + x = self.x + if y is None: + y = self.y + self.set_position(x - self.width / 2, y - self.height / 2) + def set_position(self, x, y): self.x = x self.y = y diff --git a/main.py b/main.py index ef46ff5..7c158d3 100644 --- a/main.py +++ b/main.py @@ -14,10 +14,30 @@ pygame.display.set_caption("My Game") clock = pygame.time.Clock() delta = 0 -a = Slider(100, 100, 100, 20, 0, 100, 50, Color.LIGHT_GRAY, Color.DARK_GRAY, 4, 5, 10) +a = Slider(100, 100, 100, 20, 0, 100, 50, Color.LIGHT_GRAY, Color.DARK_GRAY, 1, 1, 0, 15, False) b = Text(100, 100, "Slider", "Arial", 20, Color.BLACK) -c = VBoxContainer(100, 100, Color.LIGHT_GRAY, Color.DARK_GRAY, 4, 5, 10, 0, True, [b, a]) - +d = Button(100, 100, "Button", "arial", 15, Color.BLACK, Color.LIGHT_GRAY, Color.DARK_GRAY, 4, 5, 10, False, + lambda: print("Button clicked!")) +e = Image(100, 100, "image.PNG", Color.BLUE, 1) +e.resize(100, 100) +c = GridContainer(10, 10, 2, 2, Color.LIGHT_GRAY, Color.DARK_GRAY, 1, 5, 10, 20, [[a, b], [d, e]]) +c.center(x=640 / 2, y=480 / 2) + +g = Label(100, 100, "Label", "Arial", 20, Color.BLACK, Color.LIGHT_GRAY, 1, 5, 10, 20) +h = Text(100, 100, "Text", "Arial", 20, Color.BLACK) +i = Rectangle(100, 100, 100, 100, Color.LIGHT_GRAY, Color.DARK_GRAY, 1, 5) +f = VBoxContainer(30, 30, Color.LIGHT_GRAY, Color.DARK_GRAY, 1, 5, 10, 20, True, [g, h, i]) + +j = Button(100, 100, "Button1", "arial", 15, Color.RED, Color.BLACK, Color.BLACK, 4, 5, 10, True, + lambda: print("Button1 clicked!")) +k = Button(100, 100, "Button2", "arial", 15, Color.BLACK, Color.LIGHT_GRAY, Color.DARK_GRAY, 4, 5, 10, False, + lambda: print("Button2 clicked!")) +l = Button(100, 100, "Button3", "arial", 15, Color.BLACK, Color.LIGHT_GRAY, Color.DARK_GRAY, 4, 5, 10, False, + lambda: print("Button3 clicked!")) +m = Button(100, 100, "Button4", "arial", 15, Color.BLACK, Color.LIGHT_GRAY, Color.DARK_GRAY, 4, 5, 10, False, + lambda: print("Button4 clicked!")) +n = HBoxContainer(30, 400, Color.LIGHT_GRAY, Color.DARK_GRAY, 1, 5, 10, 20, True, [j, k, l, m]) +n.center(x=640 / 2) is_running = True while is_running: for event in pygame.event.get(): @@ -30,7 +50,14 @@ while is_running: c.update() c.draw(screen) + f.move(delta * 5, 0) + f.update() + f.draw(screen) + + n.update() + n.draw(screen) + pygame.display.update() - delta = clock.tick(60) / 1000 # Seconds since last frame + delta = clock.tick(120) / 1000 # Seconds since last frame pygame.quit() diff --git a/rectangle.py b/rectangle.py index 0901375..4b16b4a 100644 --- a/rectangle.py +++ b/rectangle.py @@ -43,6 +43,13 @@ class Rectangle: def collidepoint(self, point): return self.x <= point[0] <= self.x + self.width and self.y <= point[1] <= self.y + self.height + def center(self, x=None, y=None): + if x is None: + x = self.x + if y is None: + y = self.y + self.set_position(x - self.width / 2, y - self.height / 2) + def draw(self, screen): pygame.draw.rect(screen, self.background_color, (self.x, self.y, self.width, self.height), border_radius=self.border_radius) diff --git a/slider.py b/slider.py index 159aa96..2d79f5c 100644 --- a/slider.py +++ b/slider.py @@ -7,7 +7,8 @@ class Slider: slider_thickness = 4 def __init__(self, x, y, width, height, min_val=0, max_val=100, default=0, background_color=Color.WHITE, - slider_color=Color.BLACK, border_color=Color.BLACK, border_width=1, border_radius=0, padding=15): + slider_color=Color.BLACK, border_color=Color.BLACK, border_width=1, border_radius=0, padding=15, + darken=True): self.x = x self.y = y self.background_color = background_color @@ -30,8 +31,12 @@ class Slider: self.is_pressed = False self.is_clicked = False - self.hover_color = Color.darken(self.background_color, 20) - self.pressed_color = Color.darken(self.background_color, 40) + if darken: + self.hover_color = Color.darken(self.background_color, 20) + self.pressed_color = Color.darken(self.background_color, 40) + else: + self.hover_color = Color.lighten(self.background_color, 20) + self.pressed_color = Color.lighten(self.background_color, 40) self.last_click = 0 self.still_pressed = False diff --git a/text.py b/text.py index 6ab8c63..77603f9 100644 --- a/text.py +++ b/text.py @@ -45,6 +45,13 @@ class Text: self.text_color = text_color self.update_text() + def center(self, x=None, y=None): + if x is None: + x = self.x + if y is None: + y = self.y + self.set_position(x - self.text_surface.get_width() / 2, y - self.text_surface.get_height() / 2) + def draw(self, screen): screen.blit(self.text_surface, (self.x, self.y))