wobbl_tools/pg.py

269 lines
8.3 KiB
Python
Raw Normal View History

2023-07-21 13:23:15 +02:00
#!/usr/bin/python3
import pygame
2024-02-25 15:03:11 +01:00
from wobbl_tools import text
from typing import Union, Callable
2023-07-21 13:23:15 +02:00
2024-01-05 22:43:49 +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
2024-01-05 22:46:06 +01:00
black = (8, 8, 8)
2023-11-05 15:49:14 +01:00
gray = (40, 40, 40)
2024-01-05 22:46:06 +01:00
light_gray = (128, 128, 128)
2023-11-05 15:49:14 +01:00
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:
2024-03-17 14:13:55 +01:00
def __init__(self, rect: pygame.Rect, function=None):
self.rect = rect
2023-11-05 15:49:14 +01:00
self.function = function
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
2024-03-17 14:13:55 +01:00
if self.rect.collidepoint((mx, my)):
if not self.function is None:
self.function()
2023-08-25 16:15:01 +02:00
2024-03-17 14:13:55 +01:00
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:
2024-03-17 14:13:55 +01:00
def __init__(self, rect: pygame.Rect, function=None, key: int=0): # key: 0 = left 1 = mouse wheel pressed 2 = right
self.rect = rect
2023-11-05 15:49:14 +01:00
self.function = function
self.key = key
2023-07-21 13:23:15 +02:00
2024-03-17 14:13:55 +01:00
self.active = True
self.hover = Hover(rect, function)
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:
2024-03-17 14:13:55 +01:00
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
2024-03-17 14:13:55 +01:00
self.position = position
self.text_color = text_color
self.font = font
2023-11-05 15:49:14 +01:00
self.function = function
2024-03-17 14:13:55 +01:00
self.key = key
2023-11-05 15:49:14 +01:00
self.bg_color = bg_color
self.padding = padding
2024-03-17 14:13:55 +01:00
self.border_radius = border_radius
self.line_thickness = line_thickness
2023-10-31 19:31:22 +01:00
2024-03-17 14:13:55 +01:00
self.surface, self.size = self.generate_surface()
self.rect = self.make_rect()
2023-10-31 19:31:22 +01:00
2024-03-17 14:13:55 +01:00
self.button = Button(self.rect, self.function, self.key)
2023-10-31 19:31:22 +01:00
2024-03-17 14:13:55 +01:00
def generate_surface(self):
text_object = self.font.render(self.text, True, self.text_color)
2023-07-21 13:23:15 +02:00
2024-03-17 14:13:55 +01:00
w, h = text_object.get_size()
w, h = w + self.padding[0] * 2, h + self.padding[1] * 2
2023-07-21 13:23:15 +02:00
2024-03-17 14:13:55 +01:00
surface = pygame.Surface((w, h), pygame.SRCALPHA)
2024-03-17 14:13:55 +01:00
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]))
2024-03-17 14:13:55 +01:00
return surface, (w, h)
2024-03-17 14:13:55 +01:00
def make_rect(self):
return pygame.Rect(self.position, self.size)
2024-03-17 14:13:55 +01:00
def draw(self, surface: pygame.Surface):
surface.blit(self.surface, self.position)
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
2024-01-05 22:46:06 +01:00
class TextInput:
def __init__(
self,
position: Union[tuple, Callable],
surface: pygame.Surface,
width: int=None,
font: pygame.font.Font=default_font,
bg_color: tuple=(255, 255, 255),
text_color: tuple=black,
desc_text_color: tuple=light_gray,
desc_text: str="",
default_text: str="",
padding: tuple=(8, 8),
cursor_blink_interval: int=30,
border_radius: int=-1,
max_length: int=128
):
self.surface = surface
self.font = font
self.bg_color = bg_color
self.text_color = text_color
self.desc_text_color = desc_text_color
self.desc_text = desc_text
self.text = default_text
self.padding = padding
self.focused = True
self.cursor_blink_interval = cursor_blink_interval
self.ticks_til_blink = cursor_blink_interval
self.blink = False
self.border_radius = border_radius
self.max_length = max_length
self.cursor_position = len(default_text)
sx, sy = surface.get_size()
if width is None:
self.width = round(sx / 4)
else:
self.width = width
self.description_text_object = font.render(self.desc_text, True, self.desc_text_color)
self.text_object = font.render(self.text, True, self.text_color)
self.text_blink_object = font.render(self.text + "|", True, self.text_color)
self.height = self.text_object.get_height()
if type(position) is tuple:
self.position = position
else:
self.position = position(size=self.size)
self.callable_position = position
self.rect = self.generate_rect()
def generate_rect(self):
x, y = self.position
px, py = self.padding
x -= px
y -= py
return pygame.Rect((x, y), (self.width + px * 2, self.height + py * 2))
def blit(self):
pygame.draw.rect(self.surface, self.bg_color, self.rect, border_radius=self.border_radius)
if self.focused:
if self.blink:
self.surface.blit(self.text_blink_object, self.position)
else:
self.surface.blit(self.text_object, self.position)
def loop(self):
"""Call this function every tick to make the cursor blink."""
self.ticks_til_blink -= 1
if self.ticks_til_blink == -1:
self.ticks_til_blink = self.cursor_blink_interval
self.blink = not self.blink
def keypress(self, event: pygame.event, pressed_keys, special_keys):
"""
Call this function when a key is pressed to get user input like this:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
TextInput.keypress(event, pygame.key.get_pressed(), pygame.key.get_mods())
"""
if self.max_length is None or len(self.text) <= self.max_length:
key_unicode = event.unicode
key = event.key
if key < 32 or key > 101106 or special_keys & pygame.KMOD_CTRL or special_keys & pygame.KMOD_RCTRL or key > 126 and key < 161:
if key == pygame.K_BACKSPACE:
if self.cursor_position > 0:
self.text = text.rsap(self.text, "", self.cursor_position - 1)
self.cursor_position -= 1
elif key == pygame.K_LEFT:
if self.cursor_position > 0:
self.cursor_position -= 1
elif key == pygame.K_RIGHT:
if self.cursor_position < len(self.text):
self.cursor_position += 1
else:
self.text = text.asap(self.text, key_unicode, self.cursor_position)
self.cursor_position += 1
self.regenerate_objects()
def regenerate_objects(self):
self.rect = self.generate_rect()
self.text_object = self.font.render(self.text, True, self.text_color)
self.text_blink_object = self.font.render(text.asap(self.text, "|", self.cursor_position), True, self.text_color)
t_width, t_height = self.text_blink_object.get_size()
if t_width > self.width:
self.text_blink_object = crop_surface(self.text_blink_object, (t_width - (t_width - (t_width - self.width)), 0), (t_width - (t_width - self.width), t_height))
self.text_object = crop_surface(self.text_object, (t_width - (t_width - (t_width - self.width)), 0), (t_width - (t_width - self.width), t_height))
def set_rot_point(img, pos):
w, h = img.get_size()
w, h = w * 2, h * 2
img2 = pygame.Surface((w, h), pygame.SRCALPHA)
img2.blit(img, (w / 2 - pos[0], h / 2 - pos[1]))
return img2, (w, h)
2024-01-05 22:43:49 +01:00
def crop_surface(surface: pygame.Surface, position: tuple, size: tuple):
new_surface = pygame.Surface(size, flags=pygame.SRCALPHA)
x, y = position
x = -x
y = -y
new_surface.blit(surface, (x, y))
return new_surface
2023-11-05 15:49:14 +01:00
def check_buttons(mouse_pos, pressed):
for button in buttons:
button.check(mouse_pos, pressed)