-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UI bottom margin isn't applied #7120
Comments
I think the issue is actually that there shouldn't be a margin on the right. If you try this code in html: <div style="background: orange; width: 100%; height: 100%; margin: 5%"></div> you will have no margin on bottom and right. The layout should reserve the margin on left/top, then take 100% of width/height for the orange part, then take 5% for the margins again, offscreen. |
I'm not entirely sure what you mean with that. The html example only has one value for the margin. But bevy has 4 seperate values for each side, so I would expect each of them to funciton seperately from each other. I expanded my sample code a bit. I now use an ImageBundle to display a simple sprite and I added bevy-inspector-egui (0.16.2) so that I tinker with the parameters live: 2023-01-07_19-35-33.mp4When adjusting the left and right margins, they behaved as I expected. But as you can see in the video, the top margin seems to offset the node, rather than "squishing" it like left and right margin do ( In case anyone wants to run this example for themselves: bevy-test.zip |
That's the same with CSS, when you use only one value it's interpreted as using that value for each margin. Bevy UI is mostly following CSS specs, if you ask for something to be 110% (100% + each 5% margin) it will be 110% and go over. Most of the computations for layout is done by taffy, @alice-i-cecile is it something you've heard? |
I don't recognize this: can you file a bug upstream and link this issue? |
I'm not familiar with taffy but I'll see if I can recreate this issue there |
I suspect the reason the margin isn't overflowing for the horizontal axis is because it's a flexbox row, and |
As an update: Due to private reasons I haven't found time to work on this. I still want to recreate this with taffy but I can't give any promises as to when that would happen. |
nicoburns is right, it's not intuitive but this is the correct behaviour. commands.spawn(NodeBundle {
background_color: BackgroundColor(Color::ORANGE),
style: Style {
size: Size::new(Val::Percent(100.0), Val::Auto),
margin: UiRect::All(Val::Percent(100.0)),
..default()
},
..default()
}); Why 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());
commands.spawn(NodeBundle {
background_color: BackgroundColor(Color::WHITE),
style: Style {
size: Size::new(Val::Px(100.), Val::Px(100.)),
margin: UiRect::all(Val::Px(40.)),
..default()
},
..default()
}).with_children(|parent| {
parent.spawn(NodeBundle {
background_color: BackgroundColor(Color::RED),
style: Style {
flex_direction: FlexDirection::Column,
size: Size::new(Val::Percent(100.), Val::Percent(100.)),
margin: UiRect::all(Val::Px(10.)),
..default()
},
..default()
});
});
commands.spawn(NodeBundle {
background_color: BackgroundColor(Color::WHITE),
style: Style {
flex_direction: FlexDirection::Column,
size: Size::new(Val::Px(100.), Val::Px(100.)),
margin: UiRect::all(Val::Px(40.)),
..default()
},
..default()
}).with_children(|parent| {
parent.spawn(NodeBundle {
background_color: BackgroundColor(Color::RED),
style: Style {
size: Size::new(Val::Percent(100.), Val::Percent(100.)),
margin: UiRect::all(Val::Px(10.)),
..default()
},
..default()
});
});
commands.spawn(NodeBundle {
background_color: BackgroundColor(Color::WHITE),
style: Style {
size: Size::new(Val::Px(100.), Val::Px(100.)),
margin: UiRect::all(Val::Px(40.)),
..default()
},
..default()
}).with_children(|parent| {
parent.spawn(NodeBundle {
background_color: BackgroundColor(Color::RED),
style: Style {
size: Size::new(Val::Percent(100.), Val::Auto),
margin: UiRect::all(Val::Px(10.)),
..default()
},
..default()
});
});
} With With the height (cross-axis) set to With the height set to |
Thought about this some more, and there is a bug. |
Is there a difference between Taffy >= 0.2 has removed the |
Seems to be: use bevy::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.run();
}
fn setup(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
commands.spawn(NodeBundle {
style: Style {
size: Size::new(Val::Px(100.0), Val::Auto),
..Default::default()
},
background_color: BackgroundColor(Color::RED),
..Default::default()
});
commands.spawn(NodeBundle {
style: Style {
size: Size::new(Val::Px(100.0), Val::Undefined),
..Default::default()
},
background_color: BackgroundColor(Color::BLUE),
..Default::default()
});
} Draws only a red bar. |
It's still there? and in https://github.com/bevyengine/bevy/blob/main/crates/bevy_ui/src/flex/convert.rs pub fn from_val(scale_factor: f64, val: Val) -> taffy::style::Dimension {
match val {
Val::Auto => taffy::style::Dimension::Auto,
Val::Percent(value) => taffy::style::Dimension::Percent(value / 100.0),
Val::Px(value) => taffy::style::Dimension::Points((scale_factor * value as f64) as f32),
Val::Undefined => taffy::style::Dimension::Undefined,
}
} |
Ah, must be 0.3 that removes it. In any case, I think that versions of Taffy that have |
Hmm... perhaps there are some subtle differences. |
# Objective In CSS Flexbox width and height are auto by default, whereas in Bevy their default is `Size::Undefined`. This means that, unlike in CSS, if you elide a height or width value for a node it will be given zero length (unless it has an explicitly sized child node). This has misled users into falsely assuming that they have to explicitly set a value for both height and width all the time. relevant issue: #7120 ## Solution Change the `Size` `width` and `height` default values to `Val::Auto` ## Changelog * Changed the `Size` `width` and `height` default values to `Val::Auto` ## Migration Guide The default values for `Size` `width` and `height` have been changed from `Val::Undefined` to `Val::Auto`. It's unlikely to cause any issues with existing code.
# Objective In CSS Flexbox width and height are auto by default, whereas in Bevy their default is `Size::Undefined`. This means that, unlike in CSS, if you elide a height or width value for a node it will be given zero length (unless it has an explicitly sized child node). This has misled users into falsely assuming that they have to explicitly set a value for both height and width all the time. relevant issue: #7120 ## Solution Change the `Size` `width` and `height` default values to `Val::Auto` ## Changelog * Changed the `Size` `width` and `height` default values to `Val::Auto` ## Migration Guide The default values for `Size` `width` and `height` have been changed from `Val::Undefined` to `Val::Auto`. It's unlikely to cause any issues with existing code.
Bevy version
Tested with both the 0.9.1 release from crates.io and the current main branch (076e6f7)
[Optional] Relevant system information
What you did
I attempted to have a UI root node that spans the entire screen, but with a slight margin on each side.
Considering the following
main.rs
:my
Cargo.toml
for referenceWhat went wrong
Setting the margin in the style properties works, but not for the bottom margin.
I tried using different configurations with pixel values too, but it seemed that the bottom margin wasn't applied at all.
Additional information
Relevant discussion on the discord server: https://discord.com/channels/691052431525675048/1060632681873932329
The text was updated successfully, but these errors were encountered: