From: Skullheadx Date: Fri, 14 Apr 2023 00:49:27 +0000 (-0400) Subject: Added functional buttons X-Git-Url: http://git.skullheadx.com/nixos/NN_Heuristic%20OneTree.png?a=commitdiff_plain;h=236886a195a0018aa18d33a43f437b1ba97cb3b8;p=PygameGUIEngine.git Added functional buttons also added padding to rect new colors and darken/lighten --- diff --git a/button.py b/button.py new file mode 100644 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) diff --git a/color.py b/color.py index 64b241e..0399caa 100644 --- 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 b1337e0..b36cf6b 100644 --- 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 diff --git a/rectangle.py b/rectangle.py index 5ed959c..b68bf57 100644 --- a/rectangle.py +++ b/rectangle.py @@ -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 81a2858..da6adbc 100644 --- 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)