From 36a05d22a5036da128944d83956550648d9d3197 Mon Sep 17 00:00:00 2001 From: AlexKnauth Date: Tue, 5 Dec 2023 23:20:32 -0500 Subject: [PATCH] Timer trait: add current_split_index --- crates/livesplit-auto-splitting/src/timer.rs | 6 ++++++ crates/livesplit-auto-splitting/tests/sandboxing.rs | 3 +++ src/auto_splitting/mod.rs | 4 ++++ src/event.rs | 12 ++++++++++++ 4 files changed, 25 insertions(+) diff --git a/crates/livesplit-auto-splitting/src/timer.rs b/crates/livesplit-auto-splitting/src/timer.rs index d5052a5e..674acb96 100644 --- a/crates/livesplit-auto-splitting/src/timer.rs +++ b/crates/livesplit-auto-splitting/src/timer.rs @@ -30,6 +30,12 @@ pub trait Timer { fn undo_split(&mut self); /// Resets the timer. fn reset(&mut self); + /// Accesses the index of the split the attempt is currently on. If there's + /// no attempt in progress, `None` is returned instead. This returns an + /// index that is equal to the amount of segments when the attempt is + /// finished, but has not been reset. So you need to be careful when using + /// this value for indexing. + fn current_split_index(&self) -> Option; /// Sets the game time. fn set_game_time(&mut self, time: time::Duration); /// Pauses the game time. This does not pause the timer, only the automatic diff --git a/crates/livesplit-auto-splitting/tests/sandboxing.rs b/crates/livesplit-auto-splitting/tests/sandboxing.rs index 2ce5bf5e..8d4f97a6 100644 --- a/crates/livesplit-auto-splitting/tests/sandboxing.rs +++ b/crates/livesplit-auto-splitting/tests/sandboxing.rs @@ -19,6 +19,9 @@ impl Timer for DummyTimer { fn skip_split(&mut self) {} fn undo_split(&mut self) {} fn reset(&mut self) {} + fn current_split_index(&self) -> Option { + None + } fn set_game_time(&mut self, _time: time::Duration) {} fn pause_game_time(&mut self) {} fn resume_game_time(&mut self) {} diff --git a/src/auto_splitting/mod.rs b/src/auto_splitting/mod.rs index 04c6f55e..175090e7 100644 --- a/src/auto_splitting/mod.rs +++ b/src/auto_splitting/mod.rs @@ -749,6 +749,10 @@ impl AutoSplitTimer for Timer { self.0.reset(None) } + fn current_split_index(&self) -> Option { + self.0.current_split_index() + } + fn set_game_time(&mut self, time: time::Duration) { self.0.set_game_time(time.into()); } diff --git a/src/event.rs b/src/event.rs index e8c44fa4..c3d11be0 100644 --- a/src/event.rs +++ b/src/event.rs @@ -88,6 +88,12 @@ pub trait Sink { pub trait TimerQuery { /// Returns the current Timer Phase. fn current_phase(&self) -> TimerPhase; + /// Accesses the index of the split the attempt is currently on. If there's + /// no attempt in progress, `None` is returned instead. This returns an + /// index that is equal to the amount of segments when the attempt is + /// finished, but has not been reset. So you need to be careful when using + /// this value for indexing. + fn current_split_index(&self) -> Option; } #[cfg(feature = "std")] @@ -166,6 +172,9 @@ impl TimerQuery for crate::SharedTimer { fn current_phase(&self) -> TimerPhase { self.read().unwrap().current_phase() } + fn current_split_index(&self) -> Option { + self.read().unwrap().current_split_index() + } } impl Sink for Arc { @@ -242,4 +251,7 @@ impl TimerQuery for Arc { fn current_phase(&self) -> TimerPhase { TimerQuery::current_phase(&**self) } + fn current_split_index(&self) -> Option { + TimerQuery::current_split_index(&**self) + } }