Skip to content

Commit c2b76c4

Browse files
SUPERCILEXinodentry
authored andcommitted
Support running an animation N times (djeedai#19)
Remove `TweeningType` and split its functionalities between a new `RepeatCount` controlling the number of repeats of an animation on one hand, and `RepeatStrategy` controlling the way an animation restarts after a loop ended on the other hand. This allows more granular control on the type of playback. Remove the `tweening_type` parameter from `Tween<T>::new()` and replace it with builder methods `with_repeat_count()` and `with_repeat_strategy()`. Remove `is_looping()` from all tweenables, which was not implemented for most of them anyway.
1 parent 176f18e commit c2b76c4

File tree

11 files changed

+292
-252
lines changed

11 files changed

+292
-252
lines changed

CHANGELOG.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
### Added
99

10-
- Add `is_forward()` and `is_backward()` convenience helpers to `TweeningDirection`.
11-
- Add `Tween::set_direction()` and `Tween::with_direction()` which allow configuring the playback direction of a tween, allowing to play it backward from end to start.
12-
- Support dynamically changing an animation's speed with `Animator::set_speed`
13-
- Add `AnimationSystem` label to tweening tick systems
14-
- A `BoxedTweenable` type to make working with `Box<dyn Tweenable + ...>` easier
10+
- Added `is_forward()` and `is_backward()` convenience helpers to `TweeningDirection`.
11+
- Added `Tween::set_direction()` and `Tween::with_direction()` which allow configuring the playback direction of a tween, allowing to play it backward from end to start.
12+
- Added support for dynamically changing an animation's speed with `Animator::set_speed`.
13+
- Added `AnimationSystem` label to tweening tick systems.
14+
- Added `BoxedTweenable` type to make working with `Box<dyn Tweenable + ...>` easier.
15+
- Added `RepeatCount` and `RepeatStrategy` for more granular control over animation looping.
16+
- Added `with_repeat_count()` and `with_repeat_strategy()` builder methods to `Tween<T>`.
1517

1618
### Changed
1719

18-
- Double boxing in `Sequence` and `Tracks` was fixed. As a result, any custom tweenables
20+
- Double boxing in `Sequence` and `Tracks` was fixed. As a result, any custom tweenables.
1921
should implement `From` for `BoxedTweenable` to make those APIs easier to use.
22+
- Removed the `tweening_type` parameter from the signature of `Tween<T>::new()`; use `with_repeat_count()` and `with_repeat_strategy()` instead.
23+
24+
### Removed
25+
26+
- Removed `Tweenable::is_looping()`, which was not implemented for most tweenables.
27+
- Removed `TweeningType` in favor of `RepeatCount` and `RepeatStrategy`.
2028

2129
## [0.4.0] - 2022-04-16
2230

examples/colormaterial_color.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,14 @@ fn setup(
7777

7878
let tween = Tween::new(
7979
*ease_function,
80-
TweeningType::PingPong,
8180
Duration::from_secs(1),
8281
ColorMaterialColorLens {
8382
start: Color::RED,
8483
end: Color::BLUE,
8584
},
86-
);
85+
)
86+
.with_repeat_count(RepeatCount::Infinite)
87+
.with_repeat_strategy(RepeatStrategy::MirroredRepeat);
8788

8889
commands
8990
.spawn_bundle(MaterialMesh2dBundle {

examples/menu.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
5050
start_time_ms += 500;
5151
let tween_scale = Tween::new(
5252
EaseFunction::BounceOut,
53-
TweeningType::Once,
5453
Duration::from_secs(2),
5554
TransformScaleLens {
5655
start: Vec3::splat(0.01),

examples/sequence.rs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
use std::time::Duration;
2+
13
use bevy::prelude::*;
4+
25
use bevy_tweening::{lens::*, *};
3-
use std::time::Duration;
46

57
fn main() {
68
App::default()
@@ -107,19 +109,31 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
107109
Vec3::new(margin, screen_y - margin, 0.),
108110
Vec3::new(margin, margin, 0.),
109111
];
110-
// Build a sequence from an iterator over a Tweenable (here, a Tween<Transform>)
112+
// Build a sequence from an iterator over a Tweenable (here, a
113+
// Tracks<Transform>)
111114
let seq = Sequence::new(dests.windows(2).enumerate().map(|(index, pair)| {
112-
Tween::new(
113-
EaseFunction::QuadraticInOut,
114-
TweeningType::Once,
115-
Duration::from_secs(1),
116-
TransformPositionLens {
117-
start: pair[0] - center,
118-
end: pair[1] - center,
119-
},
120-
)
121-
// Get an event after each segment
122-
.with_completed_event(index as u64)
115+
Tracks::new([
116+
Tween::new(
117+
EaseFunction::QuadraticInOut,
118+
Duration::from_millis(250),
119+
TransformRotateZLens {
120+
start: 0.,
121+
end: 180_f32.to_radians(),
122+
},
123+
)
124+
.with_repeat_count(RepeatCount::Finite(4))
125+
.with_repeat_strategy(RepeatStrategy::MirroredRepeat),
126+
Tween::new(
127+
EaseFunction::QuadraticInOut,
128+
Duration::from_secs(1),
129+
TransformPositionLens {
130+
start: pair[0] - center,
131+
end: pair[1] - center,
132+
},
133+
)
134+
// Get an event after each segment
135+
.with_completed_event(index as u64),
136+
])
123137
}));
124138

125139
commands
@@ -138,7 +152,6 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
138152
// scaling size at the same time.
139153
let tween_move = Tween::new(
140154
EaseFunction::QuadraticInOut,
141-
TweeningType::Once,
142155
Duration::from_secs(1),
143156
TransformPositionLens {
144157
start: Vec3::new(-200., 100., 0.),
@@ -148,7 +161,6 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
148161
.with_completed_event(99); // Get an event once move completed
149162
let tween_rotate = Tween::new(
150163
EaseFunction::QuadraticInOut,
151-
TweeningType::Once,
152164
Duration::from_secs(1),
153165
TransformRotationLens {
154166
start: Quat::IDENTITY,
@@ -157,7 +169,6 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
157169
);
158170
let tween_scale = Tween::new(
159171
EaseFunction::QuadraticInOut,
160-
TweeningType::Once,
161172
Duration::from_secs(1),
162173
TransformScaleLens {
163174
start: Vec3::ONE,

examples/sprite_color.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@ fn setup(mut commands: Commands) {
6161
] {
6262
let tween = Tween::new(
6363
*ease_function,
64-
TweeningType::PingPong,
6564
std::time::Duration::from_secs(1),
6665
SpriteColorLens {
6766
start: Color::RED,
6867
end: Color::BLUE,
6968
},
70-
);
69+
)
70+
.with_repeat_count(RepeatCount::Infinite)
71+
.with_repeat_strategy(RepeatStrategy::MirroredRepeat);
7172

7273
commands
7374
.spawn_bundle(SpriteBundle {

examples/text_color.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
6868
] {
6969
let tween = Tween::new(
7070
*ease_function,
71-
TweeningType::PingPong,
7271
std::time::Duration::from_secs(1),
7372
TextColorLens {
7473
start: Color::RED,
7574
end: Color::BLUE,
7675
section: 0,
7776
},
78-
);
77+
)
78+
.with_repeat_count(RepeatCount::Infinite)
79+
.with_repeat_strategy(RepeatStrategy::MirroredRepeat);
7980

8081
commands
8182
.spawn_bundle(TextBundle {

examples/transform_rotation.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,14 @@ fn setup(mut commands: Commands) {
7777
] {
7878
let tween = Tween::new(
7979
*ease_function,
80-
TweeningType::PingPong,
8180
std::time::Duration::from_secs(1),
8281
TransformRotationLens {
8382
start: Quat::IDENTITY,
8483
end: Quat::from_axis_angle(Vec3::Z, std::f32::consts::PI / 2.),
8584
},
86-
);
85+
)
86+
.with_repeat_count(RepeatCount::Infinite)
87+
.with_repeat_strategy(RepeatStrategy::MirroredRepeat);
8788

8889
commands
8990
.spawn_bundle((

examples/transform_translation.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,14 @@ fn setup(mut commands: Commands) {
7676
] {
7777
let tween = Tween::new(
7878
*ease_function,
79-
TweeningType::PingPong,
8079
std::time::Duration::from_secs(1),
8180
TransformPositionLens {
8281
start: Vec3::new(x, screen_y, 0.),
8382
end: Vec3::new(x, -screen_y, 0.),
8483
},
85-
);
84+
)
85+
.with_repeat_count(RepeatCount::Infinite)
86+
.with_repeat_strategy(RepeatStrategy::MirroredRepeat);
8687

8788
commands
8889
.spawn_bundle(SpriteBundle {

examples/ui_position.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ fn setup(mut commands: Commands) {
7676
] {
7777
let tween = Tween::new(
7878
*ease_function,
79-
TweeningType::PingPong,
8079
std::time::Duration::from_secs(1),
8180
UiPositionLens {
8281
start: Rect {
@@ -92,7 +91,9 @@ fn setup(mut commands: Commands) {
9291
bottom: Val::Auto,
9392
},
9493
},
95-
);
94+
)
95+
.with_repeat_count(RepeatCount::Infinite)
96+
.with_repeat_strategy(RepeatStrategy::MirroredRepeat);
9697

9798
commands
9899
.spawn_bundle(NodeBundle {

0 commit comments

Comments
 (0)