saving my place; cool but slow text effect

main
Elijah Voigt 2 years ago
parent 708c7ac54d
commit 43281fbf12

@ -1,4 +1,4 @@
use bevy::prelude::*;
use bevy::{diagnostic::FrameTimeDiagnosticsPlugin, prelude::*};
const LOREM: [&str; 5] = [
"Ullam nostrum aut amet adipisci consequuntur quisquam nemo consequatur. Vel eum et ullam ullam aperiam earum voluptas consequuntur. Blanditiis earum voluptatem voluptas animi dolorum fuga aliquam ea.\n",
@ -12,7 +12,7 @@ fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "Text WTF".into(),
title: "Text Inspect".into(),
resolution: (640., 480.).into(),
..default()
}),
@ -23,6 +23,8 @@ fn main() {
.add_system(manage_buttons)
.add_system(update)
.add_system(mouse_cursor)
.add_system(play)
.add_system(framerate)
.run();
}
@ -86,19 +88,24 @@ fn init_ui(mut commands: Commands) {
ButtonShelf,
));
{
parent.spawn((
TextBundle::from_sections(LOREM.iter().map(|&section| TextSection {
value: section.into(),
style: TextStyle {
font_size: 50.0,
..default()
},
})),
Marker,
PreviewText,
));
}
parent.spawn((
TextBundle::from_sections(LOREM.iter().map(|&section| TextSection {
value: section.into(),
style: TextStyle {
font_size: 50.0,
..default()
},
}))
.with_style(Style {
max_size: Size {
width: Val::Px(500.),
height: Val::Undefined,
},
..default()
}),
Marker,
PreviewText,
));
});
}
@ -166,13 +173,15 @@ fn update(
match (i, f) {
(Interaction::Clicked, FontButton(font)) => {
let mut text = texts.single_mut();
*text = Text::from_sections(LOREM.iter().map(|&s| TextSection {
value: s.into(),
style: TextStyle {
font: font.clone(),
font_size: 50.0,
..default()
},
*text = Text::from_sections(LOREM.iter().flat_map(|&l| {
l.chars().map(|c| TextSection {
value: c.into(),
style: TextStyle {
font: font.clone(),
font_size: 50.0,
..default()
},
})
}));
}
_ => (),
@ -193,3 +202,39 @@ fn mouse_cursor(
}
}
}
fn play(
mut texts: Query<&mut Text, (With<Marker>, With<PreviewText>)>,
interactions: Query<&Interaction, (Changed<Interaction>, With<FontButton>)>,
mut playing: Local<bool>,
time: Res<Time>,
mut desired: Local<String>,
) {
for interaction in interactions.iter() {
match interaction {
Interaction::Clicked => *playing = true,
_ => (),
}
}
if *playing {
let mut text = texts.single_mut();
let total_sections = text.sections.len();
let curr_cos = time.elapsed().as_secs_f32().cos().abs();
for (idx, section) in text.sections.iter_mut().enumerate() {
let idx_ratio = (total_sections as f32) / ((idx + 1) as f32);
let intensity = (curr_cos - idx_ratio).abs() % 1.0;
section.style.color.set_a(intensity);
}
// Get text sections
//
// figure out how far into animation we are
//
// Set current vs desired ratio
//
// Set current to that % from empty to desired
}
}

Loading…
Cancel
Save