#!/usr/bin/python3 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() class NotEnoughInformationError(Exception): def __init__(self, error): super().__init__(error) log.write(error, "error") class Button: def __init__(self, position: tuple, size: tuple, function, page: str, mouse_button: str="left", button_type: str="click"): self.x, self.y = position self.width, self.height = size self.function = function self.mouse_button = mouse_button self.type = button_type if page not in buttons: buttons[page] = [] buttons[page].append(self) def check(self, mouse_pos: tuple): # check if a button is pressed x = self.x y = self.y 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() class ButtonFromCollideable: def __init__(self, rect: pygame.Rect, function, page: str, mouse_button: str="left", button_type: str="click"): self.rect = rect self.function = function self.page = page self.mouse_button = mouse_button self.button_type = button_type if page not in buttons: buttons[page] = [] buttons[page].append(self) def check(self, mouse_pos: tuple): mx, my = mouse_pos if self.rect.collidepoint((mx, my)): self.function() 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 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 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. def example(): ht = HoverTitle() if __name__ == "__main__": example()