-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdisplayio_layout_tab_layout_touchtest.py
137 lines (120 loc) · 3.56 KB
/
displayio_layout_tab_layout_touchtest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# SPDX-FileCopyrightText: 2022 Tim C
#
# SPDX-License-Identifier: MIT
"""
Make a TabLayout change tabs with the touchscreen
"""
import displayio
import board
import terminalio
import adafruit_touchscreen
from adafruit_display_text.bitmap_label import Label
from adafruit_display_shapes.rect import Rect
from adafruit_display_shapes.circle import Circle
from adafruit_display_shapes.triangle import Triangle
from adafruit_displayio_layout.layouts.tab_layout import TabLayout
# built-in display
display = board.DISPLAY
# ------------ Touchscreen setup --------------- #
# See: https://learn.adafruit.com/making-a-pyportal-user-interface-displayio/display
display = board.DISPLAY # create the display object
screen_width = display.width
screen_height = display.height
ts = adafruit_touchscreen.Touchscreen(
board.TOUCH_XL,
board.TOUCH_XR,
board.TOUCH_YD,
board.TOUCH_YU,
calibration=((5200, 59000), (5800, 57000)),
size=(screen_width, screen_height),
)
# create and show main_group
main_group = displayio.Group()
display.root_group = main_group
font = terminalio.FONT
# create the page layout
test_page_layout = TabLayout(
x=0,
y=0,
display=board.DISPLAY,
tab_text_scale=2,
custom_font=font,
inactive_tab_spritesheet="bmps/inactive_tab_sprite.bmp",
showing_tab_spritesheet="bmps/active_tab_sprite.bmp",
showing_tab_text_color=0x00AA59,
inactive_tab_text_color=0xEEEEEE,
inactive_tab_transparent_indexes=(0, 1),
showing_tab_transparent_indexes=(0, 1),
tab_count=4,
)
# make page content Groups
page_1_group = displayio.Group()
page_2_group = displayio.Group()
page_3_group = displayio.Group()
page_4_group = displayio.Group()
# labels
page_1_lbl = Label(
font=terminalio.FONT,
scale=2,
text="This is the first page!",
anchor_point=(0, 0),
anchored_position=(10, 10),
)
page_2_lbl = Label(
font=terminalio.FONT,
scale=2,
text="This page is the\nsecond page!",
anchor_point=(0, 0),
anchored_position=(10, 10),
)
page_3_lbl = Label(
font=terminalio.FONT,
scale=2,
text="The third page is fun!",
anchor_point=(0, 0),
anchored_position=(10, 10),
)
page_4_lbl = Label(
font=terminalio.FONT,
scale=2,
text="The fourth page\nis where it's at",
anchor_point=(0, 0),
anchored_position=(10, 10),
)
# shapes
square = Rect(x=20, y=70, width=40, height=40, fill=0x00DD00)
circle = Circle(50, 120, r=30, fill=0xDD00DD)
triangle = Triangle(50, 0, 100, 50, 0, 50, fill=0xDDDD00)
rectangle = Rect(x=80, y=80, width=100, height=50, fill=0x0000DD)
triangle.x = 80
triangle.y = 70
# add everything to their page groups
page_1_group.append(square)
page_1_group.append(page_1_lbl)
page_2_group.append(page_2_lbl)
page_2_group.append(circle)
page_3_group.append(page_3_lbl)
page_3_group.append(triangle)
page_4_group.append(page_4_lbl)
page_4_group.append(rectangle)
# add the pages to the layout, supply your own page names
test_page_layout.add_content(page_1_group, "One")
test_page_layout.add_content(page_2_group, "Two")
test_page_layout.add_content(page_3_group, "Thr")
test_page_layout.add_content(page_4_group, "For")
# add it to the group that is showing on the display
main_group.append(test_page_layout)
# add something new after the TabLayout was already created
another_text = Label(
terminalio.FONT,
text="And another thing!",
scale=2,
color=0x00FF00,
anchor_point=(0, 0),
anchored_position=(100, 100),
)
test_page_layout.showing_page_content.append(another_text)
while True:
touch = ts.touch_point
if touch:
test_page_layout.handle_touch_events(touch)