Skip to content

Instantly share code, notes, and snippets.

@thebluefish
Created February 15, 2024 21:44
Show Gist options
  • Save thebluefish/fd10446da8e353aa6d3589727dc2cf73 to your computer and use it in GitHub Desktop.
Save thebluefish/fd10446da8e353aa6d3589727dc2cf73 to your computer and use it in GitHub Desktop.
use bevy::app::AppExit;
use bevy::prelude::*;
use bevy::window::WindowCloseRequested;
fn main() {
App::new()
.init_state::<ExitState>()
.add_plugins(DefaultPlugins.set(WindowPlugin {
close_when_requested: false,
..default()
}))
.add_systems(Update, exit_trigger.run_if(not(in_state(ExitState::Prompt))))
.add_systems(Startup, setup)
.add_systems(Update, button_system.run_if(in_state(ExitState::Prompt)))
.add_systems(OnEnter(ExitState::Prompt), create_exit_ui.run_if(in_state(ExitState::Prompt)))
.run();
}
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)]
enum ExitState {
#[default]
None,
Prompt,
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default());
}
fn exit_trigger(mut events: EventReader<WindowCloseRequested>, mut state: ResMut<NextState<ExitState>>) {
if !events.is_empty() {
events.clear();
println!("Prompt");
state.set(ExitState::Prompt);
}
}
const NORMAL_BUTTON: Color = Color::rgb(0.15, 0.15, 0.15);
const HOVERED_BUTTON: Color = Color::rgb(0.25, 0.25, 0.25);
const PRESSED_BUTTON: Color = Color::rgb(0.35, 0.75, 0.35);
fn button_system(
mut interaction_query: Query<
(
&Interaction,
&mut BackgroundColor,
&mut BorderColor,
&Children,
),
(Changed<Interaction>, With<Button>),
>,
mut text_query: Query<&mut Text>,
mut events: EventWriter<AppExit>,
) {
for (interaction, mut color, mut border_color, children) in &mut interaction_query {
let mut text = text_query.get_mut(children[0]).unwrap();
match *interaction {
Interaction::Pressed => {
events.send(AppExit);
}
Interaction::Hovered => {
text.sections[0].value = "Hover".to_string();
*color = HOVERED_BUTTON.into();
border_color.0 = Color::WHITE;
}
Interaction::None => {
text.sections[0].value = "Button".to_string();
*color = NORMAL_BUTTON.into();
border_color.0 = Color::BLACK;
}
}
}
}
fn create_exit_ui(mut commands: Commands, asset_server: Res<AssetServer>) {
commands
.spawn(NodeBundle {
style: Style {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
..default()
},
..default()
})
.with_children(|parent| {
parent
.spawn(ButtonBundle {
style: Style {
width: Val::Px(150.0),
height: Val::Px(65.0),
border: UiRect::all(Val::Px(5.0)),
// horizontally center child text
justify_content: JustifyContent::Center,
// vertically center child text
align_items: AlignItems::Center,
..default()
},
border_color: BorderColor(Color::BLACK),
background_color: NORMAL_BUTTON.into(),
..default()
})
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"Exit",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 40.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
));
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment