@@ -58,8 +58,66 @@ class Button(displayio.Group):
58
58
:param selected_fill: Inverts the fill color.
59
59
:param selected_outline: Inverts the outline color.
60
60
:param selected_label: Inverts the label color.
61
-
62
61
"""
62
+
63
+ def _empty_self_group (self ):
64
+ while len (self ) > 0 :
65
+ self .pop ()
66
+
67
+ def _create_body (self ):
68
+ if (self .outline_color is not None ) or (self .fill_color is not None ):
69
+ if self .style == Button .RECT :
70
+ self .body = Rect (
71
+ 0 ,
72
+ 0 ,
73
+ self .width ,
74
+ self .height ,
75
+ fill = self ._fill_color ,
76
+ outline = self ._outline_color ,
77
+ )
78
+ elif self .style == Button .ROUNDRECT :
79
+ self .body = RoundRect (
80
+ 0 ,
81
+ 0 ,
82
+ self .width ,
83
+ self .height ,
84
+ r = 10 ,
85
+ fill = self ._fill_color ,
86
+ outline = self ._outline_color ,
87
+ )
88
+ elif self .style == Button .SHADOWRECT :
89
+ self .shadow = Rect (
90
+ 2 , 2 , self .width - 2 , self .height - 2 , fill = self .outline_color
91
+ )
92
+ self .body = Rect (
93
+ 0 ,
94
+ 0 ,
95
+ self .width - 2 ,
96
+ self .height - 2 ,
97
+ fill = self ._fill_color ,
98
+ outline = self ._outline_color ,
99
+ )
100
+ elif self .style == Button .SHADOWROUNDRECT :
101
+ self .shadow = RoundRect (
102
+ 2 ,
103
+ 2 ,
104
+ self .width - 2 ,
105
+ self .height - 2 ,
106
+ r = 10 ,
107
+ fill = self ._outline_color ,
108
+ )
109
+ self .body = RoundRect (
110
+ 0 ,
111
+ 0 ,
112
+ self .width - 2 ,
113
+ self .height - 2 ,
114
+ r = 10 ,
115
+ fill = self ._fill_color ,
116
+ outline = self ._outline_color ,
117
+ )
118
+ if self .shadow :
119
+ self .append (self .shadow )
120
+
63
121
RECT = const (0 )
64
122
ROUNDRECT = const (1 )
65
123
SHADOWRECT = const (2 )
@@ -86,13 +144,14 @@ def __init__(
86
144
super ().__init__ (x = x , y = y )
87
145
self .x = x
88
146
self .y = y
89
- self .width = width
90
- self .height = height
147
+ self ._width = width
148
+ self ._height = height
91
149
self ._font = label_font
92
150
self ._selected = False
93
151
self .name = name
94
152
self ._label = label
95
153
self .body = self .fill = self .shadow = None
154
+ self .style = style
96
155
97
156
self ._fill_color = _check_color (fill_color )
98
157
self ._outline_color = _check_color (outline_color )
@@ -108,51 +167,8 @@ def __init__(
108
167
if self .selected_outline is None and outline_color is not None :
109
168
self .selected_outline = (~ self ._outline_color ) & 0xFFFFFF
110
169
111
- if (outline_color is not None ) or (fill_color is not None ):
112
- if style == Button .RECT :
113
- self .body = Rect (
114
- 0 ,
115
- 0 ,
116
- width ,
117
- height ,
118
- fill = self ._fill_color ,
119
- outline = self ._outline_color ,
120
- )
121
- elif style == Button .ROUNDRECT :
122
- self .body = RoundRect (
123
- 0 ,
124
- 0 ,
125
- width ,
126
- height ,
127
- r = 10 ,
128
- fill = self ._fill_color ,
129
- outline = self ._outline_color ,
130
- )
131
- elif style == Button .SHADOWRECT :
132
- self .shadow = Rect (2 , 2 , width - 2 , height - 2 , fill = outline_color )
133
- self .body = Rect (
134
- 0 ,
135
- 0 ,
136
- width - 2 ,
137
- height - 2 ,
138
- fill = self ._fill_color ,
139
- outline = self ._outline_color ,
140
- )
141
- elif style == Button .SHADOWROUNDRECT :
142
- self .shadow = RoundRect (
143
- 2 , 2 , width - 2 , height - 2 , r = 10 , fill = self ._outline_color
144
- )
145
- self .body = RoundRect (
146
- 0 ,
147
- 0 ,
148
- width - 2 ,
149
- height - 2 ,
150
- r = 10 ,
151
- fill = self ._fill_color ,
152
- outline = self ._outline_color ,
153
- )
154
- if self .shadow :
155
- self .append (self .shadow )
170
+ self ._create_body ()
171
+ if self .body :
156
172
self .append (self .body )
157
173
158
174
self .label = label
@@ -176,7 +192,13 @@ def label(self, newtext):
176
192
self ._label = Label (self ._label_font , text = newtext )
177
193
dims = self ._label .bounding_box
178
194
if dims [2 ] >= self .width or dims [3 ] >= self .height :
179
- raise RuntimeError ("Button not large enough for label" )
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" )
180
202
self ._label .x = (self .width - dims [2 ]) // 2
181
203
self ._label .y = self .height // 2
182
204
self ._label .color = self ._label_color
@@ -291,3 +313,44 @@ def label_color(self):
291
313
def label_color (self , new_color ):
292
314
self ._label_color = _check_color (new_color )
293
315
self ._label .color = self ._label_color
316
+
317
+ @property
318
+ def width (self ):
319
+ """The width of the button"""
320
+ return self ._width
321
+
322
+ @width .setter
323
+ def width (self , new_width ):
324
+ self ._width = new_width
325
+ self ._empty_self_group ()
326
+ self ._create_body ()
327
+ if self .body :
328
+ self .append (self .body )
329
+ self .label = self .label
330
+
331
+ @property
332
+ def height (self ):
333
+ """The height of the button"""
334
+ return self ._height
335
+
336
+ @height .setter
337
+ def height (self , new_height ):
338
+ self ._height = new_height
339
+ self ._empty_self_group ()
340
+ self ._create_body ()
341
+ if self .body :
342
+ self .append (self .body )
343
+ self .label = self .label
344
+
345
+ def resize (self , new_width , new_height ):
346
+ """Resize the button to the new width and height given
347
+ :param new_width int the desired width
348
+ :param new_height int the desired height
349
+ """
350
+ self ._width = new_width
351
+ self ._height = new_height
352
+ self ._empty_self_group ()
353
+ self ._create_body ()
354
+ if self .body :
355
+ self .append (self .body )
356
+ self .label = self .label
0 commit comments