2022-05-03 19:20:13 +00:00
|
|
|
use bevy_reflect::prelude::*;
|
2021-03-05 19:59:14 +00:00
|
|
|
use bevy_reflect::Reflect;
|
|
|
|
use bevy_utils::Duration;
|
|
|
|
|
|
|
|
/// A Stopwatch is a struct that track elapsed time when started.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2022-05-26 00:27:18 +00:00
|
|
|
/// # use bevy_time::*;
|
2021-03-05 19:59:14 +00:00
|
|
|
/// use std::time::Duration;
|
|
|
|
/// let mut stopwatch = Stopwatch::new();
|
|
|
|
/// assert_eq!(stopwatch.elapsed_secs(), 0.0);
|
2021-04-16 19:13:08 +00:00
|
|
|
///
|
2021-03-05 19:59:14 +00:00
|
|
|
/// stopwatch.tick(Duration::from_secs_f32(1.0)); // tick one second
|
|
|
|
/// assert_eq!(stopwatch.elapsed_secs(), 1.0);
|
2021-04-16 19:13:08 +00:00
|
|
|
///
|
2021-03-05 19:59:14 +00:00
|
|
|
/// stopwatch.pause();
|
|
|
|
/// stopwatch.tick(Duration::from_secs_f32(1.0)); // paused stopwatches don't tick
|
|
|
|
/// assert_eq!(stopwatch.elapsed_secs(), 1.0);
|
2021-04-16 19:13:08 +00:00
|
|
|
///
|
2021-03-05 19:59:14 +00:00
|
|
|
/// stopwatch.reset(); // reset the stopwatch
|
|
|
|
/// assert!(stopwatch.paused());
|
|
|
|
/// assert_eq!(stopwatch.elapsed_secs(), 0.0);
|
|
|
|
/// ```
|
bevy_reflect: `FromReflect` Ergonomics Implementation (#6056)
# Objective
**This implementation is based on
https://github.com/bevyengine/rfcs/pull/59.**
---
Resolves #4597
Full details and motivation can be found in the RFC, but here's a brief
summary.
`FromReflect` is a very powerful and important trait within the
reflection API. It allows Dynamic types (e.g., `DynamicList`, etc.) to
be formed into Real ones (e.g., `Vec<i32>`, etc.).
This mainly comes into play concerning deserialization, where the
reflection deserializers both return a `Box<dyn Reflect>` that almost
always contain one of these Dynamic representations of a Real type. To
convert this to our Real type, we need to use `FromReflect`.
It also sneaks up in other ways. For example, it's a required bound for
`T` in `Vec<T>` so that `Vec<T>` as a whole can be made `FromReflect`.
It's also required by all fields of an enum as it's used as part of the
`Reflect::apply` implementation.
So in other words, much like `GetTypeRegistration` and `Typed`, it is
very much a core reflection trait.
The problem is that it is not currently treated like a core trait and is
not automatically derived alongside `Reflect`. This makes using it a bit
cumbersome and easy to forget.
## Solution
Automatically derive `FromReflect` when deriving `Reflect`.
Users can then choose to opt-out if needed using the
`#[reflect(from_reflect = false)]` attribute.
```rust
#[derive(Reflect)]
struct Foo;
#[derive(Reflect)]
#[reflect(from_reflect = false)]
struct Bar;
fn test<T: FromReflect>(value: T) {}
test(Foo); // <-- OK
test(Bar); // <-- Panic! Bar does not implement trait `FromReflect`
```
#### `ReflectFromReflect`
This PR also automatically adds the `ReflectFromReflect` (introduced in
#6245) registration to the derived `GetTypeRegistration` impl— if the
type hasn't opted out of `FromReflect` of course.
<details>
<summary><h4>Improved Deserialization</h4></summary>
> **Warning**
> This section includes changes that have since been descoped from this
PR. They will likely be implemented again in a followup PR. I am mainly
leaving these details in for archival purposes, as well as for reference
when implementing this logic again.
And since we can do all the above, we might as well improve
deserialization. We can now choose to deserialize into a Dynamic type or
automatically convert it using `FromReflect` under the hood.
`[Un]TypedReflectDeserializer::new` will now perform the conversion and
return the `Box`'d Real type.
`[Un]TypedReflectDeserializer::new_dynamic` will work like what we have
now and simply return the `Box`'d Dynamic type.
```rust
// Returns the Real type
let reflect_deserializer = UntypedReflectDeserializer::new(®istry);
let mut deserializer = ron::de::Deserializer::from_str(input)?;
let output: SomeStruct = reflect_deserializer.deserialize(&mut deserializer)?.take()?;
// Returns the Dynamic type
let reflect_deserializer = UntypedReflectDeserializer::new_dynamic(®istry);
let mut deserializer = ron::de::Deserializer::from_str(input)?;
let output: DynamicStruct = reflect_deserializer.deserialize(&mut deserializer)?.take()?;
```
</details>
---
## Changelog
* `FromReflect` is now automatically derived within the `Reflect` derive
macro
* This includes auto-registering `ReflectFromReflect` in the derived
`GetTypeRegistration` impl
* ~~Renamed `TypedReflectDeserializer::new` and
`UntypedReflectDeserializer::new` to
`TypedReflectDeserializer::new_dynamic` and
`UntypedReflectDeserializer::new_dynamic`, respectively~~ **Descoped**
* ~~Changed `TypedReflectDeserializer::new` and
`UntypedReflectDeserializer::new` to automatically convert the
deserialized output using `FromReflect`~~ **Descoped**
## Migration Guide
* `FromReflect` is now automatically derived within the `Reflect` derive
macro. Items with both derives will need to remove the `FromReflect`
one.
```rust
// OLD
#[derive(Reflect, FromReflect)]
struct Foo;
// NEW
#[derive(Reflect)]
struct Foo;
```
If using a manual implementation of `FromReflect` and the `Reflect`
derive, users will need to opt-out of the automatic implementation.
```rust
// OLD
#[derive(Reflect)]
struct Foo;
impl FromReflect for Foo {/* ... */}
// NEW
#[derive(Reflect)]
#[reflect(from_reflect = false)]
struct Foo;
impl FromReflect for Foo {/* ... */}
```
<details>
<summary><h4>Removed Migrations</h4></summary>
> **Warning**
> This section includes changes that have since been descoped from this
PR. They will likely be implemented again in a followup PR. I am mainly
leaving these details in for archival purposes, as well as for reference
when implementing this logic again.
* The reflect deserializers now perform a `FromReflect` conversion
internally. The expected output of `TypedReflectDeserializer::new` and
`UntypedReflectDeserializer::new` is no longer a Dynamic (e.g.,
`DynamicList`), but its Real counterpart (e.g., `Vec<i32>`).
```rust
let reflect_deserializer =
UntypedReflectDeserializer::new_dynamic(®istry);
let mut deserializer = ron::de::Deserializer::from_str(input)?;
// OLD
let output: DynamicStruct = reflect_deserializer.deserialize(&mut
deserializer)?.take()?;
// NEW
let output: SomeStruct = reflect_deserializer.deserialize(&mut
deserializer)?.take()?;
```
Alternatively, if this behavior isn't desired, use the
`TypedReflectDeserializer::new_dynamic` and
`UntypedReflectDeserializer::new_dynamic` methods instead:
```rust
// OLD
let reflect_deserializer = UntypedReflectDeserializer::new(®istry);
// NEW
let reflect_deserializer =
UntypedReflectDeserializer::new_dynamic(®istry);
```
</details>
---------
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2023-06-29 01:31:34 +00:00
|
|
|
#[derive(Clone, Debug, Default, PartialEq, Eq, Reflect)]
|
2022-10-17 14:38:57 +00:00
|
|
|
#[cfg_attr(feature = "serialize", derive(serde::Deserialize, serde::Serialize))]
|
2022-05-03 19:20:13 +00:00
|
|
|
#[reflect(Default)]
|
2021-03-05 19:59:14 +00:00
|
|
|
pub struct Stopwatch {
|
|
|
|
elapsed: Duration,
|
|
|
|
paused: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stopwatch {
|
|
|
|
/// Create a new unpaused `Stopwatch` with no elapsed time.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
2022-05-26 00:27:18 +00:00
|
|
|
/// # use bevy_time::*;
|
2021-03-05 19:59:14 +00:00
|
|
|
/// let stopwatch = Stopwatch::new();
|
|
|
|
/// assert_eq!(stopwatch.elapsed_secs(), 0.0);
|
|
|
|
/// assert_eq!(stopwatch.paused(), false);
|
|
|
|
/// ```
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the elapsed time since the last [`reset`](Stopwatch::reset)
|
|
|
|
/// of the stopwatch.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
2022-05-26 00:27:18 +00:00
|
|
|
/// # use bevy_time::*;
|
2021-03-05 19:59:14 +00:00
|
|
|
/// use std::time::Duration;
|
|
|
|
/// let mut stopwatch = Stopwatch::new();
|
|
|
|
/// stopwatch.tick(Duration::from_secs(1));
|
|
|
|
/// assert_eq!(stopwatch.elapsed(), Duration::from_secs(1));
|
|
|
|
/// ```
|
2022-01-02 20:36:40 +00:00
|
|
|
///
|
|
|
|
/// # See Also
|
|
|
|
///
|
2022-09-26 21:47:31 +00:00
|
|
|
/// [`elapsed_secs`](Stopwatch::elapsed_secs) - if an `f32` value is desirable instead.
|
|
|
|
/// [`elapsed_secs_f64`](Stopwatch::elapsed_secs_f64) - if an `f64` is desirable instead.
|
2021-03-05 19:59:14 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn elapsed(&self) -> Duration {
|
|
|
|
self.elapsed
|
|
|
|
}
|
|
|
|
|
2022-01-02 20:36:40 +00:00
|
|
|
/// Returns the elapsed time since the last [`reset`](Stopwatch::reset)
|
|
|
|
/// of the stopwatch, in seconds.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
2022-05-26 00:27:18 +00:00
|
|
|
/// # use bevy_time::*;
|
2022-01-02 20:36:40 +00:00
|
|
|
/// use std::time::Duration;
|
|
|
|
/// let mut stopwatch = Stopwatch::new();
|
|
|
|
/// stopwatch.tick(Duration::from_secs(1));
|
|
|
|
/// assert_eq!(stopwatch.elapsed_secs(), 1.0);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// # See Also
|
|
|
|
///
|
|
|
|
/// [`elapsed`](Stopwatch::elapsed) - if a `Duration` is desirable instead.
|
2022-09-26 21:47:31 +00:00
|
|
|
/// [`elapsed_secs_f64`](Stopwatch::elapsed_secs_f64) - if an `f64` is desirable instead.
|
2021-03-05 19:59:14 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn elapsed_secs(&self) -> f32 {
|
|
|
|
self.elapsed().as_secs_f32()
|
|
|
|
}
|
|
|
|
|
2022-09-13 22:41:29 +00:00
|
|
|
/// Returns the elapsed time since the last [`reset`](Stopwatch::reset)
|
|
|
|
/// of the stopwatch, in seconds, as f64.
|
|
|
|
///
|
|
|
|
/// # See Also
|
|
|
|
///
|
|
|
|
/// [`elapsed`](Stopwatch::elapsed) - if a `Duration` is desirable instead.
|
2022-09-26 21:47:31 +00:00
|
|
|
/// [`elapsed_secs`](Stopwatch::elapsed_secs) - if an `f32` is desirable instead.
|
2022-09-13 22:41:29 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn elapsed_secs_f64(&self) -> f64 {
|
|
|
|
self.elapsed().as_secs_f64()
|
|
|
|
}
|
|
|
|
|
2021-03-05 19:59:14 +00:00
|
|
|
/// Sets the elapsed time of the stopwatch.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
2022-05-26 00:27:18 +00:00
|
|
|
/// # use bevy_time::*;
|
2021-03-05 19:59:14 +00:00
|
|
|
/// use std::time::Duration;
|
|
|
|
/// let mut stopwatch = Stopwatch::new();
|
|
|
|
/// stopwatch.set_elapsed(Duration::from_secs_f32(1.0));
|
|
|
|
/// assert_eq!(stopwatch.elapsed_secs(), 1.0);
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn set_elapsed(&mut self, time: Duration) {
|
|
|
|
self.elapsed = time;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Advance the stopwatch by `delta` seconds.
|
|
|
|
/// If the stopwatch is paused, ticking will not have any effect
|
|
|
|
/// on elapsed time.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
2022-05-26 00:27:18 +00:00
|
|
|
/// # use bevy_time::*;
|
2021-03-05 19:59:14 +00:00
|
|
|
/// use std::time::Duration;
|
|
|
|
/// let mut stopwatch = Stopwatch::new();
|
|
|
|
/// stopwatch.tick(Duration::from_secs_f32(1.5));
|
|
|
|
/// assert_eq!(stopwatch.elapsed_secs(), 1.5);
|
|
|
|
/// ```
|
|
|
|
pub fn tick(&mut self, delta: Duration) -> &Self {
|
|
|
|
if !self.paused() {
|
|
|
|
self.elapsed += delta;
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Pauses the stopwatch. Any call to [`tick`](Stopwatch::tick) while
|
|
|
|
/// paused will not have any effect on the elapsed time.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
2022-05-26 00:27:18 +00:00
|
|
|
/// # use bevy_time::*;
|
2021-03-05 19:59:14 +00:00
|
|
|
/// use std::time::Duration;
|
|
|
|
/// let mut stopwatch = Stopwatch::new();
|
|
|
|
/// stopwatch.pause();
|
|
|
|
/// stopwatch.tick(Duration::from_secs_f32(1.5));
|
|
|
|
/// assert!(stopwatch.paused());
|
|
|
|
/// assert_eq!(stopwatch.elapsed_secs(), 0.0);
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn pause(&mut self) {
|
|
|
|
self.paused = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Unpauses the stopwatch. Resume the effect of ticking on elapsed time.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
2022-05-26 00:27:18 +00:00
|
|
|
/// # use bevy_time::*;
|
2021-03-05 19:59:14 +00:00
|
|
|
/// use std::time::Duration;
|
|
|
|
/// let mut stopwatch = Stopwatch::new();
|
|
|
|
/// stopwatch.pause();
|
|
|
|
/// stopwatch.tick(Duration::from_secs_f32(1.0));
|
|
|
|
/// stopwatch.unpause();
|
|
|
|
/// stopwatch.tick(Duration::from_secs_f32(1.0));
|
|
|
|
/// assert!(!stopwatch.paused());
|
|
|
|
/// assert_eq!(stopwatch.elapsed_secs(), 1.0);
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn unpause(&mut self) {
|
|
|
|
self.paused = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if the stopwatch is paused.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
2022-05-26 00:27:18 +00:00
|
|
|
/// # use bevy_time::*;
|
2021-03-05 19:59:14 +00:00
|
|
|
/// let mut stopwatch = Stopwatch::new();
|
|
|
|
/// assert!(!stopwatch.paused());
|
|
|
|
/// stopwatch.pause();
|
|
|
|
/// assert!(stopwatch.paused());
|
|
|
|
/// stopwatch.unpause();
|
|
|
|
/// assert!(!stopwatch.paused());
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn paused(&self) -> bool {
|
|
|
|
self.paused
|
|
|
|
}
|
|
|
|
|
2023-01-06 00:43:30 +00:00
|
|
|
/// Resets the stopwatch. The reset doesn't affect the paused state of the stopwatch.
|
2021-03-05 19:59:14 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
/// ```
|
2022-05-26 00:27:18 +00:00
|
|
|
/// # use bevy_time::*;
|
2021-03-05 19:59:14 +00:00
|
|
|
/// use std::time::Duration;
|
|
|
|
/// let mut stopwatch = Stopwatch::new();
|
|
|
|
/// stopwatch.tick(Duration::from_secs_f32(1.5));
|
|
|
|
/// stopwatch.reset();
|
|
|
|
/// assert_eq!(stopwatch.elapsed_secs(), 0.0);
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn reset(&mut self) {
|
|
|
|
self.elapsed = Default::default();
|
|
|
|
}
|
|
|
|
}
|