From e6006e526444a20b564cb11e8dc6987e34c5b730 Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Mon, 6 Nov 2023 19:43:24 +0100 Subject: [PATCH] Added update() function to text button. --- pg.py | 70 +++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/pg.py b/pg.py index 6741b4e..ba960fa 100644 --- a/pg.py +++ b/pg.py @@ -82,9 +82,10 @@ class Button: class TextButton: - def __init__(self, text: str, pos: tuple, function=None, key: int=0, text_color: tuple=white, bg_color: tuple=gray, font: pygame.font.Font=default_font, padding: int=8): + def __init__(self, text: str, pos: tuple, surface: pygame.Surface, function=None, key: int=0, text_color: tuple=white, bg_color: tuple=gray, font: pygame.font.Font=default_font, padding: int=8): self.text = text self.pos = pos + self.surface = surface self.function = function self.text_color = text_color self.bg_color = bg_color @@ -107,31 +108,54 @@ class TextButton: return pygame.Rect(self.pos, (w, h)) - # def update(self, new_text: str=None, new_pos: tuple = None, new_func=None, new_key: int=None, new_text_color: tuple=None, new_bg_color: tuple=None, new_font: pygame.font.Font=None): # coming soon - # if new_text is None: - # new_text = self.text - # - # if new_pos is None: - # new_pos = self.pos - # - # if new_func is None: - # new_func = self.function - # - # if new_key is None: - # new_key = self.button.key - # - # if new_text_color is None: - # new_text_color = self.text_color - # - # if new_bg_color is None: - # bg_color = self.bg_color - # - # if new_font is None: - # new_font = self.font + def update(self, new_text: str=None, new_pos: tuple=None, new_surface: pygame.Surface=None, new_func=None, new_key: int=None, new_text_color: tuple=None, new_bg_color: tuple=None, new_font: pygame.font.Font=None, new_padding: int=None): + if new_text is None: + new_text = self.text - def blit(self, surface: pygame.Surface): + if new_pos is None: + new_pos = self.pos + + if new_surface is None: + new_surface = self.surface + + if new_func is None: + new_func = self.function + + if new_key is None: + new_key = self.button.key + + if new_text_color is None: + new_text_color = self.text_color + + if new_bg_color is None: + new_bg_color = self.bg_color + + if new_font is None: + new_font = self.font + + if new_padding is None: + new_padding = self.padding + + self.text = new_text + self.pos = new_pos + self.surface = new_surface + self.function = new_func + self.text_color = new_text_color + self.bg_color = new_bg_color + self.font = new_font + self.padding = new_padding + + self.button = Button(new_func, new_bg_color, key=new_key) + + self.generate_text(new_text) + self.generate_background() + + def blit(self, surface: pygame.Surface=None): x, y = self.pos + if surface is None: + surface = self.surface + pygame.draw.rect(surface, self.bg_color, self.background) surface.blit(self.text_object, (x + self.padding, y + self.padding))