From 8fae72f67707924118f965b9e14b5838e41cc3ad Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 5 Dec 2021 03:28:56 -0500 Subject: implement speeding up and slowing down --- src/bin/ttyplay/event.rs | 2 ++ src/bin/ttyplay/input.rs | 10 ++++++++++ src/bin/ttyplay/timer.rs | 20 ++++++++++++++++++-- 3 files changed, 30 insertions(+), 2 deletions(-) (limited to 'src') 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), @@ -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), -- cgit v1.2.3-54-g00ecf