Added possibility to pass an executable to buttons to recalculate the position.

This commit is contained in:
The Wobbler 2023-11-06 20:09:30 +01:00
parent e6006e5264
commit 542e6b39ca

50
pg.py
View file

@ -2,6 +2,7 @@
import pygame import pygame
from tools import text from tools import text
from typing import Union, Callable
pygame.init() pygame.init()
@ -20,12 +21,19 @@ default_font = pygame.font.Font(pygame.font.get_default_font(), 16)
class Hover: 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.function = function
self.ca = ca self.ca = ca
self.callable_position = None
if not position is None and not size is None: if not position is None and not size is None:
self.x, self.y = position self.x, self.y = position
self.w, self.h = size self.w, self.h = size
elif position is Callable:
self.x, self.y = position()
self.callable_position = position
self.active = True self.active = True
def check(self, mouse_pos: tuple): def check(self, mouse_pos: tuple):
@ -59,10 +67,17 @@ class Hover:
class Button: 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: if not position is None and not size is None:
self.x, self.y = position self.x, self.y = position
self.width, self.height = size self.width, self.height = size
elif position is Callable:
self.x, self.y = position()
self.callable_position = position
self.function = function self.function = function
self.active = True self.active = True
self.ca = ca self.ca = ca
@ -82,9 +97,8 @@ class Button:
class TextButton: 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.text = text
self.pos = pos
self.surface = surface self.surface = surface
self.function = function self.function = function
self.text_color = text_color self.text_color = text_color
@ -92,6 +106,15 @@ class TextButton:
self.font = font self.font = font
self.padding = padding 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.text_object = self.generate_text(text)
self.background = self.generate_background() self.background = self.generate_background()
@ -108,12 +131,22 @@ 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_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: if new_text is None:
new_text = self.text new_text = self.text
new_callable_pos = None
if new_pos is 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: if new_surface is None:
new_surface = self.surface new_surface = self.surface
@ -137,7 +170,8 @@ class TextButton:
new_padding = self.padding new_padding = self.padding
self.text = new_text self.text = new_text
self.pos = new_pos self.position = new_pos
self.callable_position = new_callable_pos
self.surface = new_surface self.surface = new_surface
self.function = new_func self.function = new_func
self.text_color = new_text_color self.text_color = new_text_color
@ -151,7 +185,7 @@ class TextButton:
self.generate_background() self.generate_background()
def blit(self, surface: pygame.Surface=None): def blit(self, surface: pygame.Surface=None):
x, y = self.pos x, y = self.position
if surface is None: if surface is None:
surface = self.surface surface = self.surface