Skip to content

Commit e6e54e3

Browse files
authored
Merge pull request #23 from FoamyGuy/use_extended_group
use extended Group instead of property
2 parents 4f49de1 + 8057dcf commit e6e54e3

4 files changed

+31
-24
lines changed

adafruit_button.py

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,13 @@ def __init__(
100100
selected_outline=None,
101101
selected_label=None
102102
):
103-
super().__init__()
103+
super().__init__(x=x, y=y)
104104
self.x = x
105105
self.y = y
106106
self.width = width
107107
self.height = height
108108
self._font = label_font
109109
self._selected = False
110-
self.group = displayio.Group()
111110
self.name = name
112111
self._label = label
113112
self.body = self.fill = self.shadow = None
@@ -129,51 +128,49 @@ def __init__(
129128
if (outline_color is not None) or (fill_color is not None):
130129
if style == Button.RECT:
131130
self.body = Rect(
132-
x,
133-
y,
131+
0,
132+
0,
134133
width,
135134
height,
136135
fill=self.fill_color,
137136
outline=self.outline_color,
138137
)
139138
elif style == Button.ROUNDRECT:
140139
self.body = RoundRect(
141-
x,
142-
y,
140+
0,
141+
0,
143142
width,
144143
height,
145144
r=10,
146145
fill=self.fill_color,
147146
outline=self.outline_color,
148147
)
149148
elif style == Button.SHADOWRECT:
150-
self.shadow = Rect(
151-
x + 2, y + 2, width - 2, height - 2, fill=outline_color
152-
)
149+
self.shadow = Rect(2, 2, width - 2, height - 2, fill=outline_color)
153150
self.body = Rect(
154-
x,
155-
y,
151+
0,
152+
0,
156153
width - 2,
157154
height - 2,
158155
fill=self.fill_color,
159156
outline=self.outline_color,
160157
)
161158
elif style == Button.SHADOWROUNDRECT:
162159
self.shadow = RoundRect(
163-
x + 2, y + 2, width - 2, height - 2, r=10, fill=self.outline_color
160+
2, 2, width - 2, height - 2, r=10, fill=self.outline_color
164161
)
165162
self.body = RoundRect(
166-
x,
167-
y,
163+
0,
164+
0,
168165
width - 2,
169166
height - 2,
170167
r=10,
171168
fill=self.fill_color,
172169
outline=self.outline_color,
173170
)
174171
if self.shadow:
175-
self.group.append(self.shadow)
176-
self.group.append(self.body)
172+
self.append(self.shadow)
173+
self.append(self.body)
177174

178175
self.label = label
179176

@@ -184,8 +181,8 @@ def label(self):
184181

185182
@label.setter
186183
def label(self, newtext):
187-
if self._label and self.group and (self.group[-1] == self._label):
188-
self.group.pop()
184+
if self._label and self and (self[-1] == self._label):
185+
self.pop()
189186

190187
self._label = None
191188
if not newtext or (self._label_color is None): # no new text
@@ -197,10 +194,10 @@ def label(self, newtext):
197194
dims = self._label.bounding_box
198195
if dims[2] >= self.width or dims[3] >= self.height:
199196
raise RuntimeError("Button not large enough for label")
200-
self._label.x = self.x + (self.width - dims[2]) // 2
201-
self._label.y = self.y + self.height // 2
197+
self._label.x = (self.width - dims[2]) // 2
198+
self._label.y = self.height // 2
202199
self._label.color = self._label_color
203-
self.group.append(self._label)
200+
self.append(self._label)
204201

205202
if (self.selected_label is None) and (self._label_color is not None):
206203
self.selected_label = (~self._label_color) & 0xFFFFFF
@@ -230,6 +227,16 @@ def selected(self, value):
230227
if self._label is not None:
231228
self._label.color = new_label
232229

230+
@property
231+
def group(self):
232+
"""Return self for compatibility with old API."""
233+
print(
234+
"Warning: The group property is being deprecated. "
235+
"User code should be updated to add the Button directly to the "
236+
"Display or other Groups."
237+
)
238+
return self
239+
233240
def contains(self, point):
234241
"""Used to determine if a point is contained within a button. For example,
235242
``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for

examples/display_button_customfont.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
buttons.append(button_6)
140140

141141
for b in buttons:
142-
splash.append(b.group)
142+
splash.append(b)
143143

144144
while True:
145145
p = ts.touch_point

examples/display_button_simpletest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
)
4646

4747
# Add button to the display context
48-
splash.append(button.group)
48+
splash.append(button)
4949

5050
# Loop and look for touches
5151
while True:

examples/display_button_soundboard.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
label_color=None,
4141
name=spot["file"],
4242
)
43-
pyportal.splash.append(button.group)
43+
pyportal.splash.append(button)
4444
buttons.append(button)
4545

4646
last_pressed = None

0 commit comments

Comments
 (0)