mod tokens;
use crate::input::core::coordinator::LayerId;
use crate::input::{InputCoordinator, Sense, WidgetKind};
use crate::render::{emit_svg_path, RenderContext};
use crate::types::{Rect, WidgetId};
pub const CRITTER_SHAPES: [&str; 13] = [
"M32 5c15 0 27 11 27 27S47 59 32 59 5 47 5 32 17 5 32 5z",
"M32 4c16 0 28 10 28 26 0 18-10 30-28 30S4 48 4 30C4 14 16 4 32 4z",
"M32 6c5 0 26 38 26 46 0 6-6 8-26 8S6 58 6 52C6 44 27 6 32 6z",
"M16 6h32c6 0 10 4 10 10v32c0 6-4 10-10 10H16C10 58 6 54 6 48V16C6 10 10 6 16 6z",
"M23 5h18l17 17v20L41 59H23L6 42V22z",
"M30 5c14-2 29 9 28 27-1 17-13 27-28 26C14 57 4 46 6 30 7 16 17 6 30 5z",
"M32 4c6 0 9 6 15 8s12 4 12 12c0 6-4 9-4 15 0 12-9 21-23 21S9 51 9 39c0-6-4-9-4-15 0-8 6-10 12-12s9-8 15-8z",
"M32 6c13 0 24 11 24 24v26l-6-4-6 4-6-4-6 4-6-4-6 4-6-4-6 4V30C8 17 19 6 32 6z",
"M27 7.9q5-2.9 10 0l15.4 8.9q5 2.9 5 8.7v17q0 5.8-5 8.7L37 60.1q-5 2.9-10 0l-15.4-8.9q-5-2.9-5-8.7v-17q0-5.8 5-8.7z",
"M32 5c22 0 27 5 27 27S54 59 32 59 5 54 5 32 10 5 32 5z",
"M34 5c12 1 24 9 24 22 0 9-5 13-4 20 1 8-8 13-20 12-14-1-26-8-27-22C6 22 18 4 34 5z",
"M32 5c12 0 20 8 20 18v18c0 10-8 18-20 18S12 51 12 41V23C12 13 20 5 32 5z",
"M27 9q5-7 10 0l20 35q4 8-5 9H12q-9-1-5-9z",
];
pub const CRITTER_PALETTE: [&str; 10] = tokens::PALETTE;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CritterFamily {
Dm,
Group,
Agent,
Node,
}
impl CritterFamily {
pub fn shapes(self) -> &'static [usize] {
match self {
CritterFamily::Dm => tokens::DM_SHAPES,
CritterFamily::Group => &[3, 8],
CritterFamily::Agent => &[1, 2, 6, 7, 10, 12],
CritterFamily::Node => &[4],
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CritterView {
pub shape: usize,
pub color: &'static str,
pub eye_y: f64,
pub off: bool,
pub phase: f64,
}
pub fn critter_hash(name: &str) -> u32 {
let mut h: u32 = 2166136261;
for c in name.encode_utf16() {
h = (h ^ c as u32).wrapping_mul(16777619);
}
h
}
fn color_hash(name: &str) -> u32 {
let mut h: u32 = 0;
for c in name.encode_utf16() {
h = h.wrapping_mul(31).wrapping_add(c as u32);
}
h
}
pub fn critter_view(name: &str, family: CritterFamily) -> CritterView {
let h = critter_hash(name);
let set = family.shapes();
CritterView {
shape: set[((h >> 8) as usize) % set.len()],
color: CRITTER_PALETTE[(color_hash(name) as usize) % CRITTER_PALETTE.len()],
eye_y: 28.0 + ((h >> 4) % 5) as f64 - 2.0,
off: false,
phase: (h >> 16) as f64 / 65536.0 * BLINK,
}
}
pub fn draw_critter(ctx: &mut dyn RenderContext, rect: Rect, view: &CritterView) {
draw_critter_motion(ctx, rect, view, &CritterMotion { reduced: true, ..Default::default() });
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum CritterState {
#[default]
Idle,
Working,
Thinking,
Attention,
Off,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct CritterMotion {
pub state: CritterState,
pub t: f64,
pub look_at: Option<(f64, f64)>,
pub reduced: bool,
}
fn ease(x: f64) -> f64 {
let x = x.clamp(0.0, 1.0);
x * x * (3.0 - 2.0 * x)
}
fn swing(t: f64, period: f64) -> f64 {
let f = (t / period).rem_euclid(1.0);
if f < 0.5 { ease(f * 2.0) } else { ease((1.0 - f) * 2.0) }
}
pub const BLINK: f64 = 7.5;
fn blink(t: f64) -> f64 {
let f = (t / BLINK).rem_euclid(1.0);
if f < 0.92 {
1.0
} else if f < 0.95 {
1.0 - 0.9 * ease((f - 0.92) / 0.03)
} else {
0.1 + 0.9 * ease((f - 0.95) / 0.05)
}
}
fn roll(t: f64, period: f64) -> (f64, f64) {
const K: [(f64, f64); 5] = [(0.0, 0.0), (3.0, -2.0), (0.0, -3.0), (-3.0, -1.0), (0.0, 0.0)];
let f = (t / period).rem_euclid(1.0) * 4.0;
let i = (f as usize).min(3);
let k = ease(f - i as f64);
(K[i].0 + (K[i + 1].0 - K[i].0) * k, K[i].1 + (K[i + 1].1 - K[i].1) * k)
}
pub fn critter_look(center: (f64, f64), pointer: (f64, f64)) -> (f64, f64) {
let (dx, dy) = (pointer.0 - center.0, pointer.1 - center.1);
let d = dx.hypot(dy).max(1.0);
let k = (d / 300.0).min(1.0) * 4.0;
(dx / d * k, dy / d * k)
}
pub fn critter_next_frame(state: CritterState, t: f64, reduced: bool) -> Option<f64> {
const FAST: f64 = 1.0 / 30.0;
if reduced {
return None;
}
match state {
CritterState::Off => None,
CritterState::Idle => {
let f = (t / BLINK).rem_euclid(1.0) * BLINK;
let start = 0.92 * BLINK;
if f >= start { Some(FAST) } else { Some((start - f).max(FAST)) }
}
_ => Some(FAST),
}
}
pub fn draw_critter_motion(ctx: &mut dyn RenderContext, rect: Rect, view: &CritterView, m: &CritterMotion) {
use CritterState::*;
let st = if view.off { Off } else { m.state };
let t = if m.reduced { 0.0 } else { m.t + view.phase };
let s = rect.width.min(rect.height) / 64.0;
let ox = rect.x + (rect.width - 64.0 * s) / 2.0;
let oy = rect.y + (rect.height - 64.0 * s) / 2.0;
let about = |ctx: &mut dyn RenderContext, cx: f64, cy: f64, f: &dyn Fn(&mut dyn RenderContext)| {
ctx.translate(cx, cy);
f(ctx);
ctx.translate(-cx, -cy);
};
ctx.save();
ctx.translate(ox, oy);
ctx.scale(s, s);
if st == Off {
ctx.set_global_alpha(0.5);
}
if !m.reduced {
match st {
Working => {
let k = 1.0 + 0.05 * swing(t, 1.3);
about(ctx, 32.0, 32.0, &|c| c.scale(k, k));
}
Attention => ctx.translate(0.0, -3.0 * swing(t, 0.9)),
_ => {}
}
}
ctx.save();
if !m.reduced {
match st {
Idle => {
let p = swing(t, 5.0);
about(ctx, 32.0, 32.0, &|c| c.scale(1.0 + 0.03 * p, 1.0 - 0.02 * p));
}
Working => {
let p = swing(t, 1.4);
ctx.translate(0.0, -2.0 * p);
about(ctx, 32.0, 32.0, &|c| c.rotate((-4.0 + 8.0 * p).to_radians()));
}
Thinking => ctx.translate(0.0, -3.0 * swing(t, 2.4)),
_ => {}
}
}
emit_svg_path(ctx, CRITTER_SHAPES[view.shape % CRITTER_SHAPES.len()]);
ctx.set_fill_color(view.color);
ctx.fill();
let look = m.look_at.map(|p| critter_look((rect.x + rect.width / 2.0, rect.y + rect.height / 2.0), p));
let (mut ex, mut ey) = if st == Off || m.reduced { (0.0, 0.0) } else { look.unwrap_or((0.0, 0.0)) };
if !m.reduced {
match st {
Working => {
let r = roll(t, 2.2);
ex += r.0;
ey += r.1;
}
Thinking => {
let r = roll(t, 3.0);
ex += r.0;
ey += r.1;
}
_ => {}
}
}
let (sy, tilt) = match st {
Idle => (if m.reduced { 1.0 } else { blink(t) }, 0.0),
Off => (0.12, 0.0),
_ => (1.0, (-12.0f64).to_radians()),
};
ctx.set_fill_color(tokens::EYE);
for cx in [23.0, 41.0] {
ctx.begin_path();
ctx.ellipse(cx + ex, view.eye_y + ey, 4.0, 6.0 * sy, tilt, 0.0, std::f64::consts::TAU);
ctx.fill();
}
ctx.restore();
match st {
Thinking => {
ctx.set_fill_color(tokens::THOUGHT);
for (i, (cx, cy, r)) in [(50.0, 8.0, 2.4), (56.0, 3.0, 1.8)].into_iter().enumerate() {
let a = if m.reduced { 1.0 } else { 0.2 + 0.8 * swing(t - 0.5 * i as f64, 1.6) };
ctx.set_global_alpha(a);
ctx.begin_path();
ctx.arc(cx, cy, r, 0.0, std::f64::consts::TAU);
ctx.fill();
}
}
Attention => {
let f = if m.reduced { 0.0 } else { (t / 1.4).rem_euclid(1.0) };
let e = 1.0 - (1.0 - f).powi(3); ctx.set_global_alpha(0.9 * (1.0 - e));
ctx.set_stroke_color(tokens::ATTENTION);
ctx.set_stroke_width(2.5);
ctx.begin_path();
ctx.arc(32.0, 32.0, 30.0 * (1.0 + 0.25 * e), 0.0, std::f64::consts::TAU);
ctx.stroke();
}
_ => {}
}
ctx.restore();
}
pub fn register_critter(coord: &mut InputCoordinator, id: impl Into<WidgetId>, rect: Rect, layer: &LayerId) {
coord.register_atomic(id, WidgetKind::Button, rect, Sense::CLICK, layer);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn view_is_stable_and_in_family() {
let a = critter_view("pty:node-a-1", CritterFamily::Agent);
assert_eq!(a, critter_view("pty:node-a-1", CritterFamily::Agent));
assert!(CritterFamily::Agent.shapes().contains(&a.shape));
assert!(CritterFamily::Group.shapes().contains(&critter_view("x", CritterFamily::Group).shape));
assert!((26.0..=30.0).contains(&a.eye_y));
}
#[test]
fn motion_keyframes_and_cadence() {
assert_eq!(blink(1.0), 1.0);
assert!((blink(0.95 * BLINK) - 0.1).abs() < 1e-9);
assert!((critter_next_frame(CritterState::Idle, 1.0, false).unwrap() - (0.92 * BLINK - 1.0)).abs() < 1e-9);
assert_eq!(critter_next_frame(CritterState::Idle, 0.93 * BLINK, false), Some(1.0 / 30.0));
assert_eq!(critter_next_frame(CritterState::Off, 1.0, false), None);
assert_eq!(critter_next_frame(CritterState::Working, 1.0, true), None);
assert_eq!(roll(0.0, 2.0), (0.0, 0.0));
assert_eq!(roll(1.0, 2.0), (0.0, -3.0));
let l = critter_look((0.0, 0.0), (900.0, 0.0));
assert!((l.0 - 4.0).abs() < 1e-9 && l.1.abs() < 1e-9);
}
}