2023-07-21 13:23:15 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
import pygame
|
2023-11-05 18:36:40 +01:00
|
|
|
from tools import text
|
2023-11-06 20:09:30 +01:00
|
|
|
from typing import Union, Callable
|
2023-07-21 13:23:15 +02:00
|
|
|
|
2023-11-05 19:06:57 +01:00
|
|
|
pygame.init()
|
|
|
|
|
2023-08-25 16:15:01 +02:00
|
|
|
log = text.Log()
|
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
buttonlist = False
|
|
|
|
buttons = []
|
2023-07-21 13:23:15 +02:00
|
|
|
|
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
# colors
|
|
|
|
gray = (40, 40, 40)
|
|
|
|
white = (250, 250, 250)
|
2023-07-21 13:23:15 +02:00
|
|
|
|
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
default_font = pygame.font.Font(pygame.font.get_default_font(), 16)
|
2023-07-21 13:23:15 +02:00
|
|
|
|
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
class Hover:
|
2023-11-06 20:09:30 +01:00
|
|
|
def __init__(self, function=None, ca=None, position: Union[tuple, Callable]=None, size: tuple=None):
|
2023-11-05 15:49:14 +01:00
|
|
|
self.function = function
|
|
|
|
self.ca = ca
|
2023-11-06 20:09:30 +01:00
|
|
|
self.callable_position = None
|
|
|
|
|
2023-11-05 19:43:41 +01:00
|
|
|
if not position is None and not size is None:
|
|
|
|
self.x, self.y = position
|
|
|
|
self.w, self.h = size
|
2023-11-06 20:09:30 +01:00
|
|
|
|
|
|
|
elif position is Callable:
|
|
|
|
self.x, self.y = position()
|
|
|
|
self.callable_position = position
|
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
self.active = True
|
2023-07-21 13:23:15 +02:00
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
def check(self, mouse_pos: tuple):
|
|
|
|
if self.active:
|
|
|
|
mx, my = mouse_pos
|
2023-07-21 13:23:15 +02:00
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
if not self.ca is None:
|
2023-11-05 19:43:41 +01:00
|
|
|
if self.ca.collidepoint((mx, my)):
|
2023-11-05 15:49:14 +01:00
|
|
|
if self.function is None:
|
|
|
|
return True
|
2023-07-21 13:23:15 +02:00
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
else:
|
|
|
|
self.function()
|
|
|
|
return True
|
2023-08-25 16:15:01 +02:00
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
else:
|
|
|
|
x = self.x
|
|
|
|
y = self.y
|
|
|
|
w = self.w
|
|
|
|
h = self.h
|
2023-08-25 16:15:01 +02:00
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
if mx >= x and mx <= x + w and my >= y and my <= y + h:
|
|
|
|
if self.function is None:
|
|
|
|
return True
|
2023-08-25 16:15:01 +02:00
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
else:
|
|
|
|
self.function()
|
|
|
|
return True
|
2023-08-25 16:15:01 +02:00
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
return False
|
2023-08-25 16:15:01 +02:00
|
|
|
|
2023-07-21 13:23:15 +02:00
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
class Button:
|
2023-11-06 20:09:30 +01:00
|
|
|
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
|
|
|
|
|
2023-11-05 19:43:41 +01:00
|
|
|
if not position is None and not size is None:
|
|
|
|
self.x, self.y = position
|
|
|
|
self.width, self.height = size
|
2023-11-06 20:09:30 +01:00
|
|
|
|
|
|
|
elif position is Callable:
|
|
|
|
self.x, self.y = position()
|
|
|
|
self.callable_position = position
|
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
self.function = function
|
|
|
|
self.active = True
|
|
|
|
self.ca = ca
|
|
|
|
self.key = key
|
2023-07-21 13:23:15 +02:00
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
self.hover = Hover(function, ca, position, size)
|
2023-07-21 13:23:15 +02:00
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
if buttonlist:
|
|
|
|
buttons.append(self)
|
2023-08-25 16:15:01 +02:00
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
def check(self, mouse_pos: tuple, pressed): # check if the button is pressed
|
|
|
|
if self.active:
|
|
|
|
if pressed[self.key]:
|
|
|
|
return self.hover.check(mouse_pos)
|
2023-08-25 16:15:01 +02:00
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
return False
|
2023-07-21 13:23:15 +02:00
|
|
|
|
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
class TextButton:
|
2023-11-06 20:09:30 +01:00
|
|
|
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):
|
2023-11-09 17:15:30 +01:00
|
|
|
self.text = text
|
|
|
|
self.text_color = text_color
|
|
|
|
self.font = font
|
|
|
|
|
2023-11-06 20:18:07 +01:00
|
|
|
self.text_object = self.generate_text(text)
|
2023-11-09 17:15:30 +01:00
|
|
|
self.size = (self.text_object.get_width() + padding * 2, self.text_object.get_height() + padding * 2)
|
2023-11-06 20:18:07 +01:00
|
|
|
|
2023-11-06 19:43:24 +01:00
|
|
|
self.surface = surface
|
2023-11-05 15:49:14 +01:00
|
|
|
self.function = function
|
|
|
|
self.bg_color = bg_color
|
|
|
|
self.padding = padding
|
2023-11-06 20:09:30 +01:00
|
|
|
self.callable_position = None
|
|
|
|
|
2023-11-12 14:27:41 +01:00
|
|
|
if position is tuple:
|
2023-11-06 20:09:30 +01:00
|
|
|
self.position = position
|
|
|
|
|
|
|
|
else:
|
2023-11-09 17:15:30 +01:00
|
|
|
self.position = position(size=self.size)
|
2023-11-06 20:09:30 +01:00
|
|
|
self.callable_position = position
|
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
self.background = self.generate_background()
|
2023-10-31 19:31:22 +01:00
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
self.button = Button(function, self.background, key=key)
|
2023-10-31 19:31:22 +01:00
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
def generate_text(self, text):
|
|
|
|
self.text = text
|
2023-10-31 19:31:22 +01:00
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
return self.font.render(text, True, self.text_color)
|
2023-07-21 13:23:15 +02:00
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
def generate_background(self):
|
|
|
|
w, h = self.text_object.get_size()
|
|
|
|
w, h = w + self.padding * 2, h + self.padding * 2
|
2023-07-21 13:23:15 +02:00
|
|
|
|
2023-11-06 20:18:07 +01:00
|
|
|
return pygame.Rect(self.position, (w, h))
|
2023-07-21 13:23:15 +02:00
|
|
|
|
2023-11-06 20:09:30 +01:00
|
|
|
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):
|
2023-11-06 19:43:24 +01:00
|
|
|
if new_text is None:
|
|
|
|
new_text = self.text
|
|
|
|
|
2023-11-11 20:21:54 +01:00
|
|
|
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.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)
|
|
|
|
|
2023-11-06 20:09:30 +01:00
|
|
|
new_callable_pos = None
|
|
|
|
|
2023-11-06 19:43:24 +01:00
|
|
|
if new_pos is None:
|
2023-11-06 20:09:30 +01:00
|
|
|
new_pos = self.position
|
|
|
|
new_callable_pos = self.callable_position
|
|
|
|
|
2023-11-11 20:21:54 +01:00
|
|
|
if not new_callable_pos is None:
|
|
|
|
new_pos = new_callable_pos(self.size)
|
|
|
|
|
2023-11-12 14:27:41 +01:00
|
|
|
elif new_pos is tuple:
|
2023-11-06 20:09:30 +01:00
|
|
|
new_callable_pos = None
|
|
|
|
|
2023-11-12 14:27:41 +01:00
|
|
|
else:
|
2023-11-11 20:21:54 +01:00
|
|
|
new_pos = new_pos(self.size)
|
2023-11-06 20:09:30 +01:00
|
|
|
new_callable_pos = new_pos
|
2023-11-06 19:43:24 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-11-06 20:09:30 +01:00
|
|
|
self.position = new_pos
|
|
|
|
self.callable_position = new_callable_pos
|
2023-11-06 19:43:24 +01:00
|
|
|
self.surface = new_surface
|
|
|
|
self.function = new_func
|
|
|
|
|
|
|
|
self.button = Button(new_func, new_bg_color, key=new_key)
|
|
|
|
|
2023-11-11 20:21:54 +01:00
|
|
|
self.background = self.generate_background()
|
2023-11-06 19:43:24 +01:00
|
|
|
|
|
|
|
def blit(self, surface: pygame.Surface=None):
|
2023-11-06 20:09:30 +01:00
|
|
|
x, y = self.position
|
2023-07-21 13:23:15 +02:00
|
|
|
|
2023-11-06 19:43:24 +01:00
|
|
|
if surface is None:
|
|
|
|
surface = self.surface
|
|
|
|
|
2023-11-05 19:43:41 +01:00
|
|
|
pygame.draw.rect(surface, self.bg_color, self.background)
|
2023-11-05 15:49:14 +01:00
|
|
|
surface.blit(self.text_object, (x + self.padding, y + self.padding))
|
2023-07-21 13:23:15 +02:00
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
def check(self, mouse_pos, pressed):
|
|
|
|
return self.button.check(mouse_pos, pressed)
|
2023-07-21 13:23:15 +02:00
|
|
|
|
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
# class HoverTitle: # coming soon
|
|
|
|
# def __init__(self, position: tuple=None, size: tuple=None, page: str=None, popup: str = None, collideable=None, delay: int=20):
|
|
|
|
# self.x, self.y = position
|
|
|
|
# self.width, self.height = size
|
|
|
|
# self.page = page
|
|
|
|
# self.popup = popup
|
|
|
|
# self.collideable = collideable
|
|
|
|
# self.delay = delay
|
2023-07-21 13:23:15 +02:00
|
|
|
|
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
def bt_from_ca(ca, function): # bt_from_ca = button from collide able
|
|
|
|
return Button(function, ca)
|
2023-08-25 16:15:01 +02:00
|
|
|
|
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
def bt_from_cords(function, position: tuple, size: tuple):
|
|
|
|
return Button(function, position=position, size=size)
|
2023-08-25 16:15:01 +02:00
|
|
|
|
|
|
|
|
2023-11-05 15:49:14 +01:00
|
|
|
def check_buttons(mouse_pos, pressed):
|
|
|
|
for button in buttons:
|
|
|
|
button.check(mouse_pos, pressed)
|