data/
element.rs

1//! Defines the [`Element`] enum and it's implementations.
2
3use bevy::prelude::Component;
4use bevy::prelude::Visibility;
5
6/// An [`Element`] is the lowest level of the project hierarchy.
7///
8/// It defines a specific element that can be drawn on the screen, commonly  represented as an asset
9/// with additional metadata.
10/// For portability reasons we don't store the full path of an asset, we simply store an ID which we
11/// can resolve later through the `assets` module.
12///
13/// We don't have to store *all* information about the elements in the enum; things like translation,
14/// scale, rotation, enabled, ... can all be inferred later from the components it's placed alongside.
15///
16/// # Examples
17///
18/// Here's how to spawn a simple `Element` for the Walls.
19///
20/// ```
21/// # use bevy::prelude::*;
22/// # use data::Project;
23/// # use data::Level;
24/// # use data::Layer;
25/// # use data::Element;
26/// #
27/// # fn main() {
28/// #   App::new()
29/// #       .add_systems(Startup, spawn_project)
30/// #       .run();
31/// # }
32/// #
33/// # fn spawn_project(mut commands: Commands) {
34///     commands.spawn((
35///         Project::new("Roadside Inn"),
36///         children![(
37///             Level::new("Ground Floor"),
38///             children![(
39///                 Layer::new("Walls", Transform::IDENTITY),
40///                 children![(
41///                     Element::new_object(String::from("<hash-of-resolvable-asset")),
42///                 )]
43///             )]
44///         )]
45///     ));
46/// # }
47/// ```
48#[derive(Component)]
49#[component(immutable)]
50#[require(Visibility::default())]
51#[non_exhaustive]
52pub enum Element {
53    /// Represents an image, no pathing, patterns, ...
54    Object(String),
55}
56
57impl Element {
58    /// Generates a new [`Element`] for the given asset ID.
59    ///
60    /// **TODO**: validate assets? maybe generate a full bundle?
61    #[must_use]
62    pub fn new_object(asset_id: String) -> Self {
63        Self::Object(asset_id)
64    }
65}