use bevy::{ app::AppExit, input::{keyboard::KeyboardInput, ButtonState}, window::PrimaryWindow, }; use crate::prelude::*; pub(crate) struct MenuPlugin; impl Plugin for MenuPlugin { fn build(&self, app: &mut App) { app.add_systems(Startup, init_menu_ui) .add_systems( Update, ( handle_menu_button, handle_menu_quit, bevy::window::close_on_esc, interactive_button, ) .run_if(in_state(GameState::Menu)), ) .add_systems(OnEnter(GameState::Menu), activate::) .add_systems(OnExit(GameState::Menu), deactivate::); } } #[derive(Debug, Component)] struct Menu; #[derive(Debug, Component)] struct Quit; fn init_menu_ui(mut commands: Commands) { commands .spawn(( Menu, NodeBundle { style: Style { width: Val::Percent(100.0), height: Val::Percent(100.0), justify_content: JustifyContent::Center, align_items: AlignItems::Center, flex_direction: FlexDirection::Column, position_type: PositionType::Absolute, ..default() }, background_color: Color::NONE.into(), visibility: Visibility::Hidden, ..default() }, )) .with_children(|parent| { parent .spawn(( GameState::Play, ButtonBundle { style: Style { padding: UiRect::all(Val::Px(5.0)), margin: UiRect::all(Val::Px(5.0)), ..default() }, background_color: Color::ORANGE.with_a(0.5).into(), ..default() }, )) .with_children(|parent| { parent.spawn(( GameState::Play, TextBundle::from_section( "Start", TextStyle { color: Color::BLACK, font_size: 32.0, ..default() }, ), )); }); parent .spawn(( GameState::Credits, ButtonBundle { style: Style { padding: UiRect::all(Val::Px(5.0)), margin: UiRect::all(Val::Px(5.0)), ..default() }, background_color: Color::ORANGE.with_a(0.5).into(), ..default() }, )) .with_children(|parent| { parent.spawn(( GameState::Credits, TextBundle::from_section( "Credits", TextStyle { color: Color::BLACK, font_size: 32.0, ..default() }, ), )); }); parent .spawn(( Quit, ButtonBundle { style: Style { padding: UiRect::all(Val::Px(5.0)), margin: UiRect::all(Val::Px(5.0)), ..default() }, background_color: Color::ORANGE.with_a(0.5).into(), ..default() }, )) .with_children(|parent| { parent.spawn(( Quit, TextBundle::from_section( "Quit", TextStyle { color: Color::BLACK, font_size: 32.0, ..default() }, ), )); }); }); } fn handle_menu_button( events: Query<(&Interaction, &GameState), (With