DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: aab10455fd1c6556d71bdbc1756c49e417b6d153 deck-builder/sdbclient/src/plugins/menu/mod.rs -rw-r--r-- 7.3 KiB
aab10455Jonni Liljamo sl changes and fixes 1 year, 11 months ago
                                                                                
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
/*
 * This file is part of sdbclient
 * Copyright (C) 2022 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

use bevy::{app::AppExit, prelude::*};

use crate::cfg::CfgUser;

use crate::{despawn_screen, 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 accountlogin;
mod accountregister;

const TEXT_COLOR: Color = Color::rgb(0.9, 0.9, 0.9);

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_state(MenuState::None)
            .add_system_set(SystemSet::on_enter(GameState::MainMenu).with_system(menu_setup))
            // Systems for main menu screen
            .add_system_set(SystemSet::on_enter(MenuState::Main).with_system(main_menu_setup))
            .add_system_set(SystemSet::on_exit(MenuState::Main).with_system(despawn_screen::<OnMainMenuScreen>))
            // Systems for settings menu screen
            .add_system_set(SystemSet::on_enter(MenuState::Settings).with_system(settings_menu_setup))
            .add_system_set(SystemSet::on_exit(MenuState::Settings).with_system(despawn_screen::<OnSettingsMenuScreen>))
            // Systems for settings display screen
            .add_system_set(SystemSet::on_enter(MenuState::SettingsDisplay).with_system(settings_display_setup))
            .add_system_set(SystemSet::on_exit(MenuState::SettingsDisplay).with_system(despawn_screen::<OnSettingsDisplayScreen>))
            // Systems for settings audio screen
            .add_system_set(SystemSet::on_enter(MenuState::SettingsAudio).with_system(settings_audio_setup))
            .add_system_set(SystemSet::on_exit(MenuState::SettingsAudio).with_system(despawn_screen::<OnSettingsAudioScreen>))
            // Systems for settings misc screen
            .add_system_set(SystemSet::on_enter(MenuState::SettingsMisc).with_system(settings_misc_setup))
            .add_system_set(SystemSet::on_exit(MenuState::SettingsMisc).with_system(despawn_screen::<OnSettingsMiscScreen>))
            // Systems for account loggedout screen
            .add_system_set(SystemSet::on_enter(MenuState::AccountLoggedOut).with_system(account_loggedout_setup))
            .add_system_set(SystemSet::on_exit(MenuState::AccountLoggedOut).with_system(despawn_screen::<OnAccountLoggedOutScreen>))
            // Systems for account loggedin screen
            .add_system_set(SystemSet::on_enter(MenuState::AccountLoggedIn).with_system(account_loggedin_setup))
            .add_system_set(SystemSet::on_exit(MenuState::AccountLoggedIn).with_system(despawn_screen::<OnAccountLoggedInScreen>))
            // Common systems
            .add_system_set(SystemSet::on_update(GameState::MainMenu).with_system(menu_action).with_system(button_system));

        app.add_plugin(accountregister::AccountRegisterPlugin)
            .add_plugin(accountlogin::AccountLoginPlugin);
    }
}

/// Menu State
#[derive(Clone, Eq, PartialEq, Debug, Hash)]
pub enum MenuState {
    None,
    Main,
    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(
    interaction_query: Query<
        (&Interaction, &MenuButtonAction),
        (Changed<Interaction>, With<Button>),
    >,
    mut app_exit_events: EventWriter<AppExit>,
    mut menu_state: ResMut<State<MenuState>>,
    mut register_state: ResMut<State<accountregister::RegisterState>>,
    mut login_state: ResMut<State<accountlogin::LoginState>>,
    //mut game_state: ResMut<State<GameState>>,
    cfg_user: Res<CfgUser>,
) {
    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 => warn!("todo"),
                MenuButtonAction::Settings => menu_state.set(MenuState::Settings).unwrap(),
                MenuButtonAction::SettingsDisplay => {
                    menu_state.set(MenuState::SettingsDisplay).unwrap()
                }
                MenuButtonAction::SettingsAudio => {
                    menu_state.set(MenuState::SettingsAudio).unwrap()
                }
                MenuButtonAction::SettingsMisc => menu_state.set(MenuState::SettingsMisc).unwrap(),
                MenuButtonAction::Account => {
                    if cfg_user.logged_in {
                        menu_state.set(MenuState::AccountLoggedIn).unwrap()
                    } else {
                        menu_state.set(MenuState::AccountLoggedOut).unwrap()
                    }
                }
                MenuButtonAction::AccountLogin => {
                    menu_state.set(MenuState::AccountLogin).unwrap();
                    login_state.set(accountlogin::LoginState::Input).unwrap();
                }
                MenuButtonAction::AccountRegister => {
                    menu_state.set(MenuState::AccountRegister).unwrap();
                    register_state
                        .set(accountregister::RegisterState::Input)
                        .unwrap();
                }
                MenuButtonAction::AccountLogout => warn!("todo"),
                MenuButtonAction::BackToSettings => menu_state.set(MenuState::Settings).unwrap(),
                MenuButtonAction::BackToMainMenu => menu_state.set(MenuState::Main).unwrap(),
            }
        }
    }
}

/// 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 menu_state: ResMut<State<MenuState>>) {
    let _ = menu_state.set(MenuState::Main);
}