From c483d92a6648c30d50b27df6fd54c0f1848269d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Rudowicz?= Date: Mon, 9 Sep 2024 19:28:55 +0200 Subject: [PATCH] Slightly move buttons on mouse down --- menu.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/menu.py b/menu.py index 48c8d8c..3c7a045 100644 --- a/menu.py +++ b/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 \