Skip to content

Commit

Permalink
Rename Timer::{percent,percent_left} to `Timer::{fraction,fraction_…
Browse files Browse the repository at this point in the history
…remaining}` (#10442)

# Objective
Fixes #10439

`Timer::percent()` and `Timer::percent_left()` return values in the
range of 0.0 to 1.0, even though their names contain "percent".

These functions should be renamed for clarity.

## Solution
- Rename `Timer::percent()` to `Timer::fraction()`
- Rename `Timer::percent_left()` to `Timer::fraction_remaining()`

---

## Changelog

### Changed
- Renamed `Timer::percent()` to `Timer::fraction()`
- Renamed `Timer::percent_left()` to `Timer::fraction_remaining()`

## Migration Guide
- `Timer::percent()` has been renamed to `Timer::fraction()`
- `Timer::percent_left()` has been renamed to
`Timer::fraction_remaining()`
  • Loading branch information
mamekoro authored Nov 13, 2023
1 parent 54b7cab commit 18d001d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
50 changes: 25 additions & 25 deletions crates/bevy_time/src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,38 +334,38 @@ impl Timer {
self.times_finished_this_tick = 0;
}

/// Returns the percentage of the timer elapsed time (goes from 0.0 to 1.0).
/// Returns the fraction of the timer elapsed time (goes from 0.0 to 1.0).
///
/// # Examples
/// ```
/// # use bevy_time::*;
/// use std::time::Duration;
/// let mut timer = Timer::from_seconds(2.0, TimerMode::Once);
/// timer.tick(Duration::from_secs_f32(0.5));
/// assert_eq!(timer.percent(), 0.25);
/// assert_eq!(timer.fraction(), 0.25);
/// ```
#[inline]
pub fn percent(&self) -> f32 {
pub fn fraction(&self) -> f32 {
if self.duration == Duration::ZERO {
1.0
} else {
self.elapsed().as_secs_f32() / self.duration().as_secs_f32()
}
}

/// Returns the percentage of the timer remaining time (goes from 1.0 to 0.0).
/// Returns the fraction of the timer remaining time (goes from 1.0 to 0.0).
///
/// # Examples
/// ```
/// # use bevy_time::*;
/// use std::time::Duration;
/// let mut timer = Timer::from_seconds(2.0, TimerMode::Once);
/// timer.tick(Duration::from_secs_f32(0.5));
/// assert_eq!(timer.percent_left(), 0.75);
/// assert_eq!(timer.fraction_remaining(), 0.75);
/// ```
#[inline]
pub fn percent_left(&self) -> f32 {
1.0 - self.percent()
pub fn fraction_remaining(&self) -> f32 {
1.0 - self.fraction()
}

/// Returns the remaining time in seconds
Expand Down Expand Up @@ -452,8 +452,8 @@ mod tests {
assert!(!t.just_finished());
assert_eq!(t.times_finished_this_tick(), 0);
assert_eq!(t.mode(), TimerMode::Once);
assert_eq!(t.percent(), 0.025);
assert_eq!(t.percent_left(), 0.975);
assert_eq!(t.fraction(), 0.025);
assert_eq!(t.fraction_remaining(), 0.975);
// Ticking while paused changes nothing
t.pause();
t.tick(Duration::from_secs_f32(500.0));
Expand All @@ -463,25 +463,25 @@ mod tests {
assert!(!t.just_finished());
assert_eq!(t.times_finished_this_tick(), 0);
assert_eq!(t.mode(), TimerMode::Once);
assert_eq!(t.percent(), 0.025);
assert_eq!(t.percent_left(), 0.975);
assert_eq!(t.fraction(), 0.025);
assert_eq!(t.fraction_remaining(), 0.975);
// Tick past the end and make sure elapsed doesn't go past 0.0 and other things update
t.unpause();
t.tick(Duration::from_secs_f32(500.0));
assert_eq!(t.elapsed_secs(), 10.0);
assert!(t.finished());
assert!(t.just_finished());
assert_eq!(t.times_finished_this_tick(), 1);
assert_eq!(t.percent(), 1.0);
assert_eq!(t.percent_left(), 0.0);
assert_eq!(t.fraction(), 1.0);
assert_eq!(t.fraction_remaining(), 0.0);
// Continuing to tick when finished should only change just_finished
t.tick(Duration::from_secs_f32(1.0));
assert_eq!(t.elapsed_secs(), 10.0);
assert!(t.finished());
assert!(!t.just_finished());
assert_eq!(t.times_finished_this_tick(), 0);
assert_eq!(t.percent(), 1.0);
assert_eq!(t.percent_left(), 0.0);
assert_eq!(t.fraction(), 1.0);
assert_eq!(t.fraction_remaining(), 0.0);
}

#[test]
Expand All @@ -495,24 +495,24 @@ mod tests {
assert!(!t.just_finished());
assert_eq!(t.times_finished_this_tick(), 0);
assert_eq!(t.mode(), TimerMode::Repeating);
assert_eq!(t.percent(), 0.375);
assert_eq!(t.percent_left(), 0.625);
assert_eq!(t.fraction(), 0.375);
assert_eq!(t.fraction_remaining(), 0.625);
// Tick past the end and make sure elapsed wraps
t.tick(Duration::from_secs_f32(1.5));
assert_eq!(t.elapsed_secs(), 0.25);
assert!(t.finished());
assert!(t.just_finished());
assert_eq!(t.times_finished_this_tick(), 1);
assert_eq!(t.percent(), 0.125);
assert_eq!(t.percent_left(), 0.875);
assert_eq!(t.fraction(), 0.125);
assert_eq!(t.fraction_remaining(), 0.875);
// Continuing to tick should turn off both finished & just_finished for repeating timers
t.tick(Duration::from_secs_f32(1.0));
assert_eq!(t.elapsed_secs(), 1.25);
assert!(!t.finished());
assert!(!t.just_finished());
assert_eq!(t.times_finished_this_tick(), 0);
assert_eq!(t.percent(), 0.625);
assert_eq!(t.percent_left(), 0.375);
assert_eq!(t.fraction(), 0.625);
assert_eq!(t.fraction_remaining(), 0.375);
}

#[test]
Expand Down Expand Up @@ -543,19 +543,19 @@ mod tests {
let mut t = Timer::from_seconds(0.0, TimerMode::Repeating);
assert_eq!(t.times_finished_this_tick(), 0);
assert_eq!(t.elapsed(), Duration::ZERO);
assert_eq!(t.percent(), 1.0);
assert_eq!(t.fraction(), 1.0);
t.tick(Duration::from_secs(1));
assert_eq!(t.times_finished_this_tick(), u32::MAX);
assert_eq!(t.elapsed(), Duration::ZERO);
assert_eq!(t.percent(), 1.0);
assert_eq!(t.fraction(), 1.0);
t.tick(Duration::from_secs(2));
assert_eq!(t.times_finished_this_tick(), u32::MAX);
assert_eq!(t.elapsed(), Duration::ZERO);
assert_eq!(t.percent(), 1.0);
assert_eq!(t.fraction(), 1.0);
t.reset();
assert_eq!(t.times_finished_this_tick(), 0);
assert_eq!(t.elapsed(), Duration::ZERO);
assert_eq!(t.percent(), 1.0);
assert_eq!(t.fraction(), 1.0);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion examples/time/timers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn countdown(time: Res<Time>, mut countdown: ResMut<Countdown>) {
// Print the percent complete the main timer is.
info!(
"Timer is {:0.0}% complete!",
countdown.main_timer.percent() * 100.0
countdown.main_timer.fraction() * 100.0
);
} else {
// The timer has finished so we pause the percent output timer
Expand Down
2 changes: 1 addition & 1 deletion examples/ui/ui_scaling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl TargetScale {
}

fn current_scale(&self) -> f64 {
let completion = self.target_time.percent();
let completion = self.target_time.fraction();
let multiplier = ease_in_expo(completion as f64);
self.start_scale + (self.target_scale - self.start_scale) * multiplier
}
Expand Down

0 comments on commit 18d001d

Please sign in to comment.