Skip to content

Commit ec1ebf2

Browse files
authored
Merge pull request #12 from FoamyGuy/adding_icon_widget
Adding icon_widget
2 parents d6a7146 + 5a47fa8 commit ec1ebf2

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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))

docs/api.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@
2222
:inherited-members:
2323

2424
.. inheritance-diagram:: adafruit_displayio_layout.widgets.switch_round
25+
26+
.. automodule:: adafruit_displayio_layout.widgets.icon_widget
27+
:members:
28+
:member-order: bysource

docs/conf.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@
2828
# Uncomment the below if you use native CircuitPython modules such as
2929
# digitalio, micropython and busio. List the modules you use. Without it, the
3030
# autodoc module docs will fail to generate with a warning.
31-
autodoc_mock_imports = ["displayio", "adafruit_display_shapes"]
31+
autodoc_mock_imports = [
32+
"displayio",
33+
"adafruit_display_shapes",
34+
"terminalio",
35+
"adafruit_imageload",
36+
"adafruit_display_text",
37+
]
3238

3339

3440
intersphinx_mapping = {

0 commit comments

Comments
 (0)