feat: Replace old popup with new UI element

This commit is contained in:
demenik
2025-12-09 13:21:51 +01:00
parent c50601c23a
commit d9a1c9c2a7
11 changed files with 115 additions and 322 deletions

View File

@@ -15,7 +15,6 @@ pub enum TextType {
#[derive(Component)]
pub enum ButtonType {
SettingsOpen,
SettingsClose,
SettingsExit,
SettingsSave,
SettingsTimerChange {

View File

@@ -1,6 +1,5 @@
use crate::features::phase::components::TimerSettings;
use crate::features::savegame::messages::SavegameDumpMessage;
use crate::features::ui::messages::ClosePopupMessage;
use crate::features::{inventory, shop};
use crate::prelude::*;
use components::*;
@@ -17,27 +16,11 @@ impl Plugin for HudPlugin {
app.add_systems(OnExit(AppState::GameScreen), cleanup);
app.add_systems(
Update,
(update_status, buttons, update_timer_settings, close_popup)
.run_if(in_state(AppState::GameScreen)),
(update_status, buttons, update_timer_settings).run_if(in_state(AppState::GameScreen)),
);
}
}
fn close_popup(
mut commands: Commands,
mut close_popup_reader: MessageReader<ClosePopupMessage>,
root_query: Query<(Entity, &RootMarker)>,
) {
for _ in close_popup_reader.read() {
for (entity, root) in root_query.iter() {
match *root {
RootMarker::Settings => commands.entity(entity).despawn(),
_ => {}
}
}
}
}
fn setup(mut commands: Commands) {
commands.spawn((
RootMarker::Status,
@@ -106,7 +89,6 @@ 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>,
@@ -117,14 +99,6 @@ fn buttons(
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);

View File

@@ -199,5 +199,4 @@ pub enum RootMarker {
#[derive(Component)]
pub enum ButtonType {
InventoryOpen,
InventoryClose,
}

View File

@@ -1,4 +1,3 @@
use crate::features::ui::messages::ClosePopupMessage;
use crate::{features::inventory::ui::open_inventory, prelude::*};
use components::*;
@@ -11,35 +10,17 @@ impl Plugin for InventoryPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<Inventory>();
app.add_systems(
Update,
(buttons, close_popup).run_if(in_state(AppState::GameScreen)),
);
app.add_systems(Update, buttons.run_if(in_state(AppState::GameScreen)));
#[cfg(debug_assertions)]
app.add_systems(Update, debug_modify_berries);
}
}
fn close_popup(
mut commands: Commands,
mut close_popup_reader: MessageReader<ClosePopupMessage>,
root_query: Query<(Entity, &RootMarker)>,
) {
for _ in close_popup_reader.read() {
for (entity, root) in root_query.iter() {
match *root {
RootMarker::Inventory => commands.entity(entity).despawn(),
}
}
}
}
fn buttons(
mut commands: Commands,
mut interaction_query: Query<(&Interaction, &ButtonType), (Changed<Interaction>, With<Button>)>,
itemstack_query: Query<&ItemStack>,
root_query: Query<(Entity, &RootMarker)>,
game_config: Res<GameConfig>,
asset_server: Res<AssetServer>,
) {
@@ -49,13 +30,6 @@ fn buttons(
ButtonType::InventoryOpen => {
open_inventory(&mut commands, itemstack_query, &game_config, &asset_server);
}
ButtonType::InventoryClose => {
for (entity, root) in root_query.iter() {
match *root {
RootMarker::Inventory => commands.entity(entity).despawn(),
}
}
}
},
_ => {}
}

View File

@@ -1,4 +1,4 @@
use super::super::components::{ButtonType, RootMarker};
use super::super::components::RootMarker;
use crate::prelude::GameConfig;
use crate::{features::inventory::ui::list_itemstack, prelude::*};
@@ -8,52 +8,12 @@ pub fn open_inventory(
game_config: &Res<GameConfig>,
asset_server: &Res<AssetServer>,
) {
commands
.spawn((
spawn_popup(
commands,
RootMarker::Inventory,
Node {
position_type: PositionType::Absolute,
width: percent(100),
height: percent(100),
..Node::center()
},
ZIndex(1),
BackgroundColor(Color::srgba(0.0, 0.0, 0.0, 0.8)),
GlobalTransform::default(),
))
.with_children(|parent| {
parent
.spawn((
Node {
padding: UiRect::all(px(20.0)),
..Node::vstack(px(0))
},
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,
margin: UiRect::bottom(px(20.0)),
..Node::hstack(px(20))
},
children![
text("Inventar", 40.0, Color::WHITE),
pill_button(
ButtonType::InventoryClose,
ButtonVariant::Destructive,
Node {
width: px(40),
height: px(40),
..default()
},
|color| text("X", 24.0, color)
),
],
));
"Inventar",
Node::default(),
|parent| {
parent
.spawn(Node {
width: percent(100),
@@ -61,10 +21,13 @@ pub fn open_inventory(
..Node::vstack(px(10))
})
.with_children(|parent| {
for itemstack in items.iter() {
parent.spawn(list_itemstack(itemstack, game_config, asset_server));
}
});
});
});
items
.iter()
.map(|item| list_itemstack(item, game_config, asset_server))
.for_each(|itemstack| {
parent.spawn(itemstack);
});
});
},
);
}

View File

@@ -102,5 +102,4 @@ pub enum RootMarker {
pub enum ButtonType {
SavegameLoad { savegame_path: SavegamePath },
SavegameDelete { savegame_path: SavegamePath },
PopupClose,
}

View File

@@ -1,6 +1,5 @@
use crate::features::phase::components::{SessionTracker, TimerSettings};
use crate::features::savegame::ui::load_popup_handler;
use crate::features::ui::messages::ClosePopupMessage;
use crate::prelude::*;
use components::*;
use messages::*;
@@ -21,21 +20,7 @@ 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, close_popup));
}
}
fn close_popup(
mut commands: Commands,
mut close_popup_reader: MessageReader<ClosePopupMessage>,
root_query: Query<(Entity, &RootMarker)>,
) {
for _ in close_popup_reader.read() {
for (entity, root) in root_query.iter() {
match *root {
RootMarker::PopupSavegameLoad => commands.entity(entity).despawn(),
}
}
app.add_systems(Update, load_popup_handler);
}
}

View File

@@ -2,83 +2,46 @@ use super::super::components::{ButtonType, RootMarker};
use crate::{features::savegame::messages::SavegameLoadMessage, prelude::*};
pub fn spawn_load_popup(commands: &mut Commands) {
commands
.spawn((
spawn_popup(
commands,
RootMarker::PopupSavegameLoad,
"Spielstand wählen",
Node {
position_type: PositionType::Absolute,
width: percent(100),
height: percent(100),
..Node::center()
},
ZIndex(1),
BackgroundColor(Color::srgba(0.0, 0.0, 0.0, 0.8)),
GlobalTransform::default(),
))
.with_children(|parent| {
parent
.spawn((
Node {
width: px(600.0),
height: px(500.0),
padding: UiRect::all(px(20.0)),
align_items: AlignItems::Center,
..Node::vstack(px(10))
},
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)),
width: px(600),
height: px(500),
..default()
},
children![
text("Spielstand Auswahl", 40.0, Color::WHITE),
pill_button(
ButtonType::PopupClose,
ButtonVariant::Destructive,
Node {
width: px(40),
height: px(40),
..default()
},
|color| text("X", 24.0, color)
)
],
));
|parent| {
parent
.spawn(Node {
width: percent(100),
flex_direction: FlexDirection::Column,
overflow: Overflow::scroll_y(),
margin: UiRect::all(px(20.0)),
row_gap: px(10.0),
padding: UiRect::all(px(10)),
row_gap: px(10),
..default()
})
.with_children(|parent| {
for savegame in SavegamePath::list() {
parent.spawn(
button(
ButtonType::SavegameLoad { savegame_path: savegame.path.clone() },
parent.spawn(button(
ButtonType::SavegameLoad {
savegame_path: savegame.path.clone(),
},
ButtonVariant::Secondary,
Node {
width: percent(100),
padding: UiRect::all(px(10)),
..Node::center()
},
|color| (
|color| {
(
Node {
width: percent(100),
align_items: AlignItems::Center,
..Node::hstack(px(10))
},
children![(
children![
(
Node {
width: percent(100),
height: percent(100),
@@ -114,14 +77,15 @@ pub fn spawn_load_popup(commands: &mut Commands) {
..default()
},
|color| text("X", 24.0, color)
)]
)
),
);
],
)
},
));
}
});
});
});
},
);
}
pub fn load_popup_handler(
@@ -135,7 +99,6 @@ pub fn load_popup_handler(
match *interaction {
Interaction::Pressed => {
match button_type {
ButtonType::PopupClose => {}
ButtonType::SavegameLoad { savegame_path } => {
commands.insert_resource(savegame_path.clone());
next_state.set(AppState::GameScreen);

View File

@@ -8,7 +8,6 @@ pub enum RootMarker {
#[derive(Component)]
pub enum ButtonType {
ShopOpen,
ShopClose,
ShopBuyItem(ShopOffer),
}
@@ -71,4 +70,3 @@ impl ShopOffer {
}
}
}

View File

@@ -1,4 +1,3 @@
use crate::features::ui::messages::ClosePopupMessage;
use crate::prelude::*;
use components::*;
use ui::open_shop;
@@ -10,24 +9,7 @@ pub struct ShopPlugin;
impl Plugin for ShopPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
Update,
(buttons, close_popup).run_if(in_state(AppState::GameScreen)),
);
}
}
fn close_popup(
mut commands: Commands,
mut close_popup_reader: MessageReader<ClosePopupMessage>,
root_query: Query<(Entity, &RootMarker)>,
) {
for _ in close_popup_reader.read() {
for (entity, root) in root_query.iter() {
match *root {
RootMarker::Shop => commands.entity(entity).despawn(),
}
}
app.add_systems(Update, buttons.run_if(in_state(AppState::GameScreen)));
}
}
@@ -46,13 +28,6 @@ fn buttons(
ButtonType::ShopOpen => {
open_shop(&mut commands, &game_config, &asset_server);
}
ButtonType::ShopClose => {
for (entity, root) in root_query.iter() {
match *root {
RootMarker::Shop => commands.entity(entity).despawn(),
}
}
}
ButtonType::ShopBuyItem(offer) => {
if offer.buy(&mut inventory, &mut commands, &mut items) {
// Item bought, exit the menu

View File

@@ -9,56 +9,20 @@ pub fn open_shop(
// TODO: calculate tile_count
let offers = ShopOffer::list_all(game_config, 0);
commands
.spawn((
spawn_popup(
commands,
RootMarker::Shop,
Node {
position_type: PositionType::Absolute,
width: percent(100),
height: percent(100),
..Node::center()
},
ZIndex(1),
BackgroundColor(Color::srgba(0.0, 0.0, 0.0, 0.8)),
GlobalTransform::default(),
))
.with_children(|parent| {
parent
.spawn((
"Einkaufsladen",
Node {
width: px(700),
padding: UiRect::all(px(20.0)),
..Node::vstack(px(20))
},
BackgroundColor(Color::srgb(0.2, 0.2, 0.2)),
BorderRadius::all(px(10.0)),
))
.with_children(|parent| {
parent.spawn((
Node {
justify_content: JustifyContent::SpaceBetween,
..Node::hstack(px(20))
},
children![
text("Shop", 40.0, Color::WHITE),
pill_button(
ButtonType::ShopClose,
ButtonVariant::Destructive,
Node {
width: px(40),
height: px(40),
..default()
},
|color| text("X", 24.0, color)
),
],
));
|parent| {
parent.spawn(Node::vstack(px(10))).with_children(|parent| {
for offer in offers {
parent.spawn(shop_offer(&offer, game_config, asset_server));
}
});
});
});
},
);
}