use bevy::{ecs::system::EntityCommands, prelude::*};
#[derive(Clone, Event, Deref, DerefMut)]
pub struct WidgetClickEvent<T>(T);
#[derive(Clone, Component, Deref, DerefMut)]
pub struct WidgetClickSender<T>(T)
where
T: Clone + Send + Sync + 'static;
#[derive(Clone, Copy, Component)]
pub struct RadioButton;
#[derive(Clone, Copy, Component)]
pub struct RadioButtonText;
pub fn main_ui_node() -> Node {
Node {
flex_direction: FlexDirection::Column,
position_type: PositionType::Absolute,
row_gap: Val::Px(6.0),
left: Val::Px(10.0),
bottom: Val::Px(10.0),
..default()
}
}
pub fn spawn_option_button<T>(
parent: &mut ChildBuilder,
option_value: T,
option_name: &str,
is_selected: bool,
is_first: bool,
is_last: bool,
) where
T: Clone + Send + Sync + 'static,
{
let (bg_color, fg_color) = if is_selected {
(Color::WHITE, Color::BLACK)
} else {
(Color::BLACK, Color::WHITE)
};
parent
.spawn((
Button,
Node {
border: UiRect::all(Val::Px(1.0)).with_left(if is_first {
Val::Px(1.0)
} else {
Val::Px(0.0)
}),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
padding: UiRect::axes(Val::Px(12.0), Val::Px(6.0)),
..default()
},
BorderColor(Color::WHITE),
BorderRadius::ZERO
.with_left(if is_first { Val::Px(6.0) } else { Val::Px(0.0) })
.with_right(if is_last { Val::Px(6.0) } else { Val::Px(0.0) }),
BackgroundColor(bg_color),
))
.insert(RadioButton)
.insert(WidgetClickSender(option_value.clone()))
.with_children(|parent| {
spawn_ui_text(parent, option_name, fg_color)
.insert(RadioButtonText)
.insert(WidgetClickSender(option_value));
});
}
pub fn spawn_option_buttons<T>(parent: &mut ChildBuilder, title: &str, options: &[(T, &str)])
where
T: Clone + Send + Sync + 'static,
{
parent
.spawn(Node {
align_items: AlignItems::Center,
..default()
})
.with_children(|parent| {
spawn_ui_text(parent, title, Color::BLACK).insert(Node {
width: Val::Px(125.0),
..default()
});
for (option_index, (option_value, option_name)) in options.iter().cloned().enumerate() {
spawn_option_button(
parent,
option_value,
option_name,
option_index == 0,
option_index == 0,
option_index == options.len() - 1,
);
}
});
}
pub fn spawn_ui_text<'a>(
parent: &'a mut ChildBuilder,
label: &str,
color: Color,
) -> EntityCommands<'a> {
parent.spawn((
Text::new(label),
TextFont {
font_size: 18.0,
..default()
},
TextColor(color),
))
}
pub fn handle_ui_interactions<T>(
mut interactions: Query<
(&Interaction, &WidgetClickSender<T>),
(With<Button>, With<RadioButton>),
>,
mut widget_click_events: EventWriter<WidgetClickEvent<T>>,
) where
T: Clone + Send + Sync + 'static,
{
for (interaction, click_event) in interactions.iter_mut() {
if *interaction == Interaction::Pressed {
widget_click_events.send(WidgetClickEvent((**click_event).clone()));
}
}
}
pub fn update_ui_radio_button(background_color: &mut BackgroundColor, selected: bool) {
background_color.0 = if selected { Color::WHITE } else { Color::BLACK };
}
pub fn update_ui_radio_button_text(entity: Entity, writer: &mut TextUiWriter, selected: bool) {
let text_color = if selected { Color::BLACK } else { Color::WHITE };
writer.for_each_color(entity, |mut color| {
color.0 = text_color;
});
}