Skip to content

Commit fe0564b

Browse files
committed
alignment options. defaults LEFT!. PC example
1 parent e786890 commit fe0564b

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

circuitpython_display_frame.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141

4242
class Frame(displayio.Group):
4343
# pylint: disable=too-many-arguments
44+
45+
LABEL_ALIGN_RIGHT = 2
46+
LABEL_ALIGN_CENTER = 1
47+
LABEL_ALIGN_LEFT = 0
48+
4449
"""
4550
A rounded rectangle frame with a text label at the top center.
4651
@@ -70,6 +75,8 @@ def __init__(
7075
text_color=None,
7176
background_color=0x0,
7277
stroke=2,
78+
top_label=True,
79+
label_align=LABEL_ALIGN_LEFT,
7380
):
7481
super().__init__(x=x, y=y)
7582

@@ -96,8 +103,28 @@ def __init__(
96103
padding_left=2,
97104
padding_right=1,
98105
)
99-
self.label.anchor_point = (0.5, 0.5)
100-
self.label.anchored_position = (width // 2, 0)
106+
107+
self.label_align = label_align
108+
self.top_label = top_label
109+
101110
if self.label.bounding_box[2] * 2 < width - (corner_radius * 2):
102111
self.label.scale = 2
112+
113+
if top_label:
114+
_anchored_pos_y = 0
115+
else:
116+
_anchored_pos_y = height - 6
117+
118+
if label_align == Frame.LABEL_ALIGN_CENTER:
119+
_anchor_x = 0.5
120+
_anchored_pos_x = width // 2
121+
elif label_align == Frame.LABEL_ALIGN_RIGHT:
122+
_anchor_x = 1.0
123+
_anchored_pos_x = width - corner_radius
124+
else: # label_align == Frame.LABEL_ALIGN_LEFT:
125+
_anchor_x = 0
126+
_anchored_pos_x = corner_radius
127+
128+
self.label.anchor_point = (_anchor_x, 0.5)
129+
self.label.anchored_position = (_anchored_pos_x, _anchored_pos_y)
103130
self.append(self.label)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# SPDX-FileCopyrightText: 2020 Tim C, written for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
"""
5+
Make green and purple rectangles and a
6+
"Hello World" label.
7+
"""
8+
import displayio
9+
10+
11+
from circuitpython_display_frame import Frame
12+
from blinka_displayio_pygamedisplay import PyGameDisplay
13+
14+
15+
# Make the display context
16+
display = PyGameDisplay(icon="", width=800, height=600)
17+
18+
# Make the display context
19+
main_group = displayio.Group(max_size=10)
20+
21+
22+
example_frame = Frame(20, 20, display.width // 3, display.height - 40)
23+
main_group.append(example_frame)
24+
25+
display.show(main_group)
26+
27+
while display.running:
28+
pass

0 commit comments

Comments
 (0)