mirror of
https://github.com/bevyengine/bevy
synced 2024-11-25 06:00:20 +00:00
015f2c69ca
# Objective Continue improving the user experience of our UI Node API in the direction specified by [Bevy's Next Generation Scene / UI System](https://github.com/bevyengine/bevy/discussions/14437) ## Solution As specified in the document above, merge `Style` fields into `Node`, and move "computed Node fields" into `ComputedNode` (I chose this name over something like `ComputedNodeLayout` because it currently contains more than just layout info. If we want to break this up / rename these concepts, lets do that in a separate PR). `Style` has been removed. This accomplishes a number of goals: ## Ergonomics wins Specifying both `Node` and `Style` is now no longer required for non-default styles Before: ```rust commands.spawn(( Node::default(), Style { width: Val::Px(100.), ..default() }, )); ``` After: ```rust commands.spawn(Node { width: Val::Px(100.), ..default() }); ``` ## Conceptual clarity `Style` was never a comprehensive "style sheet". It only defined "core" style properties that all `Nodes` shared. Any "styled property" that couldn't fit that mold had to be in a separate component. A "real" style system would style properties _across_ components (`Node`, `Button`, etc). We have plans to build a true style system (see the doc linked above). By moving the `Style` fields to `Node`, we fully embrace `Node` as the driving concept and remove the "style system" confusion. ## Next Steps * Consider identifying and splitting out "style properties that aren't core to Node". This should not happen for Bevy 0.15. --- ## Migration Guide Move any fields set on `Style` into `Node` and replace all `Style` component usage with `Node`. Before: ```rust commands.spawn(( Node::default(), Style { width: Val::Px(100.), ..default() }, )); ``` After: ```rust commands.spawn(Node { width: Val::Px(100.), ..default() }); ``` For any usage of the "computed node properties" that used to live on `Node`, use `ComputedNode` instead: Before: ```rust fn system(nodes: Query<&Node>) { for node in &nodes { let computed_size = node.size(); } } ``` After: ```rust fn system(computed_nodes: Query<&ComputedNode>) { for computed_node in &computed_nodes { let computed_size = computed_node.size(); } } ```
83 lines
2.4 KiB
Rust
83 lines
2.4 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(Node {
|
|
width: Val::Percent(100.),
|
|
..default()
|
|
})
|
|
// Text where we display current resolution
|
|
.with_child((
|
|
Text::new("Resolution"),
|
|
TextFont {
|
|
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 window: Single<&mut Window>,
|
|
resolution: Res<ResolutionSettings>,
|
|
) {
|
|
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 text: Single<&mut Text, With<ResolutionText>>,
|
|
mut resize_reader: EventReader<WindowResized>,
|
|
) {
|
|
for e in resize_reader.read() {
|
|
// When resolution is being changed
|
|
text.0 = format!("{:.1} x {:.1}", e.width, e.height);
|
|
}
|
|
}
|