swoop_ui/
background.rs

1use bevy_asset::prelude::*;
2use bevy_color::prelude::*;
3use bevy_ecs::prelude::*;
4use bevy_image::prelude::*;
5use bevy_ui::prelude::*;
6
7use crate::View;
8
9/// Provides background configuration for a UI container
10pub trait BackgroundView: View {
11    /// Returns a mutable reference to the current background style
12    fn background_node(&mut self) -> &mut BackgroundStyle;
13
14    /// Sets a solid color as the background
15    fn background_color(mut self, color: impl Into<Color>) -> Self {
16        self.background_node().color = BackgroundColor(color.into());
17        self
18    }
19
20    /// Sets an image as the background
21    fn background_image(mut self, image: Handle<Image>) -> Self {
22        self.background_node().image = ImageNode {
23            image,
24            ..Default::default()
25        };
26        self
27    }
28}
29
30/// Defines how a container should be visually styled in the background
31#[derive(Bundle, Debug, Clone, Default)]
32pub struct BackgroundStyle {
33    /// A solid background color
34    color: BackgroundColor,
35    /// A textured background image
36    image: ImageNode,
37}
38
39impl BackgroundStyle {
40    pub fn button() -> Self {
41        Self {
42            color: BackgroundColor(Srgba::WHITE.into()),
43            image: ImageNode::default(),
44        }
45    }
46}