141 lines
4.8 KiB
Python
141 lines
4.8 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 .recipe_manager import RecipeManager
|
|
from .recipe import Recipe, Step, StepType
|
|
|
|
class EditStep(View, ButtonInterface):
|
|
def __init__(self, parent, im_size, center,
|
|
recipe: Recipe,
|
|
step_index: int,
|
|
recipe_manager: RecipeManager,
|
|
deactivate_command):
|
|
self.deactivate_command = deactivate_command
|
|
self.recipe_manager = recipe_manager
|
|
self.recipe = recipe
|
|
self.step_index = step_index
|
|
if step_index == 0:
|
|
self.step = recipe.name
|
|
|
|
self.confirm_view = False
|
|
self.edit_step = 0 # 0: type, 1: step/name value
|
|
self.new_type = ''
|
|
self.new_value = ''
|
|
self.value_cursor = 0
|
|
self.value_cursor_pulse = 0.0
|
|
self.morse_buffer = ''
|
|
self.morse_code_language = 'english'
|
|
if isinstance(self.step, Step) and \
|
|
self.step.step_type in [StepType.START_TIME, StepType.WEIGH]:
|
|
self.morse_code_language = 'numbers'
|
|
|
|
self.last_input = None
|
|
self.letter_timeout = 2.0 # seconds
|
|
|
|
super().__init__(parent, im_size, center)
|
|
|
|
def update_weight(self, weight: float) -> Image.Image:
|
|
im = self.bkg_im.copy()
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
x = 40
|
|
|
|
if self.last_input is not None:
|
|
if time() - self.last_input > self.letter_timeout:
|
|
if len(self.morse_buffer) < 10:
|
|
self.value_cursor += 1
|
|
self.new_value += decode(self.morse_buffer, language=self.morse_code_language).upper()
|
|
else:
|
|
self.value_cursor -= 1
|
|
self.new_value = self.new_value[:-1]
|
|
# process morse buffer
|
|
self.last_input = None
|
|
self.morse_buffer = ''
|
|
|
|
if isinstance(self.step, str):
|
|
draw.text((x, 10), "Name:", fill='black')
|
|
draw.text((x, 30), self.new_value, fill='black')
|
|
if self.value_cursor_pulse > 1.0:
|
|
draw.rectangle((x + self.value_cursor * 8, 28, x + self.value_cursor * 8 + 8, 40), fill='black')
|
|
if self.value_cursor_pulse > 2.0:
|
|
self.value_cursor_pulse = 0.0
|
|
self.value_cursor_pulse += 0.1
|
|
|
|
draw.line((x, 45, x + 80, 45), fill='black')
|
|
elif self.edit_step == 0:
|
|
pass
|
|
else:
|
|
pass
|
|
|
|
# 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) == '+':
|
|
# draw.text((x, y_pos - 5), '+', fill='white')
|
|
# elif str(step) == 'BACK':
|
|
# draw.regular_polygon((x + 5, y_pos, 5), 3, fill='white', rotation=90)
|
|
# else:
|
|
# step.step_type.render(draw, (x, y_pos - 5), fill='white')
|
|
# draw.text((x + 30, y_pos - 5), step.value_str, fill='white')
|
|
|
|
# elif str(step) == '+':
|
|
# draw.text((x, y_pos - 5), '+', fill='black')
|
|
# elif str(step) == 'BACK':
|
|
# draw.regular_polygon((x + 5, y_pos, 5), 3, fill='black', rotation=90)
|
|
# 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):
|
|
self.selected_field = (self.selected_field - 1) % (len(self.recipe.steps) + 3)
|
|
|
|
def left_long_press(self):
|
|
if self.selected_field == len(self.recipe.steps) + 2:
|
|
# back
|
|
self.deactivate_command()
|
|
elif self.selected_field == len(self.recipe.steps) + 1:
|
|
# add step
|
|
pass
|
|
elif self.selected_field == 0:
|
|
# edit name
|
|
pass
|
|
else:
|
|
# edit entry
|
|
pass
|
|
|
|
def right_press(self):
|
|
self.last_input = time()
|
|
self.morse_buffer += '.'
|
|
|
|
def right_long_press(self):
|
|
self.last_input = time()
|
|
self.morse_buffer += '-'
|
|
|
|
def has_button(self) -> Tuple[bool, bool, bool, bool]:
|
|
return True, True, True, False
|
|
|
|
|
|
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), 'Next', fill='black')
|
|
|
|
def render_right_press(self, draw, x, y):
|
|
draw.text((x - 30, y), 'Morse', fill='black') |