From d1a7a7f73d0916af13fef744d85b0035cd5a3a8b Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Fri, 7 Jul 2023 08:10:59 -0700 Subject: [PATCH] saving my place --- bin/text-inspect.rs | 128 +++++++++++++++++++++++++++++++++++--------- src/text.rs | 43 ++++++++------- 2 files changed, 128 insertions(+), 43 deletions(-) diff --git a/bin/text-inspect.rs b/bin/text-inspect.rs index 1c7f3bf..40bc4f1 100644 --- a/bin/text-inspect.rs +++ b/bin/text-inspect.rs @@ -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>); -#[derive(Component)] -struct Marker; - #[derive(Component)] struct PreviewText; @@ -48,6 +46,9 @@ struct ButtonShelf; #[derive(Component)] struct FontButton(Handle); +#[derive(Component)] +struct AnimationButton; + fn load_fonts(mut commands: Commands, server: Res) { let handles = server .load_folder("fonts") @@ -62,17 +63,14 @@ fn init_ui(mut commands: Commands) { commands.spawn(Camera2dBundle { ..default() }); commands - .spawn(( - NodeBundle { - style: Style { - size: Size::all(Val::Percent(100.0)), - ..default() - }, - background_color: BackgroundColor(Color::BLACK), + .spawn(NodeBundle { + style: Style { + size: Size::all(Val::Percent(100.0)), ..default() }, - Marker, - )) + background_color: BackgroundColor(Color::BLACK), + ..default() + }) .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, server: Res, - query: Query, With)>, + shelf_query: Query>, ) { 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, With)>, + mut texts: Query<&mut Text, With>, interaction: Query<(&Interaction, &FontButton), Changed>, ) { for (i, f) in interaction.iter() { @@ -176,6 +236,24 @@ fn manage_fonts( } } +fn manage_animation_button( + mut animation_button: Query<&mut Text, With>, + interaction: Query<(&Interaction, &FontButton), Changed>, +) { + 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, With