Skip to main content

rosu_storyboard/command/
command_loop.rs

1use std::{cell::RefCell, rc::Rc};
2
3use super::CommandTimelineGroup;
4
5/// Command loop of a [`Sprite`].
6///
7/// [`Sprite`]: crate::element::Sprite
8#[derive(Clone, Debug, PartialEq)]
9pub struct CommandLoop {
10    pub group: CommandTimelineGroup,
11    pub loop_start_time: f64,
12    pub total_iterations: u32,
13}
14
15impl CommandLoop {
16    /// Create a new [`CommandLoop`].
17    pub fn new(start_time: f64, repeat_count: u32) -> Self {
18        Self {
19            group: CommandTimelineGroup::default(),
20            loop_start_time: start_time,
21            total_iterations: repeat_count + 1,
22        }
23    }
24
25    pub fn start_time(&self) -> f64 {
26        self.loop_start_time + self.group.start_time()
27    }
28
29    pub fn end_time(&self) -> f64 {
30        self.start_time() + self.group.duration()
31    }
32}
33
34pub(crate) struct CommandLoopInternal {
35    pub group: Rc<RefCell<CommandTimelineGroup>>,
36    pub loop_start_time: f64,
37    pub total_iterations: u32,
38}
39
40impl From<CommandLoopInternal> for CommandLoop {
41    fn from(l: CommandLoopInternal) -> Self {
42        Self {
43            group: Rc::into_inner(l.group)
44                .expect("multiple strong references around")
45                .into_inner(),
46            loop_start_time: l.loop_start_time,
47            total_iterations: l.total_iterations,
48        }
49    }
50}
51
52impl CommandLoopInternal {
53    pub fn new(start_time: f64, repeat_count: u32) -> Self {
54        Self {
55            group: Rc::new(RefCell::new(CommandTimelineGroup::default())),
56            loop_start_time: start_time,
57            total_iterations: repeat_count + 1,
58        }
59    }
60}