Skip to content

Mutable height and width #27

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 4 commits into from
Feb 23, 2021
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
161 changes: 112 additions & 49 deletions adafruit_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,66 @@ class Button(displayio.Group):
:param selected_fill: Inverts the fill color.
:param selected_outline: Inverts the outline color.
:param selected_label: Inverts the label color.

"""

def _empty_self_group(self):
while len(self) > 0:
self.pop()

def _create_body(self):
if (self.outline_color is not None) or (self.fill_color is not None):
if self.style == Button.RECT:
self.body = Rect(
0,
0,
self.width,
self.height,
fill=self._fill_color,
outline=self._outline_color,
)
elif self.style == Button.ROUNDRECT:
self.body = RoundRect(
0,
0,
self.width,
self.height,
r=10,
fill=self._fill_color,
outline=self._outline_color,
)
elif self.style == Button.SHADOWRECT:
self.shadow = Rect(
2, 2, self.width - 2, self.height - 2, fill=self.outline_color
)
self.body = Rect(
0,
0,
self.width - 2,
self.height - 2,
fill=self._fill_color,
outline=self._outline_color,
)
elif self.style == Button.SHADOWROUNDRECT:
self.shadow = RoundRect(
2,
2,
self.width - 2,
self.height - 2,
r=10,
fill=self._outline_color,
)
self.body = RoundRect(
0,
0,
self.width - 2,
self.height - 2,
r=10,
fill=self._fill_color,
outline=self._outline_color,
)
if self.shadow:
self.append(self.shadow)

RECT = const(0)
ROUNDRECT = const(1)
SHADOWRECT = const(2)
Expand All @@ -86,13 +144,14 @@ def __init__(
super().__init__(x=x, y=y)
self.x = x
self.y = y
self.width = width
self.height = height
self._width = width
self._height = height
self._font = label_font
self._selected = False
self.name = name
self._label = label
self.body = self.fill = self.shadow = None
self.style = style

self._fill_color = _check_color(fill_color)
self._outline_color = _check_color(outline_color)
Expand All @@ -108,51 +167,8 @@ def __init__(
if self.selected_outline is None and outline_color is not None:
self.selected_outline = (~self._outline_color) & 0xFFFFFF

if (outline_color is not None) or (fill_color is not None):
if style == Button.RECT:
self.body = Rect(
0,
0,
width,
height,
fill=self._fill_color,
outline=self._outline_color,
)
elif style == Button.ROUNDRECT:
self.body = RoundRect(
0,
0,
width,
height,
r=10,
fill=self._fill_color,
outline=self._outline_color,
)
elif style == Button.SHADOWRECT:
self.shadow = Rect(2, 2, width - 2, height - 2, fill=outline_color)
self.body = Rect(
0,
0,
width - 2,
height - 2,
fill=self._fill_color,
outline=self._outline_color,
)
elif style == Button.SHADOWROUNDRECT:
self.shadow = RoundRect(
2, 2, width - 2, height - 2, r=10, fill=self._outline_color
)
self.body = RoundRect(
0,
0,
width - 2,
height - 2,
r=10,
fill=self._fill_color,
outline=self._outline_color,
)
if self.shadow:
self.append(self.shadow)
self._create_body()
if self.body:
self.append(self.body)

self.label = label
Expand All @@ -176,7 +192,13 @@ def label(self, newtext):
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")
while len(self._label.text) > 1 and (
dims[2] >= self.width or dims[3] >= self.height
):
self._label.text = "{}.".format(self._label.text[:-2])
dims = self._label.bounding_box
if len(self._label.text) <= 1:
raise RuntimeError("Button not large enough for label")
self._label.x = (self.width - dims[2]) // 2
self._label.y = self.height // 2
self._label.color = self._label_color
Expand Down Expand Up @@ -291,3 +313,44 @@ def label_color(self):
def label_color(self, new_color):
self._label_color = _check_color(new_color)
self._label.color = self._label_color

@property
def width(self):
"""The width of the button"""
return self._width

@width.setter
def width(self, new_width):
self._width = new_width
self._empty_self_group()
self._create_body()
if self.body:
self.append(self.body)
self.label = self.label

@property
def height(self):
"""The height of the button"""
return self._height

@height.setter
def height(self, new_height):
self._height = new_height
self._empty_self_group()
self._create_body()
if self.body:
self.append(self.body)
self.label = self.label

def resize(self, new_width, new_height):
"""Resize the button to the new width and height given
:param new_width int the desired width
:param new_height int the desired height
"""
self._width = new_width
self._height = new_height
self._empty_self_group()
self._create_body()
if self.body:
self.append(self.body)
self.label = self.label