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)
}
}
#[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::savegame::ui::load_popup_handler;
use crate::prelude::*;
use messages::*;
use std::fs::File;
@@ -6,6 +7,7 @@ use std::io::{Read, Write};
pub mod components;
pub mod messages;
pub mod ui;
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, 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 crate::prelude::*;
use super::super::components::{ButtonType, RootMarker};
use crate::{features::savegame::messages::SavegameLoadMessage, prelude::*};
pub fn spawn_load_popup(commands: &mut Commands) {
commands
@@ -78,7 +78,7 @@ pub fn spawn_load_popup(commands: &mut Commands) {
for savegame in SavegamePath::list() {
parent.spawn((
Button,
ButtonType::PopupSavegameLoad {
ButtonType::SavegameLoad {
savegame_path: savegame.path.clone(),
},
Node {
@@ -123,7 +123,7 @@ pub fn spawn_load_popup(commands: &mut Commands) {
),
(
Button,
ButtonType::PopupSavegameDelete {
ButtonType::SavegameDelete {
savegame_path: savegame.path.clone()
},
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)]
pub enum RootMarker {
MainMenu,
PopupSavegameLoad,
}
#[derive(Component)]
@@ -11,7 +10,4 @@ pub enum ButtonType {
LoadGame,
NewGame,
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 ui::*;
pub mod components;
pub mod ui;
pub struct StartScreenPlugin;
@@ -80,8 +79,6 @@ fn menu(
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 {
@@ -94,31 +91,6 @@ fn menu(
commands.insert_resource(SavegamePath::next());
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!(),
};
}