Skip to main content

ass_renderer/debug/player/
playback.rs

1#[cfg(not(feature = "nostd"))]
2use std::time::Instant;
3
4impl super::DebugPlayer {
5    pub fn set_cache_enabled(&mut self, enabled: bool) {
6        self.cache_enabled = enabled;
7        if !enabled {
8            self.frame_cache.clear();
9        }
10    }
11
12    pub fn set_max_cache_size(&mut self, size: usize) {
13        self.max_cache_size = size;
14        // Trim cache if needed
15        while self.frame_cache.len() > size {
16            // Remove oldest entries (not optimal but simple)
17            if let Some(&min_key) = self.frame_cache.keys().min() {
18                self.frame_cache.remove(&min_key);
19            }
20        }
21    }
22
23    pub fn play(&mut self) {
24        self.is_playing = true;
25        self.playback_start_instant = Some(Instant::now());
26        self.playback_start_time_ms = self.current_time_ms;
27        self.accumulated_time_ms = 0.0;
28        println!(
29            "▶️  Playback started at {current_time}ms",
30            current_time = self.current_time_ms
31        );
32    }
33
34    pub fn pause(&mut self) {
35        if self.is_playing {
36            // Save accumulated time
37            if let Some(start) = self.playback_start_instant {
38                let elapsed = start.elapsed().as_secs_f32() * 1000.0 * self.playback_speed;
39                self.accumulated_time_ms += elapsed;
40            }
41        }
42        self.is_playing = false;
43        self.playback_start_instant = None;
44        println!(
45            "⏸️  Playback paused at {current_time}ms",
46            current_time = self.current_time_ms
47        );
48    }
49
50    pub fn stop(&mut self) {
51        self.is_playing = false;
52        self.current_time_ms = self.start_time_ms;
53        self.playback_start_instant = None;
54        self.accumulated_time_ms = 0.0;
55        println!("⏹️  Playback stopped");
56    }
57
58    pub fn seek(&mut self, time_ms: u32) {
59        self.current_time_ms = time_ms.min(self.end_time_ms);
60        if self.is_playing {
61            self.playback_start_instant = Some(Instant::now());
62            self.playback_start_time_ms = self.current_time_ms;
63            self.accumulated_time_ms = 0.0;
64        }
65        println!(
66            "⏩ Seeked to {current_time}ms",
67            current_time = self.current_time_ms
68        );
69    }
70
71    pub fn set_speed(&mut self, speed: f32) {
72        // If playing, update the accumulated time before changing speed
73        if self.is_playing {
74            if let Some(start) = self.playback_start_instant {
75                let elapsed = start.elapsed().as_secs_f32() * 1000.0 * self.playback_speed;
76                self.accumulated_time_ms += elapsed;
77                self.playback_start_instant = Some(Instant::now());
78                self.playback_start_time_ms = self.current_time_ms;
79            }
80        }
81        self.playback_speed = speed.clamp(0.1, 10.0);
82        println!("🎚️  Playback speed: {speed}x", speed = self.playback_speed);
83    }
84
85    pub fn step_forward(&mut self) {
86        self.current_time_ms =
87            (self.current_time_ms + self.frame_interval_ms).min(self.end_time_ms);
88        if self.is_playing {
89            self.playback_start_instant = Some(Instant::now());
90            self.playback_start_time_ms = self.current_time_ms;
91            self.accumulated_time_ms = 0.0;
92        }
93        println!(
94            "⏭️  Step forward to {current_time}ms",
95            current_time = self.current_time_ms
96        );
97    }
98
99    pub fn step_backward(&mut self) {
100        self.current_time_ms = self.current_time_ms.saturating_sub(self.frame_interval_ms);
101        if self.is_playing {
102            self.playback_start_instant = Some(Instant::now());
103            self.playback_start_time_ms = self.current_time_ms;
104            self.accumulated_time_ms = 0.0;
105        }
106        println!(
107            "⏮️  Step backward to {current_time}ms",
108            current_time = self.current_time_ms
109        );
110    }
111
112    pub fn toggle_stats(&mut self) {
113        self.show_stats = !self.show_stats;
114        println!(
115            "📊 Stats display: {}",
116            if self.show_stats { "ON" } else { "OFF" }
117        );
118    }
119
120    pub fn toggle_frame_saving(&mut self) {
121        self.save_frames = !self.save_frames;
122        if self.save_frames {
123            #[cfg(not(feature = "nostd"))]
124            std::fs::create_dir_all(&self.output_dir).ok();
125            println!(
126                "💾 Frame saving: ON (to {output_dir})",
127                output_dir = self.output_dir
128            );
129        } else {
130            println!("💾 Frame saving: OFF");
131        }
132    }
133
134    pub fn set_loop(&mut self, enable: bool) {
135        self.loop_playback = enable;
136        println!(
137            "🔁 Loop playback: {status}",
138            status = if enable { "ON" } else { "OFF" }
139        );
140    }
141
142    pub fn set_output_dir(&mut self, dir: &str) {
143        self.output_dir = dir.to_string();
144        println!("📁 Output directory set to: {dir}");
145    }
146
147    pub fn is_playing(&self) -> bool {
148        self.is_playing
149    }
150
151    pub fn current_time(&self) -> u32 {
152        self.current_time_ms
153    }
154
155    pub fn toggle_loop(&mut self) {
156        self.loop_playback = !self.loop_playback;
157        let loop_status = if self.loop_playback { "ON" } else { "OFF" };
158        println!("🔁 Loop: {loop_status}");
159    }
160
161    pub fn increase_speed(&mut self) {
162        let new_speed = (self.playback_speed * 1.5).min(10.0);
163        self.set_speed(new_speed);
164    }
165
166    pub fn decrease_speed(&mut self) {
167        let new_speed = (self.playback_speed / 1.5).max(0.1);
168        self.set_speed(new_speed);
169    }
170}