import { DrawExpression as E } from "../graphics/draw-expression"; import { DrawOp, type ListCall } from "../graphics/display-list"; import { GrayImage, type UiFont } from "../graphics/image"; import { menuAnimationDurationMs } from "./menu-animation-pref"; export type MenuLabelPainter = (font: UiFont, x: number, y: number, width: number, text: string, value: number) => void; const contexts = new WeakMap(); let nextToken = 0x40000000; /** Shared row builders inherit their menu's focus and timeline without changing their public signature. */ export function drawMenuLabel(image: GrayImage, font: UiFont, x: number, y: number, width: number, text: string, value: number): void { const context = contexts.get(image); if (context) context.motion.draw(image, font, x, y, width, text, value, context.index, context.active); else drawStatic(image, font, x, y, width, text, value); } function drawStatic(image: GrayImage, font: UiFont, x: number, y: number, width: number, text: string, value: number): void { if (width <= 0) return; if (font.measureText(text) <= width) image.drawText(font, x, y, text, value); else { const clip = new GrayImage(width, font.lineHeight); clip.drawText(font, 0, 0, text, value); image.bitBlt(clip.withDrawsBaked(), x, y, { transparentZero: true }); } } /** One bounded, firmware-driven reading pass per selection/content/geometry/focus change. No JS timers. */ export class MenuLabelMotion { private key = ""; private timeline = { token: 0, startedAt: 0 }; row(image: GrayImage, index: number, active: boolean, draw: (drawLabel: MenuLabelPainter) => void): void { contexts.set(image, { motion: this, index, active }); try { draw((font, x, y, width, text, value) => drawMenuLabel(image, font, x, y, width, text, value)); } finally { contexts.delete(image); } } reset(): void { this.key = ""; } draw(image: GrayImage, font: UiFont, x: number, y: number, width: number, text: string, value: number, index: number, active: boolean): void { width = Math.max(0, Math.floor(width)); const measured = Math.ceil(font.measureText(text)); drawStatic(image, font, x, y, width, text, value); if (!active) return; const travel = menuAnimationDurationMs(Math.max(600, Math.ceil((measured - width) * 1000 / 30))); if (!width || measured <= width || !travel || font.lineHeight > 480) { this.reset(); return; } // Bounded tiles, never a label-wide BLE image. Extreme labels use the static fallback. const tileWidth = Math.min(640, Math.floor(65530 / font.lineHeight) * 2); if (tileWidth <= 0 || Math.ceil(measured / tileWidth) > 32) return; const key = JSON.stringify([index, text, width, font.lineHeight, measured, x, y, value, travel]); if (this.key !== key) { this.key = key; this.timeline = { token: nextToken++, startedAt: Date.now() }; } const distance = measured - width; // Hold the beginning, traverse at reading speed, hold the end, then return and stop. const offset = E.progress(travel, 900).mul(E.f32(distance)) .sub(E.progress(travel, 1800 + travel).mul(E.f32(distance))).toInt(); const resources: GrayImage[] = []; const calls: ListCall[] = []; for (let left = 0; left < measured; left += tileWidth) { const tile = new GrayImage(Math.min(tileWidth, measured - left), font.lineHeight, 16); tile.drawText(font, -left, 0, text, value); resources.push(tile.withDrawsBaked()); calls.push({ op: DrawOp.RECT_COPY, resource: resources.length - 1, x: 0, y: 0, width: tile.width, height: tile.height, dx: E.i32(left).sub(offset), dy: 0, clip: { x: 0, y: 0, width, height: font.lineHeight } }); } image.drawDisplayList({ resources, calls, timeline: this.timeline }, x, y, width, font.lineHeight); } }