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