From: Skullheadx <94652084+Skullheadx@users.noreply.github.com> Date: Fri, 14 Apr 2023 12:39:21 +0000 (-0400) Subject: button and mouse interaction X-Git-Url: http://git.skullheadx.com/nixos/static/phil/index.html?a=commitdiff_plain;h=da50bb42a250665a4f297db7604c604f8e521fb7;p=PygameGUIEngine.git button and mouse interaction fixed issue with clicking then moving onto button and clicking on button and moving out still activating func --- diff --git a/button.py b/button.py index 29309fc..ce88a25 100644 --- a/button.py +++ b/button.py @@ -4,10 +4,12 @@ from text import Label class Button(Label): - cooldown = 0.2 # seconds + cooldown = 1 # seconds + def __init__(self, x, y, text, font, size, text_color=Color.BLACK, background=Color.WHITE, border_color=Color.BLACK, border_width=1, border_radius=0, padding=10, func=None): - super().__init__(x, y, text, font, size, text_color, background, border_color, border_width, border_radius, padding) + super().__init__(x, y, text, font, size, text_color, background, border_color, border_width, border_radius, + padding) self.is_hover = False self.is_pressed = False self.is_clicked = False @@ -19,28 +21,32 @@ class Button(Label): self.last_click = 0 self.still_pressed = False - + self.click_disabled = False def update(self): mouse_pressed = pygame.mouse.get_pressed()[0] mouse_pos = pygame.mouse.get_pos() self.is_hover = self.rect.collidepoint(mouse_pos) + if mouse_pressed and not self.is_hover: + self.click_disabled = True + if self.click_disabled: + if not mouse_pressed: + self.click_disabled = False + else: + return self.is_clicked = self.is_hover and not mouse_pressed and self.is_pressed self.is_pressed = self.is_hover and mouse_pressed if self.still_pressed and not self.is_pressed: self.still_pressed = False - + if self.func is not None and self.is_hover: + self.func() if self.is_pressed: + self.rect.set_background_color(self.pressed_color) if pygame.time.get_ticks() - self.last_click > self.cooldown * 1000 and not self.still_pressed: - self.still_pressed = True self.last_click = pygame.time.get_ticks() - - self.rect.set_background_color(self.pressed_color) - if self.func is not None: - self.func() - + self.still_pressed = True elif self.is_hover: self.rect.set_background_color(self.hover_color) else: