use std::borrow::Cow;
use bevy::{
ecs::{spawn::SpawnWith, system::IntoObserverSystem},
prelude::*,
ui::Val::*,
};
use crate::theme::{interaction::InteractionPalette, palette::*};
pub(crate) fn ui_root(name: impl Into<Cow<'static, str>>) -> impl Bundle {
(
Name::new(name),
Node {
position_type: PositionType::Absolute,
width: Percent(100.0),
height: Percent(100.0),
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
flex_direction: FlexDirection::Column,
row_gap: Px(20.0),
..default()
},
BackgroundColor(SCREEN_BACKGROUND),
Pickable::IGNORE,
)
}
pub(crate) fn header(text: impl Into<String>) -> impl Bundle {
(
Name::new("Header"),
Text(text.into()),
TextFont::from_font_size(40.0),
TextColor(HEADER_TEXT),
)
}
pub(crate) fn label(text: impl Into<String>) -> impl Bundle {
label_base(text, 24.0)
}
pub(crate) fn label_small(text: impl Into<String>) -> impl Bundle {
label_base(text, 12.0)
}
fn label_base(text: impl Into<String>, font_size: f32) -> impl Bundle {
(
Name::new("Label"),
Text(text.into()),
TextFont::from_font_size(font_size),
TextColor(LABEL_TEXT),
)
}
pub(crate) fn button<E, B, M, I>(text: impl Into<String>, action: I) -> impl Bundle
where
E: Event,
B: Bundle,
I: IntoObserverSystem<E, B, M>,
{
button_base(
text,
action,
(
Node {
width: Px(300.0),
height: Px(80.0),
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
..default()
},
BorderRadius::MAX,
),
)
}
pub(crate) fn button_small<E, B, M, I>(text: impl Into<String>, action: I) -> impl Bundle
where
E: Event,
B: Bundle,
I: IntoObserverSystem<E, B, M>,
{
button_base(
text,
action,
Node {
width: Px(30.0),
height: Px(30.0),
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
..default()
},
)
}
fn button_base<E, B, M, I>(
text: impl Into<String>,
action: I,
button_bundle: impl Bundle,
) -> impl Bundle
where
E: Event,
B: Bundle,
I: IntoObserverSystem<E, B, M>,
{
let text = text.into();
let action = IntoObserverSystem::into_system(action);
(
Name::new("Button"),
Node::default(),
Children::spawn(SpawnWith(|parent: &mut ChildSpawner| {
parent
.spawn((
Name::new("Button Inner"),
Button,
BackgroundColor(BUTTON_BACKGROUND),
InteractionPalette {
none: BUTTON_BACKGROUND,
hovered: BUTTON_HOVERED_BACKGROUND,
pressed: BUTTON_PRESSED_BACKGROUND,
},
children![(
Name::new("Button Text"),
Text(text),
TextFont::from_font_size(40.0),
TextColor(BUTTON_TEXT),
Pickable::IGNORE,
)],
))
.insert(button_bundle)
.observe(action);
})),
)
}