|
| 1 | +# SPDX-FileCopyrightText: 2021 Tim Cocks |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +""" |
| 5 | +
|
| 6 | +`icon_widget` |
| 7 | +================================================================================ |
| 8 | +A touch enabled widget that includes an icon image with a small text label |
| 9 | +centered below it. |
| 10 | +
|
| 11 | +* Author(s): Tim Cocks |
| 12 | +
|
| 13 | +Implementation Notes |
| 14 | +-------------------- |
| 15 | +
|
| 16 | +**Hardware:** |
| 17 | +
|
| 18 | +**Software and Dependencies:** |
| 19 | +
|
| 20 | +* Adafruit CircuitPython firmware for the supported boards: |
| 21 | + https://github.com/adafruit/circuitpython/releases |
| 22 | +
|
| 23 | +""" |
| 24 | + |
| 25 | + |
| 26 | +import terminalio |
| 27 | +from displayio import TileGrid |
| 28 | +import adafruit_imageload |
| 29 | +from adafruit_display_text import bitmap_label |
| 30 | +from adafruit_displayio_layout.widgets.control import Control |
| 31 | +from adafruit_displayio_layout.widgets.widget import Widget |
| 32 | + |
| 33 | + |
| 34 | +class IconWidget(Widget, Control): |
| 35 | + |
| 36 | + """ |
| 37 | + A touch enabled widget that holds an icon image loaded with |
| 38 | + adafruit_imageload and a text label centered beneath it. |
| 39 | +
|
| 40 | + :param string label_text: the text that will be shown beneath the icon image. |
| 41 | + :param string icon: the filepath of the bmp image to be used as the icon. |
| 42 | +
|
| 43 | + :param int x: x location the icon widget should be placed. Pixel coordinates. |
| 44 | + :param int y: y location the icon widget should be placed. Pixel coordinates. |
| 45 | + :param anchor_point: (X,Y) values from 0.0 to 1.0 to define the anchor point relative to the |
| 46 | + widget bounding box |
| 47 | + :type anchor_point: Tuple[float,float] |
| 48 | + :param int anchored_position: (x,y) pixel value for the location of the anchor_point |
| 49 | + :type anchored_position: Tuple[int, int] |
| 50 | + :param int max_size: (Optional) this will get passed through to the |
| 51 | + displayio.Group constructor. If omitted we default to |
| 52 | + grid_size width * grid_size height to make room for all (1, 1) sized cells. |
| 53 | +
|
| 54 | + """ |
| 55 | + |
| 56 | + def __init__(self, label_text, icon, **kwargs): |
| 57 | + super().__init__(**kwargs) |
| 58 | + image, palette = adafruit_imageload.load(icon) |
| 59 | + tile_grid = TileGrid(image, pixel_shader=palette) |
| 60 | + self.append(tile_grid) |
| 61 | + _label = bitmap_label.Label( |
| 62 | + terminalio.FONT, |
| 63 | + scale=1, |
| 64 | + text=label_text, |
| 65 | + anchor_point=(0.5, 0), |
| 66 | + anchored_position=(image.width // 2, image.height), |
| 67 | + ) |
| 68 | + self.append(_label) |
| 69 | + self.touch_boundary = ( |
| 70 | + self.x, |
| 71 | + self.y, |
| 72 | + image.width, |
| 73 | + image.height + _label.bounding_box[3], |
| 74 | + ) |
| 75 | + |
| 76 | + def contains(self, touch_point): # overrides, then calls Control.contains(x,y) |
| 77 | + |
| 78 | + """Checks if the IconWidget was touched. Returns True if the touch_point is |
| 79 | + within the IconWidget's touch_boundary. |
| 80 | +
|
| 81 | + :param touch_point: x,y location of the screen, converted to local coordinates. |
| 82 | + :type touch_point: Tuple[x,y] |
| 83 | + :return: Boolean |
| 84 | + """ |
| 85 | + |
| 86 | + touch_x = ( |
| 87 | + touch_point[0] - self.x |
| 88 | + ) # adjust touch position for the local position |
| 89 | + touch_y = touch_point[1] - self.y |
| 90 | + |
| 91 | + return super().contains((touch_x, touch_y, 0)) |
0 commit comments