 eaac730617
			
		
	
	
		eaac730617
		
	
	
	
	
		
			
			# Objective `Size::width` sets the `height` field to `Val::DEFAULT` which is `Val::Undefined`, but the default for `Size` `height` is `Val::Auto`. `Size::height` has the same problem, but with the `width` field. The UI examples specify numeric values in many places where they could either be elided or replaced by composition of the Flex enum properties. related: https://github.com/bevyengine/bevy/pull/7468 fixes: https://github.com/bevyengine/bevy/issues/6498 ## Solution Change `Size::width` so it sets `height` to `Val::AUTO` and change `Size::height` so it sets `width` to `Val::AUTO`. Added some tests so this doesn't happen again. ## Changelog Changed `Size::width` so it sets the `height` to `Val::AUTO`. Changed `Size::height` so it sets the `width` to `Val::AUTO`. Added tests to `geometry.rs` for `Size` and `UiRect` to ensure correct behaviour. Simplified the UI examples. Replaced numeric values with the Flex property enums or elided them where possible, and removed the remaining use of auto margins. ## Migration Guide The `Size::width` constructor function now sets the `height` to `Val::Auto` instead of `Val::Undefined`. The `Size::height` constructor function now sets the `width` to `Val::Auto` instead of `Val::Undefined`.
		
			
				
	
	
		
			135 lines
		
	
	
		
			5.7 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			135 lines
		
	
	
		
			5.7 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| //! Demonstrates how to use the z-index component on UI nodes to control their relative depth
 | |
| //!
 | |
| //! It uses colored boxes with different z-index values to demonstrate how it can affect the order of
 | |
| //! depth of nodes compared to their siblings, but also compared to the entire UI.
 | |
| 
 | |
| use bevy::prelude::*;
 | |
| 
 | |
| fn main() {
 | |
|     App::new()
 | |
|         .insert_resource(ClearColor(Color::BLACK))
 | |
|         .add_plugins(DefaultPlugins)
 | |
|         .add_startup_system(setup)
 | |
|         .run();
 | |
| }
 | |
| 
 | |
| fn setup(mut commands: Commands) {
 | |
|     commands.spawn(Camera2dBundle::default());
 | |
| 
 | |
|     // spawn the container with default z-index.
 | |
|     // the default z-index value is `ZIndex::Local(0)`.
 | |
|     // because this is a root UI node, using local or global values will do the same thing.
 | |
|     commands
 | |
|         .spawn(NodeBundle {
 | |
|             style: Style {
 | |
|                 size: Size::width(Val::Percent(100.0)),
 | |
|                 align_items: AlignItems::Center,
 | |
|                 justify_content: JustifyContent::Center,
 | |
|                 ..default()
 | |
|             },
 | |
|             ..default()
 | |
|         })
 | |
|         .with_children(|parent| {
 | |
|             parent
 | |
|                 .spawn(NodeBundle {
 | |
|                     background_color: Color::GRAY.into(),
 | |
|                     style: Style {
 | |
|                         size: Size::new(Val::Px(180.0), Val::Px(100.0)),
 | |
|                         ..default()
 | |
|                     },
 | |
|                     ..default()
 | |
|                 })
 | |
|                 .with_children(|parent| {
 | |
|                     // spawn a node with default z-index.
 | |
|                     parent.spawn(NodeBundle {
 | |
|                         background_color: Color::RED.into(),
 | |
|                         style: Style {
 | |
|                             position_type: PositionType::Absolute,
 | |
|                             position: UiRect {
 | |
|                                 left: Val::Px(10.0),
 | |
|                                 bottom: Val::Px(40.0),
 | |
|                                 ..default()
 | |
|                             },
 | |
|                             size: Size::new(Val::Px(100.0), Val::Px(50.0)),
 | |
|                             ..default()
 | |
|                         },
 | |
|                         ..default()
 | |
|                     });
 | |
| 
 | |
|                     // spawn a node with a positive local z-index of 2.
 | |
|                     // it will show above other nodes in the grey container.
 | |
|                     parent.spawn(NodeBundle {
 | |
|                         z_index: ZIndex::Local(2),
 | |
|                         background_color: Color::BLUE.into(),
 | |
|                         style: Style {
 | |
|                             position_type: PositionType::Absolute,
 | |
|                             position: UiRect {
 | |
|                                 left: Val::Px(45.0),
 | |
|                                 bottom: Val::Px(30.0),
 | |
|                                 ..default()
 | |
|                             },
 | |
|                             size: Size::new(Val::Px(100.0), Val::Px(50.0)),
 | |
|                             ..default()
 | |
|                         },
 | |
|                         ..default()
 | |
|                     });
 | |
| 
 | |
|                     // spawn a node with a negative local z-index.
 | |
|                     // it will show under other nodes in the grey container.
 | |
|                     parent.spawn(NodeBundle {
 | |
|                         z_index: ZIndex::Local(-1),
 | |
|                         background_color: Color::GREEN.into(),
 | |
|                         style: Style {
 | |
|                             position_type: PositionType::Absolute,
 | |
|                             position: UiRect {
 | |
|                                 left: Val::Px(70.0),
 | |
|                                 bottom: Val::Px(20.0),
 | |
|                                 ..default()
 | |
|                             },
 | |
|                             size: Size::new(Val::Px(100.0), Val::Px(75.0)),
 | |
|                             ..default()
 | |
|                         },
 | |
|                         ..default()
 | |
|                     });
 | |
| 
 | |
|                     // spawn a node with a positive global z-index of 1.
 | |
|                     // it will show above all other nodes, because it's the highest global z-index in this example.
 | |
|                     // by default, boxes all share the global z-index of 0 that the grey container is added to.
 | |
|                     parent.spawn(NodeBundle {
 | |
|                         z_index: ZIndex::Global(1),
 | |
|                         background_color: Color::PURPLE.into(),
 | |
|                         style: Style {
 | |
|                             position_type: PositionType::Absolute,
 | |
|                             position: UiRect {
 | |
|                                 left: Val::Px(15.0),
 | |
|                                 bottom: Val::Px(10.0),
 | |
|                                 ..default()
 | |
|                             },
 | |
|                             size: Size::new(Val::Px(100.0), Val::Px(60.0)),
 | |
|                             ..default()
 | |
|                         },
 | |
|                         ..default()
 | |
|                     });
 | |
| 
 | |
|                     // spawn a node with a negative global z-index of -1.
 | |
|                     // this will show under all other nodes including its parent, because it's the lowest global z-index
 | |
|                     // in this example.
 | |
|                     parent.spawn(NodeBundle {
 | |
|                         z_index: ZIndex::Global(-1),
 | |
|                         background_color: Color::YELLOW.into(),
 | |
|                         style: Style {
 | |
|                             position_type: PositionType::Absolute,
 | |
|                             position: UiRect {
 | |
|                                 left: Val::Px(-15.0),
 | |
|                                 bottom: Val::Px(-15.0),
 | |
|                                 ..default()
 | |
|                             },
 | |
|                             size: Size::new(Val::Px(100.0), Val::Px(125.0)),
 | |
|                             ..default()
 | |
|                         },
 | |
|                         ..default()
 | |
|                     });
 | |
|                 });
 | |
|         });
 | |
| }
 |