**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>
		
			
				
	
	
		
			87 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			87 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
//! Shows how to render simple primitive shapes with a single color.
 | 
						|
//!
 | 
						|
//! You can toggle wireframes with the space bar except on wasm. Wasm does not support
 | 
						|
//! `POLYGON_MODE_LINE` on the gpu.
 | 
						|
 | 
						|
use bevy::prelude::*;
 | 
						|
#[cfg(not(target_arch = "wasm32"))]
 | 
						|
use bevy::sprite::{Wireframe2dConfig, Wireframe2dPlugin};
 | 
						|
 | 
						|
fn main() {
 | 
						|
    let mut app = App::new();
 | 
						|
    app.add_plugins((
 | 
						|
        DefaultPlugins,
 | 
						|
        #[cfg(not(target_arch = "wasm32"))]
 | 
						|
        Wireframe2dPlugin,
 | 
						|
    ))
 | 
						|
    .add_systems(Startup, setup);
 | 
						|
    #[cfg(not(target_arch = "wasm32"))]
 | 
						|
    app.add_systems(Update, toggle_wireframe);
 | 
						|
    app.run();
 | 
						|
}
 | 
						|
 | 
						|
const X_EXTENT: f32 = 900.;
 | 
						|
 | 
						|
fn setup(
 | 
						|
    mut commands: Commands,
 | 
						|
    mut meshes: ResMut<Assets<Mesh>>,
 | 
						|
    mut materials: ResMut<Assets<ColorMaterial>>,
 | 
						|
) {
 | 
						|
    commands.spawn(Camera2d);
 | 
						|
 | 
						|
    let shapes = [
 | 
						|
        meshes.add(Circle::new(50.0)),
 | 
						|
        meshes.add(CircularSector::new(50.0, 1.0)),
 | 
						|
        meshes.add(CircularSegment::new(50.0, 1.25)),
 | 
						|
        meshes.add(Ellipse::new(25.0, 50.0)),
 | 
						|
        meshes.add(Annulus::new(25.0, 50.0)),
 | 
						|
        meshes.add(Capsule2d::new(25.0, 50.0)),
 | 
						|
        meshes.add(Rhombus::new(75.0, 100.0)),
 | 
						|
        meshes.add(Rectangle::new(50.0, 100.0)),
 | 
						|
        meshes.add(RegularPolygon::new(50.0, 6)),
 | 
						|
        meshes.add(Triangle2d::new(
 | 
						|
            Vec2::Y * 50.0,
 | 
						|
            Vec2::new(-50.0, -50.0),
 | 
						|
            Vec2::new(50.0, -50.0),
 | 
						|
        )),
 | 
						|
    ];
 | 
						|
    let num_shapes = shapes.len();
 | 
						|
 | 
						|
    for (i, shape) in shapes.into_iter().enumerate() {
 | 
						|
        // Distribute colors evenly across the rainbow.
 | 
						|
        let color = Color::hsl(360. * i as f32 / num_shapes as f32, 0.95, 0.7);
 | 
						|
 | 
						|
        commands.spawn((
 | 
						|
            Mesh2d(shape),
 | 
						|
            MeshMaterial2d(materials.add(color)),
 | 
						|
            Transform::from_xyz(
 | 
						|
                // Distribute shapes from -X_EXTENT/2 to +X_EXTENT/2.
 | 
						|
                -X_EXTENT / 2. + i as f32 / (num_shapes - 1) as f32 * X_EXTENT,
 | 
						|
                0.0,
 | 
						|
                0.0,
 | 
						|
            ),
 | 
						|
        ));
 | 
						|
    }
 | 
						|
 | 
						|
    #[cfg(not(target_arch = "wasm32"))]
 | 
						|
    commands.spawn((
 | 
						|
        Text::new("Press space to toggle wireframes"),
 | 
						|
        Style {
 | 
						|
            position_type: PositionType::Absolute,
 | 
						|
            top: Val::Px(12.0),
 | 
						|
            left: Val::Px(12.0),
 | 
						|
            ..default()
 | 
						|
        },
 | 
						|
    ));
 | 
						|
}
 | 
						|
 | 
						|
#[cfg(not(target_arch = "wasm32"))]
 | 
						|
fn toggle_wireframe(
 | 
						|
    mut wireframe_config: ResMut<Wireframe2dConfig>,
 | 
						|
    keyboard: Res<ButtonInput<KeyCode>>,
 | 
						|
) {
 | 
						|
    if keyboard.just_pressed(KeyCode::Space) {
 | 
						|
        wireframe_config.global = !wireframe_config.global;
 | 
						|
    }
 | 
						|
}
 |