widgets: Checkbox: Add a simple checkbox widget
Signed-off-by: Daniel Thompson <daniel@redfelineninja.org.uk>
This commit is contained in:
parent
3ddb7fc13d
commit
632ec713a4
4 changed files with 63 additions and 3 deletions
BIN
res/checkbox.png
Normal file
BIN
res/checkbox.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6 KiB |
|
@ -25,10 +25,12 @@ class TestApp():
|
||||||
ICON = icons.app
|
ICON = icons.app
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.tests = ('Alarm', 'Button', 'Crash', 'Colours', 'Fill', 'Fill-H', 'Fill-V', 'Free Mem', 'Line', 'Notifications', 'RLE', 'String', 'Touch', 'Wrap')
|
self.tests = ('Alarm', 'Button', 'Checkbox', 'Crash', 'Colours', 'Fill', 'Fill-H', 'Fill-V', 'Free Mem', 'Line', 'Notifications', 'RLE', 'String', 'Touch', 'Wrap')
|
||||||
self.test = self.tests[0]
|
self.test = self.tests[0]
|
||||||
self.scroll = wasp.widgets.ScrollIndicator()
|
self.scroll = wasp.widgets.ScrollIndicator()
|
||||||
|
|
||||||
|
self._checkbox = wasp.widgets.Checkbox(4, 104, 'Check me')
|
||||||
|
|
||||||
self._sliders = (
|
self._sliders = (
|
||||||
wasp.widgets.Slider(32, 10, 90, 0xf800),
|
wasp.widgets.Slider(32, 10, 90, 0xf800),
|
||||||
wasp.widgets.Slider(64, 10, 140, 0x27e4),
|
wasp.widgets.Slider(64, 10, 140, 0x27e4),
|
||||||
|
@ -72,7 +74,9 @@ class TestApp():
|
||||||
self._draw()
|
self._draw()
|
||||||
|
|
||||||
def touch(self, event):
|
def touch(self, event):
|
||||||
if self.test == 'Colours':
|
if self.test == 'Checkbox':
|
||||||
|
self._checkbox.touch(event)
|
||||||
|
elif self.test == 'Colours':
|
||||||
if event[2] > 90:
|
if event[2] > 90:
|
||||||
s = self._sliders[(event[2] - 90) // 50]
|
s = self._sliders[(event[2] - 90) // 50]
|
||||||
s.touch(event)
|
s.touch(event)
|
||||||
|
@ -221,7 +225,9 @@ class TestApp():
|
||||||
if self.test == 'Alarm':
|
if self.test == 'Alarm':
|
||||||
draw.string("Press button to", 12, 24+24)
|
draw.string("Press button to", 12, 24+24)
|
||||||
draw.string("set alarm.", 12, 24+48)
|
draw.string("set alarm.", 12, 24+48)
|
||||||
if self.test == 'Crash':
|
elif self.test == 'Checkbox':
|
||||||
|
self._checkbox.draw()
|
||||||
|
elif self.test == 'Crash':
|
||||||
draw.string("Press button to", 12, 24+24)
|
draw.string("Press button to", 12, 24+24)
|
||||||
draw.string("throw exception.", 12, 24+48)
|
draw.string("throw exception.", 12, 24+48)
|
||||||
elif self.test == 'Colours':
|
elif self.test == 'Colours':
|
||||||
|
|
|
@ -332,3 +332,16 @@ no_button = (
|
||||||
b'?\x1c\xc1\x03\xc1?\x1c\xc1\x03\xc1?\x1c\xc1\x03\xc1?'
|
b'?\x1c\xc1\x03\xc1?\x1c\xc1\x03\xc1?\x1c\xc1\x03\xc1?'
|
||||||
b'\x1c\xc1\x03\xff\x1e?\x83'
|
b'\x1c\xc1\x03\xff\x1e?\x83'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# 2-bit RLE, generated from res/checkbox.png, 108 bytes
|
||||||
|
checkbox = (
|
||||||
|
b'\x02'
|
||||||
|
b' '
|
||||||
|
b'\x02\xdc\x03\xde\x01\xe4X\xc7Z\xc6Z\xc6Z\xc6T\x82'
|
||||||
|
b'D\xc6S\x84C\xc6R\x86B\xc6Q\x86C\xc6P\x86'
|
||||||
|
b'D\xc6O\x86E\xc6N\x86F\xc6M\x86G\xc6L\x86'
|
||||||
|
b'H\xc6D\x82E\x86I\xc6C\x84C\x86J\xc6B\x86'
|
||||||
|
b'A\x86K\xc6C\x8bL\xc6D\x89M\xc6E\x87N\xc6'
|
||||||
|
b'F\x85O\xc6G\x83P\xc6H\x81Q\xc6Z\xc6Z\xc6'
|
||||||
|
b'Z\xc7X\xe4\x01\xde\x03\xdc\x02'
|
||||||
|
)
|
||||||
|
|
|
@ -215,6 +215,47 @@ class ScrollIndicator:
|
||||||
if self.down:
|
if self.down:
|
||||||
draw.blit(icons.down_arrow, self._pos[0], self._pos[1]+13, fg=color)
|
draw.blit(icons.down_arrow, self._pos[0], self._pos[1]+13, fg=color)
|
||||||
|
|
||||||
|
class Checkbox():
|
||||||
|
"""A simple (labelled) checkbox."""
|
||||||
|
def __init__(self, x, y, label=None):
|
||||||
|
self._im = (x, y, label)
|
||||||
|
self.state = False
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
"""Draw the checkbox and label."""
|
||||||
|
draw = wasp.watch.drawable
|
||||||
|
im = self._im
|
||||||
|
if im[2]:
|
||||||
|
draw.string(im[2], im[0], im[1]+6)
|
||||||
|
self.update()
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
"""Draw the checkbox."""
|
||||||
|
draw = wasp.watch.drawable
|
||||||
|
im = self._im
|
||||||
|
if self.state:
|
||||||
|
c1 = wasp.system.theme('slider-default')
|
||||||
|
c2 = draw.lighten(c1, 15)
|
||||||
|
fg = c2
|
||||||
|
else:
|
||||||
|
c1 = 0
|
||||||
|
c2 = 0
|
||||||
|
fg = wasp.system.theme('accent-lo')
|
||||||
|
# Draw checkbox on the right margin if there is a label, otherwise
|
||||||
|
# draw at the natural location
|
||||||
|
x = 239 - 32 - 4 if im[2] else im[0]
|
||||||
|
draw.blit(icons.checkbox, x, im[1], fg, c1, c2)
|
||||||
|
|
||||||
|
def touch(self, event):
|
||||||
|
"""Handle touch events."""
|
||||||
|
y = event[2] + 4
|
||||||
|
im = self._im
|
||||||
|
if y >= im[1] and y < im[1]+40:
|
||||||
|
self.state = not self.state
|
||||||
|
self.update()
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
_SLIDER_KNOB_DIAMETER = const(40)
|
_SLIDER_KNOB_DIAMETER = const(40)
|
||||||
_SLIDER_KNOB_RADIUS = const(_SLIDER_KNOB_DIAMETER // 2)
|
_SLIDER_KNOB_RADIUS = const(_SLIDER_KNOB_DIAMETER // 2)
|
||||||
_SLIDER_WIDTH = const(220)
|
_SLIDER_WIDTH = const(220)
|
||||||
|
|
Loading…
Reference in a new issue