/*
* This file is part of sdbclient
* Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
*
* Licensed under GPL-3.0-only.
* See LICENSE for licensing information.
*/
use bevy::{app::AppExit, prelude::*};
use iyes_loopless::prelude::*;
use crate::cfg::CfgUser;
use crate::{despawn_screen, remove_ui, GameState};
mod mainmenuscreen;
use mainmenuscreen::*;
mod settingsmenuscreen;
use settingsmenuscreen::*;
mod settingsdisplayscreen;
use settingsdisplayscreen::*;
mod settingsaudioscreen;
use settingsaudioscreen::*;
mod settingsmiscscreen;
use settingsmiscscreen::*;
mod accountscreenloggedout;
use accountscreenloggedout::*;
mod accountscreenloggedin;
use accountscreenloggedin::*;
mod playscreen;
use playscreen::*;
use super::config::{SaveEvent, SaveEventValue};
mod accountlogin;
mod accountregister;
pub struct MenuPlugin;
impl Plugin for MenuPlugin {
fn build(&self, app: &mut App) {
app.
// Start with no menu. The menu is loaded when the GameState::MainMenu is entered.
add_loopless_state(MenuState::None)
.add_enter_system(GameState::MainMenu, menu_setup)
// Systems for main menu screen
.add_enter_system(MenuState::Main, main_menu_setup)
.add_exit_system(MenuState::Main, despawn_screen::<OnMainMenuScreen>)
// Systems for settings menu screen
.add_enter_system(MenuState::Settings, settings_menu_setup)
.add_exit_system(MenuState::Settings, despawn_screen::<OnSettingsMenuScreen>)
// Systems for settings display screen
.add_enter_system(MenuState::SettingsDisplay, settings_display_setup)
.add_exit_system(MenuState::SettingsDisplay, despawn_screen::<OnSettingsDisplayScreen>)
// Systems for settings audio screen
.add_enter_system(MenuState::SettingsAudio, settings_audio_setup)
.add_exit_system(MenuState::SettingsAudio, despawn_screen::<OnSettingsAudioScreen>)
// Systems for settings misc screen
.add_enter_system(MenuState::SettingsMisc, settings_misc_setup)
.add_exit_system(MenuState::SettingsMisc, despawn_screen::<OnSettingsMiscScreen>)
// Systems for account loggedout screen
.add_enter_system(MenuState::AccountLoggedOut, account_loggedout_setup)
.add_exit_system(MenuState::AccountLoggedOut, despawn_screen::<OnAccountLoggedOutScreen>)
// Systems for account loggedin screen
.add_enter_system(MenuState::AccountLoggedIn, account_loggedin_setup)
.add_exit_system(MenuState::AccountLoggedIn, despawn_screen::<OnAccountLoggedInScreen>)
// Systems for play screen
.add_enter_system(MenuState::Play, play_setup)
.add_exit_system(MenuState::Play, remove_ui)
// Common systems
.add_system_set(
ConditionSet::new()
.run_in_state(GameState::MainMenu)
.with_system(menu_action)
.with_system(button_system)
.into()
);
app.add_plugin(accountregister::AccountRegisterPlugin)
.add_plugin(accountlogin::AccountLoginPlugin);
}
}
/// Menu State
#[derive(Clone, Eq, PartialEq, Debug, Hash)]
pub enum MenuState {
None,
Main,
Play,
Lobby,
CreateGame,
JoinGame,
Settings,
SettingsDisplay,
SettingsAudio,
SettingsMisc,
AccountLoggedIn,
AccountLoggedOut,
AccountLogin,
AccountRegister,
}
/// Tag component for tagging entities on account screen
#[derive(Component)]
struct OnAccountScreen;
const NORMAL_BUTTON: Color = Color::rgb(0.15, 0.15, 0.15);
const HOVERED_BUTTON: Color = Color::rgb(0.25, 0.25, 0.25);
const HOVERED_PRESSED_BUTTON: Color = Color::rgb(0.25, 0.65, 0.25);
const PRESSED_BUTTON: Color = Color::rgb(0.35, 0.75, 0.35);
/// Tag component for tagging currently selected settings tab
#[derive(Component)]
struct SelectedSettingsTab;
/// All button actions
#[derive(Component)]
enum MenuButtonAction {
Play,
Settings,
SettingsDisplay,
SettingsAudio,
SettingsMisc,
Account,
AccountLogin,
AccountRegister,
AccountLogout,
BackToMainMenu,
BackToSettings,
Exit,
}
fn menu_action(
mut commands: Commands,
interaction_query: Query<
(&Interaction, &MenuButtonAction),
(Changed<Interaction>, With<Button>),
>,
mut app_exit_events: EventWriter<AppExit>,
mut cfg_user: ResMut<CfgUser>,
mut save_event_writer: EventWriter<SaveEvent>,
) {
for (interaction, menu_button_action) in &interaction_query {
if *interaction == Interaction::Clicked {
match menu_button_action {
MenuButtonAction::Exit => app_exit_events.send(AppExit),
MenuButtonAction::Play => commands.insert_resource(NextState(MenuState::Play)),
MenuButtonAction::Settings => {
commands.insert_resource(NextState(MenuState::Settings))
}
MenuButtonAction::SettingsDisplay => {
commands.insert_resource(NextState(MenuState::SettingsDisplay))
}
MenuButtonAction::SettingsAudio => {
commands.insert_resource(NextState(MenuState::SettingsAudio))
}
MenuButtonAction::SettingsMisc => {
commands.insert_resource(NextState(MenuState::SettingsMisc))
}
MenuButtonAction::Account => {
if cfg_user.logged_in {
commands.insert_resource(NextState(MenuState::AccountLoggedIn))
} else {
commands.insert_resource(NextState(MenuState::AccountLoggedOut))
}
}
MenuButtonAction::AccountLogin => {
commands.insert_resource(NextState(MenuState::AccountLogin));
commands.insert_resource(NextState(accountlogin::LoginState::Input));
}
MenuButtonAction::AccountRegister => {
commands.insert_resource(NextState(MenuState::AccountRegister));
commands.insert_resource(NextState(accountregister::RegisterState::Input));
}
MenuButtonAction::AccountLogout => {
// Reset CfgUser to default.
*cfg_user = CfgUser::default();
save_event_writer.send(SaveEvent {
value: SaveEventValue::User(cfg_user.clone()),
});
commands.insert_resource(NextState(MenuState::AccountLoggedOut))
}
MenuButtonAction::BackToSettings => {
commands.insert_resource(NextState(MenuState::Settings))
}
MenuButtonAction::BackToMainMenu => {
commands.insert_resource(NextState(MenuState::Main))
}
}
}
}
}
/// System for handling button hovering
fn button_system(
mut interaction_query: Query<
(
&Interaction,
&mut BackgroundColor,
Option<&SelectedSettingsTab>,
),
(Changed<Interaction>, With<Button>),
>,
) {
for (interaction, mut color, selected) in &mut interaction_query {
*color = match (*interaction, selected) {
(Interaction::Clicked, _) | (Interaction::None, Some(_)) => PRESSED_BUTTON.into(),
(Interaction::Hovered, Some(_)) => HOVERED_PRESSED_BUTTON.into(),
(Interaction::Hovered, None) => HOVERED_BUTTON.into(),
(Interaction::None, None) => NORMAL_BUTTON.into(),
}
}
}
fn menu_setup(mut commands: Commands) {
commands.insert_resource(NextState(MenuState::Main))
}