Simplified some button classes.

This commit is contained in:
The Wobbler 2024-03-17 14:13:55 +01:00
parent 45ad97fc13
commit 8d4bb14583

170
pg.py
View file

@ -12,30 +12,19 @@ log = text.Log()
buttonlist = False
buttons = []
# colors
black = (8, 8, 8)
gray = (40, 40, 40)
light_gray = (128, 128, 128)
white = (250, 250, 250)
default_font = pygame.font.Font(pygame.font.get_default_font(), 16)
class Hover:
def __init__(self, function=None, ca=None, position: Union[tuple, Callable]=None, size: tuple=None):
def __init__(self, rect: pygame.Rect, function=None):
self.rect = rect
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 callable(position):
self.x, self.y = position()
self.callable_position = position
self.active = True
@ -43,50 +32,24 @@ class Hover:
if self.active:
mx, my = mouse_pos
if not self.ca is None:
if self.ca.collidepoint((mx, my)):
if self.function is None:
return True
else:
if self.rect.collidepoint((mx, my)):
if not self.function is None:
self.function()
return True
else:
x = self.x
y = self.y
w = self.w
h = self.h
if mx >= x and mx <= x + w and my >= y and my <= y + h:
if self.function is None:
return True
else:
self.function()
return True
return False
class Button:
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 callable(position):
self.x, self.y = position()
self.callable_position = position
def __init__(self, rect: pygame.Rect, function=None, key: int=0): # key: 0 = left 1 = mouse wheel pressed 2 = right
self.rect = rect
self.function = function
self.active = True
self.ca = ca
self.key = key
self.hover = Hover(function, ca, position, size)
self.active = True
self.hover = Hover(rect, function)
if buttonlist:
buttons.append(self)
@ -100,110 +63,41 @@ class Button:
class TextButton:
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: tuple=(8, 8)):
def __init__(self, text: str, position: tuple, function=None, key: int=0, text_color: tuple=white, bg_color: tuple=gray, font: pygame.font.Font=default_font, padding: tuple=(8, 8), border_radius: int=0, line_thickness: int = 0):
self.text = text
self.position = position
self.text_color = text_color
self.font = font
self.text_object = self.generate_text(text)
self.size = (self.text_object.get_width() + padding[0] * 2, self.text_object.get_height() + padding[1] * 2)
self.surface = surface
self.function = function
self.key = key
self.bg_color = bg_color
self.padding = padding
self.callable_position = None
self.border_radius = border_radius
self.line_thickness = line_thickness
if type(position) is tuple:
self.position = position
self.surface, self.size = self.generate_surface()
self.rect = self.make_rect()
else:
self.position = position(size=self.size)
self.callable_position = position
self.button = Button(self.rect, self.function, self.key)
self.background = self.generate_background()
def generate_surface(self):
text_object = self.font.render(self.text, True, self.text_color)
self.button = Button(function, self.background, key=key)
def generate_text(self, text):
self.text = text
return self.font.render(text, True, self.text_color)
def generate_background(self):
w, h = self.text_object.get_size()
w, h = text_object.get_size()
w, h = w + self.padding[0] * 2, h + self.padding[1] * 2
return pygame.Rect(self.position, (w, h))
surface = pygame.Surface((w, h), pygame.SRCALPHA)
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: tuple=None):
if new_text is None:
new_text = self.text
pygame.draw.rect(surface, self.bg_color, pygame.Rect((0, 0), (w, h)), self.line_thickness, self.border_radius)
surface.blit(text_object, (self.padding[0], self.padding[1]))
if new_text_color is None:
new_text_color = self.text_color
return surface, (w, h)
if new_bg_color is None:
new_bg_color = self.bg_color
def make_rect(self):
return pygame.Rect(self.position, self.size)
if new_font is None:
new_font = self.font
if new_padding is None:
new_padding = self.padding
self.text = new_text
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[0] * 2, self.text_object.get_height() + self.padding[1] * 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 type(new_pos) is tuple:
new_callable_pos = None
else:
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.background = self.generate_background()
self.button = Button(new_func, self.background, key=new_key)
def blit(self, surface: pygame.Surface=None):
x, y = self.position
if surface is None:
surface = self.surface
pygame.draw.rect(surface, self.bg_color, self.background)
surface.blit(self.text_object, (x + self.padding[0], y + self.padding[1]))
def draw(self, surface: pygame.Surface):
surface.blit(self.surface, self.position)
def check(self, mouse_pos, pressed):
return self.button.check(mouse_pos, pressed)
@ -369,14 +263,6 @@ def crop_surface(surface: pygame.Surface, position: tuple, size: tuple):
return new_surface
def bt_from_ca(ca, function): # bt_from_ca = button from collide able
return Button(function, ca)
def bt_from_cords(function, position: tuple, size: tuple):
return Button(function, position=position, size=size)
def check_buttons(mouse_pos, pressed):
for button in buttons:
button.check(mouse_pos, pressed)