use crate::svg::math::abs_f32;
#[derive(Copy, Clone, Debug)]
pub struct SpringConfig {
pub stiffness: f32,
pub damping: f32,
pub mass: f32,
pub rest_eps: f32,
}
impl SpringConfig {
pub const GENTLE: Self = Self {
stiffness: 120.0,
damping: 18.0,
mass: 1.0,
rest_eps: 0.05,
};
pub const DEFAULT: Self = Self {
stiffness: 220.0,
damping: 24.0,
mass: 1.0,
rest_eps: 0.05,
};
pub const SNAPPY: Self = Self {
stiffness: 380.0,
damping: 32.0,
mass: 1.0,
rest_eps: 0.05,
};
pub const BOUNCY: Self = Self {
stiffness: 260.0,
damping: 14.0,
mass: 1.0,
rest_eps: 0.05,
};
pub const STIFF: Self = Self {
stiffness: 500.0,
damping: 42.0,
mass: 1.0,
rest_eps: 0.05,
};
#[inline(always)]
pub const fn new(stiffness: f32, damping: f32, mass: f32) -> Self {
Self {
stiffness,
damping,
mass,
rest_eps: 0.05,
}
}
}
impl Default for SpringConfig {
#[inline(always)]
fn default() -> Self {
Self::DEFAULT
}
}
#[derive(Copy, Clone, Debug)]
pub struct Spring {
value: f32,
velocity: f32,
target: f32,
config: SpringConfig,
settled: bool,
}
impl Spring {
#[inline(always)]
pub const fn new(initial: f32, config: SpringConfig) -> Self {
Self {
value: initial,
velocity: 0.0,
target: initial,
config,
settled: true,
}
}
#[inline]
pub fn from_gesture_velocity(
initial: f32,
target: f32,
velocity_px_per_sec: f32,
config: SpringConfig,
) -> Self {
let mut sp = Self {
value: initial,
velocity: velocity_px_per_sec,
target,
config,
settled: false,
};
let eps = config.rest_eps.max(1e-4);
if abs_f32(initial - target) <= eps && abs_f32(velocity_px_per_sec) <= eps * 10.0 {
sp.value = target;
sp.velocity = 0.0;
sp.settled = true;
}
sp
}
#[inline(always)]
pub const fn value(&self) -> f32 {
self.value
}
#[inline(always)]
pub fn value_i32(&self) -> i32 {
if self.value >= 0.0 {
(self.value + 0.5) as i32
} else {
(self.value - 0.5) as i32
}
}
#[inline(always)]
pub const fn velocity(&self) -> f32 {
self.velocity
}
#[inline(always)]
pub const fn target(&self) -> f32 {
self.target
}
#[inline(always)]
pub const fn is_settled(&self) -> bool {
self.settled
}
#[inline(always)]
pub const fn is_running(&self) -> bool {
!self.settled
}
#[inline]
pub fn animate_to(&mut self, target: f32) {
if abs_f32(target - self.target) <= 1e-6 && self.settled {
return;
}
self.target = target;
self.settled = false;
}
#[inline]
pub fn impulse(&mut self, delta_velocity: f32) {
self.velocity += delta_velocity;
if abs_f32(self.velocity) > self.config.rest_eps {
self.settled = false;
}
}
#[inline]
pub fn set_velocity(&mut self, velocity: f32) {
self.velocity = velocity;
if abs_f32(self.velocity) > self.config.rest_eps {
self.settled = false;
}
}
#[inline(always)]
pub fn set_config(&mut self, config: SpringConfig) {
self.config = config;
}
#[inline(always)]
pub fn jump_to(&mut self, value: f32) {
self.value = value;
self.target = value;
self.velocity = 0.0;
self.settled = true;
}
pub fn step(&mut self, dt_ms: u16) -> f32 {
if self.settled || dt_ms == 0 {
return self.value;
}
let total_s = (dt_ms.min(100) as f32) * 0.001;
let max_sub_s = 0.004f32;
let sub_steps = ((total_s / max_sub_s) as u32).max(1);
let h = total_s / (sub_steps as f32);
let inv_m = 1.0 / self.config.mass.max(0.01);
let k = self.config.stiffness.max(1.0);
let c = self.config.damping.max(0.1);
for _ in 0..sub_steps {
let disp = self.value - self.target;
let accel = (-k * disp - c * self.velocity) * inv_m;
self.velocity += accel * h;
self.value += self.velocity * h;
}
let eps = self.config.rest_eps.max(1e-4);
if abs_f32(self.value - self.target) <= eps && abs_f32(self.velocity) <= eps * 10.0 {
self.value = self.target;
self.velocity = 0.0;
self.settled = true;
}
self.value
}
}
#[inline]
pub fn rubberband(distance: f32, dimension: f32, constant: f32) -> f32 {
let dim = dimension.max(1.0);
let c = constant.clamp(0.0, 1.0);
if c == 0.0 || distance == 0.0 {
return 0.0;
}
let abs_d = abs_f32(distance);
(distance * dim * c) / (dim + c * abs_d)
}
#[inline]
pub fn rubberband_i16(distance: i16, dimension: i16, constant_q8: u16) -> i16 {
let d = distance as i64;
let dim = (dimension.max(1)) as i64;
let c = (constant_q8.min(256)) as i64;
if d == 0 || c == 0 {
return 0;
}
let abs_d = d.abs();
let num = d * dim * c;
let den = (dim << 8) + c * abs_d;
(num / den.max(1)).clamp(i16::MIN as i64, i16::MAX as i64) as i16
}