]> git.mar77i.info Git - zenbook_gui/commitdiff
allow sliders with handle width
authormar77i <mar77i@protonmail.ch>
Thu, 13 Feb 2025 18:41:05 +0000 (19:41 +0100)
committermar77i <mar77i@protonmail.ch>
Thu, 13 Feb 2025 18:41:05 +0000 (19:41 +0100)
ui/slider.py

index cb6494391f82107d6cd7312b013d3f2cc3ca8ab2..3ca12f90fb42adf30e38906740ff8c8c6450860f 100644 (file)
@@ -7,12 +7,18 @@ class Slider(Child):
     HORIZONTAL = 0
     VERTICAL = 1
 
-    def __init__(self, parent, rect, direction, value=0, callback=None):
+    def __init__(
+        self, parent, rect, direction, value=0, handle_size=None, callback=None
+    ):
         super().__init__(parent)
         self.rect = rect
         self.direction = direction
-        self.extent = (self.rect.width - 1, self.rect.height - 1)[direction]
+        self.extent = self.rect.size[direction]
         self.value = value
+        self.handle_size = handle_size
+        if handle_size is None:
+            handle_size = 1
+        self.extent -= handle_size
         self.callback = callback
         self.pushed = False
 
@@ -46,11 +52,13 @@ class Slider(Child):
             self.pushed = False
 
     def draw(self):
-        pygame.draw.rect(self.surf, "gray34", self.rect.inflate((8, 8)))
+        pygame.draw.rect(self.surf, "gray34", self.rect.inflate((8, 8)), 4)
         pygame.draw.rect(self.surf, "black", self.rect)
-        self.draw_cursor(self.surf.subsurface(self.rect))
+        (
+            self.draw_cursor, self.draw_cursor_line
+        )[self.handle_size is None](self.surf.subsurface(self.rect))
 
-    def draw_cursor(self, subsurf):
+    def draw_cursor_line(self, subsurf):
         value = self.value
         color = "gray"
         if value < 0:
@@ -61,7 +69,28 @@ class Slider(Child):
             color = "dimgray"
         if self.direction == self.HORIZONTAL:
             start_pos, end_pos = (value, 0), (value, self.rect.height)
-        else:
+        else:  # self.direction == self.VERTICAL:
             value = self.extent - value
             start_pos, end_pos = (0, value), (self.rect.width, value)
         pygame.draw.line(subsurf, color, start_pos, end_pos, 8)
+
+    def draw_cursor(self, subsurf):
+        value = self.value
+        color = "gray"
+        if value < 0:
+            color = "dimgray"
+            value = -value
+        if value > self.extent:
+            value = int(self.extent * (self.extent / value))
+            color = "dimgray"
+        if self.direction == self.HORIZONTAL:
+            rect = pygame.Rect(
+                (value, 0),
+                (self.handle_size, self.rect.height),
+            )
+        else:  # self.direction == self.VERTICAL:
+            rect = pygame.Rect(
+                (0, value),
+                (self.rect.width, self.handle_size),
+            )
+        pygame.draw.rect(subsurf, color, rect)