62 lines
1.9 KiB
Python
62 lines
1.9 KiB
Python
from typing import Tuple
|
|
|
|
from .base import View
|
|
from .button_interface import ButtonInterface
|
|
from .list_select import ListItem, CarouselView
|
|
|
|
from PIL import ImageDraw, Image
|
|
|
|
|
|
def _make_text_item(text):
|
|
return ListItem(lambda draw, pos, fill, t=text, **kw: draw.text(pos, str(t), fill=fill, **kw))
|
|
|
|
|
|
class MenuView(View, ButtonInterface):
|
|
|
|
def __init__(self, parent, im_size, center,
|
|
bluetooth_pair_command=None,
|
|
deactivate_command=None):
|
|
self.deactivate_command = deactivate_command
|
|
self.bluetooth_pair_command = bluetooth_pair_command
|
|
self.item_list = CarouselView(render_height=124, large_font_size=20)
|
|
self._actions = [
|
|
("BT Pair", self.bluetooth_pair_command),
|
|
]
|
|
self.item_list.items = [_make_text_item(label) for label, _ in self._actions]
|
|
super().__init__(parent, im_size, center)
|
|
|
|
def update_weight(self, weight: float) -> Image.Image:
|
|
im = self.bkg_im.copy()
|
|
draw = ImageDraw.Draw(im)
|
|
self.item_list.render(draw, y_start=10)
|
|
return im
|
|
|
|
def left_press(self):
|
|
self.item_list.select_previous()
|
|
|
|
def left_long_press(self):
|
|
self.deactivate_command()
|
|
|
|
def right_press(self):
|
|
self.item_list.select_next()
|
|
|
|
def right_long_press(self):
|
|
_, action = self._actions[self.item_list.selected_index]
|
|
if action:
|
|
action()
|
|
|
|
def has_button(self) -> Tuple[bool, bool, bool, bool]:
|
|
return True, True, True, True
|
|
|
|
def render_left_press(self, draw, x, y):
|
|
draw.regular_polygon((x, y + 2, 5), 3, fill='black')
|
|
|
|
def render_left_long_press(self, draw, x, y):
|
|
draw.text((x, y - 5), 'Back', fill='black')
|
|
|
|
def render_right_press(self, draw, x, y):
|
|
draw.regular_polygon((x, y + 4, 5), 3, fill='black', rotation=180)
|
|
|
|
def render_right_long_press(self, draw, x, y):
|
|
draw.text((x - 30, y - 5), 'Select', fill='black')
|