Skip to content

Commit ddd7c13

Browse files
committed
adding sprite button example
1 parent 96e272e commit ddd7c13

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed

examples/bmps/gradient_button_0.bmp

3.13 KB
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries
2+
# SPDX-License-Identifier: MIT

examples/bmps/gradient_button_1.bmp

2.76 KB
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
import time
4+
import board
5+
import displayio
6+
import adafruit_touchscreen
7+
import terminalio
8+
from adafruit_button.sprite_button import SpriteButton
9+
10+
# These pins are used as both analog and digital! XL, XR and YU must be analog
11+
# and digital capable. YD just need to be digital
12+
ts = adafruit_touchscreen.Touchscreen(
13+
board.TOUCH_XL,
14+
board.TOUCH_XR,
15+
board.TOUCH_YD,
16+
board.TOUCH_YU,
17+
calibration=((5200, 59000), (5800, 57000)),
18+
size=(board.DISPLAY.width, board.DISPLAY.height),
19+
)
20+
21+
# Make the display context
22+
main_group = displayio.Group()
23+
board.DISPLAY.show(main_group)
24+
25+
BUTTON_WIDTH = 10 * 16
26+
BUTTON_HEIGHT = 3 * 16
27+
BUTTON_MARGIN = 20
28+
29+
font = terminalio.FONT
30+
31+
buttons = []
32+
33+
34+
button_0 = SpriteButton(
35+
x=BUTTON_MARGIN,
36+
y=BUTTON_MARGIN,
37+
width=BUTTON_WIDTH,
38+
height=BUTTON_HEIGHT,
39+
label="button0",
40+
label_font=font,
41+
bmp_path="bmps/gradient_button_0.bmp",
42+
selected_bmp_path="bmps/gradient_button_1.bmp",
43+
transparent_index=0,
44+
)
45+
46+
buttons.append(button_0)
47+
48+
for b in buttons:
49+
main_group.append(b)
50+
while True:
51+
p = ts.touch_point
52+
if p:
53+
print(p)
54+
for i, b in enumerate(buttons):
55+
if b.contains(p):
56+
print("Button %d pressed" % i)
57+
b.selected = True
58+
b.label = "pressed"
59+
else:
60+
b.selected = False
61+
b.label = "button0"
62+
63+
else:
64+
for i, b in enumerate(buttons):
65+
if b.selected:
66+
b.selected = False
67+
b.label = "button0"
68+
time.sleep(0.01)

0 commit comments

Comments
 (0)