#[derive(Debug, Clone, Copy, PartialEq)]
pub enum OverlayAnimation {
None,
FadeIn {
start_time: f64,
duration: f32,
},
FadeOut {
start_time: f64,
duration: f32,
},
Pulse {
start_time: f64,
period: f32,
},
}
impl Default for OverlayAnimation {
fn default() -> Self {
OverlayAnimation::None
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum OverlayEasing {
#[default]
Linear,
EaseIn,
EaseOut,
EaseInOut,
Pulse,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum RepeatMode {
#[default]
Once,
Loop,
PingPong,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct AnimTrack<T: Copy> {
pub start_time: f64,
pub duration: f32,
pub from: T,
pub to: T,
pub easing: OverlayEasing,
pub repeat: RepeatMode,
}
impl<T: Copy + Default> Default for AnimTrack<T> {
fn default() -> Self {
Self {
start_time: 0.0,
duration: 1.0,
from: T::default(),
to: T::default(),
easing: OverlayEasing::Linear,
repeat: RepeatMode::Once,
}
}
}
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct OverlayAnimations {
pub opacity: Option<AnimTrack<f32>>,
pub position: Option<AnimTrack<[f32; 2]>>,
pub size: Option<AnimTrack<[f32; 2]>>,
pub fill: Option<AnimTrack<[f32; 4]>>,
pub border: Option<AnimTrack<[f32; 4]>>,
pub rotation: Option<AnimTrack<f32>>,
pub opacity_path: Option<PathTrack<f32>>,
pub position_path: Option<PathTrack<[f32; 2]>>,
pub size_path: Option<PathTrack<[f32; 2]>>,
pub fill_path: Option<PathTrack<[f32; 4]>>,
pub border_path: Option<PathTrack<[f32; 4]>>,
pub rotation_path: Option<PathTrack<f32>>,
}
impl OverlayAnimations {
pub fn with_opacity(mut self, track: AnimTrack<f32>) -> Self {
self.opacity = Some(track);
self
}
pub fn with_position(mut self, track: AnimTrack<[f32; 2]>) -> Self {
self.position = Some(track);
self
}
pub fn with_size(mut self, track: AnimTrack<[f32; 2]>) -> Self {
self.size = Some(track);
self
}
pub fn with_fill(mut self, track: AnimTrack<[f32; 4]>) -> Self {
self.fill = Some(track);
self
}
pub fn with_border(mut self, track: AnimTrack<[f32; 4]>) -> Self {
self.border = Some(track);
self
}
pub fn with_rotation(mut self, track: AnimTrack<f32>) -> Self {
self.rotation = Some(track);
self
}
pub fn with_opacity_path(mut self, track: PathTrack<f32>) -> Self {
self.opacity_path = Some(track);
self
}
pub fn with_position_path(mut self, track: PathTrack<[f32; 2]>) -> Self {
self.position_path = Some(track);
self
}
pub fn with_size_path(mut self, track: PathTrack<[f32; 2]>) -> Self {
self.size_path = Some(track);
self
}
pub fn with_fill_path(mut self, track: PathTrack<[f32; 4]>) -> Self {
self.fill_path = Some(track);
self
}
pub fn with_border_path(mut self, track: PathTrack<[f32; 4]>) -> Self {
self.border_path = Some(track);
self
}
pub fn with_rotation_path(mut self, track: PathTrack<f32>) -> Self {
self.rotation_path = Some(track);
self
}
}
pub trait LerpAnim: Copy {
fn lerp(from: Self, to: Self, t: f32) -> Self;
}
impl LerpAnim for f32 {
fn lerp(from: Self, to: Self, t: f32) -> Self {
from * (1.0 - t) + to * t
}
}
impl LerpAnim for [f32; 2] {
fn lerp(from: Self, to: Self, t: f32) -> Self {
[f32::lerp(from[0], to[0], t), f32::lerp(from[1], to[1], t)]
}
}
impl LerpAnim for [f32; 4] {
fn lerp(from: Self, to: Self, t: f32) -> Self {
[
f32::lerp(from[0], to[0], t),
f32::lerp(from[1], to[1], t),
f32::lerp(from[2], to[2], t),
f32::lerp(from[3], to[3], t),
]
}
}
impl<T: Copy + LerpAnim> AnimTrack<T> {
pub fn sample(&self, time: f64) -> T {
if self.duration <= 0.0 {
return self.to;
}
let raw = ((time - self.start_time) as f32) / self.duration;
let phase = resolve_phase(raw, self.repeat);
let t = apply_easing(phase, self.easing);
T::lerp(self.from, self.to, t)
}
}
fn resolve_phase(raw: f32, repeat: RepeatMode) -> f32 {
match repeat {
RepeatMode::Once => raw.clamp(0.0, 1.0),
RepeatMode::Loop => {
let f = raw - raw.floor();
if f < 0.0 { f + 1.0 } else { f }
}
RepeatMode::PingPong => {
let two = (raw * 0.5).floor() * 2.0;
let r = raw - two;
if r > 1.0 { 2.0 - r } else { r }
}
}
}
fn apply_easing(phase: f32, easing: OverlayEasing) -> f32 {
match easing {
OverlayEasing::Linear => phase,
OverlayEasing::EaseIn => phase * phase,
OverlayEasing::EaseOut => {
let inv = 1.0 - phase;
1.0 - inv * inv
}
OverlayEasing::EaseInOut => phase * phase * (3.0 - 2.0 * phase),
OverlayEasing::Pulse => (phase * std::f32::consts::PI).sin(),
}
}
#[derive(Clone)]
pub struct PathTrack<T: Copy + LerpAnim> {
pub start_time: f64,
pub duration: f32,
pub easing: OverlayEasing,
pub repeat: RepeatMode,
pub path: std::sync::Arc<dyn Fn(f32) -> T + Send + Sync>,
}
impl<T: Copy + LerpAnim> std::fmt::Debug for PathTrack<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("PathTrack")
.field("start_time", &self.start_time)
.field("duration", &self.duration)
.field("easing", &self.easing)
.field("repeat", &self.repeat)
.field("path", &"<closure>")
.finish()
}
}
impl<T: Copy + LerpAnim> PathTrack<T> {
pub fn new(
start_time: f64,
duration: f32,
path: impl Fn(f32) -> T + Send + Sync + 'static,
) -> Self {
Self {
start_time,
duration,
easing: OverlayEasing::Linear,
repeat: RepeatMode::Once,
path: std::sync::Arc::new(path),
}
}
pub fn with_easing(mut self, easing: OverlayEasing) -> Self {
self.easing = easing;
self
}
pub fn with_repeat(mut self, repeat: RepeatMode) -> Self {
self.repeat = repeat;
self
}
pub fn sample(&self, time: f64) -> T {
if self.duration <= 0.0 {
return (self.path)(1.0);
}
let raw = ((time - self.start_time) as f32) / self.duration;
let phase = resolve_phase(raw, self.repeat);
let t = apply_easing(phase, self.easing);
(self.path)(t)
}
}
impl PathTrack<[f32; 2]> {
pub fn bezier(start_time: f64, duration: f32, control_points: [[f32; 2]; 4]) -> Self {
let [p0, p1, p2, p3] = control_points;
Self::new(start_time, duration, move |t| {
let one_t = 1.0 - t;
let w0 = one_t * one_t * one_t;
let w1 = 3.0 * one_t * one_t * t;
let w2 = 3.0 * one_t * t * t;
let w3 = t * t * t;
[
w0 * p0[0] + w1 * p1[0] + w2 * p2[0] + w3 * p3[0],
w0 * p0[1] + w1 * p1[1] + w2 * p2[1] + w3 * p3[1],
]
})
}
pub fn polyline(start_time: f64, duration: f32, points: Vec<[f32; 2]>) -> Self {
Self::new(start_time, duration, move |t| {
let n = points.len();
if n == 0 {
return [0.0, 0.0];
}
if n == 1 {
return points[0];
}
let seg_count = n - 1;
let scaled = t.clamp(0.0, 1.0) * seg_count as f32;
let seg = (scaled as usize).min(seg_count - 1);
let local = scaled - seg as f32;
let a = points[seg];
let b = points[seg + 1];
[
a[0] * (1.0 - local) + b[0] * local,
a[1] * (1.0 - local) + b[1] * local,
]
})
}
}