Skip to content

Commit

Permalink
GlobalTransform included
Browse files Browse the repository at this point in the history
Signed-off-by: Torstein Grindvik <[email protected]>
  • Loading branch information
Torstein Grindvik committed May 30, 2024
1 parent c13bb4d commit 8c279c5
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 30 deletions.
3 changes: 1 addition & 2 deletions crates/bevy_transform/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//! Extension to [`EntityCommands`] to modify `bevy_hierarchy` hierarchies
//! while preserving [`GlobalTransform`].
use crate::prelude::{GlobalTransform, Transform};
use bevy_ecs::{prelude::Entity, system::EntityCommands, world::Command, world::World};
use bevy_hierarchy::{PushChild, RemoveParent};

use crate::{GlobalTransform, Transform};

/// Command similar to [`PushChild`], but updating the child transform to keep
/// it at the same [`GlobalTransform`].
///
Expand Down
10 changes: 8 additions & 2 deletions crates/bevy_transform/src/components/global_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ use std::ops::Mul;

use super::Transform;

#[cfg(feature = "bevy-support")]
use bevy_ecs::{component::Component, reflect::ReflectComponent};
use bevy_math::{Affine3A, Dir3, Mat4, Quat, Vec3, Vec3A};
#[cfg(feature = "bevy-support")]
use bevy_reflect::{std_traits::ReflectDefault, Reflect};

/// Describe the position of an entity relative to the reference frame.
Expand Down Expand Up @@ -34,9 +36,13 @@ use bevy_reflect::{std_traits::ReflectDefault, Reflect};
/// - [`transform`]
///
/// [`transform`]: https://github.com/bevyengine/bevy/blob/latest/examples/transforms/transform.rs
#[derive(Component, Debug, PartialEq, Clone, Copy, Reflect)]
#[derive(Debug, PartialEq, Clone, Copy)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[reflect(Component, Default, PartialEq)]
#[cfg_attr(
feature = "bevy-support",
derive(Component, Reflect),
reflect(Component, Default, PartialEq)
)]
pub struct GlobalTransform(Affine3A);

macro_rules! impl_local_axis {
Expand Down
2 changes: 0 additions & 2 deletions crates/bevy_transform/src/components/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#[cfg(feature = "bevy-support")]
mod global_transform;
mod transform;

#[cfg(feature = "bevy-support")]
pub use global_transform::*;
pub use transform::*;
28 changes: 11 additions & 17 deletions crates/bevy_transform/src/components/transform.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::ops::Mul;

#[cfg(feature = "bevy-support")]
use super::GlobalTransform;
#[cfg(feature = "bevy-support")]
use bevy_ecs::{component::Component, prelude::ReflectComponent};
Expand Down Expand Up @@ -551,24 +550,19 @@ impl Mul<Vec3> for Transform {
}
}

#[cfg(feature = "bevy-support")]
mod bevy_support {
use super::*;

/// The transform is expected to be non-degenerate and without shearing, or the output
/// will be invalid.
impl From<GlobalTransform> for Transform {
fn from(transform: GlobalTransform) -> Self {
transform.compute_transform()
}
/// The transform is expected to be non-degenerate and without shearing, or the output
/// will be invalid.
impl From<GlobalTransform> for Transform {
fn from(transform: GlobalTransform) -> Self {
transform.compute_transform()
}
}

impl Mul<GlobalTransform> for Transform {
type Output = GlobalTransform;
impl Mul<GlobalTransform> for Transform {
type Output = GlobalTransform;

#[inline]
fn mul(self, global_transform: GlobalTransform) -> Self::Output {
GlobalTransform::from(self) * global_transform
}
#[inline]
fn mul(self, global_transform: GlobalTransform) -> Self::Output {
GlobalTransform::from(self) * global_transform
}
}
9 changes: 3 additions & 6 deletions crates/bevy_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,12 @@ pub mod systems;
#[doc(hidden)]
pub mod prelude {
#[doc(hidden)]
pub use crate::components::Transform;
pub use crate::components::*;

#[cfg(feature = "bevy-support")]
#[doc(hidden)]
pub use crate::{
bundles::TransformBundle, commands::BuildChildrenTransformExt, components::*,
helper::TransformHelper, plugins::TransformPlugin, traits::TransformPoint,
bundles::TransformBundle, commands::BuildChildrenTransformExt, helper::TransformHelper,
plugins::TransformPlugin, traits::TransformPoint,
};
}

#[cfg(feature = "bevy-support")]
use prelude::{GlobalTransform, Transform};
1 change: 0 additions & 1 deletion crates/bevy_transform/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ impl TransformPoint for Transform {
}
}

#[cfg(feature = "bevy-support")]
impl TransformPoint for GlobalTransform {
#[inline]
fn transform_point(&self, point: impl Into<Vec3>) -> Vec3 {
Expand Down

0 comments on commit 8c279c5

Please sign in to comment.