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
use epaint::ahash::AHashMap;

use crate::{emath::remap_clamp, Id, InputState};

#[derive(Clone, Default)]
pub(crate) struct AnimationManager {
    bools: AHashMap<Id, BoolAnim>,
}

#[derive(Clone, Debug)]
struct BoolAnim {
    value: bool,
    /// when did `value` last toggle?
    toggle_time: f64,
}

impl AnimationManager {
    /// See `Context::animate_bool` for documentation
    pub fn animate_bool(
        &mut self,
        input: &InputState,
        animation_time: f32,
        id: Id,
        value: bool,
    ) -> f32 {
        match self.bools.get_mut(&id) {
            None => {
                self.bools.insert(
                    id,
                    BoolAnim {
                        value,
                        toggle_time: -f64::INFINITY, // long time ago
                    },
                );
                if value {
                    1.0
                } else {
                    0.0
                }
            }
            Some(anim) => {
                if anim.value != value {
                    anim.value = value;
                    anim.toggle_time = input.time;
                }

                let time_since_toggle = (input.time - anim.toggle_time) as f32;

                // On the frame we toggle we don't want to return the old value,
                // so we extrapolate forwards:
                let time_since_toggle = time_since_toggle + input.predicted_dt;

                if value {
                    remap_clamp(time_since_toggle, 0.0..=animation_time, 0.0..=1.0)
                } else {
                    remap_clamp(time_since_toggle, 0.0..=animation_time, 1.0..=0.0)
                }
            }
        }
    }
}