diff --git a/TODO.rst b/TODO.rst index 3637fb7..bcc29af 100644 --- a/TODO.rst +++ b/TODO.rst @@ -44,7 +44,7 @@ Wasp-os * [ ] Widgets - * [ ] Add a button widget + * [X] Add a button widget * [X] Add a checkbox widget * [X] Add a spinner widget diff --git a/wasp/widgets.py b/wasp/widgets.py index 8509fbb..0fa4a61 100644 --- a/wasp/widgets.py +++ b/wasp/widgets.py @@ -215,6 +215,46 @@ class ScrollIndicator: if self.down: draw.blit(icons.down_arrow, self._pos[0], self._pos[1]+13, fg=color) +class Button(): + """A button with a text label.""" + def __init__(self, x, y, w, h, label): + self._im = (x, y, w, h, label) + + def draw(self): + """Draw the button.""" + draw = wasp.watch.drawable + im = self._im + bg = draw.darken(wasp.system.theme('ui')) + frame = wasp.system.theme('mid') + txt = wasp.system.theme('bright') + + draw.fill(bg, im[0], im[1], im[2], im[3]) + draw.set_color(txt, bg) + draw.set_font(fonts.sans24) + draw.string(im[4], im[0], im[1]+(im[3]//2)-12, width=im[2]) + + draw.fill(frame, im[0],im[1], im[2], 2) + draw.fill(frame, im[0], im[1]+im[3]-2, im[2], 2) + draw.fill(frame, im[0], im[1], 2, im[3]) + draw.fill(frame, im[0]+im[2]-2, im[1], 2, im[3]) + + def touch(self, event): + """Handle touch events.""" + x = event[1] + y = event[2] + + # Adopt a slightly oversized hit box + im = self._im + x1 = im[0] - 10 + x2 = x1 + im[2] + 20 + y1 = im[1] - 10 + y2 = y1 + im[3] + 20 + + if x >= x1 and x < x2 and y >= y1 and y < y2: + return True + + return False + class Checkbox(): """A simple (labelled) checkbox.""" def __init__(self, x, y, label=None):