mirror of
https://github.com/bevyengine/bevy
synced 2025-01-11 12:48:56 +00:00
c2c19e5ae4
**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>
89 lines
2.5 KiB
Rust
89 lines
2.5 KiB
Rust
//! This example illustrates how to resize windows, and how to respond to a window being resized.
|
|
use bevy::{prelude::*, window::WindowResized};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.insert_resource(ResolutionSettings {
|
|
large: Vec2::new(1920.0, 1080.0),
|
|
medium: Vec2::new(800.0, 600.0),
|
|
small: Vec2::new(640.0, 360.0),
|
|
})
|
|
.add_plugins(DefaultPlugins)
|
|
.add_systems(Startup, (setup_camera, setup_ui))
|
|
.add_systems(Update, (on_resize_system, toggle_resolution))
|
|
.run();
|
|
}
|
|
|
|
/// Marker component for the text that displays the current resolution.
|
|
#[derive(Component)]
|
|
struct ResolutionText;
|
|
|
|
/// Stores the various window-resolutions we can select between.
|
|
#[derive(Resource)]
|
|
struct ResolutionSettings {
|
|
large: Vec2,
|
|
medium: Vec2,
|
|
small: Vec2,
|
|
}
|
|
|
|
// Spawns the camera that draws UI
|
|
fn setup_camera(mut commands: Commands) {
|
|
commands.spawn(Camera2d);
|
|
}
|
|
|
|
// Spawns the UI
|
|
fn setup_ui(mut commands: Commands) {
|
|
// Node that fills entire background
|
|
commands
|
|
.spawn(NodeBundle {
|
|
style: Style {
|
|
width: Val::Percent(100.),
|
|
..default()
|
|
},
|
|
..default()
|
|
})
|
|
// Text where we display current resolution
|
|
.with_child((
|
|
Text::new("Resolution"),
|
|
TextStyle {
|
|
font_size: 42.0,
|
|
..default()
|
|
},
|
|
ResolutionText,
|
|
));
|
|
}
|
|
|
|
/// This system shows how to request the window to a new resolution
|
|
fn toggle_resolution(
|
|
keys: Res<ButtonInput<KeyCode>>,
|
|
mut windows: Query<&mut Window>,
|
|
resolution: Res<ResolutionSettings>,
|
|
) {
|
|
let mut window = windows.single_mut();
|
|
|
|
if keys.just_pressed(KeyCode::Digit1) {
|
|
let res = resolution.small;
|
|
window.resolution.set(res.x, res.y);
|
|
}
|
|
if keys.just_pressed(KeyCode::Digit2) {
|
|
let res = resolution.medium;
|
|
window.resolution.set(res.x, res.y);
|
|
}
|
|
if keys.just_pressed(KeyCode::Digit3) {
|
|
let res = resolution.large;
|
|
window.resolution.set(res.x, res.y);
|
|
}
|
|
}
|
|
|
|
/// This system shows how to respond to a window being resized.
|
|
/// Whenever the window is resized, the text will update with the new resolution.
|
|
fn on_resize_system(
|
|
mut q: Query<&mut Text, With<ResolutionText>>,
|
|
mut resize_reader: EventReader<WindowResized>,
|
|
) {
|
|
let mut text = q.single_mut();
|
|
for e in resize_reader.read() {
|
|
// When resolution is being changed
|
|
**text = format!("{:.1} x {:.1}", e.width, e.height);
|
|
}
|
|
}
|