feat: Implement GameScreen settings with timer settings (#33)

This commit is contained in:
demenik
2025-11-27 21:30:33 +01:00
parent 485b2c2907
commit 41cf86063c
12 changed files with 391 additions and 26 deletions

View File

@@ -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<ClearColor>) {
*clear_color = ClearColor(Color::srgb(0.294, 0.412, 0.184));
}
fn cleanup(mut commands: Commands) {
commands.remove_resource::<ClearColor>();
fn cleanup(mut clear_color: ResMut<ClearColor>) {
*clear_color = ClearColor(Color::srgb(0.2, 0.2, 0.2));
}

View File

@@ -1,4 +1,4 @@
use crate::prelude::*;
use crate::{features::phase::components::TimerSettings, prelude::*};
#[derive(Component)]
pub enum RootMarker {
@@ -17,4 +17,51 @@ 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),
}

View File

@@ -1,5 +1,8 @@
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;
@@ -10,8 +13,10 @@ 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.run_if(in_state(AppState::GameScreen)));
app.add_systems(Update, buttons.run_if(in_state(AppState::GameScreen)));
app.add_systems(
Update,
(update_status, buttons, update_timer_settings).run_if(in_state(AppState::GameScreen)),
);
}
}
@@ -74,12 +79,42 @@ fn update_status(phase_res: Res<CurrentPhase>, mut text_query: Query<(&mut Text,
}
fn buttons(
mut commands: Commands,
mut interaction_query: Query<(&Interaction, &ButtonType), (Changed<Interaction>, With<Button>)>,
root_query: Query<(Entity, &RootMarker)>,
mut savegame_messages: MessageWriter<SavegameDumpMessage>,
mut next_state: ResMut<NextState<AppState>>,
mut timer_settings: ResMut<TimerSettings>,
) {
for (interaction, button_type) in &mut interaction_query {
match *interaction {
Interaction::Pressed => match button_type {
ButtonType::SettingsOpen => {}
ButtonType::SettingsOpen => {
open_settings(&mut commands);
}
ButtonType::SettingsClose => {
for (entity, root) in root_query.iter() {
match *root {
RootMarker::Settings => commands.entity(entity).despawn(),
_ => {}
}
}
}
ButtonType::SettingsExit => {
savegame_messages.write(SavegameDumpMessage);
next_state.set(AppState::StartScreen);
}
ButtonType::SettingsSave => {
savegame_messages.write(SavegameDumpMessage);
}
ButtonType::SettingsTimerChange { input, amount } => match input {
SettingsTimerInput::Minutes(timer_type) => {
timer_settings.change(timer_type, 60 * amount)
}
SettingsTimerInput::Seconds(timer_type) => {
timer_settings.change(timer_type, *amount)
}
},
_ => {}
},
_ => {}
@@ -92,3 +127,31 @@ fn cleanup(mut commands: Commands, query: Query<Entity, With<RootMarker>>) {
commands.entity(entity).despawn();
}
}
fn update_timer_settings(
timer_settings: ResMut<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));
}
}
}
}

View File

@@ -1 +1,5 @@
pub mod settings;
pub mod timer_settings;
pub use settings::*;
pub use timer_settings::*;

View File

@@ -1,3 +1,182 @@
use super::super::components::*;
use super::timer_settings::timer_settings;
use crate::prelude::*;
pub fn open_settings(commands: &mut Commands) {}
pub fn open_settings(commands: &mut Commands) {
commands
.spawn((
RootMarker::Settings,
Node {
position_type: PositionType::Absolute,
width: percent(100),
height: percent(100),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
ZIndex(1),
BackgroundColor(Color::srgba(0.0, 0.0, 0.0, 0.8)),
))
.with_children(|parent| {
parent
.spawn((
Node {
flex_direction: FlexDirection::Column,
align_items: AlignItems::Center,
padding: UiRect::all(px(20.0)),
..default()
},
BackgroundColor(Color::srgb(0.2, 0.2, 0.2)),
BorderRadius::all(px(10.0)),
))
.with_children(|parent| {
parent.spawn((
Node {
width: percent(100.0),
justify_content: JustifyContent::SpaceBetween,
align_items: AlignItems::Center,
margin: UiRect::bottom(px(20.0)),
column_gap: px(20.0),
..default()
},
children![
(
Text::new("Spiel Einstellungen"),
TextFont::from_font_size(40.0),
TextColor(Color::WHITE),
),
(
Button,
ButtonType::SettingsClose,
Node {
width: px(40.0),
height: px(40.0),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
BackgroundColor(Color::srgb(0.8, 0.2, 0.2)),
BorderRadius::MAX,
children![(
Text::new("X"),
TextFont::from_font_size(24.0),
TextColor(Color::WHITE),
)]
)
],
));
parent
.spawn(Node {
width: percent(100),
flex_direction: FlexDirection::Column,
margin: UiRect::top(px(10.0)),
row_gap: px(10.0),
..default()
})
.with_children(|parent| {
parent.spawn((
Button,
ButtonType::SettingsExit,
Node {
width: percent(100),
height: px(80),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
padding: UiRect::horizontal(px(10.0)),
..default()
},
BackgroundColor(NORMAL_BUTTON),
BorderRadius::all(px(10)),
children![(
Text::new("Spiel verlassen"),
TextFont::from_font_size(24.0),
TextColor(Color::WHITE)
)],
));
parent.spawn((
Button,
ButtonType::SettingsSave,
Node {
width: percent(100),
height: px(80),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
padding: UiRect::horizontal(px(10.0)),
..default()
},
BackgroundColor(NORMAL_BUTTON),
BorderRadius::all(px(10)),
children![(
Text::new("Spiel speichern"),
TextFont::from_font_size(24.0),
TextColor(Color::WHITE)
)],
));
parent.spawn((
Node {
width: percent(100),
flex_direction: FlexDirection::Row,
justify_content: JustifyContent::SpaceBetween,
align_items: AlignItems::Center,
column_gap: px(10),
padding: UiRect::horizontal(px(10.0)),
..default()
},
children![
(
Node {
flex_direction: FlexDirection::Column,
align_items: AlignItems::Center,
row_gap: px(10),
..default()
},
children![
(
Text::new("Fokus Phase"),
TextFont::from_font_size(12.0),
TextColor(Color::WHITE)
),
timer_settings(TimerType::Focus)
]
),
(
Node {
flex_direction: FlexDirection::Column,
align_items: AlignItems::Center,
row_gap: px(10),
..default()
},
children![
(
Text::new("Kurze Pause"),
TextFont::from_font_size(12.0),
TextColor(Color::WHITE)
),
timer_settings(TimerType::ShortBreak)
]
),
(
Node {
flex_direction: FlexDirection::Column,
align_items: AlignItems::Center,
row_gap: px(10),
..default()
},
children![
(
Text::new("Lange Pause"),
TextFont::from_font_size(12.0),
TextColor(Color::WHITE)
),
timer_settings(TimerType::LongBreak)
]
)
],
));
});
});
});
}

View File

@@ -0,0 +1,74 @@
use super::super::components::*;
use crate::prelude::*;
pub fn timer_settings(timer_type: TimerType) -> impl Bundle {
(
Node {
flex_direction: FlexDirection::Row,
align_items: AlignItems::Center,
..default()
},
children![
timer_settings_part(SettingsTimerInput::Minutes(timer_type.clone()), 1),
(
Text::new(":"),
TextFont::from_font_size(24.0),
TextColor(Color::WHITE)
),
timer_settings_part(SettingsTimerInput::Seconds(timer_type.clone()), 1),
],
)
}
fn timer_settings_part(input: SettingsTimerInput, amount: u32) -> impl Bundle {
(
Node {
flex_direction: FlexDirection::Column,
..default()
},
children![
(
Button,
ButtonType::SettingsTimerChange {
input: input.clone(),
amount: amount as i32
},
Node {
width: auto(),
justify_content: JustifyContent::Center,
..default()
},
BackgroundColor(NORMAL_BUTTON),
children![
Text::new("+"),
TextFont::from_font_size(12.0),
TextColor(Color::WHITE)
]
),
(
input.clone(),
Text::new("--"),
TextFont::from_font_size(24.0),
TextColor(Color::WHITE)
),
(
Button,
ButtonType::SettingsTimerChange {
input: input.clone(),
amount: -(amount as i32),
},
Node {
width: auto(),
justify_content: JustifyContent::Center,
..default()
},
BackgroundColor(NORMAL_BUTTON),
children![
Text::new("-"),
TextFont::from_font_size(12.0),
TextColor(Color::WHITE)
]
),
],
)
}

View File

@@ -42,18 +42,18 @@ pub struct CurrentPhase(pub Phase);
#[derive(Resource, Debug, Serialize, Deserialize, Clone)]
pub struct TimerSettings {
pub focus_duration: f32,
pub short_break_duration: f32,
pub long_break_duration: f32,
pub focus_duration: u32,
pub short_break_duration: u32,
pub long_break_duration: u32,
pub long_break_interval: u32,
}
impl Default for TimerSettings {
fn default() -> Self {
Self {
focus_duration: 25.0 * 60.0,
short_break_duration: 5.0 * 60.0,
long_break_duration: 15.0 * 60.0,
focus_duration: 25 * 60,
short_break_duration: 5 * 60,
long_break_duration: 15 * 60,
long_break_interval: 4,
}
}

View File

@@ -54,7 +54,7 @@ fn load_rules(mut phase_res: ResMut<CurrentPhase>, settings: Res<TimerSettings>)
let new_phase = match phase {
Phase::Focus { .. } => Some(Phase::Focus {
duration: settings.focus_duration,
duration: settings.focus_duration as f32,
}),
Phase::Break { .. } => Some(Phase::Break { duration: 0.0 }),
_ => None,
@@ -141,11 +141,11 @@ fn handle_continue(
if is_long_break {
*phase = Phase::Break {
duration: settings.long_break_duration,
duration: settings.long_break_duration as f32,
};
} else {
*phase = Phase::Break {
duration: settings.short_break_duration,
duration: settings.short_break_duration as f32,
};
}
}

View File

@@ -1,5 +0,0 @@
use crate::prelude::*;
pub const NORMAL_BUTTON: Color = Color::srgb(0.15, 0.15, 0.15);
pub const HOVERED_BUTTON: Color = Color::srgb(0.25, 0.25, 0.25);
pub const PRESSED_BUTTON: Color = Color::srgb(0.35, 0.75, 0.35);

View File

@@ -1,10 +1,8 @@
use crate::{features::savegame::messages::SavegameLoadMessage, prelude::*};
use components::*;
use consts::*;
use ui::*;
pub mod components;
pub mod consts;
pub mod ui;
pub struct StartScreenPlugin;

View File

@@ -1,4 +1,4 @@
use super::super::{components::*, consts::*};
use super::super::components::*;
use crate::prelude::*;
pub fn spawn_load_popup(commands: &mut Commands) {

View File

@@ -1 +1,6 @@
use crate::prelude::*;
pub const LINE_HEIGHT: f32 = 21.0;
pub const NORMAL_BUTTON: Color = Color::srgb(0.15, 0.15, 0.15);
pub const HOVERED_BUTTON: Color = Color::srgb(0.25, 0.25, 0.25);
pub const PRESSED_BUTTON: Color = Color::srgb(0.35, 0.75, 0.35);