widgets: button: Add a simple Button widget

Signed-off-by: Daniel Thompson <daniel@redfelineninja.org.uk>
This commit is contained in:
Daniel Thompson 2021-01-10 10:30:27 +00:00
parent 8325177ec8
commit b6b30238c6
2 changed files with 41 additions and 1 deletions

View File

@ -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

View File

@ -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):