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"
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):
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)
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),
)