From 5c5a929fec0c26dafec1eaa6e113bced980ac4dd Mon Sep 17 00:00:00 2001 From: Sergio Ribera Date: Sat, 7 Oct 2023 19:34:57 -0400 Subject: [PATCH 1/2] fix: text sections lens color management --- examples/custom_skip.rs | 5 +---- examples/simple.rs | 2 +- src/lens.rs | 32 +++++++++++++++----------------- src/splash.rs | 32 ++++++++++++++++++++++++++++---- 4 files changed, 45 insertions(+), 26 deletions(-) diff --git a/examples/custom_skip.rs b/examples/custom_skip.rs index 17d27fe..8847785 100644 --- a/examples/custom_skip.rs +++ b/examples/custom_skip.rs @@ -131,10 +131,7 @@ fn create_scene(mut cmd: Commands, assets: ResMut) { Tween::new( EaseFunction::QuadraticInOut, Duration::from_secs(3), - SplashTextColorLens { - start: Color::WHITE, - end: Color::WHITE.with_a(0.), - }, + SplashTextColorLens::new(vec![Color::WHITE]), ) .with_repeat_count(RepeatCount::Infinite) .with_repeat_strategy(RepeatStrategy::MirroredRepeat), diff --git a/examples/simple.rs b/examples/simple.rs index 77863a7..fbbd5ab 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -43,7 +43,7 @@ fn main() { "Sergio Ribera", TextStyle { font_size: 32., - color: Color::WHITE, + color: Color::BLUE, ..default() }, ), diff --git a/src/lens.rs b/src/lens.rs index 17a9844..6c2d020 100644 --- a/src/lens.rs +++ b/src/lens.rs @@ -13,29 +13,27 @@ pub struct SplashImageColorLens { pub end: Color, } -#[derive(Debug, Copy, Clone, PartialEq)] -pub struct SplashTextColorLens { - /// Start color. - pub start: Color, - /// End color. - pub end: Color, -} +#[derive(Debug, Clone, PartialEq)] +/// Lens for interpolating Bevy Text sections. The single parameter is a reference to the color of each section. +pub struct SplashTextColorLens(Vec); -impl InstanceLens for SplashTextColorLens { - fn create(start: Color, end: Color) -> Self { - Self { start, end } +impl SplashTextColorLens { + /// Create instance of Text Lens + /// + /// * `colors`: Each color refers to a section and is placed in order. + pub fn new(colors: Vec) -> Self { + Self(colors) } } impl Lens for SplashTextColorLens { fn lerp(&mut self, target: &mut Text, ratio: f32) { - let start: Vec4 = self.start.into(); - let end: Vec4 = self.end.into(); - let value = start.lerp(end, ratio); - target - .sections - .iter_mut() - .for_each(|section| section.style.color = value.into()); + target.sections.iter_mut().enumerate().for_each(|(i, section)| { + let start: Vec4 = self.0[i].with_a(0.).into(); + let end: Vec4 = self.0[i].into(); + let value = start.lerp(end, ratio); + section.style.color = value.into(); + }); } } diff --git a/src/splash.rs b/src/splash.rs index d9db2f2..9a9a9a5 100644 --- a/src/splash.rs +++ b/src/splash.rs @@ -111,10 +111,34 @@ pub(crate) fn create_splash( }, ..default() }, - create_animator::( - brand, - max_duration, - i_screen, + Animator::new( + Tween::new( + brand.ease_function, + Duration::from_secs(1), + SplashTextColorLens::new( + text.sections + .iter() + .map(|_| Color::WHITE.with_a(0.)) + .collect(), + ), + ) + .then( + Delay::new(max_duration).then( + Tween::new( + brand.ease_function, + brand.duration, + SplashTextColorLens::new( + text.sections + .iter() + .map(|s| s.style.color) + .collect(), + ), + ) + .with_repeat_strategy(RepeatStrategy::MirroredRepeat) + .with_repeat_count(RepeatCount::Finite(2)) + .with_completed_event(i_screen as u64), + ), + ), ), )) } From 7a1c95b0703071db244c014116073e0c317f027f Mon Sep 17 00:00:00 2001 From: Sergio Ribera Date: Sat, 7 Oct 2023 19:40:31 -0400 Subject: [PATCH 2/2] fix: format style --- src/lens.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/lens.rs b/src/lens.rs index 6c2d020..32bde73 100644 --- a/src/lens.rs +++ b/src/lens.rs @@ -28,12 +28,16 @@ impl SplashTextColorLens { impl Lens for SplashTextColorLens { fn lerp(&mut self, target: &mut Text, ratio: f32) { - target.sections.iter_mut().enumerate().for_each(|(i, section)| { - let start: Vec4 = self.0[i].with_a(0.).into(); - let end: Vec4 = self.0[i].into(); - let value = start.lerp(end, ratio); - section.style.color = value.into(); - }); + target + .sections + .iter_mut() + .enumerate() + .for_each(|(i, section)| { + let start: Vec4 = self.0[i].with_a(0.).into(); + let end: Vec4 = self.0[i].into(); + let value = start.lerp(end, ratio); + section.style.color = value.into(); + }); } }