Added update() function to text button.

This commit is contained in:
The Wobbler 2023-11-06 19:43:24 +01:00
parent 808b613273
commit e6006e5264

70
pg.py
View file

@ -82,9 +82,10 @@ class Button:
class TextButton: 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.text = text
self.pos = pos self.pos = pos
self.surface = surface
self.function = function self.function = function
self.text_color = text_color self.text_color = text_color
self.bg_color = bg_color self.bg_color = bg_color
@ -107,31 +108,54 @@ class TextButton:
return pygame.Rect(self.pos, (w, h)) 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 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: if new_text is None:
# new_text = self.text 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 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 x, y = self.pos
if surface is None:
surface = self.surface
pygame.draw.rect(surface, self.bg_color, self.background) pygame.draw.rect(surface, self.bg_color, self.background)
surface.blit(self.text_object, (x + self.padding, y + self.padding)) surface.blit(self.text_object, (x + self.padding, y + self.padding))