3
3
# SPDX-License-Identifier: MIT
4
4
5
5
"""
6
- `adafruit_button`
6
+ `adafruit_button.button `
7
7
================================================================================
8
8
9
9
UI Buttons for displayio
22
22
"""
23
23
24
24
from micropython import const
25
- import displayio
26
- from adafruit_display_text .label import Label
27
25
from adafruit_display_shapes .rect import Rect
28
26
from adafruit_display_shapes .roundrect import RoundRect
27
+ from adafruit_button .button_base import ButtonBase , _check_color
29
28
30
29
__version__ = "0.0.0+auto.0"
31
30
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Button.git"
32
31
33
32
34
- def _check_color (color ):
35
- # if a tuple is supplied, convert it to a RGB number
36
- if isinstance (color , tuple ):
37
- r , g , b = color
38
- return int ((r << 16 ) + (g << 8 ) + (b & 0xFF ))
39
- return color
40
-
41
-
42
- class Button (displayio .Group ):
33
+ class Button (ButtonBase ):
43
34
# pylint: disable=too-many-instance-attributes, too-many-locals
44
35
"""Helper class for creating UI buttons for ``displayio``.
45
36
@@ -141,26 +132,27 @@ def __init__(
141
132
selected_outline = None ,
142
133
selected_label = None
143
134
):
144
- super ().__init__ (x = x , y = y )
145
- self .x = x
146
- self .y = y
147
- self ._width = width
148
- self ._height = height
149
- self ._font = label_font
150
- self ._selected = False
151
- self .name = name
152
- self ._label = label
135
+ super ().__init__ (
136
+ x = x ,
137
+ y = y ,
138
+ width = width ,
139
+ height = height ,
140
+ name = name ,
141
+ label = label ,
142
+ label_font = label_font ,
143
+ label_color = label_color ,
144
+ selected_label = selected_label ,
145
+ )
146
+
153
147
self .body = self .fill = self .shadow = None
154
148
self .style = style
155
149
156
150
self ._fill_color = _check_color (fill_color )
157
151
self ._outline_color = _check_color (outline_color )
158
- self ._label_color = label_color
159
- self ._label_font = label_font
152
+
160
153
# Selecting inverts the button colors!
161
154
self ._selected_fill = _check_color (selected_fill )
162
155
self ._selected_outline = _check_color (selected_outline )
163
- self ._selected_label = _check_color (selected_label )
164
156
165
157
if self .selected_fill is None and fill_color is not None :
166
158
self .selected_fill = (~ self ._fill_color ) & 0xFFFFFF
@@ -173,64 +165,17 @@ def __init__(
173
165
174
166
self .label = label
175
167
176
- @property
177
- def label (self ):
178
- """The text label of the button"""
179
- return self ._label .text
180
-
181
- @label .setter
182
- def label (self , newtext ):
183
- if self ._label and self and (self [- 1 ] == self ._label ):
184
- self .pop ()
185
-
186
- self ._label = None
187
- if not newtext or (self ._label_color is None ): # no new text
188
- return # nothing to do!
189
-
190
- if not self ._label_font :
191
- raise RuntimeError ("Please provide label font" )
192
- self ._label = Label (self ._label_font , text = newtext )
193
- dims = self ._label .bounding_box
194
- if dims [2 ] >= self .width or dims [3 ] >= self .height :
195
- while len (self ._label .text ) > 1 and (
196
- dims [2 ] >= self .width or dims [3 ] >= self .height
197
- ):
198
- self ._label .text = "{}." .format (self ._label .text [:- 2 ])
199
- dims = self ._label .bounding_box
200
- if len (self ._label .text ) <= 1 :
201
- raise RuntimeError ("Button not large enough for label" )
202
- self ._label .x = (self .width - dims [2 ]) // 2
203
- self ._label .y = self .height // 2
204
- self ._label .color = self ._label_color
205
- self .append (self ._label )
206
-
207
- if (self .selected_label is None ) and (self ._label_color is not None ):
208
- self .selected_label = (~ self ._label_color ) & 0xFFFFFF
209
-
210
- @property
211
- def selected (self ):
212
- """Selected inverts the colors."""
213
- return self ._selected
214
-
215
- @selected .setter
216
- def selected (self , value ):
217
- if value == self ._selected :
218
- return # bail now, nothing more to do
219
- self ._selected = value
168
+ def _subclass_selected_behavior (self , value ):
220
169
if self ._selected :
221
170
new_fill = self .selected_fill
222
171
new_out = self .selected_outline
223
- new_label = self .selected_label
224
172
else :
225
173
new_fill = self ._fill_color
226
174
new_out = self ._outline_color
227
- new_label = self ._label_color
228
175
# update all relevant colors!
229
176
if self .body is not None :
230
177
self .body .fill = new_fill
231
178
self .body .outline = new_out
232
- if self ._label is not None :
233
- self ._label .color = new_label
234
179
235
180
@property
236
181
def group (self ):
@@ -295,25 +240,6 @@ def selected_outline(self, new_color):
295
240
if self .selected :
296
241
self .body .outline = self ._selected_outline
297
242
298
- @property
299
- def selected_label (self ):
300
- """The font color of the button when selected"""
301
- return self ._selected_label
302
-
303
- @selected_label .setter
304
- def selected_label (self , new_color ):
305
- self ._selected_label = _check_color (new_color )
306
-
307
- @property
308
- def label_color (self ):
309
- """The font color of the button"""
310
- return self ._label_color
311
-
312
- @label_color .setter
313
- def label_color (self , new_color ):
314
- self ._label_color = _check_color (new_color )
315
- self ._label .color = self ._label_color
316
-
317
243
@property
318
244
def width (self ):
319
245
"""The width of the button"""
0 commit comments