forked from Blendman974/faceclaw
Clean up magic-numbers used in menu animation
This commit is contained in:
1 parent
08f1b9a2a2
commit
063d8a0b03
6 files changed
+66
-29
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { encodePresentation } from "./presentation-wire";
|
||||
import { DrawRecordKind, encodePresentation } from "./presentation-wire";
|
||||
/**
|
||||
* Marshals deferred-draw identity (text glyphs and icon images) to the Kotlin
|
||||
* side for the texture-cache pipeline (CFW modes 18/19/20; see
|
||||
@@ -201,7 +201,7 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer |
|
||||
if (!inRange16(placed.x) || !inRange16(placed.y)) continue;
|
||||
const state = fontWireState(placed.font);
|
||||
if (!state || !representableGlyph(placed.font, placed.glyph)) continue;
|
||||
out.setUint8(offset, 0);
|
||||
out.setUint8(offset, DrawRecordKind.GLYPH);
|
||||
out.setUint16(offset + 1, state.fontId, true);
|
||||
out.setUint32(offset + 3, placed.glyph.encoding, true);
|
||||
out.setInt16(offset + 7, placed.x, true);
|
||||
@@ -213,14 +213,14 @@ export function prepareFrameDraws(draws: readonly DeferredDraw[]): ArrayBuffer |
|
||||
if (placed.presentation) { const bytes = encodePresentation(placed); new Uint8Array(out.buffer).set(bytes, offset); offset += bytes.length; continue; }
|
||||
const id = imageId(placed);
|
||||
if (id === null) continue;
|
||||
out.setUint8(offset, 1);
|
||||
out.setUint8(offset, DrawRecordKind.TEXTURE_IMAGE);
|
||||
out.setUint32(offset + 1, id, true);
|
||||
out.setInt16(offset + 5, placed.x, true);
|
||||
out.setInt16(offset + 7, placed.y, true);
|
||||
offset += IMAGE_RECORD_BYTES;
|
||||
} else {
|
||||
if (!fwRunOk.get(placed)) continue;
|
||||
out.setUint8(offset, 2);
|
||||
out.setUint8(offset, DrawRecordKind.FIRMWARE_TEXT);
|
||||
out.setInt16(offset + 1, placed.x, true);
|
||||
out.setInt16(offset + 3, placed.y, true);
|
||||
out.setUint8(offset + 5, placed.value);
|
||||
|
||||
@@ -1,7 +1,23 @@
|
||||
import type { PlacedImage } from './image'
|
||||
import type { MenuHighlightAnimation } from '../ui/menu-highlight-motion'
|
||||
|
||||
/** 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. */
|
||||
/** TS/native bridge tags, not firmware draw opcodes. Keep in sync with DrawRecordKind.kt. */
|
||||
export const DrawRecordKind = {
|
||||
GLYPH: 0,
|
||||
TEXTURE_IMAGE: 1,
|
||||
FIRMWARE_TEXT: 2,
|
||||
MENU_SELECTION: 3,
|
||||
TRANSPARENT_IMAGE: 4,
|
||||
MASKED_IMAGE: 5,
|
||||
ANIMATED_MENU_SELECTION: 6,
|
||||
} 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
|
||||
}
|
||||
|
||||
/** Presentation record: tag, signed xy, u16 wh/radius, gray fill/stroke, signed depth, restore-count, optional animation, gray pixels, restore rectangles. */
|
||||
export function encodePresentation(draw: PlacedImage): Uint8Array {
|
||||
const p = draw.presentation!
|
||||
const { width, height, pixels } = draw.source
|
||||
@@ -12,7 +28,9 @@ export function encodePresentation(draw: PlacedImage): Uint8Array {
|
||||
if (animation && (!Number.isInteger(animation.durationMs) || animation.durationMs < 1 || animation.durationMs > 65535)) throw new Error('Invalid menu animation duration')
|
||||
const header = animation ? 28 : 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
|
||||
bytes[0] = animation ? DrawRecordKind.ANIMATED_MENU_SELECTION
|
||||
: 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)
|
||||
@@ -35,11 +53,12 @@ export function encodePresentation(draw: PlacedImage): Uint8Array {
|
||||
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 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 ? 28 : 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,
|
||||
const kind = bytes[offset]
|
||||
const header = kind === DrawRecordKind.ANIMATED_MENU_SELECTION ? 28 : 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 (header === 28) {
|
||||
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),
|
||||
@@ -53,10 +72,10 @@ 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 || 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
|
||||
if(isPresentationKind(bytes[p])) { const record=readPresentation(bytes,p);result.push(record.selection);p=record.end }
|
||||
else if(bytes[p]===DrawRecordKind.GLYPH) p+=12
|
||||
else if(bytes[p]===DrawRecordKind.TEXTURE_IMAGE) p+=9
|
||||
else if(bytes[p]===DrawRecordKind.FIRMWARE_TEXT) p+=7+bytes[p+6]!*7
|
||||
else break
|
||||
}
|
||||
return result
|
||||
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
/** TS/native bridge tags, not firmware draw opcodes. Keep in sync with presentation-wire.ts. */
|
||||
object DrawRecordKind {
|
||||
const val GLYPH = 0
|
||||
const val TEXTURE_IMAGE = 1
|
||||
const val FIRMWARE_TEXT = 2
|
||||
const val MENU_SELECTION = 3
|
||||
const val TRANSPARENT_IMAGE = 4
|
||||
const val MASKED_IMAGE = 5
|
||||
const val ANIMATED_MENU_SELECTION = 6
|
||||
|
||||
fun isPresentation(kind: Int): Boolean = when (kind) {
|
||||
MENU_SELECTION, TRANSPARENT_IMAGE, MASKED_IMAGE, ANIMATED_MENU_SELECTION -> true
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
+11
-10
@@ -2,7 +2,7 @@ package com.faceclaw.app
|
||||
|
||||
/** A retained selection or image, replayed at stereo depth over the screen and shell surfaces. */
|
||||
class MenuSelection(val x: Int, val y: Int, val width: Int, val height: Int, val radius: Int,
|
||||
val background: Int, val border: Int, val depth: Int, val packed: ByteArray, val occlusions: List<IntArray> = emptyList(), val kind: Int = 3, val mask: List<IntArray> = emptyList(), val animation: Animation? = null) {
|
||||
val background: Int, val border: Int, val depth: Int, val packed: ByteArray, val occlusions: List<IntArray> = emptyList(), val kind: Int = DrawRecordKind.MENU_SELECTION, val mask: List<IntArray> = emptyList(), val animation: Animation? = null) {
|
||||
class Animation(val dx: Int, val dy: Int, val startedAt: Long, val token: Int, val durationMs: Int)
|
||||
val resource = CachedResource(DrawProtocol.rawImage(width, height, packed))
|
||||
val fingerprint = "${animation?.token},${animation?.dx},${animation?.dy},${animation?.durationMs},$kind,${mask.joinToString { it.joinToString() }},$x,$y,$width,$height,$radius,$background,$border,$depth,${resource.hash},${occlusions.joinToString { it.joinToString() }}"
|
||||
@@ -12,12 +12,12 @@ class MenuSelection(val x: Int, val y: Int, val width: Int, val height: Int, val
|
||||
val highlightX = animation?.let { DrawValue.animate(x + it.dx, x, it.durationMs, elapsed) } ?: DrawValue.Integer(x)
|
||||
val highlightY = animation?.let { DrawValue.animate(y + it.dy, y, it.durationMs, elapsed) } ?: DrawValue.Integer(y)
|
||||
val image = when (kind) {
|
||||
3 -> listOf(DrawProtocol.roundedRect(highlightX, highlightY, width, height, radius, background, border, depth),
|
||||
DrawRecordKind.MENU_SELECTION -> listOf(DrawProtocol.roundedRect(highlightX, highlightY, width, height, radius, background, border, depth),
|
||||
DrawProtocol.image(id, x, y, CFW_TEXTURE_OPT_BRIGHTNESS_MASK or CFW_TEXTURE_OPT_TRANSPARENT, depth = depth))
|
||||
4 -> listOf(DrawProtocol.image(id, x, y, CFW_TEXTURE_OPT_BRIGHTNESS_MASK or CFW_TEXTURE_OPT_TRANSPARENT, depth = depth))
|
||||
DrawRecordKind.TRANSPARENT_IMAGE -> listOf(DrawProtocol.image(id, x, y, CFW_TEXTURE_OPT_BRIGHTNESS_MASK or CFW_TEXTURE_OPT_TRANSPARENT, depth = depth))
|
||||
// Gray8 zero is transparent but gray8 one is opaque black. Rect copies preserve
|
||||
// that distinction after packing, including the transparent corners of app menus.
|
||||
5 -> mask.map { rect ->
|
||||
DrawRecordKind.MASKED_IMAGE -> mask.map { rect ->
|
||||
DrawProtocol.rectCopy(id, rect[0], rect[1], rect[2], rect[3],
|
||||
x + rect[0], y + rect[1], depth = depth)
|
||||
}
|
||||
@@ -29,9 +29,9 @@ class MenuSelection(val x: Int, val y: Int, val width: Int, val height: Int, val
|
||||
}
|
||||
|
||||
companion object {
|
||||
/** The caller has consumed the presentation tag (3–6). Colors cross the bridge in gray8, pixels in gray8 rows. */
|
||||
fun read(reader: ByteReader, kind: Int = 3): MenuSelection {
|
||||
require(kind in 3..6)
|
||||
/** The caller has consumed a presentation tag from [DrawRecordKind]. Colors and pixels cross in gray8. */
|
||||
fun read(reader: ByteReader, kind: Int = DrawRecordKind.MENU_SELECTION): MenuSelection {
|
||||
require(DrawRecordKind.isPresentation(kind))
|
||||
require(reader.remaining() >= 15)
|
||||
val x = reader.getShort().toInt(); val y = reader.getShort().toInt()
|
||||
val w = reader.getShort().toInt() and 65535; val h = reader.getShort().toInt() and 65535
|
||||
@@ -42,7 +42,7 @@ class MenuSelection(val x: Int, val y: Int, val width: Int, val height: Int, val
|
||||
val count = reader.getShort().toInt() and 65535
|
||||
require(count <= 2048)
|
||||
require(w in 1..640 && h in 1..480 && 5 + (w + 1) / 2 * h <= 65536 && reader.remaining() >= w * h)
|
||||
val animation = if (kind == 6) {
|
||||
val animation = if (kind == DrawRecordKind.ANIMATED_MENU_SELECTION) {
|
||||
require(reader.remaining() >= 12)
|
||||
val dx = reader.getShort().toInt()
|
||||
val dy = reader.getShort().toInt()
|
||||
@@ -56,7 +56,7 @@ class MenuSelection(val x: Int, val y: Int, val width: Int, val height: Int, val
|
||||
require(reader.remaining() >= count * 8)
|
||||
val occlusions = List(count) { IntArray(4) { reader.getShort().toInt() and 65535 } }
|
||||
val mask = ArrayList<IntArray>()
|
||||
if (kind == 5) for (yy in 0 until h) {
|
||||
if (kind == DrawRecordKind.MASKED_IMAGE) for (yy in 0 until h) {
|
||||
var start = -1
|
||||
for (xx in 0..w) {
|
||||
val covered = xx < w && gray[yy * w + xx].toInt() != 0
|
||||
@@ -68,7 +68,8 @@ class MenuSelection(val x: Int, val y: Int, val width: Int, val height: Int, val
|
||||
}
|
||||
}
|
||||
}
|
||||
return MenuSelection(x, y, w, h, radius, background, border, depth, BmpUtil.pack4bppFromGray8(gray, w, h), occlusions, if (kind == 6) 3 else kind, mask, animation)
|
||||
val normalizedKind = if (kind == DrawRecordKind.ANIMATED_MENU_SELECTION) DrawRecordKind.MENU_SELECTION else kind
|
||||
return MenuSelection(x, y, w, h, radius, background, border, depth, BmpUtil.pack4bppFromGray8(gray, w, h), occlusions, normalizedKind, mask, animation)
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -51,7 +51,7 @@ class SurfaceCompositor @JvmOverloads constructor(private val includePreviewInFr
|
||||
var out: MutableList<ScreenDraw> = ArrayList()
|
||||
while ((cursor.remaining() >= 1)) {
|
||||
var kind: Int = (cursor.get() and 0xff)
|
||||
if (kind in 3..6) { out.add(ScreenDraw.image(0, 0, 0).also { it.selection = MenuSelection.read(cursor, kind) }); continue }
|
||||
if (DrawRecordKind.isPresentation(kind)) { out.add(ScreenDraw.image(0, 0, 0).also { it.selection = MenuSelection.read(cursor, kind) }); continue }
|
||||
if (((kind == ScreenDraw.KIND_GLYPH) && (cursor.remaining() >= 11))) {
|
||||
var fontId: Int = (cursor.getShort().toInt() and 0xffff)
|
||||
var encoding: Int = cursor.getInt()
|
||||
@@ -110,12 +110,12 @@ class SurfaceCompositor @JvmOverloads constructor(private val includePreviewInFr
|
||||
class ScreenDraw {
|
||||
@JvmField var selection: MenuSelection? = null
|
||||
companion object {
|
||||
const val KIND_GLYPH: Int = 0
|
||||
const val KIND_GLYPH: Int = DrawRecordKind.GLYPH
|
||||
|
||||
const val KIND_IMAGE: Int = 1
|
||||
const val KIND_IMAGE: Int = DrawRecordKind.TEXTURE_IMAGE
|
||||
|
||||
/** A firmware-builtin-font text run (CFW mode 15). */
|
||||
const val KIND_FWTEXT: Int = 2
|
||||
const val KIND_FWTEXT: Int = DrawRecordKind.FIRMWARE_TEXT
|
||||
|
||||
@JvmStatic
|
||||
fun glyph(fontId: Int, encoding: Int, penX: Int, lineY: Int, value: Int): ScreenDraw {
|
||||
|
||||
@@ -77,7 +77,7 @@ class DrawExpressionTest {
|
||||
val wire = hex("06030002000200020001001030020000fefffcffc8002a0000002c01ffffffff")
|
||||
val input = ArrayByteReader(wire)
|
||||
assertEquals(6, input.get().toInt())
|
||||
val row = MenuSelection.read(input, 6).translated(10, 10)
|
||||
val row = MenuSelection.read(input, DrawRecordKind.ANIMATED_MENU_SELECTION).translated(10, 10)
|
||||
assertEquals(0, input.remaining())
|
||||
val motion = assertNotNull(row.animation)
|
||||
assertEquals(42, motion.token)
|
||||
@@ -96,7 +96,7 @@ class DrawExpressionTest {
|
||||
assertEquals(elapsed < 100, frame.animationPending)
|
||||
}
|
||||
for (end in 1 until wire.size) assertFails {
|
||||
MenuSelection.read(ArrayByteReader(wire.copyOf(end), 1), 6)
|
||||
MenuSelection.read(ArrayByteReader(wire.copyOf(end), 1), DrawRecordKind.ANIMATED_MENU_SELECTION)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user