Skip to content

Allow for changing label of button #3

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 3 commits into from
Mar 25, 2019
Merged
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
75 changes: 49 additions & 26 deletions adafruit_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +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)
Expand All @@ -111,7 +113,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)
Expand All @@ -132,43 +133,65 @@ 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")
dims = label_font.text_bounding_box(label)
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
self.group.append(self.label)

if self.selected_label is None and label_color is not None:
self.selected_label = (~label_color) & 0xFFFFFF
# print(dims)
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."""
return self._selected

@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
# 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

def contains(self, point):
"""Used to determine if a point is contained within a button. For example,
Expand Down