first pass at adding some menu drawing functionality

This commit is contained in:
Sundog Jones 2020-08-02 12:24:48 -07:00
parent be449fc957
commit 7d3632eda1
2 changed files with 52 additions and 2 deletions

View File

@ -68,6 +68,38 @@ class SoundslabDisplay:
return (voltage, capacity) return (voltage, capacity)
def updateMenu(self, menu_data=None):
# always start with a transparent menu overlay image
im_menu_overlay = Image.new("RGBA", self.screen_size, (0, 0, 0, 0))
menu_overlay = ImageDraw.Draw(im_menu_overlay)
if self.showMenu and menu_data is not None:
# we have a menu to display
#
# menu_data is structured as a list of dicts describing the rows to be displayed:
# [
# { output: "UP_ARROW" },
# { output: "Toggle repeat", selected: True },
# { output: "Toggle shuffle" },
# { output: "Show queue" },
# { output: "DOWN_ARROW" }
# ]
# a translucent red background rectangle for the menu area itself
menu_overlay.rectangle([(21,21), (self.screen_size[0] - 21,self.screen_size[1] - 21)], (200, 0, 0, 127)
offset_from_top = 21 # start at the 21st row of pixels to draw inside the outer overlay
font = ImageFont.truetype(font='/usr/share/fonts/truetype/hack/HackRegular.ttf', size=14)
for row in menu_data:
if row.selected:
menu_overlay.rectangle([(21, offset_from_top),(self.screen_size[0] - 21, offset_from_top + 40)], self.bg_color) # highlight background for selected menu item
output_size = menu_overlay.textsize(output) # get the size of the text to draw so we can center it in our rectangle for this row
menu_overlay.text((21 + floor(output_size[0] / 2), offset_from_top + (40 - floor(output_size[1] / 2))), row.output, font=font, fill=self.fg_color if row.selected else self.bg_color) # draw output text in appropriate color
offset_from_top += 40
# finally, set the current_menu image
self.current_menu = im_menu_overlay.copy()
def updateOverlay(self): def updateOverlay(self):
# initialize overlay # initialize overlay

View File

@ -27,6 +27,9 @@ GPIO.setmode(GPIO.BCM)
# with a "PULL UP", which weakly pulls the input signal to 3.3V. # with a "PULL UP", which weakly pulls the input signal to 3.3V.
GPIO.setup(BUTTONS, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(BUTTONS, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# a temporary place to store menu state
show_menu = False
# some button handlers # some button handlers
def NE(): def NE():
# play/pause toggle # play/pause toggle
@ -57,7 +60,11 @@ def SW():
def NW(): def NW():
# open menu # open menu
pass print("toggling menu")
if show_menu is True:
show_menu = False
else:
show_menu = True
def SE(): def SE():
# next track # next track
@ -127,6 +134,17 @@ for pin in BUTTONS:
GPIO.add_event_detect(pin, GPIO.FALLING, handle_button, bouncetime=100) GPIO.add_event_detect(pin, GPIO.FALLING, handle_button, bouncetime=100)
while True: while True:
if show_menu is True:
# test menu
display.updateMenu(
[
{output: ""},
{output: "Toggle repeat", selected=True},
{output: "Toggle shuffle"},
{output: "View queue"},
{output: ""}
]
)
# test album art # test album art
display.updateAlbumArt() display.updateAlbumArt()
# test setting up the overlay # test setting up the overlay