ak_vis/ui/playback/
state.rs

1#[derive(Clone, Copy, Debug, PartialEq)]
2pub struct PlaybackSpeedPreset {
3    pub label: &'static str,
4    pub frames_per_second: f32,
5}
6
7const PLAYBACK_SPEED_PRESETS: [PlaybackSpeedPreset; 3] = [
8    PlaybackSpeedPreset {
9        label: "1x",
10        frames_per_second: 2.0,
11    },
12    PlaybackSpeedPreset {
13        label: "2x",
14        frames_per_second: 5.0,
15    },
16    PlaybackSpeedPreset {
17        label: "4x",
18        frames_per_second: 10.0,
19    },
20];
21
22#[derive(bevy::prelude::Resource, Clone, Debug, PartialEq)]
23pub struct PlaybackState {
24    pub is_playing: bool,
25    pub speed_index: usize,
26    pub current_frame: usize,
27    pub total_frames: usize,
28    pub follow_tail: bool,
29}
30
31impl Default for PlaybackState {
32    fn default() -> Self {
33        Self {
34            is_playing: false,
35            speed_index: 1,
36            current_frame: 0,
37            total_frames: 0,
38            follow_tail: false,
39        }
40    }
41}
42
43impl PlaybackState {
44    pub fn speed_presets() -> &'static [PlaybackSpeedPreset] {
45        &PLAYBACK_SPEED_PRESETS
46    }
47
48    pub fn current_speed(&self) -> PlaybackSpeedPreset {
49        PLAYBACK_SPEED_PRESETS
50            .get(self.speed_index)
51            .copied()
52            .unwrap_or(PLAYBACK_SPEED_PRESETS[1])
53    }
54
55    pub fn set_speed_index(&mut self, index: usize) {
56        if index < PLAYBACK_SPEED_PRESETS.len() {
57            self.speed_index = index;
58        }
59    }
60
61    pub fn scrubber_fraction(&self) -> f32 {
62        if self.total_frames <= 1 {
63            return 0.0;
64        }
65
66        self.current_frame as f32 / (self.total_frames.saturating_sub(1)) as f32
67    }
68}