|
| 1 | +import displayio |
| 2 | +from adafruit_display_text.text_area import TextArea |
| 3 | +from adafruit_bitmap_font import bitmap_font |
| 4 | +from adafruit_display_shapes.rect import Rect |
| 5 | +from adafruit_display_shapes.roundrect import RoundRect |
| 6 | + |
| 7 | +class Button(): |
| 8 | + RECT = const(0) |
| 9 | + ROUNDRECT = const(1) |
| 10 | + def __init__(self, *, x, y, width, height, style=RECT, |
| 11 | + fill_color=None, outline_color=0x808080, |
| 12 | + label=None, label_font=None, label_color=0x808080): |
| 13 | + self.x = x |
| 14 | + self.y = y |
| 15 | + self.width = width |
| 16 | + self.height = height |
| 17 | + self._font = label_font |
| 18 | + |
| 19 | + self.group = displayio.Group() |
| 20 | + |
| 21 | + if outline_color or fill_color: |
| 22 | + self.body = Rect(x, y, width, height, |
| 23 | + fill=fill_color, outline=outline_color) |
| 24 | + self.group.append(self.body) |
| 25 | + |
| 26 | + if label: # button with text label |
| 27 | + if not label_font: |
| 28 | + raise RuntimeError("Please provide label font") |
| 29 | + dims = label_font.text_bounding_box(label) |
| 30 | + if dims[2] >= width or dims[3] >= height: |
| 31 | + raise RuntimeError("Button not large enough for label") |
| 32 | + self.label = TextArea(label_font, text=label) |
| 33 | + self.label.x = x + (width - dims[2])//2 |
| 34 | + self.label.y = y + (height - dims[3]) |
| 35 | + self.label.color = label_color |
| 36 | + self.group.append(self.label) |
| 37 | + print(dims) |
| 38 | + |
| 39 | + """ |
| 40 | + #else: # ok just a bounding box |
| 41 | + #self.bodyshape = displayio.Shape(width, height) |
| 42 | + #self.group.append(self.bodyshape) |
| 43 | + """ |
| 44 | + def contains(self, point): |
| 45 | + return (self.x <= point[0] <= self.x+self.width) and (self.y <= point[1] <= self.y+self.height) |
0 commit comments