From 2ab5e569690923390e18012d7d4ce6d05cc1fed2 Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Sun, 5 Nov 2023 15:49:14 +0100 Subject: [PATCH] Simplified some things. --- pg.py | 207 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 102 insertions(+), 105 deletions(-) diff --git a/pg.py b/pg.py index 8ad8666..27c0a7c 100644 --- a/pg.py +++ b/pg.py @@ -3,136 +3,133 @@ import pygame 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() +buttonlist = False +buttons = [] -class NotEnoughInformationError(Exception): - def __init__(self, error): - super().__init__(error) - log.write(error, "error") + +# 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 + 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: - 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.width, self.height = size self.function = function - self.mouse_button = mouse_button - self.type = button_type + self.active = True + self.ca = ca + self.key = key - if page not in buttons: - buttons[page] = [] + self.hover = Hover(function, ca, position, size) - buttons[page].append(self) + if buttonlist: + buttons.append(self) - def check(self, mouse_pos: tuple): # check if a button is pressed - x = self.x - y = self.y + 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) - w = self.width - h = self.height - - mx, my = mouse_pos - - if mx >= x and mx <= x + w and my >= y and my <= y + h: - self.function() + return False -class ButtonFromCollideable: - def __init__(self, rect: pygame.Rect, function, page: str, mouse_button: str="left", button_type: str="click"): - self.rect = rect +class TextButton: + 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.text = text self.function = function - self.page = page - self.mouse_button = mouse_button - self.button_type = button_type + self.text_color = text_color + self.bg_color = bg_color + self.font = font + self.padding = padding - if page not in buttons: - buttons[page] = [] + self.text_object = self.generate_text(text) + self.background = self.generate_background() - buttons[page].append(self) + self.button = Button(function, self.background, key=key) - def check(self, mouse_pos: tuple): - mx, my = mouse_pos + def generate_text(self, text): + self.text = text - if self.rect.collidepoint((mx, my)): - self.function() + 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.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: - 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: - error = "You have to either pass position and size as argument or something collideable." - if ignore_errors: - log.write(error, "error") - - else: - 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 +# 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 update_2(pressed_mousebuttons: tuple): # call this function before clock.tick() but after everything else in your loop - if not pressed_mousebuttons[0] and not pressed_mousebuttons[1] and not pressed_mousebuttons[2]: - global mouse_button_hold - - mouse_button_hold = False +def bt_from_ca(ca, function): # bt_from_ca = button from collide able + return Button(function, ca) - -def check_button(button: Button, pressed: tuple, mouse_button: str, mouse_position: tuple): - 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 bt_from_cords(function, position: tuple, size: tuple): + return Button(function, position=position, size=size) -def check_buttons(mouse_pos: tuple, mouse_buttons, page: str, popups: list=[]): - global mouse_button_hold # Make mouse_button_hold global to change it script wide. - - 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() +def check_buttons(mouse_pos, pressed): + for button in buttons: + button.check(mouse_pos, pressed)