wobbl_tools/pygame_tools/widgets/hover.py
EKNr1 4721e5e7fa Rearranged folder structure in the pygame tools.
(No warranty that everything still works. )
2024-12-05 18:47:13 +01:00

37 lines
No EOL
1.1 KiB
Python

#!/usr/bin/python3
from wobbl_tools.pygame_tools.utils import *
class Hover:
"""
Used to execute a function when the mouse hovers over a rectangle.
"""
def __init__(self, rect: pygame.Rect, function=None, args: tuple=None):
"""
:param rect: A pygame.Rect, if the mouse hovers over this rectangle, the function gets executed.
:param function: The function that gets executed when the mouse hovers over the rect.
:param tuple args: A tuple that contains the arguments that will get passed to the function.
"""
self.rect = rect
self.function = function
self.args = args
self.active = True
def check(self, mouse_pos: tuple):
if self.active:
mx, my = mouse_pos
if self.rect.collidepoint((mx, my)):
if not self.function is None:
if self.args is None:
self.function()
else:
self.function(*self.args)
return True
return False