From ef318f61b354e0e59dfb43e75f2d8dbfb657a440 Mon Sep 17 00:00:00 2001 From: mar77i Date: Thu, 13 Feb 2025 19:41:05 +0100 Subject: [PATCH] allow sliders with handle width --- ui/slider.py | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/ui/slider.py b/ui/slider.py index cb64943..3ca12f9 100644 --- a/ui/slider.py +++ b/ui/slider.py @@ -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) -- 2.51.0