]> Skullheadx's Git Forge - PygameGUIEngine.git/commitdiff
Added functional buttons
authorSkullheadx <admonty1@gmail.com>
Fri, 14 Apr 2023 00:49:27 +0000 (20:49 -0400)
committerSkullheadx <admonty1@gmail.com>
Fri, 14 Apr 2023 00:49:27 +0000 (20:49 -0400)
also added padding to rect
new colors and darken/lighten

button.py [new file with mode: 0644]
color.py
main.py
rectangle.py
text.py

diff --git a/button.py b/button.py
new file mode 100644 (file)
index 0000000..29309fc
--- /dev/null
+++ b/button.py
@@ -0,0 +1,51 @@
+import pygame
+from color import Color
+from text import Label
+
+
+class Button(Label):
+    cooldown = 0.2 # 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):
+        super().__init__(x, y, text, font, size, text_color, background, border_color, border_width, border_radius, padding)
+        self.is_hover = False
+        self.is_pressed = False
+        self.is_clicked = False
+
+        self.func = func
+
+        self.hover_color = Color.darken(self.background_color, 20)
+        self.pressed_color = Color.darken(self.background_color, 40)
+
+        self.last_click = 0
+        self.still_pressed = False
+
+
+    def update(self):
+        mouse_pressed = pygame.mouse.get_pressed()[0]
+        mouse_pos = pygame.mouse.get_pos()
+
+        self.is_hover = self.rect.collidepoint(mouse_pos)
+        self.is_clicked = self.is_hover and not mouse_pressed and self.is_pressed
+        self.is_pressed = self.is_hover and mouse_pressed
+
+        if self.still_pressed and not self.is_pressed:
+            self.still_pressed = False
+
+        if self.is_pressed:
+            if pygame.time.get_ticks() - self.last_click > self.cooldown * 1000 and not self.still_pressed:
+                self.still_pressed = True
+                self.last_click = pygame.time.get_ticks()
+
+                self.rect.set_background_color(self.pressed_color)
+                if self.func is not None:
+                    self.func()
+
+        elif self.is_hover:
+            self.rect.set_background_color(self.hover_color)
+        else:
+            self.rect.set_background_color(self.background_color)
+
+    def draw(self, screen):
+        self.rect.draw(screen)
+        super().draw(screen)
index 64b241ef018bed687286878aab767cac48b4ceea..0399caa3302d48eb2496c5af81fdcaaeb47a903c 100644 (file)
--- a/color.py
+++ b/color.py
@@ -1,8 +1,18 @@
 class Color:
     BLACK = (0, 0, 0)
+    DARK_GRAY = (64, 64, 64)
     GRAY = (128, 128, 128)
+    LIGHT_GRAY = (192, 192, 192)
     WHITE = (255, 255, 255)
 
     RED = (255, 0, 0)
     GREEN = (0, 255, 0)
     BLUE = (0, 0, 255)
+
+    @staticmethod
+    def darken(color, amount):
+        return max(0, color[0] - amount), max(color[1] - amount, 0), max(color[2] - amount, 0)
+
+    @staticmethod
+    def lighten(color, amount):
+        return min(255, color[0] + amount), min(color[1] + amount, 255), min(color[2] + amount, 255)
diff --git a/main.py b/main.py
index b1337e09ccc4c16943d7f216c525560c105a6723..b36cf6bd97acca3ce653508a9db39208b697058c 100644 (file)
--- a/main.py
+++ b/main.py
@@ -2,6 +2,7 @@ import pygame
 from color import Color
 from rectangle import Rectangle
 from text import Text, Label
+from button import Button
 
 pygame.init()
 
@@ -14,6 +15,7 @@ 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"))
 
 is_running = True
 while is_running:
@@ -29,6 +31,9 @@ while is_running:
     b.draw(screen)
     c.draw(screen)
 
+    d.update()
+    d.draw(screen)
+
     pygame.display.update()
     delta = clock.tick(60) / 1000  # Seconds since last frame
 
index 5ed959cdf2cd51c4513bd4785d39846c6d3cc3c1..b68bf573f440648e7e5b77644c92d27b5121aa13 100644 (file)
@@ -34,6 +34,9 @@ class Rectangle:
     def set_border_color(self, border_color):
         self.border_color = border_color
 
+    def collidepoint(self, point):
+        return self.x <= point[0] <= self.x + self.width and self.y <= point[1] <= self.y + self.height
+
     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/text.py b/text.py
index 81a285866010a305d730f77fc9fecdbf2050bd2f..da6adbc4c9566aa74515caeb1008f6133c9c9ad8 100644 (file)
--- a/text.py
+++ b/text.py
@@ -21,7 +21,7 @@ class Text:
     def get_height(self):
         return self.text_surface.get_height()
 
-    def update(self):
+    def update_text(self):
         self.text_surface = self.font.render(self.text, True, self.text_color)
 
     def set_position(self, x, y):
@@ -34,34 +34,36 @@ class Text:
 
     def set_text(self, text):
         self.text = text
-        self.update()
+        self.update_text()
 
     def set_font(self, font, font_size):
         self.font_size = font_size
         self.font = pygame.font.SysFont(font, font_size)
-        self.update()
+        self.update_text()
 
     def set_color(self, text_color):
         self.text_color = text_color
-        self.update()
+        self.update_text()
 
     def draw(self, screen):
         screen.blit(self.text_surface, (self.x, self.y))
 
 
 class Label(Text):
-    padding = 10
-    def __init__(self, x, y, text, font, font_size, background_color, text_color, border_color=Color.BLACK, border_width=0,
-                 border_radius=0):
+
+    def __init__(self, x, y, text, font, font_size, background_color, text_color, border_color=Color.BLACK,
+                 border_width=0, border_radius=0, padding=10):
         super().__init__(x, y, text, font, font_size, text_color)
         self.background_color = background_color
         self.border_color = border_color
         self.border_width = border_width
         self.border_radius = border_radius
+        self.padding = padding
 
         self.width = self.text_surface.get_width() + self.padding * 2
         self.height = self.text_surface.get_height() + self.padding * 2
-        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.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 set_position(self, x, y):
         super().set_position(x, y)
@@ -71,8 +73,8 @@ class Label(Text):
         super().move(dx, dy)
         self.rect.move(dx, dy)
 
-    def update(self):
-        super().update()
+    def update_text(self):
+        super().update_text()
         self.width = self.text_surface.get_width() + self.padding * 2
         self.height = self.text_surface.get_height() + self.padding * 2
         self.rect.set_size(self.width, self.height)