From 826532a06631424ab55192d6600ce9b5230f60d0 Mon Sep 17 00:00:00 2001 From: Jerome Humbert Date: Mon, 29 Apr 2024 20:40:57 +0100 Subject: [PATCH 01/66] v2 --- Cargo.toml | 1 + examples/menu.rs | 84 ++-- examples/sequence.rs | 23 +- examples/sprite_color.rs | 2 +- examples/text_color.rs | 2 +- examples/transform_rotation.rs | 7 +- examples/transform_translation.rs | 7 +- examples/ui_position.rs | 4 +- src/lib.rs | 624 +++++++++++++++++++----------- src/plugin.rs | 144 ++----- src/tweenable.rs | 152 ++++---- 11 files changed, 577 insertions(+), 473 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4493cef..1a26e59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ bevy_text = ["bevy/bevy_text", "bevy/bevy_render", "bevy/bevy_sprite"] [dependencies] interpolation = "0.3" +slotmap = "1.0" bevy = { version = "0.14", default-features = false } [dev-dependencies] diff --git a/examples/menu.rs b/examples/menu.rs index 25d6660..d33b72a 100644 --- a/examples/menu.rs +++ b/examples/menu.rs @@ -88,50 +88,49 @@ fn setup(mut commands: Commands, asset_server: Res) { ) .with_completed_event(INIT_TRANSITION_DONE); - let animator = if start_time_ms > 0 { + let mut ec = container.spawn(( + ButtonBundle { + style: Style { + min_width: Val::Px(300.), + min_height: Val::Px(80.), + margin: UiRect::all(Val::Px(8.)), + padding: UiRect::all(Val::Px(8.)), + align_content: AlignContent::Center, + align_items: AlignItems::Center, + align_self: AlignSelf::Center, + justify_content: JustifyContent::Center, + ..default() + }, + background_color: BackgroundColor(NORMAL_COLOR), + transform: Transform::from_scale(Vec3::splat(0.01)), + ..default() + }, + Name::new(format!("button:{}", text)), + label, + )); + + let mut ec = if start_time_ms > 0 { let delay = Delay::new(Duration::from_millis(start_time_ms)); - Animator::new(delay.then(tween_scale)) + ec.tween(delay.then(tween_scale)) } else { - Animator::new(tween_scale) + ec.tween(tween_scale) }; - start_time_ms += 500; - container - .spawn(( - ButtonBundle { - style: Style { - min_width: Val::Px(300.), - min_height: Val::Px(80.), - margin: UiRect::all(Val::Px(8.)), - padding: UiRect::all(Val::Px(8.)), - align_content: AlignContent::Center, - align_items: AlignItems::Center, - align_self: AlignSelf::Center, - justify_content: JustifyContent::Center, - ..default() + + ec.with_children(|parent| { + parent.spawn(TextBundle { + text: Text::from_section( + text.to_string(), + TextStyle { + font: font.clone(), + font_size: 48.0, + color: TEXT_COLOR, }, - background_color: BackgroundColor(NORMAL_COLOR), - transform: Transform::from_scale(Vec3::splat(0.01)), - ..default() - }, - Name::new(format!("button:{}", text)), - animator, - label, - )) - .with_children(|parent| { - parent.spawn(TextBundle { - text: Text::from_section( - text.to_string(), - TextStyle { - font: font.clone(), - font_size: 48.0, - color: TEXT_COLOR, - }, - ) - .with_justify(JustifyText::Center), - ..default() - }); + ) + .with_justify(JustifyText::Center), + ..default() }); + }); } }); } @@ -159,9 +158,10 @@ enum ButtonLabel { } fn interaction( + mut commands: Commands, mut interaction_query: Query< ( - &mut Animator, + Entity, &Transform, &Interaction, &mut BackgroundColor, @@ -170,7 +170,7 @@ fn interaction( (Changed, With), >, ) { - for (mut animator, transform, interaction, mut color, button_label) in &mut interaction_query { + for (entity, transform, interaction, mut color, button_label) in &mut interaction_query { match *interaction { Interaction::Pressed => { *color = CLICK_COLOR.into(); @@ -192,7 +192,7 @@ fn interaction( } Interaction::Hovered => { *color = HOVER_COLOR.into(); - animator.set_tweenable(Tween::new( + commands.entity(entity).tween(Tween::new( EaseFunction::QuadraticIn, Duration::from_millis(200), TransformScaleLens { @@ -205,7 +205,7 @@ fn interaction( *color = NORMAL_COLOR.into(); let start_scale = transform.scale; - animator.set_tweenable(Tween::new( + commands.entity(entity).tween(Tween::new( EaseFunction::QuadraticIn, Duration::from_millis(200), TransformScaleLens { diff --git a/examples/sequence.rs b/examples/sequence.rs index 5753ccf..79a88e9 100644 --- a/examples/sequence.rs +++ b/examples/sequence.rs @@ -146,11 +146,9 @@ fn setup(mut commands: Commands, asset_server: Res) { custom_size: Some(Vec2::new(size, size)), ..default() }, - ..default() - }, - RedSprite, - Animator::new(seq), - )); + RedSprite, + )) + .tween(seq); // First move from left to right, then rotate around self 180 degrees while // scaling size at the same time. @@ -193,22 +191,21 @@ fn setup(mut commands: Commands, asset_server: Res) { custom_size: Some(Vec2::new(size * 3., size)), ..default() }, - ..Default::default() - }, - BlueSprite, - Animator::new(seq2), - )); + BlueSprite, + )) + .tween(seq2); } fn update_text( + animator: Res, mut query_text_red: Query<&mut Text, (With, Without)>, mut query_text_blue: Query<&mut Text, (With, Without)>, - query_anim_red: Query<&Animator, With>, - query_anim_blue: Query<&Animator, With>, + query_anim_red: Query>, + query_anim_blue: Query>, mut query_event: EventReader, ) { let anim_red = query_anim_red.single(); - let progress_red = anim_red.tweenable().progress(); + let progress_red = animator.get(anim_red).tweenable().progress(); let anim_blue = query_anim_blue.single(); let progress_blue = anim_blue.tweenable().progress(); diff --git a/examples/sprite_color.rs b/examples/sprite_color.rs index 6c74782..647ecdc 100644 --- a/examples/sprite_color.rs +++ b/examples/sprite_color.rs @@ -84,7 +84,7 @@ fn setup(mut commands: Commands) { }, ..default() }, - Animator::new(tween), + TweenAnimator::new(tween), )); y -= size * spacing; diff --git a/examples/text_color.rs b/examples/text_color.rs index 10c11a2..be194e5 100644 --- a/examples/text_color.rs +++ b/examples/text_color.rs @@ -108,7 +108,7 @@ fn setup(mut commands: Commands, asset_server: Res) { ), ..default() }, - Animator::new(tween), + TweenAnimator::new(tween), )); y += delta_y; diff --git a/examples/transform_rotation.rs b/examples/transform_rotation.rs index 54757bc..da0f13b 100644 --- a/examples/transform_rotation.rs +++ b/examples/transform_rotation.rs @@ -106,7 +106,7 @@ fn setup(mut commands: Commands) { }, ..default() }, - Animator::new(tween), + TweenAnimator::new(tween), )); }); @@ -118,7 +118,10 @@ fn setup(mut commands: Commands) { } } -fn update_animation_speed(options: Res, mut animators: Query<&mut Animator>) { +fn update_animation_speed( + options: Res, + mut animators: Query<&mut TweenAnimator>, +) { if !options.is_changed() { return; } diff --git a/examples/transform_translation.rs b/examples/transform_translation.rs index 7cce2a2..b6ca207 100644 --- a/examples/transform_translation.rs +++ b/examples/transform_translation.rs @@ -99,14 +99,17 @@ fn setup(mut commands: Commands) { }, ..default() }, - Animator::new(tween), + TweenAnimator::new(tween), )); x += size * spacing; } } -fn update_animation_speed(options: Res, mut animators: Query<&mut Animator>) { +fn update_animation_speed( + options: Res, + mut animators: Query<&mut TweenAnimator>, +) { if !options.is_changed() { return; } diff --git a/examples/ui_position.rs b/examples/ui_position.rs index c8b5699..70de678 100644 --- a/examples/ui_position.rs +++ b/examples/ui_position.rs @@ -119,14 +119,14 @@ fn setup(mut commands: Commands) { background_color: BackgroundColor(RED.into()), ..default() }, - Animator::new(tween), + TweenAnimator::new(tween), )); x += offset_x; } } -fn update_animation_speed(options: Res, mut animators: Query<&mut Animator