pub struct Timer { /* private fields */ }
Expand description
Tracks elapsed time. Enters the finished state once duration
is reached.
Non repeating timers will stop tracking and stay in the finished state until reset.
Repeating timers will only be in the finished state on each tick duration
is reached or
exceeded, and can still be reset at any given point.
Paused timers will not have elapsed time increased.
Note that in order to advance the timer tick
MUST be called.
Implementations§
Source§impl Timer
impl Timer
Sourcepub fn new(duration: Duration, mode: TimerMode) -> Self
pub fn new(duration: Duration, mode: TimerMode) -> Self
Creates a new timer with a given duration.
See also Timer::from_seconds
.
Sourcepub fn from_seconds(duration: f32, mode: TimerMode) -> Self
pub fn from_seconds(duration: f32, mode: TimerMode) -> Self
Creates a new timer with a given duration in seconds.
§Example
let mut timer = Timer::from_seconds(1.0, TimerMode::Once);
Sourcepub fn finished(&self) -> bool
pub fn finished(&self) -> bool
Returns true
if the timer has reached its duration.
For repeating timers, this method behaves identically to Timer::just_finished
.
§Examples
use std::time::Duration;
let mut timer_once = Timer::from_seconds(1.0, TimerMode::Once);
timer_once.tick(Duration::from_secs_f32(1.5));
assert!(timer_once.finished());
timer_once.tick(Duration::from_secs_f32(0.5));
assert!(timer_once.finished());
let mut timer_repeating = Timer::from_seconds(1.0, TimerMode::Repeating);
timer_repeating.tick(Duration::from_secs_f32(1.1));
assert!(timer_repeating.finished());
timer_repeating.tick(Duration::from_secs_f32(0.8));
assert!(!timer_repeating.finished());
timer_repeating.tick(Duration::from_secs_f32(0.6));
assert!(timer_repeating.finished());
Sourcepub fn just_finished(&self) -> bool
pub fn just_finished(&self) -> bool
Returns true
only on the tick the timer reached its duration.
§Examples
use std::time::Duration;
let mut timer = Timer::from_seconds(1.0, TimerMode::Once);
timer.tick(Duration::from_secs_f32(1.5));
assert!(timer.just_finished());
timer.tick(Duration::from_secs_f32(0.5));
assert!(!timer.just_finished());
Sourcepub fn elapsed(&self) -> Duration
pub fn elapsed(&self) -> Duration
Returns the time elapsed on the timer. Guaranteed to be between 0.0 and duration
.
Will only equal duration
when the timer is finished and non repeating.
See also Stopwatch::elapsed
.
§Examples
use std::time::Duration;
let mut timer = Timer::from_seconds(1.0, TimerMode::Once);
timer.tick(Duration::from_secs_f32(0.5));
assert_eq!(timer.elapsed(), Duration::from_secs_f32(0.5));
Sourcepub fn elapsed_secs(&self) -> f32
pub fn elapsed_secs(&self) -> f32
Returns the time elapsed on the timer as an f32
.
See also Timer::elapsed
.
Sourcepub fn set_elapsed(&mut self, time: Duration)
pub fn set_elapsed(&mut self, time: Duration)
Sets the elapsed time of the timer without any other considerations.
See also Stopwatch::set
.
§
use std::time::Duration;
let mut timer = Timer::from_seconds(1.0, TimerMode::Once);
timer.set_elapsed(Duration::from_secs(2));
assert_eq!(timer.elapsed(), Duration::from_secs(2));
// the timer is not finished even if the elapsed time is greater than the duration.
assert!(!timer.finished());
Sourcepub fn duration(&self) -> Duration
pub fn duration(&self) -> Duration
Returns the duration of the timer.
§Examples
use std::time::Duration;
let timer = Timer::new(Duration::from_secs(1), TimerMode::Once);
assert_eq!(timer.duration(), Duration::from_secs(1));
Sourcepub fn set_duration(&mut self, duration: Duration)
pub fn set_duration(&mut self, duration: Duration)
Sets the duration of the timer.
§Examples
use std::time::Duration;
let mut timer = Timer::from_seconds(1.5, TimerMode::Once);
timer.set_duration(Duration::from_secs(1));
assert_eq!(timer.duration(), Duration::from_secs(1));
Sourcepub fn mode(&self) -> TimerMode
pub fn mode(&self) -> TimerMode
Returns the mode of the timer.
§Examples
let mut timer = Timer::from_seconds(1.0, TimerMode::Repeating);
assert_eq!(timer.mode(), TimerMode::Repeating);
Sourcepub fn set_mode(&mut self, mode: TimerMode)
pub fn set_mode(&mut self, mode: TimerMode)
Sets the mode of the timer.
§Examples
let mut timer = Timer::from_seconds(1.0, TimerMode::Repeating);
timer.set_mode(TimerMode::Once);
assert_eq!(timer.mode(), TimerMode::Once);
Sourcepub fn tick(&mut self, delta: Duration) -> &Self
pub fn tick(&mut self, delta: Duration) -> &Self
Advance the timer by delta
seconds.
Non repeating timer will clamp at duration.
Repeating timer will wrap around.
Will not affect paused timers.
See also Stopwatch::tick
.
§Examples
use std::time::Duration;
let mut timer = Timer::from_seconds(1.0, TimerMode::Once);
let mut repeating = Timer::from_seconds(1.0, TimerMode::Repeating);
timer.tick(Duration::from_secs_f32(1.5));
repeating.tick(Duration::from_secs_f32(1.5));
assert_eq!(timer.elapsed_secs(), 1.0);
assert_eq!(repeating.elapsed_secs(), 0.5);
Sourcepub fn pause(&mut self)
pub fn pause(&mut self)
Pauses the Timer. Disables the ticking of the timer.
See also Stopwatch::pause
.
§Examples
use std::time::Duration;
let mut timer = Timer::from_seconds(1.0, TimerMode::Once);
timer.pause();
timer.tick(Duration::from_secs_f32(0.5));
assert_eq!(timer.elapsed_secs(), 0.0);
Sourcepub fn unpause(&mut self)
pub fn unpause(&mut self)
Unpauses the Timer. Resumes the ticking of the timer.
See also Stopwatch::unpause()
.
§Examples
use std::time::Duration;
let mut timer = Timer::from_seconds(1.0, TimerMode::Once);
timer.pause();
timer.tick(Duration::from_secs_f32(0.5));
timer.unpause();
timer.tick(Duration::from_secs_f32(0.5));
assert_eq!(timer.elapsed_secs(), 0.5);
Sourcepub fn paused(&self) -> bool
pub fn paused(&self) -> bool
Returns true
if the timer is paused.
See also Stopwatch::paused
.
§Examples
let mut timer = Timer::from_seconds(1.0, TimerMode::Once);
assert!(!timer.paused());
timer.pause();
assert!(timer.paused());
timer.unpause();
assert!(!timer.paused());
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Resets the timer. The reset doesn’t affect the paused
state of the timer.
See also Stopwatch::reset
.
Examples
use std::time::Duration;
let mut timer = Timer::from_seconds(1.0, TimerMode::Once);
timer.tick(Duration::from_secs_f32(1.5));
timer.reset();
assert!(!timer.finished());
assert!(!timer.just_finished());
assert_eq!(timer.elapsed_secs(), 0.0);
Sourcepub fn fraction(&self) -> f32
pub fn fraction(&self) -> f32
Returns the fraction of the timer elapsed time (goes from 0.0 to 1.0).
§Examples
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.fraction(), 0.25);
Sourcepub fn fraction_remaining(&self) -> f32
pub fn fraction_remaining(&self) -> f32
Returns the fraction of the timer remaining time (goes from 1.0 to 0.0).
§Examples
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.fraction_remaining(), 0.75);
Sourcepub fn remaining_secs(&self) -> f32
pub fn remaining_secs(&self) -> f32
Returns the remaining time in seconds
§Examples
use std::cmp::Ordering;
use std::time::Duration;
let mut timer = Timer::from_seconds(2.0, TimerMode::Once);
timer.tick(Duration::from_secs_f32(0.5));
let result = timer.remaining_secs().total_cmp(&1.5);
assert_eq!(Ordering::Equal, result);
Sourcepub fn remaining(&self) -> Duration
pub fn remaining(&self) -> Duration
Returns the remaining time using Duration
§Examples
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.remaining(), Duration::from_secs_f32(1.5));
Sourcepub fn times_finished_this_tick(&self) -> u32
pub fn times_finished_this_tick(&self) -> u32
Returns the number of times a repeating timer
finished during the last tick
call.
For non repeating-timers, this method will only ever return 0 or 1.
§Examples
use std::time::Duration;
let mut timer = Timer::from_seconds(1.0, TimerMode::Repeating);
timer.tick(Duration::from_secs_f32(6.0));
assert_eq!(timer.times_finished_this_tick(), 6);
timer.tick(Duration::from_secs_f32(2.0));
assert_eq!(timer.times_finished_this_tick(), 2);
timer.tick(Duration::from_secs_f32(0.5));
assert_eq!(timer.times_finished_this_tick(), 0);
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Timer
impl<'de> Deserialize<'de> for Timer
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl FromReflect for Timerwhere
Self: Any + Send + Sync,
Stopwatch: FromReflect + TypePath + RegisterForReflection,
Duration: FromReflect + TypePath + RegisterForReflection,
TimerMode: FromReflect + TypePath + RegisterForReflection,
bool: FromReflect + TypePath + RegisterForReflection,
u32: FromReflect + TypePath + RegisterForReflection,
impl FromReflect for Timerwhere
Self: Any + Send + Sync,
Stopwatch: FromReflect + TypePath + RegisterForReflection,
Duration: FromReflect + TypePath + RegisterForReflection,
TimerMode: FromReflect + TypePath + RegisterForReflection,
bool: FromReflect + TypePath + RegisterForReflection,
u32: FromReflect + TypePath + RegisterForReflection,
Source§fn from_reflect(reflect: &dyn Reflect) -> Option<Self>
fn from_reflect(reflect: &dyn Reflect) -> Option<Self>
Self
from a reflected value.Source§fn take_from_reflect(
reflect: Box<dyn Reflect>,
) -> Result<Self, Box<dyn Reflect>>
fn take_from_reflect( reflect: Box<dyn Reflect>, ) -> Result<Self, Box<dyn Reflect>>
Self
using,
constructing the value using from_reflect
if that fails. Read moreSource§impl GetTypeRegistration for Timerwhere
Self: Any + Send + Sync,
Stopwatch: FromReflect + TypePath + RegisterForReflection,
Duration: FromReflect + TypePath + RegisterForReflection,
TimerMode: FromReflect + TypePath + RegisterForReflection,
bool: FromReflect + TypePath + RegisterForReflection,
u32: FromReflect + TypePath + RegisterForReflection,
impl GetTypeRegistration for Timerwhere
Self: Any + Send + Sync,
Stopwatch: FromReflect + TypePath + RegisterForReflection,
Duration: FromReflect + TypePath + RegisterForReflection,
TimerMode: FromReflect + TypePath + RegisterForReflection,
bool: FromReflect + TypePath + RegisterForReflection,
u32: FromReflect + TypePath + RegisterForReflection,
Source§fn get_type_registration() -> TypeRegistration
fn get_type_registration() -> TypeRegistration
TypeRegistration
for this type.Source§fn register_type_dependencies(registry: &mut TypeRegistry)
fn register_type_dependencies(registry: &mut TypeRegistry)
Source§impl Reflect for Timerwhere
Self: Any + Send + Sync,
Stopwatch: FromReflect + TypePath + RegisterForReflection,
Duration: FromReflect + TypePath + RegisterForReflection,
TimerMode: FromReflect + TypePath + RegisterForReflection,
bool: FromReflect + TypePath + RegisterForReflection,
u32: FromReflect + TypePath + RegisterForReflection,
impl Reflect for Timerwhere
Self: Any + Send + Sync,
Stopwatch: FromReflect + TypePath + RegisterForReflection,
Duration: FromReflect + TypePath + RegisterForReflection,
TimerMode: FromReflect + TypePath + RegisterForReflection,
bool: FromReflect + TypePath + RegisterForReflection,
u32: FromReflect + TypePath + RegisterForReflection,
Source§fn get_represented_type_info(&self) -> Option<&'static TypeInfo>
fn get_represented_type_info(&self) -> Option<&'static TypeInfo>
Source§fn as_any_mut(&mut self) -> &mut dyn Any
fn as_any_mut(&mut self) -> &mut dyn Any
&mut dyn Any
.Source§fn into_reflect(self: Box<Self>) -> Box<dyn Reflect>
fn into_reflect(self: Box<Self>) -> Box<dyn Reflect>
Source§fn as_reflect(&self) -> &dyn Reflect
fn as_reflect(&self) -> &dyn Reflect
Source§fn as_reflect_mut(&mut self) -> &mut dyn Reflect
fn as_reflect_mut(&mut self) -> &mut dyn Reflect
Source§fn clone_value(&self) -> Box<dyn Reflect>
fn clone_value(&self) -> Box<dyn Reflect>
Reflect
trait object. Read moreSource§fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>>
fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>>
Source§fn reflect_kind(&self) -> ReflectKind
fn reflect_kind(&self) -> ReflectKind
Source§fn reflect_ref(&self) -> ReflectRef<'_>
fn reflect_ref(&self) -> ReflectRef<'_>
Source§fn reflect_mut(&mut self) -> ReflectMut<'_>
fn reflect_mut(&mut self) -> ReflectMut<'_>
Source§fn reflect_owned(self: Box<Self>) -> ReflectOwned
fn reflect_owned(self: Box<Self>) -> ReflectOwned
Source§fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool>
fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option<bool>
Source§fn apply(&mut self, value: &(dyn Reflect + 'static))
fn apply(&mut self, value: &(dyn Reflect + 'static))
Source§fn reflect_hash(&self) -> Option<u64>
fn reflect_hash(&self) -> Option<u64>
Source§fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error>
fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error>
Source§fn serializable(&self) -> Option<Serializable<'_>>
fn serializable(&self) -> Option<Serializable<'_>>
Source§fn is_dynamic(&self) -> bool
fn is_dynamic(&self) -> bool
Source§impl Struct for Timerwhere
Self: Any + Send + Sync,
Stopwatch: FromReflect + TypePath + RegisterForReflection,
Duration: FromReflect + TypePath + RegisterForReflection,
TimerMode: FromReflect + TypePath + RegisterForReflection,
bool: FromReflect + TypePath + RegisterForReflection,
u32: FromReflect + TypePath + RegisterForReflection,
impl Struct for Timerwhere
Self: Any + Send + Sync,
Stopwatch: FromReflect + TypePath + RegisterForReflection,
Duration: FromReflect + TypePath + RegisterForReflection,
TimerMode: FromReflect + TypePath + RegisterForReflection,
bool: FromReflect + TypePath + RegisterForReflection,
u32: FromReflect + TypePath + RegisterForReflection,
Source§fn field(&self, name: &str) -> Option<&dyn Reflect>
fn field(&self, name: &str) -> Option<&dyn Reflect>
name
as a &dyn Reflect
.Source§fn field_mut(&mut self, name: &str) -> Option<&mut dyn Reflect>
fn field_mut(&mut self, name: &str) -> Option<&mut dyn Reflect>
name
as a
&mut dyn Reflect
.Source§fn field_at(&self, index: usize) -> Option<&dyn Reflect>
fn field_at(&self, index: usize) -> Option<&dyn Reflect>
index
as a
&dyn Reflect
.Source§fn field_at_mut(&mut self, index: usize) -> Option<&mut dyn Reflect>
fn field_at_mut(&mut self, index: usize) -> Option<&mut dyn Reflect>
index
as a &mut dyn Reflect
.Source§fn name_at(&self, index: usize) -> Option<&str>
fn name_at(&self, index: usize) -> Option<&str>
index
.Source§fn iter_fields(&self) -> FieldIter<'_>
fn iter_fields(&self) -> FieldIter<'_>
Source§fn clone_dynamic(&self) -> DynamicStruct
fn clone_dynamic(&self) -> DynamicStruct
DynamicStruct
.Source§impl TypePath for Timer
impl TypePath for Timer
Source§fn type_path() -> &'static str
fn type_path() -> &'static str
Source§fn short_type_path() -> &'static str
fn short_type_path() -> &'static str
Source§fn type_ident() -> Option<&'static str>
fn type_ident() -> Option<&'static str>
Source§fn crate_name() -> Option<&'static str>
fn crate_name() -> Option<&'static str>
Source§impl Typed for Timerwhere
Self: Any + Send + Sync,
Stopwatch: FromReflect + TypePath + RegisterForReflection,
Duration: FromReflect + TypePath + RegisterForReflection,
TimerMode: FromReflect + TypePath + RegisterForReflection,
bool: FromReflect + TypePath + RegisterForReflection,
u32: FromReflect + TypePath + RegisterForReflection,
impl Typed for Timerwhere
Self: Any + Send + Sync,
Stopwatch: FromReflect + TypePath + RegisterForReflection,
Duration: FromReflect + TypePath + RegisterForReflection,
TimerMode: FromReflect + TypePath + RegisterForReflection,
bool: FromReflect + TypePath + RegisterForReflection,
u32: FromReflect + TypePath + RegisterForReflection,
impl Eq for Timer
impl StructuralPartialEq for Timer
Auto Trait Implementations§
impl Freeze for Timer
impl RefUnwindSafe for Timer
impl Send for Timer
impl Sync for Timer
impl Unpin for Timer
impl UnwindSafe for Timer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> DynamicTypePath for Twhere
T: TypePath,
impl<T> DynamicTypePath for Twhere
T: TypePath,
Source§fn reflect_type_path(&self) -> &str
fn reflect_type_path(&self) -> &str
TypePath::type_path
.Source§fn reflect_short_type_path(&self) -> &str
fn reflect_short_type_path(&self) -> &str
Source§fn reflect_type_ident(&self) -> Option<&str>
fn reflect_type_ident(&self) -> Option<&str>
TypePath::type_ident
.Source§fn reflect_crate_name(&self) -> Option<&str>
fn reflect_crate_name(&self) -> Option<&str>
TypePath::crate_name
.Source§fn reflect_module_path(&self) -> Option<&str>
fn reflect_module_path(&self) -> Option<&str>
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
Source§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Self
using data from the given World
.Source§impl<S> GetField for Swhere
S: Struct,
impl<S> GetField for Swhere
S: Struct,
Source§impl<T> GetPath for T
impl<T> GetPath for T
Source§fn reflect_path<'p>(
&self,
path: impl ReflectPath<'p>,
) -> Result<&(dyn Reflect + 'static), ReflectPathError<'p>>
fn reflect_path<'p>( &self, path: impl ReflectPath<'p>, ) -> Result<&(dyn Reflect + 'static), ReflectPathError<'p>>
path
. Read moreSource§fn reflect_path_mut<'p>(
&mut self,
path: impl ReflectPath<'p>,
) -> Result<&mut (dyn Reflect + 'static), ReflectPathError<'p>>
fn reflect_path_mut<'p>( &mut self, path: impl ReflectPath<'p>, ) -> Result<&mut (dyn Reflect + 'static), ReflectPathError<'p>>
path
. Read moreSource§fn path<'p, T>(
&self,
path: impl ReflectPath<'p>,
) -> Result<&T, ReflectPathError<'p>>where
T: Reflect,
fn path<'p, T>(
&self,
path: impl ReflectPath<'p>,
) -> Result<&T, ReflectPathError<'p>>where
T: Reflect,
path
. Read moreSource§fn path_mut<'p, T>(
&mut self,
path: impl ReflectPath<'p>,
) -> Result<&mut T, ReflectPathError<'p>>where
T: Reflect,
fn path_mut<'p, T>(
&mut self,
path: impl ReflectPath<'p>,
) -> Result<&mut T, ReflectPathError<'p>>where
T: Reflect,
path
. Read more