Rename Timer::{percent,percent_left} to Timer::{fraction,fraction_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()`
This commit is contained in:
mamekoro 2023-11-13 14:59:42 +00:00 committed by GitHub
parent 54b7cabc7b
commit 18d001d27c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 27 deletions

View File

@ -334,7 +334,7 @@ impl Timer {
self.times_finished_this_tick = 0; 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 /// # Examples
/// ``` /// ```
@ -342,10 +342,10 @@ impl Timer {
/// use std::time::Duration; /// use std::time::Duration;
/// let mut timer = Timer::from_seconds(2.0, TimerMode::Once); /// let mut timer = Timer::from_seconds(2.0, TimerMode::Once);
/// timer.tick(Duration::from_secs_f32(0.5)); /// timer.tick(Duration::from_secs_f32(0.5));
/// assert_eq!(timer.percent(), 0.25); /// assert_eq!(timer.fraction(), 0.25);
/// ``` /// ```
#[inline] #[inline]
pub fn percent(&self) -> f32 { pub fn fraction(&self) -> f32 {
if self.duration == Duration::ZERO { if self.duration == Duration::ZERO {
1.0 1.0
} else { } else {
@ -353,7 +353,7 @@ impl Timer {
} }
} }
/// 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 /// # Examples
/// ``` /// ```
@ -361,11 +361,11 @@ impl Timer {
/// use std::time::Duration; /// use std::time::Duration;
/// let mut timer = Timer::from_seconds(2.0, TimerMode::Once); /// let mut timer = Timer::from_seconds(2.0, TimerMode::Once);
/// timer.tick(Duration::from_secs_f32(0.5)); /// timer.tick(Duration::from_secs_f32(0.5));
/// assert_eq!(timer.percent_left(), 0.75); /// assert_eq!(timer.fraction_remaining(), 0.75);
/// ``` /// ```
#[inline] #[inline]
pub fn percent_left(&self) -> f32 { pub fn fraction_remaining(&self) -> f32 {
1.0 - self.percent() 1.0 - self.fraction()
} }
/// Returns the remaining time in seconds /// Returns the remaining time in seconds
@ -452,8 +452,8 @@ mod tests {
assert!(!t.just_finished()); assert!(!t.just_finished());
assert_eq!(t.times_finished_this_tick(), 0); assert_eq!(t.times_finished_this_tick(), 0);
assert_eq!(t.mode(), TimerMode::Once); assert_eq!(t.mode(), TimerMode::Once);
assert_eq!(t.percent(), 0.025); assert_eq!(t.fraction(), 0.025);
assert_eq!(t.percent_left(), 0.975); assert_eq!(t.fraction_remaining(), 0.975);
// Ticking while paused changes nothing // Ticking while paused changes nothing
t.pause(); t.pause();
t.tick(Duration::from_secs_f32(500.0)); t.tick(Duration::from_secs_f32(500.0));
@ -463,8 +463,8 @@ mod tests {
assert!(!t.just_finished()); assert!(!t.just_finished());
assert_eq!(t.times_finished_this_tick(), 0); assert_eq!(t.times_finished_this_tick(), 0);
assert_eq!(t.mode(), TimerMode::Once); assert_eq!(t.mode(), TimerMode::Once);
assert_eq!(t.percent(), 0.025); assert_eq!(t.fraction(), 0.025);
assert_eq!(t.percent_left(), 0.975); 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 // Tick past the end and make sure elapsed doesn't go past 0.0 and other things update
t.unpause(); t.unpause();
t.tick(Duration::from_secs_f32(500.0)); t.tick(Duration::from_secs_f32(500.0));
@ -472,16 +472,16 @@ mod tests {
assert!(t.finished()); assert!(t.finished());
assert!(t.just_finished()); assert!(t.just_finished());
assert_eq!(t.times_finished_this_tick(), 1); assert_eq!(t.times_finished_this_tick(), 1);
assert_eq!(t.percent(), 1.0); assert_eq!(t.fraction(), 1.0);
assert_eq!(t.percent_left(), 0.0); assert_eq!(t.fraction_remaining(), 0.0);
// Continuing to tick when finished should only change just_finished // Continuing to tick when finished should only change just_finished
t.tick(Duration::from_secs_f32(1.0)); t.tick(Duration::from_secs_f32(1.0));
assert_eq!(t.elapsed_secs(), 10.0); assert_eq!(t.elapsed_secs(), 10.0);
assert!(t.finished()); assert!(t.finished());
assert!(!t.just_finished()); assert!(!t.just_finished());
assert_eq!(t.times_finished_this_tick(), 0); assert_eq!(t.times_finished_this_tick(), 0);
assert_eq!(t.percent(), 1.0); assert_eq!(t.fraction(), 1.0);
assert_eq!(t.percent_left(), 0.0); assert_eq!(t.fraction_remaining(), 0.0);
} }
#[test] #[test]
@ -495,24 +495,24 @@ mod tests {
assert!(!t.just_finished()); assert!(!t.just_finished());
assert_eq!(t.times_finished_this_tick(), 0); assert_eq!(t.times_finished_this_tick(), 0);
assert_eq!(t.mode(), TimerMode::Repeating); assert_eq!(t.mode(), TimerMode::Repeating);
assert_eq!(t.percent(), 0.375); assert_eq!(t.fraction(), 0.375);
assert_eq!(t.percent_left(), 0.625); assert_eq!(t.fraction_remaining(), 0.625);
// Tick past the end and make sure elapsed wraps // Tick past the end and make sure elapsed wraps
t.tick(Duration::from_secs_f32(1.5)); t.tick(Duration::from_secs_f32(1.5));
assert_eq!(t.elapsed_secs(), 0.25); assert_eq!(t.elapsed_secs(), 0.25);
assert!(t.finished()); assert!(t.finished());
assert!(t.just_finished()); assert!(t.just_finished());
assert_eq!(t.times_finished_this_tick(), 1); assert_eq!(t.times_finished_this_tick(), 1);
assert_eq!(t.percent(), 0.125); assert_eq!(t.fraction(), 0.125);
assert_eq!(t.percent_left(), 0.875); assert_eq!(t.fraction_remaining(), 0.875);
// Continuing to tick should turn off both finished & just_finished for repeating timers // Continuing to tick should turn off both finished & just_finished for repeating timers
t.tick(Duration::from_secs_f32(1.0)); t.tick(Duration::from_secs_f32(1.0));
assert_eq!(t.elapsed_secs(), 1.25); assert_eq!(t.elapsed_secs(), 1.25);
assert!(!t.finished()); assert!(!t.finished());
assert!(!t.just_finished()); assert!(!t.just_finished());
assert_eq!(t.times_finished_this_tick(), 0); assert_eq!(t.times_finished_this_tick(), 0);
assert_eq!(t.percent(), 0.625); assert_eq!(t.fraction(), 0.625);
assert_eq!(t.percent_left(), 0.375); assert_eq!(t.fraction_remaining(), 0.375);
} }
#[test] #[test]
@ -543,19 +543,19 @@ mod tests {
let mut t = Timer::from_seconds(0.0, TimerMode::Repeating); let mut t = Timer::from_seconds(0.0, TimerMode::Repeating);
assert_eq!(t.times_finished_this_tick(), 0); assert_eq!(t.times_finished_this_tick(), 0);
assert_eq!(t.elapsed(), Duration::ZERO); assert_eq!(t.elapsed(), Duration::ZERO);
assert_eq!(t.percent(), 1.0); assert_eq!(t.fraction(), 1.0);
t.tick(Duration::from_secs(1)); t.tick(Duration::from_secs(1));
assert_eq!(t.times_finished_this_tick(), u32::MAX); assert_eq!(t.times_finished_this_tick(), u32::MAX);
assert_eq!(t.elapsed(), Duration::ZERO); assert_eq!(t.elapsed(), Duration::ZERO);
assert_eq!(t.percent(), 1.0); assert_eq!(t.fraction(), 1.0);
t.tick(Duration::from_secs(2)); t.tick(Duration::from_secs(2));
assert_eq!(t.times_finished_this_tick(), u32::MAX); assert_eq!(t.times_finished_this_tick(), u32::MAX);
assert_eq!(t.elapsed(), Duration::ZERO); assert_eq!(t.elapsed(), Duration::ZERO);
assert_eq!(t.percent(), 1.0); assert_eq!(t.fraction(), 1.0);
t.reset(); t.reset();
assert_eq!(t.times_finished_this_tick(), 0); assert_eq!(t.times_finished_this_tick(), 0);
assert_eq!(t.elapsed(), Duration::ZERO); assert_eq!(t.elapsed(), Duration::ZERO);
assert_eq!(t.percent(), 1.0); assert_eq!(t.fraction(), 1.0);
} }
#[test] #[test]

View File

@ -66,7 +66,7 @@ fn countdown(time: Res<Time>, mut countdown: ResMut<Countdown>) {
// Print the percent complete the main timer is. // Print the percent complete the main timer is.
info!( info!(
"Timer is {:0.0}% complete!", "Timer is {:0.0}% complete!",
countdown.main_timer.percent() * 100.0 countdown.main_timer.fraction() * 100.0
); );
} else { } else {
// The timer has finished so we pause the percent output timer // The timer has finished so we pause the percent output timer

View File

@ -112,7 +112,7 @@ impl TargetScale {
} }
fn current_scale(&self) -> f64 { 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); let multiplier = ease_in_expo(completion as f64);
self.start_scale + (self.target_scale - self.start_scale) * multiplier self.start_scale + (self.target_scale - self.start_scale) * multiplier
} }