diff --git a/src/features/game_screen/mod.rs b/src/features/game_screen/mod.rs index 9f503b7..2a74417 100644 --- a/src/features/game_screen/mod.rs +++ b/src/features/game_screen/mod.rs @@ -9,10 +9,10 @@ impl Plugin for GameScreenPlugin { } } -fn setup(mut commands: Commands) { - commands.insert_resource(ClearColor(Color::srgb(0.294, 0.412, 0.184))); +fn setup(mut clear_color: ResMut) { + *clear_color = ClearColor(Color::srgb(0.294, 0.412, 0.184)); } -fn cleanup(mut commands: Commands) { - commands.remove_resource::(); +fn cleanup(mut clear_color: ResMut) { + *clear_color = ClearColor(Color::srgb(0.2, 0.2, 0.2)); } diff --git a/src/features/hud/components.rs b/src/features/hud/components.rs new file mode 100644 index 0000000..547dfae --- /dev/null +++ b/src/features/hud/components.rs @@ -0,0 +1,67 @@ +use crate::{features::phase::components::TimerSettings, prelude::*}; + +#[derive(Component)] +pub enum RootMarker { + Status, + Settings, +} + +#[derive(Component)] +pub enum TextType { + Phase, + Timer, +} + +#[derive(Component)] +pub enum ButtonType { + SavegameDump, + SettingsOpen, + SettingsClose, + SettingsExit, + SettingsSave, + SettingsTimerChange { + input: SettingsTimerInput, + amount: i32, + }, +} + +#[derive(Clone)] +pub enum TimerType { + Focus, + ShortBreak, + LongBreak, +} + +impl TimerSettings { + pub fn change(&mut self, timer_type: &TimerType, amount: i32) { + match timer_type { + TimerType::Focus => { + self.focus_duration = (self.focus_duration as i32 + amount) as u32; + if self.focus_duration > 7259 { + // 120:59 + self.focus_duration = 7259; + } + } + TimerType::LongBreak => { + self.long_break_duration = (self.long_break_duration as i32 + amount) as u32; + if self.long_break_duration > 7259 { + // 120:59 + self.long_break_duration = 7259; + } + } + TimerType::ShortBreak => { + self.short_break_duration = (self.short_break_duration as i32 + amount) as u32; + if self.short_break_duration > 7259 { + // 120:59 + self.short_break_duration = 7259; + } + } + } + } +} + +#[derive(Component, Clone)] +pub enum SettingsTimerInput { + Minutes(TimerType), + Seconds(TimerType), +} diff --git a/src/features/hud/mod.rs b/src/features/hud/mod.rs new file mode 100644 index 0000000..a248c1e --- /dev/null +++ b/src/features/hud/mod.rs @@ -0,0 +1,157 @@ +use crate::features::phase::components::TimerSettings; +use crate::features::savegame::messages::SavegameDumpMessage; +use crate::prelude::*; +use components::*; +use ui::*; + +pub mod components; +pub mod ui; + +pub struct HudPlugin; + +impl Plugin for HudPlugin { + fn build(&self, app: &mut App) { + app.add_systems(OnEnter(AppState::GameScreen), setup); + app.add_systems(OnExit(AppState::GameScreen), cleanup); + app.add_systems( + Update, + (update_status, buttons, update_timer_settings).run_if(in_state(AppState::GameScreen)), + ); + } +} + +fn setup(mut commands: Commands) { + commands.spawn(( + RootMarker::Status, + Node { + position_type: PositionType::Absolute, + bottom: px(0), + left: px(0), + + width: percent(100), + height: px(50), + + justify_content: JustifyContent::SpaceAround, + align_items: AlignItems::Center, + flex_direction: FlexDirection::Row, + ..default() + }, + BackgroundColor(Color::srgba(0.0, 0.0, 0.0, 0.8)), + children![ + ( + TextType::Phase, + Text::new("..."), + TextFont::from_font_size(16.0), + TextColor(Color::WHITE) + ), + ( + TextType::Timer, + Text::new("--:--"), + TextFont::from_font_size(16.0), + TextColor(Color::WHITE) + ), + ( + Button, + ButtonType::SettingsOpen, + Node::default(), + children![ + Text::new("Einstellungen"), + TextFont::from_font_size(16.0), + TextColor(Color::WHITE) + ] + ) + ], + )); +} + +fn update_status(phase_res: Res, mut text_query: Query<(&mut Text, &TextType)>) { + if !phase_res.is_changed() { + return; + } + let current_phase = &phase_res.0; + + for (mut text, status_type) in text_query.iter_mut() { + text.0 = match status_type { + TextType::Phase => current_phase.display_name().into(), + TextType::Timer => current_phase.format_duration(), + }; + } +} + +fn buttons( + mut commands: Commands, + mut interaction_query: Query<(&Interaction, &ButtonType), (Changed, With