swoop_ui/
container.rs

1use bevy_asset::prelude::*;
2use bevy_color::prelude::*;
3use bevy_ecs::prelude::*;
4use bevy_image::prelude::*;
5use bevy_ui::prelude::*;
6
7use crate::UiToBundle;
8
9pub mod grid;
10pub mod stack;
11
12pub mod prelude {
13    pub use super::grid::prelude::*;
14    pub use super::stack::prelude::*;
15    pub use super::{BackgroundContainer, BackgroundStyle, BorderContainer, BorderStyle};
16}
17
18/// Provides background configuration for a UI container
19pub trait BackgroundContainer {
20    /// Returns a mutable reference to the current background style
21    fn background_node(&mut self) -> &mut BackgroundStyle;
22
23    /// Sets a solid color as the background
24    fn background_color(mut self, color: impl Into<Color>) -> Self
25    where
26        Self: Sized,
27    {
28        *self.background_node() = BackgroundStyle::Color(color.into());
29        self
30    }
31
32    /// Sets an image as the background
33    fn background_image(mut self, image: Handle<Image>) -> Self
34    where
35        Self: Sized,
36    {
37        *self.background_node() = BackgroundStyle::Image(image);
38        self
39    }
40}
41
42/// Defines how a container should be visually styled in the background
43pub enum BackgroundStyle {
44    /// A solid background color
45    Color(Color),
46    /// A textured background image
47    Image(Handle<Image>),
48}
49
50impl UiToBundle for BackgroundStyle {
51    fn pack(self) -> impl Bundle {
52        match self {
53            BackgroundStyle::Color(color) => (
54                BackgroundColor(color),
55                ImageNode::solid_color(Srgba::NONE.into()),
56            ),
57            BackgroundStyle::Image(handle) => {
58                (BackgroundColor(Srgba::NONE.into()), ImageNode::new(handle))
59            }
60        }
61    }
62}
63
64impl Default for BackgroundStyle {
65    fn default() -> Self {
66        BackgroundStyle::Color(Srgba::NONE.into())
67    }
68}
69
70/// Provides border configuration for a UI container
71pub trait BorderContainer {
72    /// Returns a mutable reference to the current border style
73    fn border_node(&mut self) -> &mut BorderStyle;
74
75    /// Sets the border color
76    fn border_color(mut self, border_color: impl Into<Color>) -> Self
77    where
78        Self: Sized,
79    {
80        self.border_node().border_color.0 = border_color.into();
81        self
82    }
83
84    /// Sets the border radius
85    fn border_radius(mut self, border_radius: BorderRadius) -> Self
86    where
87        Self: Sized,
88    {
89        self.border_node().border_radius = border_radius;
90        self
91    }
92}
93
94/// Describes the style for rendering borders around a UI container
95pub struct BorderStyle {
96    /// The corner radius for the border
97    border_radius: BorderRadius,
98    /// The color of the border
99    border_color: BorderColor,
100}
101
102impl UiToBundle for BorderStyle {
103    fn pack(self) -> impl Bundle {
104        (self.border_radius, self.border_color)
105    }
106}
107
108impl Default for BorderStyle {
109    fn default() -> Self {
110        Self {
111            border_radius: BorderRadius::ZERO,
112            border_color: BorderColor(Srgba::NONE.into()),
113        }
114    }
115}