11# SPDX-FileCopyrightText: 2019 Limor Fried for Adafruit Industries
2+ # SPDX-FileCopyrightText: 2024 Channing Ramos
23#
34# SPDX-License-Identifier: MIT
45
910UI Buttons for displayio
1011
1112
12- * Author(s): Limor Fried
13+ * Author(s): Limor Fried, Channing Ramos
1314
1415Implementation Notes
1516--------------------
2728from adafruit_button .button_base import ButtonBase , _check_color
2829
2930try :
30- from typing import Optional , Union
31+ from typing import Optional , Union , Tuple
3132 from fontio import FontProtocol
3233 from displayio import Group
3334except ImportError :
3940
4041class Button (ButtonBase ):
4142 # pylint: disable=too-many-instance-attributes, too-many-locals
42- """Helper class for creating UI buttons for ``displayio``.
43-
44- :param x: The x position of the button.
45- :param y: The y position of the button.
46- :param width: The width of the button in pixels.
47- :param height: The height of the button in pixels.
48- :param name: The name of the button.
43+ """Helper class for creating UI buttons for ``displayio``. Provides the following
44+ buttons:
45+ RECT: A rectangular button. SHAWDOWRECT adds a drop shadow.
46+ ROUNDRECT: A rectangular button with rounded corners. SHADOWROUNDRECT adds
47+ a drop shadow.
48+
49+ :param int x: The x position of the button.
50+ :param int y: The y position of the button.
51+ :param int width: The width of the button in pixels.
52+ :param int height: The height of the button in pixels.
53+ :param Optional[str] name: The name of the button.
4954 :param style: The style of the button. Can be RECT, ROUNDRECT, SHADOWRECT, SHADOWROUNDRECT.
5055 Defaults to RECT.
51- :param fill_color: The color to fill the button. Defaults to 0xFFFFFF.
52- :param outline_color: The color of the outline of the button.
53- :param label: The text that appears inside the button. Defaults to not displaying the label.
54- :param label_font: The button label font.
55- :param label_color: The color of the button label text. Defaults to 0x0.
56- :param selected_fill: Inverts the fill color.
57- :param selected_outline: Inverts the outline color.
58- :param selected_label: Inverts the label color.
56+ :param Optional[Union[int, Tuple[int, int, int]]] fill_color: The color to fill the button.
57+ Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0xFFFFFF.
58+ :param Optional[Union[int, Tuple[int, int, int]]] outline_color: The color of the outline of
59+ the button. Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0.
60+ :param Optional[str] label: The text that appears inside the button.
61+ :param Optional[FontProtocol] label_font: The button label font. Defaults to
62+ ''terminalio.FONT''
63+ :param Optional[Union[int, Tuple[int, int, int]]] label_color: The color of the button label
64+ text. Accepts an int or a tuple of 3 integers representing RGB values. Defaults to 0x0.
65+ :param Optional[Union[int, Tuple[int, int, int]]] selected_fill: The fill color when the
66+ button is selected. Accepts an int or a tuple of 3 integers representing RGB values.
67+ Defaults to the inverse of the fill_color.
68+ :param Optional[Union[int, Tuple[int, int, int]]] selected_outline: The outline color when the
69+ button is selected. Accepts an int or a tuple of 3 integers representing RGB values.
70+ Defaults to the inverse of outline_color.
71+ :param Optional[Union[int, Tuple[int, int, int]]] selected_label: The label color when the
72+ button is selected. Accepts an int or a tuple of 3 integers representing RGB values.
73+ Defaults to inverting the label_color.
74+ :param Optional[int] label_scale: The scale factor used for the label. Defaults to 1.
5975 """
6076
61- def _empty_self_group (self ):
77+ def _empty_self_group (self ) -> None :
6278 while len (self ) > 0 :
6379 self .pop ()
6480
65- def _create_body (self ):
81+ def _create_body (self ) -> None :
6682 if (self .outline_color is not None ) or (self .fill_color is not None ):
6783 if self .style == Button .RECT :
6884 self .body = Rect (
@@ -129,17 +145,17 @@ def __init__(
129145 width : int ,
130146 height : int ,
131147 name : Optional [str ] = None ,
132- style : int = RECT ,
133- fill_color : Optional [Union [int , tuple [int , int , int ]]] = 0xFFFFFF ,
134- outline_color : Optional [Union [int , tuple [int , int , int ]]] = 0x0 ,
148+ style = RECT ,
149+ fill_color : Optional [Union [int , Tuple [int , int , int ]]] = 0xFFFFFF ,
150+ outline_color : Optional [Union [int , Tuple [int , int , int ]]] = 0x0 ,
135151 label : Optional [str ] = None ,
136152 label_font : Optional [FontProtocol ] = None ,
137- label_color : Optional [Union [int , tuple [int , int , int ]]] = 0x0 ,
138- selected_fill : Optional [Union [int , tuple [int , int , int ]]] = None ,
139- selected_outline : Optional [Union [int , tuple [int , int , int ]]] = None ,
140- selected_label : Optional [Union [int , tuple [int , int , int ]]] = None ,
141- label_scale : Optional [int ] = None
142- ) -> None :
153+ label_color : Optional [Union [int , Tuple [int , int , int ]]] = 0x0 ,
154+ selected_fill : Optional [Union [int , Tuple [int , int , int ]]] = None ,
155+ selected_outline : Optional [Union [int , Tuple [int , int , int ]]] = None ,
156+ selected_label : Optional [Union [int , Tuple [int , int , int ]]] = None ,
157+ label_scale : Optional [int ] = 1
158+ ):
143159 super ().__init__ (
144160 x = x ,
145161 y = y ,
@@ -164,9 +180,9 @@ def __init__(
164180 self ._selected_outline = _check_color (selected_outline )
165181
166182 if self .selected_fill is None and fill_color is not None :
167- self .selected_fill = (~ self ._fill_color ) & 0xFFFFFF
183+ self .selected_fill = (~ _check_color ( self ._fill_color ) ) & 0xFFFFFF
168184 if self .selected_outline is None and outline_color is not None :
169- self .selected_outline = (~ self ._outline_color ) & 0xFFFFFF
185+ self .selected_outline = (~ _check_color ( self ._outline_color ) ) & 0xFFFFFF
170186
171187 self ._create_body ()
172188 if self .body :
@@ -206,45 +222,45 @@ def contains(self, point: tuple[int, int]) -> bool:
206222 )
207223
208224 @property
209- def fill_color (self ) -> int :
225+ def fill_color (self ) -> Optional [ int ] :
210226 """The fill color of the button body"""
211227 return self ._fill_color
212228
213229 @fill_color .setter
214- def fill_color (self , new_color : int ) -> None :
230+ def fill_color (self , new_color : Union [ int , Tuple [ int , int , int ]] ) -> None :
215231 self ._fill_color = _check_color (new_color )
216232 if not self .selected :
217233 self .body .fill = self ._fill_color
218234
219235 @property
220- def outline_color (self ) -> int :
236+ def outline_color (self ) -> Optional [ int ] :
221237 """The outline color of the button body"""
222238 return self ._outline_color
223239
224240 @outline_color .setter
225- def outline_color (self , new_color : int ) -> None :
241+ def outline_color (self , new_color : Union [ int , Tuple [ int , int , int ]] ) -> None :
226242 self ._outline_color = _check_color (new_color )
227243 if not self .selected :
228244 self .body .outline = self ._outline_color
229245
230246 @property
231- def selected_fill (self ) -> int :
247+ def selected_fill (self ) -> Optional [ int ] :
232248 """The fill color of the button body when selected"""
233249 return self ._selected_fill
234250
235251 @selected_fill .setter
236- def selected_fill (self , new_color : int ) -> None :
252+ def selected_fill (self , new_color : Union [ int , Tuple [ int , int , int ]] ) -> None :
237253 self ._selected_fill = _check_color (new_color )
238254 if self .selected :
239255 self .body .fill = self ._selected_fill
240256
241257 @property
242- def selected_outline (self ) -> int :
258+ def selected_outline (self ) -> Optional [ int ] :
243259 """The outline color of the button body when selected"""
244260 return self ._selected_outline
245261
246262 @selected_outline .setter
247- def selected_outline (self , new_color : int ) -> None :
263+ def selected_outline (self , new_color : Union [ int , Tuple [ int , int , int ]] ) -> None :
248264 self ._selected_outline = _check_color (new_color )
249265 if self .selected :
250266 self .body .outline = self ._selected_outline
@@ -279,8 +295,8 @@ def height(self, new_height: int) -> None:
279295
280296 def resize (self , new_width : int , new_height : int ) -> None :
281297 """Resize the button to the new width and height given
282- :param new_width int the desired width
283- :param new_height int the desired height
298+ :param int new_width: The desired width in pixels.
299+ :param int new_height: he desired height in pixels.
284300 """
285301 self ._width = new_width
286302 self ._height = new_height
0 commit comments