1
1
# SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries
2
+ # SPDX-FileCopyrightText: 2024 Channing Ramos
2
3
#
3
4
# SPDX-License-Identifier: MIT
4
5
9
10
Bitmap 3x3 Spritesheet based UI Button for displayio
10
11
11
12
12
- * Author(s): Tim Cocks
13
+ * Author(s): Tim Cocks, Channing Ramos
13
14
14
15
Implementation Notes
15
16
--------------------
24
25
from adafruit_imageload import load
25
26
from adafruit_button .button_base import ButtonBase
26
27
28
+ try :
29
+ from typing import Optional , Union , Tuple , Any , List
30
+ from fontio import FontProtocol
31
+ except ImportError :
32
+ pass
27
33
28
34
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.
43
55
"""
44
56
45
57
def __init__ (
46
58
self ,
47
59
* ,
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
61
73
):
62
74
if bmp_path is None :
63
75
raise ValueError ("Please supply bmp_path. It cannot be None." )
@@ -104,16 +116,16 @@ def __init__(
104
116
self .label = label
105
117
106
118
@property
107
- def width (self ):
108
- """The width of the button"""
119
+ def width (self ) -> int :
120
+ """The width of the button. Read-Only """
109
121
return self ._width
110
122
111
123
@property
112
- def height (self ):
113
- """The height of the button"""
124
+ def height (self ) -> int :
125
+ """The height of the button. Read-Only """
114
126
return self ._height
115
127
116
- def contains (self , point ) :
128
+ def contains (self , point : list [ int ]) -> bool :
117
129
"""Used to determine if a point is contained within a button. For example,
118
130
``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for
119
131
determining that a button has been touched.
@@ -122,7 +134,7 @@ def contains(self, point):
122
134
self .y <= point [1 ] <= self .y + self .height
123
135
)
124
136
125
- def _subclass_selected_behavior (self , value ) :
137
+ def _subclass_selected_behavior (self , value : Optional [ Any ]) -> None :
126
138
if self ._selected :
127
139
if self ._selected_bmp is not None :
128
140
self ._btn_tilegrid .bitmap = self ._selected_bmp
0 commit comments