mirror of https://git.sr.ht/~michalr/menu
Slightly move buttons on mouse down
This commit is contained in:
parent
a852710e19
commit
c483d92a66
18
menu.py
18
menu.py
|
@ -42,6 +42,8 @@ class IBoard:
|
|||
|
||||
|
||||
class Button:
|
||||
PRESS_OFFSET = 2
|
||||
|
||||
def __init__(self, position: pygame.Rect, text: str, cbk: Callable[[], None]):
|
||||
self.position: pygame.Rect
|
||||
self.set_position(position)
|
||||
|
@ -56,9 +58,21 @@ class Button:
|
|||
def set_position(self, position: pygame.Rect):
|
||||
self.position = position
|
||||
|
||||
def get_position(self) -> pygame.Rect:
|
||||
if pygame.mouse.get_pressed(3)[0] and self.pos_is_inside(pygame.mouse.get_pos()):
|
||||
return pygame.Rect(self.position.left + self.PRESS_OFFSET, self.position.top + self.PRESS_OFFSET,
|
||||
self.position.width, self.position.height)
|
||||
return self.position
|
||||
|
||||
def get_text_pos(self) -> pygame.Rect:
|
||||
if pygame.mouse.get_pressed(3)[0] and self.pos_is_inside(pygame.mouse.get_pos()):
|
||||
return pygame.Rect(self.text_pos.left + self.PRESS_OFFSET, self.text_pos.top + self.PRESS_OFFSET,
|
||||
self.text_pos.width, self.text_pos.height)
|
||||
return self.text_pos
|
||||
|
||||
def draw(self, screen: pygame.Surface):
|
||||
pygame.draw.rect(screen, "black", self.position, 0)
|
||||
self.font.render_to(screen, self.text_pos, self.text, fgcolor="pink")
|
||||
pygame.draw.rect(screen, "black", self.get_position(), 0)
|
||||
self.font.render_to(screen, self.get_text_pos(), self.text, fgcolor="pink")
|
||||
|
||||
def pos_is_inside(self, pos: Tuple) -> bool:
|
||||
return (self.position.left < pos[0] and (self.position.left + self.position.width) > pos[0]) and \
|
||||
|
|
Loading…
Reference in New Issue