From 542e6b39cabeef2f117b809808eaab50e2877fc7 Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Mon, 6 Nov 2023 20:09:30 +0100 Subject: [PATCH] Added possibility to pass an executable to buttons to recalculate the position. --- pg.py | 50 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/pg.py b/pg.py index ba960fa..f734b0a 100644 --- a/pg.py +++ b/pg.py @@ -2,6 +2,7 @@ import pygame from tools import text +from typing import Union, Callable pygame.init() @@ -20,12 +21,19 @@ default_font = pygame.font.Font(pygame.font.get_default_font(), 16) class Hover: - def __init__(self, function=None, ca=None, position: tuple=None, size: tuple=None): + def __init__(self, function=None, ca=None, position: Union[tuple, Callable]=None, size: tuple=None): self.function = function self.ca = ca + self.callable_position = None + if not position is None and not size is None: self.x, self.y = position self.w, self.h = size + + elif position is Callable: + self.x, self.y = position() + self.callable_position = position + self.active = True def check(self, mouse_pos: tuple): @@ -59,10 +67,17 @@ class Hover: class Button: - def __init__(self, function=None, ca=None, position: tuple=None, size: tuple=None, key: int=0): # ca = collide able, key: 0 = left 1 = mouse wheel pressed 2 = right + def __init__(self, function=None, ca=None, position: Union[tuple, Callable]=None, size: tuple=None, key: int=0): # ca = collide able, key: 0 = left 1 = mouse wheel pressed 2 = right + self.callable_position = None + if not position is None and not size is None: self.x, self.y = position self.width, self.height = size + + elif position is Callable: + self.x, self.y = position() + self.callable_position = position + self.function = function self.active = True self.ca = ca @@ -82,9 +97,8 @@ class Button: class TextButton: - 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): + def __init__(self, text: str, position: Union[tuple, Callable], 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 @@ -92,6 +106,15 @@ class TextButton: self.font = font self.padding = padding + self.callable_position = None + + if position is int: + self.position = position + + else: + self.position = position() + self.callable_position = position + self.text_object = self.generate_text(text) self.background = self.generate_background() @@ -108,12 +131,22 @@ class TextButton: return pygame.Rect(self.pos, (w, h)) - 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): + def update(self, new_text: str=None, new_pos: Union[tuple, Callable]=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 + new_callable_pos = None + if new_pos is None: - new_pos = self.pos + 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 @@ -137,7 +170,8 @@ class TextButton: new_padding = self.padding self.text = new_text - self.pos = new_pos + self.position = new_pos + self.callable_position = new_callable_pos self.surface = new_surface self.function = new_func self.text_color = new_text_color @@ -151,7 +185,7 @@ class TextButton: self.generate_background() def blit(self, surface: pygame.Surface=None): - x, y = self.pos + x, y = self.position if surface is None: surface = self.surface