from typing import Tuple from PIL import ImageDraw, Image from ..base import View from ..button_interface import ButtonInterface from .recipe_manager import RecipeManager from .recipe import Recipe class EditRecipe(View, ButtonInterface): def __init__(self, parent, im_size, center, recipe_manager: RecipeManager, deactivate_command, recipe: Recipe = None): self.deactivate_command = deactivate_command self.recipe_manager = recipe_manager self.recipe = recipe if recipe is None: self.recipe = Recipe("New", []) self.confirm_view = False self.selected_field = 0 # 0: name, 1+: steps super().__init__(parent, im_size, center) def _get_visual_steps(self): steps = self.recipe.steps + ['+'] if len(steps) < 4: return steps, 0 start = max(0, self.selected_field - 2) end = min(len(steps), start + 4) steps = steps[start:end] return steps, start def update_weight(self, weight: float) -> Image.Image: im = self.bkg_im.copy() draw = ImageDraw.Draw(im) x = 40 draw.text((x, 10), "Name:", fill='black') if self.selected_field == 0: r = 10 offset = 35 for i in range(0, 90, r // 2): draw.circle((x + i, offset), r, fill='black') draw.text((x, 30), self.recipe.name, fill='white') else: draw.text((x, 30), self.recipe.name, fill='black') visual_steps, start = self._get_visual_steps() for idx, step in enumerate(visual_steps): y_pos = 60 + idx * 20 if start + idx + 1 == self.selected_field: r = 10 offset = 15 for i in range(0, 90, r // 2): draw.circle((x + i, y_pos), r, fill='black') if str(step) != '+': step.step_type.render(draw, (x, y_pos - 5), fill='white') draw.text((x + 30, y_pos - 5), step.value_str, fill='white') else: draw.text((x, y_pos - 5), '+', fill='white') elif str(step) == '+': draw.text((x, y_pos - 5), '+', fill='black') else: step.step_type.render(draw, (x, y_pos - 5), fill='black') draw.text((x + 30, y_pos - 5), step.value_str, fill='black') return im def left_press(self): # edit entry pass def left_long_press(self): # save self.recipe_manager.add_recipe(self.recipe) self.deactivate_command() def right_press(self): self.selected_field += 1 if self.selected_field > len(self.recipe.steps) + 1: self.selected_field = 0 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, 5), 3, fill='black', rotation=270) def render_left_long_press(self, draw, x, y): draw.text((x - 3, y - 5), 'Save', fill='black') def render_right_press(self, draw, x, y): draw.regular_polygon((x, y + 6, 5), 3, fill='black', rotation=180) def render_right_long_press(self, draw, x, y): draw.text((x - 30, y - 5), 'Cancel', fill='black')