from typing import Tuple from .base import View from .button_interface import ButtonInterface from PIL import ImageDraw, Image class RecipeSelection(View, ButtonInterface): recipes = [ "Recipe 1", "Recipe 2", ] def __init__(self, parent, im_size, center): self.selected_index = 0 super().__init__(parent, im_size, center) def _get_visual_recipes(self): recipes = self.recipes + ['+'] if len(self.recipes) < 5: return recipes, 0 start = max(0, self.selected_index - 2) end = min(len(recipes), start + 5) recipes = recipes[start:end] if len(recipes) < 5: recipes += ['+'] return recipes, start def update_weight(self, weight: float) -> Image.Image: im = self.bkg_im.copy() draw = ImageDraw.Draw(im) recipes, start = self._get_visual_recipes() for idx, recipe in enumerate(recipes): if idx + start == self.selected_index: draw.rectangle((35, 8 + idx * 20, 130, 25 + idx * 20), fill='black') draw.text((40, 10 + idx * 20), recipe, fill='white') else: draw.text((40, 10 + idx * 20), recipe, fill='black') return im def left_press(self): self.selected_index = (self.selected_index - 1) % (len(self.recipes) + 1) def right_press(self): self.selected_index = (self.selected_index + 1) % (len(self.recipes) + 1) 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): pass 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): pass