Animate scrolling in menus

This commit is contained in:
James Babcock committed 2026-09-24 11:04:10 -07:00
1 parent 915f155819
commit c801bd5185
23 files changed
+514 -91

No files matched your search

+13 -8
View File
@@ -32,10 +32,12 @@ Supported calls in this first pass:
- `IMAGE`: a local resource index, integer `x`/`y`, and `transparent`. Transparent
images skip pixels quantized to zero. Resources have `width`, `height`, and
gray8 `pixels`; a baked `GrayImage` can be used directly.
- `RECT_COPY`: a local resource index, integer source rectangle and destination
`dx`/`dy`. `SCREEN` reads the uncomposed frame; its source coordinates are also
relative to this list's placement. Each call may add its own `depth` to the
list's depth. A screen restore normally uses the negative of the list depth.
- `RECT_COPY`: a local resource index, source rectangle and destination
`dx`/`dy`. Source x/y and `dx`/`dy` accept expressions (revision 29); the
source rectangle must stay inside the source at every time, or firmware
rejects the list. `SCREEN` reads the uncomposed frame; its source coordinates
are also relative to this list's placement. Each call may add its own `depth`
to the list's depth. A screen restore normally uses the negative of the list depth.
Kotlin allocates/cache-pins all image resources, resolves the local indices, applies
surface placement, and includes the calls in the root firmware display list. The
@@ -48,7 +50,9 @@ the current firmware protocol; no firmware update is needed for this bridge chan
integer remainder, negation, explicit conversions, lerp, and the easing functions
in `ExprOp`. `ease()` defaults to smoothstep. Integers wrap at 32 bits; float
operations round to f32. The firmware's existing stack/error semantics apply.
Only rounded-rectangle x/y accept expressions in this first pass.
Rounded-rectangle x/y and rect-copy source/destination coordinates accept
expressions. `E.progress(duration, delay)` stays 0 for the first `delay` ms of
the timeline, so one list can run motions that started at different times.
`maximum.time()` returns `min(milliseconds since PRESENT, maximum)`. The existing
45 ms repaint timer remains active while this is below maximum. `E.elapsed()` is
@@ -64,7 +68,8 @@ exclude the changing elapsed duration. Without a timeline, raw `time()` expressi
start at each PRESENT. Native compilation strips the bridge-only elapsed opcode.
`menu-selection-list.ts` authors both the rounded highlight and the transparent row
image using this API. Navigation eligibility, duration, and easing remain in
image using this API. `menu-scroll-list.ts` authors a menu scroll: a viewport-sized
copy from a strip resource whose source y animates, then the highlight. Navigation eligibility, duration, and easing remain in
TypeScript. There is no animated-menu tag or menu animation policy in Kotlin.
## Bridge framing
@@ -73,8 +78,8 @@ Tag 7 is followed by a little-endian u32 byte length and a bounded body:
placement (i16 x/y, u16 width/height, i8 depth), timeline (u32 token/elapsed),
u16 resource count, gray8 images (u16 width/height followed by pixels), u16 call
count, then calls. Calls start with the firmware opcode and an i16 relative depth;
operands otherwise use their fixed-width fields, except rounded x/y which use
extended varints. Expressions use the firmware bytecode plus bridge-only
operands otherwise use their fixed-width fields, except rounded x/y and rect-copy
x/y/dx/dy which use extended varints. Expressions use the firmware bytecode plus bridge-only
`ELAPSED = 128`. Resource indices are local to this record, never firmware IDs.
Rect-copy source x/y are signed here to permit relative SCREEN coordinates.
+8 -6
View File
@@ -8,7 +8,7 @@ export type ListImage = { readonly width: number; readonly height: number; reado
export type ListCall = (
| { op: typeof DrawOp.ROUNDED_RECT; x: DrawValue; y: DrawValue; width: number; height: number; radius: number; background: number; border: number }
| { op: typeof DrawOp.IMAGE; resource: number; x: number; y: number; transparent: boolean }
| { op: typeof DrawOp.RECT_COPY; resource: number; x: number; y: number; width: number; height: number; dx: number; dy: number }
| { op: typeof DrawOp.RECT_COPY; resource: number; x: DrawValue; y: DrawValue; width: number; height: number; dx: DrawValue; dy: DrawValue }
) & { depth?: number };
export type DisplayList = {
readonly resources: readonly ListImage[];
@@ -53,12 +53,13 @@ export function encodeDisplayList(placed: PlacedDisplayList, now = Date.now()):
out.u8(integer(call.background, 0, 15)); out.u8(integer(call.border, 0, 16));
} else {
if (call.resource !== SCREEN) integer(call.resource, 0, list.resources.length - 1);
if (call.op === DrawOp.RECT_COPY && call.resource !== SCREEN && (call.x < 0 || call.y < 0)) throw new Error('Negative image source coordinate');
if (call.op === DrawOp.RECT_COPY && call.resource !== SCREEN && [call.x, call.y].some(v => typeof v === 'number' && v < 0)) throw new Error('Negative image source coordinate');
if (call.op === DrawOp.IMAGE && call.resource === SCREEN) throw new Error('Use rectCopy to read SCREEN');
out.u16(call.resource);
if (call.op === DrawOp.IMAGE) { out.i16(call.x); out.i16(call.y); out.u8(call.transparent ? 31 : 15); }
else {
out.i16(call.x); out.i16(call.y); out.u16(integer(call.width, 1, 640)); out.u16(integer(call.height, 1, 480)); out.i16(call.dx); out.i16(call.dy);
out.raw(encodeValue(call.x)); out.raw(encodeValue(call.y)); out.u16(integer(call.width, 1, 640)); out.u16(integer(call.height, 1, 480));
out.raw(encodeValue(call.dx)); out.raw(encodeValue(call.dy));
}
}
}
@@ -81,7 +82,7 @@ export function readDisplayList(bytes: Uint8Array, offset: number, now = Date.no
const op = r.u8(), depth = r.i16();
if (op === DrawOp.ROUNDED_RECT) calls.push({ op, depth, x: readValue(r), y: readValue(r), width: r.u16(), height: r.u16(), radius: r.u16(), background: r.u8(), border: r.u8() });
else if (op === DrawOp.IMAGE) calls.push({ op, depth, resource: r.u16(), x: r.i16(), y: r.i16(), transparent: (r.u8() & 16) !== 0 });
else if (op === DrawOp.RECT_COPY) calls.push({ op, depth, resource: r.u16(), x: r.i16(), y: r.i16(), width: r.u16(), height: r.u16(), dx: r.i16(), dy: r.i16() });
else if (op === DrawOp.RECT_COPY) calls.push({ op, depth, resource: r.u16(), x: readValue(r), y: readValue(r), width: r.u16(), height: r.u16(), dx: readValue(r), dy: readValue(r) });
else throw new Error('Unsupported display-list call');
}
if (r.offset !== length) throw new Error('Trailing display-list bytes');
@@ -121,8 +122,9 @@ export function paintDisplayList(output: Uint8Array, screen: Uint8Array, width:
} else {
const source = c.resource === SCREEN ? { width, height, pixels: screen } : placed.displayList.resources[c.resource];
const w = c.op === DrawOp.IMAGE ? source.width : c.width, h = c.op === DrawOp.IMAGE ? source.height : c.height;
const sx = c.op === DrawOp.IMAGE ? 0 : c.x + (c.resource === SCREEN ? placed.x : 0), sy = c.op === DrawOp.IMAGE ? 0 : c.y + (c.resource === SCREEN ? placed.y : 0);
const x = placed.x + (c.op === DrawOp.IMAGE ? c.x : c.dx) + shift, y = placed.y + (c.op === DrawOp.IMAGE ? c.y : c.dy);
const at = (v: DrawValue) => evaluate(v, elapsed, presentTime).value;
const sx = c.op === DrawOp.IMAGE ? 0 : at(c.x) + (c.resource === SCREEN ? placed.x : 0), sy = c.op === DrawOp.IMAGE ? 0 : at(c.y) + (c.resource === SCREEN ? placed.y : 0);
const x = placed.x + (c.op === DrawOp.IMAGE ? c.x : at(c.dx)) + shift, y = placed.y + (c.op === DrawOp.IMAGE ? c.y : at(c.dy));
for (let yy = 0; yy < h; yy++) for (let xx = 0; xx < w; xx++) {
if (x + xx < 0 || y + yy < 0 || x + xx >= width || y + yy >= height || sx + xx < 0 || sy + yy < 0 || sx + xx >= source.width || sy + yy >= source.height) continue;
const v = q(source.pixels[(sy + yy) * source.width + sx + xx]);
+10 -5
View File
@@ -59,12 +59,17 @@ export class DrawExpression<T extends 'i32' | 'f32'> {
}
/** Time already elapsed on this list's timeline when the next PRESENT happens. */
static elapsed(): DrawExpression<'i32'> { return new DrawExpression('i32', [ExprOp.ELAPSED]); }
/** A 0..1 timeline fraction that survives subsequent PRESENT clock resets. */
static progress(durationMs: number): DrawExpression<'f32'> {
/**
* A 0..1 timeline fraction that survives subsequent PRESENT clock resets.
* With a delay, it stays 0 for the first delayMs of the timeline.
*/
static progress(durationMs: number, delayMs = 0): DrawExpression<'f32'> {
const duration = DrawExpression.i32(integer(durationMs, 1, 2147483647));
const elapsed = DrawExpression.elapsed().min(duration);
return duration.sub(elapsed).max(DrawExpression.i32(0)).time()
.add(elapsed).toFloat().div(duration.toFloat());
const end = DrawExpression.i32(integer(delayMs + durationMs, 1, 2147483647));
const elapsed = DrawExpression.elapsed().min(end);
let total = end.sub(elapsed).max(DrawExpression.i32(0)).time().add(elapsed);
if (delayMs) total = total.sub(DrawExpression.i32(delayMs)).max(DrawExpression.i32(0));
return total.toFloat().div(duration.toFloat());
}
static decode(code: Uint8Array): DrawExpression<'i32'> { return new DrawExpression('i32', Array.from(code)); }
private binary(other: DrawExpression<T>, i: number, f: number): DrawExpression<T> {
+46
View File
@@ -0,0 +1,46 @@
import type { MenuHighlightAnimation } from '../ui/menu-highlight-motion';
import type { MenuScrollAnimation } from '../ui/menu-scroll-motion';
import { DrawExpression as E, type DrawValue } from './draw-expression';
import { DrawOp, type DisplayList, type ListCall, type ListImage } from './display-list';
export type MenuScrollHighlight = {
/** Final row top within the viewport. */
y: number;
width: number;
height: number;
radius: number;
/** Gray8, like drawMenuSelection. */
background: number;
border: number;
animation?: MenuHighlightAnimation;
};
/**
* A menu viewport scrolling over a pre-rendered strip. `strip` holds every
* row that is visible at any point of the scroll, starting at content offset
* `stripTop` and `stripX` pixels into the viewport. The copy's source row is
* the animated value; the highlight is max-blended over the copied rows,
* sliding on the same timeline and clamped inside the viewport.
*/
export function menuScrollList(strip: ListImage, stripX: number, stripTop: number, viewportHeight: number,
scroll: MenuScrollAnimation, highlight?: MenuScrollHighlight): DisplayList {
const eased = (durationMs: number, delayMs = 0) => E.progress(durationMs, delayMs).ease();
const sourceY: DrawValue = eased(scroll.durationMs)
.lerp(E.i32(scroll.from - stripTop).toFloat(), E.i32(scroll.to - stripTop).toFloat()).toInt();
const calls: ListCall[] = [
{ op: DrawOp.RECT_COPY, resource: 0, x: 0, y: sourceY, width: strip.width, height: viewportHeight, dx: stripX, dy: 0 },
];
if (highlight) {
const q = (n: number) => Math.min(15, (n + 8) >> 4);
const motion = highlight.animation;
let y: DrawValue = highlight.y;
if (motion?.dy) {
const delay = Math.max(0, motion.startedAt - scroll.startedAt);
y = eased(motion.durationMs, delay).lerp(E.i32(highlight.y + motion.dy).toFloat(), E.i32(highlight.y).toFloat()).toInt()
.max(E.i32(0)).min(E.i32(viewportHeight - highlight.height));
}
calls.push({ op: DrawOp.ROUNDED_RECT, x: 0, y, width: highlight.width, height: highlight.height, radius: highlight.radius,
background: q(highlight.background), border: q(highlight.border) });
}
return { resources: [strip], timeline: scroll, calls };
}