Merge pull request #2 from adafruit/use_anchored_group

use adafruit_anchored_group instead of sized_group
This commit is contained in:
Scott Shawcroft 2025-04-15 09:43:21 -07:00 committed by GitHub
commit 8cc6ab40ec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 17 additions and 66 deletions

View file

@ -7,13 +7,11 @@ loaded by adafruit_bitmap_font
"""
import array
import board
import displayio
import pathlib
import supervisor
import sys
import usb
from adafruit_display_text import label
from adafruit_bitmap_font import bitmap_font
from adafruit_display_text.text_box import TextBox
from adafruit_display_text.bitmap_label import Label
@ -21,7 +19,7 @@ from adafruit_displayio_layout.layouts.grid_layout import GridLayout
from adafruit_anchored_tilegrid import AnchoredTileGrid
import adafruit_imageload
import adafruit_usb_host_descriptors
from sized_group import SizedGroup
from adafruit_anchored_group import AnchoredGroup
display = supervisor.runtime.display
font_file = "/fonts/terminal.lvfontbin"
@ -36,7 +34,7 @@ bg_tg = displayio.TileGrid(bitmap=background_bmp, pixel_shader=bg_palette)
main_group.append(bg_tg)
# load the mouse cursor bitmap
mouse_bmp = displayio.OnDiskBitmap("mouse_cursor.bmp")
mouse_bmp = displayio.OnDiskBitmap("launcher_assets/mouse_cursor.bmp")
# make the background pink pixels transparent
mouse_bmp.pixel_shader.make_transparent(0)
@ -47,15 +45,15 @@ mouse_tg = displayio.TileGrid(mouse_bmp, pixel_shader=mouse_bmp.pixel_shader)
# move it to the center of the display
mouse_tg.x = display.width // 2
mouse_tg.y = display.height // 2
#046d:c52f
# 046d:c52f
#mouse = usb.core.find(idVendor=0x046d, idProduct=0xc52f)
# mouse = usb.core.find(idVendor=0x046d, idProduct=0xc52f)
DIR_IN = 0x80
mouse_interface_index, mouse_endpoint_address = None, None
mouse = None
#scan for connected USB device and loop over any found
# scan for connected USB device and loop over any found
print("scanning usb")
for device in usb.core.find(find_all=True):
# print device info
@ -152,7 +150,7 @@ config = {
cell_width = WIDTH // config["width"]
default_icon_bmp, default_icon_palette = adafruit_imageload.load("default_icon_64.bmp")
default_icon_bmp, default_icon_palette = adafruit_imageload.load("launcher_assets/default_icon.bmp")
default_icon_palette.make_transparent(0)
menu_grid = GridLayout(x=10, y=26, width=WIDTH, height=HEIGHT, grid_size=(config["width"], config["height"]),
divider_lines=False)
@ -160,7 +158,7 @@ main_group.append(menu_grid)
menu_title_txt = Label(font, text=config["menu_title"])
menu_title_txt.anchor_point = (0.5, 0.5)
menu_title_txt.anchored_position = (display.width//2, 2)
menu_title_txt.anchored_position = (display.width // 2, 2)
main_group.append(menu_title_txt)
app_titles = []
@ -172,7 +170,7 @@ for path in app_path.iterdir():
code_file = path / "code.py"
if not code_file.exists():
continue
cell_group = SizedGroup()
cell_group = AnchoredGroup()
icon_file = path / "icon.bmp"
if not icon_file.exists():
icon_file = None
@ -189,18 +187,19 @@ for path in app_path.iterdir():
icon_tg = displayio.TileGrid(bitmap=icon_bmp, pixel_shader=icon_palette)
cell_group.append(icon_tg)
icon_tg.x = cell_width//2 - icon_tg.tile_width//2
title_txt = TextBox(font, text=apps[-1]["title"], width=WIDTH // config["width"], height=18, align=TextBox.ALIGN_CENTER)
icon_tg.x = cell_width // 2 - icon_tg.tile_width // 2
title_txt = TextBox(font, text=apps[-1]["title"], width=WIDTH // config["width"], height=18,
align=TextBox.ALIGN_CENTER)
cell_group.append(title_txt)
title_txt.anchor_point = (0,0)
title_txt.anchor_point = (0, 0)
title_txt.anchored_position = (0, icon_tg.y + icon_tg.tile_height)
app_titles.append(title_txt)
menu_grid.add_content(cell_group, grid_position=(i % config["width"], i // config["width"]), cell_size=(1, 1))
i += 1
left_bmp, left_palette = adafruit_imageload.load("arrow_left.bmp")
left_bmp, left_palette = adafruit_imageload.load("launcher_assets/arrow_left.bmp")
left_palette.make_transparent(0)
right_bmp, right_palette = adafruit_imageload.load("arrow_right.bmp")
right_bmp, right_palette = adafruit_imageload.load("launcher_assets/arrow_right.bmp")
right_palette.make_transparent(0)
left_tg = AnchoredTileGrid(bitmap=left_bmp, pixel_shader=left_palette)
@ -257,9 +256,10 @@ while True:
clicked_cell = menu_grid.which_cell_contains((mouse_tg.x, mouse_tg.y))
if clicked_cell is not None:
index = clicked_cell[1] * config["width"] + clicked_cell[0]
if index is not None:
supervisor.set_next_code_file(config["apps"][index]["file"], sticky_on_reload=True, reload_on_error=True, working_directory="/apps/matrix")
supervisor.set_next_code_file(config["apps"][index]["file"], sticky_on_reload=True, reload_on_error=True,
working_directory="/apps/matrix")
if mouse and not mouse.is_kernel_driver_active(0):
mouse.attach_kernel_driver(0)

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 198 B

View file

@ -1,49 +0,0 @@
import displayio
class SizedGroup(displayio.Group):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@property
def size(self):
min_x = 0
min_y = 0
max_x = 0
max_y = 0
for element in self:
# print(type(element))
if type(element) == displayio.TileGrid:
if element.x < min_x:
min_x = element.x
if element.y < min_y:
min_y = element.y
_element_max_x = element.x + (element.width * element.tile_width)
_element_max_y = element.y + (element.height * element.tile_height)
if _element_max_x > max_x:
max_x = _element_max_x
if _element_max_y > max_y:
max_y = _element_max_y
else:
if element.x < min_x:
min_x = element.x
if element.y < min_y:
min_y = element.y
_element_max_x = element.x + (element.width * element.scale)
_element_max_y = element.y + (element.height * element.scale)
if _element_max_x > max_x:
max_x = _element_max_x
if _element_max_y > max_y:
max_y = _element_max_y
return max_x - min_x, max_y - min_y
@property
def width(self):
return self.size[0]
@property
def height(self):
return self.size[1]