Skip to content
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

simplify transform usage where possible #494

Merged
merged 1 commit into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion crates/bevy_transform/src/components/transform.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy_math::{Mat3, Mat4, Quat, Vec3};
use bevy_math::{Mat3, Mat4, Quat, Vec3, Vec4};
use bevy_property::Properties;
use std::fmt;

Expand Down Expand Up @@ -60,6 +60,10 @@ impl Transform {
Vec3::from(self.value.w_axis().truncate())
}

pub fn translation_mut(&mut self) -> &mut Vec4 {
self.value.w_axis_mut()
}

pub fn rotation(&self) -> Quat {
let scale = self.scale();

Expand Down
4 changes: 1 addition & 3 deletions crates/bevy_ui/src/flex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ pub fn flex_node_system(
for (entity, mut node, mut transform, parent) in &mut node_transform_query.iter() {
let layout = flex_surface.get_layout(entity).unwrap();
node.size = Vec2::new(layout.size.width, layout.size.height);
let mut position = transform.translation();
let position = transform.translation_mut();
position.set_x(layout.location.x + layout.size.width / 2.0);
position.set_y(layout.location.y + layout.size.height / 2.0);
if let Some(parent) = parent {
Expand All @@ -202,7 +202,5 @@ pub fn flex_node_system(
*position.y_mut() -= parent_layout.size.height / 2.0;
}
}

transform.set_translation(position);
}
}
6 changes: 2 additions & 4 deletions crates/bevy_ui/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,15 @@ fn update_node_entity(
parent_result: Option<f32>,
previous_result: Option<f32>,
) -> Option<f32> {
let mut transform = node_query.get_mut::<Transform>(entity).ok()?;
let mut z = UI_Z_STEP;
let parent_global_z = parent_result.unwrap();
if let Some(previous_global_z) = previous_result {
z += previous_global_z - parent_global_z;
};
let global_z = z + parent_global_z;

let mut position = transform.translation();
position.set_z(z);
transform.set_translation(position);
let mut transform = node_query.get_mut::<Transform>(entity).ok()?;
transform.translation_mut().set_z(z);

Some(global_z)
}
6 changes: 2 additions & 4 deletions crates/bevy_ui/src/widget/text.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{CalculatedSize, Node};
use bevy_asset::{Assets, Handle};
use bevy_ecs::{Changed, Query, Res, ResMut};
use bevy_math::{Size, Vec3};
use bevy_math::Size;
use bevy_render::{
draw::{Draw, DrawContext, Drawable},
prelude::Msaa,
Expand Down Expand Up @@ -61,9 +61,7 @@ pub fn draw_text_system(
mut query: Query<(&mut Draw, &Text, &Node, &GlobalTransform)>,
) {
for (mut draw, text, node, global_transform) in &mut query.iter() {
let position = Vec3::from(global_transform.value().w_axis().truncate())
- (node.size / 2.0).extend(0.0);

let position = global_transform.translation() - (node.size / 2.0).extend(0.0);
let mut drawable_text = DrawableText {
font: fonts.get(&text.font).unwrap(),
font_atlas_set: font_atlas_sets
Expand Down
12 changes: 4 additions & 8 deletions examples/game/breakout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,11 @@ fn paddle_movement_system(
direction += 1.0;
}

transform.translate(Vec3::unit_x() * time.delta_seconds * direction * paddle.speed);

let translation = transform.translation_mut();
// move the paddle horizontally
*translation.x_mut() += time.delta_seconds * direction * paddle.speed;
// bound the paddle within the walls
let translation = transform.translation();
transform.set_translation(Vec3::new(
f32::max(-380.0, f32::min(380.0, translation.x())),
translation.y(),
translation.z(),
));
*translation.x_mut() = translation.x().min(380.0).max(-380.0);
}
}

Expand Down