Skip to content

Commit 8c8df5c

Browse files
committed
Subclass Group and use new accessors.
Fixes #1
1 parent 2ff5676 commit 8c8df5c

File tree

1 file changed

+15
-56
lines changed

1 file changed

+15
-56
lines changed

adafruit_display_text/text_area.py

Lines changed: 15 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@
4545
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Text.git"
4646

4747

48-
class TextArea:
49-
def __init__(self, font, *, text=None, width=None, color=0x0, height=1):
48+
class TextArea(displayio.Group):
49+
def __init__(self, font, *, text=None, width=None, color=0xffffff, height=1):
5050
if not width and not text:
5151
raise RuntimeError("Please provide a width")
52+
super().__init__(max_size=width * height)
5253
if not width:
5354
width = len(text)
5455
self.width = width
@@ -59,16 +60,9 @@ def __init__(self, font, *, text=None, width=None, color=0x0, height=1):
5960
self.p.make_transparent(0)
6061
self.p[1] = color
6162

62-
self.group = displayio.Group(max_size=width * height)
63-
6463
bounds = self.font.get_bounding_box()
6564
self.height = bounds[1]
6665

67-
self.sprites = [None] * (width * height)
68-
69-
self._x = 0
70-
self._y = 0
71-
7266
if text:
7367
self._update_text(text)
7468

@@ -86,30 +80,21 @@ def _update_text(self, new_text):
8680
glyph = self.font.get_glyph(ord(c))
8781
if not glyph:
8882
continue
89-
# Remove any characters that are different
90-
if first_different and c != self._text[i]:
91-
# TODO(tannewt): Make this smarter when we can remove and add things into the middle
92-
# of a group.
93-
for _ in range(len(self.sprites) - i):
94-
try:
95-
self.group.pop()
96-
except IndexError:
97-
break
98-
first_different = False
99-
if not first_different:
100-
position = (self._x + x, self._y + y + self.height - glyph["bounds"][1] - glyph["bounds"][3])
101-
try:
102-
face = displayio.TileGrid(glyph["bitmap"], pixel_shader=self.p, position=position)
103-
except:
104-
face = displayio.Sprite(glyph["bitmap"], pixel_shader=self.p, position=position)
105-
self.group.append(face)
106-
self.sprites[i] = face
107-
x += glyph["shift"][0]
83+
if not self._text or i >= len(self._text) or c != self._text[i]:
84+
face = displayio.TileGrid(glyph.bitmap, pixel_shader=self.p, default_tile=glyph.tile_index,
85+
tile_width=glyph.width, tile_height=glyph.height,
86+
position=(x, y + self.height - glyph.height - glyph.dy))
87+
if i < len(self):
88+
self[i] = face
89+
else:
90+
self.append(face)
91+
x += glyph.shift_x
10892

10993
# TODO skip this for control sequences or non-printables.
11094
i += 1
111-
112-
# TODO: support multiple lines by adjusting y
95+
# Remove the rest
96+
while len(self) > i:
97+
self.pop()
11398
self._text = new_text
11499

115100
@property
@@ -127,29 +112,3 @@ def text(self, t):
127112
@text.setter
128113
def text(self, t):
129114
self._update_text(t)
130-
131-
@property
132-
def y(self):
133-
return self._y
134-
135-
@y.setter
136-
def y(self, new_y):
137-
for sprite in self.sprites:
138-
if not sprite:
139-
continue
140-
pos = sprite.position
141-
sprite.position = (pos[0], (pos[1] - self._y) + new_y)
142-
self._y = new_y
143-
144-
@property
145-
def x(self):
146-
return self._x
147-
148-
@x.setter
149-
def x(self, new_x):
150-
for sprite in self.sprites:
151-
if not sprite:
152-
continue
153-
pos = sprite.position
154-
sprite.position = ((pos[0] - self._x) + new_x, pos[1])
155-
self._x = new_x

0 commit comments

Comments
 (0)