Display-list based menu slide animation

This commit is contained in:
James Babcock committed 2026-09-23 01:13:03 -07:00
1 parent 92e8def0f0
commit c8d835176f
28 files changed
+1546 -263

No files matched your search

+5 -4
View File
@@ -1,3 +1,4 @@
import type { MenuHighlightAnimation } from "../ui/menu-highlight-motion";
import { Glyph } from "./bdffont";
import { wrapText } from "./textwrap";
@@ -73,7 +74,7 @@ export type PlacedGlyph = {
* from the moment it is placed.
*/
export type PlacedImage = {
presentation?: { mode?: "image" | "masked-image"; radius: number; background: number; border: number; depth: number; occlusions?: readonly { x: number; y: number; width: number; height: number }[] };
presentation?: { animation?: MenuHighlightAnimation; mode?: "image" | "masked-image"; radius: number; background: number; border: number; depth: number; occlusions?: readonly { x: number; y: number; width: number; height: number }[] };
kind: "image";
source: GrayImage;
x: number;
@@ -394,7 +395,7 @@ export class GrayImage {
hash = mixInt(hash, placed.source.width);
hash = mixInt(hash, placed.source.height);
hash = mixInt(hash, placed.source.sourceContentHash32());
if (placed.presentation) { const p = placed.presentation; for (const value of [p.radius, p.background, p.border, p.depth, p.mode === "image" ? 1 : p.mode === "masked-image" ? 2 : 0]) hash = mixInt(hash, value); }
if (placed.presentation) { const p = placed.presentation; for (const value of [p.radius, p.background, p.border, p.depth, p.mode === "image" ? 1 : p.mode === "masked-image" ? 2 : 0, p.animation?.token ?? 0, p.animation?.dx ?? 0, p.animation?.dy ?? 0]) hash = mixInt(hash, value); }
} else {
hash = mixInt(hash, 0xf17e);
hash = mixInt(hash, placed.font.atlasTag);
@@ -506,9 +507,9 @@ export class GrayImage {
}
/** Retain the selected row separately from the menu surface for glasses-side composition. */
drawMenuSelection(source: GrayImage, x: number, y: number, background: number, border: number, radius = 8, depth = 2): void {
drawMenuSelection(source: GrayImage, x: number, y: number, background: number, border: number, radius = 8, depth = 2, animation?: MenuHighlightAnimation): void {
this.drawList.push({ kind: "image", source: source.withDrawsBaked(), x, y,
presentation: { radius, background, border, depth } });
presentation: { radius, background, border, depth, animation } });
}
/** Replay an image at stereo depth. Masked images preserve intentional gray8 black (1). */
+30 -12
View File
@@ -1,20 +1,27 @@
import type { PlacedImage } from './image'
/** Bridge record: tag 3 (selection), 4 (image), 5 (gray8-masked image), signed xy, u16 wh/radius, gray fill/stroke, signed depth, restore-count, gray pixels, restore rectangles. */
/** Bridge record: tag 3 (selection), 4 (image), 5 (gray8-masked image), 6 (animated selection), 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 (!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 bytes = new Uint8Array(16 + width * height + (p.occlusions?.length ?? 0) * 8), view = new DataView(bytes.buffer)
bytes[0] = p.mode === "image" ? 4 : p.mode === "masked-image" ? 5 : 3
const animation = p.animation;
const header = animation ? 26 : 16;
const bytes = new Uint8Array(header + width * height + (p.occlusions?.length ?? 0) * 8), view = new DataView(bytes.buffer)
bytes[0] = animation ? 6 : p.mode === "image" ? 4 : p.mode === "masked-image" ? 5 : 3
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)
bytes.set(pixels, 16)
let offset = 16 + pixels.length
if (animation) {
view.setInt16(16, animation.dx, true); view.setInt16(18, animation.dy, true);
view.setUint16(20, Math.max(0, Math.min(500, Date.now() - animation.startedAt)), true);
view.setUint32(22, animation.token, true);
}
bytes.set(pixels, header)
let offset = header + pixels.length
for (const rect of p.occlusions ?? []) {
view.setUint16(offset, rect.x, true); view.setUint16(offset + 2, rect.y, true)
view.setUint16(offset + 4, rect.width, true); view.setUint16(offset + 6, rect.height, true); offset += 8
@@ -22,13 +29,16 @@ export function encodePresentation(draw: PlacedImage): Uint8Array {
return bytes
}
export type Selection = { 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 = { animation?: { dx: number; dy: number; startedAt: number; token: number }; 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 } {
const v = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength), w = v.getUint16(offset + 5, true), h = v.getUint16(offset + 7, true)
const header = bytes[offset] === 6 ? 26 : 16
const selection: Selection = { mode: bytes[offset] === 4 ? "image" : bytes[offset] === 5 ? "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+16,offset+16+w*h), occlusions:[] }
let end=offset+16+w*h
pixels:bytes.slice(offset+header,offset+header+w*h), occlusions:[] }
if (header === 26) selection.animation = { dx:v.getInt16(offset+16,true), dy:v.getInt16(offset+18,true),
startedAt:Date.now()-v.getUint16(offset+20,true), token:v.getUint32(offset+22,true) }
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}
}
@@ -36,7 +46,7 @@ export function presentationRecords(buffer: ArrayBuffer | null): Selection[] {
if (!buffer) return []
const bytes=new Uint8Array(buffer), result:Selection[]=[]
for(let p=0;p<bytes.length;) {
if(bytes[p]===3 || bytes[p]===4 || bytes[p]===5) { const record=readPresentation(bytes,p);result.push(record.selection);p=record.end }
if(bytes[p]===3 || bytes[p]===4 || bytes[p]===5 || bytes[p]===6) { const record=readPresentation(bytes,p);result.push(record.selection);p=record.end }
else if(bytes[p]===0) p+=12
else if(bytes[p]===1) p+=9
else if(bytes[p]===2) p+=7+bytes[p+6]!*7
@@ -52,13 +62,21 @@ 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) / 500)) : 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)
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
const i=y*width+x
output[i]=!inside(xx-1,yy-1,row.width-2,row.height-2,Math.max(0,row.radius-1))?q(row.border)*16:Math.max(output[i]!,q(row.background)*16)
}
for(let yy=0;yy<row.height;yy++) for(let xx=0;xx<row.width;xx++) {
const x=row.x+xx+shift,y=row.y+yy
if(x<0||y<0||x>=width||y>=height) continue
const i=y*width+x
if(!row.mode && inside(xx,yy,row.width,row.height,row.radius)) output[i]=!inside(xx-1,yy-1,row.width-2,row.height-2,Math.max(0,row.radius-1))?q(row.border)*16:Math.max(output[i]!,q(row.background)*16)
const value=q(row.pixels[yy*row.width+xx]!)
if(row.mode === "masked-image" ? row.pixels[yy*row.width+xx] !== 0 : value !== 0) output[i]=value*16
if(row.mode === "masked-image" ? row.pixels[yy*row.width+xx] !== 0 : value !== 0) output[y*width+x]=value*16
}
for(const r of row.occlusions) for(let yy=r.y;yy<r.y+r.height;yy++) for(let xx=r.x;xx<r.x+r.width;xx++) if(xx>=0&&yy>=0&&xx<width&&yy<height) output[yy*width+xx]=q(screen[yy*width+xx]!)*16
}
+3 -2
View File
@@ -10,7 +10,8 @@ type Surface = SurfaceConfiguration & { pixels: Uint8Array; visible: boolean; se
export class SurfaceCompositor {
private readonly surfaces = new Map<string, Surface>()
private shellScene: Uint8Array | null = null
setShellScene(bytes: Uint8Array): void { this.shellScene = new Uint8Array(bytes) }
private shellReceivedAt = 0
setShellScene(bytes: Uint8Array): void { this.shellScene = new Uint8Array(bytes); this.shellReceivedAt = Date.now() }
private dimBelow = 0
private dimFactor = 1
private blanked = false
@@ -92,7 +93,7 @@ export class SurfaceCompositor {
if(x+xx+shift>=0 && y+yy>=0 && x+xx+shift<this.width && y+yy<this.height) output[(y+yy)*this.width+x+xx+shift]=quantize(bytes[p+yy*w+xx])*16
}
p+=w*h
for(let j=0;j<selections;j++) { const record=readPresentation(bytes,p);p=record.end;paintPresentation(output,screen,this.width,this.height,record.selection) }
for(let j=0;j<selections;j++) { const record=readPresentation(bytes,p);p=record.end;if(record.selection.animation) record.selection.animation.startedAt -= Date.now()-this.shellReceivedAt;paintPresentation(output,screen,this.width,this.height,record.selection) }
}
}
return output