bevy/crates/bevy_render/src/spatial_bundle.rs
ickk a0448eca2f enum Visibility component (#6320)
Consolidation of all the feedback about #6271 as well as the addition of an "unconditionally visible" mode.

# Objective

The current implementation of the `Visibility` struct simply wraps a boolean.. which seems like an odd pattern when rust has such nice enums that allow for more expression using pattern-matching. 

Additionally as it stands Bevy only has two settings for visibility of an entity: 
- "unconditionally hidden" `Visibility { is_visible: false }`, 
- "inherit visibility from parent" `Visibility { is_visible: true }`
   where a root level entity set to "inherit" is visible. 

Note that given the behaviour, the current naming of the inner field is a little deceptive or unclear.

Using an enum for `Visibility` opens the door for adding an extra behaviour mode. This PR adds a new "unconditionally visible" mode, which causes an entity to be visible even if its Parent entity is hidden. There should not really be any performance cost to the addition of this new mode.

--
The recently added `toggle` method is removed in this PR, as its semantics could be confusing with 3 variants.

## Solution

Change the Visibility component into
```rust
enum Visibility {
  Hidden,    // unconditionally hidden
  Visible,   // unconditionally visible
  Inherited, // inherit visibility from parent
}
```

---

## Changelog

### Changed

`Visibility` is now an enum

## Migration Guide

- evaluation of the `visibility.is_visible` field should now check for `visibility == Visibility::Inherited`.
- setting the `visibility.is_visible` field should now directly set the value: `*visibility = Visibility::Inherited`.
- usage of `Visibility::VISIBLE` or `Visibility::INVISIBLE` should now use `Visibility::Inherited` or `Visibility::Hidden` respectively.
- `ComputedVisibility::INVISIBLE` and `SpatialBundle::VISIBLE_IDENTITY` have been renamed to `ComputedVisibility::HIDDEN` and `SpatialBundle::INHERITED_IDENTITY` respectively.






Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2022-12-25 00:39:29 +00:00

60 lines
2.3 KiB
Rust

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,
..Self::INHERITED_IDENTITY
}
}
/// A visible [`SpatialBundle`], with no translation, rotation, and a scale of 1 on all axes.
pub const INHERITED_IDENTITY: Self = SpatialBundle {
visibility: Visibility::Inherited,
computed: ComputedVisibility::HIDDEN,
transform: Transform::IDENTITY,
global_transform: GlobalTransform::IDENTITY,
};
/// An invisible [`SpatialBundle`], with no translation, rotation, and a scale of 1 on all axes.
pub const HIDDEN_IDENTITY: Self = SpatialBundle {
visibility: Visibility::Hidden,
..Self::INHERITED_IDENTITY
};
}
impl From<Transform> for SpatialBundle {
#[inline]
fn from(transform: Transform) -> Self {
Self::from_transform(transform)
}
}