diff options
| author | Daniel Thompson <daniel@redfelineninja.org.uk> | 2021-01-10 10:30:27 (GMT) |
|---|---|---|
| committer | Daniel Thompson <daniel@redfelineninja.org.uk> | 2021-01-10 10:30:27 (GMT) |
| commit | b6b30238c6f714986f6b5a54a79bdb5672b534c1 (patch) | |
| tree | 97ecf7317c634af3a39e0d43633516ae1986d36d /wasp/widgets.py | |
| parent | 8325177ec84c8dbb43e57bfd23054422a73c9e34 (diff) | |
widgets: button: Add a simple Button widget
Signed-off-by: Daniel Thompson <daniel@redfelineninja.org.uk>
Diffstat (limited to 'wasp/widgets.py')
| -rw-r--r-- | wasp/widgets.py | 40 |
1 files changed, 40 insertions, 0 deletions
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): |
