Files
frontend-dev/frontend/views/recipes/edit_step.py

319 lines
12 KiB
Python

from typing import Tuple
from PIL import ImageDraw, Image
from time import time
from MorseCodePy import decode
from ..base import View
from ..button_interface import ButtonInterface
from ..list_select import ListItem, ListView
from .recipe_manager import RecipeManager
from .recipe import Recipe, Step, StepType
# Step types that accept a typed value
_TYPES_WITH_VALUE = {StepType.SECTION, StepType.WEIGH, StepType.WEIGH_WITH_TIMER}
# Step types whose value is numeric (use numbers language for morse)
_NUMERIC_TYPES = {StepType.WEIGH, StepType.WEIGH_WITH_TIMER}
def _make_type_item(step_type: StepType) -> ListItem:
"""ListItem showing a StepType icon + name."""
def render(draw, pos, fill, **kw):
step_type.render(draw, pos, fill=fill)
draw.text((pos[0] + 30, pos[1]), step_type.name, fill=fill, **kw)
return ListItem(render)
def _make_delete_item() -> ListItem:
"""ListItem that renders as a delete/trash label."""
return ListItem(
lambda draw, pos, fill, **kw: draw.text(pos, 'Delete', fill=fill, **kw)
)
class EditStep(View, ButtonInterface):
"""Edit a single recipe step (or the recipe name).
Flow for name editing (step_index == 0):
- Go straight to morse value entry.
Flow for step editing (step_index >= 1):
Phase 0 - select step type via ListView.
left_press : navigate up
right_press : navigate down
left_long_press : confirm type -> advance to phase 1, or save
immediately if the type carries no value
Phase 1 - enter value via morse.
left_press : cancel
left_long_press : save and exit
right_press : morse dot '.'
right_long_press : morse dash '-'
"""
def __init__(self, parent, im_size, center,
recipe_id: int,
step_index: int,
recipe_manager: RecipeManager,
deactivate_command):
self.deactivate_command = deactivate_command
self.recipe_manager = recipe_manager
self.recipe_id = recipe_id
self.step_index = step_index
recipe = recipe_manager.get_recipe(recipe_id)
if step_index == 0:
self.step = recipe.name # str -> name editing
else:
self.step = recipe.steps[step_index - 1] # Step
# edit_step: 0 = type selection | 1 = value entry | 2 = goal time entry
# Name editing starts directly in phase 1.
self.edit_step = 1 if step_index == 0 else 0
# Type selection list (used only when editing a Step)
self.type_list = ListView(x_offset=40, max_visible=5)
self.type_list.items = [_make_type_item(t) for t in StepType] + [_make_delete_item()]
if isinstance(self.step, Step):
all_types = list(StepType)
self.type_list.selected_index = all_types.index(self.step.step_type)
self.new_type = self.step.step_type
else:
self.new_type = None
self.new_value = ''
self.new_goal_time = ''
self.value_cursor = 0
self.value_cursor_pulse = 0.0
self.morse_buffer = ''
self.morse_code_language = self._morse_language_for(self.new_type)
self.last_input = None
self.letter_timeout = 2.0 # seconds
super().__init__(parent, im_size, center)
# ------------------------------------------------------------------
# helpers
# ------------------------------------------------------------------
@staticmethod
def _morse_language_for(step_type) -> str:
return 'numbers' if step_type in _NUMERIC_TYPES else 'english'
def _process_morse_timeout(self):
"""Decode pending morse buffer after the letter timeout."""
if self.last_input is not None:
if time() - self.last_input > self.letter_timeout:
if self.edit_step == 2:
if len(self.morse_buffer) < 10:
decoded = decode(self.morse_buffer, language=self.morse_code_language)
self.new_goal_time += decoded.upper()
self.value_cursor += 1
else:
self.new_goal_time = self.new_goal_time[:-1]
self.value_cursor = max(0, self.value_cursor - 1)
else:
if len(self.morse_buffer) < 10:
decoded = decode(self.morse_buffer, language=self.morse_code_language)
self.new_value += decoded.upper()
self.value_cursor += 1
else:
self.new_value = self.new_value[:-1]
self.value_cursor = max(0, self.value_cursor - 1)
self.last_input = None
self.morse_buffer = ''
def _render_value_entry(self, draw: ImageDraw.ImageDraw, x: int):
"""Render the morse value-entry screen."""
font_size = 16
y_start = 30
if isinstance(self.step, str):
label = "Name:"
elif self.edit_step == 2:
label = "Goal time(s):"
else:
label = f"{self.new_type.name}:"
draw.text((x, y_start), label, fill='black')
current_value = self.new_goal_time if self.edit_step == 2 else self.new_value
draw.text((x, y_start + 20), current_value, fill='black', font_size=font_size)
if self.value_cursor_pulse > 1.0:
draw.rectangle(
(x + self.value_cursor * 10, y_start + 18,
x + self.value_cursor * 10 + 8, y_start + 34),
fill='black'
)
self.value_cursor_pulse += 0.1
if self.value_cursor_pulse > 2.0:
self.value_cursor_pulse = 0.0
draw.line((x, y_start + 37, x + 80, y_start + 37), fill='black')
if self.morse_buffer:
letter = decode(self.morse_buffer, language=self.morse_code_language) \
if len(self.morse_buffer) < 10 else 'del'
draw.text((x, y_start + 50), f'{self.morse_buffer} -> {letter}',
fill='black', font_size=14)
def _render_type_selection(self, draw: ImageDraw.ImageDraw, x: int):
"""Render the step-type selection screen."""
draw.text((x, 10), "Step type:", fill='black')
self.type_list.render(draw, y_start=30)
# ------------------------------------------------------------------
# View
# ------------------------------------------------------------------
def update_weight(self, weight: float) -> Image.Image:
im = self.bkg_im.copy()
draw = ImageDraw.Draw(im)
x = 40
if isinstance(self.step, str) or self.edit_step >= 1:
self._process_morse_timeout()
self._render_value_entry(draw, x)
else:
self._render_type_selection(draw, x)
return im
# ------------------------------------------------------------------
# ButtonInterface
# ------------------------------------------------------------------
def left_press(self):
if isinstance(self.step, str) or self.edit_step >= 1:
self.deactivate_command()
else:
self.type_list.select_previous()
def left_long_press(self):
if isinstance(self.step, str):
if self.new_value:
recipe = self.recipe_manager.get_recipe(self.recipe_id)
recipe.name = self.new_value
self.deactivate_command()
elif self.edit_step == 0:
# always go back from type selection
self.deactivate_command()
elif self.edit_step == 1 and self.new_type == StepType.WEIGH_WITH_TIMER:
# Save weight value and advance to goal time entry
raw = self.new_value
if raw:
try:
self._saved_weight = int(raw)
except ValueError:
self._saved_weight = None
else:
self._saved_weight = None
self.edit_step = 2
self.morse_code_language = 'numbers'
self.new_goal_time = ''
self.value_cursor = 0
self.morse_buffer = ''
self.last_input = None
else:
if self.edit_step == 2:
raw_goal = self.new_goal_time
try:
goal_time = float(raw_goal) if raw_goal else 0.0
except ValueError:
goal_time = 0.0
value = self._saved_weight
else:
raw = self.new_value
if raw and self.new_type in _NUMERIC_TYPES:
try:
value = int(raw)
except ValueError:
value = None
elif raw:
value = raw
else:
value = None
goal_time = 0.0
recipe = self.recipe_manager.get_recipe(self.recipe_id)
step = recipe.steps[self.step_index - 1]
step.step_type = self.new_type
step.value = value
step.goal_time = goal_time
self.deactivate_command()
def right_press(self):
if isinstance(self.step, str) or self.edit_step >= 1:
self.last_input = time()
self.morse_buffer += '.'
else:
self.type_list.select_next()
def right_long_press(self):
if isinstance(self.step, str) or self.edit_step >= 1:
self.last_input = time()
self.morse_buffer += '-'
elif self.edit_step == 0:
all_types = list(StepType)
if self.type_list.selected_index == len(all_types):
# delete item selected
recipe = self.recipe_manager.get_recipe(self.recipe_id)
del recipe.steps[self.step_index - 1]
self.deactivate_command()
return
self.new_type = all_types[self.type_list.selected_index]
if self.new_type not in _TYPES_WITH_VALUE:
# No value needed -> save immediately
recipe = self.recipe_manager.get_recipe(self.recipe_id)
recipe.steps[self.step_index - 1].step_type = self.new_type
recipe.steps[self.step_index - 1].value = None
self.deactivate_command()
else:
# Advance to value entry
self.morse_code_language = self._morse_language_for(self.new_type)
self.new_value = ''
self.value_cursor = 0
self.morse_buffer = ''
self.last_input = None
self.edit_step = 1
def has_button(self) -> Tuple[bool, bool, bool, bool]:
return True, True, True, True
# ------------------------------------------------------------------
# Button hint rendering
# ------------------------------------------------------------------
def render_left_press(self, draw, x, y):
if isinstance(self.step, str) or self.edit_step >= 1:
draw.text((x, y - 5), 'Cancel', fill='black')
else:
draw.regular_polygon((x, y + 2, 5), 3, fill='black')
def render_left_long_press(self, draw, x, y):
if self.edit_step == 0 and not isinstance(self.step, str):
draw.text((x, y - 5), 'Back', fill='black')
elif self.edit_step == 1 and self.new_type == StepType.WEIGH_WITH_TIMER:
draw.text((x, y - 5), 'Next', fill='black')
else:
draw.text((x, y - 5), 'Save', fill='black')
def render_right_press(self, draw, x, y):
if isinstance(self.step, str) or self.edit_step >= 1:
draw.text((x - 5, y - 5), '.', fill='black')
else:
draw.regular_polygon((x, y + 4, 5), 3, fill='black', rotation=180)
def render_right_long_press(self, draw, x, y):
if isinstance(self.step, str) or self.edit_step >= 1:
draw.text((x - 5, y - 5), '-', fill='black')
elif self.edit_step == 0:
all_types = list(StepType)
if self.type_list.selected_index == len(all_types):
draw.text((x - 20, y - 5), 'Delete', fill='black')
else:
draw.text((x - 20, y - 5), 'Select', fill='black')