11# SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries
2+ # SPDX-FileCopyrightText: 2024 Channing Ramos
23#
34# SPDX-License-Identifier: MIT
45
910Bitmap 3x3 Spritesheet based UI Button for displayio
1011
1112
12- * Author(s): Tim Cocks
13+ * Author(s): Tim Cocks, Channing Ramos
1314
1415Implementation Notes
1516--------------------
2425from adafruit_imageload import load
2526from 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
2834class 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