use super::context::RenderContext;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct AnimParams {
pub w: f64,
pub h: f64,
pub seed: u32,
pub mouse: Option<(f64, f64)>,
pub args: [f64; ANIM_ARGS],
}
pub const ANIM_ARGS: usize = 8;
impl Default for AnimParams {
fn default() -> Self {
Self { w: 64.0, h: 64.0, seed: 0, mouse: None, args: [0.0; ANIM_ARGS] }
}
}
impl AnimParams {
pub fn to_array(&self) -> [f64; 6 + ANIM_ARGS] {
let (mx, my, hv) = match self.mouse {
Some((x, y)) => (x, y, 1.0),
None => (0.0, 0.0, 0.0),
};
let mut q = [0.0; 6 + ANIM_ARGS];
q[..6].copy_from_slice(&[self.w, self.h, self.seed as f64, mx, my, hv]);
q[6..].copy_from_slice(&self.args);
q
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum AnimScriptError {
Unavailable(&'static str),
Script(String),
BadOp(usize),
}
impl core::fmt::Display for AnimScriptError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Unavailable(why) => write!(f, "anim script engine unavailable: {why}"),
Self::Script(m) => write!(f, "anim script error: {m}"),
Self::BadOp(i) => write!(f, "anim op stream malformed at {i}"),
}
}
}
impl std::error::Error for AnimScriptError {}
pub trait AnimScript {
fn frame(&mut self, t: f64, params: &AnimParams, out: &mut Vec<f64>) -> Result<(), AnimScriptError>;
fn paint(
&mut self,
ctx: &mut dyn RenderContext,
t: f64,
params: &AnimParams,
scratch: &mut Vec<f64>,
) -> Result<(), AnimScriptError> {
self.frame(t, params, scratch)?;
replay_anim_ops(ctx, scratch)
}
}
pub mod op {
pub const SAVE: u8 = 0;
pub const RESTORE: u8 = 1;
pub const TRANSLATE: u8 = 2;
pub const ROTATE: u8 = 3;
pub const SCALE: u8 = 4;
pub const BEGIN_PATH: u8 = 5;
pub const MOVE_TO: u8 = 6;
pub const LINE_TO: u8 = 7;
pub const QUAD_TO: u8 = 8;
pub const BEZIER_TO: u8 = 9;
pub const ARC: u8 = 10;
pub const ELLIPSE: u8 = 11;
pub const CLOSE_PATH: u8 = 12;
pub const FILL: u8 = 13;
pub const STROKE: u8 = 14;
pub const FILL_COLOR: u8 = 15;
pub const STROKE_COLOR: u8 = 16;
pub const LINE_WIDTH: u8 = 17;
pub const GLOBAL_ALPHA: u8 = 18;
pub const FILL_RECT: u8 = 19;
pub const STROKE_RECT: u8 = 20;
pub const CLIP: u8 = 21;
pub const TRANSFORM: u8 = 22;
pub const DRAW_IMAGE: u8 = 23;
pub const DRAW_TRI: u8 = 24;
pub const FILTER: u8 = 25;
pub const fn arity(code: u8) -> Option<usize> {
Some(match code {
SAVE | RESTORE | BEGIN_PATH | CLOSE_PATH | FILL | STROKE | CLIP => 0,
ROTATE | FILL_COLOR | STROKE_COLOR | LINE_WIDTH | GLOBAL_ALPHA | FILTER => 1,
TRANSLATE | SCALE | MOVE_TO | LINE_TO => 2,
QUAD_TO | FILL_RECT | STROKE_RECT => 4,
ARC => 5,
BEZIER_TO | TRANSFORM => 6,
ELLIPSE => 7,
DRAW_IMAGE => 9,
DRAW_TRI => 13,
_ => return None,
})
}
}
#[cfg(test)]
fn rgba_hex(c: u32) -> [u8; 9] {
const HEX: &[u8; 16] = b"0123456789abcdef";
let mut s = [b'#'; 9];
for i in 0..8 {
s[1 + i] = HEX[((c >> (28 - 4 * i)) & 0xf) as usize];
}
s
}
pub fn replay_anim_ops(ctx: &mut dyn RenderContext, ops: &[f64]) -> Result<(), AnimScriptError> {
super::anim_bitmap::replay_anim_ops_bitmaps(ctx, ops, &[])
}
pub struct AnimOpWriter<'a>(pub &'a mut Vec<f64>);
impl AnimOpWriter<'_> {
fn push(&mut self, code: u8, args: &[f64]) {
self.0.push(code as f64);
self.0.extend_from_slice(args);
}
pub fn save(&mut self) { self.push(op::SAVE, &[]) }
pub fn restore(&mut self) { self.push(op::RESTORE, &[]) }
pub fn translate(&mut self, x: f64, y: f64) { self.push(op::TRANSLATE, &[x, y]) }
pub fn rotate(&mut self, a: f64) { self.push(op::ROTATE, &[a]) }
pub fn scale(&mut self, x: f64, y: f64) { self.push(op::SCALE, &[x, y]) }
pub fn begin_path(&mut self) { self.push(op::BEGIN_PATH, &[]) }
pub fn move_to(&mut self, x: f64, y: f64) { self.push(op::MOVE_TO, &[x, y]) }
pub fn line_to(&mut self, x: f64, y: f64) { self.push(op::LINE_TO, &[x, y]) }
pub fn close_path(&mut self) { self.push(op::CLOSE_PATH, &[]) }
pub fn fill(&mut self) { self.push(op::FILL, &[]) }
pub fn stroke(&mut self) { self.push(op::STROKE, &[]) }
pub fn fill_color(&mut self, rgba: u32) { self.push(op::FILL_COLOR, &[rgba as f64]) }
pub fn stroke_color(&mut self, rgba: u32) { self.push(op::STROKE_COLOR, &[rgba as f64]) }
pub fn line_width(&mut self, w: f64) { self.push(op::LINE_WIDTH, &[w]) }
pub fn global_alpha(&mut self, a: f64) { self.push(op::GLOBAL_ALPHA, &[a]) }
}
pub struct BakedAnim {
pub fps: f64,
pub frames: Vec<Vec<f64>>,
}
impl AnimScript for BakedAnim {
fn frame(&mut self, t: f64, _p: &AnimParams, out: &mut Vec<f64>) -> Result<(), AnimScriptError> {
out.clear();
if self.frames.is_empty() {
return Ok(());
}
let n = self.frames.len() as i64;
let i = ((t * self.fps).floor() as i64).rem_euclid(n) as usize;
out.extend_from_slice(&self.frames[i]);
Ok(())
}
}
pub struct FnAnim<F>(pub F);
impl<F: FnMut(&mut dyn RenderContext, f64, &AnimParams)> AnimScript for FnAnim<F> {
fn frame(&mut self, _t: f64, _p: &AnimParams, out: &mut Vec<f64>) -> Result<(), AnimScriptError> {
out.clear();
Err(AnimScriptError::Unavailable("FnAnim paints directly"))
}
fn paint(&mut self, ctx: &mut dyn RenderContext, t: f64, p: &AnimParams, _s: &mut Vec<f64>) -> Result<(), AnimScriptError> {
(self.0)(ctx, t, p);
Ok(())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AnimSource {
Primary,
Fallback,
}
struct AnimEntry {
name: String,
primary: Option<Box<dyn AnimScript>>,
fallback: Option<Box<dyn AnimScript>>,
retry_at: f64,
last_error: Option<AnimScriptError>,
}
#[derive(Default)]
pub struct AnimRegistry {
entries: Vec<AnimEntry>,
scratch: Vec<f64>,
}
impl AnimRegistry {
pub fn new() -> Self {
Self::default()
}
fn entry(&mut self, name: &str) -> &mut AnimEntry {
let i = match self.entries.iter().position(|e| e.name == name) {
Some(i) => i,
None => {
self.entries.push(AnimEntry { name: name.to_owned(), primary: None, fallback: None, retry_at: f64::NEG_INFINITY, last_error: None });
self.entries.len() - 1
}
};
&mut self.entries[i]
}
pub fn register(&mut self, name: &str, script: Box<dyn AnimScript>) -> &mut Self {
let e = self.entry(name);
e.primary = Some(script);
e.retry_at = f64::NEG_INFINITY;
e.last_error = None;
self
}
pub fn fallback(&mut self, name: &str, script: Box<dyn AnimScript>) -> &mut Self {
self.entry(name).fallback = Some(script);
self
}
pub fn has(&self, name: &str) -> bool {
self.entries.iter().any(|e| e.name == name && (e.primary.is_some() || e.fallback.is_some()))
}
pub fn last_error(&self, name: &str) -> Option<&AnimScriptError> {
self.entries.iter().find(|e| e.name == name).and_then(|e| e.last_error.as_ref())
}
pub fn paint(&mut self, ctx: &mut dyn RenderContext, name: &str, t: f64, params: &AnimParams) -> Result<AnimSource, AnimScriptError> {
let Some(e) = self.entries.iter_mut().find(|e| e.name == name) else {
return Err(AnimScriptError::Unavailable("no animation registered under this name"));
};
let scratch = &mut self.scratch;
if let Some(p) = e.primary.as_mut().filter(|_| t >= e.retry_at) {
match p.frame(t, params, scratch) {
Ok(()) => {
e.last_error = None;
replay_anim_ops(ctx, scratch)?;
return Ok(AnimSource::Primary);
}
Err(err) => {
e.retry_at = if matches!(err, AnimScriptError::Unavailable(_)) { f64::INFINITY } else { t + 1.0 };
e.last_error = Some(err);
}
}
}
match e.fallback.as_mut() {
Some(f) => f.paint(ctx, t, params, scratch).map(|()| AnimSource::Fallback),
None => Err(e.last_error.clone().unwrap_or(AnimScriptError::Unavailable("no fallback painter"))),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn color_hex_roundtrip() {
assert_eq!(&rgba_hex(0xf2a93bff), b"#f2a93bff");
assert_eq!(crate::render::parse_color(core::str::from_utf8(&rgba_hex(0x11223344)).unwrap()), (0x11, 0x22, 0x33, 0x44));
}
#[test]
fn arity_covers_all_codes() {
for c in 0..=25u8 {
assert!(op::arity(c).is_some(), "code {c}");
}
assert!(op::arity(26).is_none());
}
#[test]
fn params_wire_layout() {
let p = AnimParams { w: 10.0, h: 20.0, seed: 3, mouse: Some((1.0, 2.0)), args: [4.0, 5.0, 6.0, 7.0, 0.0, 0.0, 0.0, 8.0] };
assert_eq!(p.to_array(), [10.0, 20.0, 3.0, 1.0, 2.0, 1.0, 4.0, 5.0, 6.0, 7.0, 0.0, 0.0, 0.0, 8.0]);
}
struct Fails(AnimScriptError, std::rc::Rc<std::cell::Cell<u32>>);
impl AnimScript for Fails {
fn frame(&mut self, _t: f64, _p: &AnimParams, _o: &mut Vec<f64>) -> Result<(), AnimScriptError> {
self.1.set(self.1.get() + 1);
Err(self.0.clone())
}
}
#[test]
fn registry_falls_back_and_retries_script_errors_only() {
use crate::testing::RecordingRenderContext;
let mut ctx = RecordingRenderContext::new();
let p = AnimParams::default();
let calls = std::rc::Rc::new(std::cell::Cell::new(0));
let mut reg = AnimRegistry::new();
assert!(reg.paint(&mut ctx, "x", 0.0, &p).is_err());
reg.register("x", Box::new(Fails(AnimScriptError::Script("not loaded".into()), calls.clone())));
let hits = std::rc::Rc::new(std::cell::Cell::new(0));
let h2 = hits.clone();
reg.fallback("x", Box::new(FnAnim(move |_: &mut dyn RenderContext, _t: f64, _p: &AnimParams| h2.set(h2.get() + 1))));
assert_eq!(reg.paint(&mut ctx, "x", 0.0, &p), Ok(AnimSource::Fallback));
assert_eq!(reg.paint(&mut ctx, "x", 0.5, &p), Ok(AnimSource::Fallback));
assert_eq!(calls.get(), 1, "a script error waits a second before the next try");
reg.paint(&mut ctx, "x", 1.0, &p).unwrap();
assert_eq!((calls.get(), hits.get()), (2, 3));
assert!(matches!(reg.last_error("x"), Some(AnimScriptError::Script(_))));
let mut ops = Vec::new();
AnimOpWriter(&mut ops).fill_color(0xff0000ff);
reg.register("x", Box::new(BakedAnim { fps: 1.0, frames: vec![ops] }));
assert_eq!(reg.paint(&mut ctx, "x", 2.0, &p), Ok(AnimSource::Primary));
assert!(reg.last_error("x").is_none());
let c2 = std::rc::Rc::new(std::cell::Cell::new(0));
reg.register("y", Box::new(Fails(AnimScriptError::Unavailable("none"), c2.clone())));
reg.fallback("y", Box::new(FnAnim(|_: &mut dyn RenderContext, _t: f64, _p: &AnimParams| {})));
for t in 0..5 {
assert_eq!(reg.paint(&mut ctx, "y", t as f64 * 10.0, &p), Ok(AnimSource::Fallback));
}
assert_eq!(c2.get(), 1);
}
}