From bef56f4b2868d5e12d4445d225be20abe44467cd Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Sat, 11 Nov 2023 20:21:54 +0100 Subject: [PATCH] Made the callable position in TextButton() work. --- pg.py | 63 +++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/pg.py b/pg.py index a0f822a..f6e275a 100644 --- a/pg.py +++ b/pg.py @@ -137,28 +137,6 @@ class TextButton: if new_text is None: new_text = self.text - new_callable_pos = None - - if new_pos is None: - new_pos = self.position - new_callable_pos = self.callable_position - - elif new_pos is int: - new_callable_pos = None - - elif new_pos is Callable: - new_pos = new_pos() - new_callable_pos = new_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 @@ -172,19 +150,48 @@ class TextButton: new_padding = self.padding self.text = new_text - self.position = new_pos - self.callable_position = new_callable_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.text_object = self.generate_text(new_text) + + self.size = (self.text_object.get_width() + self.padding * 2, self.text_object.get_height() + self.padding * 2) + + new_callable_pos = None + + if new_pos is None: + new_pos = self.position + new_callable_pos = self.callable_position + + if not new_callable_pos is None: + new_pos = new_callable_pos(self.size) + + elif new_pos is int: + new_callable_pos = None + + elif new_pos is Callable: + new_pos = new_pos(self.size) + new_callable_pos = new_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 + + self.position = new_pos + self.callable_position = new_callable_pos + self.surface = new_surface + self.function = new_func + self.button = Button(new_func, new_bg_color, key=new_key) - self.generate_text(new_text) - self.generate_background() + self.background = self.generate_background() def blit(self, surface: pygame.Surface=None): x, y = self.position