~ruther/map-led-strip

ref: b7e43afe1dd99a6454be3c355d038bd228571ba2 map-led-strip/src/animations/animation_storage.rs -rw-r--r-- 1.4 KiB
b7e43afe — František Boháček feat: add map clear 1 year, 9 months ago
                                                                                
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
use alloc::boxed::Box;
use crate::animations::animation::{Animation, AnimationError};
use crate::animations::animation_step::AnimationStep;
use crate::map::Map;

pub struct AnimationStorage {
    animation: Option<Box<dyn Animation>>,
}

impl AnimationStorage {
    pub fn new() -> Self {
        Self {
            animation: None
        }
    }

    pub fn animation<'a>(&'a mut self) -> Option<impl Animation + 'a> {
        if self.animation.is_none() {
            None
        } else {
            Some(StorageAnimation { storage: self })
        }
    }

    pub fn set_animation<T: Animation + 'static>(&mut self, animation: T) -> () {
        self.animation = Some(Box::new(animation));
    }

    pub fn remove_animation(&mut self) -> () {
        self.animation = None;
    }
}

struct StorageAnimation<'a> {
    storage: &'a mut AnimationStorage
}

impl<'a> Animation for StorageAnimation<'a> {
    fn is_started(&self) -> bool {
        self.storage.animation.as_ref().unwrap().is_started()
    }

    fn init(&mut self) -> Result<(), AnimationError> {
        self.storage.animation.as_mut().unwrap().init()
    }

    fn next(&mut self) -> Result<AnimationStep, AnimationError> {
        self.storage.animation.as_mut().unwrap().next()
    }

    fn apply(&mut self, map: &mut Map) -> Result<(), AnimationError> {
        self.storage.animation.as_mut().unwrap().apply(map)
    }
}
Do not follow this link