This commit is contained in:
jeremygan2021
2026-03-04 20:26:06 +08:00
parent 87af3b346f
commit 1e9354fd6f
3 changed files with 210 additions and 187 deletions

View File

@@ -211,7 +211,37 @@ class Display:
self.tft.line(x, y + 5, x + 3, y + 8, st7789.GREEN)
self.tft.line(x + 3, y + 8, x + 10, y, st7789.GREEN)
def render_confirm_screen(self, asr_text=""):
def measure_text(self, text):
"""计算文本宽度"""
width = 0
for char in text:
if ord(char) > 127:
width += 16
else:
width += 8
return width
def draw_centered_text(self, text, x, y, w, h, color, bg=None):
"""在指定区域居中显示文本"""
if not self.tft: return
text_width = self.measure_text(text)
start_x = x + (w - text_width) // 2
start_y = y + (h - 16) // 2
# 确保不超出边界
start_x = max(x, start_x)
if bg is not None:
self.tft.fill_rect(x, y, w, h, bg)
self.text(text, start_x, start_y, color)
def draw_button(self, text, x, y, w, h, bg_color, text_color=st7789.WHITE):
"""绘制带居中文字的按钮"""
if not self.tft: return
self.tft.fill_rect(x, y, w, h, bg_color)
self.draw_centered_text(text, x, y, w, h, text_color)
def render_confirm_screen(self, asr_text="", waiting=False):
"""渲染确认界面"""
if not self.tft:
return
@@ -220,12 +250,14 @@ class Display:
# Header
self.tft.fill_rect(0, 0, 240, 30, st7789.CYAN)
self.text("说完了吗?", 75, 8, st7789.BLACK)
self.draw_centered_text("说完了吗?", 0, 0, 240, 30, st7789.BLACK)
# Content box
self.tft.fill_rect(10, 50, 220, 90, 0x4208) # DARKGREY
if asr_text:
if waiting:
self.draw_centered_text("正在识别...", 10, 50, 220, 90, st7789.YELLOW)
elif asr_text:
# 自动换行逻辑
max_width = 200
lines = []
@@ -254,18 +286,119 @@ class Display:
for i, line in enumerate(lines):
# 计算水平居中
line_width = 0
for c in line:
line_width += 16 if ord(c) > 127 else 8
line_width = self.measure_text(line)
center_x = 20 + (200 - line_width) // 2
self.text(line, center_x, start_y + i * 20, st7789.WHITE, wait=False)
else:
self.text("未识别到文字", 70, 85, st7789.WHITE)
self.draw_centered_text("未识别到文字", 10, 50, 220, 90, st7789.WHITE)
# Buttons
self.tft.fill_rect(20, 160, 90, 30, st7789.GREEN)
self.text("短按确认", 30, 168, st7789.BLACK)
self.draw_button("短按确认", 20, 160, 90, 30, st7789.GREEN, st7789.BLACK)
self.draw_button("长按重录", 130, 160, 90, 30, st7789.RED, st7789.WHITE)
def render_recording_screen(self, asr_text="", audio_level=0, is_recording=False):
"""渲染录音界面"""
if not self.tft:
return
self.tft.fill_rect(130, 160, 90, 30, st7789.RED)
self.text("长按重录", 140, 168, st7789.WHITE)
self.tft.fill(st7789.BLACK)
self.tft.fill_rect(0, 0, 240, 30, st7789.WHITE)
self.draw_centered_text("语音识别", 0, 0, 240, 30, st7789.BLACK)
self.draw_mic_icon(105, 50)
if audio_level > 0:
bar_width = min(int(audio_level * 2), 200)
self.tft.fill_rect(20, 100, bar_width, 10, st7789.GREEN)
if asr_text:
self.text(asr_text[:20], 20, 130, st7789.WHITE, wait=False)
if is_recording:
self.draw_button("松开停止", 60, 200, 120, 25, st7789.RED, st7789.WHITE)
else:
self.draw_button("长按录音", 60, 200, 120, 25, st7789.BLUE, st7789.WHITE)
def render_result_screen(self, status="", prompt="", image_received=False):
"""渲染结果界面"""
if not self.tft:
return
if status == "OPTIMIZING":
self.tft.fill(st7789.BLACK)
self.tft.fill_rect(0, 0, 240, 30, st7789.WHITE)
self.draw_centered_text("AI 生成中", 0, 0, 240, 30, st7789.BLACK)
self.draw_centered_text("正在思考...", 0, 60, 240, 20, st7789.CYAN)
self.draw_centered_text("优化提示词中", 0, 80, 240, 20, st7789.CYAN)
self.draw_progress_bar(40, 110, 160, 6, 0.3, st7789.CYAN)
elif status == "RENDERING":
self.tft.fill(st7789.BLACK)
self.tft.fill_rect(0, 0, 240, 30, st7789.WHITE)
self.draw_centered_text("AI 生成中", 0, 0, 240, 30, st7789.BLACK)
self.draw_centered_text("正在绘画...", 0, 60, 240, 20, st7789.YELLOW)
self.draw_centered_text("AI作画中", 0, 80, 240, 20, st7789.YELLOW)
self.draw_progress_bar(40, 110, 160, 6, 0.7, st7789.YELLOW)
elif status == "COMPLETE" or image_received:
# Don't clear screen, image is already there
self.tft.fill_rect(230, 230, 10, 10, st7789.GREEN)
elif status == "ERROR":
self.tft.fill(st7789.BLACK)
self.tft.fill_rect(0, 0, 240, 30, st7789.WHITE)
self.draw_centered_text("AI 生成中", 0, 0, 240, 30, st7789.BLACK)
self.draw_centered_text("生成失败", 0, 50, 240, 20, st7789.RED)
if prompt and not image_received:
self.tft.fill_rect(10, 140, 220, 50, 0x2124) # Dark Grey
self.text("提示词:", 15, 145, st7789.CYAN)
self.text(prompt[:25] + "..." if len(prompt) > 25 else prompt, 15, 165, st7789.WHITE)
if not image_received:
self.draw_button("长按返回", 60, 210, 120, 25, st7789.BLUE, st7789.WHITE)
def draw_loading_spinner(self, x, y, angle, color=st7789.WHITE):
"""绘制旋转加载图标"""
if not self.tft:
return
import math
rad = math.radians(angle)
center_x = x + 10
center_y = y + 10
radius = 8
for i in range(8):
theta = math.radians(i * 45) + rad
px = int(center_x + radius * math.cos(theta))
py = int(center_y + radius * math.sin(theta))
self.tft.pixel(px, py, color)
def draw_progress_bar(self, x, y, width, height, progress, color=st7789.CYAN):
"""绘制进度条"""
if not self.tft:
return
self.tft.fill_rect(x, y, width, height, 0x4208) # DARKGREY
if progress > 0:
bar_width = int(width * min(progress, 1.0))
self.tft.fill_rect(x, y, bar_width, height, color)
def draw_mic_icon(self, x, y, active=True):
"""绘制麦克风图标"""
if not self.tft:
return
color = st7789.GREEN if active else 0x4208 # DARKGREY
self.tft.fill_rect(x + 5, y, 10, 5, color)
self.tft.fill_rect(x + 3, y + 5, 14, 10, color)
self.tft.fill_rect(x + 8, y + 15, 4, 8, color)
self.tft.fill_rect(x + 6, y + 23, 8, 2, color)
self.tft.fill_rect(x + 8, y + 25, 4, 3, color)