From b35fd9fa7e4b25fb641d2990c47220ec6999f86c Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Fri, 21 Jul 2023 13:23:15 +0200 Subject: [PATCH] Moved tools.py to own project --- tools.py | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 tools.py diff --git a/tools.py b/tools.py new file mode 100644 index 0000000..6a5026c --- /dev/null +++ b/tools.py @@ -0,0 +1,102 @@ +#!/usr/bin/python3 + +import pygame + + +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. + + +class NotEnoughInformationError(Exception): + def __init__(self, error): + super().__init__(error) + + +class Button: # Create class Button to make creating buttons easier. + def __init__(self, position: tuple, size: tuple, function, page: str, mouse_button: str="left", rect: pygame.rect=None, button_type: str="click"): + self.x, self.y = position # Make variables class wide + self.width, self.height = size + self.function = function + self.mouse_button = mouse_button + self.rect = rect + self.type = button_type + + if not page in buttons: + buttons[page] = [] + + buttons[page].append(self) # Add button to button-list. + + def check(self, mouse_pos: tuple): # Check if the button is pressed. + x = self.x # Make variable names shorter + y = self.y + + w = self.width + h = self.height + + mx, my = mouse_pos + + if self.rect is None: + if mx >= x and mx <= x + w and my >= y and my <= y + h: + self.function() # If button pressed, execute the function. + + else: + if self.rect.collidepoint((mx, my)): # Check for collide point of rect + self.function() + + +class HoverTitle: + 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 # Make variables class wide + self.width, self.height = size + self.page = page + self.popup = popup + self.collideable = collideable + self.delay = delay + + 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: + print() + raise(NotEnoughInformationError(error)) + + +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 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.