From 6e269d5cd75c5ce869c6de4190c2edf4d899c32f Mon Sep 17 00:00:00 2001 From: Daniel Thompson Date: Mon, 18 Jan 2021 21:52:55 +0000 Subject: [PATCH] widgets: Add a button with a graphical icon Signed-off-by: Daniel Thompson --- wasp/widgets.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/wasp/widgets.py b/wasp/widgets.py index 4d17c02..970ab74 100644 --- a/wasp/widgets.py +++ b/wasp/widgets.py @@ -302,6 +302,34 @@ class Checkbox(): return True return False +class GfxButton(): + """A button with a graphical icon.""" + def __init__(self, x, y, gfx): + self._im = (x, y) + self.gfx = gfx + + def draw(self): + """Draw the button.""" + im = self._im + wasp.watch.drawable.blit(self.gfx, im[0], im[1]) + + def touch(self, event): + x = event[1] + y = event[2] + + # Adopt a slightly oversized hit box + im = self._im + gfx = self.gfx + x1 = im[0] - 10 + x2 = x1 + gfx[1] + 20 + y1 = im[1] - 10 + y2 = y1 + gfx[2] + 20 + + if x >= x1 and x < x2 and y >= y1 and y < y2: + return True + + return False + _SLIDER_KNOB_DIAMETER = const(40) _SLIDER_KNOB_RADIUS = const(_SLIDER_KNOB_DIAMETER // 2) _SLIDER_WIDTH = const(220)