Skip to main content

animate_core/
lib.rs

1pub mod spring;
2pub mod tween;
3pub mod types;
4
5use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
6
7pub use spring::*;
8pub use tween::easing;
9pub use tween::*;
10
11pub static FRAME_TIME: AtomicUsize = AtomicUsize::new(0);
12pub static LAST_DELTA: AtomicUsize = AtomicUsize::new(0);
13pub static IS_ANIMATING: AtomicBool = AtomicBool::new(false);
14
15pub trait Animate {
16    type Value;
17    fn update(&mut self);
18    fn get(&self) -> &Self::Value;
19    fn set(&mut self, target: Self::Value);
20    fn target(&self) -> &Self::Value;
21}
22
23pub trait Mode {}
24
25#[derive(Debug, Clone, Copy, Default)]
26pub struct Once;
27impl Mode for Once {}
28
29#[derive(Debug, Clone, Copy, Default)]
30pub struct Cycle;
31impl Mode for Cycle {}
32
33#[derive(Debug, Clone, Copy, Default)]
34pub struct Alternate;
35impl Mode for Alternate {}
36
37#[inline(always)]
38pub fn tick(delta: usize) {
39    FRAME_TIME.fetch_add(delta, Ordering::Relaxed);
40    LAST_DELTA.store(delta, Ordering::Relaxed);
41    IS_ANIMATING.store(false, Ordering::Relaxed);
42}
43
44pub fn frame() -> usize {
45    FRAME_TIME.load(Ordering::Relaxed)
46}
47
48#[inline]
49pub fn delta() -> f64 {
50    let delta_ms = crate::LAST_DELTA.load(Ordering::Relaxed);
51    (delta_ms as f64 / 1000.0).max(f64::MIN_POSITIVE)
52}
53
54pub fn is_animating() -> bool {
55    IS_ANIMATING.load(Ordering::Relaxed)
56}