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    /// Sets background image mode
30    fn background_image_mode(mut self, image_mode: NodeImageMode) -> Self {
31        self.background_node().image.image_mode = image_mode;
32        self
33    }
34}
35
36/// Defines how a container should be visually styled in the background
37#[derive(Bundle, Debug, Clone, Default)]
38pub struct BackgroundStyle {
39    /// A solid background color
40    color: BackgroundColor,
41    /// A textured background image
42    image: ImageNode,
43}
44
45impl BackgroundStyle {
46    pub fn button() -> Self {
47        Self {
48            color: BackgroundColor(Srgba::WHITE.into()),
49            image: ImageNode::default(),
50        }
51    }
52}