diff options
author | Jesse Luehrs <doy@tozt.net> | 2021-12-05 03:28:56 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2021-12-05 03:28:56 -0500 |
commit | 8fae72f67707924118f965b9e14b5838e41cc3ad (patch) | |
tree | e0f547da9e2a249d672d0ed3ec5fc4ce4f6cde7f | |
parent | 84860b34b93047821584ca21824cd0fb5f9b3f24 (diff) | |
download | ttyrec-bin-8fae72f67707924118f965b9e14b5838e41cc3ad.tar.gz ttyrec-bin-8fae72f67707924118f965b9e14b5838e41cc3ad.zip |
implement speeding up and slowing down
-rw-r--r-- | src/bin/ttyplay/event.rs | 2 | ||||
-rw-r--r-- | src/bin/ttyplay/input.rs | 10 | ||||
-rw-r--r-- | src/bin/ttyplay/timer.rs | 20 |
3 files changed, 30 insertions, 2 deletions
diff --git a/src/bin/ttyplay/event.rs b/src/bin/ttyplay/event.rs index f3bbc62..38dcb8a 100644 --- a/src/bin/ttyplay/event.rs +++ b/src/bin/ttyplay/event.rs @@ -13,6 +13,8 @@ pub enum TimerAction { LastFrame, NextFrame, PreviousFrame, + SpeedUp, + SlowDown, Quit, } diff --git a/src/bin/ttyplay/input.rs b/src/bin/ttyplay/input.rs index d8c8e92..8c45b69 100644 --- a/src/bin/ttyplay/input.rs +++ b/src/bin/ttyplay/input.rs @@ -30,6 +30,16 @@ pub fn spawn_task( crate::event::TimerAction::Pause, ), textmode::Key::Ctrl(b'i') => crate::event::Event::ToggleUi, + textmode::Key::Char('=' | '+') => { + crate::event::Event::TimerAction( + crate::event::TimerAction::SpeedUp, + ) + } + textmode::Key::Char('_' | '-') => { + crate::event::Event::TimerAction( + crate::event::TimerAction::SlowDown, + ) + } _ => continue, }; event_w.send(event).await.unwrap(); diff --git a/src/bin/ttyplay/timer.rs b/src/bin/ttyplay/timer.rs index 7be5245..db85001 100644 --- a/src/bin/ttyplay/timer.rs +++ b/src/bin/ttyplay/timer.rs @@ -17,6 +17,7 @@ pub fn spawn_task( None }; let mut force_update_time = false; + let mut playback_ratio = 16; loop { enum Res { Wait(Option<vt100::Screen>), @@ -34,7 +35,7 @@ pub fn spawn_task( frames.lock_arc().await.get(idx).unwrap().clone(); if force_update_time { let now = std::time::Instant::now(); - start_time = now - frame.delay() + start_time = now - frame.delay() * playback_ratio / 16 // give a bit of extra time before moving to the // next frame, otherwise backing up behind two // frames that are extremely close together @@ -48,7 +49,8 @@ pub fn spawn_task( std::future::pending::<()>().await; } else { async_std::task::sleep( - (start_time + frame.delay()) + (start_time + + frame.delay() * playback_ratio / 16) .saturating_duration_since( std::time::Instant::now(), ), @@ -111,6 +113,20 @@ pub fn spawn_task( idx = idx.saturating_sub(2); force_update_time = true; } + crate::event::TimerAction::SpeedUp => { + if playback_ratio > 1 { + playback_ratio /= 2; + let now = std::time::Instant::now(); + start_time = now - (now - start_time) / 2; + } + } + crate::event::TimerAction::SlowDown => { + if playback_ratio < 256 { + playback_ratio *= 2; + let now = std::time::Instant::now(); + start_time = now - (now - start_time) * 2; + } + } crate::event::TimerAction::Quit => break, }, Res::TimerAction(Err(e)) => panic!("{}", e), |