Skip to content

use extended Group instead of property #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions adafruit_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,13 @@ def __init__(
selected_outline=None,
selected_label=None
):
super().__init__()
super().__init__(x=x, y=y)
self.x = x
self.y = y
self.width = width
self.height = height
self._font = label_font
self._selected = False
self.group = displayio.Group()
self.name = name
self._label = label
self.body = self.fill = self.shadow = None
Expand All @@ -129,51 +128,49 @@ def __init__(
if (outline_color is not None) or (fill_color is not None):
if style == Button.RECT:
self.body = Rect(
x,
y,
0,
0,
width,
height,
fill=self.fill_color,
outline=self.outline_color,
)
elif style == Button.ROUNDRECT:
self.body = RoundRect(
x,
y,
0,
0,
width,
height,
r=10,
fill=self.fill_color,
outline=self.outline_color,
)
elif style == Button.SHADOWRECT:
self.shadow = Rect(
x + 2, y + 2, width - 2, height - 2, fill=outline_color
)
self.shadow = Rect(2, 2, width - 2, height - 2, fill=outline_color)
self.body = Rect(
x,
y,
0,
0,
width - 2,
height - 2,
fill=self.fill_color,
outline=self.outline_color,
)
elif style == Button.SHADOWROUNDRECT:
self.shadow = RoundRect(
x + 2, y + 2, width - 2, height - 2, r=10, fill=self.outline_color
2, 2, width - 2, height - 2, r=10, fill=self.outline_color
)
self.body = RoundRect(
x,
y,
0,
0,
width - 2,
height - 2,
r=10,
fill=self.fill_color,
outline=self.outline_color,
)
if self.shadow:
self.group.append(self.shadow)
self.group.append(self.body)
self.append(self.shadow)
self.append(self.body)

self.label = label

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

@label.setter
def label(self, newtext):
if self._label and self.group and (self.group[-1] == self._label):
self.group.pop()
if self._label and self and (self[-1] == self._label):
self.pop()

self._label = None
if not newtext or (self._label_color is None): # no new text
Expand All @@ -197,10 +194,10 @@ def label(self, newtext):
dims = self._label.bounding_box
if dims[2] >= self.width or dims[3] >= self.height:
raise RuntimeError("Button not large enough for label")
self._label.x = self.x + (self.width - dims[2]) // 2
self._label.y = self.y + self.height // 2
self._label.x = (self.width - dims[2]) // 2
self._label.y = self.height // 2
self._label.color = self._label_color
self.group.append(self._label)
self.append(self._label)

if (self.selected_label is None) and (self._label_color is not None):
self.selected_label = (~self._label_color) & 0xFFFFFF
Expand Down Expand Up @@ -230,6 +227,16 @@ def selected(self, value):
if self._label is not None:
self._label.color = new_label

@property
def group(self):
"""Return self for compatibility with old API."""
print(
"Warning: The group property is being deprecated. "
"User code should be updated to add the Button directly to the "
"Display or other Groups."
)
return self

def contains(self, point):
"""Used to determine if a point is contained within a button. For example,
``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for
Expand Down
2 changes: 1 addition & 1 deletion examples/display_button_customfont.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
buttons.append(button_6)

for b in buttons:
splash.append(b.group)
splash.append(b)

while True:
p = ts.touch_point
Expand Down
2 changes: 1 addition & 1 deletion examples/display_button_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
)

# Add button to the display context
splash.append(button.group)
splash.append(button)

# Loop and look for touches
while True:
Expand Down
2 changes: 1 addition & 1 deletion examples/display_button_soundboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
label_color=None,
name=spot["file"],
)
pyportal.splash.append(button.group)
pyportal.splash.append(button)
buttons.append(button)

last_pressed = None
Expand Down