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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
use crate::prelude::*;

use super::ImageMeta;

mod credits;
mod map_select;
pub mod player_select;
pub(super) mod settings;
use shadow_rs::shadow;

// Generate build info.
shadow!(build_info);

#[cfg(not(target_arch = "wasm32"))]
mod network_game;

#[derive(HasSchema, Debug, Default, Clone)]
#[repr(C)]
pub struct MainMenuMeta {
    pub title_font: FontMeta,
    pub subtitle_font: FontMeta,
    pub background_image: ImageMeta,
    pub menu_width: f32,
}

pub fn session_plugin(session: &mut Session) {
    session
        // Install the default bones_framework plugin for this session
        .install_plugin(DefaultSessionPlugin)
        .add_startup_system(setup_menu)
        // Add our menu system to the update stage
        .add_system_to_stage(Update, main_menu_system);
}

fn setup_menu(
    meta: Root<GameMeta>,
    mut egui_settings: ResMutInit<EguiSettings>,
    mut entities: ResMut<Entities>,
    mut sprites: CompMut<Sprite>,
    mut transforms: CompMut<Transform>,
    mut cameras: CompMut<Camera>,
    mut clear_color: ResMutInit<ClearColor>,
) {
    egui_settings.scale = meta.theme.scale;
    **clear_color = Color::BLACK;
    spawn_default_camera(&mut entities, &mut transforms, &mut cameras);

    for i in -1..=1 {
        let ent = entities.create();
        transforms.insert(
            ent,
            Transform::from_translation(vec3(
                meta.main_menu.background_image.image_size.x * i as f32,
                0.,
                0.,
            )),
        );
        sprites.insert(
            ent,
            Sprite {
                image: meta.main_menu.background_image.image,
                ..default()
            },
        );
    }
}

/// Which page of the menu we are on
#[derive(HasSchema, Clone, Copy, Default)]
#[repr(C, u8)]
pub enum MenuPage {
    #[default]
    Home,
    Settings,
    PlayerSelect,
    MapSelect {
        /// Indicates the client is waiting for the map to be selected, not actually picking the
        /// map.
        is_waiting: bool,
    },
    Credits,
    NetworkGame,
}

#[allow(clippy::const_is_empty)]
static VERSION_STRING: Lazy<String> = Lazy::new(|| {
    format!(
        "{}{}",
        build_info::PKG_VERSION,
        if !build_info::SHORT_COMMIT.is_empty() {
            format!(
                "-{}{}",
                build_info::SHORT_COMMIT,
                if build_info::GIT_CLEAN {
                    ""
                } else {
                    " (dirty)"
                }
            )
        } else {
            String::default()
        }
    )
});

fn main_menu_system(world: &World) {
    let ctx = (*world.resource::<EguiCtx>()).clone();
    let mut close_settings_menu = false;

    // Go to player select menu if either of the `TEST_PLAYER` or `TEST_MAP`
    // debug env vars are present.
    #[cfg(debug_assertions)]
    {
        use std::env::var_os;
        use std::sync::atomic::{AtomicBool, Ordering};
        static DEBUG_DID_CHECK_ENV_VARS: AtomicBool = AtomicBool::new(false);
        if DEBUG_DID_CHECK_ENV_VARS
            .compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed)
            .is_ok()
        {
            let test_vars = [
                var_os("TEST_MAP"),
                var_os("TEST_PLAYER"),
                var_os("TEST_HAT"),
                var_os("TEST_CONTROLLER"),
            ];
            if test_vars.iter().any(Option::is_some) {
                ctx.set_state(MenuPage::PlayerSelect);
            }
        }
    }

    egui::CentralPanel::default()
        .frame(egui::Frame::none())
        .show(&ctx, |ui| match ctx.get_state::<MenuPage>() {
            MenuPage::Home => world.run_system(home_menu, ui),
            MenuPage::Settings => {
                world.run_system(settings::widget, (ui, &mut close_settings_menu))
            }
            MenuPage::PlayerSelect => world.run_system(player_select::widget, ui),
            MenuPage::MapSelect { .. } => world.run_system(map_select::widget, ui),
            MenuPage::Credits => world.run_system(credits::widget, ui),
            MenuPage::NetworkGame =>
            {
                #[cfg(not(target_arch = "wasm32"))]
                world.run_system(network_game::widget, ui)
            }
        });

    if close_settings_menu {
        ctx.set_state(MenuPage::Home);
    }

    egui::CentralPanel::default()
        .frame(egui::Frame::none())
        .show(&ctx, |ui| {
            ui.with_layout(egui::Layout::bottom_up(egui::Align::Max), |ui| {
                ui.add_space(5.0);
                ui.with_layout(egui::Layout::right_to_left(egui::Align::Max), |ui| {
                    ui.add_space(5.0);

                    ui.label(
                        egui::RichText::new(VERSION_STRING.as_str()).color(egui::Color32::WHITE),
                    );
                })
            });
        });
}

/// System to render the home menu.
fn home_menu(
    mut ui: In<&mut egui::Ui>,
    meta: Root<GameMeta>,
    localization: Localization<GameMeta>,
    #[cfg(not(target_arch = "wasm32"))] exit_game: Option<ResMut<ExitBones>>,
) {
    let ui = &mut *ui;
    ui.vertical_centered(|ui| {
        ui.add_space(meta.main_menu.title_font.size / 2.0);
        ui.label(meta.main_menu.title_font.rich(localization.get("title")));
        ui.label(
            meta.main_menu
                .subtitle_font
                .rich(localization.get("subtitle")),
        );

        ui.add_space(meta.main_menu.subtitle_font.size / 2.0);

        BorderedFrame::new(&meta.theme.panel.border)
            .padding(meta.theme.panel.padding)
            .show(ui, |ui| {
                ui.set_width(meta.main_menu.menu_width);

                // Local game
                if BorderedButton::themed(
                    &meta.theme.buttons.normal,
                    localization.get("local-game"),
                )
                .min_size(vec2(ui.available_width(), 0.0))
                .show(ui)
                .focus_by_default(ui)
                .clicked()
                {
                    ui.ctx().set_state(MenuPage::PlayerSelect);
                }

                // Online game
                #[cfg(not(target_arch = "wasm32"))]
                if BorderedButton::themed(
                    &meta.theme.buttons.normal,
                    localization.get("online-game"),
                )
                .min_size(vec2(ui.available_width(), 0.0))
                .show(ui)
                .clicked()
                {
                    ui.ctx().set_state(MenuPage::NetworkGame);
                }

                // Settings
                if BorderedButton::themed(&meta.theme.buttons.normal, localization.get("settings"))
                    .min_size(vec2(ui.available_width(), 0.0))
                    .show(ui)
                    .clicked()
                {
                    ui.ctx().set_state(MenuPage::Settings);
                }

                // Credits
                if BorderedButton::themed(&meta.theme.buttons.normal, localization.get("credits"))
                    .min_size(vec2(ui.available_width(), 0.0))
                    .show(ui)
                    .clicked()
                {
                    ui.ctx().set_state(MenuPage::Credits);
                }

                #[cfg(not(target_arch = "wasm32"))]
                if BorderedButton::themed(&meta.theme.buttons.normal, localization.get("quit"))
                    .min_size(vec2(ui.available_width(), 0.0))
                    .show(ui)
                    .clicked()
                {
                    if let Some(mut exit) = exit_game {
                        **exit = true;
                    }
                }
            });
    });
}

#[cfg(debug_assertions)]
fn handle_names_to_string<'handles, T, It, F>(it: It, get_name: F) -> String
where
    T: 'handles,
    It: IntoIterator<Item = Handle<T>>,
    F: Fn(Handle<T>) -> &'static str,
{
    let mut names = String::new();
    let mut is_first = true;
    for h in it.into_iter() {
        if is_first {
            is_first = false;
        } else {
            names.push_str(", ");
        }
        names.push('"');
        names.push_str(get_name(h));
        names.push('"');
    }
    names
}