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
--------------------
28
29
from adafruit_button .button_base import ButtonBase , _check_color
29
30
30
31
try :
31
- from typing import Optional , Union
32
-
32
+ from typing import Optional , Union , Tuple
33
33
from displayio import Group
34
34
from fontio import FontProtocol
35
35
except ImportError :
40
40
41
41
42
42
class Button (ButtonBase ):
43
- """Helper class for creating UI buttons for ``displayio``.
44
-
45
- :param x: The x position of the button.
46
- :param y: The y position of the button.
47
- :param width: The width of the button in pixels.
48
- :param height: The height of the button in pixels.
49
- :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.
50
54
:param style: The style of the button. Can be RECT, ROUNDRECT, SHADOWRECT, SHADOWROUNDRECT.
51
55
Defaults to RECT.
52
- :param fill_color: The color to fill the button. Defaults to 0xFFFFFF.
53
- :param outline_color: The color of the outline of the button.
54
- :param label: The text that appears inside the button. Defaults to not displaying the label.
55
- :param label_font: The button label font.
56
- :param label_color: The color of the button label text. Defaults to 0x0.
57
- :param selected_fill: Inverts the fill color.
58
- :param selected_outline: Inverts the outline color.
59
- :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.
60
75
"""
61
76
62
- def _empty_self_group (self ):
77
+ def _empty_self_group (self ) -> None :
63
78
while len (self ) > 0 :
64
79
self .pop ()
65
80
66
- def _create_body (self ):
81
+ def _create_body (self ) -> None :
67
82
if (self .outline_color is not None ) or (self .fill_color is not None ):
68
83
if self .style == Button .RECT :
69
84
self .body = Rect (
@@ -128,17 +143,17 @@ def __init__( # noqa: PLR0913 Too many arguments
128
143
width : int ,
129
144
height : int ,
130
145
name : Optional [str ] = None ,
131
- style : int = RECT ,
132
- fill_color : Optional [Union [int , tuple [int , int , int ]]] = 0xFFFFFF ,
133
- outline_color : Optional [Union [int , tuple [int , int , int ]]] = 0x0 ,
146
+ style = RECT ,
147
+ fill_color : Optional [Union [int , Tuple [int , int , int ]]] = 0xFFFFFF ,
148
+ outline_color : Optional [Union [int , Tuple [int , int , int ]]] = 0x0 ,
134
149
label : Optional [str ] = None ,
135
150
label_font : Optional [FontProtocol ] = None ,
136
- label_color : Optional [Union [int , tuple [int , int , int ]]] = 0x0 ,
137
- selected_fill : Optional [Union [int , tuple [int , int , int ]]] = None ,
138
- selected_outline : Optional [Union [int , tuple [int , int , int ]]] = None ,
139
- selected_label : Optional [Union [int , tuple [int , int , int ]]] = None ,
140
- label_scale : Optional [int ] = None ,
141
- ) -> None :
151
+ label_color : Optional [Union [int , Tuple [int , int , int ]]] = 0x0 ,
152
+ selected_fill : Optional [Union [int , Tuple [int , int , int ]]] = None ,
153
+ selected_outline : Optional [Union [int , Tuple [int , int , int ]]] = None ,
154
+ selected_label : Optional [Union [int , Tuple [int , int , int ]]] = None ,
155
+ label_scale : Optional [int ] = 1 ,
156
+ ):
142
157
super ().__init__ (
143
158
x = x ,
144
159
y = y ,
@@ -163,9 +178,9 @@ def __init__( # noqa: PLR0913 Too many arguments
163
178
self ._selected_outline = _check_color (selected_outline )
164
179
165
180
if self .selected_fill is None and fill_color is not None :
166
- self .selected_fill = (~ self ._fill_color ) & 0xFFFFFF
181
+ self .selected_fill = (~ _check_color ( self ._fill_color ) ) & 0xFFFFFF
167
182
if self .selected_outline is None and outline_color is not None :
168
- self .selected_outline = (~ self ._outline_color ) & 0xFFFFFF
183
+ self .selected_outline = (~ _check_color ( self ._outline_color ) ) & 0xFFFFFF
169
184
170
185
self ._create_body ()
171
186
if self .body :
@@ -205,45 +220,45 @@ def contains(self, point: tuple[int, int]) -> bool:
205
220
)
206
221
207
222
@property
208
- def fill_color (self ) -> int :
223
+ def fill_color (self ) -> Optional [ int ] :
209
224
"""The fill color of the button body"""
210
225
return self ._fill_color
211
226
212
227
@fill_color .setter
213
- def fill_color (self , new_color : int ) -> None :
228
+ def fill_color (self , new_color : Union [ int , Tuple [ int , int , int ]] ) -> None :
214
229
self ._fill_color = _check_color (new_color )
215
230
if not self .selected :
216
231
self .body .fill = self ._fill_color
217
232
218
233
@property
219
- def outline_color (self ) -> int :
234
+ def outline_color (self ) -> Optional [ int ] :
220
235
"""The outline color of the button body"""
221
236
return self ._outline_color
222
237
223
238
@outline_color .setter
224
- def outline_color (self , new_color : int ) -> None :
239
+ def outline_color (self , new_color : Union [ int , Tuple [ int , int , int ]] ) -> None :
225
240
self ._outline_color = _check_color (new_color )
226
241
if not self .selected :
227
242
self .body .outline = self ._outline_color
228
243
229
244
@property
230
- def selected_fill (self ) -> int :
245
+ def selected_fill (self ) -> Optional [ int ] :
231
246
"""The fill color of the button body when selected"""
232
247
return self ._selected_fill
233
248
234
249
@selected_fill .setter
235
- def selected_fill (self , new_color : int ) -> None :
250
+ def selected_fill (self , new_color : Union [ int , Tuple [ int , int , int ]] ) -> None :
236
251
self ._selected_fill = _check_color (new_color )
237
252
if self .selected :
238
253
self .body .fill = self ._selected_fill
239
254
240
255
@property
241
- def selected_outline (self ) -> int :
256
+ def selected_outline (self ) -> Optional [ int ] :
242
257
"""The outline color of the button body when selected"""
243
258
return self ._selected_outline
244
259
245
260
@selected_outline .setter
246
- def selected_outline (self , new_color : int ) -> None :
261
+ def selected_outline (self , new_color : Union [ int , Tuple [ int , int , int ]] ) -> None :
247
262
self ._selected_outline = _check_color (new_color )
248
263
if self .selected :
249
264
self .body .outline = self ._selected_outline
@@ -278,8 +293,8 @@ def height(self, new_height: int) -> None:
278
293
279
294
def resize (self , new_width : int , new_height : int ) -> None :
280
295
"""Resize the button to the new width and height given
281
- :param new_width int the desired width
282
- :param new_height int the desired height
296
+ :param int new_width: The desired width in pixels.
297
+ :param int new_height: he desired height in pixels.
283
298
"""
284
299
self ._width = new_width
285
300
self ._height = new_height
0 commit comments