Skip to content

Commit

Permalink
Timer trait: add current_split_index
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKnauth committed Jun 12, 2024
1 parent 5e48065 commit 36a05d2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions crates/livesplit-auto-splitting/src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize>;
/// 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
Expand Down
3 changes: 3 additions & 0 deletions crates/livesplit-auto-splitting/tests/sandboxing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize> {
None
}
fn set_game_time(&mut self, _time: time::Duration) {}
fn pause_game_time(&mut self) {}
fn resume_game_time(&mut self) {}
Expand Down
4 changes: 4 additions & 0 deletions src/auto_splitting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,10 @@ impl<E: event::Sink + TimerQuery> AutoSplitTimer for Timer<E> {
self.0.reset(None)
}

fn current_split_index(&self) -> Option<usize> {
self.0.current_split_index()
}

fn set_game_time(&mut self, time: time::Duration) {
self.0.set_game_time(time.into());
}
Expand Down
12 changes: 12 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize>;
}

#[cfg(feature = "std")]
Expand Down Expand Up @@ -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<usize> {
self.read().unwrap().current_split_index()
}
}

impl<T: Sink + ?Sized> Sink for Arc<T> {
Expand Down Expand Up @@ -242,4 +251,7 @@ impl<T: TimerQuery + ?Sized> TimerQuery for Arc<T> {
fn current_phase(&self) -> TimerPhase {
TimerQuery::current_phase(&**self)
}
fn current_split_index(&self) -> Option<usize> {
TimerQuery::current_split_index(&**self)
}
}

0 comments on commit 36a05d2

Please sign in to comment.