Simplified some things.

This commit is contained in:
The Wobbler 2023-11-05 15:49:14 +01:00
parent 2b3ac6a31d
commit 2ab5e56969

207
pg.py
View file

@ -3,136 +3,133 @@
import pygame import pygame
import text import text
ignore_errors = False
buttons = {
"popup_none": []
}
mouse_button_hold = False # Variable is True, while a mouse button is down to prevent from clicking multiple buttons at a time.
log = text.Log() log = text.Log()
buttonlist = False
buttons = []
class NotEnoughInformationError(Exception):
def __init__(self, error): # colors
super().__init__(error) gray = (40, 40, 40)
log.write(error, "error") 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
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.position((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: class Button:
def __init__(self, position: tuple, size: tuple, function, page: str, mouse_button: str="left", button_type: str="click"): 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
self.x, self.y = position self.x, self.y = position
self.width, self.height = size self.width, self.height = size
self.function = function self.function = function
self.mouse_button = mouse_button self.active = True
self.type = button_type self.ca = ca
self.key = key
if page not in buttons: self.hover = Hover(function, ca, position, size)
buttons[page] = []
buttons[page].append(self) if buttonlist:
buttons.append(self)
def check(self, mouse_pos: tuple): # check if a button is pressed def check(self, mouse_pos: tuple, pressed): # check if the button is pressed
x = self.x if self.active:
y = self.y if pressed[self.key]:
return self.hover.check(mouse_pos)
w = self.width return False
h = self.height
mx, my = mouse_pos
if mx >= x and mx <= x + w and my >= y and my <= y + h:
self.function()
class ButtonFromCollideable: class TextButton:
def __init__(self, rect: pygame.Rect, function, page: str, mouse_button: str="left", button_type: str="click"): def __init__(self, text: str, function=None, key: int=0, text_color: tuple=white, bg_color: tuple=gray, font: pygame.font.Font=default_font, padding: int=8):
self.rect = rect self.text = text
self.function = function self.function = function
self.page = page self.text_color = text_color
self.mouse_button = mouse_button self.bg_color = bg_color
self.button_type = button_type self.font = font
self.padding = padding
if page not in buttons: self.text_object = self.generate_text(text)
buttons[page] = [] self.background = self.generate_background()
buttons[page].append(self) self.button = Button(function, self.background, key=key)
def check(self, mouse_pos: tuple): def generate_text(self, text):
mx, my = mouse_pos self.text = text
if self.rect.collidepoint((mx, my)): return self.font.render(text, True, self.text_color)
self.function()
def generate_background(self):
w, h = self.text_object.get_size()
w, h = w + self.padding * 2, h + self.padding * 2
return pygame.Surface((w, h))
def blit(self, surface: pygame.Surface, pos: tuple):
x, y = pos
surface.blit(self.background, pos)
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: # class HoverTitle: # coming soon
def __init__(self, position: tuple=None, size: tuple=None, page: str=None, popup: str = None, collideable=None, delay: int=20): # def __init__(self, position: tuple=None, size: tuple=None, page: str=None, popup: str = None, collideable=None, delay: int=20):
if position is None or size is None and collideable is None: # self.x, self.y = position
error = "You have to either pass position and size as argument or something collideable." # self.width, self.height = size
if ignore_errors: # self.page = page
log.write(error, "error") # self.popup = popup
# self.collideable = collideable
else: # self.delay = delay
raise NotEnoughInformationError(error)
self.x, self.y = position # Make variables class wide
self.width, self.height = size
self.page = page
self.popup = popup
self.collideable = collideable
self.delay = delay
def update_2(pressed_mousebuttons: tuple): # call this function before clock.tick() but after everything else in your loop def bt_from_ca(ca, function): # bt_from_ca = button from collide able
if not pressed_mousebuttons[0] and not pressed_mousebuttons[1] and not pressed_mousebuttons[2]: return Button(function, ca)
global mouse_button_hold
mouse_button_hold = False
def bt_from_cords(function, position: tuple, size: tuple):
def check_button(button: Button, pressed: tuple, mouse_button: str, mouse_position: tuple): return Button(function, position=position, size=size)
mx, my = mouse_position # Make variable names shorter
button_name_to_int = { # Convert button name to button number for better understanding.
"left": 0,
"middle": 1,
"right": 2
}
if pressed[button_name_to_int[mouse_button]]: # If the selected mouse button is pressed
if button.mouse_button == mouse_button: # If the button can be clicked with the selected mouse button
if button.type == "click": # If the click has to start on the button
if not mouse_button_hold: # If the click just started
button.check((mx, my)) # Check if the button is clicked.
elif button.type == "press": # If the click can start everywhere outside the button.
button.check((mx, my)) # Check if the button is pressed.
def check_buttons(mouse_pos: tuple, mouse_buttons, page: str, popups: list=[]): def check_buttons(mouse_pos, pressed):
global mouse_button_hold # Make mouse_button_hold global to change it script wide. for button in buttons:
button.check(mouse_pos, pressed)
pressed = mouse_buttons # Make variable names shorter
for button in buttons[page]: # Check for all buttons that are on the current page.
check_button(button, pressed, "left", mouse_pos) # Check buttons that can be clicked with the left mouse button.
check_button(button, pressed, "middle", mouse_pos) # Check buttons that can be clicked with the middle mouse button.
check_button(button, pressed, "right", mouse_pos) # Check buttons that can be clicked with the right mouse button.
for popup in popups: # Check for all popups
for button in buttons[popup]: # Check for all buttons that are on the popup.
check_button(button, pressed, "left", mouse_pos) # Check buttons that can be clicked with the left mouse button.
check_button(button, pressed, "middle", mouse_pos) # Check buttons that can be clicked with the middle mouse button.
check_button(button, pressed, "right", mouse_pos) # Check buttons that can be clicked with the right mouse button.
if pressed[0] or pressed[1] or pressed[2]: # If the left, middle or right mouse button is pressed.
mouse_button_hold = True # Buttonpress has already begun.
def example():
ht = HoverTitle()
if __name__ == "__main__":
example()