1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! Base systems for the menu.

use bevy::{prelude::*, sprite::MaterialMesh2dBundle, window::PrimaryWindow};
use game_library::colors::BACKGROUND_COLOR_50;

use game_library::state::Settings;

/// An entity tag for ease of cleanup when the menu is disabled.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Component)]
pub struct SettingsMenuEntity;

/// A tag specifically for the menu background.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Component)]
pub struct SettingsMenuBackground;

/// Clear the background (draw a blur) only when the menu is not disabled.
pub(super) fn clear_background(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<ColorMaterial>>,
    window_query: Query<&Window, With<PrimaryWindow>>,
    existing_background_query: Query<Entity, With<SettingsMenuBackground>>,
) {
    // Check if the background already exists.
    if existing_background_query.get_single().is_ok() {
        // If it does, just return.
        return;
    }

    let Ok(window) = window_query.get_single() else {
        tracing::warn!("Failed to get window size for menu background");
        return;
    };

    // Quad that draws over the whole screen.
    commands.spawn((
        MaterialMesh2dBundle {
            mesh: meshes
                .add(shape::Quad::new(Vec2::new(window.width(), window.height())).into())
                .into(),
            material: materials.add(ColorMaterial::from(BACKGROUND_COLOR_50)),
            transform: Transform::from_xyz(0., 0., 10.),
            ..default()
        },
        SettingsMenuBackground,
    ));
}

/// System to setup settings menu.
///
/// When the settings menu is entered, we should setup the menu.
pub(super) fn transition_to_base_menu(mut menu_state: ResMut<NextState<Settings>>) {
    menu_state.set(Settings::Main);
}