152 lines
5.6 KiB
Python
152 lines
5.6 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_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
|
|
if step_index == 0:
|
|
self.step = recipe_manager.get_recipe(recipe_id).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.value_cursor = max(0, self.value_cursor)
|
|
self.new_value = self.new_value[:-1]
|
|
# process morse buffer
|
|
self.last_input = None
|
|
self.morse_buffer = ''
|
|
|
|
if isinstance(self.step, str):
|
|
font_size = 16
|
|
y_start = 30
|
|
draw.text((x, y_start), "Name:", fill='black')
|
|
draw.text((x, y_start + 20), self.new_value, fill='black', font_size=font_size)
|
|
if self.value_cursor_pulse > 1.0:
|
|
draw.rectangle((x + self.value_cursor * 10,
|
|
y_start + 20 - 2,
|
|
x + self.value_cursor * 10 + 8,
|
|
y_start + 34),
|
|
fill='black')
|
|
if self.value_cursor_pulse > 2.0:
|
|
self.value_cursor_pulse = 0.0
|
|
self.value_cursor_pulse += 0.1
|
|
|
|
draw.line((x, y_start + 37, x + 80, y_start + 37), fill='black')
|
|
|
|
if self.morse_buffer != '':
|
|
for i, suffix in enumerate(['']):#, '.', '-', '.' * 10]):
|
|
letter = 'del'
|
|
if len(suffix) != 10 and len(self.morse_buffer) + len(suffix) < 10:
|
|
letter = decode(self.morse_buffer + suffix, language=self.morse_code_language)
|
|
draw.text((x, y_start + 50 + i * 15),
|
|
f'{self.morse_buffer + suffix} {letter}',
|
|
fill='black', font_size=14)
|
|
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.deactivate_command()
|
|
|
|
def left_long_press(self):
|
|
if self.step_index == 0:
|
|
# editing name
|
|
if self.new_value:
|
|
recipe = self.recipe_manager.get_recipe(self.recipe_id)
|
|
recipe.name = self.new_value
|
|
self.deactivate_command()
|
|
else:
|
|
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.text((x, y-5), 'Cancel', 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') |