Skip to content

Commit

Permalink
Merge pull request #15 from SergioRibera/text_sections_color
Browse files Browse the repository at this point in the history
fix #10 - Inconsistent color definition
  • Loading branch information
SergioRibera authored Oct 7, 2023
2 parents 8d47d45 + 7a1c95b commit 1045609
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 23 deletions.
5 changes: 1 addition & 4 deletions examples/custom_skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,7 @@ fn create_scene(mut cmd: Commands, assets: ResMut<AssetServer>) {
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),
Expand Down
2 changes: 1 addition & 1 deletion examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn main() {
"Sergio Ribera",
TextStyle {
font_size: 32.,
color: Color::WHITE,
color: Color::BLUE,
..default()
},
),
Expand Down
30 changes: 16 additions & 14 deletions src/lens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,31 @@ 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<Color>);

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<Color>) -> Self {
Self(colors)
}
}

impl Lens<Text> 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());
.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();
});
}
}

Expand Down
32 changes: 28 additions & 4 deletions src/splash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,34 @@ pub(crate) fn create_splash(
},
..default()
},
create_animator::<Text, SplashTextColorLens>(
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),
),
),
),
))
}
Expand Down

0 comments on commit 1045609

Please sign in to comment.