 f86ee32576
			
		
	
	
		f86ee32576
		
			
		
	
	
	
	
		
			
			# Objective - Fixes #14826 - For context, see #15238 ## Solution Add a `GhostNode` component to `bevy_ui` and update all the relevant systems to use it to traverse for UI children. - [x] `ghost_hierarchy` module - [x] Add `GhostNode` - [x] Add `UiRootNodes` system param for iterating (ghost-aware) UI root nodes - [x] Add `UiChildren` system param for iterating (ghost-aware) UI children - [x] Update `layout::ui_layout_system` - [x] Use ghost-aware root nodes for camera updates - [x] Update and remove children in taffy - [x] Initial spawn - [x] Detect changes on nested UI children - [x] Use ghost-aware children traversal in `update_uinode_geometry_recursive` - [x] Update the rest of the UI systems to use the ghost hierarchy - [x] `stack::ui_stack_system` - [x] `update::` - [x] `update_clipping_system` - [x] `update_target_camera_system` - [x] `accessibility::calc_name` ## Testing - [x] Added a new example `ghost_nodes` that can be used as a testbed. - [x] Added unit tests for _some_ of the traversal utilities in `ghost_hierarchy` - [x] Ensure this fulfills the needs for currently known use cases - [x] Reactivity libraries (test with `bevy_reactor`) - [ ] Text spans (mentioned by koe [on discord](https://discord.com/channels/691052431525675048/1285371432460881991/1285377442998915246)) --- ## Performance [See comment below](https://github.com/bevyengine/bevy/pull/15341#issuecomment-2385456820) ## Migration guide Any code that previously relied on `Parent`/`Children` to iterate UI children may now want to use `bevy_ui::UiChildren` to ensure ghost nodes are skipped, and their first descendant Nodes included. UI root nodes may now be children of ghost nodes, which means `Without<Parent>` might not query all root nodes. Use `bevy_ui::UiRootNodes` where needed to iterate root nodes instead. ## Potential future work - Benchmarking/optimizations of hierarchies containing lots of ghost nodes - Further exploration of UI hierarchies and markers for root nodes/leaf nodes to create better ergonomics for things like `UiLayer` (world-space ui) --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com> Co-authored-by: UkoeHB <37489173+UkoeHB@users.noreply.github.com>
		
			
				
	
	
		
			123 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			123 lines
		
	
	
		
			4.1 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.
 | |
| 
 | |
| use bevy::{prelude::*, ui::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(Camera2dBundle::default());
 | |
| 
 | |
|     // Ghost UI root
 | |
|     commands.spawn(GhostNode).with_children(|ghost_root| {
 | |
|         ghost_root
 | |
|             .spawn(NodeBundle::default())
 | |
|             .with_child(create_label(
 | |
|                 "This text node is rendered under a ghost root",
 | |
|                 font_handle.clone(),
 | |
|             ));
 | |
|     });
 | |
| 
 | |
|     // Normal UI root
 | |
|     commands
 | |
|         .spawn(NodeBundle {
 | |
|             style: Style {
 | |
|                 width: Val::Percent(100.0),
 | |
|                 height: Val::Percent(100.0),
 | |
|                 align_items: AlignItems::Center,
 | |
|                 justify_content: JustifyContent::Center,
 | |
|                 ..default()
 | |
|             },
 | |
|             ..default()
 | |
|         })
 | |
|         .with_children(|parent| {
 | |
|             parent
 | |
|                 .spawn((NodeBundle::default(), Counter(0)))
 | |
|                 .with_children(|layout_parent| {
 | |
|                     layout_parent
 | |
|                         .spawn((GhostNode, 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() -> ButtonBundle {
 | |
|     ButtonBundle {
 | |
|         style: Style {
 | |
|             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()
 | |
|         },
 | |
|         border_color: BorderColor(Color::BLACK),
 | |
|         border_radius: BorderRadius::MAX,
 | |
|         background_color: Color::srgb(0.15, 0.15, 0.15).into(),
 | |
|         ..default()
 | |
|     }
 | |
| }
 | |
| 
 | |
| fn create_label(text: &str, font: Handle<Font>) -> TextBundle {
 | |
|     TextBundle::from_section(
 | |
|         text,
 | |
|         TextStyle {
 | |
|             font,
 | |
|             font_size: 33.0,
 | |
|             color: 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.sections[0].value = counter.0.to_string();
 | |
|     }
 | |
| }
 |