From 6ff2955e24ff36d7704a9c9d84d2e58b04d1900a Mon Sep 17 00:00:00 2001 From: ladyada Date: Sun, 24 Mar 2019 15:52:28 -0400 Subject: [PATCH 1/3] fix selection code and label print --- adafruit_button.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/adafruit_button.py b/adafruit_button.py index 33c5c6f..fc10d47 100755 --- a/adafruit_button.py +++ b/adafruit_button.py @@ -96,6 +96,7 @@ def __init__(self, *, x, y, width, height, name=None, style=RECT, self.group = displayio.Group() self.name = name self.label = label + self.body = self.fill = self.shadow = None self.fill_color = _check_color(fill_color) self.outline_color = _check_color(outline_color) @@ -111,7 +112,6 @@ def __init__(self, *, x, y, width, height, name=None, style=RECT, self.selected_outline = (~self.outline_color) & 0xFFFFFF if outline_color or fill_color: - self.body = self.shadow = None if style == Button.RECT: self.body = Rect(x, y, width, height, fill=self.fill_color, outline=self.outline_color) @@ -135,10 +135,10 @@ def __init__(self, *, x, y, width, height, name=None, style=RECT, if label and (label_color is not None): # button with text label if not label_font: raise RuntimeError("Please provide label font") - dims = label_font.text_bounding_box(label) + self.label = Label(label_font, text=label) + dims = self.label.bounding_box if dims[2] >= width or dims[3] >= height: raise RuntimeError("Button not large enough for label") - self.label = Label(label_font, text=label) self.label.x = x + (width - dims[2]) // 2 self.label.y = y + height // 2 self.label.color = label_color @@ -146,7 +146,6 @@ def __init__(self, *, x, y, width, height, name=None, style=RECT, if self.selected_label is None and label_color is not None: self.selected_label = (~label_color) & 0xFFFFFF - # print(dims) # else: # ok just a bounding box # self.bodyshape = displayio.Shape(width, height) @@ -159,16 +158,23 @@ def selected(self): @selected.setter def selected(self, value): - if value != self._selected: - self._selected = value + if value == self._selected: + return # bail now, nothing more to do + self._selected = value if self._selected: - self.body.fill = self.selected_fill - self.body.outline = self.selected_outline - self.label.color = self.selected_label + new_fill = self.selected_fill + new_out = self.selected_outline + new_label = self.selected_label else: - self.body.fill = self.fill_color - self.body.outline = self.outline_color - self.label.color = self.label_color + new_fill = self.fill_color + new_out = self.outline_color + new_label = self.label_color + + if self.body is not None: + self.body.fill = new_fill + self.body.outline = new_out + if self.label is not None: + self.label.color = new_label def contains(self, point): """Used to determine if a point is contained within a button. For example, From c73cab2da07bdf23954de8d269dfa571dc1a14bb Mon Sep 17 00:00:00 2001 From: ladyada Date: Sun, 24 Mar 2019 15:52:55 -0400 Subject: [PATCH 2/3] a note --- adafruit_button.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_button.py b/adafruit_button.py index fc10d47..4025b9b 100755 --- a/adafruit_button.py +++ b/adafruit_button.py @@ -169,7 +169,7 @@ def selected(self, value): new_fill = self.fill_color new_out = self.outline_color new_label = self.label_color - + # update all relevant colros! if self.body is not None: self.body.fill = new_fill self.body.outline = new_out From 3d242c408d03cf0f4c4b931b3685755e2bb2ff78 Mon Sep 17 00:00:00 2001 From: ladyada Date: Sun, 24 Mar 2019 19:18:49 -0400 Subject: [PATCH 3/3] Allow dynamic changing of the button label --- adafruit_button.py | 55 ++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/adafruit_button.py b/adafruit_button.py index 4025b9b..115fc66 100755 --- a/adafruit_button.py +++ b/adafruit_button.py @@ -95,12 +95,13 @@ def __init__(self, *, x, y, width, height, name=None, style=RECT, self._selected = False self.group = displayio.Group() self.name = name - self.label = label + self._label = label self.body = self.fill = self.shadow = None self.fill_color = _check_color(fill_color) self.outline_color = _check_color(outline_color) - self.label_color = label_color + self._label_color = label_color + self._label_font = label_font # Selecting inverts the button colors! self.selected_fill = _check_color(selected_fill) self.selected_outline = _check_color(selected_outline) @@ -132,25 +133,41 @@ def __init__(self, *, x, y, width, height, name=None, style=RECT, self.group.append(self.shadow) self.group.append(self.body) - if label and (label_color is not None): # button with text label - if not label_font: - raise RuntimeError("Please provide label font") - self.label = Label(label_font, text=label) - dims = self.label.bounding_box - if dims[2] >= width or dims[3] >= height: - raise RuntimeError("Button not large enough for label") - self.label.x = x + (width - dims[2]) // 2 - self.label.y = y + height // 2 - self.label.color = label_color - self.group.append(self.label) - - if self.selected_label is None and label_color is not None: - self.selected_label = (~label_color) & 0xFFFFFF + self.label = label # else: # ok just a bounding box # self.bodyshape = displayio.Shape(width, height) # self.group.append(self.bodyshape) + @property + def label(self): + """The text label of the button""" + return self._label.text + + @label.setter + def label(self, newtext): + if self._label and (self.group[-1] == self._label): + self.group.pop() + + self._label = None + if not newtext or (self._label_color is None): # no new text + return # nothing to do! + + if not self._label_font: + raise RuntimeError("Please provide label font") + self._label = Label(self._label_font, text=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.color = self._label_color + self.group.append(self._label) + + if (self.selected_label is None) and (self._label_color is not None): + self.selected_label = (~self._label_color) & 0xFFFFFF + + @property def selected(self): """Selected inverts the colors.""" @@ -168,13 +185,13 @@ def selected(self, value): else: new_fill = self.fill_color new_out = self.outline_color - new_label = self.label_color + new_label = self._label_color # update all relevant colros! if self.body is not None: self.body.fill = new_fill self.body.outline = new_out - if self.label is not None: - self.label.color = new_label + if self._label is not None: + self._label.color = new_label def contains(self, point): """Used to determine if a point is contained within a button. For example,