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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use bevy::{color::palettes::css::BLACK, prelude::*, window::PrimaryWindow};

use bevy_lunex::{
    prelude::{MainUi, Pickable, Rl, UiNodeTreeInitTrait, UiTree},
    Base, MovableByCamera, PackageLayout, UiClickEvent, UiImage2dBundle, UiLayout, UiLink,
    UiTreeBundle,
};

use crate::{
    loading::components::Loading,
    main_menu::components::MainMenuButton,
    resources::CodexSettings,
    settings::components::SettingsPg,
    widgets::{
        button::components::{CustomButton, CustomButtonRef},
        panel::components::Panel,
    },
};

use super::components::MainMenu;

pub fn build_main_menu(
    mut commands: Commands,
    asset_server: Res<AssetServer>,
    query: Query<Entity, Added<MainMenu>>,
    window: Query<&Window, With<PrimaryWindow>>,
    codex_settings: Res<CodexSettings>,
) {
    for route_entity in &query {
        if let Ok(resolution) = window.get_single() {
            let r_size = (resolution.width(), resolution.height());
            commands
                .entity(route_entity)
                .insert(SpatialBundle::default())
                .with_children(|route| {
                    route
                        .spawn((
                            UiTreeBundle::<MainUi>::from(UiTree::new2d("MainMenu")),
                            MovableByCamera,
                        ))
                        .with_children(|ui| {
                            let root = UiLink::<MainUi>::path("Root");
                            ui.spawn((
                                root.clone(),
                                UiLayout::window().size(r_size).pack::<Base>(),
                            ));
                            let background = UiLink::<MainUi>::path("Background");

                            ui.spawn((
                                background.clone(),
                                UiLayout::window_full().pack::<Base>(),
                                Pickable::IGNORE,
                                UiImage2dBundle::from(asset_server.load("Level_base_diffuse.png")),
                            ));
                            let content = vec![
                                CustomButtonRef {
                                    link: MainMenuButton::NewGame.str(),
                                },
                                CustomButtonRef {
                                    link: MainMenuButton::Settings.str(),
                                },
                                CustomButtonRef {
                                    link: MainMenuButton::QuitGame.str(),
                                },
                            ];
                            ui.spawn((
                                background.add("Panel"),
                                UiLayout::window()
                                    .size(Rl((40.0, 80.0)))
                                    .pos(Rl((10.0, 10.0)))
                                    .pack::<Base>(),
                                Panel {
                                    text: Some(codex_settings.title.to_string()),
                                    color: BLACK.into(),
                                    content,
                                    ..default()
                                },
                                Pickable::IGNORE,
                            ));
                        });
                });
        }
    }
}

pub fn main_menu_button_clicked_system(
    mut commands: Commands,
    mut events: EventReader<UiClickEvent>,
    query: Query<&CustomButton>,
    menu_q: Query<Entity, With<MainMenu>>,
    mut exit: EventWriter<bevy::app::AppExit>,
) {
    for event in events.read() {
        if let Ok(ent) = menu_q.get_single() {
            if let Ok(button) = query.get(event.target) {
                if button.text == MainMenuButton::NewGame.str() {
                    commands.entity(ent).despawn_recursive();
                    commands.spawn(Loading(Some("Loading...".to_string())));
                } else if button.text == MainMenuButton::Settings.str() {
                    commands.entity(ent).despawn_recursive();
                    commands.spawn(SettingsPg);
                } else if button.text == MainMenuButton::QuitGame.str() {
                    commands.entity(event.target).despawn_recursive();
                    exit.send(bevy::app::AppExit::Success);
                }
            }
        }
    }
}