-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Replace Transform
in the UI with NodeTransform
#8240
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
Conversation
…n for the vertical text.
…not just changed.
NodePosition
Transform
and GlobalTransform
in the UI with NodePosition
This partially fixes #7876. Strongly in favor of this direction. |
Haven't been able to give this a thorough review just yet, but I'm also in favor of this change, if not only to avoid transform propagation from wasting time on UI hierarchies when the layoutting algorithm already does that natively. |
This is a regression, as we lose the ability to rotate and scale independently of the layout. Having a I would prefer to add support for (even limited to rotation and scale) transform functions to our UI before removing the actual |
ode_transform example.
You added a new example but didn't add metadata for it. Please update the root Cargo.toml file. |
Transform
and GlobalTransform
in the UI with NodeTransform
Transform
in the UI with NodeTransform
on rotations, this does not behave like I expected it to: use bevy::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, rotate)
.run();
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default());
commands
.spawn(NodeBundle { ..default() })
.with_children(|parent| {
parent.spawn((TextBundle::from_section(
"Text Example 1",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 30.0,
color: Color::WHITE,
},
),));
parent.spawn((
TextBundle::from_section(
"Text Example 2",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 30.0,
color: Color::WHITE,
},
),
NodeRotation::default(),
));
});
}
fn rotate(mut rots: Query<&mut NodeRotation>, time: Res<Time>) {
for mut rot in &mut rots {
rot.0 += time.delta_seconds();
}
} text-rotation.mp4 |
examples/ui/node_transform.rs
Outdated
background_color: Color::WHITE.into(), | ||
..default() | ||
}) | ||
.insert(RotateButton(-std::f32::consts::PI / 8.)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.insert(RotateButton(-std::f32::consts::PI / 8.)) | |
.insert(RotateButton(-std::f32::consts::FRAC_PI_8)) |
You should use the appropriate constants instead of dividing PI
see https://doc.rust-lang.org/std/f32/consts/index.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please also do the other PI fractions
Co-authored-by: François <[email protected]>
This is because it's rotating the Ui Node, not only the text. If you add background colors it becomes obvious what is happening: By default items stretch to fill the cross-axis, you can get the text to rotate on its center by setting
I thought about adding some sort of anchors, but I think it goes beyond the scope of this PR probably. |
Agreed: I'd like anchors, but not in this PR. IMO rotating the entire node is the correct behavior here. |
Still I was expecting it to behave like this html: text-rotation-html.mp4EDIT: this PR does behave the same as in Bevy 0.10 use bevy::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, rotate)
.run();
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default());
commands
.spawn(NodeBundle { ..default() })
.with_children(|parent| {
parent.spawn((TextBundle::from_section(
"Text Example 1",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 30.0,
color: Color::WHITE,
},
),));
parent.spawn((
TextBundle::from_section(
"Text Example 2",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 30.0,
color: Color::WHITE,
},
),
NodeRotation,
));
});
}
#[derive(Component)]
struct NodeRotation;
fn rotate(mut rots: Query<&mut Transform, With<NodeRotation>>, time: Res<Time>) {
for mut rot in &mut rots {
rot.rotate_local_z(time.delta_seconds());
}
} |
Co-authored-by: François <[email protected]>
Rotating content like this text is really easy to implement at the widget level. We would just need to add a rotation value to |
Closing in favour of #16615 |
Objective
Bevy UI uses
Transform
andGlobalTransform
to manage node positions. There are lots of problems:Transform
breaking the UI layout.Transform
andGlobalTransform
are big, 112 bytes together in total.GlobalTransform
.Transform
.propagate_transforms
has to be run afterflex_layout_system
Solution
Remove the
Transform
andGlobalTransform
components and queries from the UI bundles and systems. Replace them with a a tuple struct component wrapping anAffine2
calledNodeTransform
.Bevy.App.2023-03-30.14-12-09.mp4
Changelog
NodeTransform
which is a tuple struct component wrapping anAffine2
calledNodeTransform
.NodeTransform
to the UI node bundles.Transform and
GlobalTransform` from the UI node bundles.TransformSystem
after and before conditions fromUiPlugin
NodePosition
instead ofTransform
/GlobalTransform
.ui_focus_system
to respond correctly to interactions with rotated and scaled elements.node_transform
that demos interactions with translated, rotated and scaled buttons.NodeTranslation
,NodeRotation
andNodeScale
components.Migration Guide
The
Transform
andGlobalTransform
components have been replaced in Bevy UI's node bundles by the componentNodeTransform
.The components
NodeTranslation
,NodeRotation
andNodeScale
can be added to UI node entities to allow translation, rotation and scaling of a node and its children.