1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use bevy::{
    ecs::system::{EntityCommands, SystemParam},
    prelude::*,
};

use crate::{GuiCommands, OuterStyle};

const NORMAL_BUTTON: Color = Color::rgb(0.15, 0.15, 0.15);
const HOVERED_BUTTON: Color = Color::rgb(0.25, 0.25, 0.25);

pub(crate) struct ButtonPlugin;

impl Plugin for ButtonPlugin {
    fn build(&self, app: &mut App) {
        app.add_systems(Update, color_system);
    }
}

pub trait ButtonCommands<'w, 's> {
    fn spawn_button(&mut self, size: OuterStyle, caption: impl Into<String>) -> EntityCommands<'_>;
}

impl<'w, 's> ButtonCommands<'w, 's> for GuiCommands<'w, 's> {
    fn spawn_button(
        &mut self,
        style: OuterStyle,
        caption: impl Into<String>,
    ) -> EntityCommands<'_> {
        let text_style = self.text_props().button_text_style();

        let mut commands = self.spawn(ButtonBundle {
            style: Style {
                justify_content: JustifyContent::Center,
                align_items: AlignItems::Center,
                width: style.width,
                height: style.height,
                margin: style.margin,
                ..default()
            },
            ..default()
        });

        commands.with_children(|builder| {
            builder.spawn(TextBundle::from_section(caption, text_style));
        });

        commands
    }
}

#[derive(SystemParam)]
pub struct ButtonOps<'w, 's> {
    button_query: Query<'w, 's, &'static Children, With<Button>>,
    text_query: Query<'w, 's, &'static mut Text>,
}

impl<'w, 's> ButtonOps<'w, 's> {
    /// This method changes text (e.g. caption) of a UI button.
    ///
    /// The entity must have [`Button`] component and at least one child with
    /// [`Text`] component. The text must consist of a single section. If such
    /// a child is found, its text is changed.
    pub fn set_text(&mut self, entity: Entity, text: String) -> Result<(), &'static str> {
        let Ok(children) = self.button_query.get(entity) else {
            return Err("Button does not exist.");
        };
        for &child in children.iter() {
            if let Ok(mut text_component) = self.text_query.get_mut(child) {
                if text_component.sections.len() == 1 {
                    text_component.sections[0].value = text;
                    return Ok(());
                }
            }
        }

        Err("Button does not have a child with a single-section text..")
    }
}

type ButtonInteractions<'w, 'q> = Query<
    'w,
    'q,
    (&'static Interaction, &'static mut BackgroundColor),
    (Changed<Interaction>, With<Button>),
>;

fn color_system(mut interactions: ButtonInteractions) {
    for (&interaction, mut color) in interactions.iter_mut() {
        match interaction {
            Interaction::Pressed => (),
            Interaction::Hovered => {
                *color = HOVERED_BUTTON.into();
            }
            Interaction::None => {
                *color = NORMAL_BUTTON.into();
            }
        }
    }
}