Skip to content

Commit

Permalink
Forbid Delay with a zero duration (#56)
Browse files Browse the repository at this point in the history
Make `Delay::new()` panic if a zero duration is passed as argument. Fix
the `menu` example to skip inserting a `Sequence<Transform>` containing
a zero-duration `Delay`.

Bug: #41
  • Loading branch information
djeedai authored Sep 27, 2022
1 parent c05ed69 commit fdc3ede
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Removed the `tweening_type` parameter from the signature of `Tween<T>::new()`; use `with_repeat_count()` and `with_repeat_strategy()` instead.
- Animators now always have a tween (instead of it being optional). This means the default animator implementation was removed.
- `Delay::new()` now panics if the `duration` is zero. This prevents creating no-op `Delay` objects, and avoids an internal edge case producing wrong results.

### Removed

Expand Down
12 changes: 8 additions & 4 deletions examples/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
.with_children(|container| {
let mut start_time_ms = 0;
for text in &["Continue", "New Game", "Settings", "Quit"] {
let delay = Delay::new(Duration::from_millis(start_time_ms));
start_time_ms += 500;
let tween_scale = Tween::new(
EaseFunction::BounceOut,
Duration::from_secs(2),
Expand All @@ -55,7 +53,13 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
end: Vec3::ONE,
},
);
let seq = delay.then(tween_scale);
let animator = if start_time_ms > 0 {
let delay = Delay::new(Duration::from_millis(start_time_ms));
Animator::new(delay.then(tween_scale))
} else {
Animator::new(tween_scale)
};
start_time_ms += 500;
container
.spawn_bundle(NodeBundle {
node: Node {
Expand All @@ -76,7 +80,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
..default()
})
.insert(Name::new(format!("button:{}", text)))
.insert(Animator::new(seq))
.insert(animator)
.with_children(|parent| {
parent.spawn_bundle(TextBundle {
text: Text::from_section(
Expand Down
15 changes: 13 additions & 2 deletions src/tweenable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,15 +782,20 @@ pub struct Delay {

impl Delay {
/// Create a new [`Delay`] with a given duration.
///
/// # Panics
///
/// Panics if the duration is zero.
#[must_use]
pub fn new(duration: Duration) -> Self {
assert!(!duration.is_zero());
Self {
timer: Timer::new(duration, false),
}
}

/// Chain another [`Tweenable`] after this tween, making a sequence with the
/// two.
/// Chain another [`Tweenable`] after this tween, making a [`Sequence`] with
/// the two.
#[must_use]
pub fn then<T>(self, tween: impl Tweenable<T> + Send + Sync + 'static) -> Sequence<T> {
Sequence::with_capacity(2).then(self).then(tween)
Expand Down Expand Up @@ -1472,4 +1477,10 @@ mod tests {
}
}
}

#[test]
#[should_panic]
fn delay_zero_duration_panics() {
let _ = Delay::new(Duration::ZERO);
}
}

0 comments on commit fdc3ede

Please sign in to comment.