2022-07-18 23:27:30 +00:00
|
|
|
use bevy_ecs::prelude::Bundle;
|
|
|
|
use bevy_transform::prelude::{GlobalTransform, Transform};
|
|
|
|
|
|
|
|
use crate::view::{ComputedVisibility, Visibility};
|
|
|
|
|
|
|
|
/// A [`Bundle`] with the following [`Component`](bevy_ecs::component::Component)s:
|
|
|
|
/// * [`Visibility`] and [`ComputedVisibility`], which describe the visibility of an entity
|
|
|
|
/// * [`Transform`] and [`GlobalTransform`], which describe the position of an entity
|
|
|
|
///
|
|
|
|
/// * To show or hide an entity, you should set its [`Visibility`].
|
|
|
|
/// * To get the computed visibility of an entity, you should get its [`ComputedVisibility`].
|
|
|
|
/// * To place or move an entity, you should set its [`Transform`].
|
|
|
|
/// * To get the global transform of an entity, you should get its [`GlobalTransform`].
|
|
|
|
/// * For hierarchies to work correctly, you must have all four components.
|
|
|
|
/// * You may use the [`SpatialBundle`] to guarantee this.
|
|
|
|
#[derive(Bundle, Debug, Default)]
|
|
|
|
pub struct SpatialBundle {
|
|
|
|
/// The visibility of the entity.
|
|
|
|
pub visibility: Visibility,
|
|
|
|
/// The computed visibility of the entity.
|
|
|
|
pub computed: ComputedVisibility,
|
|
|
|
/// The transform of the entity.
|
|
|
|
pub transform: Transform,
|
|
|
|
/// The global transform of the entity.
|
|
|
|
pub global_transform: GlobalTransform,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SpatialBundle {
|
|
|
|
/// Creates a new [`SpatialBundle`] from a [`Transform`].
|
|
|
|
///
|
|
|
|
/// This initializes [`GlobalTransform`] as identity, and visibility as visible
|
|
|
|
#[inline]
|
|
|
|
pub const fn from_transform(transform: Transform) -> Self {
|
|
|
|
SpatialBundle {
|
|
|
|
transform,
|
2022-12-25 00:39:29 +00:00
|
|
|
..Self::INHERITED_IDENTITY
|
2022-07-18 23:27:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 22:10:24 +00:00
|
|
|
/// A visible [`SpatialBundle`], with no translation, rotation, and a scale of 1 on all axes.
|
2022-12-25 00:39:29 +00:00
|
|
|
pub const INHERITED_IDENTITY: Self = SpatialBundle {
|
|
|
|
visibility: Visibility::Inherited,
|
|
|
|
computed: ComputedVisibility::HIDDEN,
|
2022-08-30 22:10:24 +00:00
|
|
|
transform: Transform::IDENTITY,
|
|
|
|
global_transform: GlobalTransform::IDENTITY,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// An invisible [`SpatialBundle`], with no translation, rotation, and a scale of 1 on all axes.
|
2022-12-25 00:39:29 +00:00
|
|
|
pub const HIDDEN_IDENTITY: Self = SpatialBundle {
|
|
|
|
visibility: Visibility::Hidden,
|
|
|
|
..Self::INHERITED_IDENTITY
|
2022-08-30 22:10:24 +00:00
|
|
|
};
|
2022-07-18 23:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Transform> for SpatialBundle {
|
|
|
|
#[inline]
|
|
|
|
fn from(transform: Transform) -> Self {
|
|
|
|
Self::from_transform(transform)
|
|
|
|
}
|
|
|
|
}
|