186 lines
6.1 KiB
Rust
186 lines
6.1 KiB
Rust
use crate::features::hud::components::{SettingsTimerInput, TimerType};
|
|
use crate::features::phase::components::TimerSettings;
|
|
use crate::features::savegame::ui::spawn_load_popup;
|
|
use crate::prelude::*;
|
|
use components::*;
|
|
use ui::settings::open_settings_menu;
|
|
|
|
pub mod components;
|
|
pub mod ui;
|
|
|
|
/// Plugin for the main menu screen.
|
|
pub struct StartScreenPlugin;
|
|
|
|
impl Plugin for StartScreenPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app.init_resource::<StartScreenTimerSettings>();
|
|
app.add_systems(OnEnter(AppState::StartScreen), setup);
|
|
app.add_systems(OnExit(AppState::StartScreen), cleanup);
|
|
app.add_systems(
|
|
Update,
|
|
(menu, handle_settings_buttons, update_timer_settings_display)
|
|
.run_if(in_state(AppState::StartScreen)),
|
|
);
|
|
app.add_systems(
|
|
PostUpdate,
|
|
apply_start_screen_settings.run_if(in_state(AppState::GameScreen)),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Spawns the main menu UI.
|
|
fn setup(mut commands: Commands) {
|
|
commands.spawn((
|
|
RootMarker::MainMenu,
|
|
Node {
|
|
width: percent(100),
|
|
height: percent(100),
|
|
flex_direction: FlexDirection::Column,
|
|
row_gap: px(10),
|
|
..Node::center()
|
|
},
|
|
children![
|
|
text("Pomomon Garden", 64.0, Color::WHITE),
|
|
button(
|
|
ButtonType::LoadGame,
|
|
ButtonVariant::Primary,
|
|
Node {
|
|
width: px(280),
|
|
padding: UiRect::all(px(10)),
|
|
..default()
|
|
},
|
|
|color| text("Spiel laden", 33.0, color)
|
|
),
|
|
button(
|
|
ButtonType::NewGame,
|
|
ButtonVariant::Primary,
|
|
Node {
|
|
width: px(280),
|
|
padding: UiRect::all(px(10)),
|
|
..default()
|
|
},
|
|
|color| text("Neues Spiel", 33.0, color)
|
|
),
|
|
button(
|
|
ButtonType::Settings,
|
|
ButtonVariant::Secondary,
|
|
Node {
|
|
width: px(280),
|
|
padding: UiRect::all(px(10)),
|
|
..default()
|
|
},
|
|
|color| text("Einstellungen", 33.0, color)
|
|
),
|
|
],
|
|
));
|
|
}
|
|
|
|
/// Handles main menu button interactions.
|
|
fn menu(
|
|
mut commands: Commands,
|
|
mut next_state: ResMut<NextState<AppState>>,
|
|
mut interaction_query: Query<(&Interaction, &ButtonType), (Changed<Interaction>, With<Button>)>,
|
|
asset_server: Res<AssetServer>,
|
|
) {
|
|
for (interaction, button_type) in &mut interaction_query {
|
|
match *interaction {
|
|
Interaction::Pressed => match button_type {
|
|
ButtonType::LoadGame => {
|
|
spawn_load_popup(&mut commands, &asset_server);
|
|
}
|
|
ButtonType::NewGame => {
|
|
commands.insert_resource(SavegamePath::next());
|
|
next_state.set(AppState::GameScreen);
|
|
}
|
|
ButtonType::Settings => {
|
|
open_settings_menu(&mut commands);
|
|
}
|
|
},
|
|
_ => (),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Cleans up main menu resources.
|
|
fn cleanup(mut commands: Commands, query: Query<Entity, With<RootMarker>>) {
|
|
for entity in query.iter() {
|
|
commands.entity(entity).despawn();
|
|
}
|
|
}
|
|
|
|
/// Handles button interactions within the settings menu.
|
|
fn handle_settings_buttons(
|
|
mut interaction_query: Query<
|
|
(&Interaction, &crate::features::hud::components::ButtonType),
|
|
(Changed<Interaction>, With<Button>),
|
|
>,
|
|
mut timer_settings: ResMut<TimerSettings>,
|
|
mut ss_timer_settings: ResMut<StartScreenTimerSettings>,
|
|
keys: Res<ButtonInput<KeyCode>>,
|
|
) {
|
|
let shift_multiplier = if keys.any_pressed([KeyCode::ShiftLeft, KeyCode::ShiftRight]) {
|
|
10
|
|
} else {
|
|
1
|
|
};
|
|
|
|
for (interaction, button_type) in &mut interaction_query {
|
|
if *interaction == Interaction::Pressed {
|
|
if let crate::features::hud::components::ButtonType::SettingsTimerChange {
|
|
input,
|
|
amount,
|
|
} = button_type
|
|
{
|
|
match input {
|
|
SettingsTimerInput::Minutes(timer_type) => {
|
|
timer_settings.change(timer_type, 60 * amount * shift_multiplier)
|
|
}
|
|
SettingsTimerInput::Seconds(timer_type) => {
|
|
timer_settings.change(timer_type, *amount * shift_multiplier)
|
|
}
|
|
}
|
|
ss_timer_settings.0 = Some(timer_settings.clone());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Updates the timer settings display in the settings menu.
|
|
fn update_timer_settings_display(
|
|
timer_settings: Res<TimerSettings>,
|
|
mut query: Query<(&SettingsTimerInput, &mut Text)>,
|
|
) {
|
|
for (input_type, mut text) in query.iter_mut() {
|
|
match input_type {
|
|
SettingsTimerInput::Minutes(timer_type) => {
|
|
let value = match timer_type {
|
|
TimerType::Focus => timer_settings.focus_duration,
|
|
TimerType::ShortBreak => timer_settings.short_break_duration,
|
|
TimerType::LongBreak => timer_settings.long_break_duration,
|
|
} as f32;
|
|
|
|
text.0 = format!("{:0>2}", (value / 60.0).floor());
|
|
}
|
|
SettingsTimerInput::Seconds(timer_type) => {
|
|
let value = match timer_type {
|
|
TimerType::Focus => timer_settings.focus_duration,
|
|
TimerType::ShortBreak => timer_settings.short_break_duration,
|
|
TimerType::LongBreak => timer_settings.long_break_duration,
|
|
};
|
|
|
|
text.0 = format!("{:0>2}", (value % 60));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Applies the timer settings from the start screen once the game screen is entered.
|
|
fn apply_start_screen_settings(
|
|
mut settings: ResMut<TimerSettings>,
|
|
mut ss_settings: ResMut<StartScreenTimerSettings>,
|
|
) {
|
|
if let Some(new_settings) = ss_settings.0.take() {
|
|
*settings = new_settings;
|
|
}
|
|
}
|