Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Replace progress APIs in favor of elapsed APIs #38

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
9288b0f
Move towards more flexible animation looping
SUPERCILEX May 30, 2022
30b077a
Don't use nightly stuff
SUPERCILEX May 30, 2022
c01b3ea
Merge remote-tracking branch 'upstream/main' into loop
SUPERCILEX Jun 7, 2022
3df8870
Get rid of is_looping and finish implementing stuff
SUPERCILEX Jun 7, 2022
4e15064
Reformat all comments using rustfmt
SUPERCILEX Jun 7, 2022
31ef21f
Merge branch 'comments' into loop
SUPERCILEX Jun 7, 2022
178db98
Final cleanup
SUPERCILEX Jun 7, 2022
351c180
Merge remote-tracking branch 'upstream/main' into loop
SUPERCILEX Jun 14, 2022
e119f73
Tidy
SUPERCILEX Jun 14, 2022
2f96f06
Use repeat and mirrored repeat terminology
SUPERCILEX Jul 2, 2022
90bd931
Scootch duration to a place that makes more sense
SUPERCILEX Jul 3, 2022
7e2aac1
Merge remote-tracking branch 'upstream/main' into loop
SUPERCILEX Jul 11, 2022
af67aaa
Start implementing plan
SUPERCILEX Jul 11, 2022
74a79cc
Fix very odd failure that doesn't occur on nightly
SUPERCILEX Jul 11, 2022
595c2f9
Remove debug statement
SUPERCILEX Jul 11, 2022
035c8cc
Address feedback
SUPERCILEX Jul 12, 2022
ed9b383
Progress
SUPERCILEX Aug 2, 2022
d3f208d
Merge remote-tracking branch 'upstream/main' into elapsed
SUPERCILEX Aug 4, 2022
d098f83
Add back bad merge stuff
SUPERCILEX Aug 4, 2022
2212592
Fix broken code
SUPERCILEX Aug 4, 2022
cf7bdb9
Progress
SUPERCILEX Aug 4, 2022
5691216
Blah
SUPERCILEX Aug 4, 2022
96cab31
Finish implementation
SUPERCILEX Aug 6, 2022
a1352ec
Merge remote-tracking branch 'upstream/main' into elapsed
SUPERCILEX Aug 10, 2022
ce32b99
Fix bad merge
SUPERCILEX Aug 10, 2022
fa4463f
Get rid of progress state in favor of computing it on-the-fly
SUPERCILEX Aug 10, 2022
115fe63
Fix
SUPERCILEX Aug 12, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 26 additions & 55 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,27 +151,25 @@
//! [`Sprite`]: https://docs.rs/bevy/0.8.0/bevy/sprite/struct.Sprite.html
//! [`Transform`]: https://docs.rs/bevy/0.8.0/bevy/transform/components/struct.Transform.html

use bevy::prelude::*;
use std::time::Duration;

#[cfg(feature = "bevy_asset")]
use bevy::asset::Asset;

use bevy::prelude::*;
use interpolation::Ease as IEase;
pub use interpolation::{EaseFunction, Lerp};

pub mod lens;
mod plugin;
mod tweenable;

pub use lens::Lens;
#[cfg(feature = "bevy_asset")]
pub use plugin::asset_animator_system;
pub use plugin::{component_animator_system, AnimationSystem, TweeningPlugin};
pub use tweenable::{
BoxedTweenable, Delay, Sequence, Tracks, Tween, TweenCompleted, TweenState, Tweenable,
};

#[cfg(feature = "bevy_asset")]
pub use plugin::asset_animator_system;
pub mod lens;
mod plugin;
mod tweenable;

/// How many times to repeat a tween animation. See also: [`RepeatStrategy`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -388,35 +386,6 @@ macro_rules! animator_impl {
}
}

/// Set the current animation playback progress.
///
/// See [`progress()`] for details on the meaning.
///
/// [`progress()`]: Animator::progress
pub fn set_progress(&mut self, progress: f32) {
if let Some(tweenable) = &mut self.tweenable {
tweenable.set_progress(progress)
}
}

/// Get the current progress of the tweenable. See [`Tweenable::progress`] for
/// details.
///
/// For sequences, the progress is measured over the entire sequence, from 0 at
/// the start of the first child tweenable to 1 at the end of the last one.
///
/// For tracks (parallel execution), the progress is measured like a sequence
/// over the longest "path" of child tweenables. In other words, this is the
/// current elapsed time over the total tweenable duration.
#[must_use]
pub fn progress(&self) -> f32 {
if let Some(tweenable) = &self.tweenable {
tweenable.progress()
} else {
0.
}
}

/// Ticks the tween, if present. See [`Tweenable::tick`] for details.
pub fn tick(
&mut self,
Expand Down Expand Up @@ -549,6 +518,8 @@ mod tests {
#[cfg(feature = "bevy_asset")]
use bevy::reflect::TypeUuid;

use crate::tweenable::TweenableExt;

use super::{lens::*, *};

struct DummyLens {
Expand Down Expand Up @@ -690,32 +661,32 @@ mod tests {
);
let mut animator = Animator::new(tween);
assert_eq!(animator.state, AnimatorState::Playing);
assert!(animator.progress().abs() <= 1e-5);
assert!(animator.tweenable().unwrap().progress().abs() <= 1e-5);

animator.stop();
assert_eq!(animator.state, AnimatorState::Paused);
assert!(animator.progress().abs() <= 1e-5);
assert!(animator.tweenable().unwrap().progress().abs() <= 1e-5);

animator.set_progress(0.5);
animator.tweenable_mut().unwrap().set_progress(0.5);
assert_eq!(animator.state, AnimatorState::Paused);
assert!((animator.progress() - 0.5).abs() <= 1e-5);
assert!((animator.tweenable().unwrap().progress() - 0.5).abs() <= 1e-5);

animator.rewind();
assert_eq!(animator.state, AnimatorState::Paused);
assert!(animator.progress().abs() <= 1e-5);
assert!(animator.tweenable().unwrap().progress().abs() <= 1e-5);

animator.set_progress(0.5);
animator.tweenable_mut().unwrap().set_progress(0.5);
animator.state = AnimatorState::Playing;
assert_eq!(animator.state, AnimatorState::Playing);
assert!((animator.progress() - 0.5).abs() <= 1e-5);
assert!((animator.tweenable().unwrap().progress() - 0.5).abs() <= 1e-5);

animator.rewind();
assert_eq!(animator.state, AnimatorState::Playing);
assert!(animator.progress().abs() <= 1e-5);
assert!(animator.tweenable().unwrap().progress().abs() <= 1e-5);

animator.stop();
assert_eq!(animator.state, AnimatorState::Paused);
assert!(animator.progress().abs() <= 1e-5);
assert!(animator.tweenable().unwrap().progress().abs() <= 1e-5);
}

/// AssetAnimator::new()
Expand Down Expand Up @@ -780,31 +751,31 @@ mod tests {
);
let mut animator = AssetAnimator::new(Handle::<DummyAsset>::default(), tween);
assert_eq!(animator.state, AnimatorState::Playing);
assert!(animator.progress().abs() <= 1e-5);
assert!(animator.tweenable().unwrap().progress().abs() <= 1e-5);

animator.stop();
assert_eq!(animator.state, AnimatorState::Paused);
assert!(animator.progress().abs() <= 1e-5);
assert!(animator.tweenable().unwrap().progress().abs() <= 1e-5);

animator.set_progress(0.5);
animator.tweenable_mut().unwrap().set_progress(0.5);
assert_eq!(animator.state, AnimatorState::Paused);
assert!((animator.progress() - 0.5).abs() <= 1e-5);
assert!((animator.tweenable().unwrap().progress() - 0.5).abs() <= 1e-5);

animator.rewind();
assert_eq!(animator.state, AnimatorState::Paused);
assert!(animator.progress().abs() <= 1e-5);
assert!(animator.tweenable().unwrap().progress().abs() <= 1e-5);

animator.set_progress(0.5);
animator.tweenable_mut().unwrap().set_progress(0.5);
animator.state = AnimatorState::Playing;
assert_eq!(animator.state, AnimatorState::Playing);
assert!((animator.progress() - 0.5).abs() <= 1e-5);
assert!((animator.tweenable().unwrap().progress() - 0.5).abs() <= 1e-5);

animator.rewind();
assert_eq!(animator.state, AnimatorState::Playing);
assert!(animator.progress().abs() <= 1e-5);
assert!(animator.tweenable().unwrap().progress().abs() <= 1e-5);

animator.stop();
assert_eq!(animator.state, AnimatorState::Paused);
assert!(animator.progress().abs() <= 1e-5);
assert!(animator.tweenable().unwrap().progress().abs() <= 1e-5);
}
}
Loading