Skip to content

Commit

Permalink
Merge 2ee6180 into 5e6214b
Browse files Browse the repository at this point in the history
  • Loading branch information
dgsantana committed Feb 27, 2024
2 parents 5e6214b + 2ee6180 commit c3773bc
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 31 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ bevy_ui = ["bevy/bevy_ui", "bevy/bevy_render"]
bevy_text = ["bevy/bevy_text", "bevy/bevy_render", "bevy/bevy_sprite"]

[dependencies]
interpolation = "0.2"
bevy = { version = "0.12", default-features = false }
interpolation = "0.3"
bevy = { version = "0.13", default-features = false }

[dev-dependencies]
bevy-inspector-egui = "0.21"
bevy-inspector-egui = "0.23"

[[example]]
name = "menu"
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ include = ["assets", "thirdparty"]
exclude = ["examples/*.gif"]

[dependencies]
criterion = { version = "0.4", features = ["html_reports"] }
criterion = { version = "0.5", features = ["html_reports"] }
bevy_tweening = { path = "../" }

[dependencies.bevy]
version = "0.12"
version = "0.13"
default-features = false
features = ["bevy_render", "bevy_sprite", "bevy_text", "bevy_ui"]

Expand Down
28 changes: 12 additions & 16 deletions src/lens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@

use bevy::prelude::*;

/// Required for color lerp.
#[allow(unused_imports)]
use crate::Lerper as _;

/// A lens over a subset of a component.
///
/// The lens takes a `target` component or asset from a query, as a mutable
Expand Down Expand Up @@ -95,12 +99,10 @@ impl Lens<Text> for TextColorLens {
fn lerp(&mut self, target: &mut Text, ratio: f32) {
// Note: Add<f32> for Color affects alpha, but not Mul<f32>. So use Vec4 for
// consistency.
let start: Vec4 = self.start.into();
let end: Vec4 = self.end.into();
let value = start.lerp(end, ratio);
let value = self.start.lerp(&self.end, ratio);

if let Some(section) = target.sections.get_mut(self.section) {
section.style.color = value.into();
section.style.color = value;
}
}
}
Expand Down Expand Up @@ -335,10 +337,8 @@ pub struct UiBackgroundColorLens {
#[cfg(feature = "bevy_ui")]
impl Lens<BackgroundColor> for UiBackgroundColorLens {
fn lerp(&mut self, target: &mut BackgroundColor, ratio: f32) {
let start: Vec4 = self.start.into();
let end: Vec4 = self.end.into();
let value = start.lerp(end, ratio);
target.0 = value.into();
let value = self.start.lerp(&self.end, ratio);
target.0 = value;
}
}

Expand All @@ -360,10 +360,8 @@ impl Lens<ColorMaterial> for ColorMaterialColorLens {
fn lerp(&mut self, target: &mut ColorMaterial, ratio: f32) {
// Note: Add<f32> for Color affects alpha, but not Mul<f32>. So use Vec4 for
// consistency.
let start: Vec4 = self.start.into();
let end: Vec4 = self.end.into();
let value = start.lerp(end, ratio);
target.color = value.into();
let value = self.start.lerp(&self.end, ratio);
target.color = value;
}
}

Expand All @@ -385,10 +383,8 @@ impl Lens<Sprite> for SpriteColorLens {
fn lerp(&mut self, target: &mut Sprite, ratio: f32) {
// Note: Add<f32> for Color affects alpha, but not Mul<f32>. So use Vec4 for
// consistency.
let start: Vec4 = self.start.into();
let end: Vec4 = self.end.into();
let value = start.lerp(end, ratio);
target.color = value.into();
let value = self.start.lerp(&self.end, ratio);
target.color = value;
}
}

Expand Down
28 changes: 21 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,11 +539,26 @@ impl<T: Asset> AssetAnimator<T> {
animator_impl!();
}

/// Trait to interpolate between two values.
/// Needed for color.
#[allow(dead_code)]
trait Lerper {
fn lerp(&self, target: &Self, ratio: f32) -> Self;
}

#[allow(dead_code)]
impl Lerper for Color {
fn lerp(&self, target: &Color, ratio: f32) -> Color {
let r = self.r().lerp(target.r(), ratio);
let g = self.g().lerp(target.g(), ratio);
let b = self.b().lerp(target.b(), ratio);
let a = self.a().lerp(target.a(), ratio);
Color::rgba(r, g, b, a)
}
}

#[cfg(test)]
mod tests {
#[cfg(feature = "bevy_asset")]
use bevy::reflect::TypeUuid;

use super::*;
use crate::test_utils::*;

Expand All @@ -558,15 +573,14 @@ mod tests {
}

#[cfg(feature = "bevy_asset")]
#[derive(Asset, Debug, Default, Reflect, TypeUuid)]
#[uuid = "a33abc11-264e-4bbb-82e8-b87226bb4383"]
#[derive(Asset, Debug, Default, Reflect)]
struct DummyAsset {
value: f32,
}

impl Lens<DummyComponent> for DummyLens {
fn lerp(&mut self, target: &mut DummyComponent, ratio: f32) {
target.value = self.start.lerp(&self.end, &ratio);
target.value = self.start.lerp(self.end, ratio);
}
}

Expand All @@ -583,7 +597,7 @@ mod tests {
#[cfg(feature = "bevy_asset")]
impl Lens<DummyAsset> for DummyLens {
fn lerp(&mut self, target: &mut DummyAsset, ratio: f32) {
target.value = self.start.lerp(&self.end, &ratio);
target.value = self.start.lerp(self.end, ratio);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(feature = "bevy_asset")]
use bevy::asset::Asset;
use bevy::{ecs::component::Component, prelude::*};
use bevy::prelude::*;

#[cfg(feature = "bevy_asset")]
use crate::{tweenable::AssetTarget, AssetAnimator};
Expand Down
4 changes: 2 additions & 2 deletions src/tweenable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ impl<T> Tween<T> {
/// .with_completed_event(42);
///
/// fn my_system(mut reader: EventReader<TweenCompleted>) {
/// for ev in reader.iter() {
/// for ev in reader.read() {
/// assert_eq!(ev.user_data, 42);
/// println!("Entity {:?} raised TweenCompleted!", ev.entity);
/// }
Expand Down Expand Up @@ -991,7 +991,7 @@ impl<T> Delay<T> {
/// .with_completed_event(42);
///
/// fn my_system(mut reader: EventReader<TweenCompleted>) {
/// for ev in reader.iter() {
/// for ev in reader.read() {
/// assert_eq!(ev.user_data, 42);
/// println!("Entity {:?} raised TweenCompleted!", ev.entity);
/// }
Expand Down

0 comments on commit c3773bc

Please sign in to comment.