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] = [ 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", "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() App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin { .add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window { primary_window: Some(Window {
title: "Text WTF".into(), title: "Text Inspect".into(),
resolution: (640., 480.).into(), resolution: (640., 480.).into(),
..default() ..default()
}), }),
@ -23,6 +23,8 @@ fn main() {
.add_system(manage_buttons) .add_system(manage_buttons)
.add_system(update) .add_system(update)
.add_system(mouse_cursor) .add_system(mouse_cursor)
.add_system(play)
.add_system(framerate)
.run(); .run();
} }
@ -86,19 +88,24 @@ fn init_ui(mut commands: Commands) {
ButtonShelf, ButtonShelf,
)); ));
{ parent.spawn((
parent.spawn(( TextBundle::from_sections(LOREM.iter().map(|&section| TextSection {
TextBundle::from_sections(LOREM.iter().map(|&section| TextSection { value: section.into(),
value: section.into(), style: TextStyle {
style: TextStyle { font_size: 50.0,
font_size: 50.0, ..default()
..default() },
}, }))
})), .with_style(Style {
Marker, max_size: Size {
PreviewText, width: Val::Px(500.),
)); height: Val::Undefined,
} },
..default()
}),
Marker,
PreviewText,
));
}); });
} }
@ -166,13 +173,15 @@ fn update(
match (i, f) { match (i, f) {
(Interaction::Clicked, FontButton(font)) => { (Interaction::Clicked, FontButton(font)) => {
let mut text = texts.single_mut(); let mut text = texts.single_mut();
*text = Text::from_sections(LOREM.iter().map(|&s| TextSection { *text = Text::from_sections(LOREM.iter().flat_map(|&l| {
value: s.into(), l.chars().map(|c| TextSection {
style: TextStyle { value: c.into(),
font: font.clone(), style: TextStyle {
font_size: 50.0, font: font.clone(),
..default() 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