Skip to content
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

Added "cx" and "cy" property to the label to change center of Label #22

Merged
merged 10 commits into from
Jan 28, 2020
28 changes: 27 additions & 1 deletion adafruit_display_text/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def __init__(self, font, *, text=None, max_glyphs=None, color=0xffffff,
self.width = max_glyphs
self.font = font
self._text = None

self._anchor_point = (0, 0)

self.palette = displayio.Palette(2)
self.palette.make_transparent(0)
self.palette[1] = color
Expand Down Expand Up @@ -172,3 +173,28 @@ def text(self):
@text.setter
def text(self, new_text):
self._update_text(new_text)

@property
def anchor_point(self):
"""Point that anchored_position moves relative to.
Tuple with decimal percentage of width and height.
(E.g. (0,0) is top left, (1.0, 0.5): is middle right.)"""
return self._anchor_point

@anchor_point.setter
def anchor_point(self, new_anchor_point):
self._anchor_point = new_anchor_point

@property
def anchored_position(self):
"""Position relative to the anchor_point. Tuple containing x,y
pixel coordinates."""
_anchored_position = (
self.x-self._boundingbox[2]*self._anchor_point[0],
self.y-self._boundingbox[3]*self._anchor_point[1])
return _anchored_position
Copy link
Contributor

Choose a reason for hiding this comment

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

You can avoid creating _anchored_position and shorten this up to something like:

return self.x-self._boundingbox[2]*self._anchor_point[0],
       self.y-self._boundingbox[3]*self._anchor_point[1]

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I see. Thank you, this way it won't need to create the variable at all.


@anchored_position.setter
def anchored_position(self, new_position):
self.x = int(new_position[0]-(self._boundingbox[2]*self._anchor_point[0]))
self.y = int(new_position[1]-(self._boundingbox[3]*self._anchor_point[1]))