**Ready for review. Examples migration progress: 100%.** # Objective - Implement https://github.com/bevyengine/bevy/discussions/15014 ## Solution This implements [cart's proposal](https://github.com/bevyengine/bevy/discussions/15014#discussioncomment-10574459) faithfully except for one change. I separated `TextSpan` from `TextSpan2d` because `TextSpan` needs to require the `GhostNode` component, which is a `bevy_ui` component only usable by UI. Extra changes: - Added `EntityCommands::commands_mut` that returns a mutable reference. This is a blocker for extension methods that return something other than `self`. Note that `sickle_ui`'s `UiBuilder::commands` returns a mutable reference for this reason. ## Testing - [x] Text examples all work. --- ## Showcase TODO: showcase-worthy ## Migration Guide TODO: very breaking ### Accessing text spans by index Text sections are now text sections on different entities in a hierarchy, Use the new `TextReader` and `TextWriter` system parameters to access spans by index. Before: ```rust fn refresh_text(mut query: Query<&mut Text, With<TimeText>>, time: Res<Time>) { let text = query.single_mut(); text.sections[1].value = format_time(time.elapsed()); } ``` After: ```rust fn refresh_text( query: Query<Entity, With<TimeText>>, mut writer: UiTextWriter, time: Res<Time> ) { let entity = query.single(); *writer.text(entity, 1) = format_time(time.elapsed()); } ``` ### Iterating text spans Text spans are now entities in a hierarchy, so the new `UiTextReader` and `UiTextWriter` system parameters provide ways to iterate that hierarchy. The `UiTextReader::iter` method will give you a normal iterator over spans, and `UiTextWriter::for_each` lets you visit each of the spans. --------- Co-authored-by: ickshonpe <david.curthoys@googlemail.com> Co-authored-by: Carter Anderson <mcanders1@gmail.com>
		
			
				
	
	
		
			117 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			117 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
//! Demonstrate how to use animation events.
 | 
						|
 | 
						|
use bevy::{
 | 
						|
    color::palettes::css::{ALICE_BLUE, BLACK, CRIMSON},
 | 
						|
    core_pipeline::bloom::Bloom,
 | 
						|
    prelude::*,
 | 
						|
};
 | 
						|
 | 
						|
fn main() {
 | 
						|
    App::new()
 | 
						|
        .add_plugins(DefaultPlugins)
 | 
						|
        .add_event::<MessageEvent>()
 | 
						|
        .add_systems(Startup, setup)
 | 
						|
        .add_systems(PreUpdate, (animate_text_opacity, edit_message))
 | 
						|
        .run();
 | 
						|
}
 | 
						|
 | 
						|
#[derive(Component)]
 | 
						|
struct MessageText;
 | 
						|
 | 
						|
#[derive(Event, Reflect, Clone)]
 | 
						|
#[reflect(AnimationEvent)]
 | 
						|
struct MessageEvent {
 | 
						|
    value: String,
 | 
						|
    color: Color,
 | 
						|
}
 | 
						|
 | 
						|
// AnimationEvent can also be derived, but doing so will
 | 
						|
// trigger it as an observer event which is triggered in PostUpdate.
 | 
						|
// We need to set the message text before that so it is
 | 
						|
// updated before rendering without a one frame delay.
 | 
						|
impl AnimationEvent for MessageEvent {
 | 
						|
    fn trigger(&self, _time: f32, _weight: f32, _entity: Entity, world: &mut World) {
 | 
						|
        world.send_event(self.clone());
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
fn edit_message(
 | 
						|
    mut event_reader: EventReader<MessageEvent>,
 | 
						|
    text: Single<(&mut Text2d, &mut TextStyle), With<MessageText>>,
 | 
						|
) {
 | 
						|
    let (mut text, mut style) = text.into_inner();
 | 
						|
    for event in event_reader.read() {
 | 
						|
        text.0 = event.value.clone();
 | 
						|
        style.color = event.color;
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
fn setup(
 | 
						|
    mut commands: Commands,
 | 
						|
    mut animations: ResMut<Assets<AnimationClip>>,
 | 
						|
    mut graphs: ResMut<Assets<AnimationGraph>>,
 | 
						|
) {
 | 
						|
    // Camera
 | 
						|
    commands.spawn((
 | 
						|
        Camera2d,
 | 
						|
        Camera {
 | 
						|
            clear_color: ClearColorConfig::Custom(BLACK.into()),
 | 
						|
            hdr: true,
 | 
						|
            ..Default::default()
 | 
						|
        },
 | 
						|
        Bloom {
 | 
						|
            intensity: 0.4,
 | 
						|
            ..Bloom::NATURAL
 | 
						|
        },
 | 
						|
    ));
 | 
						|
 | 
						|
    // The text that will be changed by animation events.
 | 
						|
    commands.spawn((
 | 
						|
        MessageText,
 | 
						|
        Text2d::default(),
 | 
						|
        TextStyle {
 | 
						|
            font_size: 119.0,
 | 
						|
            color: Color::NONE,
 | 
						|
            ..default()
 | 
						|
        },
 | 
						|
    ));
 | 
						|
 | 
						|
    // Create a new animation clip.
 | 
						|
    let mut animation = AnimationClip::default();
 | 
						|
 | 
						|
    // This is only necessary if you want the duration of the
 | 
						|
    // animation to be longer than the last event in the clip.
 | 
						|
    animation.set_duration(2.0);
 | 
						|
 | 
						|
    // Add events at the specified time.
 | 
						|
    animation.add_event(
 | 
						|
        0.0,
 | 
						|
        MessageEvent {
 | 
						|
            value: "HELLO".into(),
 | 
						|
            color: ALICE_BLUE.into(),
 | 
						|
        },
 | 
						|
    );
 | 
						|
    animation.add_event(
 | 
						|
        1.0,
 | 
						|
        MessageEvent {
 | 
						|
            value: "BYE".into(),
 | 
						|
            color: CRIMSON.into(),
 | 
						|
        },
 | 
						|
    );
 | 
						|
 | 
						|
    // Create the animation graph.
 | 
						|
    let (graph, animation_index) = AnimationGraph::from_clip(animations.add(animation));
 | 
						|
    let mut player = AnimationPlayer::default();
 | 
						|
    player.play(animation_index).repeat();
 | 
						|
 | 
						|
    commands.spawn((AnimationGraphHandle(graphs.add(graph)), player));
 | 
						|
}
 | 
						|
 | 
						|
// Slowly fade out the text opacity.
 | 
						|
fn animate_text_opacity(mut styles: Query<&mut TextStyle>, time: Res<Time>) {
 | 
						|
    for mut style in &mut styles {
 | 
						|
        let a = style.color.alpha();
 | 
						|
        style.color.set_alpha(a - time.delta_seconds());
 | 
						|
    }
 | 
						|
}
 |