From 6842af4a48abdfd2c5e2d6e3bf76aa9ed444bb08 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Mon, 10 Aug 2020 14:45:28 -0500 Subject: [PATCH 1/5] use extended Group instead of property --- adafruit_button.py | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/adafruit_button.py b/adafruit_button.py index bfb119e..551e036 100755 --- a/adafruit_button.py +++ b/adafruit_button.py @@ -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 @@ -129,8 +128,8 @@ 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, @@ -138,8 +137,8 @@ def __init__( ) elif style == Button.ROUNDRECT: self.body = RoundRect( - x, - y, + 0, + 0, width, height, r=10, @@ -148,11 +147,11 @@ def __init__( ) elif style == Button.SHADOWRECT: self.shadow = Rect( - x + 2, y + 2, width - 2, height - 2, fill=outline_color + 0 + 2, 0 + 2, width - 2, height - 2, fill=outline_color ) self.body = Rect( - x, - y, + 0, + 0, width - 2, height - 2, fill=self.fill_color, @@ -160,11 +159,11 @@ def __init__( ) elif style == Button.SHADOWROUNDRECT: self.shadow = RoundRect( - x + 2, y + 2, width - 2, height - 2, r=10, fill=self.outline_color + 0 + 2, 0 + 2, width - 2, height - 2, r=10, fill=self.outline_color ) self.body = RoundRect( - x, - y, + 0, + 0, width - 2, height - 2, r=10, @@ -172,8 +171,8 @@ def __init__( 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 @@ -184,8 +183,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 @@ -197,10 +196,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 = 0 + (self.width - dims[2]) // 2 + self._label.y = 0 + 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 From 4ead3aaaaf69167a476eecad023dc2852ffff75d Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Tue, 11 Aug 2020 21:34:40 -0500 Subject: [PATCH 2/5] add group property for backward compatibility with warning --- adafruit_button.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/adafruit_button.py b/adafruit_button.py index 551e036..9af9866 100755 --- a/adafruit_button.py +++ b/adafruit_button.py @@ -229,6 +229,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 From dd266cbf2684f70ae5c8f60659e7df0e53cf3c23 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Tue, 11 Aug 2020 21:45:19 -0500 Subject: [PATCH 3/5] update example scripts to use new API for adding to other Groups --- examples/display_button_customfont.py | 2 +- examples/display_button_simpletest.py | 2 +- examples/display_button_soundboard.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/display_button_customfont.py b/examples/display_button_customfont.py index 1c6d5aa..bce7679 100644 --- a/examples/display_button_customfont.py +++ b/examples/display_button_customfont.py @@ -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 diff --git a/examples/display_button_simpletest.py b/examples/display_button_simpletest.py index 4db6e28..0fe5a73 100644 --- a/examples/display_button_simpletest.py +++ b/examples/display_button_simpletest.py @@ -45,7 +45,7 @@ ) # Add button to the display context -splash.append(button.group) +splash.append(button) # Loop and look for touches while True: diff --git a/examples/display_button_soundboard.py b/examples/display_button_soundboard.py index de2ec36..06537d2 100644 --- a/examples/display_button_soundboard.py +++ b/examples/display_button_soundboard.py @@ -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 From 4e495dffdf0b6b506931e5be22c8e5b26b637641 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Wed, 12 Aug 2020 23:00:54 -0500 Subject: [PATCH 4/5] remove unneeded zeros --- adafruit_button.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/adafruit_button.py b/adafruit_button.py index 9af9866..de776ee 100755 --- a/adafruit_button.py +++ b/adafruit_button.py @@ -147,7 +147,7 @@ def __init__( ) elif style == Button.SHADOWRECT: self.shadow = Rect( - 0 + 2, 0 + 2, width - 2, height - 2, fill=outline_color + 2, 2, width - 2, height - 2, fill=outline_color ) self.body = Rect( 0, @@ -159,7 +159,7 @@ def __init__( ) elif style == Button.SHADOWROUNDRECT: self.shadow = RoundRect( - 0 + 2, 0 + 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( 0, @@ -196,8 +196,8 @@ 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 = 0 + (self.width - dims[2]) // 2 - self._label.y = 0 + self.height // 2 + self._label.x = (self.width - dims[2]) // 2 + self._label.y = self.height // 2 self._label.color = self._label_color self.append(self._label) From 8057dcf0de11c7207bc02f215233a3345be30622 Mon Sep 17 00:00:00 2001 From: FoamyGuy Date: Wed, 12 Aug 2020 23:03:59 -0500 Subject: [PATCH 5/5] black format --- adafruit_button.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/adafruit_button.py b/adafruit_button.py index de776ee..0a7bef4 100755 --- a/adafruit_button.py +++ b/adafruit_button.py @@ -146,9 +146,7 @@ def __init__( outline=self.outline_color, ) elif style == Button.SHADOWRECT: - self.shadow = Rect( - 2, 2, width - 2, height - 2, fill=outline_color - ) + self.shadow = Rect(2, 2, width - 2, height - 2, fill=outline_color) self.body = Rect( 0, 0,