use bevy::prelude::*;
#[derive(Clone, BufferedEvent, 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 const BUTTON_BORDER: UiRect = UiRect::all(Val::Px(1.0));
pub const BUTTON_BORDER_COLOR: BorderColor = BorderColor {
left: Color::WHITE,
right: Color::WHITE,
top: Color::WHITE,
bottom: Color::WHITE,
};
pub const BUTTON_BORDER_RADIUS_SIZE: Val = Val::Px(6.0);
pub const BUTTON_PADDING: UiRect = UiRect::axes(Val::Px(12.0), Val::Px(6.0));
pub fn main_ui_node() -> Node {
Node {
flex_direction: FlexDirection::Column,
position_type: PositionType::Absolute,
row_gap: px(6),
left: px(10),
bottom: px(10),
..default()
}
}
pub fn option_button<T>(
option_value: T,
option_name: &str,
is_selected: bool,
is_first: bool,
is_last: bool,
) -> impl Bundle
where
T: Clone + Send + Sync + 'static,
{
let (bg_color, fg_color) = if is_selected {
(Color::WHITE, Color::BLACK)
} else {
(Color::BLACK, Color::WHITE)
};
(
Button,
Node {
border: BUTTON_BORDER.with_left(if is_first { px(1) } else { px(0) }),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
padding: BUTTON_PADDING,
..default()
},
BUTTON_BORDER_COLOR,
BorderRadius::ZERO
.with_left(if is_first {
BUTTON_BORDER_RADIUS_SIZE
} else {
px(0)
})
.with_right(if is_last {
BUTTON_BORDER_RADIUS_SIZE
} else {
px(0)
}),
BackgroundColor(bg_color),
RadioButton,
WidgetClickSender(option_value.clone()),
children![(
ui_text(option_name, fg_color),
RadioButtonText,
WidgetClickSender(option_value),
)],
)
}
pub fn option_buttons<T>(title: &str, options: &[(T, &str)]) -> impl Bundle
where
T: Clone + Send + Sync + 'static,
{
let buttons = options
.iter()
.cloned()
.enumerate()
.map(|(option_index, (option_value, option_name))| {
option_button(
option_value,
option_name,
option_index == 0,
option_index == 0,
option_index == options.len() - 1,
)
})
.collect::<Vec<_>>();
(
Node {
align_items: AlignItems::Center,
..default()
},
Children::spawn((
Spawn((
ui_text(title, Color::BLACK),
Node {
width: px(125),
..default()
},
)),
SpawnIter(buttons.into_iter()),
)),
)
}
pub fn ui_text(label: &str, color: Color) -> impl Bundle + use<> {
(
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.write(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;
});
}