Skip to content

Commit

Permalink
Implement Pause Menu
Browse files Browse the repository at this point in the history
  • Loading branch information
zicklag committed Jul 2, 2022
1 parent 72215ce commit ca0b674
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 9 deletions.
11 changes: 8 additions & 3 deletions assets/default.game.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ camera_height: 448

main_menu:
title: "Fish Fight\nPunchy"
title_size: 45
title_font: ark

background_image:
Expand All @@ -14,6 +13,12 @@ ui_theme:
fonts:
ark: ui/ark-pixel-16px-latin.ttf

font_sizes:
jumbo: 45
big: 35
normal: 15
small: 10

panel:
text_color: [51, 40, 40]
padding:
Expand All @@ -33,13 +38,13 @@ ui_theme:

button:
text_color: [255, 255, 255]
font_size: 30

font: ark
padding:
top: 12
left: 12
right: 12
bottom: 12
bottom: 16
borders:
default:
image: ui/green-button.png
Expand Down
12 changes: 10 additions & 2 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ pub struct GameMeta {
#[serde(deny_unknown_fields)]
pub struct MainMenuMeta {
pub title: String,
pub title_size: f32,
pub title_font: String,
pub background_image: ImageMeta,
}
Expand Down Expand Up @@ -141,10 +140,20 @@ pub struct UIThemeMeta {
pub fonts: HashMap<String, String>,
#[serde(skip)]
pub font_handles: HashMap<String, Handle<EguiFont>>,
pub font_sizes: FontSizesMeta,
pub panel: UIPanelThemeMeta,
pub button: UIButtonThemeMeta,
}

#[derive(Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct FontSizesMeta {
pub jumbo: f32,
pub big: f32,
pub normal: f32,
pub small: f32,
}

#[derive(Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct UIPanelThemeMeta {
Expand All @@ -160,7 +169,6 @@ pub struct UIPanelThemeMeta {
pub struct UIButtonThemeMeta {
#[serde(default)]
pub text_color: ColorMeta,
pub font_size: f32,
pub font: String,
#[serde(default)]
pub padding: MarginMeta,
Expand Down
92 changes: 88 additions & 4 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,92 @@ fn update_ui_scale(
}
}

fn pause_menu(mut egui_context: ResMut<EguiContext>) {
egui::Window::new("Paused").show(egui_context.ctx_mut(), |_ui| {});
fn pause_menu(
mut commands: Commands,
mut egui_context: ResMut<EguiContext>,
game: Res<GameMeta>,
non_camera_entities: Query<Entity, Without<Camera>>,
) {
let ui_theme = &game.ui_theme;

egui::CentralPanel::default()
.frame(egui::Frame::none())
.show(egui_context.ctx_mut(), |ui| {
let screen_rect = ui.max_rect();

let pause_menu_width = 300.0;
let x_margin = (screen_rect.width() - pause_menu_width) / 2.0;
let outer_margin = egui::style::Margin::symmetric(x_margin, screen_rect.height() * 0.2);

BorderedFrame::new(&ui_theme.panel.border)
.margin(outer_margin)
.padding(ui_theme.panel.padding.into())
.show(ui, |ui| {
let text_color = ui_theme.panel.text_color;

ui.set_min_width(ui.available_width());

ui.vertical_centered(|ui| {
ui.label(
RichText::new("Paused")
.font(egui::FontId::new(
ui_theme.font_sizes.big,
egui::FontFamily::Name(
game.main_menu.title_font.clone().into(),
),
))
.color(text_color),
);

ui.add_space(10.0);

let width = ui.available_width();

if BorderedButton::new(
RichText::new("Continue")
.font(egui::FontId::new(
ui_theme.font_sizes.normal,
egui::FontFamily::Name(ui_theme.button.font.clone().into()),
))
.color(ui_theme.button.text_color),
)
.min_size(egui::Vec2::new(width, 0.0))
.padding(ui_theme.button.padding.into())
.border(&ui_theme.button.borders.default)
.on_click_border(ui_theme.button.borders.clicked.as_ref())
.on_hover_border(ui_theme.button.borders.hovered.as_ref())
.show(ui)
.clicked()
{
commands.insert_resource(NextState(GameState::InGame));
}

if BorderedButton::new(
RichText::new("Main Menu")
.font(egui::FontId::new(
ui_theme.font_sizes.normal,
egui::FontFamily::Name(ui_theme.button.font.clone().into()),
))
.color(ui_theme.button.text_color),
)
.min_size(egui::Vec2::new(width, 0.0))
.padding(ui_theme.button.padding.into())
.border(&ui_theme.button.borders.default)
.on_click_border(ui_theme.button.borders.clicked.as_ref())
.on_hover_border(ui_theme.button.borders.hovered.as_ref())
.show(ui)
.clicked()
{
// Clean up all entities other than the camera
for entity in non_camera_entities.iter() {
commands.entity(entity).despawn();
}
// Show the main menu
commands.insert_resource(NextState(GameState::MainMenu));
}
});
})
});
}

#[derive(Component)]
Expand Down Expand Up @@ -180,7 +264,7 @@ fn main_menu(mut commands: Commands, mut egui_context: ResMut<EguiContext>, game
ui.label(
RichText::new(&game.main_menu.title)
.font(egui::FontId::new(
game.main_menu.title_size,
ui_theme.font_sizes.jumbo,
egui::FontFamily::Name(
game.main_menu.title_font.clone().into(),
),
Expand All @@ -194,7 +278,7 @@ fn main_menu(mut commands: Commands, mut egui_context: ResMut<EguiContext>, game
if BorderedButton::new(
RichText::new("Start Game")
.font(egui::FontId::new(
ui_theme.button.font_size,
ui_theme.font_sizes.big,
egui::FontFamily::Name(ui_theme.button.font.clone().into()),
))
.color(ui_theme.button.text_color),
Expand Down

0 comments on commit ca0b674

Please sign in to comment.