refactor: Move load savegame popup to savegame feature

This commit is contained in:
demenik
2025-11-28 12:37:57 +01:00
parent cdcb8c08d4
commit a2566f3643
6 changed files with 57 additions and 38 deletions

View File

@@ -92,3 +92,15 @@ impl SavegamePath {
Self::new(next_index) Self::new(next_index)
} }
} }
#[derive(Component)]
pub enum RootMarker {
PopupSavegameLoad,
}
#[derive(Component)]
pub enum ButtonType {
SavegameLoad { savegame_path: SavegamePath },
SavegameDelete { savegame_path: SavegamePath },
PopupClose,
}

View File

@@ -1,4 +1,5 @@
use crate::features::phase::components::{SessionTracker, TimerSettings}; use crate::features::phase::components::{SessionTracker, TimerSettings};
use crate::features::savegame::ui::load_popup_handler;
use crate::prelude::*; use crate::prelude::*;
use messages::*; use messages::*;
use std::fs::File; use std::fs::File;
@@ -6,6 +7,7 @@ use std::io::{Read, Write};
pub mod components; pub mod components;
pub mod messages; pub mod messages;
pub mod ui;
pub struct SavegamePlugin; pub struct SavegamePlugin;
@@ -16,6 +18,8 @@ impl Plugin for SavegamePlugin {
app.add_systems(Update, dump_savegame.run_if(in_state(AppState::GameScreen))); app.add_systems(Update, dump_savegame.run_if(in_state(AppState::GameScreen)));
app.add_systems(Update, load_savegame.run_if(in_state(AppState::GameScreen))); app.add_systems(Update, load_savegame.run_if(in_state(AppState::GameScreen)));
app.add_systems(Update, load_popup_handler);
} }
} }

View File

@@ -1,5 +1,5 @@
use super::super::components::*; use super::super::components::{ButtonType, RootMarker};
use crate::prelude::*; use crate::{features::savegame::messages::SavegameLoadMessage, prelude::*};
pub fn spawn_load_popup(commands: &mut Commands) { pub fn spawn_load_popup(commands: &mut Commands) {
commands commands
@@ -78,7 +78,7 @@ pub fn spawn_load_popup(commands: &mut Commands) {
for savegame in SavegamePath::list() { for savegame in SavegamePath::list() {
parent.spawn(( parent.spawn((
Button, Button,
ButtonType::PopupSavegameLoad { ButtonType::SavegameLoad {
savegame_path: savegame.path.clone(), savegame_path: savegame.path.clone(),
}, },
Node { Node {
@@ -123,7 +123,7 @@ pub fn spawn_load_popup(commands: &mut Commands) {
), ),
( (
Button, Button,
ButtonType::PopupSavegameDelete { ButtonType::SavegameDelete {
savegame_path: savegame.path.clone() savegame_path: savegame.path.clone()
}, },
Node { Node {
@@ -146,3 +146,38 @@ pub fn spawn_load_popup(commands: &mut Commands) {
}); });
}); });
} }
pub fn load_popup_handler(
mut commands: Commands,
mut next_state: ResMut<NextState<AppState>>,
mut interaction_query: Query<(&Interaction, &ButtonType), (Changed<Interaction>, With<Button>)>,
root_query: Query<(Entity, &RootMarker)>,
mut savegame_messages: MessageWriter<SavegameLoadMessage>,
) {
for (interaction, button_type) in &mut interaction_query {
match *interaction {
Interaction::Pressed => {
match button_type {
ButtonType::PopupClose => {}
ButtonType::SavegameLoad { savegame_path } => {
commands.insert_resource(savegame_path.clone());
next_state.set(AppState::GameScreen);
savegame_messages.write(SavegameLoadMessage);
}
ButtonType::SavegameDelete { savegame_path } => {
if let Err(e) = std::fs::remove_file(savegame_path.clone().0) {
println!("Error while deleting savegame: {:?}", e);
}
}
};
for (entity, root) in root_query.iter() {
match *root {
RootMarker::PopupSavegameLoad => commands.entity(entity).despawn(),
}
}
}
_ => (),
}
}
}

View File

@@ -3,7 +3,6 @@ use crate::prelude::*;
#[derive(Component)] #[derive(Component)]
pub enum RootMarker { pub enum RootMarker {
MainMenu, MainMenu,
PopupSavegameLoad,
} }
#[derive(Component)] #[derive(Component)]
@@ -11,7 +10,4 @@ pub enum ButtonType {
LoadGame, LoadGame,
NewGame, NewGame,
Settings, Settings,
PopupSavegameLoad { savegame_path: SavegamePath },
PopupSavegameDelete { savegame_path: SavegamePath },
PopupClose,
} }

View File

@@ -1,9 +1,8 @@
use crate::{features::savegame::messages::SavegameLoadMessage, prelude::*}; use crate::features::savegame::ui::spawn_load_popup;
use crate::prelude::*;
use components::*; use components::*;
use ui::*;
pub mod components; pub mod components;
pub mod ui;
pub struct StartScreenPlugin; pub struct StartScreenPlugin;
@@ -80,8 +79,6 @@ fn menu(
mut commands: Commands, mut commands: Commands,
mut next_state: ResMut<NextState<AppState>>, mut next_state: ResMut<NextState<AppState>>,
mut interaction_query: Query<(&Interaction, &ButtonType), (Changed<Interaction>, With<Button>)>, mut interaction_query: Query<(&Interaction, &ButtonType), (Changed<Interaction>, With<Button>)>,
root_query: Query<(Entity, &RootMarker)>,
mut savegame_messages: MessageWriter<SavegameLoadMessage>,
) { ) {
for (interaction, button_type) in &mut interaction_query { for (interaction, button_type) in &mut interaction_query {
match *interaction { match *interaction {
@@ -94,31 +91,6 @@ fn menu(
commands.insert_resource(SavegamePath::next()); commands.insert_resource(SavegamePath::next());
next_state.set(AppState::GameScreen); next_state.set(AppState::GameScreen);
} }
ButtonType::PopupClose => {
for (entity, root) in root_query.iter() {
match *root {
RootMarker::PopupSavegameLoad => commands.entity(entity).despawn(),
_ => {}
}
}
}
ButtonType::PopupSavegameLoad { savegame_path } => {
commands.insert_resource(savegame_path.clone());
next_state.set(AppState::GameScreen);
savegame_messages.write(SavegameLoadMessage);
}
ButtonType::PopupSavegameDelete { savegame_path } => {
if let Err(e) = std::fs::remove_file(savegame_path.clone().0) {
println!("Error while deleting savegame: {:?}", e);
}
for (entity, root) in root_query.iter() {
match *root {
RootMarker::PopupSavegameLoad => commands.entity(entity).despawn(),
_ => {}
}
}
}
ButtonType::Settings => todo!(), ButtonType::Settings => todo!(),
}; };
} }