]> Skullheadx's Git Forge - PygameGUIEngine.git/commitdiff
button and mouse interaction
authorSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Fri, 14 Apr 2023 12:39:21 +0000 (08:39 -0400)
committerSkullheadx <94652084+Skullheadx@users.noreply.github.com>
Fri, 14 Apr 2023 12:39:21 +0000 (08:39 -0400)
fixed issue with clicking then moving onto button and clicking on button and moving out still activating func

button.py

index 29309fc981a63c33d7fdc494756d7efc91f8b408..ce88a25dae5176063b64b9634d4da62cb431dc05 100644 (file)
--- 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: