From: mar77i Date: Tue, 11 Feb 2025 08:55:07 +0000 (+0100) Subject: rotate <, > for spinner arrows. X-Git-Url: https://git.mar77i.info/?a=commitdiff_plain;h=7b2afd226e83dc061222fcfdb3003e21daa3fb24;p=zenbook_gui rotate <, > for spinner arrows. --- diff --git a/ui/button.py b/ui/button.py index 52fe025..26345cf 100644 --- a/ui/button.py +++ b/ui/button.py @@ -12,6 +12,12 @@ class Button(Child): self.is_active = is_active self.pushed = False + def draw_value(self, color): + fs = self.font.render(self.value, True, color) + fs_size = fs.get_size() + center = self.rect.center + self.surf.blit(fs, (center[0] - fs_size[0] // 2, center[1] - fs_size[1] // 2)) + def draw(self): if not self.pushed: value_color = "lime" if self.is_active else "gray" @@ -20,10 +26,7 @@ class Button(Child): colors = ("darkgray", "lightgray", "black") pygame.draw.rect(self.surf, colors[0], self.rect) pygame.draw.rect(self.surf, colors[1], self.rect, 8) - fs = self.font.render(self.value, True, colors[2]) - fs_size = fs.get_size() - center = self.rect.center - self.surf.blit(fs, (center[0] - fs_size[0] // 2, center[1] - fs_size[1] // 2)) + self.draw_value(colors[2]) def handle_mousebuttondown(self, ev): if ev.button == 1 and self.rect.collidepoint(ev.pos): diff --git a/ui/spinner.py b/ui/spinner.py index 4dbfca4..86bd7d3 100644 --- a/ui/spinner.py +++ b/ui/spinner.py @@ -50,6 +50,33 @@ class RepeatButton(Button): self.dirty = True +class RepeatButtonRotate(RepeatButton): + @staticmethod + def x_crop(surf): + size = surf.get_size() + with pygame.PixelArray(surf) as pa: + state = 0 + start = 0 + end = size[0] - 1 + for x, col_x in enumerate(pa): + t_set = {c >> 24 for c in col_x} + if state == 0 and t_set != {0}: + start = x + state = 1 + elif state == 1 and t_set != {0}: + end = x + return surf.subsurface(pygame.Rect((start, 0), (end - start + 1, size[1]))) + + def draw_value(self, color): + fs = self.font.render(self.value, True, color) + fs = self.x_crop(fs) + fs = pygame.transform.rotate(fs, 90) + fs = self.x_crop(fs) + fs_size = fs.get_size() + center = self.rect.center + self.surf.blit(fs, (center[0] - fs_size[0] // 2, center[1] - fs_size[1] // 2)) + + class Spinner(Parent, Child): def __init__(self, parent, rect, callback, value=0): super().__init__(parent) @@ -65,22 +92,22 @@ class Spinner(Parent, Child): str(value), re.compile(r"[-+]?\d*").fullmatch, ) - RepeatButton( + RepeatButtonRotate( self, pygame.Rect( (rect.right - button_size[0], rect.top), button_size, ), - "^", + ">", # points up partial(self.spin_callback, 1), ) - RepeatButton( + RepeatButtonRotate( self, pygame.Rect( (rect.right - button_size[0], rect.top + button_size[1]), button_size, ), - "v", + "<", # points down partial(self.spin_callback, -1), )