wobbl_tools/pg.py

162 lines
4.6 KiB
Python

#!/usr/bin/python3
import pygame
from tools import text
pygame.init()
log = text.Log()
buttonlist = False
buttons = []
# colors
gray = (40, 40, 40)
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: tuple=None, size: tuple=None):
self.function = function
self.ca = ca
if not position is None and not size is None:
self.x, self.y = position
self.w, self.h = size
self.active = True
def check(self, mouse_pos: tuple):
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:
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: tuple=None, size: tuple=None, key: int=0): # ca = collide able, key: 0 = left 1 = mouse wheel pressed 2 = right
if not position is None and not size is None:
self.x, self.y = position
self.width, self.height = size
self.function = function
self.active = True
self.ca = ca
self.key = key
self.hover = Hover(function, ca, position, size)
if buttonlist:
buttons.append(self)
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)
return False
class TextButton:
def __init__(self, text: str, pos: tuple, 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.function = function
self.text_color = text_color
self.bg_color = bg_color
self.font = font
self.padding = padding
self.text_object = self.generate_text(text)
self.background = self.generate_background()
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 = w + self.padding * 2, h + self.padding * 2
return pygame.Rect(self.pos, (w, h))
# def update(self, new_text: str=None, new_pos: tuple = None, new_func=None, new_key: int=None, new_text_color: tuple=None, new_bg_color: tuple=None, new_font: pygame.font.Font=None): # coming soon
# if new_text is None:
# new_text = self.text
#
# if new_pos is None:
# new_pos = self.pos
#
# if new_func is None:
# new_func = self.function
#
# if new_key is None:
# new_key = self.button.key
#
# if new_text_color is None:
# new_text_color = self.text_color
#
# if new_bg_color is None:
# bg_color = self.bg_color
#
# if new_font is None:
# new_font = self.font
def blit(self, surface: pygame.Surface):
x, y = self.pos
pygame.draw.rect(surface, self.bg_color, self.background)
surface.blit(self.text_object, (x + self.padding, y + self.padding))
def check(self, mouse_pos, pressed):
return self.button.check(mouse_pos, pressed)
# 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
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)