mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 12:43:34 +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(); } } ```
128 lines
4.3 KiB
Rust
128 lines
4.3 KiB
Rust
//! This example demonstrates the use of Ghost Nodes.
|
|
//!
|
|
//! UI layout will ignore ghost nodes, and treat their children as if they were direct descendants of the first non-ghost ancestor.
|
|
//!
|
|
//! # Warning
|
|
//!
|
|
//! This is an experimental feature, and should be used with caution,
|
|
//! especially in concert with 3rd party plugins or systems that may not be aware of ghost nodes.
|
|
//!
|
|
//! To add [`GhostNode`] components to entities, you must enable the `ghost_nodes` feature flag,
|
|
//! as they are otherwise unconstructable even though the type is defined.
|
|
|
|
use bevy::{prelude::*, ui::experimental::GhostNode, winit::WinitSettings};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins)
|
|
.insert_resource(WinitSettings::desktop_app())
|
|
.add_systems(Startup, setup)
|
|
.add_systems(Update, button_system)
|
|
.run();
|
|
}
|
|
|
|
#[derive(Component)]
|
|
struct Counter(i32);
|
|
|
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|
let font_handle = asset_server.load("fonts/FiraSans-Bold.ttf");
|
|
|
|
commands.spawn(Camera2d);
|
|
|
|
// Ghost UI root
|
|
commands
|
|
.spawn(GhostNode::new())
|
|
.with_children(|ghost_root| {
|
|
ghost_root.spawn(Node::default()).with_child(create_label(
|
|
"This text node is rendered under a ghost root",
|
|
font_handle.clone(),
|
|
));
|
|
});
|
|
|
|
// Normal UI root
|
|
commands
|
|
.spawn(Node {
|
|
width: Val::Percent(100.0),
|
|
height: Val::Percent(100.0),
|
|
align_items: AlignItems::Center,
|
|
justify_content: JustifyContent::Center,
|
|
..default()
|
|
})
|
|
.with_children(|parent| {
|
|
parent
|
|
.spawn((Node::default(), Counter(0)))
|
|
.with_children(|layout_parent| {
|
|
layout_parent
|
|
.spawn((GhostNode::new(), Counter(0)))
|
|
.with_children(|ghost_parent| {
|
|
// Ghost children using a separate counter state
|
|
// These buttons are being treated as children of layout_parent in the context of UI
|
|
ghost_parent
|
|
.spawn(create_button())
|
|
.with_child(create_label("0", font_handle.clone()));
|
|
ghost_parent
|
|
.spawn(create_button())
|
|
.with_child(create_label("0", font_handle.clone()));
|
|
});
|
|
|
|
// A normal child using the layout parent counter
|
|
layout_parent
|
|
.spawn(create_button())
|
|
.with_child(create_label("0", font_handle.clone()));
|
|
});
|
|
});
|
|
}
|
|
|
|
fn create_button() -> impl Bundle {
|
|
(
|
|
Button,
|
|
Node {
|
|
width: Val::Px(150.0),
|
|
height: Val::Px(65.0),
|
|
border: UiRect::all(Val::Px(5.0)),
|
|
// horizontally center child text
|
|
justify_content: JustifyContent::Center,
|
|
// vertically center child text
|
|
align_items: AlignItems::Center,
|
|
..default()
|
|
},
|
|
BorderColor(Color::BLACK),
|
|
BorderRadius::MAX,
|
|
BackgroundColor(Color::srgb(0.15, 0.15, 0.15)),
|
|
)
|
|
}
|
|
|
|
fn create_label(text: &str, font: Handle<Font>) -> (Text, TextFont, TextColor) {
|
|
(
|
|
Text::new(text),
|
|
TextFont {
|
|
font,
|
|
font_size: 33.0,
|
|
..default()
|
|
},
|
|
TextColor(Color::srgb(0.9, 0.9, 0.9)),
|
|
)
|
|
}
|
|
|
|
fn button_system(
|
|
mut interaction_query: Query<(&Interaction, &Parent), (Changed<Interaction>, With<Button>)>,
|
|
labels_query: Query<(&Children, &Parent), With<Button>>,
|
|
mut text_query: Query<&mut Text>,
|
|
mut counter_query: Query<&mut Counter>,
|
|
) {
|
|
// Update parent counter on click
|
|
for (interaction, parent) in &mut interaction_query {
|
|
if matches!(interaction, Interaction::Pressed) {
|
|
let mut counter = counter_query.get_mut(parent.get()).unwrap();
|
|
counter.0 += 1;
|
|
}
|
|
}
|
|
|
|
// Update button labels to match their parent counter
|
|
for (children, parent) in &labels_query {
|
|
let counter = counter_query.get(parent.get()).unwrap();
|
|
let mut text = text_query.get_mut(children[0]).unwrap();
|
|
|
|
**text = counter.0.to_string();
|
|
}
|
|
}
|