Animations are mainly on the typescript side, not kotlin

This commit is contained in:
James Babcock committed 2026-09-23 22:05:06 -07:00
1 parent 063d8a0b03
commit 440674b261
26 files changed
+783 -128

No files matched your search

+22 -28
View File
@@ -1,5 +1,5 @@
import type { PlacedImage } from './image'
import type { MenuHighlightAnimation } from '../ui/menu-highlight-motion'
import { DISPLAY_LIST_RECORD, DrawOp, SCREEN, encodeDisplayList, readDisplayList, paintDisplayList, type DisplayList } from './display-list'
/** TS/native bridge tags, not firmware draw opcodes. Keep in sync with DrawRecordKind.kt. */
export const DrawRecordKind = {
@@ -9,38 +9,36 @@ export const DrawRecordKind = {
MENU_SELECTION: 3,
TRANSPARENT_IMAGE: 4,
MASKED_IMAGE: 5,
ANIMATED_MENU_SELECTION: 6,
DISPLAY_LIST: DISPLAY_LIST_RECORD,
} as const
export function isPresentationKind(kind: number | undefined): boolean {
return kind === DrawRecordKind.MENU_SELECTION || kind === DrawRecordKind.TRANSPARENT_IMAGE ||
kind === DrawRecordKind.MASKED_IMAGE || kind === DrawRecordKind.ANIMATED_MENU_SELECTION
kind === DrawRecordKind.MASKED_IMAGE || kind === DrawRecordKind.DISPLAY_LIST
}
/** Presentation record: tag, signed xy, u16 wh/radius, gray fill/stroke, signed depth, restore-count, optional animation, gray pixels, restore rectangles. */
/** Presentation record: tag, signed xy, u16 wh/radius, gray fill/stroke, signed depth, restore-count, gray pixels, restore rectangles. */
export function encodePresentation(draw: PlacedImage): Uint8Array {
const p = draw.presentation!
const { width, height, pixels } = draw.source
if (width < 1 || height < 1 || width > 640 || height > 480 || 5 + Math.ceil(width / 2) * height > 65536) throw new Error('Presentation image exceeds the resource size limit')
if (width < 1 || height < 1 || width > 640 || height > 480 || !p.displayList && 5 + Math.ceil(width / 2) * height > 65536) throw new Error('Presentation image exceeds the resource size limit')
if (!Number.isInteger(p.depth) || p.depth < -128 || p.depth > 127) throw new Error('Invalid menu depth')
if ((p.occlusions?.length ?? 0) > 2048) throw new Error('Too many menu occlusion rectangles')
const animation = p.animation;
if (animation && (!Number.isInteger(animation.durationMs) || animation.durationMs < 1 || animation.durationMs > 65535)) throw new Error('Invalid menu animation duration')
const header = animation ? 28 : 16;
if (p.displayList) {
const calls = [...p.displayList.calls, ...(p.occlusions ?? []).map(r => ({
op: DrawOp.RECT_COPY, resource: SCREEN, x: r.x - draw.x, y: r.y - draw.y,
width: r.width, height: r.height, dx: r.x - draw.x, dy: r.y - draw.y, depth: -p.depth,
}))];
return encodeDisplayList({ displayList: { ...p.displayList, calls }, x: draw.x, y: draw.y, width, height, depth: p.depth });
}
const header = 16;
const bytes = new Uint8Array(header + width * height + (p.occlusions?.length ?? 0) * 8), view = new DataView(bytes.buffer)
bytes[0] = animation ? DrawRecordKind.ANIMATED_MENU_SELECTION
: p.mode === "image" ? DrawRecordKind.TRANSPARENT_IMAGE
bytes[0] = p.mode === "image" ? DrawRecordKind.TRANSPARENT_IMAGE
: p.mode === "masked-image" ? DrawRecordKind.MASKED_IMAGE : DrawRecordKind.MENU_SELECTION
view.setInt16(1, draw.x, true); view.setInt16(3, draw.y, true)
view.setUint16(5, width, true); view.setUint16(7, height, true); view.setUint16(9, p.radius, true)
bytes[11] = p.background; bytes[12] = p.border; view.setInt8(13, p.depth)
view.setUint16(14, p.occlusions?.length ?? 0, true)
if (animation) {
view.setInt16(16, animation.dx, true); view.setInt16(18, animation.dy, true);
view.setUint16(20, Math.max(0, Math.min(animation.durationMs, Date.now() - animation.startedAt)), true);
view.setUint32(22, animation.token, true);
view.setUint16(26, animation.durationMs, true);
}
bytes.set(pixels, header)
let offset = header + pixels.length
for (const rect of p.occlusions ?? []) {
@@ -50,20 +48,18 @@ export function encodePresentation(draw: PlacedImage): Uint8Array {
return bytes
}
export type Selection = { animation?: MenuHighlightAnimation; mode?: "image" | "masked-image"; x: number; y: number; width: number; height: number; radius: number; background: number; border: number; depth: number; pixels: Uint8Array; occlusions: { x: number; y: number; width: number; height: number }[] }
export type Selection = { displayList?: DisplayList; mode?: "image" | "masked-image"; x: number; y: number; width: number; height: number; radius: number; background: number; border: number; depth: number; pixels: Uint8Array; occlusions: { x: number; y: number; width: number; height: number }[] }
export function readPresentation(bytes: Uint8Array, offset: number): { selection: Selection; end: number } {
if (bytes[offset] === DrawRecordKind.DISPLAY_LIST) {
const { placed, end } = readDisplayList(bytes, offset);
return { selection: { ...placed, radius: 0, background: 0, border: 0, pixels: new Uint8Array(0), occlusions: [] }, end };
}
const v = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength), w = v.getUint16(offset + 5, true), h = v.getUint16(offset + 7, true)
const kind = bytes[offset]
const header = kind === DrawRecordKind.ANIMATED_MENU_SELECTION ? 28 : 16
const header = 16
const selection: Selection = { mode: kind === DrawRecordKind.TRANSPARENT_IMAGE ? "image" : kind === DrawRecordKind.MASKED_IMAGE ? "masked-image" : undefined, x: v.getInt16(offset+1,true), y: v.getInt16(offset+3,true), width:w, height:h,
radius:v.getUint16(offset+9,true), background:bytes[offset+11]!, border:bytes[offset+12]!, depth:v.getInt8(offset+13),
pixels:bytes.slice(offset+header,offset+header+w*h), occlusions:[] }
if (kind === DrawRecordKind.ANIMATED_MENU_SELECTION) {
const durationMs = v.getUint16(offset+26,true), elapsed = v.getUint16(offset+20,true)
if (!durationMs || elapsed > durationMs) throw new Error('Invalid menu animation timing')
selection.animation = { dx:v.getInt16(offset+16,true), dy:v.getInt16(offset+18,true),
startedAt:Date.now()-elapsed, token:v.getUint32(offset+22,true), durationMs }
}
let end=offset+header+w*h
for(let i=0;i<v.getUint16(offset+14,true);i++) { selection.occlusions.push({x:v.getUint16(end,true),y:v.getUint16(end+2,true),width:v.getUint16(end+4,true),height:v.getUint16(end+6,true)});end+=8 }
return {selection,end}
@@ -81,6 +77,7 @@ export function presentationRecords(buffer: ArrayBuffer | null): Selection[] {
return result
}
export function paintPresentation(output: Uint8Array, screen: Uint8Array, width: number, height: number, row: Selection, right = false): void {
if (row.displayList) { paintDisplayList(output, screen, width, height, { ...row, displayList: row.displayList }, right); return; }
const q=(n:number)=>Math.min(15,(n+8)>>4), shift=right?-Math.floor((row.depth+1)/2):Math.floor(row.depth/2)
const inside=(x:number,y:number,w:number,h:number,r:number)=>{
if(x<0||y<0||x>=w||y>=h) return false
@@ -88,10 +85,7 @@ export function paintPresentation(output: Uint8Array, screen: Uint8Array, width:
const dx=Math.max(0,2*r-(2*Math.min(x,w-1-x)+1)),dy=Math.max(0,2*r-(2*Math.min(y,h-1-y)+1))
return dx*dx+dy*dy<=4*r*r
}
const t = row.animation ? Math.max(0, Math.min(1, (Date.now() - row.animation.startedAt) / row.animation.durationMs)) : 1
const remaining = 1 - t * t * (3 - 2 * t)
const highlightX = Math.trunc(row.x + (row.animation?.dx ?? 0) * remaining)
const highlightY = Math.trunc(row.y + (row.animation?.dy ?? 0) * remaining)
const highlightX = row.x, highlightY = row.y
if (!row.mode) for(let yy=0;yy<row.height;yy++) for(let xx=0;xx<row.width;xx++) {
const x=highlightX+xx+shift,y=highlightY+yy
if(x<0||y<0||x>=width||y>=height || !inside(xx,yy,row.width,row.height,row.radius)) continue