Skip to content

Allow label to be scaled #42

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
Dec 14, 2023
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
4 changes: 3 additions & 1 deletion adafruit_button/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ def __init__(
label_color=0x0,
selected_fill=None,
selected_outline=None,
selected_label=None
selected_label=None,
label_scale=None
):
super().__init__(
x=x,
Expand All @@ -142,6 +143,7 @@ def __init__(
label_font=label_font,
label_color=label_color,
selected_label=selected_label,
label_scale=label_scale,
)

self.body = self.fill = self.shadow = None
Expand Down
14 changes: 10 additions & 4 deletions adafruit_button/button_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def __init__(
label=None,
label_font=None,
label_color=0x0,
selected_label=None
selected_label=None,
label_scale=None
):
super().__init__(x=x, y=y)
self.x = x
Expand All @@ -72,6 +73,7 @@ def __init__(
self._label_color = label_color
self._label_font = label_font
self._selected_label = _check_color(selected_label)
self._label_scale = label_scale or 1
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is passed in to Label which wouldn't like a None value for scale, so this defaults the value to 1. I prefer for optional parameters to default to None, that way callers can pass in a None and get default behavior. But if it's desirable to set the default on the parameter to 1 as Label does it, I can change it.


@property
def label(self):
Expand All @@ -89,14 +91,18 @@ def label(self, newtext):

if not self._label_font:
raise RuntimeError("Please provide label font")
self._label = Label(self._label_font, text=newtext)
dims = self._label.bounding_box
self._label = Label(self._label_font, text=newtext, scale=self._label_scale)
dims = list(self._label.bounding_box)
dims[2] *= self._label.scale
dims[3] *= self._label.scale
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not in love with having to do this with the bounding box dimensions. As I mentioned in the description, I'm not sure if it was intentional or not that the bounding box for Label isn't scaled, but since that's a separate repository, I thought I'd just work with what we have. I'm open to suggestions here.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think the approach you've gone for here is fine. It may be worth considering changing in Label in the long term, but there could be reasons in there why it's like this, I don't know for certain.

if dims[2] >= self.width or dims[3] >= self.height:
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
dims = list(self._label.bounding_box)
dims[2] *= self._label.scale
dims[3] *= self._label.scale
if len(self._label.text) <= 1:
raise RuntimeError("Button not large enough for label")
self._label.x = (self.width - dims[2]) // 2
Expand Down
4 changes: 3 additions & 1 deletion adafruit_button/sprite_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def __init__(
selected_label=None,
bmp_path=None,
selected_bmp_path=None,
transparent_index=None
transparent_index=None,
label_scale=None
):
if bmp_path is None:
raise ValueError("Please supply bmp_path. It cannot be None.")
Expand All @@ -71,6 +72,7 @@ def __init__(
label_font=label_font,
label_color=label_color,
selected_label=selected_label,
label_scale=label_scale,
)

self._bmp, self._bmp_palette = load(bmp_path)
Expand Down