data/
layer.rs

1//! Defines the [`Layer`] struct and it's implementations.
2use bevy::prelude::{Bundle, Component, Name, Transform, Visibility};
3use std::borrow::Cow;
4
5/// A [`Layer`] represents a distinct editing plane within a [`crate::Level`], allowing elements to be
6/// grouped by function or intent (e.g., "Walls", "Objects", "Lighting").
7///
8/// Layers are most commonly used for selective visibility, per-layer export, and to control edit
9/// interactions (such as locking or isolating layers during drawing or object placement).
10#[derive(Component, Default)]
11#[component(immutable)]
12#[require(Visibility::default())]
13pub struct Layer;
14
15impl Layer {
16    /// Generates a new [`Bundle`] with a layer under which graphic items can be grouped.
17    /// The `transform` determines how the layer is positioned relative to other layers.
18    ///
19    /// # Examples
20    ///
21    /// Here's how to spawn a simple `Layer` for the Walls.
22    ///
23    /// ```
24    /// # use bevy::prelude::*;
25    /// # use data::Project;
26    /// # use data::Level;
27    /// # use data::Layer;
28    /// #
29    /// # fn main() {
30    /// #   App::new()
31    /// #       .add_systems(Startup, spawn_project)
32    /// #       .run();
33    /// # }
34    /// #
35    /// # fn spawn_project(mut commands: Commands) {
36    ///     commands.spawn((
37    ///         Project::new("Roadside Inn"),
38    ///         children![
39    ///             Level::new("Ground Floor"),
40    ///             children![
41    ///                 Layer::new("Walls", Transform::IDENTITY),
42    ///             ]
43    ///         ]
44    ///     ));
45    /// # }
46    /// ```
47    #[allow(clippy::new_ret_no_self)]
48    #[must_use = "Layer won't be added to the world unless spawned"]
49    pub fn new(name: impl Into<Cow<'static, str>>, transform: Transform) -> impl Bundle {
50        (Name::new(name), Layer {}, transform)
51    }
52}