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
use bevy::{
    ecs::system::{EntityCommands, SystemParam},
    prelude::*,
};

use crate::{GuiCommands, OuterStyle};

/// marker component for UI `BasicText`
#[derive(Component)]
struct BodyText;

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

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

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

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

        commands
    }
}

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

impl<'w, 's> BodyTextOps<'w, 's> {
    /// This method changes text (e.g. caption) of UI body text.
    pub fn set_text(&mut self, entity: Entity, text: impl Into<String>) -> Result<(), &str> {
        let text = text.into();
        let children = match self.body_text_query.get(entity) {
            Ok(children) => children,
            Err(e) => {
                trace!("BodyText does not exist. {:?}", e);
                return Err("BodyText does not exist.");
            }
        };
        for &child in children.iter() {
            if let Ok(mut text_component) = self.text_query.get_mut(child) {
                if text_component.sections[0].value == text {
                    // avoid unnecessary update
                    return Ok(());
                }
                text_component.sections[0].value = text;
                return Ok(());
            }
        }
        trace!("BodyText does not have a child with Text component.");
        Err("BodyText does not have a child with Text component.")
    }
}