20
20
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
21
# THE SOFTWARE.
22
22
"""
23
- `adafruit_display_button `
23
+ `adafruit_button `
24
24
================================================================================
25
25
26
26
UI Buttons for displayio
38
38
39
39
"""
40
40
41
+ from micropython import const
41
42
import displayio
42
- from adafruit_display_text .text_area import TextArea
43
- from adafruit_bitmap_font import bitmap_font
43
+ from adafruit_display_text .label import Label
44
44
from adafruit_display_shapes .rect import Rect
45
45
from adafruit_display_shapes .roundrect import RoundRect
46
46
47
47
__version__ = "0.0.0-auto.0"
48
48
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Button.git"
49
49
50
50
51
+ def _check_color (color ):
52
+ # if a tuple is supplied, convert it to a RGB number
53
+ if isinstance (color , tuple ):
54
+ r , g , b = color
55
+ return int ((r << 16 ) + (g << 8 ) + (b & 0xff ))
56
+ return color
57
+
58
+
51
59
class Button ():
60
+ # pylint: disable=too-many-instance-attributes, too-many-locals
61
+ """Helper class for creating UI buttons for ``displayio``.
62
+
63
+ :param x: The x position of the button.
64
+ :param y: The y position of the button.
65
+ :param width: The width of the button in pixels.
66
+ :param height: The height of the button in pixels.
67
+ :param name: The name of the button.
68
+ :param style: The style of the button. Can be RECT, ROUNDRECT, SHADOWRECT, SHADOWROUNDRECT.
69
+ Defaults to RECT.
70
+ :param fill_color: The color to fill the button. Defaults to 0xFFFFFF.
71
+ :param outline_color: The color of the outline of the button.
72
+ :param label: The text that appears inside the button. Defaults to not displaying the label.
73
+ :param label_font: The button label font.
74
+ :param label_color: The color of the button label text. Defaults to 0x0.
75
+ :param selected_fill: Inverts the fill color.
76
+ :param selected_outline: Inverts the outline color.
77
+ :param selected_label: Inverts the label color.
78
+
79
+ """
52
80
RECT = const (0 )
53
81
ROUNDRECT = const (1 )
54
82
SHADOWRECT = const (2 )
55
83
SHADOWROUNDRECT = const (3 )
84
+
56
85
def __init__ (self , * , x , y , width , height , name = None , style = RECT ,
57
86
fill_color = 0xFFFFFF , outline_color = 0x0 ,
58
87
label = None , label_font = None , label_color = 0x0 ,
@@ -66,66 +95,66 @@ def __init__(self, *, x, y, width, height, name=None, style=RECT,
66
95
self ._selected = False
67
96
self .group = displayio .Group ()
68
97
self .name = name
98
+ self .label = label
69
99
70
- self .fill_color = fill_color
71
- self .outline_color = outline_color
100
+ self .fill_color = _check_color ( fill_color )
101
+ self .outline_color = _check_color ( outline_color )
72
102
self .label_color = label_color
73
103
# Selecting inverts the button colors!
74
- self .selected_fill = selected_fill
75
- self .selected_outline = selected_outline
76
- self .selected_label = selected_label
104
+ self .selected_fill = _check_color ( selected_fill )
105
+ self .selected_outline = _check_color ( selected_outline )
106
+ self .selected_label = _check_color ( selected_label )
77
107
78
108
if self .selected_fill is None and fill_color is not None :
79
- self .selected_fill = (~ fill_color ) & 0xFFFFFF
109
+ self .selected_fill = (~ self . fill_color ) & 0xFFFFFF
80
110
if self .selected_outline is None and outline_color is not None :
81
- self .selected_outline = (~ outline_color ) & 0xFFFFFF
111
+ self .selected_outline = (~ self . outline_color ) & 0xFFFFFF
82
112
83
113
if outline_color or fill_color :
84
114
self .body = self .shadow = None
85
- if style == RECT :
115
+ if style == Button . RECT :
86
116
self .body = Rect (x , y , width , height ,
87
- fill = fill_color , outline = outline_color )
88
- elif style == ROUNDRECT :
117
+ fill = self . fill_color , outline = self . outline_color )
118
+ elif style == Button . ROUNDRECT :
89
119
self .body = RoundRect (x , y , width , height , r = 10 ,
90
- fill = fill_color , outline = outline_color )
91
- elif style == SHADOWRECT :
92
- self .shadow = Rect (x + 2 , y + 2 , width - 2 , height - 2 ,
93
- fill = outline_color )
94
- self .body = Rect (x , y , width - 2 , height - 2 ,
95
- fill = fill_color , outline = outline_color )
96
- elif style == SHADOWROUNDRECT :
97
- self .shadow = RoundRect (x + 2 , y + 2 , width - 2 , height - 2 , r = 10 ,
120
+ fill = self .fill_color , outline = self .outline_color )
121
+ elif style == Button .SHADOWRECT :
122
+ self .shadow = Rect (x + 2 , y + 2 , width - 2 , height - 2 ,
98
123
fill = outline_color )
99
- self .body = RoundRect (x , y , width - 2 , height - 2 , r = 10 ,
100
- fill = fill_color , outline = outline_color )
124
+ self .body = Rect (x , y , width - 2 , height - 2 ,
125
+ fill = self .fill_color , outline = self .outline_color )
126
+ elif style == Button .SHADOWROUNDRECT :
127
+ self .shadow = RoundRect (x + 2 , y + 2 , width - 2 , height - 2 , r = 10 ,
128
+ fill = self .outline_color )
129
+ self .body = RoundRect (x , y , width - 2 , height - 2 , r = 10 ,
130
+ fill = self .fill_color , outline = self .outline_color )
101
131
if self .shadow :
102
132
self .group .append (self .shadow )
103
133
self .group .append (self .body )
104
134
105
- if label and (label_color is not None ): # button with text label
135
+ if label and (label_color is not None ): # button with text label
106
136
if not label_font :
107
137
raise RuntimeError ("Please provide label font" )
108
138
dims = label_font .text_bounding_box (label )
109
139
if dims [2 ] >= width or dims [3 ] >= height :
110
140
raise RuntimeError ("Button not large enough for label" )
111
- self .label = TextArea (label_font , text = label )
112
- self .label .x = x + (width - dims [2 ])// 2
141
+ self .label = Label (label_font , text = label )
142
+ self .label .x = x + (width - dims [2 ]) // 2
113
143
self .label .y = y + (height - dims [3 ])
114
144
self .label .color = label_color
115
145
self .group .append (self .label )
116
146
117
147
if self .selected_label is None and label_color is not None :
118
148
self .selected_label = (~ label_color ) & 0xFFFFFF
119
- #print(dims)
149
+ # print(dims)
120
150
121
- """
122
- #else: # ok just a bounding box
123
- #self.bodyshape = displayio.Shape(width, height)
124
- #self.group.append(self.bodyshape)
125
- """
151
+ # else: # ok just a bounding box
152
+ # self.bodyshape = displayio.Shape(width, height)
153
+ # self.group.append(self.bodyshape)
126
154
127
155
@property
128
156
def selected (self ):
157
+ """Selected inverts the colors."""
129
158
return self ._selected
130
159
131
160
@selected .setter
@@ -142,4 +171,9 @@ def selected(self, value):
142
171
self .label .color = self .label_color
143
172
144
173
def contains (self , point ):
145
- return (self .x <= point [0 ] <= self .x + self .width ) and (self .y <= point [1 ] <= self .y + self .height )
174
+ """Used to determine if a point is contained within a button. For example,
175
+ ``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for
176
+ determining that a button has been touched.
177
+ """
178
+ return (self .x <= point [0 ] <= self .x + self .width ) and (self .y <= point [1 ] <=
179
+ self .y + self .height )
0 commit comments