saving my place

main
Elijah Voigt 2 years ago
parent 09d48951bc
commit d1a7a7f73d

@ -27,6 +27,7 @@ fn main() {
.add_startup_system(load_fonts)
.add_startup_system(init_ui)
.add_system(manage_buttons)
.add_system(manage_animation_button)
.add_system(manage_fonts)
.add_system(mouse_cursor)
.add_system(manage_animation)
@ -36,9 +37,6 @@ fn main() {
#[derive(Resource)]
struct Fonts(Vec<Handle<Font>>);
#[derive(Component)]
struct Marker;
#[derive(Component)]
struct PreviewText;
@ -48,6 +46,9 @@ struct ButtonShelf;
#[derive(Component)]
struct FontButton(Handle<Font>);
#[derive(Component)]
struct AnimationButton;
fn load_fonts(mut commands: Commands, server: Res<AssetServer>) {
let handles = server
.load_folder("fonts")
@ -62,17 +63,14 @@ fn init_ui(mut commands: Commands) {
commands.spawn(Camera2dBundle { ..default() });
commands
.spawn((
NodeBundle {
.spawn(NodeBundle {
style: Style {
size: Size::all(Val::Percent(100.0)),
..default()
},
background_color: BackgroundColor(Color::BLACK),
..default()
},
Marker,
))
})
.with_children(|parent| {
parent.spawn((
NodeBundle {
@ -89,18 +87,81 @@ fn init_ui(mut commands: Commands) {
background_color: BackgroundColor(Color::BLACK),
..default()
},
Marker,
ButtonShelf,
));
parent.spawn((
AnimatedTextBundle {
text_bundle: TextBundle { ..default() },
text_bundle: TextBundle {
text: Text {
sections: LOREM
.iter()
.map(|&line| TextSection {
value: line.into(),
style: TextStyle {
font_size: 18.0,
color: Color::WHITE,
..default()
},
})
.collect(),
..default()
},
style: Style {
size: Size {
width: Val::Px(400.0),
..default()
},
..default()
},
..default()
},
animated_text: AnimatedText::new(TextAnimationType::Typing(12.0)),
},
Marker,
PreviewText,
));
parent
.spawn((
ButtonBundle {
style: Style {
align_self: AlignSelf::FlexEnd,
position_type: PositionType::Absolute,
position: UiRect {
bottom: Val::Px(5.0),
left: Val::Px(5.0),
..default()
},
size: Size {
width: Val::Px(200.0),
..default()
},
..default()
},
background_color: BackgroundColor(Color::default().with_a(0.0)),
..default()
},
AnimationButton,
))
.with_children(|parent| {
parent.spawn((
TextBundle {
text: Text {
sections: vec![TextSection {
value: "Toggle Animation".into(),
style: TextStyle {
font_size: 12.0,
color: Color::WHITE,
..default()
},
}],
..default()
},
..default()
},
AnimationButton,
));
});
});
}
@ -109,10 +170,10 @@ fn manage_buttons(
mut commands: Commands,
fonts: Res<Fonts>,
server: Res<AssetServer>,
query: Query<Entity, (With<Marker>, With<ButtonShelf>)>,
shelf_query: Query<Entity, With<ButtonShelf>>,
) {
if fonts.is_added() || fonts.is_changed() {
let root = query.get_single().expect("Fetching root UI node");
let root = shelf_query.get_single().expect("Fetching root UI node");
let mut root_cmd = commands.get_entity(root).expect("Root UI node commands");
root_cmd.clear_children();
@ -149,11 +210,10 @@ fn manage_buttons(
..default()
},
FontButton(font.clone()),
Marker,
))
.with_children(|parent| {
info!("Adding {} button", fname);
parent.spawn((TextBundle::from_section(fname, style), Marker));
parent.spawn(TextBundle::from_section(fname, style));
});
});
});
@ -161,7 +221,7 @@ fn manage_buttons(
}
fn manage_fonts(
mut texts: Query<&mut Text, (With<Marker>, With<PreviewText>)>,
mut texts: Query<&mut Text, With<PreviewText>>,
interaction: Query<(&Interaction, &FontButton), Changed<Interaction>>,
) {
for (i, f) in interaction.iter() {
@ -176,6 +236,24 @@ fn manage_fonts(
}
}
fn manage_animation_button(
mut animation_button: Query<&mut Text, With<AnimationButton>>,
interaction: Query<(&Interaction, &FontButton), Changed<Interaction>>,
) {
for (i, f) in interaction.iter() {
match (i, f) {
(Interaction::Clicked, FontButton(font)) => {
animation_button
.single_mut()
.sections
.iter_mut()
.for_each(|section| section.style.font = font.clone());
}
_ => (),
}
}
}
fn mouse_cursor(
mut windows: Query<&mut Window>,
interactions: Query<&Interaction, (Changed<Interaction>, With<Button>)>,
@ -191,16 +269,16 @@ fn mouse_cursor(
}
fn manage_animation(
mut texts: Query<&mut Text, (With<Marker>, With<PreviewText>)>,
mut animated_texts: Query<&mut AnimatedText, With<PreviewText>>,
interactions: Query<&Interaction, (Changed<Interaction>, With<FontButton>)>,
time: Res<Time>,
mut duration: Local<Duration>,
mut desired: Local<Vec<String>>,
) {
for interaction in interactions.iter() {
match interaction {
Interaction::Clicked => {
// Start playing animation
let mut preview = animated_texts
.get_single_mut()
.expect("Loading animated text");
preview.play();
}
_ => (),
}

@ -29,7 +29,7 @@ pub struct AnimatedTextBundle {
/// Animated Text Marker
/// Use this to filter out entities managed by Text Animation systems
#[derive(Component, Default)]
#[derive(Component, Default, Debug)]
pub struct AnimatedText {
animation_type: Option<TextAnimationType>,
animation_status: TextAnimationStatus,
@ -61,11 +61,12 @@ impl AnimatedText {
}
}
#[derive(Debug)]
pub enum TextAnimationType {
Typing(f32), // Typing text out for duration
}
#[derive(Default)]
#[derive(Default, Debug)]
enum TextAnimationStatus {
#[default]
Stopped,
@ -81,7 +82,7 @@ enum TextAnimationStatus {
/// Any time a text is updated we need to ensure it conforms to the needs of it's animation
///
/// FIXME: Only update according to animation type
fn manage_texts(mut texts: Query<&mut Text, Changed<Text>>) {
fn manage_texts(mut texts: Query<&mut Text, (Changed<Text>, With<AnimatedText>)>) {
// Check if any Text entities are "dirty"
let dirty = texts
.iter()
@ -111,29 +112,35 @@ fn manage_texts(mut texts: Query<&mut Text, Changed<Text>>) {
}
}
fn animate_texts(
mut query: Query<(&mut Text, &mut AnimatedText), With<AnimatedText>>,
time: Res<Time>,
) {
fn animate_texts(mut query: Query<(&mut Text, &mut AnimatedText)>, time: Res<Time>) {
for (mut text, mut animated_text) in query.iter_mut() {
match animated_text.animation_status {
TextAnimationStatus::Stopped => (),
TextAnimationStatus::Playing => match animated_text.animation_type {
None => (),
Some(TextAnimationType::Typing(seconds)) => {
match animated_text.animation_duration {
None => {
// We have just started the animation, so set the duration appropriately
animated_text.animation_duration =
Some(Duration::from_secs_f32(seconds));
animated_text.animation_duration = match animated_text.animation_duration {
None | Some(Duration::ZERO) => {
// We have just (re)started the animation, so set the duration to full
Some(Duration::from_secs_f32(seconds))
}
// FIXME: Why can't mutate inner on animation_duration?
Some(inner) => {
{
let percentage = 1.0 - (inner.as_secs_f32()) - seconds;
let len_total = text
.sections
.iter()
.fold(0, |acc, curr| acc + curr.value.len());
let target = (len_total as f32 * percentage) as usize;
for mut section in text.sections.iter_mut() {}
}
Some(mut inner) => {
// We are continuing the animation, so decrement the remaining duration
inner = Duration::from_secs_f32(
inner.as_secs_f32() - time.delta().as_secs_f32(),
);
todo!("Do the work of actually animating the text here");
// We are continuing the animation, so decrement the remaining duration
Some(inner.saturating_sub(time.delta()))
}
}
}

Loading…
Cancel
Save