1
1
# SPDX-FileCopyrightText: 2019 Limor Fried for Adafruit Industries
2
+ # SPDX-FileCopyrightText: 2024 Channing Ramos
2
3
#
3
4
# SPDX-License-Identifier: MIT
4
5
9
10
UI Buttons for displayio
10
11
11
12
12
- * Author(s): Limor Fried
13
+ * Author(s): Limor Fried, Channing Ramos
13
14
14
15
Implementation Notes
15
16
--------------------
27
28
from adafruit_button .button_base import ButtonBase , _check_color
28
29
29
30
try :
30
- from typing import Optional , Union
31
+ from typing import Optional , Union , Tuple
31
32
from fontio import FontProtocol
32
33
from displayio import Group
33
34
except ImportError :
39
40
40
41
class Button (ButtonBase ):
41
42
# 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.
49
54
:param style: The style of the button. Can be RECT, ROUNDRECT, SHADOWRECT, SHADOWROUNDRECT.
50
55
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.
59
75
"""
60
76
61
- def _empty_self_group (self ):
77
+ def _empty_self_group (self ) -> None :
62
78
while len (self ) > 0 :
63
79
self .pop ()
64
80
65
- def _create_body (self ):
81
+ def _create_body (self ) -> None :
66
82
if (self .outline_color is not None ) or (self .fill_color is not None ):
67
83
if self .style == Button .RECT :
68
84
self .body = Rect (
@@ -129,17 +145,17 @@ def __init__(
129
145
width : int ,
130
146
height : int ,
131
147
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 ,
135
151
label : Optional [str ] = None ,
136
152
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
+ ):
143
159
super ().__init__ (
144
160
x = x ,
145
161
y = y ,
@@ -164,9 +180,9 @@ def __init__(
164
180
self ._selected_outline = _check_color (selected_outline )
165
181
166
182
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
168
184
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
170
186
171
187
self ._create_body ()
172
188
if self .body :
@@ -206,45 +222,45 @@ def contains(self, point: tuple[int, int]) -> bool:
206
222
)
207
223
208
224
@property
209
- def fill_color (self ) -> int :
225
+ def fill_color (self ) -> Optional [ int ] :
210
226
"""The fill color of the button body"""
211
227
return self ._fill_color
212
228
213
229
@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 :
215
231
self ._fill_color = _check_color (new_color )
216
232
if not self .selected :
217
233
self .body .fill = self ._fill_color
218
234
219
235
@property
220
- def outline_color (self ) -> int :
236
+ def outline_color (self ) -> Optional [ int ] :
221
237
"""The outline color of the button body"""
222
238
return self ._outline_color
223
239
224
240
@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 :
226
242
self ._outline_color = _check_color (new_color )
227
243
if not self .selected :
228
244
self .body .outline = self ._outline_color
229
245
230
246
@property
231
- def selected_fill (self ) -> int :
247
+ def selected_fill (self ) -> Optional [ int ] :
232
248
"""The fill color of the button body when selected"""
233
249
return self ._selected_fill
234
250
235
251
@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 :
237
253
self ._selected_fill = _check_color (new_color )
238
254
if self .selected :
239
255
self .body .fill = self ._selected_fill
240
256
241
257
@property
242
- def selected_outline (self ) -> int :
258
+ def selected_outline (self ) -> Optional [ int ] :
243
259
"""The outline color of the button body when selected"""
244
260
return self ._selected_outline
245
261
246
262
@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 :
248
264
self ._selected_outline = _check_color (new_color )
249
265
if self .selected :
250
266
self .body .outline = self ._selected_outline
@@ -279,8 +295,8 @@ def height(self, new_height: int) -> None:
279
295
280
296
def resize (self , new_width : int , new_height : int ) -> None :
281
297
"""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.
284
300
"""
285
301
self ._width = new_width
286
302
self ._height = new_height
0 commit comments