generated from NiklasEi/bevy_game_template
-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #179 from janhohenheim/menu-egui
Former-commit-id: 921c16d
- Loading branch information
Showing
4 changed files
with
100 additions
and
138 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,60 @@ | ||
use crate::file_system_interaction::asset_loading::FontAssets; | ||
use crate::util::log_error::log_errors; | ||
use crate::GameState; | ||
use anyhow::Ok; | ||
use anyhow::Result; | ||
use bevy::prelude::*; | ||
use bevy_egui::egui::FontFamily::Proportional; | ||
use bevy_egui::egui::FontId; | ||
use bevy_egui::egui::TextStyle::{Button, Heading}; | ||
use bevy_egui::{egui, EguiContext}; | ||
|
||
/// This plugin is responsible for the game menu | ||
/// The menu is only drawn during the State `GameState::Menu` and is removed when that state is exited. | ||
/// Because the Bevy UI situation is not quite mature yet, this work would ideally be done in egui instead, | ||
/// so don't try to replicate this. | ||
/// See [issue #13](https://github.com/janhohenheim/foxtrot/issues/13) | ||
pub struct MenuPlugin; | ||
|
||
impl Plugin for MenuPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.init_resource::<ButtonColors>() | ||
.add_system_set(SystemSet::on_enter(GameState::Menu).with_system(setup_menu)) | ||
.add_system_set( | ||
SystemSet::on_update(GameState::Menu) | ||
.with_system(click_play_button.pipe(log_errors)), | ||
); | ||
} | ||
} | ||
|
||
#[derive(Resource)] | ||
struct ButtonColors { | ||
normal: Color, | ||
hovered: Color, | ||
} | ||
|
||
impl Default for ButtonColors { | ||
fn default() -> Self { | ||
ButtonColors { | ||
normal: Color::rgb(0.15, 0.15, 0.15), | ||
hovered: Color::rgb(0.25, 0.25, 0.25), | ||
} | ||
app.add_system_set( | ||
SystemSet::on_update(GameState::Menu).with_system(setup_menu.pipe(log_errors)), | ||
); | ||
} | ||
} | ||
|
||
fn setup_menu( | ||
mut commands: Commands, | ||
font_assets: Res<FontAssets>, | ||
button_colors: Res<ButtonColors>, | ||
) { | ||
commands | ||
.spawn(ButtonBundle { | ||
style: Style { | ||
size: Size::new(Val::Px(120.0), Val::Px(50.0)), | ||
margin: UiRect::all(Val::Auto), | ||
justify_content: JustifyContent::Center, | ||
align_items: AlignItems::Center, | ||
..default() | ||
}, | ||
background_color: button_colors.normal.into(), | ||
..default() | ||
}) | ||
.insert(Name::new("Play Button")) | ||
.with_children(|parent| { | ||
parent | ||
.spawn(TextBundle { | ||
text: Text { | ||
sections: vec![TextSection { | ||
value: "Play".to_string(), | ||
style: TextStyle { | ||
font: font_assets.fira_sans.clone(), | ||
font_size: 40.0, | ||
color: Color::rgb(0.9, 0.9, 0.9), | ||
}, | ||
}], | ||
alignment: default(), | ||
}, | ||
..default() | ||
}) | ||
.insert(Name::new("Play Text")); | ||
}); | ||
} | ||
|
||
#[allow(clippy::type_complexity)] | ||
fn click_play_button( | ||
mut commands: Commands, | ||
button_colors: Res<ButtonColors>, | ||
mut egui_context: ResMut<EguiContext>, | ||
mut state: ResMut<State<GameState>>, | ||
mut interaction_query: Query< | ||
(Entity, &Interaction, &mut BackgroundColor), | ||
(Changed<Interaction>, With<Button>), | ||
>, | ||
) -> Result<()> { | ||
for (button, interaction, mut color) in interaction_query.iter_mut() { | ||
match *interaction { | ||
Interaction::Clicked => { | ||
commands.entity(button).despawn_recursive(); | ||
state.set(GameState::Playing)?; | ||
} | ||
Interaction::Hovered => { | ||
*color = button_colors.hovered.into(); | ||
} | ||
Interaction::None => { | ||
*color = button_colors.normal.into(); | ||
} | ||
} | ||
} | ||
get_menu_panel() | ||
.show(egui_context.ctx_mut(), |ui| { | ||
set_menu_style(ui.style_mut()); | ||
ui.vertical_centered_justified(|ui| { | ||
ui.add_space(50.); | ||
ui.heading("Foxtrot"); | ||
ui.separator(); | ||
ui.add_space(50.); | ||
if ui.button("Play").clicked() { | ||
state.set(GameState::Playing)?; | ||
} | ||
Ok(()) | ||
}) | ||
.inner | ||
}) | ||
.inner?; | ||
Ok(()) | ||
} | ||
|
||
fn get_menu_panel() -> egui::CentralPanel { | ||
egui::CentralPanel::default().frame(egui::Frame { | ||
inner_margin: egui::style::Margin::same(60.), | ||
..default() | ||
}) | ||
} | ||
|
||
fn set_menu_style(style: &mut egui::Style) { | ||
style.text_styles = [ | ||
(Heading, FontId::new(30.0, Proportional)), | ||
(Button, FontId::new(20.0, Proportional)), | ||
] | ||
.into(); | ||
style.visuals.widgets.noninteractive.fg_stroke.color = egui::Color32::from_gray(250); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters