use bevy::prelude::*;
use bevy_ui_navigation::{
prelude::{DefaultNavigationPlugins, FocusState, Focusable, NavEvent, NavRequestSystem},
systems::InputMapping,
};
fn main() {
App::new()
.add_plugins((DefaultPlugins, DefaultNavigationPlugins))
.add_systems(Startup, setup)
.add_systems(
Update,
(
button_system.after(NavRequestSystem),
print_nav_events.after(NavRequestSystem),
),
)
.run();
}
fn button_system(
mut interaction_query: Query<(&Focusable, &mut BackgroundColor), Changed<Focusable>>,
) {
for (focusable, mut material) in interaction_query.iter_mut() {
if let FocusState::Focused = focusable.state() {
*material = Color::ORANGE_RED.into();
} else {
*material = Color::DARK_GRAY.into();
}
}
}
fn print_nav_events(mut events: EventReader<NavEvent>) {
for event in events.read() {
println!("{:?}", event);
}
}
fn setup(mut commands: Commands, mut input_mapping: ResMut<InputMapping>) {
input_mapping.keyboard_navigation = true;
input_mapping.focus_follows_mouse = true;
commands.spawn(Camera2dBundle::default());
let positions = [
Vec2::new(10.0, 10.0),
Vec2::new(15.0, 50.0),
Vec2::new(20.0, 90.0),
Vec2::new(30.0, 10.0),
Vec2::new(35.0, 50.0),
Vec2::new(40.0, 90.0),
Vec2::new(60.0, 10.0),
Vec2::new(55.0, 50.0),
Vec2::new(50.0, 90.0),
];
commands
.spawn(NodeBundle {
style: Style {
position_type: PositionType::Absolute,
width: Val::Percent(100.0),
height: Val::Percent(100.0),
..Default::default()
},
..Default::default()
})
.with_children(|commands| {
for pos in positions {
spawn_button(pos, commands);
}
});
}
fn spawn_button(position: Vec2, commands: &mut ChildBuilder) {
commands.spawn((
ButtonBundle {
style: Style {
width: Val::Px(95.0),
height: Val::Px(65.0),
left: Val::Percent(position.x),
top: Val::Percent(position.y),
position_type: PositionType::Absolute,
..Default::default()
},
background_color: Color::DARK_GRAY.into(),
..Default::default()
},
Focusable::default(),
));
}