summaryrefslogtreecommitdiffstats
path: root/examples/progress_indicators/src/easing.rs
blob: 104796596263056e6f2ec9d8239dd814c529ed5b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use flo_curves::bezier::Curve;
use flo_curves::*;
use lazy_static::lazy_static;

use std::borrow::Cow;

lazy_static! {
    pub static ref EXAMPLES: [Easing; 3] = [
        Easing::CubicBezier(Curve::from_points(
            Coord2(0.0, 0.0),
            (Coord2(0.05, 0.7), Coord2(0.1, 1.0)),
            Coord2(1.0, 1.0),
        )),
        Easing::CubicBezier(Curve::from_points(
            Coord2(0.0, 0.0),
            (Coord2(0.3, 0.0), Coord2(0.8, 0.15)),
            Coord2(1.0, 1.0),
        )),
        Easing::CubicBezier(Curve::from_points(
            Coord2(0.0, 0.0),
            (Coord2(0.2, 0.0), Coord2(0.0, 1.0)),
            Coord2(1.0, 1.0),
        ))
    ];
    pub static ref STANDARD: Easing = {
        Easing::CubicBezier(Curve::from_points(
            Coord2(0.0, 0.0),
            (Coord2(0.2, 0.0), Coord2(0.0, 1.0)),
            Coord2(1.0, 1.0),
        ))
    };
}

#[derive(Clone, Debug)]
pub enum Easing {
    BezierPath(Vec<Curve<Coord2>>),
    CubicBezier(Curve<Coord2>),
}

impl Easing {
    pub fn y_at_x(&self, x: f32) -> f32 {
        let x = x as f64;

        match self {
            Self::BezierPath(curves) => curves
                .iter()
                .find_map(|curve| {
                    (curve.start_point().0 <= x && curve.end_point().0 >= x)
                        .then(|| curve.point_at_pos(x).1 as f32)
                })
                .unwrap_or_default(),
            Self::CubicBezier(curve) => curve.point_at_pos(x).1 as f32,
        }
    }
}

impl<'a> From<Easing> for Cow<'a, Easing> {
    fn from(easing: Easing) -> Self {
        Cow::Owned(easing)
    }
}

impl<'a> From<&'a Easing> for Cow<'a, Easing> {
    fn from(easing: &'a Easing) -> Self {
        Cow::Borrowed(easing)
    }
}