]> Skullheadx's Git Forge - PygameGUIEngine.git/commitdiff
darken and centering
authorSkullheadx <admonty1@gmail.com>
Fri, 14 Apr 2023 22:13:34 +0000 (18:13 -0400)
committerSkullheadx <admonty1@gmail.com>
Fri, 14 Apr 2023 22:13:34 +0000 (18:13 -0400)
button.py
container.py
image.py
main.py
rectangle.py
slider.py
text.py

index d774d0cbd8addbb744a351114223c9d9e144f3d3..875bbfc4a7b4788273ab5b26d79f362dee48d028 100644 (file)
--- 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
index c4f066c6c897bc6ab2075cfdab0e007a10953784..bec6a649efd6f222d39c5c820eae3fdddf39220e 100644 (file)
@@ -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
index 55a4cb821e7f05aeefbc5baa63941c7b248833dc..bf8d2bf46d590ded3319ad86d7897dc693251f2d 100644 (file)
--- 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 ef46ff543b9da2aa33546a5164ba6c8c91fa052b..7c158d3e5e8a105151f733ddfe9da31b54f5f8e6 100644 (file)
--- 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()
index 0901375e413b08c89b7e68f2acdb7123e1119acb..4b16b4a15039ef2ec1f8164b512294eed654b31d 100644 (file)
@@ -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)
index 159aa9678e26b4b3004d2f68bba27cb1824daac9..2d79f5c72d062bd9cf85fd9b38974a3266d28b0b 100644 (file)
--- 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 6ab8c639a041e2fd3461f18c640d248772ed8410..77603f93dfc8286a57519f80adfed7300f53c2e3 100644 (file)
--- 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))