Skip to content

Commit 94e60a7

Browse files
committed
Type annotations to SpriteButton, Further documentation changes.
Completed type annotations for the sprite_button.py file as well as further improvements to documentation.
1 parent fdf13e9 commit 94e60a7

File tree

3 files changed

+56
-41
lines changed

3 files changed

+56
-41
lines changed

adafruit_button/button.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ class Button(ButtonBase):
5353
Defaults to RECT.
5454
:param int|Tuple(int, int, int) fill_color: The color to fill the button. Defaults to 0xFFFFFF.
5555
:param int|Tuple(int, int, int) outline_color: The color of the outline of the button.
56-
:param str label: The text that appears inside the button. Defaults to not displaying the label.
57-
:param FontProtocol label_font: The button label font. Defaults to terminalio.FONT
56+
:param str label: The text that appears inside the button.
57+
:param FontProtocol label_font: The button label font. Defaults to ''terminalio.FONT''
5858
:param int|Tuple(int, int, int) label_color: The color of the button label text. Defaults to 0x0.
5959
:param int|Tuple(int, int, int) selected_fill: The fill color when the button is selected.
6060
Defaults to the inverse of the fill_color.

adafruit_button/button_base.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries
2+
# SPDX-FileCopyrightText: 2024 Channing Ramos
23
#
34
# SPDX-License-Identifier: MIT
45

@@ -9,7 +10,7 @@
910
UI Buttons for displayio
1011
1112
12-
* Author(s): Limor Fried
13+
* Author(s): Limor Fried, Channing Ramos
1314
1415
Implementation Notes
1516
--------------------
@@ -47,10 +48,12 @@ class ButtonBase(Group):
4748
:param int width: The width of the button in tiles.
4849
:param int height: The height of the button in tiles.
4950
:param str name: A name, or miscellaneous string that is stored on the button.
50-
:param str label: The text that appears inside the button. Defaults to not displaying the label.
51-
:param FontProtocol label_font: The button label font. Defaults to terminalio.FONT
52-
:param int|Tuple(int, int, int) label_color: The color of the button label text. Defaults to 0x0.
51+
:param str label: The text that appears inside the button.
52+
:param FontProtocol label_font: The button label font. Defaults to ''terminalio.FONT''
53+
:param int|Tuple(int, int, int) label_color: The color of the button label text.
54+
Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0.
5355
:param int|Tuple(int, int, int) selected_label: The color of button label text when the button is selected.
56+
Accepts an int or a tuple of 3 integers representing RGB values. Defaults to an inverse of label_color.
5457
:param int label_scale: The scale factor used for the label. Defaults to 1.
5558
"""
5659

@@ -83,7 +86,7 @@ def __init__(
8386
self._label_scale = label_scale
8487

8588
@property
86-
def label(self) -> Optional[Tuple[str, str]]:
89+
def label(self) -> Optional[str]:
8790
"""The text label of the button"""
8891
return getattr(self._label, "text", None)
8992

adafruit_button/sprite_button.py

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries
2+
# SPDX-FileCopyrightText: 2024 Channing Ramos
23
#
34
# SPDX-License-Identifier: MIT
45

@@ -9,7 +10,7 @@
910
Bitmap 3x3 Spritesheet based UI Button for displayio
1011
1112
12-
* Author(s): Tim Cocks
13+
* Author(s): Tim Cocks, Channing Ramos
1314
1415
Implementation Notes
1516
--------------------
@@ -24,40 +25,51 @@
2425
from adafruit_imageload import load
2526
from adafruit_button.button_base import ButtonBase
2627

28+
try:
29+
from typing import Optional, Union, Tuple, Any, List
30+
from fontio import FontProtocol
31+
except ImportError:
32+
pass
2733

2834
class SpriteButton(ButtonBase):
29-
"""Helper class for creating 3x3 Bitmap Spritesheet UI buttons for ``displayio``.
30-
31-
:param x: The x position of the button.
32-
:param y: The y position of the button.
33-
:param width: The width of the button in tiles.
34-
:param height: The height of the button in tiles.
35-
:param name: A name, or miscellaneous string that is stored on the button.
36-
:param label: The text that appears inside the button. Defaults to not displaying the label.
37-
:param label_font: The button label font.
38-
:param label_color: The color of the button label text. Defaults to 0x0.
39-
:param selected_label: Text that appears when selected
40-
:param string bmp_path: The path of the 3x3 spritesheet Bitmap file
41-
:param string selected_bmp_path: The path of the 3x3 spritesheet Bitmap file to use when pressed
42-
:param int or tuple transparent_index: Index(s) that will be made transparent on the Palette
35+
"""Helper class for creating 3x3 Bitmap Spritesheet UI buttons for ``displayio``. Compatible with any format
36+
supported by ''adafruit_imageload''.
37+
38+
:param int x: The x position of the button.
39+
:param int y: The y position of the button.
40+
:param int width: The width of the button in tiles.
41+
:param int height: The height of the button in tiles.
42+
:param Optional[str] name: A name, or miscellaneous string that is stored on the button.
43+
:param Optional[str] label: The text that appears inside the button.
44+
:param Optional[FontProtocol] label_font: The button label font.
45+
:param Optional[Union[int, Tuple[int, int, int]]] label_color: The color of the button label text. Accepts either
46+
an integer or a tuple of 3 integers representing RGB values. Defaults to 0x0.
47+
:param Optional[Union[int, Tuple[int, int, int]]] selected_label: The color of the button label text when the button
48+
is selected. Accepts either an integer or a tuple of 3 integers representing RGB values. Defaults to the inverse of
49+
label_color.
50+
:param str bmp_path: The path of the 3x3 spritesheet mage file
51+
:param Optional[str] selected_bmp_path: The path of the 3x3 spritesheet image file to use when pressed
52+
:param Optional[Union[int, Tuple]] transparent_index: Palette index(s) that will be set to transparent. PNG files have these index(s)
53+
set automatically. Not compatible with JPG files.
54+
:param Optional[int] label_scale: The scale multiplier of the button label. Defaults to 1.
4355
"""
4456

4557
def __init__(
4658
self,
4759
*,
48-
x,
49-
y,
50-
width,
51-
height,
52-
name=None,
53-
label=None,
54-
label_font=None,
55-
label_color=0x0,
56-
selected_label=None,
57-
bmp_path=None,
58-
selected_bmp_path=None,
59-
transparent_index=None,
60-
label_scale=None
60+
x: int,
61+
y: int,
62+
width: int,
63+
height: int,
64+
name: Optional[str] = None,
65+
label: Optional[str] = None,
66+
label_font: Optional[FontProtocol] = None,
67+
label_color: Optional[Union[int, tuple[int, int, int]]] = 0x0,
68+
selected_label: Optional[Union[int, tuple[int, int, int]]] = None,
69+
bmp_path: str = None,
70+
selected_bmp_path: Optional[str] = None,
71+
transparent_index: Optional[Union[int, tuple]] = None,
72+
label_scale: Optional[int] = 1
6173
):
6274
if bmp_path is None:
6375
raise ValueError("Please supply bmp_path. It cannot be None.")
@@ -104,16 +116,16 @@ def __init__(
104116
self.label = label
105117

106118
@property
107-
def width(self):
108-
"""The width of the button"""
119+
def width(self) -> int:
120+
"""The width of the button. Read-Only"""
109121
return self._width
110122

111123
@property
112-
def height(self):
113-
"""The height of the button"""
124+
def height(self) -> int:
125+
"""The height of the button. Read-Only"""
114126
return self._height
115127

116-
def contains(self, point):
128+
def contains(self, point: list[int]) -> bool:
117129
"""Used to determine if a point is contained within a button. For example,
118130
``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for
119131
determining that a button has been touched.
@@ -122,7 +134,7 @@ def contains(self, point):
122134
self.y <= point[1] <= self.y + self.height
123135
)
124136

125-
def _subclass_selected_behavior(self, value):
137+
def _subclass_selected_behavior(self, value: Optional[Any]) -> None:
126138
if self._selected:
127139
if self._selected_bmp is not None:
128140
self._btn_tilegrid.bitmap = self._selected_bmp

0 commit comments

Comments
 (0)