]> git.mar77i.info Git - zenbook_gui/commitdiff
rotate <, > for spinner arrows.
authormar77i <mar77i@protonmail.ch>
Tue, 11 Feb 2025 08:55:07 +0000 (09:55 +0100)
committermar77i <mar77i@protonmail.ch>
Tue, 11 Feb 2025 08:55:07 +0000 (09:55 +0100)
ui/button.py
ui/spinner.py

index 52fe025fde3dd5aed8b1afaa8ab2e667412dd2e5..26345cfa29006d59a0f693988479a9b49f205b8e 100644 (file)
@@ -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):
index 4dbfca40b4756e3dfb5f1afd988ab4f37eb5ccee..86bd7d37528a0e0e5973a61aa4eddb9f7209786a 100644 (file)
@@ -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),
         )